org.el 539 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559
  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.12b
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.12b"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-modules'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (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)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global id's for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " mouse: Additional mouse support" org-mouse)
  155. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  156. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  157. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  158. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  159. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  160. (const :tag "C eval: Include command output as text" org-eval)
  161. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  162. (const :tag "C id: Global id's for identifying entries" org-id)
  163. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  164. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  165. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  166. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  167. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  168. (const :tag "C registry: A registry for Org links" org-registry)
  169. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  170. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  171. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  172. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  173. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  174. (defgroup org-startup nil
  175. "Options concerning startup of Org-mode."
  176. :tag "Org Startup"
  177. :group 'org)
  178. (defcustom org-startup-folded t
  179. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  180. This can also be configured on a per-file basis by adding one of
  181. the following lines anywhere in the buffer:
  182. #+STARTUP: fold
  183. #+STARTUP: nofold
  184. #+STARTUP: content"
  185. :group 'org-startup
  186. :type '(choice
  187. (const :tag "nofold: show all" nil)
  188. (const :tag "fold: overview" t)
  189. (const :tag "content: all headlines" content)))
  190. (defcustom org-startup-truncated t
  191. "Non-nil means, entering Org-mode will set `truncate-lines'.
  192. This is useful since some lines containing links can be very long and
  193. uninteresting. Also tables look terrible when wrapped."
  194. :group 'org-startup
  195. :type 'boolean)
  196. (defcustom org-startup-align-all-tables nil
  197. "Non-nil means, align all tables when visiting a file.
  198. This is useful when the column width in tables is forced with <N> cookies
  199. in table fields. Such tables will look correct only after the first re-align.
  200. This can also be configured on a per-file basis by adding one of
  201. the following lines anywhere in the buffer:
  202. #+STARTUP: align
  203. #+STARTUP: noalign"
  204. :group 'org-startup
  205. :type 'boolean)
  206. (defcustom org-insert-mode-line-in-empty-file nil
  207. "Non-nil means insert the first line setting Org-mode in empty files.
  208. When the function `org-mode' is called interactively in an empty file, this
  209. normally means that the file name does not automatically trigger Org-mode.
  210. To ensure that the file will always be in Org-mode in the future, a
  211. line enforcing Org-mode will be inserted into the buffer, if this option
  212. has been set."
  213. :group 'org-startup
  214. :type 'boolean)
  215. (defcustom org-replace-disputed-keys nil
  216. "Non-nil means use alternative key bindings for some keys.
  217. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  218. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  219. If you want to use Org-mode together with one of these other modes,
  220. or more generally if you would like to move some Org-mode commands to
  221. other keys, set this variable and configure the keys with the variable
  222. `org-disputed-keys'.
  223. This option is only relevant at load-time of Org-mode, and must be set
  224. *before* org.el is loaded. Changing it requires a restart of Emacs to
  225. become effective."
  226. :group 'org-startup
  227. :type 'boolean)
  228. (defcustom org-use-extra-keys nil
  229. "Non-nil means use extra key sequence definitions for certain
  230. commands. This happens automatically if you run XEmacs or if
  231. window-system is nil. This variable lets you do the same
  232. manually. You must set it before loading org.
  233. Example: on Carbon Emacs 22 running graphically, with an external
  234. keyboard on a Powerbook, the default way of setting M-left might
  235. not work for either Alt or ESC. Setting this variable will make
  236. it work for ESC."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (if (fboundp 'defvaralias)
  240. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  241. (defcustom org-disputed-keys
  242. '(([(shift up)] . [(meta p)])
  243. ([(shift down)] . [(meta n)])
  244. ([(shift left)] . [(meta -)])
  245. ([(shift right)] . [(meta +)])
  246. ([(control shift right)] . [(meta shift +)])
  247. ([(control shift left)] . [(meta shift -)]))
  248. "Keys for which Org-mode and other modes compete.
  249. This is an alist, cars are the default keys, second element specifies
  250. the alternative to use when `org-replace-disputed-keys' is t.
  251. Keys can be specified in any syntax supported by `define-key'.
  252. The value of this option takes effect only at Org-mode's startup,
  253. therefore you'll have to restart Emacs to apply it after changing."
  254. :group 'org-startup
  255. :type 'alist)
  256. (defun org-key (key)
  257. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  258. Or return the original if not disputed."
  259. (if org-replace-disputed-keys
  260. (let* ((nkey (key-description key))
  261. (x (org-find-if (lambda (x)
  262. (equal (key-description (car x)) nkey))
  263. org-disputed-keys)))
  264. (if x (cdr x) key))
  265. key))
  266. (defun org-find-if (predicate seq)
  267. (catch 'exit
  268. (while seq
  269. (if (funcall predicate (car seq))
  270. (throw 'exit (car seq))
  271. (pop seq)))))
  272. (defun org-defkey (keymap key def)
  273. "Define a key, possibly translated, as returned by `org-key'."
  274. (define-key keymap (org-key key) def))
  275. (defcustom org-ellipsis nil
  276. "The ellipsis to use in the Org-mode outline.
  277. When nil, just use the standard three dots. When a string, use that instead,
  278. When a face, use the standart 3 dots, but with the specified face.
  279. The change affects only Org-mode (which will then use its own display table).
  280. Changing this requires executing `M-x org-mode' in a buffer to become
  281. effective."
  282. :group 'org-startup
  283. :type '(choice (const :tag "Default" nil)
  284. (face :tag "Face" :value org-warning)
  285. (string :tag "String" :value "...#")))
  286. (defvar org-display-table nil
  287. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  288. (defgroup org-keywords nil
  289. "Keywords in Org-mode."
  290. :tag "Org Keywords"
  291. :group 'org)
  292. (defcustom org-deadline-string "DEADLINE:"
  293. "String to mark deadline entries.
  294. A deadline is this string, followed by a time stamp. Should be a word,
  295. terminated by a colon. You can insert a schedule keyword and
  296. a timestamp with \\[org-deadline].
  297. Changes become only effective after restarting Emacs."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-scheduled-string "SCHEDULED:"
  301. "String to mark scheduled TODO entries.
  302. A schedule is this string, followed by a time stamp. Should be a word,
  303. terminated by a colon. You can insert a schedule keyword and
  304. a timestamp with \\[org-schedule].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-closed-string "CLOSED:"
  309. "String used as the prefix for timestamps logging closing a TODO entry."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-clock-string "CLOCK:"
  313. "String used as prefix for timestamps clocking work hours on an item."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-comment-string "COMMENT"
  317. "Entries starting with this keyword will never be exported.
  318. An entry can be toggled between COMMENT and normal with
  319. \\[org-toggle-comment].
  320. Changes become only effective after restarting Emacs."
  321. :group 'org-keywords
  322. :type 'string)
  323. (defcustom org-quote-string "QUOTE"
  324. "Entries starting with this keyword will be exported in fixed-width font.
  325. Quoting applies only to the text in the entry following the headline, and does
  326. not extend beyond the next headline, even if that is lower level.
  327. An entry can be toggled between QUOTE and normal with
  328. \\[org-toggle-fixed-width-section]."
  329. :group 'org-keywords
  330. :type 'string)
  331. (defconst org-repeat-re
  332. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  333. "Regular expression for specifying repeated events.
  334. After a match, group 1 contains the repeat expression.")
  335. (defgroup org-structure nil
  336. "Options concerning the general structure of Org-mode files."
  337. :tag "Org Structure"
  338. :group 'org)
  339. (defgroup org-reveal-location nil
  340. "Options about how to make context of a location visible."
  341. :tag "Org Reveal Location"
  342. :group 'org-structure)
  343. (defconst org-context-choice
  344. '(choice
  345. (const :tag "Always" t)
  346. (const :tag "Never" nil)
  347. (repeat :greedy t :tag "Individual contexts"
  348. (cons
  349. (choice :tag "Context"
  350. (const agenda)
  351. (const org-goto)
  352. (const occur-tree)
  353. (const tags-tree)
  354. (const link-search)
  355. (const mark-goto)
  356. (const bookmark-jump)
  357. (const isearch)
  358. (const default))
  359. (boolean))))
  360. "Contexts for the reveal options.")
  361. (defcustom org-show-hierarchy-above '((default . t))
  362. "Non-nil means, show full hierarchy when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the hierarchy of headings
  365. above the exposed location is shown.
  366. Turning this off for example for sparse trees makes them very compact.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. Valid contexts are
  369. agenda when exposing an entry from the agenda
  370. org-goto when using the command `org-goto' on key C-c C-j
  371. occur-tree when using the command `org-occur' on key C-c /
  372. tags-tree when constructing a sparse tree based on tags matches
  373. link-search when exposing search matches associated with a link
  374. mark-goto when exposing the jump goal of a mark
  375. bookmark-jump when exposing a bookmark location
  376. isearch when exiting from an incremental search
  377. default default for all contexts not set explicitly"
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-following-heading '((default . nil))
  381. "Non-nil means, show following heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the heading following the
  384. match is shown.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-siblings '((default . nil) (isearch t))
  393. "Non-nil means, show all sibling heading when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the sibling of the current entry
  396. heading are all made visible. If `org-show-hierarchy-above' is t,
  397. the same happens on each level of the hierarchy above the current entry.
  398. By default this is on for the isearch context, off for all other contexts.
  399. Turning this off for example for sparse trees makes them very compact,
  400. but makes it harder to edit the location of the match. In such a case,
  401. use the command \\[org-reveal] to show more context.
  402. Instead of t, this can also be an alist specifying this option for different
  403. contexts. See `org-show-hierarchy-above' for valid contexts."
  404. :group 'org-reveal-location
  405. :type org-context-choice)
  406. (defcustom org-show-entry-below '((default . nil))
  407. "Non-nil means, show the entry below a headline when revealing a location.
  408. Org-mode often shows locations in an org-mode file which might have
  409. been invisible before. When this is set, the text below the headline that is
  410. exposed is also shown.
  411. By default this is off for all contexts.
  412. Instead of t, this can also be an alist specifying this option for different
  413. contexts. See `org-show-hierarchy-above' for valid contexts."
  414. :group 'org-reveal-location
  415. :type org-context-choice)
  416. (defcustom org-indirect-buffer-display 'other-window
  417. "How should indirect tree buffers be displayed?
  418. This applies to indirect buffers created with the commands
  419. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  420. Valid values are:
  421. current-window Display in the current window
  422. other-window Just display in another window.
  423. dedicated-frame Create one new frame, and re-use it each time.
  424. new-frame Make a new frame each time. Note that in this case
  425. previously-made indirect buffers are kept, and you need to
  426. kill these buffers yourself."
  427. :group 'org-structure
  428. :group 'org-agenda-windows
  429. :type '(choice
  430. (const :tag "In current window" current-window)
  431. (const :tag "In current frame, other window" other-window)
  432. (const :tag "Each time a new frame" new-frame)
  433. (const :tag "One dedicated frame" dedicated-frame)))
  434. (defgroup org-cycle nil
  435. "Options concerning visibility cycling in Org-mode."
  436. :tag "Org Cycle"
  437. :group 'org-structure)
  438. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  439. "Names of drawers. Drawers are not opened by cycling on the headline above.
  440. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  441. this:
  442. :DRAWERNAME:
  443. .....
  444. :END:
  445. The drawer \"PROPERTIES\" is special for capturing properties through
  446. the property API.
  447. Drawers can be defined on the per-file basis with a line like:
  448. #+DRAWERS: HIDDEN STATE PROPERTIES"
  449. :group 'org-structure
  450. :type '(repeat (string :tag "Drawer Name")))
  451. (defcustom org-cycle-global-at-bob nil
  452. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  453. This makes it possible to do global cycling without having to use S-TAB or
  454. C-u TAB. For this special case to work, the first line of the buffer
  455. must not be a headline - it may be empty ot some other text. When used in
  456. this way, `org-cycle-hook' is disables temporarily, to make sure the
  457. cursor stays at the beginning of the buffer.
  458. When this option is nil, don't do anything special at the beginning
  459. of the buffer."
  460. :group 'org-cycle
  461. :type 'boolean)
  462. (defcustom org-cycle-emulate-tab t
  463. "Where should `org-cycle' emulate TAB.
  464. nil Never
  465. white Only in completely white lines
  466. whitestart Only at the beginning of lines, before the first non-white char
  467. t Everywhere except in headlines
  468. exc-hl-bol Everywhere except at the start of a headline
  469. If TAB is used in a place where it does not emulate TAB, the current subtree
  470. visibility is cycled."
  471. :group 'org-cycle
  472. :type '(choice (const :tag "Never" nil)
  473. (const :tag "Only in completely white lines" white)
  474. (const :tag "Before first char in a line" whitestart)
  475. (const :tag "Everywhere except in headlines" t)
  476. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  477. ))
  478. (defcustom org-cycle-separator-lines 2
  479. "Number of empty lines needed to keep an empty line between collapsed trees.
  480. If you leave an empty line between the end of a subtree and the following
  481. headline, this empty line is hidden when the subtree is folded.
  482. Org-mode will leave (exactly) one empty line visible if the number of
  483. empty lines is equal or larger to the number given in this variable.
  484. So the default 2 means, at least 2 empty lines after the end of a subtree
  485. are needed to produce free space between a collapsed subtree and the
  486. following headline.
  487. Special case: when 0, never leave empty lines in collapsed view."
  488. :group 'org-cycle
  489. :type 'integer)
  490. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  491. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  492. org-cycle-hide-drawers
  493. org-cycle-show-empty-lines
  494. org-optimize-window-after-visibility-change)
  495. "Hook that is run after `org-cycle' has changed the buffer visibility.
  496. The function(s) in this hook must accept a single argument which indicates
  497. the new state that was set by the most recent `org-cycle' command. The
  498. argument is a symbol. After a global state change, it can have the values
  499. `overview', `content', or `all'. After a local state change, it can have
  500. the values `folded', `children', or `subtree'."
  501. :group 'org-cycle
  502. :type 'hook)
  503. (defgroup org-edit-structure nil
  504. "Options concerning structure editing in Org-mode."
  505. :tag "Org Edit Structure"
  506. :group 'org-structure)
  507. (defcustom org-odd-levels-only nil
  508. "Non-nil means, skip even levels and only use odd levels for the outline.
  509. This has the effect that two stars are being added/taken away in
  510. promotion/demotion commands. It also influences how levels are
  511. handled by the exporters.
  512. Changing it requires restart of `font-lock-mode' to become effective
  513. for fontification also in regions already fontified.
  514. You may also set this on a per-file basis by adding one of the following
  515. lines to the buffer:
  516. #+STARTUP: odd
  517. #+STARTUP: oddeven"
  518. :group 'org-edit-structure
  519. :group 'org-font-lock
  520. :type 'boolean)
  521. (defcustom org-adapt-indentation t
  522. "Non-nil means, adapt indentation when promoting and demoting.
  523. When this is set and the *entire* text in an entry is indented, the
  524. indentation is increased by one space in a demotion command, and
  525. decreased by one in a promotion command. If any line in the entry
  526. body starts at column 0, indentation is not changed at all."
  527. :group 'org-edit-structure
  528. :type 'boolean)
  529. (defcustom org-special-ctrl-a/e nil
  530. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  531. When t, `C-a' will bring back the cursor to the beginning of the
  532. headline text, i.e. after the stars and after a possible TODO keyword.
  533. In an item, this will be the position after the bullet.
  534. When the cursor is already at that position, another `C-a' will bring
  535. it to the beginning of the line.
  536. `C-e' will jump to the end of the headline, ignoring the presence of tags
  537. in the headline. A second `C-e' will then jump to the true end of the
  538. line, after any tags.
  539. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  540. and only a directly following, identical keypress will bring the cursor
  541. to the special positions."
  542. :group 'org-edit-structure
  543. :type '(choice
  544. (const :tag "off" nil)
  545. (const :tag "after bullet first" t)
  546. (const :tag "border first" reversed)))
  547. (if (fboundp 'defvaralias)
  548. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  549. (defcustom org-special-ctrl-k nil
  550. "Non-nil means `C-k' will behave specially in headlines.
  551. When nil, `C-k' will call the default `kill-line' command.
  552. When t, the following will happen while the cursor is in the headline:
  553. - When the cursor is at the beginning of a headline, kill the entire
  554. line and possible the folded subtree below the line.
  555. - When in the middle of the headline text, kill the headline up to the tags.
  556. - When after the headline text, kill the tags."
  557. :group 'org-edit-structure
  558. :type 'boolean)
  559. (defcustom org-yank-folded-subtrees t
  560. "Non-nil means, when yanking subtrees, fold them.
  561. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  562. it starts with a heading and all other headings in it are either children
  563. or siblings, then fold all the subtrees. However, do this only if no
  564. text after the yank would be swallowed into a folded tree by this action."
  565. :group 'org-edit-structure
  566. :type 'boolean)
  567. (defcustom org-yank-adjusted-subtrees t
  568. "Non-nil means, when yanking subtrees, adjust the level.
  569. With this setting, `org-paste-subtree' is used to insert the subtree, see
  570. this function for details."
  571. :group 'org-edit-structure
  572. :type 'boolean)
  573. (defcustom org-M-RET-may-split-line '((default . t))
  574. "Non-nil means, M-RET will split the line at the cursor position.
  575. When nil, it will go to the end of the line before making a
  576. new line.
  577. You may also set this option in a different way for different
  578. contexts. Valid contexts are:
  579. headline when creating a new headline
  580. item when creating a new item
  581. table in a table field
  582. default the value to be used for all contexts not explicitly
  583. customized"
  584. :group 'org-structure
  585. :group 'org-table
  586. :type '(choice
  587. (const :tag "Always" t)
  588. (const :tag "Never" nil)
  589. (repeat :greedy t :tag "Individual contexts"
  590. (cons
  591. (choice :tag "Context"
  592. (const headline)
  593. (const item)
  594. (const table)
  595. (const default))
  596. (boolean)))))
  597. (defcustom org-insert-heading-respect-content nil
  598. "Non-nil means, insert new headings after the current subtree.
  599. When nil, the new heading is created directly after the current line.
  600. The commands \\[org-insert-heading-respect-content] and
  601. \\[org-insert-todo-heading-respect-content] turn this variable on
  602. for the duration of the command."
  603. :group 'org-structure
  604. :type 'boolean)
  605. (defcustom org-blank-before-new-entry '((heading . nil)
  606. (plain-list-item . nil))
  607. "Should `org-insert-heading' leave a blank line before new heading/item?
  608. The value is an alist, with `heading' and `plain-list-item' as car,
  609. and a boolean flag as cdr."
  610. :group 'org-edit-structure
  611. :type '(list
  612. (cons (const heading) (boolean))
  613. (cons (const plain-list-item) (boolean))))
  614. (defcustom org-insert-heading-hook nil
  615. "Hook being run after inserting a new heading."
  616. :group 'org-edit-structure
  617. :type 'hook)
  618. (defcustom org-enable-fixed-width-editor t
  619. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  620. This currently only means, they are never auto-wrapped.
  621. When nil, such lines will be treated like ordinary lines.
  622. See also the QUOTE keyword."
  623. :group 'org-edit-structure
  624. :type 'boolean)
  625. (defcustom org-edit-src-region-extra nil
  626. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  627. For examples see the function `org-edit-src-find-region-and-lang'.
  628. The regular expression identifying the begin marker should end with a newline,
  629. and the regexp marking the end line should start with a newline, to make sure
  630. there are kept outside the narrowed region."
  631. :group 'org-edit-structure
  632. :type '(repeat
  633. (list
  634. (regexp :tag "begin regexp")
  635. (regexp :tag "end regexp")
  636. (choice :tag "language"
  637. (string :tag "specify")
  638. (integer :tag "from match group")
  639. (const :tag "from `lang' element")
  640. (const :tag "from `style' element")))))
  641. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  642. "The mode that should be used to edit fixed-width regions.
  643. These are the regions where each line starts with a colon."
  644. :group 'org-edit-structure
  645. :type '(choice
  646. (const artist-mode)
  647. (const picture-mode)
  648. (const fundamental-mode)
  649. (function :tag "Other (specify)")))
  650. (defcustom org-goto-auto-isearch t
  651. "Non-nil means, typing characters in org-goto starts incremental search."
  652. :group 'org-edit-structure
  653. :type 'boolean)
  654. (defgroup org-sparse-trees nil
  655. "Options concerning sparse trees in Org-mode."
  656. :tag "Org Sparse Trees"
  657. :group 'org-structure)
  658. (defcustom org-highlight-sparse-tree-matches t
  659. "Non-nil means, highlight all matches that define a sparse tree.
  660. The highlights will automatically disappear the next time the buffer is
  661. changed by an edit command."
  662. :group 'org-sparse-trees
  663. :type 'boolean)
  664. (defcustom org-remove-highlights-with-change t
  665. "Non-nil means, any change to the buffer will remove temporary highlights.
  666. Such highlights are created by `org-occur' and `org-clock-display'.
  667. When nil, `C-c C-c needs to be used to get rid of the highlights.
  668. The highlights created by `org-preview-latex-fragment' always need
  669. `C-c C-c' to be removed."
  670. :group 'org-sparse-trees
  671. :group 'org-time
  672. :type 'boolean)
  673. (defcustom org-occur-hook '(org-first-headline-recenter)
  674. "Hook that is run after `org-occur' has constructed a sparse tree.
  675. This can be used to recenter the window to show as much of the structure
  676. as possible."
  677. :group 'org-sparse-trees
  678. :type 'hook)
  679. (defgroup org-imenu-and-speedbar nil
  680. "Options concerning imenu and speedbar in Org-mode."
  681. :tag "Org Imenu and Speedbar"
  682. :group 'org-structure)
  683. (defcustom org-imenu-depth 2
  684. "The maximum level for Imenu access to Org-mode headlines.
  685. This also applied for speedbar access."
  686. :group 'org-imenu-and-speedbar
  687. :type 'number)
  688. (defgroup org-table nil
  689. "Options concerning tables in Org-mode."
  690. :tag "Org Table"
  691. :group 'org)
  692. (defcustom org-enable-table-editor 'optimized
  693. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  694. When nil, such lines will be treated like ordinary lines.
  695. When equal to the symbol `optimized', the table editor will be optimized to
  696. do the following:
  697. - Automatic overwrite mode in front of whitespace in table fields.
  698. This makes the structure of the table stay in tact as long as the edited
  699. field does not exceed the column width.
  700. - Minimize the number of realigns. Normally, the table is aligned each time
  701. TAB or RET are pressed to move to another field. With optimization this
  702. happens only if changes to a field might have changed the column width.
  703. Optimization requires replacing the functions `self-insert-command',
  704. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  705. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  706. very good at guessing when a re-align will be necessary, but you can always
  707. force one with \\[org-ctrl-c-ctrl-c].
  708. If you would like to use the optimized version in Org-mode, but the
  709. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  710. This variable can be used to turn on and off the table editor during a session,
  711. but in order to toggle optimization, a restart is required.
  712. See also the variable `org-table-auto-blank-field'."
  713. :group 'org-table
  714. :type '(choice
  715. (const :tag "off" nil)
  716. (const :tag "on" t)
  717. (const :tag "on, optimized" optimized)))
  718. (defcustom org-table-tab-recognizes-table.el t
  719. "Non-nil means, TAB will automatically notice a table.el table.
  720. When it sees such a table, it moves point into it and - if necessary -
  721. calls `table-recognize-table'."
  722. :group 'org-table-editing
  723. :type 'boolean)
  724. (defgroup org-link nil
  725. "Options concerning links in Org-mode."
  726. :tag "Org Link"
  727. :group 'org)
  728. (defvar org-link-abbrev-alist-local nil
  729. "Buffer-local version of `org-link-abbrev-alist', which see.
  730. The value of this is taken from the #+LINK lines.")
  731. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  732. (defcustom org-link-abbrev-alist nil
  733. "Alist of link abbreviations.
  734. The car of each element is a string, to be replaced at the start of a link.
  735. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  736. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  737. [[linkkey:tag][description]]
  738. If REPLACE is a string, the tag will simply be appended to create the link.
  739. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  740. the placeholder \"%h\" will cause a url-encoded version of the tag to
  741. be inserted at that point (see the function `url-hexify-string').
  742. REPLACE may also be a function that will be called with the tag as the
  743. only argument to create the link, which should be returned as a string.
  744. See the manual for examples."
  745. :group 'org-link
  746. :type '(repeat
  747. (cons
  748. (string :tag "Protocol")
  749. (choice
  750. (string :tag "Format")
  751. (function)))))
  752. (defcustom org-descriptive-links t
  753. "Non-nil means, hide link part and only show description of bracket links.
  754. Bracket links are like [[link][descritpion]]. This variable sets the initial
  755. state in new org-mode buffers. The setting can then be toggled on a
  756. per-buffer basis from the Org->Hyperlinks menu."
  757. :group 'org-link
  758. :type 'boolean)
  759. (defcustom org-link-file-path-type 'adaptive
  760. "How the path name in file links should be stored.
  761. Valid values are:
  762. relative Relative to the current directory, i.e. the directory of the file
  763. into which the link is being inserted.
  764. absolute Absolute path, if possible with ~ for home directory.
  765. noabbrev Absolute path, no abbreviation of home directory.
  766. adaptive Use relative path for files in the current directory and sub-
  767. directories of it. For other files, use an absolute path."
  768. :group 'org-link
  769. :type '(choice
  770. (const relative)
  771. (const absolute)
  772. (const noabbrev)
  773. (const adaptive)))
  774. (defcustom org-activate-links '(bracket angle plain radio tag date)
  775. "Types of links that should be activated in Org-mode files.
  776. This is a list of symbols, each leading to the activation of a certain link
  777. type. In principle, it does not hurt to turn on most link types - there may
  778. be a small gain when turning off unused link types. The types are:
  779. bracket The recommended [[link][description]] or [[link]] links with hiding.
  780. angular Links in angular brackes that may contain whitespace like
  781. <bbdb:Carsten Dominik>.
  782. plain Plain links in normal text, no whitespace, like http://google.com.
  783. radio Text that is matched by a radio target, see manual for details.
  784. tag Tag settings in a headline (link to tag search).
  785. date Time stamps (link to calendar).
  786. Changing this variable requires a restart of Emacs to become effective."
  787. :group 'org-link
  788. :type '(set (const :tag "Double bracket links (new style)" bracket)
  789. (const :tag "Angular bracket links (old style)" angular)
  790. (const :tag "Plain text links" plain)
  791. (const :tag "Radio target matches" radio)
  792. (const :tag "Tags" tag)
  793. (const :tag "Timestamps" date)))
  794. (defcustom org-make-link-description-function nil
  795. "Function to use to generate link descriptions from links. If
  796. nil the link location will be used. This function must take two
  797. parameters; the first is the link and the second the description
  798. org-insert-link has generated, and should return the description
  799. to use."
  800. :group 'org-link
  801. :type 'function)
  802. (defgroup org-link-store nil
  803. "Options concerning storing links in Org-mode."
  804. :tag "Org Store Link"
  805. :group 'org-link)
  806. (defcustom org-email-link-description-format "Email %c: %.30s"
  807. "Format of the description part of a link to an email or usenet message.
  808. The following %-excapes will be replaced by corresponding information:
  809. %F full \"From\" field
  810. %f name, taken from \"From\" field, address if no name
  811. %T full \"To\" field
  812. %t first name in \"To\" field, address if no name
  813. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  814. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  815. %s subject
  816. %m message-id.
  817. You may use normal field width specification between the % and the letter.
  818. This is for example useful to limit the length of the subject.
  819. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  820. :group 'org-link-store
  821. :type 'string)
  822. (defcustom org-from-is-user-regexp
  823. (let (r1 r2)
  824. (when (and user-mail-address (not (string= user-mail-address "")))
  825. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  826. (when (and user-full-name (not (string= user-full-name "")))
  827. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  828. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  829. "Regexp mached against the \"From:\" header of an email or usenet message.
  830. It should match if the message is from the user him/herself."
  831. :group 'org-link-store
  832. :type 'regexp)
  833. (defcustom org-context-in-file-links t
  834. "Non-nil means, file links from `org-store-link' contain context.
  835. A search string will be added to the file name with :: as separator and
  836. used to find the context when the link is activated by the command
  837. `org-open-at-point'.
  838. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  839. negates this setting for the duration of the command."
  840. :group 'org-link-store
  841. :type 'boolean)
  842. (defcustom org-keep-stored-link-after-insertion nil
  843. "Non-nil means, keep link in list for entire session.
  844. The command `org-store-link' adds a link pointing to the current
  845. location to an internal list. These links accumulate during a session.
  846. The command `org-insert-link' can be used to insert links into any
  847. Org-mode file (offering completion for all stored links). When this
  848. option is nil, every link which has been inserted once using \\[org-insert-link]
  849. will be removed from the list, to make completing the unused links
  850. more efficient."
  851. :group 'org-link-store
  852. :type 'boolean)
  853. (defgroup org-link-follow nil
  854. "Options concerning following links in Org-mode."
  855. :tag "Org Follow Link"
  856. :group 'org-link)
  857. (defcustom org-link-translation-function nil
  858. "Function to translate links with different syntax to Org syntax.
  859. This can be used to translate links created for example by the Planner
  860. or emacs-wiki packages to Org syntax.
  861. The function must accept two parameters, a TYPE containing the link
  862. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  863. which is everything after the link protocol. It should return a cons
  864. with possibly modifed values of type and path.
  865. Org contains a function for this, so if you set this variable to
  866. `org-translate-link-from-planner', you should be able follow many
  867. links created by planner."
  868. :group 'org-link-follow
  869. :type 'function)
  870. (defcustom org-follow-link-hook nil
  871. "Hook that is run after a link has been followed."
  872. :group 'org-link-follow
  873. :type 'hook)
  874. (defcustom org-tab-follows-link nil
  875. "Non-nil means, on links TAB will follow the link.
  876. Needs to be set before org.el is loaded."
  877. :group 'org-link-follow
  878. :type 'boolean)
  879. (defcustom org-return-follows-link nil
  880. "Non-nil means, on links RET will follow the link.
  881. Needs to be set before org.el is loaded."
  882. :group 'org-link-follow
  883. :type 'boolean)
  884. (defcustom org-mouse-1-follows-link
  885. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  886. "Non-nil means, mouse-1 on a link will follow the link.
  887. A longer mouse click will still set point. Does not work on XEmacs.
  888. Needs to be set before org.el is loaded."
  889. :group 'org-link-follow
  890. :type 'boolean)
  891. (defcustom org-mark-ring-length 4
  892. "Number of different positions to be recorded in the ring
  893. Changing this requires a restart of Emacs to work correctly."
  894. :group 'org-link-follow
  895. :type 'interger)
  896. (defcustom org-link-frame-setup
  897. '((vm . vm-visit-folder-other-frame)
  898. (gnus . gnus-other-frame)
  899. (file . find-file-other-window))
  900. "Setup the frame configuration for following links.
  901. When following a link with Emacs, it may often be useful to display
  902. this link in another window or frame. This variable can be used to
  903. set this up for the different types of links.
  904. For VM, use any of
  905. `vm-visit-folder'
  906. `vm-visit-folder-other-frame'
  907. For Gnus, use any of
  908. `gnus'
  909. `gnus-other-frame'
  910. `org-gnus-no-new-news'
  911. For FILE, use any of
  912. `find-file'
  913. `find-file-other-window'
  914. `find-file-other-frame'
  915. For the calendar, use the variable `calendar-setup'.
  916. For BBDB, it is currently only possible to display the matches in
  917. another window."
  918. :group 'org-link-follow
  919. :type '(list
  920. (cons (const vm)
  921. (choice
  922. (const vm-visit-folder)
  923. (const vm-visit-folder-other-window)
  924. (const vm-visit-folder-other-frame)))
  925. (cons (const gnus)
  926. (choice
  927. (const gnus)
  928. (const gnus-other-frame)
  929. (const org-gnus-no-new-news)))
  930. (cons (const file)
  931. (choice
  932. (const find-file)
  933. (const find-file-other-window)
  934. (const find-file-other-frame)))))
  935. (defcustom org-display-internal-link-with-indirect-buffer nil
  936. "Non-nil means, use indirect buffer to display infile links.
  937. Activating internal links (from one location in a file to another location
  938. in the same file) normally just jumps to the location. When the link is
  939. activated with a C-u prefix (or with mouse-3), the link is displayed in
  940. another window. When this option is set, the other window actually displays
  941. an indirect buffer clone of the current buffer, to avoid any visibility
  942. changes to the current buffer."
  943. :group 'org-link-follow
  944. :type 'boolean)
  945. (defcustom org-open-non-existing-files nil
  946. "Non-nil means, `org-open-file' will open non-existing files.
  947. When nil, an error will be generated."
  948. :group 'org-link-follow
  949. :type 'boolean)
  950. (defcustom org-open-directory-means-index-dot-org nil
  951. "Non-nil means, a link to a directory really means to index.org.
  952. When nil, following a directory link will run dired or open a finder/explorer
  953. window on that directory."
  954. :group 'org-link-follow
  955. :type 'boolean)
  956. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  957. "Function and arguments to call for following mailto links.
  958. This is a list with the first element being a lisp function, and the
  959. remaining elements being arguments to the function. In string arguments,
  960. %a will be replaced by the address, and %s will be replaced by the subject
  961. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  962. :group 'org-link-follow
  963. :type '(choice
  964. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  965. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  966. (const :tag "message-mail" (message-mail "%a" "%s"))
  967. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  968. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  969. "Non-nil means, ask for confirmation before executing shell links.
  970. Shell links can be dangerous: just think about a link
  971. [[shell:rm -rf ~/*][Google Search]]
  972. This link would show up in your Org-mode document as \"Google Search\",
  973. but really it would remove your entire home directory.
  974. Therefore we advise against setting this variable to nil.
  975. Just change it to `y-or-n-p' of you want to confirm with a
  976. single keystroke rather than having to type \"yes\"."
  977. :group 'org-link-follow
  978. :type '(choice
  979. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  980. (const :tag "with y-or-n (faster)" y-or-n-p)
  981. (const :tag "no confirmation (dangerous)" nil)))
  982. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  983. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  984. Elisp links can be dangerous: just think about a link
  985. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  986. This link would show up in your Org-mode document as \"Google Search\",
  987. but really it would remove your entire home directory.
  988. Therefore we advise against setting this variable to nil.
  989. Just change it to `y-or-n-p' of you want to confirm with a
  990. single keystroke rather than having to type \"yes\"."
  991. :group 'org-link-follow
  992. :type '(choice
  993. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  994. (const :tag "with y-or-n (faster)" y-or-n-p)
  995. (const :tag "no confirmation (dangerous)" nil)))
  996. (defconst org-file-apps-defaults-gnu
  997. '((remote . emacs)
  998. (system . mailcap)
  999. (t . mailcap))
  1000. "Default file applications on a UNIX or GNU/Linux system.
  1001. See `org-file-apps'.")
  1002. (defconst org-file-apps-defaults-macosx
  1003. '((remote . emacs)
  1004. (t . "open %s")
  1005. (system . "open %s")
  1006. ("ps.gz" . "gv %s")
  1007. ("eps.gz" . "gv %s")
  1008. ("dvi" . "xdvi %s")
  1009. ("fig" . "xfig %s"))
  1010. "Default file applications on a MacOS X system.
  1011. The system \"open\" is known as a default, but we use X11 applications
  1012. for some files for which the OS does not have a good default.
  1013. See `org-file-apps'.")
  1014. (defconst org-file-apps-defaults-windowsnt
  1015. (list
  1016. '(remote . emacs)
  1017. (cons t
  1018. (list (if (featurep 'xemacs)
  1019. 'mswindows-shell-execute
  1020. 'w32-shell-execute)
  1021. "open" 'file))
  1022. (cons 'system
  1023. (list (if (featurep 'xemacs)
  1024. 'mswindows-shell-execute
  1025. 'w32-shell-execute)
  1026. "open" 'file)))
  1027. "Default file applications on a Windows NT system.
  1028. The system \"open\" is used for most files.
  1029. See `org-file-apps'.")
  1030. (defcustom org-file-apps
  1031. '(
  1032. (auto-mode . emacs)
  1033. ("\\.x?html?\\'" . default)
  1034. ("\\.pdf\\'" . default)
  1035. )
  1036. "External applications for opening `file:path' items in a document.
  1037. Org-mode uses system defaults for different file types, but
  1038. you can use this variable to set the application for a given file
  1039. extension. The entries in this list are cons cells where the car identifies
  1040. files and the cdr the corresponding command. Possible values for the
  1041. file identifier are
  1042. \"regex\" Regular expression matched against the file name. For backward
  1043. compatibility, this can also be a string with only alphanumeric
  1044. characters, which is then interpreted as an extension.
  1045. `directory' Matches a directory
  1046. `remote' Matches a remote file, accessible through tramp or efs.
  1047. Remote files most likely should be visited through Emacs
  1048. because external applications cannot handle such paths.
  1049. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1050. so all files Emacs knows how to handle. Using this with
  1051. command `emacs' will open most files in Emacs. Beware that this
  1052. will also open html files insite Emacs, unless you add
  1053. (\"html\" . default) to the list as well.
  1054. t Default for files not matched by any of the other options.
  1055. `system' The system command to open files, like `open' on Windows
  1056. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1057. that will be selected if you call `C-c C-o' with a double
  1058. `C-u C-u' prefix.
  1059. Possible values for the command are:
  1060. `emacs' The file will be visited by the current Emacs process.
  1061. `default' Use the default application for this file type, which is the
  1062. association for t in the list, most likely in the system-specific
  1063. part.
  1064. This can be used to overrule an unwanted seting in the
  1065. system-specific variable.
  1066. `system' Use the system command for opening files, like \"open\".
  1067. This command is specified by the entry whose car is `system'.
  1068. Most likely, the system-specific version of this variable
  1069. does define this command, but you can overrule/replace it
  1070. here.
  1071. string A command to be executed by a shell; %s will be replaced
  1072. by the path to the file.
  1073. sexp A Lisp form which will be evaluated. The file path will
  1074. be available in the Lisp variable `file'.
  1075. For more examples, see the system specific constants
  1076. `org-file-apps-defaults-macosx'
  1077. `org-file-apps-defaults-windowsnt'
  1078. `org-file-apps-defaults-gnu'."
  1079. :group 'org-link-follow
  1080. :type '(repeat
  1081. (cons (choice :value ""
  1082. (string :tag "Extension")
  1083. (const :tag "System command to open files" system)
  1084. (const :tag "Default for unrecognized files" t)
  1085. (const :tag "Remote file" remote)
  1086. (const :tag "Links to a directory" directory)
  1087. (const :tag "Any files that have Emacs modes"
  1088. auto-mode))
  1089. (choice :value ""
  1090. (const :tag "Visit with Emacs" emacs)
  1091. (const :tag "Use default" default)
  1092. (const :tag "Use the system command" system)
  1093. (string :tag "Command")
  1094. (sexp :tag "Lisp form")))))
  1095. (defgroup org-refile nil
  1096. "Options concerning refiling entries in Org-mode."
  1097. :tag "Org Remember"
  1098. :group 'org)
  1099. (defcustom org-directory "~/org"
  1100. "Directory with org files.
  1101. This directory will be used as default to prompt for org files.
  1102. Used by the hooks for remember.el."
  1103. :group 'org-refile
  1104. :group 'org-remember
  1105. :type 'directory)
  1106. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1107. "Default target for storing notes.
  1108. Used by the hooks for remember.el. This can be a string, or nil to mean
  1109. the value of `remember-data-file'.
  1110. You can set this on a per-template basis with the variable
  1111. `org-remember-templates'."
  1112. :group 'org-refile
  1113. :group 'org-remember
  1114. :type '(choice
  1115. (const :tag "Default from remember-data-file" nil)
  1116. file))
  1117. (defcustom org-goto-interface 'outline
  1118. "The default interface to be used for `org-goto'.
  1119. Allowed vaues are:
  1120. outline The interface shows an outline of the relevant file
  1121. and the correct heading is found by moving through
  1122. the outline or by searching with incremental search.
  1123. outline-path-completion Headlines in the current buffer are offered via
  1124. completion."
  1125. :group 'org-refile
  1126. :type '(choice
  1127. (const :tag "Outline" outline)
  1128. (const :tag "Outline-path-completion" outline-path-completion)))
  1129. (defcustom org-reverse-note-order nil
  1130. "Non-nil means, store new notes at the beginning of a file or entry.
  1131. When nil, new notes will be filed to the end of a file or entry.
  1132. This can also be a list with cons cells of regular expressions that
  1133. are matched against file names, and values."
  1134. :group 'org-remember
  1135. :type '(choice
  1136. (const :tag "Reverse always" t)
  1137. (const :tag "Reverse never" nil)
  1138. (repeat :tag "By file name regexp"
  1139. (cons regexp boolean))))
  1140. (defcustom org-refile-targets nil
  1141. "Targets for refiling entries with \\[org-refile].
  1142. This is list of cons cells. Each cell contains:
  1143. - a specification of the files to be considered, either a list of files,
  1144. or a symbol whose function or variable value will be used to retrieve
  1145. a file name or a list of file names. Nil means, refile to a different
  1146. heading in the current buffer.
  1147. - A specification of how to find candidate refile targets. This may be
  1148. any of
  1149. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1150. This tag has to be present in all target headlines, inheritance will
  1151. not be considered.
  1152. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1153. todo keyword.
  1154. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1155. headlines that are refiling targets.
  1156. - a cons cell (:level . N). Any headline of level N is considered a target.
  1157. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1158. When this variable is nil, all top-level headlines in the current buffer
  1159. are used, equivalent to the value `((nil . (:level . 1))'."
  1160. :group 'org-remember
  1161. :type '(repeat
  1162. (cons
  1163. (choice :value org-agenda-files
  1164. (const :tag "All agenda files" org-agenda-files)
  1165. (const :tag "Current buffer" nil)
  1166. (function) (variable) (file))
  1167. (choice :tag "Identify target headline by"
  1168. (cons :tag "Specific tag" (const :tag) (string))
  1169. (cons :tag "TODO keyword" (const :todo) (string))
  1170. (cons :tag "Regular expression" (const :regexp) (regexp))
  1171. (cons :tag "Level number" (const :level) (integer))
  1172. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1173. (defcustom org-refile-use-outline-path nil
  1174. "Non-nil means, provide refile targets as paths.
  1175. So a level 3 headline will be available as level1/level2/level3.
  1176. When the value is `file', also include the file name (without directory)
  1177. into the path. When `full-file-path', include the full file path."
  1178. :group 'org-remember
  1179. :type '(choice
  1180. (const :tag "Not" nil)
  1181. (const :tag "Yes" t)
  1182. (const :tag "Start with file name" file)
  1183. (const :tag "Start with full file path" full-file-path)))
  1184. (defgroup org-todo nil
  1185. "Options concerning TODO items in Org-mode."
  1186. :tag "Org TODO"
  1187. :group 'org)
  1188. (defgroup org-progress nil
  1189. "Options concerning Progress logging in Org-mode."
  1190. :tag "Org Progress"
  1191. :group 'org-time)
  1192. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1193. "List of TODO entry keyword sequences and their interpretation.
  1194. \\<org-mode-map>This is a list of sequences.
  1195. Each sequence starts with a symbol, either `sequence' or `type',
  1196. indicating if the keywords should be interpreted as a sequence of
  1197. action steps, or as different types of TODO items. The first
  1198. keywords are states requiring action - these states will select a headline
  1199. for inclusion into the global TODO list Org-mode produces. If one of
  1200. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1201. signify that no further action is necessary. If \"|\" is not found,
  1202. the last keyword is treated as the only DONE state of the sequence.
  1203. The command \\[org-todo] cycles an entry through these states, and one
  1204. additional state where no keyword is present. For details about this
  1205. cycling, see the manual.
  1206. TODO keywords and interpretation can also be set on a per-file basis with
  1207. the special #+SEQ_TODO and #+TYP_TODO lines.
  1208. Each keyword can optionally specify a character for fast state selection
  1209. \(in combination with the variable `org-use-fast-todo-selection')
  1210. and specifiers for state change logging, using the same syntax
  1211. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1212. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1213. indicates to record a time stamp each time this state is selected.
  1214. Each keyword may also specify if a timestamp or a note should be
  1215. recorded when entering or leaving the state, by adding additional
  1216. characters in the parenthesis after the keyword. This looks like this:
  1217. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1218. record only the time of the state change. With X and Y being either
  1219. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1220. Y when leaving the state if and only if the *target* state does not
  1221. define X. You may omit any of the fast-selection key or X or /Y,
  1222. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1223. For backward compatibility, this variable may also be just a list
  1224. of keywords - in this case the interptetation (sequence or type) will be
  1225. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1226. :group 'org-todo
  1227. :group 'org-keywords
  1228. :type '(choice
  1229. (repeat :tag "Old syntax, just keywords"
  1230. (string :tag "Keyword"))
  1231. (repeat :tag "New syntax"
  1232. (cons
  1233. (choice
  1234. :tag "Interpretation"
  1235. (const :tag "Sequence (cycling hits every state)" sequence)
  1236. (const :tag "Type (cycling directly to DONE)" type))
  1237. (repeat
  1238. (string :tag "Keyword"))))))
  1239. (defvar org-todo-keywords-1 nil
  1240. "All TODO and DONE keywords active in a buffer.")
  1241. (make-variable-buffer-local 'org-todo-keywords-1)
  1242. (defvar org-todo-keywords-for-agenda nil)
  1243. (defvar org-done-keywords-for-agenda nil)
  1244. (defvar org-todo-keyword-alist-for-agenda nil)
  1245. (defvar org-tag-alist-for-agenda nil)
  1246. (defvar org-agenda-contributing-files nil)
  1247. (defvar org-not-done-keywords nil)
  1248. (make-variable-buffer-local 'org-not-done-keywords)
  1249. (defvar org-done-keywords nil)
  1250. (make-variable-buffer-local 'org-done-keywords)
  1251. (defvar org-todo-heads nil)
  1252. (make-variable-buffer-local 'org-todo-heads)
  1253. (defvar org-todo-sets nil)
  1254. (make-variable-buffer-local 'org-todo-sets)
  1255. (defvar org-todo-log-states nil)
  1256. (make-variable-buffer-local 'org-todo-log-states)
  1257. (defvar org-todo-kwd-alist nil)
  1258. (make-variable-buffer-local 'org-todo-kwd-alist)
  1259. (defvar org-todo-key-alist nil)
  1260. (make-variable-buffer-local 'org-todo-key-alist)
  1261. (defvar org-todo-key-trigger nil)
  1262. (make-variable-buffer-local 'org-todo-key-trigger)
  1263. (defcustom org-todo-interpretation 'sequence
  1264. "Controls how TODO keywords are interpreted.
  1265. This variable is in principle obsolete and is only used for
  1266. backward compatibility, if the interpretation of todo keywords is
  1267. not given already in `org-todo-keywords'. See that variable for
  1268. more information."
  1269. :group 'org-todo
  1270. :group 'org-keywords
  1271. :type '(choice (const sequence)
  1272. (const type)))
  1273. (defcustom org-use-fast-todo-selection 'prefix
  1274. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1275. This variable describes if and under what circumstances the cycling
  1276. mechanism for TODO keywords will be replaced by a single-key, direct
  1277. selection scheme.
  1278. When nil, fast selection is never used.
  1279. When the symbol `prefix', it will be used when `org-todo' is called with
  1280. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1281. in an agenda buffer.
  1282. When t, fast selection is used by default. In this case, the prefix
  1283. argument forces cycling instead.
  1284. In all cases, the special interface is only used if access keys have actually
  1285. been assigned by the user, i.e. if keywords in the configuration are followed
  1286. by a letter in parenthesis, like TODO(t)."
  1287. :group 'org-todo
  1288. :type '(choice
  1289. (const :tag "Never" nil)
  1290. (const :tag "By default" t)
  1291. (const :tag "Only with C-u C-c C-t" prefix)))
  1292. (defcustom org-provide-todo-statistics t
  1293. "Non-nil means, update todo statistics after insert and toggle.
  1294. When this is set, todo statistics is updated in the parent of the current
  1295. entry each time a todo state is changed."
  1296. :group 'org-todo
  1297. :type 'boolean)
  1298. (defcustom org-after-todo-state-change-hook nil
  1299. "Hook which is run after the state of a TODO item was changed.
  1300. The new state (a string with a TODO keyword, or nil) is available in the
  1301. Lisp variable `state'."
  1302. :group 'org-todo
  1303. :type 'hook)
  1304. (defcustom org-todo-state-tags-triggers nil
  1305. "Tag changes that should be triggered by TODO state changes.
  1306. This is a list. Each entry is
  1307. (state-change (tag . flag) .......)
  1308. State-change can be a string with a state, and empty string to indicate the
  1309. state that has no TODO keyword, or it can be one of the symbols `todo'
  1310. or `done', meaning any not-done or done state, respectively."
  1311. :group 'org-todo
  1312. :group 'org-tags
  1313. :type '(repeat
  1314. (cons (choice :tag "When changing to"
  1315. (const :tag "Not-done state" todo)
  1316. (const :tag "Done state" done)
  1317. (string :tag "State"))
  1318. (repeat
  1319. (cons :tag "Tag action"
  1320. (string :tag "Tag")
  1321. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1322. (defcustom org-log-done nil
  1323. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1324. When equal to the list (done), also prompt for a closing note.
  1325. This can also be configured on a per-file basis by adding one of
  1326. the following lines anywhere in the buffer:
  1327. #+STARTUP: logdone
  1328. #+STARTUP: lognotedone
  1329. #+STARTUP: nologdone"
  1330. :group 'org-todo
  1331. :group 'org-progress
  1332. :type '(choice
  1333. (const :tag "No logging" nil)
  1334. (const :tag "Record CLOSED timestamp" time)
  1335. (const :tag "Record CLOSED timestamp with closing note." note)))
  1336. ;; Normalize old uses of org-log-done.
  1337. (cond
  1338. ((eq org-log-done t) (setq org-log-done 'time))
  1339. ((and (listp org-log-done) (memq 'done org-log-done))
  1340. (setq org-log-done 'note)))
  1341. (defcustom org-log-note-clock-out nil
  1342. "Non-nil means, record a note when clocking out of an item.
  1343. This can also be configured on a per-file basis by adding one of
  1344. the following lines anywhere in the buffer:
  1345. #+STARTUP: lognoteclock-out
  1346. #+STARTUP: nolognoteclock-out"
  1347. :group 'org-todo
  1348. :group 'org-progress
  1349. :type 'boolean)
  1350. (defcustom org-log-done-with-time t
  1351. "Non-nil means, the CLOSED time stamp will contain date and time.
  1352. When nil, only the date will be recorded."
  1353. :group 'org-progress
  1354. :type 'boolean)
  1355. (defcustom org-log-note-headings
  1356. '((done . "CLOSING NOTE %t")
  1357. (state . "State %-12s %t")
  1358. (note . "Note taken on %t")
  1359. (clock-out . ""))
  1360. "Headings for notes added to entries.
  1361. The value is an alist, with the car being a symbol indicating the note
  1362. context, and the cdr is the heading to be used. The heading may also be the
  1363. empty string.
  1364. %t in the heading will be replaced by a time stamp.
  1365. %s will be replaced by the new TODO state, in double quotes.
  1366. %u will be replaced by the user name.
  1367. %U will be replaced by the full user name."
  1368. :group 'org-todo
  1369. :group 'org-progress
  1370. :type '(list :greedy t
  1371. (cons (const :tag "Heading when closing an item" done) string)
  1372. (cons (const :tag
  1373. "Heading when changing todo state (todo sequence only)"
  1374. state) string)
  1375. (cons (const :tag "Heading when just taking a note" note) string)
  1376. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1377. (unless (assq 'note org-log-note-headings)
  1378. (push '(note . "%t") org-log-note-headings))
  1379. (defcustom org-log-state-notes-insert-after-drawers nil
  1380. "Non-nil means, insert state change notes after any drawers in entry.
  1381. Only the drawers that *immediately* follow the headline and the
  1382. deadline/scheduled line are skipped.
  1383. When nil, insert notes right after the heading and perhaps the line
  1384. with deadline/scheduling if present."
  1385. :group 'org-todo
  1386. :group 'org-progress
  1387. :type 'boolean)
  1388. (defcustom org-log-states-order-reversed t
  1389. "Non-nil means, the latest state change note will be directly after heading.
  1390. When nil, the notes will be orderer according to time."
  1391. :group 'org-todo
  1392. :group 'org-progress
  1393. :type 'boolean)
  1394. (defcustom org-log-repeat 'time
  1395. "Non-nil means, record moving through the DONE state when triggering repeat.
  1396. An auto-repeating tasks is immediately switched back to TODO when marked
  1397. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1398. the TODO keyword definition, or recording a closing note by setting
  1399. `org-log-done', there will be no record of the task moving through DONE.
  1400. This variable forces taking a note anyway. Possible values are:
  1401. nil Don't force a record
  1402. time Record a time stamp
  1403. note Record a note
  1404. This option can also be set with on a per-file-basis with
  1405. #+STARTUP: logrepeat
  1406. #+STARTUP: lognoterepeat
  1407. #+STARTUP: nologrepeat
  1408. You can have local logging settings for a subtree by setting the LOGGING
  1409. property to one or more of these keywords."
  1410. :group 'org-todo
  1411. :group 'org-progress
  1412. :type '(choice
  1413. (const :tag "Don't force a record" nil)
  1414. (const :tag "Force recording the DONE state" time)
  1415. (const :tag "Force recording a note with the DONE state" note)))
  1416. (defgroup org-priorities nil
  1417. "Priorities in Org-mode."
  1418. :tag "Org Priorities"
  1419. :group 'org-todo)
  1420. (defcustom org-highest-priority ?A
  1421. "The highest priority of TODO items. A character like ?A, ?B etc.
  1422. Must have a smaller ASCII number than `org-lowest-priority'."
  1423. :group 'org-priorities
  1424. :type 'character)
  1425. (defcustom org-lowest-priority ?C
  1426. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1427. Must have a larger ASCII number than `org-highest-priority'."
  1428. :group 'org-priorities
  1429. :type 'character)
  1430. (defcustom org-default-priority ?B
  1431. "The default priority of TODO items.
  1432. This is the priority an item get if no explicit priority is given."
  1433. :group 'org-priorities
  1434. :type 'character)
  1435. (defcustom org-priority-start-cycle-with-default t
  1436. "Non-nil means, start with default priority when starting to cycle.
  1437. When this is nil, the first step in the cycle will be (depending on the
  1438. command used) one higher or lower that the default priority."
  1439. :group 'org-priorities
  1440. :type 'boolean)
  1441. (defgroup org-time nil
  1442. "Options concerning time stamps and deadlines in Org-mode."
  1443. :tag "Org Time"
  1444. :group 'org)
  1445. (defcustom org-insert-labeled-timestamps-at-point nil
  1446. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1447. When nil, these labeled time stamps are forces into the second line of an
  1448. entry, just after the headline. When scheduling from the global TODO list,
  1449. the time stamp will always be forced into the second line."
  1450. :group 'org-time
  1451. :type 'boolean)
  1452. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1453. "Formats for `format-time-string' which are used for time stamps.
  1454. It is not recommended to change this constant.")
  1455. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1456. "Number of minutes to round time stamps to.
  1457. These are two values, the first applies when first creating a time stamp.
  1458. The second applies when changing it with the commands `S-up' and `S-down'.
  1459. When changing the time stamp, this means that it will change in steps
  1460. of N minutes, as given by the second value.
  1461. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1462. numbers should be factors of 60, so for example 5, 10, 15.
  1463. When this is larger than 1, you can still force an exact time-stamp by using
  1464. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1465. and by using a prefix arg to `S-up/down' to specify the exact number
  1466. of minutes to shift."
  1467. :group 'org-time
  1468. :get '(lambda (var) ; Make sure all entries have 5 elements
  1469. (if (integerp (default-value var))
  1470. (list (default-value var) 5)
  1471. (default-value var)))
  1472. :type '(list
  1473. (integer :tag "when inserting times")
  1474. (integer :tag "when modifying times")))
  1475. ;; Normalize old customizations of this variable.
  1476. (when (integerp org-time-stamp-rounding-minutes)
  1477. (setq org-time-stamp-rounding-minutes
  1478. (list org-time-stamp-rounding-minutes
  1479. org-time-stamp-rounding-minutes)))
  1480. (defcustom org-display-custom-times nil
  1481. "Non-nil means, overlay custom formats over all time stamps.
  1482. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1483. To turn this on on a per-file basis, insert anywhere in the file:
  1484. #+STARTUP: customtime"
  1485. :group 'org-time
  1486. :set 'set-default
  1487. :type 'sexp)
  1488. (make-variable-buffer-local 'org-display-custom-times)
  1489. (defcustom org-time-stamp-custom-formats
  1490. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1491. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1492. These are overlayed over the default ISO format if the variable
  1493. `org-display-custom-times' is set. Time like %H:%M should be at the
  1494. end of the second format."
  1495. :group 'org-time
  1496. :type 'sexp)
  1497. (defun org-time-stamp-format (&optional long inactive)
  1498. "Get the right format for a time string."
  1499. (let ((f (if long (cdr org-time-stamp-formats)
  1500. (car org-time-stamp-formats))))
  1501. (if inactive
  1502. (concat "[" (substring f 1 -1) "]")
  1503. f)))
  1504. (defcustom org-time-clocksum-format "%d:%02d"
  1505. "The format string used when creating CLOCKSUM lines, or when
  1506. org-mode generates a time duration."
  1507. :group 'org-time
  1508. :type 'string)
  1509. (defcustom org-deadline-warning-days 14
  1510. "No. of days before expiration during which a deadline becomes active.
  1511. This variable governs the display in sparse trees and in the agenda.
  1512. When 0 or negative, it means use this number (the absolute value of it)
  1513. even if a deadline has a different individual lead time specified."
  1514. :group 'org-time
  1515. :group 'org-agenda-daily/weekly
  1516. :type 'number)
  1517. (defcustom org-read-date-prefer-future t
  1518. "Non-nil means, assume future for incomplete date input from user.
  1519. This affects the following situations:
  1520. 1. The user gives a day, but no month.
  1521. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1522. read this as the third of *next* month. However, if you enter \"17\",
  1523. it will be considered as *this* month.
  1524. 2. The user gives a month but not a year.
  1525. For example, if it is april and you enter \"feb 2\", this will be read
  1526. as feb 2, *next* year. \"May 5\", however, will be this year.
  1527. Currently this does not work for ISO week specifications.
  1528. When this option is nil, the current month and year will always be used
  1529. as defaults."
  1530. :group 'org-time
  1531. :type 'boolean)
  1532. (defcustom org-read-date-display-live t
  1533. "Non-nil means, display current interpretation of date prompt live.
  1534. This display will be in an overlay, in the minibuffer."
  1535. :group 'org-time
  1536. :type 'boolean)
  1537. (defcustom org-read-date-popup-calendar t
  1538. "Non-nil means, pop up a calendar when prompting for a date.
  1539. In the calendar, the date can be selected with mouse-1. However, the
  1540. minibuffer will also be active, and you can simply enter the date as well.
  1541. When nil, only the minibuffer will be available."
  1542. :group 'org-time
  1543. :type 'boolean)
  1544. (if (fboundp 'defvaralias)
  1545. (defvaralias 'org-popup-calendar-for-date-prompt
  1546. 'org-read-date-popup-calendar))
  1547. (defcustom org-extend-today-until 0
  1548. "The hour when your day really ends. Must be an integer.
  1549. This has influence for the following applications:
  1550. - When switching the agenda to \"today\". It it is still earlier than
  1551. the time given here, the day recognized as TODAY is actually yesterday.
  1552. - When a date is read from the user and it is still before the time given
  1553. here, the current date and time will be assumed to be yesterday, 23:59.
  1554. Also, timestamps inserted in remember templates follow this rule.
  1555. IMPORTANT: This is a feature whose implementation is and likely will
  1556. remain incomplete. Really, it is only here because past midnight seems to
  1557. be the favorite working time of John Wiegley :-)"
  1558. :group 'org-time
  1559. :type 'number)
  1560. (defcustom org-edit-timestamp-down-means-later nil
  1561. "Non-nil means, S-down will increase the time in a time stamp.
  1562. When nil, S-up will increase."
  1563. :group 'org-time
  1564. :type 'boolean)
  1565. (defcustom org-calendar-follow-timestamp-change t
  1566. "Non-nil means, make the calendar window follow timestamp changes.
  1567. When a timestamp is modified and the calendar window is visible, it will be
  1568. moved to the new date."
  1569. :group 'org-time
  1570. :type 'boolean)
  1571. (defgroup org-tags nil
  1572. "Options concerning tags in Org-mode."
  1573. :tag "Org Tags"
  1574. :group 'org)
  1575. (defcustom org-tag-alist nil
  1576. "List of tags allowed in Org-mode files.
  1577. When this list is nil, Org-mode will base TAG input on what is already in the
  1578. buffer.
  1579. The value of this variable is an alist, the car of each entry must be a
  1580. keyword as a string, the cdr may be a character that is used to select
  1581. that tag through the fast-tag-selection interface.
  1582. See the manual for details."
  1583. :group 'org-tags
  1584. :type '(repeat
  1585. (choice
  1586. (cons (string :tag "Tag name")
  1587. (character :tag "Access char"))
  1588. (const :tag "Start radio group" (:startgroup))
  1589. (const :tag "End radio group" (:endgroup)))))
  1590. (defvar org-file-tags nil
  1591. "List of tags that can be inherited by all entries in the file.
  1592. The tags will be inherited if the variable `org-use-tag-inheritance'
  1593. says they should be.
  1594. This variable is populated from #+TAG lines.")
  1595. (defcustom org-use-fast-tag-selection 'auto
  1596. "Non-nil means, use fast tag selection scheme.
  1597. This is a special interface to select and deselect tags with single keys.
  1598. When nil, fast selection is never used.
  1599. When the symbol `auto', fast selection is used if and only if selection
  1600. characters for tags have been configured, either through the variable
  1601. `org-tag-alist' or through a #+TAGS line in the buffer.
  1602. When t, fast selection is always used and selection keys are assigned
  1603. automatically if necessary."
  1604. :group 'org-tags
  1605. :type '(choice
  1606. (const :tag "Always" t)
  1607. (const :tag "Never" nil)
  1608. (const :tag "When selection characters are configured" 'auto)))
  1609. (defcustom org-fast-tag-selection-single-key nil
  1610. "Non-nil means, fast tag selection exits after first change.
  1611. When nil, you have to press RET to exit it.
  1612. During fast tag selection, you can toggle this flag with `C-c'.
  1613. This variable can also have the value `expert'. In this case, the window
  1614. displaying the tags menu is not even shown, until you press C-c again."
  1615. :group 'org-tags
  1616. :type '(choice
  1617. (const :tag "No" nil)
  1618. (const :tag "Yes" t)
  1619. (const :tag "Expert" expert)))
  1620. (defvar org-fast-tag-selection-include-todo nil
  1621. "Non-nil means, fast tags selection interface will also offer TODO states.
  1622. This is an undocumented feature, you should not rely on it.")
  1623. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1624. "The column to which tags should be indented in a headline.
  1625. If this number is positive, it specifies the column. If it is negative,
  1626. it means that the tags should be flushright to that column. For example,
  1627. -80 works well for a normal 80 character screen."
  1628. :group 'org-tags
  1629. :type 'integer)
  1630. (defcustom org-auto-align-tags t
  1631. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1632. These operations change the length of a headline and therefore shift
  1633. the tags around. With this options turned on, after each such operation
  1634. the tags are again aligned to `org-tags-column'."
  1635. :group 'org-tags
  1636. :type 'boolean)
  1637. (defcustom org-use-tag-inheritance t
  1638. "Non-nil means, tags in levels apply also for sublevels.
  1639. When nil, only the tags directly given in a specific line apply there.
  1640. If this option is t, a match early-on in a tree can lead to a large
  1641. number of matches in the subtree. If you only want to see the first
  1642. match in a tree during a search, check out the variable
  1643. `org-tags-match-list-sublevels'.
  1644. This may also be a list of tags that should be inherited, or a regexp that
  1645. matches tags that should be inherited."
  1646. :group 'org-tags
  1647. :type '(choice
  1648. (const :tag "Not" nil)
  1649. (const :tag "Always" t)
  1650. (repeat :tag "Specific tags" (string :tag "Tag"))
  1651. (regexp :tag "Tags matched by regexp")))
  1652. (defun org-tag-inherit-p (tag)
  1653. "Check if TAG is one that should be inherited."
  1654. (cond
  1655. ((eq org-use-tag-inheritance t) t)
  1656. ((not org-use-tag-inheritance) nil)
  1657. ((stringp org-use-tag-inheritance)
  1658. (string-match org-use-tag-inheritance tag))
  1659. ((listp org-use-tag-inheritance)
  1660. (member tag org-use-tag-inheritance))
  1661. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1662. (defcustom org-tags-match-list-sublevels t
  1663. "Non-nil means list also sublevels of headlines matching tag search.
  1664. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1665. the sublevels of a headline matching a tag search often also match
  1666. the same search. Listing all of them can create very long lists.
  1667. Setting this variable to nil causes subtrees of a match to be skipped.
  1668. This option is off by default, because inheritance in on. If you turn
  1669. inheritance off, you very likely want to turn this option on.
  1670. As a special case, if the tag search is restricted to TODO items, the
  1671. value of this variable is ignored and sublevels are always checked, to
  1672. make sure all corresponding TODO items find their way into the list."
  1673. :group 'org-tags
  1674. :type 'boolean)
  1675. (defvar org-tags-history nil
  1676. "History of minibuffer reads for tags.")
  1677. (defvar org-last-tags-completion-table nil
  1678. "The last used completion table for tags.")
  1679. (defvar org-after-tags-change-hook nil
  1680. "Hook that is run after the tags in a line have changed.")
  1681. (defgroup org-properties nil
  1682. "Options concerning properties in Org-mode."
  1683. :tag "Org Properties"
  1684. :group 'org)
  1685. (defcustom org-property-format "%-10s %s"
  1686. "How property key/value pairs should be formatted by `indent-line'.
  1687. When `indent-line' hits a property definition, it will format the line
  1688. according to this format, mainly to make sure that the values are
  1689. lined-up with respect to each other."
  1690. :group 'org-properties
  1691. :type 'string)
  1692. (defcustom org-use-property-inheritance nil
  1693. "Non-nil means, properties apply also for sublevels.
  1694. This setting is chiefly used during property searches. Turning it on can
  1695. cause significant overhead when doing a search, which is why it is not
  1696. on by default.
  1697. When nil, only the properties directly given in the current entry count.
  1698. When t, every property is inherited. The value may also be a list of
  1699. properties that should have inheritance, or a regular expression matching
  1700. properties that should be inherited.
  1701. However, note that some special properties use inheritance under special
  1702. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1703. and the properties ending in \"_ALL\" when they are used as descriptor
  1704. for valid values of a property.
  1705. Note for programmers:
  1706. When querying an entry with `org-entry-get', you can control if inheritance
  1707. should be used. By default, `org-entry-get' looks only at the local
  1708. properties. You can request inheritance by setting the inherit argument
  1709. to t (to force inheritance) or to `selective' (to respect the setting
  1710. in this variable)."
  1711. :group 'org-properties
  1712. :type '(choice
  1713. (const :tag "Not" nil)
  1714. (const :tag "Always" t)
  1715. (repeat :tag "Specific properties" (string :tag "Property"))
  1716. (regexp :tag "Properties matched by regexp")))
  1717. (defun org-property-inherit-p (property)
  1718. "Check if PROPERTY is one that should be inherited."
  1719. (cond
  1720. ((eq org-use-property-inheritance t) t)
  1721. ((not org-use-property-inheritance) nil)
  1722. ((stringp org-use-property-inheritance)
  1723. (string-match org-use-property-inheritance property))
  1724. ((listp org-use-property-inheritance)
  1725. (member property org-use-property-inheritance))
  1726. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1727. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1728. "The default column format, if no other format has been defined.
  1729. This variable can be set on the per-file basis by inserting a line
  1730. #+COLUMNS: %25ITEM ....."
  1731. :group 'org-properties
  1732. :type 'string)
  1733. (defcustom org-columns-ellipses ".."
  1734. "The ellipses to be used when a field in column view is truncated.
  1735. When this is the empty string, as many characters as possible are shown,
  1736. but then there will be no visual indication that the field has been truncated.
  1737. When this is a string of length N, the last N characters of a truncated
  1738. field are replaced by this string. If the column is narrower than the
  1739. ellipses string, only part of the ellipses string will be shown."
  1740. :group 'org-properties
  1741. :type 'string)
  1742. (defcustom org-columns-modify-value-for-display-function nil
  1743. "Function that modifies values for display in column view.
  1744. For example, it can be used to cut out a certain part from a time stamp.
  1745. The function must take 2 arguments:
  1746. column-title The tite of the column (*not* the property name)
  1747. value The value that should be modified.
  1748. The function should return the value that should be displayed,
  1749. or nil if the normal value should be used."
  1750. :group 'org-properties
  1751. :type 'function)
  1752. (defcustom org-effort-property "Effort"
  1753. "The property that is being used to keep track of effort estimates.
  1754. Effort estimates given in this property need to have the format H:MM."
  1755. :group 'org-properties
  1756. :group 'org-progress
  1757. :type '(string :tag "Property"))
  1758. (defconst org-global-properties-fixed
  1759. '(("VISIBILITY_ALL" . "folded children content all"))
  1760. "List of property/value pairs that can be inherited by any entry.
  1761. These are fixed values, for the preset properties.")
  1762. (defcustom org-global-properties nil
  1763. "List of property/value pairs that can be inherited by any entry.
  1764. You can set buffer-local values for this by adding lines like
  1765. #+PROPERTY: NAME VALUE"
  1766. :group 'org-properties
  1767. :type '(repeat
  1768. (cons (string :tag "Property")
  1769. (string :tag "Value"))))
  1770. (defvar org-file-properties nil
  1771. "List of property/value pairs that can be inherited by any entry.
  1772. Valid for the current buffer.
  1773. This variable is populated from #+PROPERTY lines.")
  1774. (make-variable-buffer-local 'org-file-properties)
  1775. (defgroup org-agenda nil
  1776. "Options concerning agenda views in Org-mode."
  1777. :tag "Org Agenda"
  1778. :group 'org)
  1779. (defvar org-category nil
  1780. "Variable used by org files to set a category for agenda display.
  1781. Such files should use a file variable to set it, for example
  1782. # -*- mode: org; org-category: \"ELisp\"
  1783. or contain a special line
  1784. #+CATEGORY: ELisp
  1785. If the file does not specify a category, then file's base name
  1786. is used instead.")
  1787. (make-variable-buffer-local 'org-category)
  1788. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1789. (defcustom org-agenda-files nil
  1790. "The files to be used for agenda display.
  1791. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1792. \\[org-remove-file]. You can also use customize to edit the list.
  1793. If an entry is a directory, all files in that directory that are matched by
  1794. `org-agenda-file-regexp' will be part of the file list.
  1795. If the value of the variable is not a list but a single file name, then
  1796. the list of agenda files is actually stored and maintained in that file, one
  1797. agenda file per line."
  1798. :group 'org-agenda
  1799. :type '(choice
  1800. (repeat :tag "List of files and directories" file)
  1801. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1802. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1803. "Regular expression to match files for `org-agenda-files'.
  1804. If any element in the list in that variable contains a directory instead
  1805. of a normal file, all files in that directory that are matched by this
  1806. regular expression will be included."
  1807. :group 'org-agenda
  1808. :type 'regexp)
  1809. (defcustom org-agenda-text-search-extra-files nil
  1810. "List of extra files to be searched by text search commands.
  1811. These files will be search in addition to the agenda files by the
  1812. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1813. Note that these files will only be searched for text search commands,
  1814. not for the other agenda views like todo lists, tag searches or the weekly
  1815. agenda. This variable is intended to list notes and possibly archive files
  1816. that should also be searched by these two commands.
  1817. In fact, if the first element in the list is the symbol `agenda-archives',
  1818. than all archive files of all agenda files will be added to the search
  1819. scope."
  1820. :group 'org-agenda
  1821. :type '(set :greedy t
  1822. (const :tag "Agenda Archives" agenda-archives)
  1823. (repeat :inline t (file))))
  1824. (if (fboundp 'defvaralias)
  1825. (defvaralias 'org-agenda-multi-occur-extra-files
  1826. 'org-agenda-text-search-extra-files))
  1827. (defcustom org-agenda-skip-unavailable-files nil
  1828. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1829. A nil value means to remove them, after a query, from the list."
  1830. :group 'org-agenda
  1831. :type 'boolean)
  1832. (defcustom org-calendar-to-agenda-key [?c]
  1833. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1834. The command `org-calendar-goto-agenda' will be bound to this key. The
  1835. default is the character `c' because then `c' can be used to switch back and
  1836. forth between agenda and calendar."
  1837. :group 'org-agenda
  1838. :type 'sexp)
  1839. (defcustom org-calendar-agenda-action-key [?k]
  1840. "The key to be installed in `calendar-mode-map' for agenda-action.
  1841. The command `org-agenda-action' will be bound to this key. The
  1842. default is the character `k' because we use the same key in the agenda."
  1843. :group 'org-agenda
  1844. :type 'sexp)
  1845. (eval-after-load "calendar"
  1846. '(progn
  1847. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1848. 'org-calendar-goto-agenda)
  1849. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1850. 'org-agenda-action)))
  1851. (defgroup org-latex nil
  1852. "Options for embedding LaTeX code into Org-mode."
  1853. :tag "Org LaTeX"
  1854. :group 'org)
  1855. (defcustom org-format-latex-options
  1856. '(:foreground default :background default :scale 1.0
  1857. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1858. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1859. "Options for creating images from LaTeX fragments.
  1860. This is a property list with the following properties:
  1861. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1862. `default' means use the foreground of the default face.
  1863. :background the background color, or \"Transparent\".
  1864. `default' means use the background of the default face.
  1865. :scale a scaling factor for the size of the images.
  1866. :html-foreground, :html-background, :html-scale
  1867. the same numbers for HTML export.
  1868. :matchers a list indicating which matchers should be used to
  1869. find LaTeX fragments. Valid members of this list are:
  1870. \"begin\" find environments
  1871. \"$\" find math expressions surrounded by $...$
  1872. \"$$\" find math expressions surrounded by $$....$$
  1873. \"\\(\" find math expressions surrounded by \\(...\\)
  1874. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1875. :group 'org-latex
  1876. :type 'plist)
  1877. (defcustom org-format-latex-header "\\documentclass{article}
  1878. \\usepackage{fullpage} % do not remove
  1879. \\usepackage{amssymb}
  1880. \\usepackage[usenames]{color}
  1881. \\usepackage{amsmath}
  1882. \\usepackage{latexsym}
  1883. \\usepackage[mathscr]{eucal}
  1884. \\pagestyle{empty} % do not remove"
  1885. "The document header used for processing LaTeX fragments."
  1886. :group 'org-latex
  1887. :type 'string)
  1888. (defgroup org-font-lock nil
  1889. "Font-lock settings for highlighting in Org-mode."
  1890. :tag "Org Font Lock"
  1891. :group 'org)
  1892. (defcustom org-level-color-stars-only nil
  1893. "Non-nil means fontify only the stars in each headline.
  1894. When nil, the entire headline is fontified.
  1895. Changing it requires restart of `font-lock-mode' to become effective
  1896. also in regions already fontified."
  1897. :group 'org-font-lock
  1898. :type 'boolean)
  1899. (defcustom org-hide-leading-stars nil
  1900. "Non-nil means, hide the first N-1 stars in a headline.
  1901. This works by using the face `org-hide' for these stars. This
  1902. face is white for a light background, and black for a dark
  1903. background. You may have to customize the face `org-hide' to
  1904. make this work.
  1905. Changing it requires restart of `font-lock-mode' to become effective
  1906. also in regions already fontified.
  1907. You may also set this on a per-file basis by adding one of the following
  1908. lines to the buffer:
  1909. #+STARTUP: hidestars
  1910. #+STARTUP: showstars"
  1911. :group 'org-font-lock
  1912. :type 'boolean)
  1913. (defcustom org-fontify-done-headline nil
  1914. "Non-nil means, change the face of a headline if it is marked DONE.
  1915. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1916. When this is non-nil, the headline after the keyword is set to the
  1917. `org-headline-done' as an additional indication."
  1918. :group 'org-font-lock
  1919. :type 'boolean)
  1920. (defcustom org-fontify-emphasized-text t
  1921. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1922. Changing this variable requires a restart of Emacs to take effect."
  1923. :group 'org-font-lock
  1924. :type 'boolean)
  1925. (defcustom org-highlight-latex-fragments-and-specials nil
  1926. "Non-nil means, fontify what is treated specially by the exporters."
  1927. :group 'org-font-lock
  1928. :type 'boolean)
  1929. (defcustom org-hide-emphasis-markers nil
  1930. "Non-nil mean font-lock should hide the emphasis marker characters."
  1931. :group 'org-font-lock
  1932. :type 'boolean)
  1933. (defvar org-emph-re nil
  1934. "Regular expression for matching emphasis.")
  1935. (defvar org-verbatim-re nil
  1936. "Regular expression for matching verbatim text.")
  1937. (defvar org-emphasis-regexp-components) ; defined just below
  1938. (defvar org-emphasis-alist) ; defined just below
  1939. (defun org-set-emph-re (var val)
  1940. "Set variable and compute the emphasis regular expression."
  1941. (set var val)
  1942. (when (and (boundp 'org-emphasis-alist)
  1943. (boundp 'org-emphasis-regexp-components)
  1944. org-emphasis-alist org-emphasis-regexp-components)
  1945. (let* ((e org-emphasis-regexp-components)
  1946. (pre (car e))
  1947. (post (nth 1 e))
  1948. (border (nth 2 e))
  1949. (body (nth 3 e))
  1950. (nl (nth 4 e))
  1951. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1952. (body1 (concat body "*?"))
  1953. (markers (mapconcat 'car org-emphasis-alist ""))
  1954. (vmarkers (mapconcat
  1955. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1956. org-emphasis-alist "")))
  1957. ;; make sure special characters appear at the right position in the class
  1958. (if (string-match "\\^" markers)
  1959. (setq markers (concat (replace-match "" t t markers) "^")))
  1960. (if (string-match "-" markers)
  1961. (setq markers (concat (replace-match "" t t markers) "-")))
  1962. (if (string-match "\\^" vmarkers)
  1963. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1964. (if (string-match "-" vmarkers)
  1965. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1966. (if (> nl 0)
  1967. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1968. (int-to-string nl) "\\}")))
  1969. ;; Make the regexp
  1970. (setq org-emph-re
  1971. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1972. "\\("
  1973. "\\([" markers "]\\)"
  1974. "\\("
  1975. "[^" border "]\\|"
  1976. "[^" border (if (and nil stacked) markers) "]"
  1977. body1
  1978. "[^" border (if (and nil stacked) markers) "]"
  1979. "\\)"
  1980. "\\3\\)"
  1981. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1982. (setq org-verbatim-re
  1983. (concat "\\([" pre "]\\|^\\)"
  1984. "\\("
  1985. "\\([" vmarkers "]\\)"
  1986. "\\("
  1987. "[^" border "]\\|"
  1988. "[^" border "]"
  1989. body1
  1990. "[^" border "]"
  1991. "\\)"
  1992. "\\3\\)"
  1993. "\\([" post "]\\|$\\)")))))
  1994. (defcustom org-emphasis-regexp-components
  1995. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1996. "Components used to build the regular expression for emphasis.
  1997. This is a list with 6 entries. Terminology: In an emphasis string
  1998. like \" *strong word* \", we call the initial space PREMATCH, the final
  1999. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2000. and \"trong wor\" is the body. The different components in this variable
  2001. specify what is allowed/forbidden in each part:
  2002. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2003. post Chars allowed as postmatch. End of line will be allowed too.
  2004. border The chars *forbidden* as border characters.
  2005. body-regexp A regexp like \".\" to match a body character. Don't use
  2006. non-shy groups here, and don't allow newline here.
  2007. newline The maximum number of newlines allowed in an emphasis exp.
  2008. Use customize to modify this, or restart Emacs after changing it."
  2009. :group 'org-font-lock
  2010. :set 'org-set-emph-re
  2011. :type '(list
  2012. (sexp :tag "Allowed chars in pre ")
  2013. (sexp :tag "Allowed chars in post ")
  2014. (sexp :tag "Forbidden chars in border ")
  2015. (sexp :tag "Regexp for body ")
  2016. (integer :tag "number of newlines allowed")
  2017. (option (boolean :tag "Please ignore this button"))))
  2018. (defcustom org-emphasis-alist
  2019. `(("*" bold "<b>" "</b>")
  2020. ("/" italic "<i>" "</i>")
  2021. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2022. ("=" org-code "<code>" "</code>" verbatim)
  2023. ("~" org-verbatim "<code>" "</code>" verbatim)
  2024. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2025. "<del>" "</del>")
  2026. )
  2027. "Special syntax for emphasized text.
  2028. Text starting and ending with a special character will be emphasized, for
  2029. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2030. characters, the face to be used by font-lock for highlighting in Org-mode
  2031. Emacs buffers, and the HTML tags to be used for this.
  2032. Use customize to modify this, or restart Emacs after changing it."
  2033. :group 'org-font-lock
  2034. :set 'org-set-emph-re
  2035. :type '(repeat
  2036. (list
  2037. (string :tag "Marker character")
  2038. (choice
  2039. (face :tag "Font-lock-face")
  2040. (plist :tag "Face property list"))
  2041. (string :tag "HTML start tag")
  2042. (string :tag "HTML end tag")
  2043. (option (const verbatim)))))
  2044. ;;; Miscellaneous options
  2045. (defgroup org-completion nil
  2046. "Completion in Org-mode."
  2047. :tag "Org Completion"
  2048. :group 'org)
  2049. (defcustom org-completion-fallback-command 'hippie-expand
  2050. "The expansion command called by \\[org-complete] in normal context.
  2051. Normal means, no org-mode-specific context."
  2052. :group 'org-completion
  2053. :type 'function)
  2054. ;;; Functions and variables from ther packages
  2055. ;; Declared here to avoid compiler warnings
  2056. ;; XEmacs only
  2057. (defvar outline-mode-menu-heading)
  2058. (defvar outline-mode-menu-show)
  2059. (defvar outline-mode-menu-hide)
  2060. (defvar zmacs-regions) ; XEmacs regions
  2061. ;; Emacs only
  2062. (defvar mark-active)
  2063. ;; Various packages
  2064. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2065. (declare-function calendar-forward-day "cal-move" (arg))
  2066. (declare-function calendar-goto-date "cal-move" (date))
  2067. (declare-function calendar-goto-today "cal-move" ())
  2068. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2069. (defvar calc-embedded-close-formula)
  2070. (defvar calc-embedded-open-formula)
  2071. (declare-function cdlatex-tab "ext:cdlatex" ())
  2072. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2073. (defvar font-lock-unfontify-region-function)
  2074. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2075. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2076. (defvar iswitchb-temp-buflist)
  2077. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2078. (declare-function org-agenda-skip "org-agenda" ())
  2079. (declare-function org-format-agenda-item "org-agenda"
  2080. (extra txt &optional category tags dotime noprefix remove-re))
  2081. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2082. (declare-function org-agenda-change-all-lines "org-agenda"
  2083. (newhead hdmarker &optional fixface))
  2084. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2085. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2086. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2087. (beg end))
  2088. (declare-function parse-time-string "parse-time" (string))
  2089. (declare-function remember "remember" (&optional initial))
  2090. (declare-function remember-buffer-desc "remember" ())
  2091. (declare-function remember-finalize "remember" ())
  2092. (defvar remember-save-after-remembering)
  2093. (defvar remember-data-file)
  2094. (defvar remember-register)
  2095. (defvar remember-buffer)
  2096. (defvar remember-handler-functions)
  2097. (defvar remember-annotation-functions)
  2098. (defvar texmathp-why)
  2099. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2100. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2101. (defvar w3m-current-url)
  2102. (defvar w3m-current-title)
  2103. (defvar org-latex-regexps)
  2104. ;;; Autoload and prepare some org modules
  2105. ;; Some table stuff that needs to be defined here, because it is used
  2106. ;; by the functions setting up org-mode or checking for table context.
  2107. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2108. "Detects an org-type or table-type table.")
  2109. (defconst org-table-line-regexp "^[ \t]*|"
  2110. "Detects an org-type table line.")
  2111. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2112. "Detects an org-type table line.")
  2113. (defconst org-table-hline-regexp "^[ \t]*|-"
  2114. "Detects an org-type table hline.")
  2115. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2116. "Detects a table-type table hline.")
  2117. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2118. "Searching from within a table (any type) this finds the first line
  2119. outside the table.")
  2120. ;; Autoload the functions in org-table.el that are needed by functions here.
  2121. (eval-and-compile
  2122. (org-autoload "org-table"
  2123. '(org-table-align org-table-begin org-table-blank-field
  2124. org-table-convert org-table-convert-region org-table-copy-down
  2125. org-table-copy-region org-table-create
  2126. org-table-create-or-convert-from-region
  2127. org-table-create-with-table.el org-table-current-dline
  2128. org-table-cut-region org-table-delete-column org-table-edit-field
  2129. org-table-edit-formulas org-table-end org-table-eval-formula
  2130. org-table-export org-table-field-info
  2131. org-table-get-stored-formulas org-table-goto-column
  2132. org-table-hline-and-move org-table-import org-table-insert-column
  2133. org-table-insert-hline org-table-insert-row org-table-iterate
  2134. org-table-justify-field-maybe org-table-kill-row
  2135. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2136. org-table-move-column org-table-move-column-left
  2137. org-table-move-column-right org-table-move-row
  2138. org-table-move-row-down org-table-move-row-up
  2139. org-table-next-field org-table-next-row org-table-paste-rectangle
  2140. org-table-previous-field org-table-recalculate
  2141. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2142. org-table-toggle-coordinate-overlays
  2143. org-table-toggle-formula-debugger org-table-wrap-region
  2144. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2145. (defun org-at-table-p (&optional table-type)
  2146. "Return t if the cursor is inside an org-type table.
  2147. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2148. (if org-enable-table-editor
  2149. (save-excursion
  2150. (beginning-of-line 1)
  2151. (looking-at (if table-type org-table-any-line-regexp
  2152. org-table-line-regexp)))
  2153. nil))
  2154. (defsubst org-table-p () (org-at-table-p))
  2155. (defun org-at-table.el-p ()
  2156. "Return t if and only if we are at a table.el table."
  2157. (and (org-at-table-p 'any)
  2158. (save-excursion
  2159. (goto-char (org-table-begin 'any))
  2160. (looking-at org-table1-hline-regexp))))
  2161. (defun org-table-recognize-table.el ()
  2162. "If there is a table.el table nearby, recognize it and move into it."
  2163. (if org-table-tab-recognizes-table.el
  2164. (if (org-at-table.el-p)
  2165. (progn
  2166. (beginning-of-line 1)
  2167. (if (looking-at org-table-dataline-regexp)
  2168. nil
  2169. (if (looking-at org-table1-hline-regexp)
  2170. (progn
  2171. (beginning-of-line 2)
  2172. (if (looking-at org-table-any-border-regexp)
  2173. (beginning-of-line -1)))))
  2174. (if (re-search-forward "|" (org-table-end t) t)
  2175. (progn
  2176. (require 'table)
  2177. (if (table--at-cell-p (point))
  2178. t
  2179. (message "recognizing table.el table...")
  2180. (table-recognize-table)
  2181. (message "recognizing table.el table...done")))
  2182. (error "This should not happen..."))
  2183. t)
  2184. nil)
  2185. nil))
  2186. (defun org-at-table-hline-p ()
  2187. "Return t if the cursor is inside a hline in a table."
  2188. (if org-enable-table-editor
  2189. (save-excursion
  2190. (beginning-of-line 1)
  2191. (looking-at org-table-hline-regexp))
  2192. nil))
  2193. (defvar org-table-clean-did-remove-column nil)
  2194. (defun org-table-map-tables (function)
  2195. "Apply FUNCTION to the start of all tables in the buffer."
  2196. (save-excursion
  2197. (save-restriction
  2198. (widen)
  2199. (goto-char (point-min))
  2200. (while (re-search-forward org-table-any-line-regexp nil t)
  2201. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2202. (beginning-of-line 1)
  2203. (if (looking-at org-table-line-regexp)
  2204. (save-excursion (funcall function)))
  2205. (re-search-forward org-table-any-border-regexp nil 1))))
  2206. (message "Mapping tables: done"))
  2207. ;; Declare and autoload functions from org-exp.el
  2208. (declare-function org-default-export-plist "org-exp")
  2209. (declare-function org-infile-export-plist "org-exp")
  2210. (declare-function org-get-current-options "org-exp")
  2211. (eval-and-compile
  2212. (org-autoload "org-exp"
  2213. '(org-export org-export-as-ascii org-export-visible
  2214. org-insert-export-options-template org-export-as-html-and-open
  2215. org-export-as-html-batch org-export-as-html-to-buffer
  2216. org-replace-region-by-html org-export-region-as-html
  2217. org-export-as-html org-export-icalendar-this-file
  2218. org-export-icalendar-all-agenda-files
  2219. org-table-clean-before-export
  2220. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2221. ;; Declare and autoload functions from org-agenda.el
  2222. (eval-and-compile
  2223. (org-autoload "org-agenda"
  2224. '(org-agenda org-agenda-list org-search-view
  2225. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2226. org-diary org-agenda-to-appt)))
  2227. ;; Autoload org-remember
  2228. (eval-and-compile
  2229. (org-autoload "org-remember"
  2230. '(org-remember-insinuate org-remember-annotation
  2231. org-remember-apply-template org-remember org-remember-handler)))
  2232. ;; Autoload org-clock.el
  2233. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2234. (beg end))
  2235. (declare-function org-update-mode-line "org-clock" ())
  2236. (defvar org-clock-start-time)
  2237. (defvar org-clock-marker (make-marker)
  2238. "Marker recording the last clock-in.")
  2239. (eval-and-compile
  2240. (org-autoload
  2241. "org-clock"
  2242. '(org-clock-in org-clock-out org-clock-cancel
  2243. org-clock-goto org-clock-sum org-clock-display
  2244. org-remove-clock-overlays org-clock-report
  2245. org-clocktable-shift org-dblock-write:clocktable
  2246. org-get-clocktable)))
  2247. (defun org-clock-update-time-maybe ()
  2248. "If this is a CLOCK line, update it and return t.
  2249. Otherwise, return nil."
  2250. (interactive)
  2251. (save-excursion
  2252. (beginning-of-line 1)
  2253. (skip-chars-forward " \t")
  2254. (when (looking-at org-clock-string)
  2255. (let ((re (concat "[ \t]*" org-clock-string
  2256. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2257. "\\([ \t]*=>.*\\)?\\)?"))
  2258. ts te h m s neg)
  2259. (cond
  2260. ((not (looking-at re))
  2261. nil)
  2262. ((not (match-end 2))
  2263. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2264. (> org-clock-marker (point))
  2265. (<= org-clock-marker (point-at-eol)))
  2266. ;; The clock is running here
  2267. (setq org-clock-start-time
  2268. (apply 'encode-time
  2269. (org-parse-time-string (match-string 1))))
  2270. (org-update-mode-line)))
  2271. (t
  2272. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2273. (end-of-line 1)
  2274. (setq ts (match-string 1)
  2275. te (match-string 3))
  2276. (setq s (- (time-to-seconds
  2277. (apply 'encode-time (org-parse-time-string te)))
  2278. (time-to-seconds
  2279. (apply 'encode-time (org-parse-time-string ts))))
  2280. neg (< s 0)
  2281. s (abs s)
  2282. h (floor (/ s 3600))
  2283. s (- s (* 3600 h))
  2284. m (floor (/ s 60))
  2285. s (- s (* 60 s)))
  2286. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2287. t))))))
  2288. (defun org-check-running-clock ()
  2289. "Check if the current buffer contains the running clock.
  2290. If yes, offer to stop it and to save the buffer with the changes."
  2291. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2292. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2293. (buffer-name))))
  2294. (org-clock-out)
  2295. (when (y-or-n-p "Save changed buffer?")
  2296. (save-buffer))))
  2297. (defun org-clocktable-try-shift (dir n)
  2298. "Check if this line starts a clock table, if yes, shift the time block."
  2299. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2300. (org-clocktable-shift dir n)))
  2301. ;; Autoload archiving code
  2302. ;; The stuff that is needed for cycling and tags has to be defined here.
  2303. (defgroup org-archive nil
  2304. "Options concerning archiving in Org-mode."
  2305. :tag "Org Archive"
  2306. :group 'org-structure)
  2307. (defcustom org-archive-location "%s_archive::"
  2308. "The location where subtrees should be archived.
  2309. Otherwise, the value of this variable is a string, consisting of two
  2310. parts, separated by a double-colon.
  2311. The first part is a file name - when omitted, archiving happens in the same
  2312. file. %s will be replaced by the current file name (without directory part).
  2313. Archiving to a different file is useful to keep archived entries from
  2314. contributing to the Org-mode Agenda.
  2315. The part after the double colon is a headline. The archived entries will be
  2316. filed under that headline. When omitted, the subtrees are simply filed away
  2317. at the end of the file, as top-level entries.
  2318. Here are a few examples:
  2319. \"%s_archive::\"
  2320. If the current file is Projects.org, archive in file
  2321. Projects.org_archive, as top-level trees. This is the default.
  2322. \"::* Archived Tasks\"
  2323. Archive in the current file, under the top-level headline
  2324. \"* Archived Tasks\".
  2325. \"~/org/archive.org::\"
  2326. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2327. \"basement::** Finished Tasks\"
  2328. Archive in file ./basement (relative path), as level 3 trees
  2329. below the level 2 heading \"** Finished Tasks\".
  2330. You may set this option on a per-file basis by adding to the buffer a
  2331. line like
  2332. #+ARCHIVE: basement::** Finished Tasks
  2333. You may also define it locally for a subtree by setting an ARCHIVE property
  2334. in the entry. If such a property is found in an entry, or anywhere up
  2335. the hierarchy, it will be used."
  2336. :group 'org-archive
  2337. :type 'string)
  2338. (defcustom org-archive-tag "ARCHIVE"
  2339. "The tag that marks a subtree as archived.
  2340. An archived subtree does not open during visibility cycling, and does
  2341. not contribute to the agenda listings.
  2342. After changing this, font-lock must be restarted in the relevant buffers to
  2343. get the proper fontification."
  2344. :group 'org-archive
  2345. :group 'org-keywords
  2346. :type 'string)
  2347. (defcustom org-agenda-skip-archived-trees t
  2348. "Non-nil means, the agenda will skip any items located in archived trees.
  2349. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2350. variable is no longer recommended, you should leave it at the value t.
  2351. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2352. :group 'org-archive
  2353. :group 'org-agenda-skip
  2354. :type 'boolean)
  2355. (defcustom org-cycle-open-archived-trees nil
  2356. "Non-nil means, `org-cycle' will open archived trees.
  2357. An archived tree is a tree marked with the tag ARCHIVE.
  2358. When nil, archived trees will stay folded. You can still open them with
  2359. normal outline commands like `show-all', but not with the cycling commands."
  2360. :group 'org-archive
  2361. :group 'org-cycle
  2362. :type 'boolean)
  2363. (defcustom org-sparse-tree-open-archived-trees nil
  2364. "Non-nil means sparse tree construction shows matches in archived trees.
  2365. When nil, matches in these trees are highlighted, but the trees are kept in
  2366. collapsed state."
  2367. :group 'org-archive
  2368. :group 'org-sparse-trees
  2369. :type 'boolean)
  2370. (defun org-cycle-hide-archived-subtrees (state)
  2371. "Re-hide all archived subtrees after a visibility state change."
  2372. (when (and (not org-cycle-open-archived-trees)
  2373. (not (memq state '(overview folded))))
  2374. (save-excursion
  2375. (let* ((globalp (memq state '(contents all)))
  2376. (beg (if globalp (point-min) (point)))
  2377. (end (if globalp (point-max) (org-end-of-subtree t))))
  2378. (org-hide-archived-subtrees beg end)
  2379. (goto-char beg)
  2380. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2381. (message "%s" (substitute-command-keys
  2382. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2383. (defun org-force-cycle-archived ()
  2384. "Cycle subtree even if it is archived."
  2385. (interactive)
  2386. (setq this-command 'org-cycle)
  2387. (let ((org-cycle-open-archived-trees t))
  2388. (call-interactively 'org-cycle)))
  2389. (defun org-hide-archived-subtrees (beg end)
  2390. "Re-hide all archived subtrees after a visibility state change."
  2391. (save-excursion
  2392. (let* ((re (concat ":" org-archive-tag ":")))
  2393. (goto-char beg)
  2394. (while (re-search-forward re end t)
  2395. (and (org-on-heading-p) (hide-subtree))
  2396. (org-end-of-subtree t)))))
  2397. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2398. (eval-and-compile
  2399. (org-autoload "org-archive"
  2400. '(org-add-archive-files org-archive-subtree
  2401. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2402. ;; Autoload Column View Code
  2403. (declare-function org-columns-number-to-string "org-colview")
  2404. (declare-function org-columns-get-format-and-top-level "org-colview")
  2405. (declare-function org-columns-compute "org-colview")
  2406. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2407. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2408. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2409. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2410. ;; Autoload ID code
  2411. (org-autoload "org-id"
  2412. '(org-id-get-create org-id-new org-id-copy org-id-get
  2413. org-id-get-with-outline-path-completion
  2414. org-id-get-with-outline-drilling
  2415. org-id-goto org-id-find))
  2416. ;;; Variables for pre-computed regular expressions, all buffer local
  2417. (defvar org-drawer-regexp nil
  2418. "Matches first line of a hidden block.")
  2419. (make-variable-buffer-local 'org-drawer-regexp)
  2420. (defvar org-todo-regexp nil
  2421. "Matches any of the TODO state keywords.")
  2422. (make-variable-buffer-local 'org-todo-regexp)
  2423. (defvar org-not-done-regexp nil
  2424. "Matches any of the TODO state keywords except the last one.")
  2425. (make-variable-buffer-local 'org-not-done-regexp)
  2426. (defvar org-todo-line-regexp nil
  2427. "Matches a headline and puts TODO state into group 2 if present.")
  2428. (make-variable-buffer-local 'org-todo-line-regexp)
  2429. (defvar org-complex-heading-regexp nil
  2430. "Matches a headline and puts everything into groups:
  2431. group 1: the stars
  2432. group 2: The todo keyword, maybe
  2433. group 3: Priority cookie
  2434. group 4: True headline
  2435. group 5: Tags")
  2436. (make-variable-buffer-local 'org-complex-heading-regexp)
  2437. (defvar org-todo-line-tags-regexp nil
  2438. "Matches a headline and puts TODO state into group 2 if present.
  2439. Also put tags into group 4 if tags are present.")
  2440. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2441. (defvar org-nl-done-regexp nil
  2442. "Matches newline followed by a headline with the DONE keyword.")
  2443. (make-variable-buffer-local 'org-nl-done-regexp)
  2444. (defvar org-looking-at-done-regexp nil
  2445. "Matches the DONE keyword a point.")
  2446. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2447. (defvar org-ds-keyword-length 12
  2448. "Maximum length of the Deadline and SCHEDULED keywords.")
  2449. (make-variable-buffer-local 'org-ds-keyword-length)
  2450. (defvar org-deadline-regexp nil
  2451. "Matches the DEADLINE keyword.")
  2452. (make-variable-buffer-local 'org-deadline-regexp)
  2453. (defvar org-deadline-time-regexp nil
  2454. "Matches the DEADLINE keyword together with a time stamp.")
  2455. (make-variable-buffer-local 'org-deadline-time-regexp)
  2456. (defvar org-deadline-line-regexp nil
  2457. "Matches the DEADLINE keyword and the rest of the line.")
  2458. (make-variable-buffer-local 'org-deadline-line-regexp)
  2459. (defvar org-scheduled-regexp nil
  2460. "Matches the SCHEDULED keyword.")
  2461. (make-variable-buffer-local 'org-scheduled-regexp)
  2462. (defvar org-scheduled-time-regexp nil
  2463. "Matches the SCHEDULED keyword together with a time stamp.")
  2464. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2465. (defvar org-closed-time-regexp nil
  2466. "Matches the CLOSED keyword together with a time stamp.")
  2467. (make-variable-buffer-local 'org-closed-time-regexp)
  2468. (defvar org-keyword-time-regexp nil
  2469. "Matches any of the 4 keywords, together with the time stamp.")
  2470. (make-variable-buffer-local 'org-keyword-time-regexp)
  2471. (defvar org-keyword-time-not-clock-regexp nil
  2472. "Matches any of the 3 keywords, together with the time stamp.")
  2473. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2474. (defvar org-maybe-keyword-time-regexp nil
  2475. "Matches a timestamp, possibly preceeded by a keyword.")
  2476. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2477. (defvar org-planning-or-clock-line-re nil
  2478. "Matches a line with planning or clock info.")
  2479. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2480. (defconst org-plain-time-of-day-regexp
  2481. (concat
  2482. "\\(\\<[012]?[0-9]"
  2483. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2484. "\\(--?"
  2485. "\\(\\<[012]?[0-9]"
  2486. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2487. "\\)?")
  2488. "Regular expression to match a plain time or time range.
  2489. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2490. groups carry important information:
  2491. 0 the full match
  2492. 1 the first time, range or not
  2493. 8 the second time, if it is a range.")
  2494. (defconst org-plain-time-extension-regexp
  2495. (concat
  2496. "\\(\\<[012]?[0-9]"
  2497. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2498. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2499. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2500. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2501. groups carry important information:
  2502. 0 the full match
  2503. 7 hours of duration
  2504. 9 minutes of duration")
  2505. (defconst org-stamp-time-of-day-regexp
  2506. (concat
  2507. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2508. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2509. "\\(--?"
  2510. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2511. "Regular expression to match a timestamp time or time range.
  2512. After a match, the following groups carry important information:
  2513. 0 the full match
  2514. 1 date plus weekday, for backreferencing to make sure both times on same day
  2515. 2 the first time, range or not
  2516. 4 the second time, if it is a range.")
  2517. (defconst org-startup-options
  2518. '(("fold" org-startup-folded t)
  2519. ("overview" org-startup-folded t)
  2520. ("nofold" org-startup-folded nil)
  2521. ("showall" org-startup-folded nil)
  2522. ("content" org-startup-folded content)
  2523. ("hidestars" org-hide-leading-stars t)
  2524. ("showstars" org-hide-leading-stars nil)
  2525. ("odd" org-odd-levels-only t)
  2526. ("oddeven" org-odd-levels-only nil)
  2527. ("align" org-startup-align-all-tables t)
  2528. ("noalign" org-startup-align-all-tables nil)
  2529. ("customtime" org-display-custom-times t)
  2530. ("logdone" org-log-done time)
  2531. ("lognotedone" org-log-done note)
  2532. ("nologdone" org-log-done nil)
  2533. ("lognoteclock-out" org-log-note-clock-out t)
  2534. ("nolognoteclock-out" org-log-note-clock-out nil)
  2535. ("logrepeat" org-log-repeat state)
  2536. ("lognoterepeat" org-log-repeat note)
  2537. ("nologrepeat" org-log-repeat nil)
  2538. ("constcgs" constants-unit-system cgs)
  2539. ("constSI" constants-unit-system SI))
  2540. "Variable associated with STARTUP options for org-mode.
  2541. Each element is a list of three items: The startup options as written
  2542. in the #+STARTUP line, the corresponding variable, and the value to
  2543. set this variable to if the option is found. An optional forth element PUSH
  2544. means to push this value onto the list in the variable.")
  2545. (defun org-set-regexps-and-options ()
  2546. "Precompute regular expressions for current buffer."
  2547. (when (org-mode-p)
  2548. (org-set-local 'org-todo-kwd-alist nil)
  2549. (org-set-local 'org-todo-key-alist nil)
  2550. (org-set-local 'org-todo-key-trigger nil)
  2551. (org-set-local 'org-todo-keywords-1 nil)
  2552. (org-set-local 'org-done-keywords nil)
  2553. (org-set-local 'org-todo-heads nil)
  2554. (org-set-local 'org-todo-sets nil)
  2555. (org-set-local 'org-todo-log-states nil)
  2556. (org-set-local 'org-file-properties nil)
  2557. (org-set-local 'org-file-tags nil)
  2558. (let ((re (org-make-options-regexp
  2559. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2560. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2561. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2562. (splitre "[ \t]+")
  2563. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2564. tail sep kws1 prio props ftags drawers
  2565. ext-setup-or-nil setup-contents (start 0))
  2566. (save-excursion
  2567. (save-restriction
  2568. (widen)
  2569. (goto-char (point-min))
  2570. (while (or (and ext-setup-or-nil
  2571. (string-match re ext-setup-or-nil start)
  2572. (setq start (match-end 0)))
  2573. (and (setq ext-setup-or-nil nil start 0)
  2574. (re-search-forward re nil t)))
  2575. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2576. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2577. (cond
  2578. ((equal key "CATEGORY")
  2579. (if (string-match "[ \t]+$" value)
  2580. (setq value (replace-match "" t t value)))
  2581. (setq cat value))
  2582. ((member key '("SEQ_TODO" "TODO"))
  2583. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2584. ((equal key "TYP_TODO")
  2585. (push (cons 'type (org-split-string value splitre)) kwds))
  2586. ((equal key "TAGS")
  2587. (setq tags (append tags (org-split-string value splitre))))
  2588. ((equal key "COLUMNS")
  2589. (org-set-local 'org-columns-default-format value))
  2590. ((equal key "LINK")
  2591. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2592. (push (cons (match-string 1 value)
  2593. (org-trim (match-string 2 value)))
  2594. links)))
  2595. ((equal key "PRIORITIES")
  2596. (setq prio (org-split-string value " +")))
  2597. ((equal key "PROPERTY")
  2598. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2599. (push (cons (match-string 1 value) (match-string 2 value))
  2600. props)))
  2601. ((equal key "FILETAGS")
  2602. (when (string-match "\\S-" value)
  2603. (setq ftags
  2604. (append
  2605. ftags
  2606. (apply 'append
  2607. (mapcar (lambda (x) (org-split-string x ":"))
  2608. (org-split-string value)))))))
  2609. ((equal key "DRAWERS")
  2610. (setq drawers (org-split-string value splitre)))
  2611. ((equal key "CONSTANTS")
  2612. (setq const (append const (org-split-string value splitre))))
  2613. ((equal key "STARTUP")
  2614. (let ((opts (org-split-string value splitre))
  2615. l var val)
  2616. (while (setq l (pop opts))
  2617. (when (setq l (assoc l org-startup-options))
  2618. (setq var (nth 1 l) val (nth 2 l))
  2619. (if (not (nth 3 l))
  2620. (set (make-local-variable var) val)
  2621. (if (not (listp (symbol-value var)))
  2622. (set (make-local-variable var) nil))
  2623. (set (make-local-variable var) (symbol-value var))
  2624. (add-to-list var val))))))
  2625. ((equal key "ARCHIVE")
  2626. (string-match " *$" value)
  2627. (setq arch (replace-match "" t t value))
  2628. (remove-text-properties 0 (length arch)
  2629. '(face t fontified t) arch))
  2630. ((equal key "SETUPFILE")
  2631. (setq setup-contents (org-file-contents
  2632. (expand-file-name
  2633. (org-remove-double-quotes value))
  2634. 'noerror))
  2635. (if (not ext-setup-or-nil)
  2636. (setq ext-setup-or-nil setup-contents start 0)
  2637. (setq ext-setup-or-nil
  2638. (concat (substring ext-setup-or-nil 0 start)
  2639. "\n" setup-contents "\n"
  2640. (substring ext-setup-or-nil start)))))
  2641. ))))
  2642. (when cat
  2643. (org-set-local 'org-category (intern cat))
  2644. (push (cons "CATEGORY" cat) props))
  2645. (when prio
  2646. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2647. (setq prio (mapcar 'string-to-char prio))
  2648. (org-set-local 'org-highest-priority (nth 0 prio))
  2649. (org-set-local 'org-lowest-priority (nth 1 prio))
  2650. (org-set-local 'org-default-priority (nth 2 prio)))
  2651. (and props (org-set-local 'org-file-properties (nreverse props)))
  2652. (and ftags (org-set-local 'org-file-tags ftags))
  2653. (and drawers (org-set-local 'org-drawers drawers))
  2654. (and arch (org-set-local 'org-archive-location arch))
  2655. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2656. ;; Process the TODO keywords
  2657. (unless kwds
  2658. ;; Use the global values as if they had been given locally.
  2659. (setq kwds (default-value 'org-todo-keywords))
  2660. (if (stringp (car kwds))
  2661. (setq kwds (list (cons org-todo-interpretation
  2662. (default-value 'org-todo-keywords)))))
  2663. (setq kwds (reverse kwds)))
  2664. (setq kwds (nreverse kwds))
  2665. (let (inter kws kw)
  2666. (while (setq kws (pop kwds))
  2667. (setq inter (pop kws) sep (member "|" kws)
  2668. kws0 (delete "|" (copy-sequence kws))
  2669. kwsa nil
  2670. kws1 (mapcar
  2671. (lambda (x)
  2672. ;; 1 2
  2673. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2674. (progn
  2675. (setq kw (match-string 1 x)
  2676. key (and (match-end 2) (match-string 2 x))
  2677. log (org-extract-log-state-settings x))
  2678. (push (cons kw (and key (string-to-char key))) kwsa)
  2679. (and log (push log org-todo-log-states))
  2680. kw)
  2681. (error "Invalid TODO keyword %s" x)))
  2682. kws0)
  2683. kwsa (if kwsa (append '((:startgroup))
  2684. (nreverse kwsa)
  2685. '((:endgroup))))
  2686. hw (car kws1)
  2687. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2688. tail (list inter hw (car dws) (org-last dws)))
  2689. (add-to-list 'org-todo-heads hw 'append)
  2690. (push kws1 org-todo-sets)
  2691. (setq org-done-keywords (append org-done-keywords dws nil))
  2692. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2693. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2694. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2695. (setq org-todo-sets (nreverse org-todo-sets)
  2696. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2697. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2698. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2699. ;; Process the constants
  2700. (when const
  2701. (let (e cst)
  2702. (while (setq e (pop const))
  2703. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2704. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2705. (setq org-table-formula-constants-local cst)))
  2706. ;; Process the tags.
  2707. (when tags
  2708. (let (e tgs)
  2709. (while (setq e (pop tags))
  2710. (cond
  2711. ((equal e "{") (push '(:startgroup) tgs))
  2712. ((equal e "}") (push '(:endgroup) tgs))
  2713. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2714. (push (cons (match-string 1 e)
  2715. (string-to-char (match-string 2 e)))
  2716. tgs))
  2717. (t (push (list e) tgs))))
  2718. (org-set-local 'org-tag-alist nil)
  2719. (while (setq e (pop tgs))
  2720. (or (and (stringp (car e))
  2721. (assoc (car e) org-tag-alist))
  2722. (push e org-tag-alist)))))
  2723. ;; Compute the regular expressions and other local variables
  2724. (if (not org-done-keywords)
  2725. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2726. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2727. (length org-scheduled-string)
  2728. (length org-clock-string)
  2729. (length org-closed-string)))
  2730. org-drawer-regexp
  2731. (concat "^[ \t]*:\\("
  2732. (mapconcat 'regexp-quote org-drawers "\\|")
  2733. "\\):[ \t]*$")
  2734. org-not-done-keywords
  2735. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2736. org-todo-regexp
  2737. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2738. "\\|") "\\)\\>")
  2739. org-not-done-regexp
  2740. (concat "\\<\\("
  2741. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2742. "\\)\\>")
  2743. org-todo-line-regexp
  2744. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2745. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2746. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2747. org-complex-heading-regexp
  2748. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2749. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2750. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2751. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2752. org-nl-done-regexp
  2753. (concat "\n\\*+[ \t]+"
  2754. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2755. "\\)" "\\>")
  2756. org-todo-line-tags-regexp
  2757. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2758. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2759. (org-re
  2760. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2761. org-looking-at-done-regexp
  2762. (concat "^" "\\(?:"
  2763. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2764. "\\>")
  2765. org-deadline-regexp (concat "\\<" org-deadline-string)
  2766. org-deadline-time-regexp
  2767. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2768. org-deadline-line-regexp
  2769. (concat "\\<\\(" org-deadline-string "\\).*")
  2770. org-scheduled-regexp
  2771. (concat "\\<" org-scheduled-string)
  2772. org-scheduled-time-regexp
  2773. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2774. org-closed-time-regexp
  2775. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2776. org-keyword-time-regexp
  2777. (concat "\\<\\(" org-scheduled-string
  2778. "\\|" org-deadline-string
  2779. "\\|" org-closed-string
  2780. "\\|" org-clock-string "\\)"
  2781. " *[[<]\\([^]>]+\\)[]>]")
  2782. org-keyword-time-not-clock-regexp
  2783. (concat "\\<\\(" org-scheduled-string
  2784. "\\|" org-deadline-string
  2785. "\\|" org-closed-string
  2786. "\\)"
  2787. " *[[<]\\([^]>]+\\)[]>]")
  2788. org-maybe-keyword-time-regexp
  2789. (concat "\\(\\<\\(" org-scheduled-string
  2790. "\\|" org-deadline-string
  2791. "\\|" org-closed-string
  2792. "\\|" org-clock-string "\\)\\)?"
  2793. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2794. org-planning-or-clock-line-re
  2795. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2796. "\\|" org-deadline-string
  2797. "\\|" org-closed-string "\\|" org-clock-string
  2798. "\\)\\>\\)")
  2799. )
  2800. (org-compute-latex-and-specials-regexp)
  2801. (org-set-font-lock-defaults))))
  2802. (defun org-file-contents (file &optional noerror)
  2803. "Return the contents of FILE, as a string."
  2804. (if (or (not file)
  2805. (not (file-readable-p file)))
  2806. (if noerror
  2807. (progn
  2808. (message "Cannot read file %s" file)
  2809. (ding) (sit-for 2)
  2810. "")
  2811. (error "Cannot read file %s" file))
  2812. (with-temp-buffer
  2813. (insert-file-contents file)
  2814. (buffer-string))))
  2815. (defun org-extract-log-state-settings (x)
  2816. "Extract the log state setting from a TODO keyword string.
  2817. This will extract info from a string like \"WAIT(w@/!)\"."
  2818. (let (kw key log1 log2)
  2819. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2820. (setq kw (match-string 1 x)
  2821. key (and (match-end 2) (match-string 2 x))
  2822. log1 (and (match-end 3) (match-string 3 x))
  2823. log2 (and (match-end 4) (match-string 4 x)))
  2824. (and (or log1 log2)
  2825. (list kw
  2826. (and log1 (if (equal log1 "!") 'time 'note))
  2827. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2828. (defun org-remove-keyword-keys (list)
  2829. "Remove a pair of parenthesis at the end of each string in LIST."
  2830. (mapcar (lambda (x)
  2831. (if (string-match "(.*)$" x)
  2832. (substring x 0 (match-beginning 0))
  2833. x))
  2834. list))
  2835. ;; FIXME: this could be done much better, using second characters etc.
  2836. (defun org-assign-fast-keys (alist)
  2837. "Assign fast keys to a keyword-key alist.
  2838. Respect keys that are already there."
  2839. (let (new e k c c1 c2 (char ?a))
  2840. (while (setq e (pop alist))
  2841. (cond
  2842. ((equal e '(:startgroup)) (push e new))
  2843. ((equal e '(:endgroup)) (push e new))
  2844. (t
  2845. (setq k (car e) c2 nil)
  2846. (if (cdr e)
  2847. (setq c (cdr e))
  2848. ;; automatically assign a character.
  2849. (setq c1 (string-to-char
  2850. (downcase (substring
  2851. k (if (= (string-to-char k) ?@) 1 0)))))
  2852. (if (or (rassoc c1 new) (rassoc c1 alist))
  2853. (while (or (rassoc char new) (rassoc char alist))
  2854. (setq char (1+ char)))
  2855. (setq c2 c1))
  2856. (setq c (or c2 char)))
  2857. (push (cons k c) new))))
  2858. (nreverse new)))
  2859. ;;; Some variables used in various places
  2860. (defvar org-window-configuration nil
  2861. "Used in various places to store a window configuration.")
  2862. (defvar org-finish-function nil
  2863. "Function to be called when `C-c C-c' is used.
  2864. This is for getting out of special buffers like remember.")
  2865. ;; FIXME: Occasionally check by commenting these, to make sure
  2866. ;; no other functions uses these, forgetting to let-bind them.
  2867. (defvar entry)
  2868. (defvar state)
  2869. (defvar last-state)
  2870. (defvar date)
  2871. (defvar description)
  2872. ;; Defined somewhere in this file, but used before definition.
  2873. (defvar org-html-entities)
  2874. (defvar org-struct-menu)
  2875. (defvar org-org-menu)
  2876. (defvar org-tbl-menu)
  2877. (defvar org-agenda-keymap)
  2878. ;;;; Define the Org-mode
  2879. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2880. (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."))
  2881. ;; We use a before-change function to check if a table might need
  2882. ;; an update.
  2883. (defvar org-table-may-need-update t
  2884. "Indicates that a table might need an update.
  2885. This variable is set by `org-before-change-function'.
  2886. `org-table-align' sets it back to nil.")
  2887. (defun org-before-change-function (beg end)
  2888. "Every change indicates that a table might need an update."
  2889. (setq org-table-may-need-update t))
  2890. (defvar org-mode-map)
  2891. (defvar org-mode-hook nil)
  2892. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2893. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2894. (defvar org-table-buffer-is-an nil)
  2895. (defconst org-outline-regexp "\\*+ ")
  2896. ;;;###autoload
  2897. (define-derived-mode org-mode outline-mode "Org"
  2898. "Outline-based notes management and organizer, alias
  2899. \"Carsten's outline-mode for keeping track of everything.\"
  2900. Org-mode develops organizational tasks around a NOTES file which
  2901. contains information about projects as plain text. Org-mode is
  2902. implemented on top of outline-mode, which is ideal to keep the content
  2903. of large files well structured. It supports ToDo items, deadlines and
  2904. time stamps, which magically appear in the diary listing of the Emacs
  2905. calendar. Tables are easily created with a built-in table editor.
  2906. Plain text URL-like links connect to websites, emails (VM), Usenet
  2907. messages (Gnus), BBDB entries, and any files related to the project.
  2908. For printing and sharing of notes, an Org-mode file (or a part of it)
  2909. can be exported as a structured ASCII or HTML file.
  2910. The following commands are available:
  2911. \\{org-mode-map}"
  2912. ;; Get rid of Outline menus, they are not needed
  2913. ;; Need to do this here because define-derived-mode sets up
  2914. ;; the keymap so late. Still, it is a waste to call this each time
  2915. ;; we switch another buffer into org-mode.
  2916. (if (featurep 'xemacs)
  2917. (when (boundp 'outline-mode-menu-heading)
  2918. ;; Assume this is Greg's port, it used easymenu
  2919. (easy-menu-remove outline-mode-menu-heading)
  2920. (easy-menu-remove outline-mode-menu-show)
  2921. (easy-menu-remove outline-mode-menu-hide))
  2922. (define-key org-mode-map [menu-bar headings] 'undefined)
  2923. (define-key org-mode-map [menu-bar hide] 'undefined)
  2924. (define-key org-mode-map [menu-bar show] 'undefined))
  2925. (org-load-modules-maybe)
  2926. (easy-menu-add org-org-menu)
  2927. (easy-menu-add org-tbl-menu)
  2928. (org-install-agenda-files-menu)
  2929. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2930. (org-add-to-invisibility-spec '(org-cwidth))
  2931. (when (featurep 'xemacs)
  2932. (org-set-local 'line-move-ignore-invisible t))
  2933. (org-set-local 'outline-regexp org-outline-regexp)
  2934. (org-set-local 'outline-level 'org-outline-level)
  2935. (when (and org-ellipsis
  2936. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2937. (fboundp 'make-glyph-code))
  2938. (unless org-display-table
  2939. (setq org-display-table (make-display-table)))
  2940. (set-display-table-slot
  2941. org-display-table 4
  2942. (vconcat (mapcar
  2943. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2944. org-ellipsis)))
  2945. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2946. (setq buffer-display-table org-display-table))
  2947. (org-set-regexps-and-options)
  2948. ;; Calc embedded
  2949. (org-set-local 'calc-embedded-open-mode "# ")
  2950. (modify-syntax-entry ?# "<")
  2951. (modify-syntax-entry ?@ "w")
  2952. (if org-startup-truncated (setq truncate-lines t))
  2953. (org-set-local 'font-lock-unfontify-region-function
  2954. 'org-unfontify-region)
  2955. ;; Activate before-change-function
  2956. (org-set-local 'org-table-may-need-update t)
  2957. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2958. 'local)
  2959. ;; Check for running clock before killing a buffer
  2960. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2961. ;; Paragraphs and auto-filling
  2962. (org-set-autofill-regexps)
  2963. (setq indent-line-function 'org-indent-line-function)
  2964. (org-update-radio-target-regexp)
  2965. ;; Comment characters
  2966. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2967. (org-set-local 'comment-padding " ")
  2968. ;; Align options lines
  2969. (org-set-local
  2970. 'align-mode-rules-list
  2971. '((org-in-buffer-settings
  2972. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2973. (modes . '(org-mode)))))
  2974. ;; Imenu
  2975. (org-set-local 'imenu-create-index-function
  2976. 'org-imenu-get-tree)
  2977. ;; Make isearch reveal context
  2978. (if (or (featurep 'xemacs)
  2979. (not (boundp 'outline-isearch-open-invisible-function)))
  2980. ;; Emacs 21 and XEmacs make use of the hook
  2981. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2982. ;; Emacs 22 deals with this through a special variable
  2983. (org-set-local 'outline-isearch-open-invisible-function
  2984. (lambda (&rest ignore) (org-show-context 'isearch))))
  2985. ;; If empty file that did not turn on org-mode automatically, make it to.
  2986. (if (and org-insert-mode-line-in-empty-file
  2987. (interactive-p)
  2988. (= (point-min) (point-max)))
  2989. (insert "# -*- mode: org -*-\n\n"))
  2990. (unless org-inhibit-startup
  2991. (when org-startup-align-all-tables
  2992. (let ((bmp (buffer-modified-p)))
  2993. (org-table-map-tables 'org-table-align)
  2994. (set-buffer-modified-p bmp)))
  2995. (org-set-startup-visibility)))
  2996. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2997. (defun org-current-time ()
  2998. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2999. (if (> (car org-time-stamp-rounding-minutes) 1)
  3000. (let ((r (car org-time-stamp-rounding-minutes))
  3001. (time (decode-time)))
  3002. (apply 'encode-time
  3003. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3004. (nthcdr 2 time))))
  3005. (current-time)))
  3006. ;;;; Font-Lock stuff, including the activators
  3007. (defvar org-mouse-map (make-sparse-keymap))
  3008. (org-defkey org-mouse-map
  3009. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3010. (org-defkey org-mouse-map
  3011. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3012. (when org-mouse-1-follows-link
  3013. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3014. (when org-tab-follows-link
  3015. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3016. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3017. (when org-return-follows-link
  3018. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3019. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3020. (require 'font-lock)
  3021. (defconst org-non-link-chars "]\t\n\r<>")
  3022. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3023. "shell" "elisp"))
  3024. (defvar org-link-types-re nil
  3025. "Matches a link that has a url-like prefix like \"http:\"")
  3026. (defvar org-link-re-with-space nil
  3027. "Matches a link with spaces, optional angular brackets around it.")
  3028. (defvar org-link-re-with-space2 nil
  3029. "Matches a link with spaces, optional angular brackets around it.")
  3030. (defvar org-link-re-with-space3 nil
  3031. "Matches a link with spaces, only for internal part in bracket links.")
  3032. (defvar org-angle-link-re nil
  3033. "Matches link with angular brackets, spaces are allowed.")
  3034. (defvar org-plain-link-re nil
  3035. "Matches plain link, without spaces.")
  3036. (defvar org-bracket-link-regexp nil
  3037. "Matches a link in double brackets.")
  3038. (defvar org-bracket-link-analytic-regexp nil
  3039. "Regular expression used to analyze links.
  3040. Here is what the match groups contain after a match:
  3041. 1: http:
  3042. 2: http
  3043. 3: path
  3044. 4: [desc]
  3045. 5: desc")
  3046. (defvar org-any-link-re nil
  3047. "Regular expression matching any link.")
  3048. (defun org-make-link-regexps ()
  3049. "Update the link regular expressions.
  3050. This should be called after the variable `org-link-types' has changed."
  3051. (setq org-link-types-re
  3052. (concat
  3053. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3054. org-link-re-with-space
  3055. (concat
  3056. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3057. "\\([^" org-non-link-chars " ]"
  3058. "[^" org-non-link-chars "]*"
  3059. "[^" org-non-link-chars " ]\\)>?")
  3060. org-link-re-with-space2
  3061. (concat
  3062. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3063. "\\([^" org-non-link-chars " ]"
  3064. "[^\t\n\r]*"
  3065. "[^" org-non-link-chars " ]\\)>?")
  3066. org-link-re-with-space3
  3067. (concat
  3068. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3069. "\\([^" org-non-link-chars " ]"
  3070. "[^\t\n\r]*\\)")
  3071. org-angle-link-re
  3072. (concat
  3073. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3074. "\\([^" org-non-link-chars " ]"
  3075. "[^" org-non-link-chars "]*"
  3076. "\\)>")
  3077. org-plain-link-re
  3078. (concat
  3079. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3080. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3081. org-bracket-link-regexp
  3082. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3083. org-bracket-link-analytic-regexp
  3084. (concat
  3085. "\\[\\["
  3086. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3087. "\\([^]]+\\)"
  3088. "\\]"
  3089. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3090. "\\]")
  3091. org-any-link-re
  3092. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3093. org-angle-link-re "\\)\\|\\("
  3094. org-plain-link-re "\\)")))
  3095. (org-make-link-regexps)
  3096. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3097. "Regular expression for fast time stamp matching.")
  3098. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3099. "Regular expression for fast time stamp matching.")
  3100. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3101. "Regular expression matching time strings for analysis.
  3102. This one does not require the space after the date, so it can be used
  3103. on a string that terminates immediately after the date.")
  3104. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3105. "Regular expression matching time strings for analysis.")
  3106. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3107. "Regular expression matching time stamps, with groups.")
  3108. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3109. "Regular expression matching time stamps (also [..]), with groups.")
  3110. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3111. "Regular expression matching a time stamp range.")
  3112. (defconst org-tr-regexp-both
  3113. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3114. "Regular expression matching a time stamp range.")
  3115. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3116. org-ts-regexp "\\)?")
  3117. "Regular expression matching a time stamp or time stamp range.")
  3118. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3119. org-ts-regexp-both "\\)?")
  3120. "Regular expression matching a time stamp or time stamp range.
  3121. The time stamps may be either active or inactive.")
  3122. (defvar org-emph-face nil)
  3123. (defun org-do-emphasis-faces (limit)
  3124. "Run through the buffer and add overlays to links."
  3125. (let (rtn)
  3126. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3127. (if (not (= (char-after (match-beginning 3))
  3128. (char-after (match-beginning 4))))
  3129. (progn
  3130. (setq rtn t)
  3131. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3132. 'face
  3133. (nth 1 (assoc (match-string 3)
  3134. org-emphasis-alist)))
  3135. (add-text-properties (match-beginning 2) (match-end 2)
  3136. '(font-lock-multiline t))
  3137. (when org-hide-emphasis-markers
  3138. (add-text-properties (match-end 4) (match-beginning 5)
  3139. '(invisible org-link))
  3140. (add-text-properties (match-beginning 3) (match-end 3)
  3141. '(invisible org-link)))))
  3142. (backward-char 1))
  3143. rtn))
  3144. (defun org-emphasize (&optional char)
  3145. "Insert or change an emphasis, i.e. a font like bold or italic.
  3146. If there is an active region, change that region to a new emphasis.
  3147. If there is no region, just insert the marker characters and position
  3148. the cursor between them.
  3149. CHAR should be either the marker character, or the first character of the
  3150. HTML tag associated with that emphasis. If CHAR is a space, the means
  3151. to remove the emphasis of the selected region.
  3152. If char is not given (for example in an interactive call) it
  3153. will be prompted for."
  3154. (interactive)
  3155. (let ((eal org-emphasis-alist) e det
  3156. (erc org-emphasis-regexp-components)
  3157. (prompt "")
  3158. (string "") beg end move tag c s)
  3159. (if (org-region-active-p)
  3160. (setq beg (region-beginning) end (region-end)
  3161. string (buffer-substring beg end))
  3162. (setq move t))
  3163. (while (setq e (pop eal))
  3164. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3165. c (aref tag 0))
  3166. (push (cons c (string-to-char (car e))) det)
  3167. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3168. (substring tag 1)))))
  3169. (setq det (nreverse det))
  3170. (unless char
  3171. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3172. (setq char (read-char-exclusive)))
  3173. (setq char (or (cdr (assoc char det)) char))
  3174. (if (equal char ?\ )
  3175. (setq s "" move nil)
  3176. (unless (assoc (char-to-string char) org-emphasis-alist)
  3177. (error "No such emphasis marker: \"%c\"" char))
  3178. (setq s (char-to-string char)))
  3179. (while (and (> (length string) 1)
  3180. (equal (substring string 0 1) (substring string -1))
  3181. (assoc (substring string 0 1) org-emphasis-alist))
  3182. (setq string (substring string 1 -1)))
  3183. (setq string (concat s string s))
  3184. (if beg (delete-region beg end))
  3185. (unless (or (bolp)
  3186. (string-match (concat "[" (nth 0 erc) "\n]")
  3187. (char-to-string (char-before (point)))))
  3188. (insert " "))
  3189. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3190. (char-to-string (char-after (point))))
  3191. (insert " ") (backward-char 1))
  3192. (insert string)
  3193. (and move (backward-char 1))))
  3194. (defconst org-nonsticky-props
  3195. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3196. (defun org-activate-plain-links (limit)
  3197. "Run through the buffer and add overlays to links."
  3198. (catch 'exit
  3199. (let (f)
  3200. (while (re-search-forward org-plain-link-re limit t)
  3201. (setq f (get-text-property (match-beginning 0) 'face))
  3202. (if (or (eq f 'org-tag)
  3203. (and (listp f) (memq 'org-tag f)))
  3204. nil
  3205. (add-text-properties (match-beginning 0) (match-end 0)
  3206. (list 'mouse-face 'highlight
  3207. 'rear-nonsticky org-nonsticky-props
  3208. 'keymap org-mouse-map
  3209. ))
  3210. (throw 'exit t))))))
  3211. (defun org-activate-code (limit)
  3212. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3213. (progn
  3214. (remove-text-properties (match-beginning 0) (match-end 0)
  3215. '(display t invisible t intangible t))
  3216. t)))
  3217. (defun org-activate-angle-links (limit)
  3218. "Run through the buffer and add overlays to links."
  3219. (if (re-search-forward org-angle-link-re limit t)
  3220. (progn
  3221. (add-text-properties (match-beginning 0) (match-end 0)
  3222. (list 'mouse-face 'highlight
  3223. 'rear-nonsticky org-nonsticky-props
  3224. 'keymap org-mouse-map
  3225. ))
  3226. t)))
  3227. (defun org-activate-bracket-links (limit)
  3228. "Run through the buffer and add overlays to bracketed links."
  3229. (if (re-search-forward org-bracket-link-regexp limit t)
  3230. (let* ((help (concat "LINK: "
  3231. (org-match-string-no-properties 1)))
  3232. ;; FIXME: above we should remove the escapes.
  3233. ;; but that requires another match, protecting match data,
  3234. ;; a lot of overhead for font-lock.
  3235. (ip (org-maybe-intangible
  3236. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3237. 'keymap org-mouse-map 'mouse-face 'highlight
  3238. 'font-lock-multiline t 'help-echo help)))
  3239. (vp (list 'rear-nonsticky org-nonsticky-props
  3240. 'keymap org-mouse-map 'mouse-face 'highlight
  3241. ' font-lock-multiline t 'help-echo help)))
  3242. ;; We need to remove the invisible property here. Table narrowing
  3243. ;; may have made some of this invisible.
  3244. (remove-text-properties (match-beginning 0) (match-end 0)
  3245. '(invisible nil))
  3246. (if (match-end 3)
  3247. (progn
  3248. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3249. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3250. (add-text-properties (match-end 3) (match-end 0) ip))
  3251. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3252. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3253. (add-text-properties (match-end 1) (match-end 0) ip))
  3254. t)))
  3255. (defun org-activate-dates (limit)
  3256. "Run through the buffer and add overlays to dates."
  3257. (if (re-search-forward org-tsr-regexp-both limit t)
  3258. (progn
  3259. (add-text-properties (match-beginning 0) (match-end 0)
  3260. (list 'mouse-face 'highlight
  3261. 'rear-nonsticky org-nonsticky-props
  3262. 'keymap org-mouse-map))
  3263. (when org-display-custom-times
  3264. (if (match-end 3)
  3265. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3266. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3267. t)))
  3268. (defvar org-target-link-regexp nil
  3269. "Regular expression matching radio targets in plain text.")
  3270. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3271. "Regular expression matching a link target.")
  3272. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3273. "Regular expression matching a radio target.")
  3274. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3275. "Regular expression matching any target.")
  3276. (defun org-activate-target-links (limit)
  3277. "Run through the buffer and add overlays to target matches."
  3278. (when org-target-link-regexp
  3279. (let ((case-fold-search t))
  3280. (if (re-search-forward org-target-link-regexp limit t)
  3281. (progn
  3282. (add-text-properties (match-beginning 0) (match-end 0)
  3283. (list 'mouse-face 'highlight
  3284. 'rear-nonsticky org-nonsticky-props
  3285. 'keymap org-mouse-map
  3286. 'help-echo "Radio target link"
  3287. 'org-linked-text t))
  3288. t)))))
  3289. (defun org-update-radio-target-regexp ()
  3290. "Find all radio targets in this file and update the regular expression."
  3291. (interactive)
  3292. (when (memq 'radio org-activate-links)
  3293. (setq org-target-link-regexp
  3294. (org-make-target-link-regexp (org-all-targets 'radio)))
  3295. (org-restart-font-lock)))
  3296. (defun org-hide-wide-columns (limit)
  3297. (let (s e)
  3298. (setq s (text-property-any (point) (or limit (point-max))
  3299. 'org-cwidth t))
  3300. (when s
  3301. (setq e (next-single-property-change s 'org-cwidth))
  3302. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3303. (goto-char e)
  3304. t)))
  3305. (defvar org-latex-and-specials-regexp nil
  3306. "Regular expression for highlighting export special stuff.")
  3307. (defvar org-match-substring-regexp)
  3308. (defvar org-match-substring-with-braces-regexp)
  3309. (defvar org-export-html-special-string-regexps)
  3310. (defun org-compute-latex-and-specials-regexp ()
  3311. "Compute regular expression for stuff treated specially by exporters."
  3312. (if (not org-highlight-latex-fragments-and-specials)
  3313. (org-set-local 'org-latex-and-specials-regexp nil)
  3314. (require 'org-exp)
  3315. (let*
  3316. ((matchers (plist-get org-format-latex-options :matchers))
  3317. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3318. org-latex-regexps)))
  3319. (options (org-combine-plists (org-default-export-plist)
  3320. (org-infile-export-plist)))
  3321. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3322. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3323. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3324. (org-export-html-expand (plist-get options :expand-quoted-html))
  3325. (org-export-with-special-strings (plist-get options :special-strings))
  3326. (re-sub
  3327. (cond
  3328. ((equal org-export-with-sub-superscripts '{})
  3329. (list org-match-substring-with-braces-regexp))
  3330. (org-export-with-sub-superscripts
  3331. (list org-match-substring-regexp))
  3332. (t nil)))
  3333. (re-latex
  3334. (if org-export-with-LaTeX-fragments
  3335. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3336. (re-macros
  3337. (if org-export-with-TeX-macros
  3338. (list (concat "\\\\"
  3339. (regexp-opt
  3340. (append (mapcar 'car org-html-entities)
  3341. (if (boundp 'org-latex-entities)
  3342. org-latex-entities nil))
  3343. 'words))) ; FIXME
  3344. ))
  3345. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3346. (re-special (if org-export-with-special-strings
  3347. (mapcar (lambda (x) (car x))
  3348. org-export-html-special-string-regexps)))
  3349. (re-rest
  3350. (delq nil
  3351. (list
  3352. (if org-export-html-expand "@<[^>\n]+>")
  3353. ))))
  3354. (org-set-local
  3355. 'org-latex-and-specials-regexp
  3356. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3357. re-rest) "\\|")))))
  3358. (defun org-do-latex-and-special-faces (limit)
  3359. "Run through the buffer and add overlays to links."
  3360. (when org-latex-and-specials-regexp
  3361. (let (rtn d)
  3362. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3363. limit t))
  3364. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3365. 'face))
  3366. '(org-code org-verbatim underline)))
  3367. (progn
  3368. (setq rtn t
  3369. d (cond ((member (char-after (1+ (match-beginning 0)))
  3370. '(?_ ?^)) 1)
  3371. (t 0)))
  3372. (font-lock-prepend-text-property
  3373. (+ d (match-beginning 0)) (match-end 0)
  3374. 'face 'org-latex-and-export-specials)
  3375. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3376. '(font-lock-multiline t)))))
  3377. rtn)))
  3378. (defun org-restart-font-lock ()
  3379. "Restart font-lock-mode, to force refontification."
  3380. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3381. (font-lock-mode -1)
  3382. (font-lock-mode 1)))
  3383. (defun org-all-targets (&optional radio)
  3384. "Return a list of all targets in this file.
  3385. With optional argument RADIO, only find radio targets."
  3386. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3387. rtn)
  3388. (save-excursion
  3389. (goto-char (point-min))
  3390. (while (re-search-forward re nil t)
  3391. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3392. rtn)))
  3393. (defun org-make-target-link-regexp (targets)
  3394. "Make regular expression matching all strings in TARGETS.
  3395. The regular expression finds the targets also if there is a line break
  3396. between words."
  3397. (and targets
  3398. (concat
  3399. "\\<\\("
  3400. (mapconcat
  3401. (lambda (x)
  3402. (while (string-match " +" x)
  3403. (setq x (replace-match "\\s-+" t t x)))
  3404. x)
  3405. targets
  3406. "\\|")
  3407. "\\)\\>")))
  3408. (defun org-activate-tags (limit)
  3409. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3410. (progn
  3411. (add-text-properties (match-beginning 1) (match-end 1)
  3412. (list 'mouse-face 'highlight
  3413. 'rear-nonsticky org-nonsticky-props
  3414. 'keymap org-mouse-map))
  3415. t)))
  3416. (defun org-outline-level ()
  3417. (save-excursion
  3418. (looking-at outline-regexp)
  3419. (if (match-beginning 1)
  3420. (+ (org-get-string-indentation (match-string 1)) 1000)
  3421. (1- (- (match-end 0) (match-beginning 0))))))
  3422. (defvar org-font-lock-keywords nil)
  3423. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3424. "Regular expression matching a property line.")
  3425. (defvar org-font-lock-hook nil
  3426. "Functions to be called for special font lock stuff.")
  3427. (defun org-font-lock-hook (limit)
  3428. (run-hook-with-args 'org-font-lock-hook limit))
  3429. (defun org-set-font-lock-defaults ()
  3430. (let* ((em org-fontify-emphasized-text)
  3431. (lk org-activate-links)
  3432. (org-font-lock-extra-keywords
  3433. (list
  3434. ;; Call the hook
  3435. '(org-font-lock-hook)
  3436. ;; Headlines
  3437. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3438. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3439. ;; Table lines
  3440. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3441. (1 'org-table t))
  3442. ;; Table internals
  3443. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3444. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3445. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3446. ;; Drawers
  3447. (list org-drawer-regexp '(0 'org-special-keyword t))
  3448. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3449. ;; Properties
  3450. (list org-property-re
  3451. '(1 'org-special-keyword t)
  3452. '(3 'org-property-value t))
  3453. (if org-format-transports-properties-p
  3454. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3455. ;; Links
  3456. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3457. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3458. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3459. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3460. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3461. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3462. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3463. '(org-hide-wide-columns (0 nil append))
  3464. ;; TODO lines
  3465. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3466. '(1 (org-get-todo-face 1) t))
  3467. ;; DONE
  3468. (if org-fontify-done-headline
  3469. (list (concat "^[*]+ +\\<\\("
  3470. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3471. "\\)\\(.*\\)")
  3472. '(2 'org-headline-done t))
  3473. nil)
  3474. ;; Priorities
  3475. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3476. ;; Special keywords
  3477. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3478. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3479. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3480. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3481. ;; Emphasis
  3482. (if em
  3483. (if (featurep 'xemacs)
  3484. '(org-do-emphasis-faces (0 nil append))
  3485. '(org-do-emphasis-faces)))
  3486. ;; Checkboxes
  3487. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3488. 2 'bold prepend)
  3489. (if org-provide-checkbox-statistics
  3490. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3491. (0 (org-get-checkbox-statistics-face) t)))
  3492. ;; Description list items
  3493. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3494. 2 'bold prepend)
  3495. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3496. '(1 'org-archived prepend))
  3497. ;; Specials
  3498. '(org-do-latex-and-special-faces)
  3499. ;; Code
  3500. '(org-activate-code (1 'org-code t))
  3501. ;; COMMENT
  3502. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3503. "\\|" org-quote-string "\\)\\>")
  3504. '(1 'org-special-keyword t))
  3505. '("^#.*" (0 'font-lock-comment-face t))
  3506. )))
  3507. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3508. ;; Now set the full font-lock-keywords
  3509. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3510. (org-set-local 'font-lock-defaults
  3511. '(org-font-lock-keywords t nil nil backward-paragraph))
  3512. (kill-local-variable 'font-lock-keywords) nil))
  3513. (defvar org-m nil)
  3514. (defvar org-l nil)
  3515. (defvar org-f nil)
  3516. (defun org-get-level-face (n)
  3517. "Get the right face for match N in font-lock matching of healdines."
  3518. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3519. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3520. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3521. (cond
  3522. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3523. ((eq n 2) org-f)
  3524. (t (if org-level-color-stars-only nil org-f))))
  3525. (defun org-get-todo-face (kwd)
  3526. "Get the right face for a TODO keyword KWD.
  3527. If KWD is a number, get the corresponding match group."
  3528. (if (numberp kwd) (setq kwd (match-string kwd)))
  3529. (or (cdr (assoc kwd org-todo-keyword-faces))
  3530. (and (member kwd org-done-keywords) 'org-done)
  3531. 'org-todo))
  3532. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3533. "Remove fontification and activation overlays from links."
  3534. (font-lock-default-unfontify-region beg end)
  3535. (let* ((buffer-undo-list t)
  3536. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3537. (inhibit-modification-hooks t)
  3538. deactivate-mark buffer-file-name buffer-file-truename)
  3539. (remove-text-properties beg end
  3540. '(mouse-face t keymap t org-linked-text t
  3541. invisible t intangible t))))
  3542. ;;;; Visibility cycling, including org-goto and indirect buffer
  3543. ;;; Cycling
  3544. (defvar org-cycle-global-status nil)
  3545. (make-variable-buffer-local 'org-cycle-global-status)
  3546. (defvar org-cycle-subtree-status nil)
  3547. (make-variable-buffer-local 'org-cycle-subtree-status)
  3548. ;;;###autoload
  3549. (defun org-cycle (&optional arg)
  3550. "Visibility cycling for Org-mode.
  3551. - When this function is called with a prefix argument, rotate the entire
  3552. buffer through 3 states (global cycling)
  3553. 1. OVERVIEW: Show only top-level headlines.
  3554. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3555. 3. SHOW ALL: Show everything.
  3556. When called with two C-u C-u prefixes, switch to the startup visibility,
  3557. determined by the variable `org-startup-folded', and by any VISIBILITY
  3558. properties in the buffer.
  3559. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3560. including drawers.
  3561. - When point is at the beginning of a headline, rotate the subtree started
  3562. by this line through 3 different states (local cycling)
  3563. 1. FOLDED: Only the main headline is shown.
  3564. 2. CHILDREN: The main headline and the direct children are shown.
  3565. From this state, you can move to one of the children
  3566. and zoom in further.
  3567. 3. SUBTREE: Show the entire subtree, including body text.
  3568. - When there is a numeric prefix, go up to a heading with level ARG, do
  3569. a `show-subtree' and return to the previous cursor position. If ARG
  3570. is negative, go up that many levels.
  3571. - When point is not at the beginning of a headline, execute the global
  3572. binding for TAB, which is re-indenting the line. See the option
  3573. `org-cycle-emulate-tab' for details.
  3574. - Special case: if point is at the beginning of the buffer and there is
  3575. no headline in line 1, this function will act as if called with prefix arg.
  3576. But only if also the variable `org-cycle-global-at-bob' is t."
  3577. (interactive "P")
  3578. (org-load-modules-maybe)
  3579. (let* ((outline-regexp
  3580. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3581. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3582. outline-regexp))
  3583. (bob-special (and org-cycle-global-at-bob (bobp)
  3584. (not (looking-at outline-regexp))))
  3585. (org-cycle-hook
  3586. (if bob-special
  3587. (delq 'org-optimize-window-after-visibility-change
  3588. (copy-sequence org-cycle-hook))
  3589. org-cycle-hook))
  3590. (pos (point)))
  3591. (if (or bob-special (equal arg '(4)))
  3592. ;; special case: use global cycling
  3593. (setq arg t))
  3594. (cond
  3595. ((equal arg '(16))
  3596. (org-set-startup-visibility)
  3597. (message "Startup visibility, plus VISIBILITY properties"))
  3598. ((equal arg '(64))
  3599. (show-all)
  3600. (message "Entire buffer visible, including drawers"))
  3601. ((org-at-table-p 'any)
  3602. ;; Enter the table or move to the next field in the table
  3603. (or (org-table-recognize-table.el)
  3604. (progn
  3605. (if arg (org-table-edit-field t)
  3606. (org-table-justify-field-maybe)
  3607. (call-interactively 'org-table-next-field)))))
  3608. ((eq arg t) ;; Global cycling
  3609. (cond
  3610. ((and (eq last-command this-command)
  3611. (eq org-cycle-global-status 'overview))
  3612. ;; We just created the overview - now do table of contents
  3613. ;; This can be slow in very large buffers, so indicate action
  3614. (message "CONTENTS...")
  3615. (org-content)
  3616. (message "CONTENTS...done")
  3617. (setq org-cycle-global-status 'contents)
  3618. (run-hook-with-args 'org-cycle-hook 'contents))
  3619. ((and (eq last-command this-command)
  3620. (eq org-cycle-global-status 'contents))
  3621. ;; We just showed the table of contents - now show everything
  3622. (show-all)
  3623. (message "SHOW ALL")
  3624. (setq org-cycle-global-status 'all)
  3625. (run-hook-with-args 'org-cycle-hook 'all))
  3626. (t
  3627. ;; Default action: go to overview
  3628. (org-overview)
  3629. (message "OVERVIEW")
  3630. (setq org-cycle-global-status 'overview)
  3631. (run-hook-with-args 'org-cycle-hook 'overview))))
  3632. ((and org-drawers org-drawer-regexp
  3633. (save-excursion
  3634. (beginning-of-line 1)
  3635. (looking-at org-drawer-regexp)))
  3636. ;; Toggle block visibility
  3637. (org-flag-drawer
  3638. (not (get-char-property (match-end 0) 'invisible))))
  3639. ((integerp arg)
  3640. ;; Show-subtree, ARG levels up from here.
  3641. (save-excursion
  3642. (org-back-to-heading)
  3643. (outline-up-heading (if (< arg 0) (- arg)
  3644. (- (funcall outline-level) arg)))
  3645. (org-show-subtree)))
  3646. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3647. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3648. ;; At a heading: rotate between three different views
  3649. (org-back-to-heading)
  3650. (let ((goal-column 0) eoh eol eos)
  3651. ;; First, some boundaries
  3652. (save-excursion
  3653. (org-back-to-heading)
  3654. (save-excursion
  3655. (beginning-of-line 2)
  3656. (while (and (not (eobp)) ;; this is like `next-line'
  3657. (get-char-property (1- (point)) 'invisible))
  3658. (beginning-of-line 2)) (setq eol (point)))
  3659. (outline-end-of-heading) (setq eoh (point))
  3660. (org-end-of-subtree t)
  3661. (unless (eobp)
  3662. (skip-chars-forward " \t\n")
  3663. (beginning-of-line 1) ; in case this is an item
  3664. )
  3665. (setq eos (1- (point))))
  3666. ;; Find out what to do next and set `this-command'
  3667. (cond
  3668. ((= eos eoh)
  3669. ;; Nothing is hidden behind this heading
  3670. (message "EMPTY ENTRY")
  3671. (setq org-cycle-subtree-status nil)
  3672. (save-excursion
  3673. (goto-char eos)
  3674. (outline-next-heading)
  3675. (if (org-invisible-p) (org-flag-heading nil))))
  3676. ((or (>= eol eos)
  3677. (not (string-match "\\S-" (buffer-substring eol eos))))
  3678. ;; Entire subtree is hidden in one line: open it
  3679. (org-show-entry)
  3680. (show-children)
  3681. (message "CHILDREN")
  3682. (save-excursion
  3683. (goto-char eos)
  3684. (outline-next-heading)
  3685. (if (org-invisible-p) (org-flag-heading nil)))
  3686. (setq org-cycle-subtree-status 'children)
  3687. (run-hook-with-args 'org-cycle-hook 'children))
  3688. ((and (eq last-command this-command)
  3689. (eq org-cycle-subtree-status 'children))
  3690. ;; We just showed the children, now show everything.
  3691. (org-show-subtree)
  3692. (message "SUBTREE")
  3693. (setq org-cycle-subtree-status 'subtree)
  3694. (run-hook-with-args 'org-cycle-hook 'subtree))
  3695. (t
  3696. ;; Default action: hide the subtree.
  3697. (hide-subtree)
  3698. (message "FOLDED")
  3699. (setq org-cycle-subtree-status 'folded)
  3700. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3701. ;; TAB emulation and template completion
  3702. (buffer-read-only (org-back-to-heading))
  3703. ((org-try-structure-completion))
  3704. ((org-try-cdlatex-tab))
  3705. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3706. (or (not (bolp))
  3707. (not (looking-at outline-regexp))))
  3708. (call-interactively (global-key-binding "\t")))
  3709. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3710. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3711. (or (and (eq org-cycle-emulate-tab 'white)
  3712. (= (match-end 0) (point-at-eol)))
  3713. (and (eq org-cycle-emulate-tab 'whitestart)
  3714. (>= (match-end 0) pos))))
  3715. t
  3716. (eq org-cycle-emulate-tab t))
  3717. (call-interactively (global-key-binding "\t")))
  3718. (t (save-excursion
  3719. (org-back-to-heading)
  3720. (org-cycle))))))
  3721. ;;;###autoload
  3722. (defun org-global-cycle (&optional arg)
  3723. "Cycle the global visibility. For details see `org-cycle'.
  3724. With C-u prefix arg, switch to startup visibility.
  3725. With a numeric prefix, show all headlines up to that level."
  3726. (interactive "P")
  3727. (let ((org-cycle-include-plain-lists
  3728. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3729. (cond
  3730. ((integerp arg)
  3731. (show-all)
  3732. (hide-sublevels arg)
  3733. (setq org-cycle-global-status 'contents))
  3734. ((equal arg '(4))
  3735. (org-set-startup-visibility)
  3736. (message "Startup visibility, plus VISIBILITY properties."))
  3737. (t
  3738. (org-cycle '(4))))))
  3739. (defun org-set-startup-visibility ()
  3740. "Set the visibility required by startup options and properties."
  3741. (cond
  3742. ((eq org-startup-folded t)
  3743. (org-cycle '(4)))
  3744. ((eq org-startup-folded 'content)
  3745. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3746. (org-cycle '(4)) (org-cycle '(4)))))
  3747. (org-set-visibility-according-to-property 'no-cleanup)
  3748. (org-cycle-hide-archived-subtrees 'all)
  3749. (org-cycle-hide-drawers 'all)
  3750. (org-cycle-show-empty-lines 'all))
  3751. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3752. "Switch subtree visibilities according to :VISIBILITY: property."
  3753. (interactive)
  3754. (let (state)
  3755. (save-excursion
  3756. (goto-char (point-min))
  3757. (while (re-search-forward
  3758. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3759. nil t)
  3760. (setq state (match-string 1))
  3761. (save-excursion
  3762. (org-back-to-heading t)
  3763. (hide-subtree)
  3764. (org-reveal)
  3765. (cond
  3766. ((equal state '("fold" "folded"))
  3767. (hide-subtree))
  3768. ((equal state "children")
  3769. (org-show-hidden-entry)
  3770. (show-children))
  3771. ((equal state "content")
  3772. (save-excursion
  3773. (save-restriction
  3774. (org-narrow-to-subtree)
  3775. (org-content))))
  3776. ((member state '("all" "showall"))
  3777. (show-subtree)))))
  3778. (unless no-cleanup
  3779. (org-cycle-hide-archived-subtrees 'all)
  3780. (org-cycle-hide-drawers 'all)
  3781. (org-cycle-show-empty-lines 'all)))))
  3782. (defun org-overview ()
  3783. "Switch to overview mode, shoing only top-level headlines.
  3784. Really, this shows all headlines with level equal or greater than the level
  3785. of the first headline in the buffer. This is important, because if the
  3786. first headline is not level one, then (hide-sublevels 1) gives confusing
  3787. results."
  3788. (interactive)
  3789. (let ((level (save-excursion
  3790. (goto-char (point-min))
  3791. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3792. (progn
  3793. (goto-char (match-beginning 0))
  3794. (funcall outline-level))))))
  3795. (and level (hide-sublevels level))))
  3796. (defun org-content (&optional arg)
  3797. "Show all headlines in the buffer, like a table of contents.
  3798. With numerical argument N, show content up to level N."
  3799. (interactive "P")
  3800. (save-excursion
  3801. ;; Visit all headings and show their offspring
  3802. (and (integerp arg) (org-overview))
  3803. (goto-char (point-max))
  3804. (catch 'exit
  3805. (while (and (progn (condition-case nil
  3806. (outline-previous-visible-heading 1)
  3807. (error (goto-char (point-min))))
  3808. t)
  3809. (looking-at outline-regexp))
  3810. (if (integerp arg)
  3811. (show-children (1- arg))
  3812. (show-branches))
  3813. (if (bobp) (throw 'exit nil))))))
  3814. (defun org-optimize-window-after-visibility-change (state)
  3815. "Adjust the window after a change in outline visibility.
  3816. This function is the default value of the hook `org-cycle-hook'."
  3817. (when (get-buffer-window (current-buffer))
  3818. (cond
  3819. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3820. ; ((eq state 'overview) (org-beginning-of-line))
  3821. ((eq state 'content) nil)
  3822. ((eq state 'all) nil)
  3823. ((eq state 'folded) nil)
  3824. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3825. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3826. (defun org-compact-display-after-subtree-move ()
  3827. (let (beg end)
  3828. (save-excursion
  3829. (if (org-up-heading-safe)
  3830. (progn
  3831. (hide-subtree)
  3832. (show-entry)
  3833. (show-children)
  3834. (org-cycle-show-empty-lines 'children)
  3835. (org-cycle-hide-drawers 'children))
  3836. (org-overview)))))
  3837. (defun org-cycle-show-empty-lines (state)
  3838. "Show empty lines above all visible headlines.
  3839. The region to be covered depends on STATE when called through
  3840. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3841. entire buffer covered. Note that an empty line is only shown if there
  3842. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3843. (when (> org-cycle-separator-lines 0)
  3844. (save-excursion
  3845. (let* ((n org-cycle-separator-lines)
  3846. (re (cond
  3847. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3848. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3849. (t (let ((ns (number-to-string (- n 2))))
  3850. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3851. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3852. beg end)
  3853. (cond
  3854. ((memq state '(overview contents t))
  3855. (setq beg (point-min) end (point-max)))
  3856. ((memq state '(children folded))
  3857. (setq beg (point) end (progn (org-end-of-subtree t t)
  3858. (beginning-of-line 2)
  3859. (point)))))
  3860. (when beg
  3861. (goto-char beg)
  3862. (while (re-search-forward re end t)
  3863. (if (not (get-char-property (match-end 1) 'invisible))
  3864. (outline-flag-region
  3865. (match-beginning 1) (match-end 1) nil)))))))
  3866. ;; Never hide empty lines at the end of the file.
  3867. (save-excursion
  3868. (goto-char (point-max))
  3869. (outline-previous-heading)
  3870. (outline-end-of-heading)
  3871. (if (and (looking-at "[ \t\n]+")
  3872. (= (match-end 0) (point-max)))
  3873. (outline-flag-region (point) (match-end 0) nil))))
  3874. (defun org-show-empty-lines-in-parent ()
  3875. "Move to the parent and re-show empty lines before visible headlines."
  3876. (save-excursion
  3877. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3878. (org-cycle-show-empty-lines context))))
  3879. (defun org-cycle-hide-drawers (state)
  3880. "Re-hide all drawers after a visibility state change."
  3881. (when (and (org-mode-p)
  3882. (not (memq state '(overview folded))))
  3883. (save-excursion
  3884. (let* ((globalp (memq state '(contents all)))
  3885. (beg (if globalp (point-min) (point)))
  3886. (end (if globalp (point-max) (org-end-of-subtree t))))
  3887. (goto-char beg)
  3888. (while (re-search-forward org-drawer-regexp end t)
  3889. (org-flag-drawer t))))))
  3890. (defun org-flag-drawer (flag)
  3891. (save-excursion
  3892. (beginning-of-line 1)
  3893. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3894. (let ((b (match-end 0))
  3895. (outline-regexp org-outline-regexp))
  3896. (if (re-search-forward
  3897. "^[ \t]*:END:"
  3898. (save-excursion (outline-next-heading) (point)) t)
  3899. (outline-flag-region b (point-at-eol) flag)
  3900. (error ":END: line missing"))))))
  3901. (defun org-subtree-end-visible-p ()
  3902. "Is the end of the current subtree visible?"
  3903. (pos-visible-in-window-p
  3904. (save-excursion (org-end-of-subtree t) (point))))
  3905. (defun org-first-headline-recenter (&optional N)
  3906. "Move cursor to the first headline and recenter the headline.
  3907. Optional argument N means, put the headline into the Nth line of the window."
  3908. (goto-char (point-min))
  3909. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3910. (beginning-of-line)
  3911. (recenter (prefix-numeric-value N))))
  3912. ;;; Org-goto
  3913. (defvar org-goto-window-configuration nil)
  3914. (defvar org-goto-marker nil)
  3915. (defvar org-goto-map
  3916. (let ((map (make-sparse-keymap)))
  3917. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3918. (while (setq cmd (pop cmds))
  3919. (substitute-key-definition cmd cmd map global-map)))
  3920. (suppress-keymap map)
  3921. (org-defkey map "\C-m" 'org-goto-ret)
  3922. (org-defkey map [(return)] 'org-goto-ret)
  3923. (org-defkey map [(left)] 'org-goto-left)
  3924. (org-defkey map [(right)] 'org-goto-right)
  3925. (org-defkey map [(control ?g)] 'org-goto-quit)
  3926. (org-defkey map "\C-i" 'org-cycle)
  3927. (org-defkey map [(tab)] 'org-cycle)
  3928. (org-defkey map [(down)] 'outline-next-visible-heading)
  3929. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3930. (if org-goto-auto-isearch
  3931. (if (fboundp 'define-key-after)
  3932. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3933. nil)
  3934. (org-defkey map "q" 'org-goto-quit)
  3935. (org-defkey map "n" 'outline-next-visible-heading)
  3936. (org-defkey map "p" 'outline-previous-visible-heading)
  3937. (org-defkey map "f" 'outline-forward-same-level)
  3938. (org-defkey map "b" 'outline-backward-same-level)
  3939. (org-defkey map "u" 'outline-up-heading))
  3940. (org-defkey map "/" 'org-occur)
  3941. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3942. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3943. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3944. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3945. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3946. map))
  3947. (defconst org-goto-help
  3948. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3949. RET=jump to location [Q]uit and return to previous location
  3950. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3951. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3952. ;; FIXME: Docstring doe not mention both interfaces
  3953. (defun org-goto (&optional alternative-interface)
  3954. "Look up a different location in the current file, keeping current visibility.
  3955. When you want look-up or go to a different location in a document, the
  3956. fastest way is often to fold the entire buffer and then dive into the tree.
  3957. This method has the disadvantage, that the previous location will be folded,
  3958. which may not be what you want.
  3959. This command works around this by showing a copy of the current buffer
  3960. in an indirect buffer, in overview mode. You can dive into the tree in
  3961. that copy, use org-occur and incremental search to find a location.
  3962. When pressing RET or `Q', the command returns to the original buffer in
  3963. which the visibility is still unchanged. After RET is will also jump to
  3964. the location selected in the indirect buffer and expose the
  3965. the headline hierarchy above."
  3966. (interactive "P")
  3967. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3968. (org-refile-use-outline-path t)
  3969. (interface
  3970. (if (not alternative-interface)
  3971. org-goto-interface
  3972. (if (eq org-goto-interface 'outline)
  3973. 'outline-path-completion
  3974. 'outline)))
  3975. (org-goto-start-pos (point))
  3976. (selected-point
  3977. (if (eq interface 'outline)
  3978. (car (org-get-location (current-buffer) org-goto-help))
  3979. (nth 3 (org-refile-get-location "Goto: ")))))
  3980. (if selected-point
  3981. (progn
  3982. (org-mark-ring-push org-goto-start-pos)
  3983. (goto-char selected-point)
  3984. (if (or (org-invisible-p) (org-invisible-p2))
  3985. (org-show-context 'org-goto)))
  3986. (message "Quit"))))
  3987. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3988. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3989. (defvar org-goto-local-auto-isearch-map) ; defined below
  3990. (defun org-get-location (buf help)
  3991. "Let the user select a location in the Org-mode buffer BUF.
  3992. This function uses a recursive edit. It returns the selected position
  3993. or nil."
  3994. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3995. (isearch-hide-immediately nil)
  3996. (isearch-search-fun-function
  3997. (lambda () 'org-goto-local-search-headings))
  3998. (org-goto-selected-point org-goto-exit-command))
  3999. (save-excursion
  4000. (save-window-excursion
  4001. (delete-other-windows)
  4002. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4003. (switch-to-buffer
  4004. (condition-case nil
  4005. (make-indirect-buffer (current-buffer) "*org-goto*")
  4006. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4007. (with-output-to-temp-buffer "*Help*"
  4008. (princ help))
  4009. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4010. (setq buffer-read-only nil)
  4011. (let ((org-startup-truncated t)
  4012. (org-startup-folded nil)
  4013. (org-startup-align-all-tables nil))
  4014. (org-mode)
  4015. (org-overview))
  4016. (setq buffer-read-only t)
  4017. (if (and (boundp 'org-goto-start-pos)
  4018. (integer-or-marker-p org-goto-start-pos))
  4019. (let ((org-show-hierarchy-above t)
  4020. (org-show-siblings t)
  4021. (org-show-following-heading t))
  4022. (goto-char org-goto-start-pos)
  4023. (and (org-invisible-p) (org-show-context)))
  4024. (goto-char (point-min)))
  4025. (org-beginning-of-line)
  4026. (message "Select location and press RET")
  4027. (use-local-map org-goto-map)
  4028. (recursive-edit)
  4029. ))
  4030. (kill-buffer "*org-goto*")
  4031. (cons org-goto-selected-point org-goto-exit-command)))
  4032. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4033. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4034. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4035. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4036. (defun org-goto-local-search-headings (string bound noerror)
  4037. "Search and make sure that any matches are in headlines."
  4038. (catch 'return
  4039. (while (if isearch-forward
  4040. (search-forward string bound noerror)
  4041. (search-backward string bound noerror))
  4042. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4043. (and (member :headline context)
  4044. (not (member :tags context))))
  4045. (throw 'return (point))))))
  4046. (defun org-goto-local-auto-isearch ()
  4047. "Start isearch."
  4048. (interactive)
  4049. (goto-char (point-min))
  4050. (let ((keys (this-command-keys)))
  4051. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4052. (isearch-mode t)
  4053. (isearch-process-search-char (string-to-char keys)))))
  4054. (defun org-goto-ret (&optional arg)
  4055. "Finish `org-goto' by going to the new location."
  4056. (interactive "P")
  4057. (setq org-goto-selected-point (point)
  4058. org-goto-exit-command 'return)
  4059. (throw 'exit nil))
  4060. (defun org-goto-left ()
  4061. "Finish `org-goto' by going to the new location."
  4062. (interactive)
  4063. (if (org-on-heading-p)
  4064. (progn
  4065. (beginning-of-line 1)
  4066. (setq org-goto-selected-point (point)
  4067. org-goto-exit-command 'left)
  4068. (throw 'exit nil))
  4069. (error "Not on a heading")))
  4070. (defun org-goto-right ()
  4071. "Finish `org-goto' by going to the new location."
  4072. (interactive)
  4073. (if (org-on-heading-p)
  4074. (progn
  4075. (setq org-goto-selected-point (point)
  4076. org-goto-exit-command 'right)
  4077. (throw 'exit nil))
  4078. (error "Not on a heading")))
  4079. (defun org-goto-quit ()
  4080. "Finish `org-goto' without cursor motion."
  4081. (interactive)
  4082. (setq org-goto-selected-point nil)
  4083. (setq org-goto-exit-command 'quit)
  4084. (throw 'exit nil))
  4085. ;;; Indirect buffer display of subtrees
  4086. (defvar org-indirect-dedicated-frame nil
  4087. "This is the frame being used for indirect tree display.")
  4088. (defvar org-last-indirect-buffer nil)
  4089. (defun org-tree-to-indirect-buffer (&optional arg)
  4090. "Create indirect buffer and narrow it to current subtree.
  4091. With numerical prefix ARG, go up to this level and then take that tree.
  4092. If ARG is negative, go up that many levels.
  4093. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4094. indirect buffer previously made with this command, to avoid proliferation of
  4095. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4096. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4097. is kept so that you can work with several indirect buffers at the same time.
  4098. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4099. requests that a new frame be made for the new buffer, so that the dedicated
  4100. frame is not changed."
  4101. (interactive "P")
  4102. (let ((cbuf (current-buffer))
  4103. (cwin (selected-window))
  4104. (pos (point))
  4105. beg end level heading ibuf)
  4106. (save-excursion
  4107. (org-back-to-heading t)
  4108. (when (numberp arg)
  4109. (setq level (org-outline-level))
  4110. (if (< arg 0) (setq arg (+ level arg)))
  4111. (while (> (setq level (org-outline-level)) arg)
  4112. (outline-up-heading 1 t)))
  4113. (setq beg (point)
  4114. heading (org-get-heading))
  4115. (org-end-of-subtree t) (setq end (point)))
  4116. (if (and (buffer-live-p org-last-indirect-buffer)
  4117. (not (eq org-indirect-buffer-display 'new-frame))
  4118. (not arg))
  4119. (kill-buffer org-last-indirect-buffer))
  4120. (setq ibuf (org-get-indirect-buffer cbuf)
  4121. org-last-indirect-buffer ibuf)
  4122. (cond
  4123. ((or (eq org-indirect-buffer-display 'new-frame)
  4124. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4125. (select-frame (make-frame))
  4126. (delete-other-windows)
  4127. (switch-to-buffer ibuf)
  4128. (org-set-frame-title heading))
  4129. ((eq org-indirect-buffer-display 'dedicated-frame)
  4130. (raise-frame
  4131. (select-frame (or (and org-indirect-dedicated-frame
  4132. (frame-live-p org-indirect-dedicated-frame)
  4133. org-indirect-dedicated-frame)
  4134. (setq org-indirect-dedicated-frame (make-frame)))))
  4135. (delete-other-windows)
  4136. (switch-to-buffer ibuf)
  4137. (org-set-frame-title (concat "Indirect: " heading)))
  4138. ((eq org-indirect-buffer-display 'current-window)
  4139. (switch-to-buffer ibuf))
  4140. ((eq org-indirect-buffer-display 'other-window)
  4141. (pop-to-buffer ibuf))
  4142. (t (error "Invalid value.")))
  4143. (if (featurep 'xemacs)
  4144. (save-excursion (org-mode) (turn-on-font-lock)))
  4145. (narrow-to-region beg end)
  4146. (show-all)
  4147. (goto-char pos)
  4148. (and (window-live-p cwin) (select-window cwin))))
  4149. (defun org-get-indirect-buffer (&optional buffer)
  4150. (setq buffer (or buffer (current-buffer)))
  4151. (let ((n 1) (base (buffer-name buffer)) bname)
  4152. (while (buffer-live-p
  4153. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4154. (setq n (1+ n)))
  4155. (condition-case nil
  4156. (make-indirect-buffer buffer bname 'clone)
  4157. (error (make-indirect-buffer buffer bname)))))
  4158. (defun org-set-frame-title (title)
  4159. "Set the title of the current frame to the string TITLE."
  4160. ;; FIXME: how to name a single frame in XEmacs???
  4161. (unless (featurep 'xemacs)
  4162. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4163. ;;;; Structure editing
  4164. ;;; Inserting headlines
  4165. (defun org-insert-heading (&optional force-heading)
  4166. "Insert a new heading or item with same depth at point.
  4167. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4168. If point is at the beginning of a headline, insert a sibling before the
  4169. current headline. If point is not at the beginning, do not split the line,
  4170. but create the new headline after the current line."
  4171. (interactive "P")
  4172. (if (= (buffer-size) 0)
  4173. (insert "\n* ")
  4174. (when (or force-heading (not (org-insert-item)))
  4175. (let* ((head (save-excursion
  4176. (condition-case nil
  4177. (progn
  4178. (org-back-to-heading)
  4179. (match-string 0))
  4180. (error "*"))))
  4181. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4182. pos hide-previous previous-pos)
  4183. (cond
  4184. ((and (org-on-heading-p) (bolp)
  4185. (or (bobp)
  4186. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4187. ;; insert before the current line
  4188. (open-line (if blank 2 1)))
  4189. ((and (bolp)
  4190. (or (bobp)
  4191. (save-excursion
  4192. (backward-char 1) (not (org-invisible-p)))))
  4193. ;; insert right here
  4194. nil)
  4195. (t
  4196. ;; somewhere in the line
  4197. (save-excursion
  4198. (setq previous-pos (point-at-bol))
  4199. (end-of-line)
  4200. (setq hide-previous (org-invisible-p)))
  4201. (and org-insert-heading-respect-content (org-show-subtree))
  4202. (let ((split
  4203. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4204. (save-excursion
  4205. (let ((p (point)))
  4206. (goto-char (point-at-bol))
  4207. (and (looking-at org-complex-heading-regexp)
  4208. (> p (match-beginning 4)))))))
  4209. tags pos)
  4210. (cond
  4211. (org-insert-heading-respect-content
  4212. (org-end-of-subtree nil t)
  4213. (or (bolp) (newline))
  4214. (open-line 1))
  4215. ((org-on-heading-p)
  4216. (when hide-previous
  4217. (show-children)
  4218. (org-show-entry))
  4219. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4220. (setq tags (and (match-end 2) (match-string 2)))
  4221. (and (match-end 1)
  4222. (delete-region (match-beginning 1) (match-end 1)))
  4223. (setq pos (point-at-bol))
  4224. (or split (end-of-line 1))
  4225. (delete-horizontal-space)
  4226. (newline (if blank 2 1))
  4227. (when tags
  4228. (save-excursion
  4229. (goto-char pos)
  4230. (end-of-line 1)
  4231. (insert " " tags)
  4232. (org-set-tags nil 'align))))
  4233. (t
  4234. (or split (end-of-line 1))
  4235. (newline (if blank 2 1)))))))
  4236. (insert head) (just-one-space)
  4237. (setq pos (point))
  4238. (end-of-line 1)
  4239. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4240. (when (and org-insert-heading-respect-content hide-previous)
  4241. (save-excursion
  4242. (goto-char previous-pos)
  4243. (hide-subtree)))
  4244. (run-hooks 'org-insert-heading-hook)))))
  4245. (defun org-get-heading (&optional no-tags)
  4246. "Return the heading of the current entry, without the stars."
  4247. (save-excursion
  4248. (org-back-to-heading t)
  4249. (if (looking-at
  4250. (if no-tags
  4251. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4252. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4253. (match-string 1) "")))
  4254. (defun org-insert-heading-after-current ()
  4255. "Insert a new heading with same level as current, after current subtree."
  4256. (interactive)
  4257. (org-back-to-heading)
  4258. (org-insert-heading)
  4259. (org-move-subtree-down)
  4260. (end-of-line 1))
  4261. (defun org-insert-heading-respect-content ()
  4262. (interactive)
  4263. (let ((org-insert-heading-respect-content t))
  4264. (org-insert-heading t)))
  4265. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4266. (interactive "P")
  4267. (let ((org-insert-heading-respect-content t))
  4268. (org-insert-todo-heading force-state t)))
  4269. (defun org-insert-todo-heading (arg &optional force-heading)
  4270. "Insert a new heading with the same level and TODO state as current heading.
  4271. If the heading has no TODO state, or if the state is DONE, use the first
  4272. state (TODO by default). Also with prefix arg, force first state."
  4273. (interactive "P")
  4274. (when (or force-heading (not (org-insert-item 'checkbox)))
  4275. (org-insert-heading force-heading)
  4276. (save-excursion
  4277. (org-back-to-heading)
  4278. (outline-previous-heading)
  4279. (looking-at org-todo-line-regexp))
  4280. (if (or arg
  4281. (not (match-beginning 2))
  4282. (member (match-string 2) org-done-keywords))
  4283. (insert (car org-todo-keywords-1) " ")
  4284. (insert (match-string 2) " "))
  4285. (when org-provide-todo-statistics
  4286. (org-update-parent-todo-statistics))))
  4287. (defun org-insert-subheading (arg)
  4288. "Insert a new subheading and demote it.
  4289. Works for outline headings and for plain lists alike."
  4290. (interactive "P")
  4291. (org-insert-heading arg)
  4292. (cond
  4293. ((org-on-heading-p) (org-do-demote))
  4294. ((org-at-item-p) (org-indent-item 1))))
  4295. (defun org-insert-todo-subheading (arg)
  4296. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4297. Works for outline headings and for plain lists alike."
  4298. (interactive "P")
  4299. (org-insert-todo-heading arg)
  4300. (cond
  4301. ((org-on-heading-p) (org-do-demote))
  4302. ((org-at-item-p) (org-indent-item 1))))
  4303. ;;; Promotion and Demotion
  4304. (defun org-promote-subtree ()
  4305. "Promote the entire subtree.
  4306. See also `org-promote'."
  4307. (interactive)
  4308. (save-excursion
  4309. (org-map-tree 'org-promote))
  4310. (org-fix-position-after-promote))
  4311. (defun org-demote-subtree ()
  4312. "Demote the entire subtree. See `org-demote'.
  4313. See also `org-promote'."
  4314. (interactive)
  4315. (save-excursion
  4316. (org-map-tree 'org-demote))
  4317. (org-fix-position-after-promote))
  4318. (defun org-do-promote ()
  4319. "Promote the current heading higher up the tree.
  4320. If the region is active in `transient-mark-mode', promote all headings
  4321. in the region."
  4322. (interactive)
  4323. (save-excursion
  4324. (if (org-region-active-p)
  4325. (org-map-region 'org-promote (region-beginning) (region-end))
  4326. (org-promote)))
  4327. (org-fix-position-after-promote))
  4328. (defun org-do-demote ()
  4329. "Demote the current heading lower down the tree.
  4330. If the region is active in `transient-mark-mode', demote all headings
  4331. in the region."
  4332. (interactive)
  4333. (save-excursion
  4334. (if (org-region-active-p)
  4335. (org-map-region 'org-demote (region-beginning) (region-end))
  4336. (org-demote)))
  4337. (org-fix-position-after-promote))
  4338. (defun org-fix-position-after-promote ()
  4339. "Make sure that after pro/demotion cursor position is right."
  4340. (let ((pos (point)))
  4341. (when (save-excursion
  4342. (beginning-of-line 1)
  4343. (looking-at org-todo-line-regexp)
  4344. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4345. (cond ((eobp) (insert " "))
  4346. ((eolp) (insert " "))
  4347. ((equal (char-after) ?\ ) (forward-char 1))))))
  4348. (defun org-reduced-level (l)
  4349. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4350. (defun org-get-valid-level (level &optional change)
  4351. "Rectify a level change under the influence of `org-odd-levels-only'
  4352. LEVEL is a current level, CHANGE is by how much the level should be
  4353. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4354. even level numbers will become the next higher odd number."
  4355. (if org-odd-levels-only
  4356. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4357. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4358. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4359. (max 1 (+ level change))))
  4360. (if (boundp 'define-obsolete-function-alias)
  4361. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4362. (define-obsolete-function-alias 'org-get-legal-level
  4363. 'org-get-valid-level)
  4364. (define-obsolete-function-alias 'org-get-legal-level
  4365. 'org-get-valid-level "23.1")))
  4366. (defun org-promote ()
  4367. "Promote the current heading higher up the tree.
  4368. If the region is active in `transient-mark-mode', promote all headings
  4369. in the region."
  4370. (org-back-to-heading t)
  4371. (let* ((level (save-match-data (funcall outline-level)))
  4372. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4373. (diff (abs (- level (length up-head) -1))))
  4374. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4375. (replace-match up-head nil t)
  4376. ;; Fixup tag positioning
  4377. (and org-auto-align-tags (org-set-tags nil t))
  4378. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4379. (defun org-demote ()
  4380. "Demote the current heading lower down the tree.
  4381. If the region is active in `transient-mark-mode', demote all headings
  4382. in the region."
  4383. (org-back-to-heading t)
  4384. (let* ((level (save-match-data (funcall outline-level)))
  4385. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4386. (diff (abs (- level (length down-head) -1))))
  4387. (replace-match down-head nil t)
  4388. ;; Fixup tag positioning
  4389. (and org-auto-align-tags (org-set-tags nil t))
  4390. (if org-adapt-indentation (org-fixup-indentation diff))))
  4391. (defun org-map-tree (fun)
  4392. "Call FUN for every heading underneath the current one."
  4393. (org-back-to-heading)
  4394. (let ((level (funcall outline-level)))
  4395. (save-excursion
  4396. (funcall fun)
  4397. (while (and (progn
  4398. (outline-next-heading)
  4399. (> (funcall outline-level) level))
  4400. (not (eobp)))
  4401. (funcall fun)))))
  4402. (defun org-map-region (fun beg end)
  4403. "Call FUN for every heading between BEG and END."
  4404. (let ((org-ignore-region t))
  4405. (save-excursion
  4406. (setq end (copy-marker end))
  4407. (goto-char beg)
  4408. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4409. (< (point) end))
  4410. (funcall fun))
  4411. (while (and (progn
  4412. (outline-next-heading)
  4413. (< (point) end))
  4414. (not (eobp)))
  4415. (funcall fun)))))
  4416. (defun org-fixup-indentation (diff)
  4417. "Change the indentation in the current entry by DIFF
  4418. However, if any line in the current entry has no indentation, or if it
  4419. would end up with no indentation after the change, nothing at all is done."
  4420. (save-excursion
  4421. (let ((end (save-excursion (outline-next-heading)
  4422. (point-marker)))
  4423. (prohibit (if (> diff 0)
  4424. "^\\S-"
  4425. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4426. col)
  4427. (unless (save-excursion (end-of-line 1)
  4428. (re-search-forward prohibit end t))
  4429. (while (and (< (point) end)
  4430. (re-search-forward "^[ \t]+" end t))
  4431. (goto-char (match-end 0))
  4432. (setq col (current-column))
  4433. (if (< diff 0) (replace-match ""))
  4434. (indent-to (+ diff col))))
  4435. (move-marker end nil))))
  4436. (defun org-convert-to-odd-levels ()
  4437. "Convert an org-mode file with all levels allowed to one with odd levels.
  4438. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4439. level 5 etc."
  4440. (interactive)
  4441. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4442. (let ((org-odd-levels-only nil) n)
  4443. (save-excursion
  4444. (goto-char (point-min))
  4445. (while (re-search-forward "^\\*\\*+ " nil t)
  4446. (setq n (- (length (match-string 0)) 2))
  4447. (while (>= (setq n (1- n)) 0)
  4448. (org-demote))
  4449. (end-of-line 1))))))
  4450. (defun org-convert-to-oddeven-levels ()
  4451. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4452. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4453. section with an even level, conversion would destroy the structure of the file. An error
  4454. is signaled in this case."
  4455. (interactive)
  4456. (goto-char (point-min))
  4457. ;; First check if there are no even levels
  4458. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4459. (org-show-context t)
  4460. (error "Not all levels are odd in this file. Conversion not possible."))
  4461. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4462. (let ((org-odd-levels-only nil) n)
  4463. (save-excursion
  4464. (goto-char (point-min))
  4465. (while (re-search-forward "^\\*\\*+ " nil t)
  4466. (setq n (/ (1- (length (match-string 0))) 2))
  4467. (while (>= (setq n (1- n)) 0)
  4468. (org-promote))
  4469. (end-of-line 1))))))
  4470. (defun org-tr-level (n)
  4471. "Make N odd if required."
  4472. (if org-odd-levels-only (1+ (/ n 2)) n))
  4473. ;;; Vertical tree motion, cutting and pasting of subtrees
  4474. (defun org-move-subtree-up (&optional arg)
  4475. "Move the current subtree up past ARG headlines of the same level."
  4476. (interactive "p")
  4477. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4478. (defun org-move-subtree-down (&optional arg)
  4479. "Move the current subtree down past ARG headlines of the same level."
  4480. (interactive "p")
  4481. (setq arg (prefix-numeric-value arg))
  4482. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4483. 'outline-get-last-sibling))
  4484. (ins-point (make-marker))
  4485. (cnt (abs arg))
  4486. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4487. ;; Select the tree
  4488. (org-back-to-heading)
  4489. (setq beg0 (point))
  4490. (save-excursion
  4491. (setq ne-beg (org-back-over-empty-lines))
  4492. (setq beg (point)))
  4493. (save-match-data
  4494. (save-excursion (outline-end-of-heading)
  4495. (setq folded (org-invisible-p)))
  4496. (outline-end-of-subtree))
  4497. (outline-next-heading)
  4498. (setq ne-end (org-back-over-empty-lines))
  4499. (setq end (point))
  4500. (goto-char beg0)
  4501. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4502. ;; include less whitespace
  4503. (save-excursion
  4504. (goto-char beg)
  4505. (forward-line (- ne-beg ne-end))
  4506. (setq beg (point))))
  4507. ;; Find insertion point, with error handling
  4508. (while (> cnt 0)
  4509. (or (and (funcall movfunc) (looking-at outline-regexp))
  4510. (progn (goto-char beg0)
  4511. (error "Cannot move past superior level or buffer limit")))
  4512. (setq cnt (1- cnt)))
  4513. (if (> arg 0)
  4514. ;; Moving forward - still need to move over subtree
  4515. (progn (org-end-of-subtree t t)
  4516. (save-excursion
  4517. (org-back-over-empty-lines)
  4518. (or (bolp) (newline)))))
  4519. (setq ne-ins (org-back-over-empty-lines))
  4520. (move-marker ins-point (point))
  4521. (setq txt (buffer-substring beg end))
  4522. (org-save-markers-in-region beg end)
  4523. (delete-region beg end)
  4524. (outline-flag-region (1- beg) beg nil)
  4525. (outline-flag-region (1- (point)) (point) nil)
  4526. (let ((bbb (point)))
  4527. (insert-before-markers txt)
  4528. (org-reinstall-markers-in-region bbb)
  4529. (move-marker ins-point bbb))
  4530. (or (bolp) (insert "\n"))
  4531. (setq ins-end (point))
  4532. (goto-char ins-point)
  4533. (org-skip-whitespace)
  4534. (when (and (< arg 0)
  4535. (org-first-sibling-p)
  4536. (> ne-ins ne-beg))
  4537. ;; Move whitespace back to beginning
  4538. (save-excursion
  4539. (goto-char ins-end)
  4540. (let ((kill-whole-line t))
  4541. (kill-line (- ne-ins ne-beg)) (point)))
  4542. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4543. (move-marker ins-point nil)
  4544. (org-compact-display-after-subtree-move)
  4545. (org-show-empty-lines-in-parent)
  4546. (unless folded
  4547. (org-show-entry)
  4548. (show-children)
  4549. (org-cycle-hide-drawers 'children))))
  4550. (defvar org-subtree-clip ""
  4551. "Clipboard for cut and paste of subtrees.
  4552. This is actually only a copy of the kill, because we use the normal kill
  4553. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4554. (defvar org-subtree-clip-folded nil
  4555. "Was the last copied subtree folded?
  4556. This is used to fold the tree back after pasting.")
  4557. (defun org-cut-subtree (&optional n)
  4558. "Cut the current subtree into the clipboard.
  4559. With prefix arg N, cut this many sequential subtrees.
  4560. This is a short-hand for marking the subtree and then cutting it."
  4561. (interactive "p")
  4562. (org-copy-subtree n 'cut))
  4563. (defun org-copy-subtree (&optional n cut force-store-markers)
  4564. "Cut the current subtree into the clipboard.
  4565. With prefix arg N, cut this many sequential subtrees.
  4566. This is a short-hand for marking the subtree and then copying it.
  4567. If CUT is non-nil, actually cut the subtree.
  4568. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4569. of some markers in the region, even if CUT is non-nil. This is
  4570. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4571. (interactive "p")
  4572. (let (beg end folded (beg0 (point)))
  4573. (if (interactive-p)
  4574. (org-back-to-heading nil) ; take what looks like a subtree
  4575. (org-back-to-heading t)) ; take what is really there
  4576. (org-back-over-empty-lines)
  4577. (setq beg (point))
  4578. (skip-chars-forward " \t\r\n")
  4579. (save-match-data
  4580. (save-excursion (outline-end-of-heading)
  4581. (setq folded (org-invisible-p)))
  4582. (condition-case nil
  4583. (outline-forward-same-level (1- n))
  4584. (error nil))
  4585. (org-end-of-subtree t t))
  4586. (org-back-over-empty-lines)
  4587. (setq end (point))
  4588. (goto-char beg0)
  4589. (when (> end beg)
  4590. (setq org-subtree-clip-folded folded)
  4591. (when (or cut force-store-markers)
  4592. (org-save-markers-in-region beg end))
  4593. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4594. (setq org-subtree-clip (current-kill 0))
  4595. (message "%s: Subtree(s) with %d characters"
  4596. (if cut "Cut" "Copied")
  4597. (length org-subtree-clip)))))
  4598. (defun org-paste-subtree (&optional level tree for-yank)
  4599. "Paste the clipboard as a subtree, with modification of headline level.
  4600. The entire subtree is promoted or demoted in order to match a new headline
  4601. level.
  4602. If the cursor is at the beginning of a headline, the same level as
  4603. that headline is used to paste the tree
  4604. If not, the new level is derived from the *visible* headings
  4605. before and after the insertion point, and taken to be the inferior headline
  4606. level of the two. So if the previous visible heading is level 3 and the
  4607. next is level 4 (or vice versa), level 4 will be used for insertion.
  4608. This makes sure that the subtree remains an independent subtree and does
  4609. not swallow low level entries.
  4610. You can also force a different level, either by using a numeric prefix
  4611. argument, or by inserting the heading marker by hand. For example, if the
  4612. cursor is after \"*****\", then the tree will be shifted to level 5.
  4613. If optional TREE is given, use this text instead of the kill ring.
  4614. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4615. move back over whitespace before inserting, and move point to the end of
  4616. the inserted text when done."
  4617. (interactive "P")
  4618. (unless (org-kill-is-subtree-p tree)
  4619. (error "%s"
  4620. (substitute-command-keys
  4621. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4622. (let* ((visp (not (org-invisible-p)))
  4623. (txt (or tree (and kill-ring (current-kill 0))))
  4624. (^re (concat "^\\(" outline-regexp "\\)"))
  4625. (re (concat "\\(" outline-regexp "\\)"))
  4626. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4627. (old-level (if (string-match ^re txt)
  4628. (- (match-end 0) (match-beginning 0) 1)
  4629. -1))
  4630. (force-level (cond (level (prefix-numeric-value level))
  4631. ((and (looking-at "[ \t]*$")
  4632. (string-match
  4633. ^re_ (buffer-substring
  4634. (point-at-bol) (point))))
  4635. (- (match-end 1) (match-beginning 1)))
  4636. ((and (bolp)
  4637. (looking-at org-outline-regexp))
  4638. (- (match-end 0) (point) 1))
  4639. (t nil)))
  4640. (previous-level (save-excursion
  4641. (condition-case nil
  4642. (progn
  4643. (outline-previous-visible-heading 1)
  4644. (if (looking-at re)
  4645. (- (match-end 0) (match-beginning 0) 1)
  4646. 1))
  4647. (error 1))))
  4648. (next-level (save-excursion
  4649. (condition-case nil
  4650. (progn
  4651. (or (looking-at outline-regexp)
  4652. (outline-next-visible-heading 1))
  4653. (if (looking-at re)
  4654. (- (match-end 0) (match-beginning 0) 1)
  4655. 1))
  4656. (error 1))))
  4657. (new-level (or force-level (max previous-level next-level)))
  4658. (shift (if (or (= old-level -1)
  4659. (= new-level -1)
  4660. (= old-level new-level))
  4661. 0
  4662. (- new-level old-level)))
  4663. (delta (if (> shift 0) -1 1))
  4664. (func (if (> shift 0) 'org-demote 'org-promote))
  4665. (org-odd-levels-only nil)
  4666. beg end newend)
  4667. ;; Remove the forced level indicator
  4668. (if force-level
  4669. (delete-region (point-at-bol) (point)))
  4670. ;; Paste
  4671. (beginning-of-line 1)
  4672. (unless for-yank (org-back-over-empty-lines))
  4673. (setq beg (point))
  4674. (insert-before-markers txt)
  4675. (unless (string-match "\n\\'" txt) (insert "\n"))
  4676. (setq newend (point))
  4677. (org-reinstall-markers-in-region beg)
  4678. (setq end (point))
  4679. (goto-char beg)
  4680. (skip-chars-forward " \t\n\r")
  4681. (setq beg (point))
  4682. (if (and (org-invisible-p) visp)
  4683. (save-excursion (outline-show-heading)))
  4684. ;; Shift if necessary
  4685. (unless (= shift 0)
  4686. (save-restriction
  4687. (narrow-to-region beg end)
  4688. (while (not (= shift 0))
  4689. (org-map-region func (point-min) (point-max))
  4690. (setq shift (+ delta shift)))
  4691. (goto-char (point-min))
  4692. (setq newend (point-max))))
  4693. (when (or (interactive-p) for-yank)
  4694. (message "Clipboard pasted as level %d subtree" new-level))
  4695. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4696. kill-ring
  4697. (eq org-subtree-clip (current-kill 0))
  4698. org-subtree-clip-folded)
  4699. ;; The tree was folded before it was killed/copied
  4700. (hide-subtree))
  4701. (and for-yank (goto-char newend))))
  4702. (defun org-kill-is-subtree-p (&optional txt)
  4703. "Check if the current kill is an outline subtree, or a set of trees.
  4704. Returns nil if kill does not start with a headline, or if the first
  4705. headline level is not the largest headline level in the tree.
  4706. So this will actually accept several entries of equal levels as well,
  4707. which is OK for `org-paste-subtree'.
  4708. If optional TXT is given, check this string instead of the current kill."
  4709. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4710. (start-level (and kill
  4711. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4712. org-outline-regexp "\\)")
  4713. kill)
  4714. (- (match-end 2) (match-beginning 2) 1)))
  4715. (re (concat "^" org-outline-regexp))
  4716. (start (1+ (or (match-beginning 2) -1))))
  4717. (if (not start-level)
  4718. (progn
  4719. nil) ;; does not even start with a heading
  4720. (catch 'exit
  4721. (while (setq start (string-match re kill (1+ start)))
  4722. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4723. (throw 'exit nil)))
  4724. t))))
  4725. (defvar org-markers-to-move nil
  4726. "Markers that should be moved with a cut-and-paste operation.
  4727. Those markers are stored together with their positions relative to
  4728. the start of the region.")
  4729. (defun org-save-markers-in-region (beg end)
  4730. "Check markers in region.
  4731. If these markers are between BEG and END, record their position relative
  4732. to BEG, so that after moving the block of text, we can put the markers back
  4733. into place.
  4734. This function gets called just before an entry or tree gets cut from the
  4735. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4736. called immediately, to move the markers with the entries."
  4737. (setq org-markers-to-move nil)
  4738. (when (featurep 'org-clock)
  4739. (org-clock-save-markers-for-cut-and-paste beg end))
  4740. (when (featurep 'org-agenda)
  4741. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4742. (defun org-check-and-save-marker (marker beg end)
  4743. "Check if MARKER is between BEG and END.
  4744. If yes, remember the marker and the distance to BEG."
  4745. (when (and (marker-buffer marker)
  4746. (equal (marker-buffer marker) (current-buffer)))
  4747. (if (and (>= marker beg) (< marker end))
  4748. (push (cons marker (- marker beg)) org-markers-to-move))))
  4749. (defun org-reinstall-markers-in-region (beg)
  4750. "Move all remembered markers to their position relative to BEG."
  4751. (mapc (lambda (x)
  4752. (move-marker (car x) (+ beg (cdr x))))
  4753. org-markers-to-move)
  4754. (setq org-markers-to-move nil))
  4755. (defun org-narrow-to-subtree ()
  4756. "Narrow buffer to the current subtree."
  4757. (interactive)
  4758. (save-excursion
  4759. (save-match-data
  4760. (narrow-to-region
  4761. (progn (org-back-to-heading) (point))
  4762. (progn (org-end-of-subtree t) (point))))))
  4763. ;;; Outline Sorting
  4764. (defun org-sort (with-case)
  4765. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4766. Optional argument WITH-CASE means sort case-sensitively."
  4767. (interactive "P")
  4768. (if (org-at-table-p)
  4769. (org-call-with-arg 'org-table-sort-lines with-case)
  4770. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4771. (defun org-sort-remove-invisible (s)
  4772. (remove-text-properties 0 (length s) org-rm-props s)
  4773. (while (string-match org-bracket-link-regexp s)
  4774. (setq s (replace-match (if (match-end 2)
  4775. (match-string 3 s)
  4776. (match-string 1 s)) t t s)))
  4777. s)
  4778. (defvar org-priority-regexp) ; defined later in the file
  4779. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4780. "Sort entries on a certain level of an outline tree.
  4781. If there is an active region, the entries in the region are sorted.
  4782. Else, if the cursor is before the first entry, sort the top-level items.
  4783. Else, the children of the entry at point are sorted.
  4784. Sorting can be alphabetically, numerically, and by date/time as given by
  4785. the first time stamp in the entry. The command prompts for the sorting
  4786. type unless it has been given to the function through the SORTING-TYPE
  4787. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4788. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4789. called with point at the beginning of the record. It must return either
  4790. a string or a number that should serve as the sorting key for that record.
  4791. Comparing entries ignores case by default. However, with an optional argument
  4792. WITH-CASE, the sorting considers case as well."
  4793. (interactive "P")
  4794. (let ((case-func (if with-case 'identity 'downcase))
  4795. start beg end stars re re2
  4796. txt what tmp plain-list-p)
  4797. ;; Find beginning and end of region to sort
  4798. (cond
  4799. ((org-region-active-p)
  4800. ;; we will sort the region
  4801. (setq end (region-end)
  4802. what "region")
  4803. (goto-char (region-beginning))
  4804. (if (not (org-on-heading-p)) (outline-next-heading))
  4805. (setq start (point)))
  4806. ((org-at-item-p)
  4807. ;; we will sort this plain list
  4808. (org-beginning-of-item-list) (setq start (point))
  4809. (org-end-of-item-list) (setq end (point))
  4810. (goto-char start)
  4811. (setq plain-list-p t
  4812. what "plain list"))
  4813. ((or (org-on-heading-p)
  4814. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4815. ;; we will sort the children of the current headline
  4816. (org-back-to-heading)
  4817. (setq start (point)
  4818. end (progn (org-end-of-subtree t t)
  4819. (org-back-over-empty-lines)
  4820. (point))
  4821. what "children")
  4822. (goto-char start)
  4823. (show-subtree)
  4824. (outline-next-heading))
  4825. (t
  4826. ;; we will sort the top-level entries in this file
  4827. (goto-char (point-min))
  4828. (or (org-on-heading-p) (outline-next-heading))
  4829. (setq start (point) end (point-max) what "top-level")
  4830. (goto-char start)
  4831. (show-all)))
  4832. (setq beg (point))
  4833. (if (>= beg end) (error "Nothing to sort"))
  4834. (unless plain-list-p
  4835. (looking-at "\\(\\*+\\)")
  4836. (setq stars (match-string 1)
  4837. re (concat "^" (regexp-quote stars) " +")
  4838. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4839. txt (buffer-substring beg end))
  4840. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4841. (if (and (not (equal stars "*")) (string-match re2 txt))
  4842. (error "Region to sort contains a level above the first entry")))
  4843. (unless sorting-type
  4844. (message
  4845. (if plain-list-p
  4846. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4847. "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:")
  4848. what)
  4849. (setq sorting-type (read-char-exclusive))
  4850. (and (= (downcase sorting-type) ?f)
  4851. (setq getkey-func
  4852. (completing-read "Sort using function: "
  4853. obarray 'fboundp t nil nil))
  4854. (setq getkey-func (intern getkey-func)))
  4855. (and (= (downcase sorting-type) ?r)
  4856. (setq property
  4857. (completing-read "Property: "
  4858. (mapcar 'list (org-buffer-property-keys t))
  4859. nil t))))
  4860. (message "Sorting entries...")
  4861. (save-restriction
  4862. (narrow-to-region start end)
  4863. (let ((dcst (downcase sorting-type))
  4864. (now (current-time)))
  4865. (sort-subr
  4866. (/= dcst sorting-type)
  4867. ;; This function moves to the beginning character of the "record" to
  4868. ;; be sorted.
  4869. (if plain-list-p
  4870. (lambda nil
  4871. (if (org-at-item-p) t (goto-char (point-max))))
  4872. (lambda nil
  4873. (if (re-search-forward re nil t)
  4874. (goto-char (match-beginning 0))
  4875. (goto-char (point-max)))))
  4876. ;; This function moves to the last character of the "record" being
  4877. ;; sorted.
  4878. (if plain-list-p
  4879. 'org-end-of-item
  4880. (lambda nil
  4881. (save-match-data
  4882. (condition-case nil
  4883. (outline-forward-same-level 1)
  4884. (error
  4885. (goto-char (point-max)))))))
  4886. ;; This function returns the value that gets sorted against.
  4887. (if plain-list-p
  4888. (lambda nil
  4889. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4890. (cond
  4891. ((= dcst ?n)
  4892. (string-to-number (buffer-substring (match-end 0)
  4893. (point-at-eol))))
  4894. ((= dcst ?a)
  4895. (buffer-substring (match-end 0) (point-at-eol)))
  4896. ((= dcst ?t)
  4897. (if (re-search-forward org-ts-regexp
  4898. (point-at-eol) t)
  4899. (org-time-string-to-time (match-string 0))
  4900. now))
  4901. ((= dcst ?f)
  4902. (if getkey-func
  4903. (progn
  4904. (setq tmp (funcall getkey-func))
  4905. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4906. tmp)
  4907. (error "Invalid key function `%s'" getkey-func)))
  4908. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4909. (lambda nil
  4910. (cond
  4911. ((= dcst ?n)
  4912. (if (looking-at org-complex-heading-regexp)
  4913. (string-to-number (match-string 4))
  4914. nil))
  4915. ((= dcst ?a)
  4916. (if (looking-at org-complex-heading-regexp)
  4917. (funcall case-func (match-string 4))
  4918. nil))
  4919. ((= dcst ?t)
  4920. (if (re-search-forward org-ts-regexp
  4921. (save-excursion
  4922. (forward-line 2)
  4923. (point)) t)
  4924. (org-time-string-to-time (match-string 0))
  4925. now))
  4926. ((= dcst ?p)
  4927. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4928. (string-to-char (match-string 2))
  4929. org-default-priority))
  4930. ((= dcst ?r)
  4931. (or (org-entry-get nil property) ""))
  4932. ((= dcst ?o)
  4933. (if (looking-at org-complex-heading-regexp)
  4934. (- 9999 (length (member (match-string 2)
  4935. org-todo-keywords-1)))))
  4936. ((= dcst ?f)
  4937. (if getkey-func
  4938. (progn
  4939. (setq tmp (funcall getkey-func))
  4940. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4941. tmp)
  4942. (error "Invalid key function `%s'" getkey-func)))
  4943. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4944. nil
  4945. (cond
  4946. ((= dcst ?a) 'string<)
  4947. ((= dcst ?t) 'time-less-p)
  4948. (t nil)))))
  4949. (message "Sorting entries...done")))
  4950. (defun org-do-sort (table what &optional with-case sorting-type)
  4951. "Sort TABLE of WHAT according to SORTING-TYPE.
  4952. The user will be prompted for the SORTING-TYPE if the call to this
  4953. function does not specify it. WHAT is only for the prompt, to indicate
  4954. what is being sorted. The sorting key will be extracted from
  4955. the car of the elements of the table.
  4956. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4957. (unless sorting-type
  4958. (message
  4959. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4960. what)
  4961. (setq sorting-type (read-char-exclusive)))
  4962. (let ((dcst (downcase sorting-type))
  4963. extractfun comparefun)
  4964. ;; Define the appropriate functions
  4965. (cond
  4966. ((= dcst ?n)
  4967. (setq extractfun 'string-to-number
  4968. comparefun (if (= dcst sorting-type) '< '>)))
  4969. ((= dcst ?a)
  4970. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4971. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4972. comparefun (if (= dcst sorting-type)
  4973. 'string<
  4974. (lambda (a b) (and (not (string< a b))
  4975. (not (string= a b)))))))
  4976. ((= dcst ?t)
  4977. (setq extractfun
  4978. (lambda (x)
  4979. (if (string-match org-ts-regexp x)
  4980. (time-to-seconds
  4981. (org-time-string-to-time (match-string 0 x)))
  4982. 0))
  4983. comparefun (if (= dcst sorting-type) '< '>)))
  4984. (t (error "Invalid sorting type `%c'" sorting-type)))
  4985. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4986. table)
  4987. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4988. ;;; Editing source examples
  4989. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4990. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4991. (defvar org-edit-src-force-single-line nil)
  4992. (defvar org-edit-src-from-org-mode nil)
  4993. (defvar org-edit-src-picture nil)
  4994. (define-minor-mode org-exit-edit-mode
  4995. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4996. (defun org-edit-src-code ()
  4997. "Edit the source code example at point.
  4998. An indirect buffer is created, and that buffer is then narrowed to the
  4999. example at point and switched to the correct language mode. When done,
  5000. exit by killing the buffer with \\[org-edit-src-exit]."
  5001. (interactive)
  5002. (let ((line (org-current-line))
  5003. (case-fold-search t)
  5004. (msg (substitute-command-keys
  5005. "Edit, then exit with C-c ' (C-c and single quote)"))
  5006. (info (org-edit-src-find-region-and-lang))
  5007. (org-mode-p (eq major-mode 'org-mode))
  5008. beg end lang lang-f single)
  5009. (if (not info)
  5010. nil
  5011. (setq beg (nth 0 info)
  5012. end (nth 1 info)
  5013. lang (nth 2 info)
  5014. single (nth 3 info)
  5015. lang-f (intern (concat lang "-mode")))
  5016. (unless (functionp lang-f)
  5017. (error "No such language mode: %s" lang-f))
  5018. (goto-line line)
  5019. (if (get-buffer "*Org Edit Src Example*")
  5020. (kill-buffer "*Org Edit Src Example*"))
  5021. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5022. "*Org Edit Src Example*"))
  5023. (narrow-to-region beg end)
  5024. (remove-text-properties beg end '(display nil invisible nil
  5025. intangible nil))
  5026. (let ((org-inhibit-startup t))
  5027. (funcall lang-f))
  5028. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5029. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5030. (when org-mode-p
  5031. (goto-char (point-min))
  5032. (while (re-search-forward "^," nil t)
  5033. (replace-match "")))
  5034. (goto-line line)
  5035. (org-exit-edit-mode)
  5036. (org-set-local 'header-line-format msg)
  5037. (message "%s" msg)
  5038. t)))
  5039. (defun org-edit-fixed-width-region ()
  5040. "Edit the fixed-width ascii drawing at point.
  5041. This must be a region where each line starts with ca colon followed by
  5042. a space character.
  5043. An indirect buffer is created, and that buffer is then narrowed to the
  5044. example at point and switched to artist-mode. When done,
  5045. exit by killing the buffer with \\[org-edit-src-exit]."
  5046. (interactive)
  5047. (let ((line (org-current-line))
  5048. (case-fold-search t)
  5049. (msg (substitute-command-keys
  5050. "Edit, then exit with C-c ' (C-c and single quote)"))
  5051. (org-mode-p (eq major-mode 'org-mode))
  5052. beg end lang lang-f)
  5053. (beginning-of-line 1)
  5054. (if (looking-at "[ \t]*[^:\n \t]")
  5055. nil
  5056. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5057. (setq beg (point) end beg)
  5058. (save-excursion
  5059. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5060. (setq beg (point-at-bol 2))
  5061. (setq beg (point))))
  5062. (save-excursion
  5063. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5064. (setq end (1- (match-beginning 0)))
  5065. (setq end (point))))
  5066. (goto-line line))
  5067. (if (get-buffer "*Org Edit Picture*")
  5068. (kill-buffer "*Org Edit Picture*"))
  5069. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5070. "*Org Edit Picture*"))
  5071. (narrow-to-region beg end)
  5072. (remove-text-properties beg end '(display nil invisible nil
  5073. intangible nil))
  5074. (when (fboundp 'font-lock-unfontify-region)
  5075. (font-lock-unfontify-region (point-min) (point-max)))
  5076. (cond
  5077. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5078. (fundamental-mode)
  5079. (artist-mode 1))
  5080. (t (funcall org-edit-fixed-width-region-mode)))
  5081. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5082. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5083. (set (make-local-variable 'org-edit-src-picture) t)
  5084. (goto-char (point-min))
  5085. (while (re-search-forward "^[ \t]*: ?" nil t)
  5086. (replace-match ""))
  5087. (goto-line line)
  5088. (org-exit-edit-mode)
  5089. (org-set-local 'header-line-format msg)
  5090. (message "%s" msg)
  5091. t)))
  5092. (defun org-edit-src-find-region-and-lang ()
  5093. "Find the region and language for a local edit.
  5094. Return a list with beginning and end of the region, a string representing
  5095. the language, a switch telling of the content should be in a single line."
  5096. (let ((re-list
  5097. (append
  5098. org-edit-src-region-extra
  5099. '(
  5100. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5101. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5102. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5103. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5104. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5105. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5106. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5107. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5108. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5109. ("^#\\+html:" "\n" "html" single-line)
  5110. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5111. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5112. ("^#\\+latex:" "\n" "latex" single-line)
  5113. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5114. ("^#\\+ascii:" "\n" "ascii" single-line)
  5115. )))
  5116. (pos (point))
  5117. re re1 re2 single beg end lang)
  5118. (catch 'exit
  5119. (while (setq entry (pop re-list))
  5120. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5121. single (nth 3 entry))
  5122. (save-excursion
  5123. (if (or (looking-at re1)
  5124. (re-search-backward re1 nil t))
  5125. (progn
  5126. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5127. (if (and (re-search-forward re2 nil t)
  5128. (>= (match-end 0) pos))
  5129. (throw 'exit (list beg (match-beginning 0) lang single))))
  5130. (if (or (looking-at re2)
  5131. (re-search-forward re2 nil t))
  5132. (progn
  5133. (setq end (match-beginning 0))
  5134. (if (and (re-search-backward re1 nil t)
  5135. (<= (match-beginning 0) pos))
  5136. (throw 'exit
  5137. (list (match-end 0) end
  5138. (org-edit-src-get-lang lang) single)))))))))))
  5139. (defun org-edit-src-get-lang (lang)
  5140. "Extract the src language."
  5141. (let ((m (match-string 0)))
  5142. (cond
  5143. ((stringp lang) lang)
  5144. ((integerp lang) (match-string lang))
  5145. ((and (eq lang 'lang)
  5146. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5147. (match-string 1 m))
  5148. ((and (eq lang 'style)
  5149. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5150. (match-string 1 m))
  5151. (t "fundamental"))))
  5152. (defun org-edit-src-exit ()
  5153. "Exit special edit and protect problematic lines."
  5154. (interactive)
  5155. (unless (buffer-base-buffer (current-buffer))
  5156. (error "This is not an indirect buffer, something is wrong..."))
  5157. (unless (> (point-min) 1)
  5158. (error "This buffer is not narrowed, something is wrong..."))
  5159. (goto-char (point-min))
  5160. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5161. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5162. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5163. (goto-char (point-min))
  5164. (while (re-search-forward "\n" nil t)
  5165. (replace-match " "))
  5166. (goto-char (point-min))
  5167. (if (looking-at "\\s-*") (replace-match " "))
  5168. (if (re-search-forward "\\s-+\\'" nil t)
  5169. (replace-match "")))
  5170. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5171. (goto-char (point-min))
  5172. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5173. (replace-match ",\\1"))
  5174. (when font-lock-mode
  5175. (font-lock-unfontify-region (point-min) (point-max)))
  5176. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5177. (when (org-bound-and-true-p org-edit-src-picture)
  5178. (untabify (point-min) (point-max))
  5179. (goto-char (point-min))
  5180. (while (re-search-forward "^" nil t)
  5181. (replace-match ": "))
  5182. (when font-lock-mode
  5183. (font-lock-unfontify-region (point-min) (point-max)))
  5184. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5185. (kill-buffer (current-buffer))
  5186. (and (org-mode-p) (org-restart-font-lock)))
  5187. ;;; The orgstruct minor mode
  5188. ;; Define a minor mode which can be used in other modes in order to
  5189. ;; integrate the org-mode structure editing commands.
  5190. ;; This is really a hack, because the org-mode structure commands use
  5191. ;; keys which normally belong to the major mode. Here is how it
  5192. ;; works: The minor mode defines all the keys necessary to operate the
  5193. ;; structure commands, but wraps the commands into a function which
  5194. ;; tests if the cursor is currently at a headline or a plain list
  5195. ;; item. If that is the case, the structure command is used,
  5196. ;; temporarily setting many Org-mode variables like regular
  5197. ;; expressions for filling etc. However, when any of those keys is
  5198. ;; used at a different location, function uses `key-binding' to look
  5199. ;; up if the key has an associated command in another currently active
  5200. ;; keymap (minor modes, major mode, global), and executes that
  5201. ;; command. There might be problems if any of the keys is otherwise
  5202. ;; used as a prefix key.
  5203. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5204. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5205. ;; addresses this by checking explicitly for both bindings.
  5206. (defvar orgstruct-mode-map (make-sparse-keymap)
  5207. "Keymap for the minor `orgstruct-mode'.")
  5208. (defvar org-local-vars nil
  5209. "List of local variables, for use by `orgstruct-mode'")
  5210. ;;;###autoload
  5211. (define-minor-mode orgstruct-mode
  5212. "Toggle the minor more `orgstruct-mode'.
  5213. This mode is for using Org-mode structure commands in other modes.
  5214. The following key behave as if Org-mode was active, if the cursor
  5215. is on a headline, or on a plain list item (both in the definition
  5216. of Org-mode).
  5217. M-up Move entry/item up
  5218. M-down Move entry/item down
  5219. M-left Promote
  5220. M-right Demote
  5221. M-S-up Move entry/item up
  5222. M-S-down Move entry/item down
  5223. M-S-left Promote subtree
  5224. M-S-right Demote subtree
  5225. M-q Fill paragraph and items like in Org-mode
  5226. C-c ^ Sort entries
  5227. C-c - Cycle list bullet
  5228. TAB Cycle item visibility
  5229. M-RET Insert new heading/item
  5230. S-M-RET Insert new TODO heading / Chekbox item
  5231. C-c C-c Set tags / toggle checkbox"
  5232. nil " OrgStruct" nil
  5233. (org-load-modules-maybe)
  5234. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5235. ;;;###autoload
  5236. (defun turn-on-orgstruct ()
  5237. "Unconditionally turn on `orgstruct-mode'."
  5238. (orgstruct-mode 1))
  5239. ;;;###autoload
  5240. (defun turn-on-orgstruct++ ()
  5241. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5242. In addition to setting orgstruct-mode, this also exports all indentation and
  5243. autofilling variables from org-mode into the buffer. Note that turning
  5244. off orgstruct-mode will *not* remove these additional settings."
  5245. (orgstruct-mode 1)
  5246. (let (var val)
  5247. (mapc
  5248. (lambda (x)
  5249. (when (string-match
  5250. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5251. (symbol-name (car x)))
  5252. (setq var (car x) val (nth 1 x))
  5253. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5254. org-local-vars)))
  5255. (defun orgstruct-error ()
  5256. "Error when there is no default binding for a structure key."
  5257. (interactive)
  5258. (error "This key has no function outside structure elements"))
  5259. (defun orgstruct-setup ()
  5260. "Setup orgstruct keymaps."
  5261. (let ((nfunc 0)
  5262. (bindings
  5263. (list
  5264. '([(meta up)] org-metaup)
  5265. '([(meta down)] org-metadown)
  5266. '([(meta left)] org-metaleft)
  5267. '([(meta right)] org-metaright)
  5268. '([(meta shift up)] org-shiftmetaup)
  5269. '([(meta shift down)] org-shiftmetadown)
  5270. '([(meta shift left)] org-shiftmetaleft)
  5271. '([(meta shift right)] org-shiftmetaright)
  5272. '([(shift up)] org-shiftup)
  5273. '([(shift down)] org-shiftdown)
  5274. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5275. '("\M-q" fill-paragraph)
  5276. '("\C-c^" org-sort)
  5277. '("\C-c-" org-cycle-list-bullet)))
  5278. elt key fun cmd)
  5279. (while (setq elt (pop bindings))
  5280. (setq nfunc (1+ nfunc))
  5281. (setq key (org-key (car elt))
  5282. fun (nth 1 elt)
  5283. cmd (orgstruct-make-binding fun nfunc key))
  5284. (org-defkey orgstruct-mode-map key cmd))
  5285. ;; Special treatment needed for TAB and RET
  5286. (org-defkey orgstruct-mode-map [(tab)]
  5287. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5288. (org-defkey orgstruct-mode-map "\C-i"
  5289. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5290. (org-defkey orgstruct-mode-map "\M-\C-m"
  5291. (orgstruct-make-binding 'org-insert-heading 105
  5292. "\M-\C-m" [(meta return)]))
  5293. (org-defkey orgstruct-mode-map [(meta return)]
  5294. (orgstruct-make-binding 'org-insert-heading 106
  5295. [(meta return)] "\M-\C-m"))
  5296. (org-defkey orgstruct-mode-map [(shift meta return)]
  5297. (orgstruct-make-binding 'org-insert-todo-heading 107
  5298. [(meta return)] "\M-\C-m"))
  5299. (unless org-local-vars
  5300. (setq org-local-vars (org-get-local-variables)))
  5301. t))
  5302. (defun orgstruct-make-binding (fun n &rest keys)
  5303. "Create a function for binding in the structure minor mode.
  5304. FUN is the command to call inside a table. N is used to create a unique
  5305. command name. KEYS are keys that should be checked in for a command
  5306. to execute outside of tables."
  5307. (eval
  5308. (list 'defun
  5309. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5310. '(arg)
  5311. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5312. "Outside of structure, run the binding of `"
  5313. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5314. "'.")
  5315. '(interactive "p")
  5316. (list 'if
  5317. '(org-context-p 'headline 'item)
  5318. (list 'org-run-like-in-org-mode (list 'quote fun))
  5319. (list 'let '(orgstruct-mode)
  5320. (list 'call-interactively
  5321. (append '(or)
  5322. (mapcar (lambda (k)
  5323. (list 'key-binding k))
  5324. keys)
  5325. '('orgstruct-error))))))))
  5326. (defun org-context-p (&rest contexts)
  5327. "Check if local context is any of CONTEXTS.
  5328. Possible values in the list of contexts are `table', `headline', and `item'."
  5329. (let ((pos (point)))
  5330. (goto-char (point-at-bol))
  5331. (prog1 (or (and (memq 'table contexts)
  5332. (looking-at "[ \t]*|"))
  5333. (and (memq 'headline contexts)
  5334. ;;????????? (looking-at "\\*+"))
  5335. (looking-at outline-regexp))
  5336. (and (memq 'item contexts)
  5337. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5338. (goto-char pos))))
  5339. (defun org-get-local-variables ()
  5340. "Return a list of all local variables in an org-mode buffer."
  5341. (let (varlist)
  5342. (with-current-buffer (get-buffer-create "*Org tmp*")
  5343. (erase-buffer)
  5344. (org-mode)
  5345. (setq varlist (buffer-local-variables)))
  5346. (kill-buffer "*Org tmp*")
  5347. (delq nil
  5348. (mapcar
  5349. (lambda (x)
  5350. (setq x
  5351. (if (symbolp x)
  5352. (list x)
  5353. (list (car x) (list 'quote (cdr x)))))
  5354. (if (string-match
  5355. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5356. (symbol-name (car x)))
  5357. x nil))
  5358. varlist))))
  5359. ;;;###autoload
  5360. (defun org-run-like-in-org-mode (cmd)
  5361. (org-load-modules-maybe)
  5362. (unless org-local-vars
  5363. (setq org-local-vars (org-get-local-variables)))
  5364. (eval (list 'let org-local-vars
  5365. (list 'call-interactively (list 'quote cmd)))))
  5366. ;;;; Archiving
  5367. (defun org-get-category (&optional pos)
  5368. "Get the category applying to position POS."
  5369. (get-text-property (or pos (point)) 'org-category))
  5370. (defun org-refresh-category-properties ()
  5371. "Refresh category text properties in the buffer."
  5372. (let ((def-cat (cond
  5373. ((null org-category)
  5374. (if buffer-file-name
  5375. (file-name-sans-extension
  5376. (file-name-nondirectory buffer-file-name))
  5377. "???"))
  5378. ((symbolp org-category) (symbol-name org-category))
  5379. (t org-category)))
  5380. beg end cat pos optionp)
  5381. (org-unmodified
  5382. (save-excursion
  5383. (save-restriction
  5384. (widen)
  5385. (goto-char (point-min))
  5386. (put-text-property (point) (point-max) 'org-category def-cat)
  5387. (while (re-search-forward
  5388. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5389. (setq pos (match-end 0)
  5390. optionp (equal (char-after (match-beginning 0)) ?#)
  5391. cat (org-trim (match-string 2)))
  5392. (if optionp
  5393. (setq beg (point-at-bol) end (point-max))
  5394. (org-back-to-heading t)
  5395. (setq beg (point) end (org-end-of-subtree t t)))
  5396. (put-text-property beg end 'org-category cat)
  5397. (goto-char pos)))))))
  5398. ;;;; Link Stuff
  5399. ;;; Link abbreviations
  5400. (defun org-link-expand-abbrev (link)
  5401. "Apply replacements as defined in `org-link-abbrev-alist."
  5402. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5403. (let* ((key (match-string 1 link))
  5404. (as (or (assoc key org-link-abbrev-alist-local)
  5405. (assoc key org-link-abbrev-alist)))
  5406. (tag (and (match-end 2) (match-string 3 link)))
  5407. rpl)
  5408. (if (not as)
  5409. link
  5410. (setq rpl (cdr as))
  5411. (cond
  5412. ((symbolp rpl) (funcall rpl tag))
  5413. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5414. ((string-match "%h" rpl)
  5415. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5416. (t (concat rpl tag)))))
  5417. link))
  5418. ;;; Storing and inserting links
  5419. (defvar org-insert-link-history nil
  5420. "Minibuffer history for links inserted with `org-insert-link'.")
  5421. (defvar org-stored-links nil
  5422. "Contains the links stored with `org-store-link'.")
  5423. (defvar org-store-link-plist nil
  5424. "Plist with info about the most recently link created with `org-store-link'.")
  5425. (defvar org-link-protocols nil
  5426. "Link protocols added to Org-mode using `org-add-link-type'.")
  5427. (defvar org-store-link-functions nil
  5428. "List of functions that are called to create and store a link.
  5429. Each function will be called in turn until one returns a non-nil
  5430. value. Each function should check if it is responsible for creating
  5431. this link (for example by looking at the major mode).
  5432. If not, it must exit and return nil.
  5433. If yes, it should return a non-nil value after a calling
  5434. `org-store-link-props' with a list of properties and values.
  5435. Special properties are:
  5436. :type The link prefix. like \"http\". This must be given.
  5437. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5438. This is obligatory as well.
  5439. :description Optional default description for the second pair
  5440. of brackets in an Org-mode link. The user can still change
  5441. this when inserting this link into an Org-mode buffer.
  5442. In addition to these, any additional properties can be specified
  5443. and then used in remember templates.")
  5444. (defun org-add-link-type (type &optional follow export)
  5445. "Add TYPE to the list of `org-link-types'.
  5446. Re-compute all regular expressions depending on `org-link-types'
  5447. FOLLOW and EXPORT are two functions.
  5448. FOLLOW should take the link path as the single argument and do whatever
  5449. is necessary to follow the link, for example find a file or display
  5450. a mail message.
  5451. EXPORT should format the link path for export to one of the export formats.
  5452. It should be a function accepting three arguments:
  5453. path the path of the link, the text after the prefix (like \"http:\")
  5454. desc the description of the link, if any, nil if there was no descripton
  5455. format the export format, a symbol like `html' or `latex'.
  5456. The function may use the FORMAT information to return different values
  5457. depending on the format. The return value will be put literally into
  5458. the exported file.
  5459. Org-mode has a built-in default for exporting links. If you are happy with
  5460. this default, there is no need to define an export function for the link
  5461. type. For a simple example of an export function, see `org-bbdb.el'."
  5462. (add-to-list 'org-link-types type t)
  5463. (org-make-link-regexps)
  5464. (if (assoc type org-link-protocols)
  5465. (setcdr (assoc type org-link-protocols) (list follow export))
  5466. (push (list type follow export) org-link-protocols)))
  5467. ;;;###autoload
  5468. (defun org-store-link (arg)
  5469. "\\<org-mode-map>Store an org-link to the current location.
  5470. This link is added to `org-stored-links' and can later be inserted
  5471. into an org-buffer with \\[org-insert-link].
  5472. For some link types, a prefix arg is interpreted:
  5473. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5474. For file links, arg negates `org-context-in-file-links'."
  5475. (interactive "P")
  5476. (org-load-modules-maybe)
  5477. (setq org-store-link-plist nil) ; reset
  5478. (let (link cpltxt desc description search txt)
  5479. (cond
  5480. ((run-hook-with-args-until-success 'org-store-link-functions)
  5481. (setq link (plist-get org-store-link-plist :link)
  5482. desc (or (plist-get org-store-link-plist :description) link)))
  5483. ((eq major-mode 'calendar-mode)
  5484. (let ((cd (calendar-cursor-to-date)))
  5485. (setq link
  5486. (format-time-string
  5487. (car org-time-stamp-formats)
  5488. (apply 'encode-time
  5489. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5490. nil nil nil))))
  5491. (org-store-link-props :type "calendar" :date cd)))
  5492. ((eq major-mode 'w3-mode)
  5493. (setq cpltxt (url-view-url t)
  5494. link (org-make-link cpltxt))
  5495. (org-store-link-props :type "w3" :url (url-view-url t)))
  5496. ((eq major-mode 'w3m-mode)
  5497. (setq cpltxt (or w3m-current-title w3m-current-url)
  5498. link (org-make-link w3m-current-url))
  5499. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5500. ((setq search (run-hook-with-args-until-success
  5501. 'org-create-file-search-functions))
  5502. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5503. "::" search))
  5504. (setq cpltxt (or description link)))
  5505. ((eq major-mode 'image-mode)
  5506. (setq cpltxt (concat "file:"
  5507. (abbreviate-file-name buffer-file-name))
  5508. link (org-make-link cpltxt))
  5509. (org-store-link-props :type "image" :file buffer-file-name))
  5510. ((eq major-mode 'dired-mode)
  5511. ;; link to the file in the current line
  5512. (setq cpltxt (concat "file:"
  5513. (abbreviate-file-name
  5514. (expand-file-name
  5515. (dired-get-filename nil t))))
  5516. link (org-make-link cpltxt)))
  5517. ((and buffer-file-name (org-mode-p))
  5518. ;; Just link to current headline
  5519. (setq cpltxt (concat "file:"
  5520. (abbreviate-file-name buffer-file-name)))
  5521. ;; Add a context search string
  5522. (when (org-xor org-context-in-file-links arg)
  5523. ;; Check if we are on a target
  5524. (if (org-in-regexp "<<\\(.*?\\)>>")
  5525. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5526. (setq txt (cond
  5527. ((org-on-heading-p) nil)
  5528. ((org-region-active-p)
  5529. (buffer-substring (region-beginning) (region-end)))
  5530. (t nil)))
  5531. (when (or (null txt) (string-match "\\S-" txt))
  5532. (setq cpltxt
  5533. (concat cpltxt "::"
  5534. (condition-case nil
  5535. (org-make-org-heading-search-string txt)
  5536. (error "")))
  5537. desc "NONE"))))
  5538. (if (string-match "::\\'" cpltxt)
  5539. (setq cpltxt (substring cpltxt 0 -2)))
  5540. (setq link (org-make-link cpltxt)))
  5541. ((buffer-file-name (buffer-base-buffer))
  5542. ;; Just link to this file here.
  5543. (setq cpltxt (concat "file:"
  5544. (abbreviate-file-name
  5545. (buffer-file-name (buffer-base-buffer)))))
  5546. ;; Add a context string
  5547. (when (org-xor org-context-in-file-links arg)
  5548. (setq txt (if (org-region-active-p)
  5549. (buffer-substring (region-beginning) (region-end))
  5550. (buffer-substring (point-at-bol) (point-at-eol))))
  5551. ;; Only use search option if there is some text.
  5552. (when (string-match "\\S-" txt)
  5553. (setq cpltxt
  5554. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5555. desc "NONE")))
  5556. (setq link (org-make-link cpltxt)))
  5557. ((interactive-p)
  5558. (error "Cannot link to a buffer which is not visiting a file"))
  5559. (t (setq link nil)))
  5560. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5561. (setq link (or link cpltxt)
  5562. desc (or desc cpltxt))
  5563. (if (equal desc "NONE") (setq desc nil))
  5564. (if (and (interactive-p) link)
  5565. (progn
  5566. (setq org-stored-links
  5567. (cons (list link desc) org-stored-links))
  5568. (message "Stored: %s" (or desc link)))
  5569. (and link (org-make-link-string link desc)))))
  5570. (defun org-store-link-props (&rest plist)
  5571. "Store link properties, extract names and addresses."
  5572. (let (x adr)
  5573. (when (setq x (plist-get plist :from))
  5574. (setq adr (mail-extract-address-components x))
  5575. (setq plist (plist-put plist :fromname (car adr)))
  5576. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5577. (when (setq x (plist-get plist :to))
  5578. (setq adr (mail-extract-address-components x))
  5579. (setq plist (plist-put plist :toname (car adr)))
  5580. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5581. (let ((from (plist-get plist :from))
  5582. (to (plist-get plist :to)))
  5583. (when (and from to org-from-is-user-regexp)
  5584. (setq plist
  5585. (plist-put plist :fromto
  5586. (if (string-match org-from-is-user-regexp from)
  5587. (concat "to %t")
  5588. (concat "from %f"))))))
  5589. (setq org-store-link-plist plist))
  5590. (defun org-add-link-props (&rest plist)
  5591. "Add these properties to the link property list."
  5592. (let (key value)
  5593. (while plist
  5594. (setq key (pop plist) value (pop plist))
  5595. (setq org-store-link-plist
  5596. (plist-put org-store-link-plist key value)))))
  5597. (defun org-email-link-description (&optional fmt)
  5598. "Return the description part of an email link.
  5599. This takes information from `org-store-link-plist' and formats it
  5600. according to FMT (default from `org-email-link-description-format')."
  5601. (setq fmt (or fmt org-email-link-description-format))
  5602. (let* ((p org-store-link-plist)
  5603. (to (plist-get p :toaddress))
  5604. (from (plist-get p :fromaddress))
  5605. (table
  5606. (list
  5607. (cons "%c" (plist-get p :fromto))
  5608. (cons "%F" (plist-get p :from))
  5609. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5610. (cons "%T" (plist-get p :to))
  5611. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5612. (cons "%s" (plist-get p :subject))
  5613. (cons "%m" (plist-get p :message-id)))))
  5614. (when (string-match "%c" fmt)
  5615. ;; Check if the user wrote this message
  5616. (if (and org-from-is-user-regexp from to
  5617. (save-match-data (string-match org-from-is-user-regexp from)))
  5618. (setq fmt (replace-match "to %t" t t fmt))
  5619. (setq fmt (replace-match "from %f" t t fmt))))
  5620. (org-replace-escapes fmt table)))
  5621. (defun org-make-org-heading-search-string (&optional string heading)
  5622. "Make search string for STRING or current headline."
  5623. (interactive)
  5624. (let ((s (or string (org-get-heading))))
  5625. (unless (and string (not heading))
  5626. ;; We are using a headline, clean up garbage in there.
  5627. (if (string-match org-todo-regexp s)
  5628. (setq s (replace-match "" t t s)))
  5629. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5630. (setq s (replace-match "" t t s)))
  5631. (setq s (org-trim s))
  5632. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5633. org-comment-string "\\)") s)
  5634. (setq s (replace-match "" t t s)))
  5635. (while (string-match org-ts-regexp s)
  5636. (setq s (replace-match "" t t s))))
  5637. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5638. (setq s (replace-match " " t t s)))
  5639. (or string (setq s (concat "*" s))) ; Add * for headlines
  5640. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5641. (defun org-make-link (&rest strings)
  5642. "Concatenate STRINGS."
  5643. (apply 'concat strings))
  5644. (defun org-make-link-string (link &optional description)
  5645. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5646. (unless (string-match "\\S-" link)
  5647. (error "Empty link"))
  5648. (when (stringp description)
  5649. ;; Remove brackets from the description, they are fatal.
  5650. (while (string-match "\\[" description)
  5651. (setq description (replace-match "{" t t description)))
  5652. (while (string-match "\\]" description)
  5653. (setq description (replace-match "}" t t description))))
  5654. (when (equal (org-link-escape link) description)
  5655. ;; No description needed, it is identical
  5656. (setq description nil))
  5657. (when (and (not description)
  5658. (not (equal link (org-link-escape link))))
  5659. (setq description (org-extract-attributes link)))
  5660. (concat "[[" (org-link-escape link) "]"
  5661. (if description (concat "[" description "]") "")
  5662. "]"))
  5663. (defconst org-link-escape-chars
  5664. '((?\ . "%20")
  5665. (?\[ . "%5B")
  5666. (?\] . "%5D")
  5667. (?\340 . "%E0") ; `a
  5668. (?\342 . "%E2") ; ^a
  5669. (?\347 . "%E7") ; ,c
  5670. (?\350 . "%E8") ; `e
  5671. (?\351 . "%E9") ; 'e
  5672. (?\352 . "%EA") ; ^e
  5673. (?\356 . "%EE") ; ^i
  5674. (?\364 . "%F4") ; ^o
  5675. (?\371 . "%F9") ; `u
  5676. (?\373 . "%FB") ; ^u
  5677. (?\; . "%3B")
  5678. (?? . "%3F")
  5679. (?= . "%3D")
  5680. (?+ . "%2B")
  5681. )
  5682. "Association list of escapes for some characters problematic in links.
  5683. This is the list that is used for internal purposes.")
  5684. (defconst org-link-escape-chars-browser
  5685. '((?\ . "%20")) ; 32 for the SPC char
  5686. "Association list of escapes for some characters problematic in links.
  5687. This is the list that is used before handing over to the browser.")
  5688. (defun org-link-escape (text &optional table)
  5689. "Escape charaters in TEXT that are problematic for links."
  5690. (setq table (or table org-link-escape-chars))
  5691. (when text
  5692. (let ((re (mapconcat (lambda (x) (regexp-quote
  5693. (char-to-string (car x))))
  5694. table "\\|")))
  5695. (while (string-match re text)
  5696. (setq text
  5697. (replace-match
  5698. (cdr (assoc (string-to-char (match-string 0 text))
  5699. table))
  5700. t t text)))
  5701. text)))
  5702. (defun org-link-unescape (text &optional table)
  5703. "Reverse the action of `org-link-escape'."
  5704. (setq table (or table org-link-escape-chars))
  5705. (when text
  5706. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5707. table "\\|")))
  5708. (while (string-match re text)
  5709. (setq text
  5710. (replace-match
  5711. (char-to-string (car (rassoc (match-string 0 text) table)))
  5712. t t text)))
  5713. text)))
  5714. (defun org-xor (a b)
  5715. "Exclusive or."
  5716. (if a (not b) b))
  5717. (defun org-get-header (header)
  5718. "Find a header field in the current buffer."
  5719. (save-excursion
  5720. (goto-char (point-min))
  5721. (let ((case-fold-search t) s)
  5722. (cond
  5723. ((eq header 'from)
  5724. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5725. (setq s (match-string 1)))
  5726. (while (string-match "\"" s)
  5727. (setq s (replace-match "" t t s)))
  5728. (if (string-match "[<(].*" s)
  5729. (setq s (replace-match "" t t s))))
  5730. ((eq header 'message-id)
  5731. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5732. (setq s (match-string 1))))
  5733. ((eq header 'subject)
  5734. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5735. (setq s (match-string 1)))))
  5736. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5737. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5738. s)))
  5739. (defun org-fixup-message-id-for-http (s)
  5740. "Replace special characters in a message id, so it can be used in an http query."
  5741. (while (string-match "<" s)
  5742. (setq s (replace-match "%3C" t t s)))
  5743. (while (string-match ">" s)
  5744. (setq s (replace-match "%3E" t t s)))
  5745. (while (string-match "@" s)
  5746. (setq s (replace-match "%40" t t s)))
  5747. s)
  5748. ;;;###autoload
  5749. (defun org-insert-link-global ()
  5750. "Insert a link like Org-mode does.
  5751. This command can be called in any mode to insert a link in Org-mode syntax."
  5752. (interactive)
  5753. (org-load-modules-maybe)
  5754. (org-run-like-in-org-mode 'org-insert-link))
  5755. (defun org-insert-link (&optional complete-file link-location)
  5756. "Insert a link. At the prompt, enter the link.
  5757. Completion can be used to insert any of the link protocol prefixes like
  5758. http or ftp in use.
  5759. The history can be used to select a link previously stored with
  5760. `org-store-link'. When the empty string is entered (i.e. if you just
  5761. press RET at the prompt), the link defaults to the most recently
  5762. stored link. As SPC triggers completion in the minibuffer, you need to
  5763. use M-SPC or C-q SPC to force the insertion of a space character.
  5764. You will also be prompted for a description, and if one is given, it will
  5765. be displayed in the buffer instead of the link.
  5766. If there is already a link at point, this command will allow you to edit link
  5767. and description parts.
  5768. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5769. be selected using completion. The path to the file will be relative to the
  5770. current directory if the file is in the current directory or a subdirectory.
  5771. Otherwise, the link will be the absolute path as completed in the minibuffer
  5772. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5773. option `org-link-file-path-type'.
  5774. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5775. the current directory or below.
  5776. With three \\[universal-argument] prefixes, negate the meaning of
  5777. `org-keep-stored-link-after-insertion'.
  5778. If `org-make-link-description-function' is non-nil, this function will be
  5779. called with the link target, and the result will be the default
  5780. link description.
  5781. If the LINK-LOCATION parameter is non-nil, this value will be
  5782. used as the link location instead of reading one interactively."
  5783. (interactive "P")
  5784. (let* ((wcf (current-window-configuration))
  5785. (region (if (org-region-active-p)
  5786. (buffer-substring (region-beginning) (region-end))))
  5787. (remove (and region (list (region-beginning) (region-end))))
  5788. (desc region)
  5789. tmphist ; byte-compile incorrectly complains about this
  5790. (link link-location)
  5791. entry file)
  5792. (cond
  5793. (link-location) ; specified by arg, just use it.
  5794. ((org-in-regexp org-bracket-link-regexp 1)
  5795. ;; We do have a link at point, and we are going to edit it.
  5796. (setq remove (list (match-beginning 0) (match-end 0)))
  5797. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5798. (setq link (read-string "Link: "
  5799. (org-link-unescape
  5800. (org-match-string-no-properties 1)))))
  5801. ((or (org-in-regexp org-angle-link-re)
  5802. (org-in-regexp org-plain-link-re))
  5803. ;; Convert to bracket link
  5804. (setq remove (list (match-beginning 0) (match-end 0))
  5805. link (read-string "Link: "
  5806. (org-remove-angle-brackets (match-string 0)))))
  5807. ((member complete-file '((4) (16)))
  5808. ;; Completing read for file names.
  5809. (setq file (read-file-name "File: "))
  5810. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5811. (pwd1 (file-name-as-directory (abbreviate-file-name
  5812. (expand-file-name ".")))))
  5813. (cond
  5814. ((equal complete-file '(16))
  5815. (setq link (org-make-link
  5816. "file:"
  5817. (abbreviate-file-name (expand-file-name file)))))
  5818. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5819. (setq link (org-make-link "file:" (match-string 1 file))))
  5820. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5821. (expand-file-name file))
  5822. (setq link (org-make-link
  5823. "file:" (match-string 1 (expand-file-name file)))))
  5824. (t (setq link (org-make-link "file:" file))))))
  5825. (t
  5826. ;; Read link, with completion for stored links.
  5827. (with-output-to-temp-buffer "*Org Links*"
  5828. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5829. (when org-stored-links
  5830. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5831. (princ (mapconcat
  5832. (lambda (x)
  5833. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5834. (reverse org-stored-links) "\n"))))
  5835. (let ((cw (selected-window)))
  5836. (select-window (get-buffer-window "*Org Links*"))
  5837. (org-fit-window-to-buffer)
  5838. (setq truncate-lines t)
  5839. (select-window cw))
  5840. ;; Fake a link history, containing the stored links.
  5841. (setq tmphist (append (mapcar 'car org-stored-links)
  5842. org-insert-link-history))
  5843. (unwind-protect
  5844. (setq link (org-completing-read
  5845. "Link: "
  5846. (append
  5847. (mapcar (lambda (x) (list (concat (car x) ":")))
  5848. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5849. (mapcar (lambda (x) (list (concat x ":")))
  5850. org-link-types))
  5851. nil nil nil
  5852. 'tmphist
  5853. (or (car (car org-stored-links)))))
  5854. (set-window-configuration wcf)
  5855. (kill-buffer "*Org Links*"))
  5856. (setq entry (assoc link org-stored-links))
  5857. (or entry (push link org-insert-link-history))
  5858. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5859. (not org-keep-stored-link-after-insertion))
  5860. (setq org-stored-links (delq (assoc link org-stored-links)
  5861. org-stored-links)))
  5862. (setq desc (or desc (nth 1 entry)))))
  5863. (if (string-match org-plain-link-re link)
  5864. ;; URL-like link, normalize the use of angular brackets.
  5865. (setq link (org-make-link (org-remove-angle-brackets link))))
  5866. ;; Check if we are linking to the current file with a search option
  5867. ;; If yes, simplify the link by using only the search option.
  5868. (when (and buffer-file-name
  5869. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5870. (let* ((path (match-string 1 link))
  5871. (case-fold-search nil)
  5872. (search (match-string 2 link)))
  5873. (save-match-data
  5874. (if (equal (file-truename buffer-file-name) (file-truename path))
  5875. ;; We are linking to this same file, with a search option
  5876. (setq link search)))))
  5877. ;; Check if we can/should use a relative path. If yes, simplify the link
  5878. (when (string-match "\\<file:\\(.*\\)" link)
  5879. (let* ((path (match-string 1 link))
  5880. (origpath path)
  5881. (case-fold-search nil))
  5882. (cond
  5883. ((or (eq org-link-file-path-type 'absolute)
  5884. (equal complete-file '(16)))
  5885. (setq path (abbreviate-file-name (expand-file-name path))))
  5886. ((eq org-link-file-path-type 'noabbrev)
  5887. (setq path (expand-file-name path)))
  5888. ((eq org-link-file-path-type 'relative)
  5889. (setq path (file-relative-name path)))
  5890. (t
  5891. (save-match-data
  5892. (if (string-match (concat "^" (regexp-quote
  5893. (file-name-as-directory
  5894. (expand-file-name "."))))
  5895. (expand-file-name path))
  5896. ;; We are linking a file with relative path name.
  5897. (setq path (substring (expand-file-name path)
  5898. (match-end 0)))
  5899. (setq path (abbreviate-file-name (expand-file-name path)))))))
  5900. (setq link (concat "file:" path))
  5901. (if (equal desc origpath)
  5902. (setq desc path))))
  5903. (if org-make-link-description-function
  5904. (setq desc (funcall org-make-link-description-function link desc)))
  5905. (setq desc (read-string "Description: " desc))
  5906. (unless (string-match "\\S-" desc) (setq desc nil))
  5907. (if remove (apply 'delete-region remove))
  5908. (insert (org-make-link-string link desc))))
  5909. (defun org-completing-read (&rest args)
  5910. "Completing-read with SPACE being a normal character."
  5911. (let ((minibuffer-local-completion-map
  5912. (copy-keymap minibuffer-local-completion-map)))
  5913. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5914. (apply 'completing-read args)))
  5915. (defun org-extract-attributes (s)
  5916. "Extract the attributes cookie from a string and set as text property."
  5917. (let (a attr (start 0) key value)
  5918. (save-match-data
  5919. (when (string-match "{{\\([^}]+\\)}}$" s)
  5920. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5921. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5922. (setq key (match-string 1 a) value (match-string 2 a)
  5923. start (match-end 0)
  5924. attr (plist-put attr (intern key) value))))
  5925. (org-add-props s nil 'org-attributes attr))
  5926. s))
  5927. (defun org-attributes-to-string (plist)
  5928. "Format a property list into an HTML attribute list."
  5929. (let ((s "") key value)
  5930. (while plist
  5931. (setq key (pop plist) value (pop plist))
  5932. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5933. s))
  5934. ;;; Opening/following a link
  5935. (defvar org-link-search-failed nil)
  5936. (defun org-next-link ()
  5937. "Move forward to the next link.
  5938. If the link is in hidden text, expose it."
  5939. (interactive)
  5940. (when (and org-link-search-failed (eq this-command last-command))
  5941. (goto-char (point-min))
  5942. (message "Link search wrapped back to beginning of buffer"))
  5943. (setq org-link-search-failed nil)
  5944. (let* ((pos (point))
  5945. (ct (org-context))
  5946. (a (assoc :link ct)))
  5947. (if a (goto-char (nth 2 a)))
  5948. (if (re-search-forward org-any-link-re nil t)
  5949. (progn
  5950. (goto-char (match-beginning 0))
  5951. (if (org-invisible-p) (org-show-context)))
  5952. (goto-char pos)
  5953. (setq org-link-search-failed t)
  5954. (error "No further link found"))))
  5955. (defun org-previous-link ()
  5956. "Move backward to the previous link.
  5957. If the link is in hidden text, expose it."
  5958. (interactive)
  5959. (when (and org-link-search-failed (eq this-command last-command))
  5960. (goto-char (point-max))
  5961. (message "Link search wrapped back to end of buffer"))
  5962. (setq org-link-search-failed nil)
  5963. (let* ((pos (point))
  5964. (ct (org-context))
  5965. (a (assoc :link ct)))
  5966. (if a (goto-char (nth 1 a)))
  5967. (if (re-search-backward org-any-link-re nil t)
  5968. (progn
  5969. (goto-char (match-beginning 0))
  5970. (if (org-invisible-p) (org-show-context)))
  5971. (goto-char pos)
  5972. (setq org-link-search-failed t)
  5973. (error "No further link found"))))
  5974. (defun org-translate-link (s)
  5975. "Translate a link string if a translation function has been defined."
  5976. (if (and org-link-translation-function
  5977. (fboundp org-link-translation-function)
  5978. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)"))
  5979. (progn
  5980. (setq s (funcall org-link-translation-function
  5981. (match-string 1) (match-string 2)))
  5982. (concat (car s) ":" (cdr s)))
  5983. s))
  5984. (defun org-translate-link-from-planner (type path)
  5985. "Translate a link from Emacs Planner syntax so that Org can follow it.
  5986. This is still an experimental function, your mileage may vary."
  5987. (cond
  5988. ((member type '("http" "https" "news" "ftp"))
  5989. ;; standard Internet links are the same.
  5990. nil)
  5991. ((and (equal type "irc") (string-match "^//" path))
  5992. ;; Planner has two / at the beginning of an irc link, we have 1.
  5993. ;; We should have zero, actually....
  5994. (setq path (substring path 1)))
  5995. ((and (equal type "lisp") (string-match "^/" path))
  5996. ;; Planner has a slash, we do not.
  5997. (setq type elisp path (substring path 1)))
  5998. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  5999. ;; A typical message link. Planner has the id after the fina slash,
  6000. ;; we separate it with a hash mark
  6001. (setq path (concat (match-string 1 path) "#"
  6002. (org-remove-angle-brackets (match-string 2 path)))))
  6003. )
  6004. (cons type path))
  6005. (defun org-find-file-at-mouse (ev)
  6006. "Open file link or URL at mouse."
  6007. (interactive "e")
  6008. (mouse-set-point ev)
  6009. (org-open-at-point 'in-emacs))
  6010. (defun org-open-at-mouse (ev)
  6011. "Open file link or URL at mouse."
  6012. (interactive "e")
  6013. (mouse-set-point ev)
  6014. (if (eq major-mode 'org-agenda-mode)
  6015. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6016. (org-open-at-point))
  6017. (defvar org-window-config-before-follow-link nil
  6018. "The window configuration before following a link.
  6019. This is saved in case the need arises to restore it.")
  6020. (defvar org-open-link-marker (make-marker)
  6021. "Marker pointing to the location where `org-open-at-point; was called.")
  6022. ;;;###autoload
  6023. (defun org-open-at-point-global ()
  6024. "Follow a link like Org-mode does.
  6025. This command can be called in any mode to follow a link that has
  6026. Org-mode syntax."
  6027. (interactive)
  6028. (org-run-like-in-org-mode 'org-open-at-point))
  6029. ;;;###autoload
  6030. (defun org-open-link-from-string (s &optional arg)
  6031. "Open a link in the string S, as if it was in Org-mode."
  6032. (interactive "sLink: \nP")
  6033. (with-temp-buffer
  6034. (let ((org-inhibit-startup t))
  6035. (org-mode)
  6036. (insert s)
  6037. (goto-char (point-min))
  6038. (org-open-at-point arg))))
  6039. (defun org-open-at-point (&optional in-emacs)
  6040. "Open link at or after point.
  6041. If there is no link at point, this function will search forward up to
  6042. the end of the current subtree.
  6043. Normally, files will be opened by an appropriate application. If the
  6044. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6045. With a double prefix argument, try to open outside of Emacs, in the
  6046. application the system uses for this file type."
  6047. (interactive "P")
  6048. (org-load-modules-maybe)
  6049. (move-marker org-open-link-marker (point))
  6050. (setq org-window-config-before-follow-link (current-window-configuration))
  6051. (org-remove-occur-highlights nil nil t)
  6052. (if (org-at-timestamp-p t)
  6053. (org-follow-timestamp-link)
  6054. (let (type path link line search (pos (point)))
  6055. (catch 'match
  6056. (save-excursion
  6057. (skip-chars-forward "^]\n\r")
  6058. (when (org-in-regexp org-bracket-link-regexp)
  6059. (setq link (org-extract-attributes
  6060. (org-link-unescape (org-match-string-no-properties 1))))
  6061. (while (string-match " *\n *" link)
  6062. (setq link (replace-match " " t t link)))
  6063. (setq link (org-link-expand-abbrev link))
  6064. (cond
  6065. ((or (file-name-absolute-p link)
  6066. (string-match "^\\.\\.?/" link))
  6067. (setq type "file" path link))
  6068. ((string-match org-link-re-with-space3 link)
  6069. (setq type (match-string 1 link) path (match-string 2 link)))
  6070. (t (setq type "thisfile" path link)))
  6071. (throw 'match t)))
  6072. (when (get-text-property (point) 'org-linked-text)
  6073. (setq type "thisfile"
  6074. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6075. (1+ (point)) (point))
  6076. path (buffer-substring
  6077. (previous-single-property-change pos 'org-linked-text)
  6078. (next-single-property-change pos 'org-linked-text)))
  6079. (throw 'match t))
  6080. (save-excursion
  6081. (when (or (org-in-regexp org-angle-link-re)
  6082. (org-in-regexp org-plain-link-re))
  6083. (setq type (match-string 1) path (match-string 2))
  6084. (throw 'match t)))
  6085. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6086. (setq type "tree-match"
  6087. path (match-string 1))
  6088. (throw 'match t))
  6089. (save-excursion
  6090. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6091. (setq type "tags"
  6092. path (match-string 1))
  6093. (while (string-match ":" path)
  6094. (setq path (replace-match "+" t t path)))
  6095. (throw 'match t))))
  6096. (unless path
  6097. (error "No link found"))
  6098. ;; Remove any trailing spaces in path
  6099. (if (string-match " +\\'" path)
  6100. (setq path (replace-match "" t t path)))
  6101. (if (and org-link-translation-function
  6102. (fboundp org-link-translation-function))
  6103. ;; Check if we need to translate the link
  6104. (let ((tmp (funcall org-link-translation-function type path)))
  6105. (setq type (car tmp) path (cdr tmp))))
  6106. (cond
  6107. ((assoc type org-link-protocols)
  6108. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6109. ((equal type "mailto")
  6110. (let ((cmd (car org-link-mailto-program))
  6111. (args (cdr org-link-mailto-program)) args1
  6112. (address path) (subject "") a)
  6113. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6114. (setq address (match-string 1 path)
  6115. subject (org-link-escape (match-string 2 path))))
  6116. (while args
  6117. (cond
  6118. ((not (stringp (car args))) (push (pop args) args1))
  6119. (t (setq a (pop args))
  6120. (if (string-match "%a" a)
  6121. (setq a (replace-match address t t a)))
  6122. (if (string-match "%s" a)
  6123. (setq a (replace-match subject t t a)))
  6124. (push a args1))))
  6125. (apply cmd (nreverse args1))))
  6126. ((member type '("http" "https" "ftp" "news"))
  6127. (browse-url (concat type ":" (org-link-escape
  6128. path org-link-escape-chars-browser))))
  6129. ((member type '("message"))
  6130. (browse-url (concat type ":" path)))
  6131. ((string= type "tags")
  6132. (org-tags-view in-emacs path))
  6133. ((string= type "thisfile")
  6134. (if in-emacs
  6135. (switch-to-buffer-other-window
  6136. (org-get-buffer-for-internal-link (current-buffer)))
  6137. (org-mark-ring-push))
  6138. (let ((cmd `(org-link-search
  6139. ,path
  6140. ,(cond ((equal in-emacs '(4)) 'occur)
  6141. ((equal in-emacs '(16)) 'org-occur)
  6142. (t nil))
  6143. ,pos)))
  6144. (condition-case nil (eval cmd)
  6145. (error (progn (widen) (eval cmd))))))
  6146. ((string= type "tree-match")
  6147. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6148. ((string= type "file")
  6149. (if (string-match "::\\([0-9]+\\)\\'" path)
  6150. (setq line (string-to-number (match-string 1 path))
  6151. path (substring path 0 (match-beginning 0)))
  6152. (if (string-match "::\\(.+\\)\\'" path)
  6153. (setq search (match-string 1 path)
  6154. path (substring path 0 (match-beginning 0)))))
  6155. (if (string-match "[*?{]" (file-name-nondirectory path))
  6156. (dired path)
  6157. (org-open-file path in-emacs line search)))
  6158. ((string= type "news")
  6159. (require 'org-gnus)
  6160. (org-gnus-follow-link path))
  6161. ((string= type "shell")
  6162. (let ((cmd path))
  6163. (if (or (not org-confirm-shell-link-function)
  6164. (funcall org-confirm-shell-link-function
  6165. (format "Execute \"%s\" in shell? "
  6166. (org-add-props cmd nil
  6167. 'face 'org-warning))))
  6168. (progn
  6169. (message "Executing %s" cmd)
  6170. (shell-command cmd))
  6171. (error "Abort"))))
  6172. ((string= type "elisp")
  6173. (let ((cmd path))
  6174. (if (or (not org-confirm-elisp-link-function)
  6175. (funcall org-confirm-elisp-link-function
  6176. (format "Execute \"%s\" as elisp? "
  6177. (org-add-props cmd nil
  6178. 'face 'org-warning))))
  6179. (message "%s => %s" cmd
  6180. (if (equal (string-to-char cmd) ?\()
  6181. (call-interactively (read cmd))
  6182. (eval (read cmd))))
  6183. (error "Abort"))))
  6184. (t
  6185. (browse-url-at-point)))))
  6186. (move-marker org-open-link-marker nil)
  6187. (run-hook-with-args 'org-follow-link-hook))
  6188. ;;;; Time estimates
  6189. (defun org-get-effort (&optional pom)
  6190. "Get the effort estimate for the current entry."
  6191. (org-entry-get pom org-effort-property))
  6192. ;;; File search
  6193. (defvar org-create-file-search-functions nil
  6194. "List of functions to construct the right search string for a file link.
  6195. These functions are called in turn with point at the location to
  6196. which the link should point.
  6197. A function in the hook should first test if it would like to
  6198. handle this file type, for example by checking the major-mode or
  6199. the file extension. If it decides not to handle this file, it
  6200. should just return nil to give other functions a chance. If it
  6201. does handle the file, it must return the search string to be used
  6202. when following the link. The search string will be part of the
  6203. file link, given after a double colon, and `org-open-at-point'
  6204. will automatically search for it. If special measures must be
  6205. taken to make the search successful, another function should be
  6206. added to the companion hook `org-execute-file-search-functions',
  6207. which see.
  6208. A function in this hook may also use `setq' to set the variable
  6209. `description' to provide a suggestion for the descriptive text to
  6210. be used for this link when it gets inserted into an Org-mode
  6211. buffer with \\[org-insert-link].")
  6212. (defvar org-execute-file-search-functions nil
  6213. "List of functions to execute a file search triggered by a link.
  6214. Functions added to this hook must accept a single argument, the
  6215. search string that was part of the file link, the part after the
  6216. double colon. The function must first check if it would like to
  6217. handle this search, for example by checking the major-mode or the
  6218. file extension. If it decides not to handle this search, it
  6219. should just return nil to give other functions a chance. If it
  6220. does handle the search, it must return a non-nil value to keep
  6221. other functions from trying.
  6222. Each function can access the current prefix argument through the
  6223. variable `current-prefix-argument'. Note that a single prefix is
  6224. used to force opening a link in Emacs, so it may be good to only
  6225. use a numeric or double prefix to guide the search function.
  6226. In case this is needed, a function in this hook can also restore
  6227. the window configuration before `org-open-at-point' was called using:
  6228. (set-window-configuration org-window-config-before-follow-link)")
  6229. (defun org-link-search (s &optional type avoid-pos)
  6230. "Search for a link search option.
  6231. If S is surrounded by forward slashes, it is interpreted as a
  6232. regular expression. In org-mode files, this will create an `org-occur'
  6233. sparse tree. In ordinary files, `occur' will be used to list matches.
  6234. If the current buffer is in `dired-mode', grep will be used to search
  6235. in all files. If AVOID-POS is given, ignore matches near that position."
  6236. (let ((case-fold-search t)
  6237. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6238. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6239. (append '(("") (" ") ("\t") ("\n"))
  6240. org-emphasis-alist)
  6241. "\\|") "\\)"))
  6242. (pos (point))
  6243. (pre nil) (post nil)
  6244. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6245. (cond
  6246. ;; First check if there are any special
  6247. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6248. ;; Now try the builtin stuff
  6249. ((save-excursion
  6250. (goto-char (point-min))
  6251. (and
  6252. (re-search-forward
  6253. (concat "<<" (regexp-quote s0) ">>") nil t)
  6254. (setq type 'dedicated
  6255. pos (match-beginning 0))))
  6256. ;; There is an exact target for this
  6257. (goto-char pos))
  6258. ((string-match "^/\\(.*\\)/$" s)
  6259. ;; A regular expression
  6260. (cond
  6261. ((org-mode-p)
  6262. (org-occur (match-string 1 s)))
  6263. ;;((eq major-mode 'dired-mode)
  6264. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6265. (t (org-do-occur (match-string 1 s)))))
  6266. (t
  6267. ;; A normal search strings
  6268. (when (equal (string-to-char s) ?*)
  6269. ;; Anchor on headlines, post may include tags.
  6270. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6271. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6272. s (substring s 1)))
  6273. (remove-text-properties
  6274. 0 (length s)
  6275. '(face nil mouse-face nil keymap nil fontified nil) s)
  6276. ;; Make a series of regular expressions to find a match
  6277. (setq words (org-split-string s "[ \n\r\t]+")
  6278. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6279. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6280. "\\)" markers)
  6281. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6282. re2a (concat "[ \t\r\n]" re2a_)
  6283. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6284. re4 (concat "[^a-zA-Z_]" re4_)
  6285. re1 (concat pre re2 post)
  6286. re3 (concat pre (if pre re4_ re4) post)
  6287. re5 (concat pre ".*" re4)
  6288. re2 (concat pre re2)
  6289. re2a (concat pre (if pre re2a_ re2a))
  6290. re4 (concat pre (if pre re4_ re4))
  6291. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6292. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6293. re5 "\\)"
  6294. ))
  6295. (cond
  6296. ((eq type 'org-occur) (org-occur reall))
  6297. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6298. (t (goto-char (point-min))
  6299. (setq type 'fuzzy)
  6300. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6301. (org-search-not-self 1 re1 nil t)
  6302. (org-search-not-self 1 re2 nil t)
  6303. (org-search-not-self 1 re2a nil t)
  6304. (org-search-not-self 1 re3 nil t)
  6305. (org-search-not-self 1 re4 nil t)
  6306. (org-search-not-self 1 re5 nil t)
  6307. )
  6308. (goto-char (match-beginning 1))
  6309. (goto-char pos)
  6310. (error "No match")))))
  6311. (t
  6312. ;; Normal string-search
  6313. (goto-char (point-min))
  6314. (if (search-forward s nil t)
  6315. (goto-char (match-beginning 0))
  6316. (error "No match"))))
  6317. (and (org-mode-p) (org-show-context 'link-search))
  6318. type))
  6319. (defun org-search-not-self (group &rest args)
  6320. "Execute `re-search-forward', but only accept matches that do not
  6321. enclose the position of `org-open-link-marker'."
  6322. (let ((m org-open-link-marker))
  6323. (catch 'exit
  6324. (while (apply 're-search-forward args)
  6325. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6326. (goto-char (match-end group))
  6327. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6328. (> (match-beginning 0) (marker-position m))
  6329. (< (match-end 0) (marker-position m)))
  6330. (save-match-data
  6331. (or (not (org-in-regexp
  6332. org-bracket-link-analytic-regexp 1))
  6333. (not (match-end 4)) ; no description
  6334. (and (<= (match-beginning 4) (point))
  6335. (>= (match-end 4) (point))))))
  6336. (throw 'exit (point))))))))
  6337. (defun org-get-buffer-for-internal-link (buffer)
  6338. "Return a buffer to be used for displaying the link target of internal links."
  6339. (cond
  6340. ((not org-display-internal-link-with-indirect-buffer)
  6341. buffer)
  6342. ((string-match "(Clone)$" (buffer-name buffer))
  6343. (message "Buffer is already a clone, not making another one")
  6344. ;; we also do not modify visibility in this case
  6345. buffer)
  6346. (t ; make a new indirect buffer for displaying the link
  6347. (let* ((bn (buffer-name buffer))
  6348. (ibn (concat bn "(Clone)"))
  6349. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6350. (with-current-buffer ib (org-overview))
  6351. ib))))
  6352. (defun org-do-occur (regexp &optional cleanup)
  6353. "Call the Emacs command `occur'.
  6354. If CLEANUP is non-nil, remove the printout of the regular expression
  6355. in the *Occur* buffer. This is useful if the regex is long and not useful
  6356. to read."
  6357. (occur regexp)
  6358. (when cleanup
  6359. (let ((cwin (selected-window)) win beg end)
  6360. (when (setq win (get-buffer-window "*Occur*"))
  6361. (select-window win))
  6362. (goto-char (point-min))
  6363. (when (re-search-forward "match[a-z]+" nil t)
  6364. (setq beg (match-end 0))
  6365. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6366. (setq end (1- (match-beginning 0)))))
  6367. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6368. (goto-char (point-min))
  6369. (select-window cwin))))
  6370. ;;; The mark ring for links jumps
  6371. (defvar org-mark-ring nil
  6372. "Mark ring for positions before jumps in Org-mode.")
  6373. (defvar org-mark-ring-last-goto nil
  6374. "Last position in the mark ring used to go back.")
  6375. ;; Fill and close the ring
  6376. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6377. (loop for i from 1 to org-mark-ring-length do
  6378. (push (make-marker) org-mark-ring))
  6379. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6380. org-mark-ring)
  6381. (defun org-mark-ring-push (&optional pos buffer)
  6382. "Put the current position or POS into the mark ring and rotate it."
  6383. (interactive)
  6384. (setq pos (or pos (point)))
  6385. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6386. (move-marker (car org-mark-ring)
  6387. (or pos (point))
  6388. (or buffer (current-buffer)))
  6389. (message "%s"
  6390. (substitute-command-keys
  6391. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6392. (defun org-mark-ring-goto (&optional n)
  6393. "Jump to the previous position in the mark ring.
  6394. With prefix arg N, jump back that many stored positions. When
  6395. called several times in succession, walk through the entire ring.
  6396. Org-mode commands jumping to a different position in the current file,
  6397. or to another Org-mode file, automatically push the old position
  6398. onto the ring."
  6399. (interactive "p")
  6400. (let (p m)
  6401. (if (eq last-command this-command)
  6402. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6403. (setq p org-mark-ring))
  6404. (setq org-mark-ring-last-goto p)
  6405. (setq m (car p))
  6406. (switch-to-buffer (marker-buffer m))
  6407. (goto-char m)
  6408. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6409. (defun org-remove-angle-brackets (s)
  6410. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6411. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6412. s)
  6413. (defun org-add-angle-brackets (s)
  6414. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6415. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6416. s)
  6417. (defun org-remove-double-quotes (s)
  6418. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6419. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6420. s)
  6421. ;;; Following specific links
  6422. (defun org-follow-timestamp-link ()
  6423. (cond
  6424. ((org-at-date-range-p t)
  6425. (let ((org-agenda-start-on-weekday)
  6426. (t1 (match-string 1))
  6427. (t2 (match-string 2)))
  6428. (setq t1 (time-to-days (org-time-string-to-time t1))
  6429. t2 (time-to-days (org-time-string-to-time t2)))
  6430. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6431. ((org-at-timestamp-p t)
  6432. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6433. (substring (match-string 1) 0 10)))
  6434. 1))
  6435. (t (error "This should not happen"))))
  6436. ;;; Following file links
  6437. (defvar org-wait nil)
  6438. (defun org-open-file (path &optional in-emacs line search)
  6439. "Open the file at PATH.
  6440. First, this expands any special file name abbreviations. Then the
  6441. configuration variable `org-file-apps' is checked if it contains an
  6442. entry for this file type, and if yes, the corresponding command is launched.
  6443. If no application is found, Emacs simply visits the file.
  6444. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6445. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6446. and o use an external application to visit the file.
  6447. Optional LINE specifies a line to go to, optional SEARCH a string to
  6448. search for. If LINE or SEARCH is given, the file will always be
  6449. opened in Emacs.
  6450. If the file does not exist, an error is thrown."
  6451. (setq in-emacs (or in-emacs line search))
  6452. (let* ((file (if (equal path "")
  6453. buffer-file-name
  6454. (substitute-in-file-name (expand-file-name path))))
  6455. (apps (append org-file-apps (org-default-apps)))
  6456. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6457. (dirp (if remp nil (file-directory-p file)))
  6458. (file (if (and dirp org-open-directory-means-index-dot-org)
  6459. (concat (file-name-as-directory file) "index.org")
  6460. file))
  6461. (a-m-a-p (assq 'auto-mode apps))
  6462. (dfile (downcase file))
  6463. (old-buffer (current-buffer))
  6464. (old-pos (point))
  6465. (old-mode major-mode)
  6466. ext cmd)
  6467. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6468. (setq ext (match-string 1 dfile))
  6469. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6470. (setq ext (match-string 1 dfile))))
  6471. (cond
  6472. ((equal in-emacs '(16))
  6473. (setq cmd (cdr (assoc 'system apps))))
  6474. (in-emacs (setq cmd 'emacs))
  6475. (t
  6476. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6477. (and dirp (cdr (assoc 'directory apps)))
  6478. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6479. 'string-match)
  6480. (cdr (assoc ext apps))
  6481. (cdr (assoc t apps))))))
  6482. (when (eq cmd 'system)
  6483. (setq cmd (cdr (assoc 'system apps))))
  6484. (when (eq cmd 'default)
  6485. (setq cmd (cdr (assoc t apps))))
  6486. (when (eq cmd 'mailcap)
  6487. (require 'mailcap)
  6488. (mailcap-parse-mailcaps)
  6489. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6490. (command (mailcap-mime-info mime-type)))
  6491. (if (stringp command)
  6492. (setq cmd command)
  6493. (setq cmd 'emacs))))
  6494. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6495. (not (file-exists-p file))
  6496. (not org-open-non-existing-files))
  6497. (error "No such file: %s" file))
  6498. (cond
  6499. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6500. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6501. (while (string-match "['\"]%s['\"]" cmd)
  6502. (setq cmd (replace-match "%s" t t cmd)))
  6503. (while (string-match "%s" cmd)
  6504. (setq cmd (replace-match
  6505. (save-match-data
  6506. (shell-quote-argument
  6507. (convert-standard-filename file)))
  6508. t t cmd)))
  6509. (save-window-excursion
  6510. (start-process-shell-command cmd nil cmd)
  6511. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6512. ))
  6513. ((or (stringp cmd)
  6514. (eq cmd 'emacs))
  6515. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6516. (widen)
  6517. (if line (goto-line line)
  6518. (if search (org-link-search search))))
  6519. ((consp cmd)
  6520. (let ((file (convert-standard-filename file)))
  6521. (eval cmd)))
  6522. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6523. (and (org-mode-p) (eq old-mode 'org-mode)
  6524. (or (not (equal old-buffer (current-buffer)))
  6525. (not (equal old-pos (point))))
  6526. (org-mark-ring-push old-pos old-buffer))))
  6527. (defun org-default-apps ()
  6528. "Return the default applications for this operating system."
  6529. (cond
  6530. ((eq system-type 'darwin)
  6531. org-file-apps-defaults-macosx)
  6532. ((eq system-type 'windows-nt)
  6533. org-file-apps-defaults-windowsnt)
  6534. (t org-file-apps-defaults-gnu)))
  6535. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6536. "Convert extensions to regular expressions in the cars of LIST.
  6537. Also, weed out any non-string entries, because the return value is used
  6538. only for regexp matching.
  6539. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6540. point to the symbol `emacs', indicating that the file should
  6541. be opened in Emacs."
  6542. (append
  6543. (delq nil
  6544. (mapcar (lambda (x)
  6545. (if (not (stringp (car x)))
  6546. nil
  6547. (if (string-match "\\W" (car x))
  6548. x
  6549. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6550. list))
  6551. (if add-auto-mode
  6552. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6553. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6554. (defun org-file-remote-p (file)
  6555. "Test whether FILE specifies a location on a remote system.
  6556. Return non-nil if the location is indeed remote.
  6557. For example, the filename \"/user@host:/foo\" specifies a location
  6558. on the system \"/user@host:\"."
  6559. (cond ((fboundp 'file-remote-p)
  6560. (file-remote-p file))
  6561. ((fboundp 'tramp-handle-file-remote-p)
  6562. (tramp-handle-file-remote-p file))
  6563. ((and (boundp 'ange-ftp-name-format)
  6564. (string-match (car ange-ftp-name-format) file))
  6565. t)
  6566. (t nil)))
  6567. ;;;; Refiling
  6568. (defun org-get-org-file ()
  6569. "Read a filename, with default directory `org-directory'."
  6570. (let ((default (or org-default-notes-file remember-data-file)))
  6571. (read-file-name (format "File name [%s]: " default)
  6572. (file-name-as-directory org-directory)
  6573. default)))
  6574. (defun org-notes-order-reversed-p ()
  6575. "Check if the current file should receive notes in reversed order."
  6576. (cond
  6577. ((not org-reverse-note-order) nil)
  6578. ((eq t org-reverse-note-order) t)
  6579. ((not (listp org-reverse-note-order)) nil)
  6580. (t (catch 'exit
  6581. (let ((all org-reverse-note-order)
  6582. entry)
  6583. (while (setq entry (pop all))
  6584. (if (string-match (car entry) buffer-file-name)
  6585. (throw 'exit (cdr entry))))
  6586. nil)))))
  6587. (defvar org-refile-target-table nil
  6588. "The list of refile targets, created by `org-refile'.")
  6589. (defvar org-agenda-new-buffers nil
  6590. "Buffers created to visit agenda files.")
  6591. (defun org-get-refile-targets (&optional default-buffer)
  6592. "Produce a table with refile targets."
  6593. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6594. targets txt re files f desc descre)
  6595. (with-current-buffer (or default-buffer (current-buffer))
  6596. (while (setq entry (pop entries))
  6597. (setq files (car entry) desc (cdr entry))
  6598. (cond
  6599. ((null files) (setq files (list (current-buffer))))
  6600. ((eq files 'org-agenda-files)
  6601. (setq files (org-agenda-files 'unrestricted)))
  6602. ((and (symbolp files) (fboundp files))
  6603. (setq files (funcall files)))
  6604. ((and (symbolp files) (boundp files))
  6605. (setq files (symbol-value files))))
  6606. (if (stringp files) (setq files (list files)))
  6607. (cond
  6608. ((eq (car desc) :tag)
  6609. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6610. ((eq (car desc) :todo)
  6611. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6612. ((eq (car desc) :regexp)
  6613. (setq descre (cdr desc)))
  6614. ((eq (car desc) :level)
  6615. (setq descre (concat "^\\*\\{" (number-to-string
  6616. (if org-odd-levels-only
  6617. (1- (* 2 (cdr desc)))
  6618. (cdr desc)))
  6619. "\\}[ \t]")))
  6620. ((eq (car desc) :maxlevel)
  6621. (setq descre (concat "^\\*\\{1," (number-to-string
  6622. (if org-odd-levels-only
  6623. (1- (* 2 (cdr desc)))
  6624. (cdr desc)))
  6625. "\\}[ \t]")))
  6626. (t (error "Bad refiling target description %s" desc)))
  6627. (while (setq f (pop files))
  6628. (save-excursion
  6629. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6630. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6631. (save-excursion
  6632. (save-restriction
  6633. (widen)
  6634. (goto-char (point-min))
  6635. (while (re-search-forward descre nil t)
  6636. (goto-char (point-at-bol))
  6637. (when (looking-at org-complex-heading-regexp)
  6638. (setq txt (org-link-display-format (match-string 4))
  6639. re (concat "^" (regexp-quote
  6640. (buffer-substring (match-beginning 1)
  6641. (match-end 4)))))
  6642. (if (match-end 5) (setq re (concat re "[ \t]+"
  6643. (regexp-quote
  6644. (match-string 5)))))
  6645. (setq re (concat re "[ \t]*$"))
  6646. (when org-refile-use-outline-path
  6647. (setq txt (mapconcat 'org-protect-slash
  6648. (append
  6649. (if (eq org-refile-use-outline-path 'file)
  6650. (list (file-name-nondirectory
  6651. (buffer-file-name (buffer-base-buffer))))
  6652. (if (eq org-refile-use-outline-path 'full-file-path)
  6653. (list (buffer-file-name (buffer-base-buffer)))))
  6654. (org-get-outline-path)
  6655. (list txt))
  6656. "/")))
  6657. (push (list txt f re (point)) targets))
  6658. (goto-char (point-at-eol))))))))
  6659. (nreverse targets))))
  6660. (defun org-protect-slash (s)
  6661. (while (string-match "/" s)
  6662. (setq s (replace-match "\\" t t s)))
  6663. s)
  6664. (defun org-get-outline-path ()
  6665. "Return the outline path to the current entry, as a list."
  6666. (let (rtn)
  6667. (save-excursion
  6668. (while (org-up-heading-safe)
  6669. (when (looking-at org-complex-heading-regexp)
  6670. (push (org-match-string-no-properties 4) rtn)))
  6671. rtn)))
  6672. (defvar org-refile-history nil
  6673. "History for refiling operations.")
  6674. (defun org-refile (&optional goto default-buffer)
  6675. "Move the entry at point to another heading.
  6676. The list of target headings is compiled using the information in
  6677. `org-refile-targets', which see. This list is created before each use
  6678. and will therefore always be up-to-date.
  6679. At the target location, the entry is filed as a subitem of the target heading.
  6680. Depending on `org-reverse-note-order', the new subitem will either be the
  6681. first or the last subitem.
  6682. If there is an active region, all entries in that region will be moved.
  6683. However, the region must fulfil the requirement that the first heading
  6684. is the first one sets the top-level of the moved text - at most siblings
  6685. below it are allowed.
  6686. With prefix arg GOTO, the command will only visit the target location,
  6687. not actually move anything.
  6688. With a double prefix `C-u C-u', go to the location where the last refiling
  6689. operation has put the subtree."
  6690. (interactive "P")
  6691. (let* ((cbuf (current-buffer))
  6692. (regionp (org-region-active-p))
  6693. (region-start (and regionp (region-beginning)))
  6694. (region-end (and regionp (region-end)))
  6695. (region-length (and regionp (- region-end region-start)))
  6696. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6697. pos it nbuf file re level reversed)
  6698. (when regionp (goto-char region-start)
  6699. (unless (org-kill-is-subtree-p
  6700. (buffer-substring region-start region-end))
  6701. (error "The region is not a (sequence of) subtree(s)")))
  6702. (if (equal goto '(16))
  6703. (org-refile-goto-last-stored)
  6704. (when (setq it (org-refile-get-location
  6705. (if goto "Goto: " "Refile to: ") default-buffer))
  6706. (setq file (nth 1 it)
  6707. re (nth 2 it)
  6708. pos (nth 3 it))
  6709. (setq nbuf (or (find-buffer-visiting file)
  6710. (find-file-noselect file)))
  6711. (if goto
  6712. (progn
  6713. (switch-to-buffer nbuf)
  6714. (goto-char pos)
  6715. (org-show-context 'org-goto))
  6716. (if regionp
  6717. (progn
  6718. (kill-new (buffer-substring region-start region-end))
  6719. (org-save-markers-in-region region-start region-end))
  6720. (org-copy-subtree 1 nil t))
  6721. (save-excursion
  6722. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6723. (find-file-noselect file))))
  6724. (setq reversed (org-notes-order-reversed-p))
  6725. (save-excursion
  6726. (save-restriction
  6727. (widen)
  6728. (goto-char pos)
  6729. (looking-at outline-regexp)
  6730. (setq level (org-get-valid-level (funcall outline-level) 1))
  6731. (goto-char
  6732. (if reversed
  6733. (or (outline-next-heading) (point-max))
  6734. (or (save-excursion (outline-get-next-sibling))
  6735. (org-end-of-subtree t t)
  6736. (point-max))))
  6737. (if (not (bolp)) (newline))
  6738. (bookmark-set "org-refile-last-stored")
  6739. (org-paste-subtree level))))
  6740. (if regionp
  6741. (delete-region (point) (+ (point) region-length))
  6742. (org-cut-subtree))
  6743. (setq org-markers-to-move nil)
  6744. (message "Refiled to \"%s\"" (car it)))))))
  6745. (defun org-refile-goto-last-stored ()
  6746. "Go to the location where the last refile was stored."
  6747. (interactive)
  6748. (bookmark-jump "org-refile-last-stored")
  6749. (message "This is the location of the last refile"))
  6750. (defun org-refile-get-location (&optional prompt default-buffer)
  6751. "Prompt the user for a refile location, using PROMPT."
  6752. (let ((org-refile-targets org-refile-targets)
  6753. (org-refile-use-outline-path org-refile-use-outline-path))
  6754. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6755. (unless org-refile-target-table
  6756. (error "No refile targets"))
  6757. (let* ((cbuf (current-buffer))
  6758. (cfunc (if org-refile-use-outline-path
  6759. 'org-olpath-completing-read
  6760. 'completing-read))
  6761. (extra (if org-refile-use-outline-path "/" ""))
  6762. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6763. (fname (and filename (file-truename filename)))
  6764. (tbl (mapcar
  6765. (lambda (x)
  6766. (if (not (equal fname (file-truename (nth 1 x))))
  6767. (cons (concat (car x) extra " ("
  6768. (file-name-nondirectory (nth 1 x)) ")")
  6769. (cdr x))
  6770. (cons (concat (car x) extra) (cdr x))))
  6771. org-refile-target-table))
  6772. (completion-ignore-case t))
  6773. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6774. tbl)))
  6775. (defun org-olpath-completing-read (prompt collection &rest args)
  6776. "Read an outline path like a file name."
  6777. (let ((thetable collection))
  6778. (apply
  6779. 'completing-read prompt
  6780. (lambda (string predicate &optional flag)
  6781. (let (rtn r s f (l (length string)))
  6782. (cond
  6783. ((eq flag nil)
  6784. ;; try completion
  6785. (try-completion string thetable))
  6786. ((eq flag t)
  6787. ;; all-completions
  6788. (setq rtn (all-completions string thetable predicate))
  6789. (mapcar
  6790. (lambda (x)
  6791. (setq r (substring x l))
  6792. (if (string-match " ([^)]*)$" x)
  6793. (setq f (match-string 0 x))
  6794. (setq f ""))
  6795. (if (string-match "/" r)
  6796. (concat string (substring r 0 (match-end 0)) f)
  6797. x))
  6798. rtn))
  6799. ((eq flag 'lambda)
  6800. ;; exact match?
  6801. (assoc string thetable)))
  6802. ))
  6803. args)))
  6804. ;;;; Dynamic blocks
  6805. (defun org-find-dblock (name)
  6806. "Find the first dynamic block with name NAME in the buffer.
  6807. If not found, stay at current position and return nil."
  6808. (let (pos)
  6809. (save-excursion
  6810. (goto-char (point-min))
  6811. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6812. nil t)
  6813. (match-beginning 0))))
  6814. (if pos (goto-char pos))
  6815. pos))
  6816. (defconst org-dblock-start-re
  6817. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6818. "Matches the startline of a dynamic block, with parameters.")
  6819. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6820. "Matches the end of a dyhamic block.")
  6821. (defun org-create-dblock (plist)
  6822. "Create a dynamic block section, with parameters taken from PLIST.
  6823. PLIST must containe a :name entry which is used as name of the block."
  6824. (unless (bolp) (newline))
  6825. (let ((name (plist-get plist :name)))
  6826. (insert "#+BEGIN: " name)
  6827. (while plist
  6828. (if (eq (car plist) :name)
  6829. (setq plist (cddr plist))
  6830. (insert " " (prin1-to-string (pop plist)))))
  6831. (insert "\n\n#+END:\n")
  6832. (beginning-of-line -2)))
  6833. (defun org-prepare-dblock ()
  6834. "Prepare dynamic block for refresh.
  6835. This empties the block, puts the cursor at the insert position and returns
  6836. the property list including an extra property :name with the block name."
  6837. (unless (looking-at org-dblock-start-re)
  6838. (error "Not at a dynamic block"))
  6839. (let* ((begdel (1+ (match-end 0)))
  6840. (name (org-no-properties (match-string 1)))
  6841. (params (append (list :name name)
  6842. (read (concat "(" (match-string 3) ")")))))
  6843. (unless (re-search-forward org-dblock-end-re nil t)
  6844. (error "Dynamic block not terminated"))
  6845. (setq params
  6846. (append params
  6847. (list :content (buffer-substring
  6848. begdel (match-beginning 0)))))
  6849. (delete-region begdel (match-beginning 0))
  6850. (goto-char begdel)
  6851. (open-line 1)
  6852. params))
  6853. (defun org-map-dblocks (&optional command)
  6854. "Apply COMMAND to all dynamic blocks in the current buffer.
  6855. If COMMAND is not given, use `org-update-dblock'."
  6856. (let ((cmd (or command 'org-update-dblock))
  6857. pos)
  6858. (save-excursion
  6859. (goto-char (point-min))
  6860. (while (re-search-forward org-dblock-start-re nil t)
  6861. (goto-char (setq pos (match-beginning 0)))
  6862. (condition-case nil
  6863. (funcall cmd)
  6864. (error (message "Error during update of dynamic block")))
  6865. (goto-char pos)
  6866. (unless (re-search-forward org-dblock-end-re nil t)
  6867. (error "Dynamic block not terminated"))))))
  6868. (defun org-dblock-update (&optional arg)
  6869. "User command for updating dynamic blocks.
  6870. Update the dynamic block at point. With prefix ARG, update all dynamic
  6871. blocks in the buffer."
  6872. (interactive "P")
  6873. (if arg
  6874. (org-update-all-dblocks)
  6875. (or (looking-at org-dblock-start-re)
  6876. (org-beginning-of-dblock))
  6877. (org-update-dblock)))
  6878. (defun org-update-dblock ()
  6879. "Update the dynamic block at point
  6880. This means to empty the block, parse for parameters and then call
  6881. the correct writing function."
  6882. (save-window-excursion
  6883. (let* ((pos (point))
  6884. (line (org-current-line))
  6885. (params (org-prepare-dblock))
  6886. (name (plist-get params :name))
  6887. (cmd (intern (concat "org-dblock-write:" name))))
  6888. (message "Updating dynamic block `%s' at line %d..." name line)
  6889. (funcall cmd params)
  6890. (message "Updating dynamic block `%s' at line %d...done" name line)
  6891. (goto-char pos))))
  6892. (defun org-beginning-of-dblock ()
  6893. "Find the beginning of the dynamic block at point.
  6894. Error if there is no scuh block at point."
  6895. (let ((pos (point))
  6896. beg)
  6897. (end-of-line 1)
  6898. (if (and (re-search-backward org-dblock-start-re nil t)
  6899. (setq beg (match-beginning 0))
  6900. (re-search-forward org-dblock-end-re nil t)
  6901. (> (match-end 0) pos))
  6902. (goto-char beg)
  6903. (goto-char pos)
  6904. (error "Not in a dynamic block"))))
  6905. (defun org-update-all-dblocks ()
  6906. "Update all dynamic blocks in the buffer.
  6907. This function can be used in a hook."
  6908. (when (org-mode-p)
  6909. (org-map-dblocks 'org-update-dblock)))
  6910. ;;;; Completion
  6911. (defconst org-additional-option-like-keywords
  6912. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6913. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6914. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6915. "BEGIN_QUOTE" "END_QUOTE"
  6916. "BEGIN_VERSE" "END_VERSE"
  6917. "BEGIN_SRC" "END_SRC"))
  6918. (defcustom org-structure-template-alist
  6919. '(
  6920. ("s" "#+begin_src ?\n\n#+end_src"
  6921. "<src lang=\"?\">\n\n</src>")
  6922. ("e" "#+begin_example\n?\n#+end_example"
  6923. "<example>\n?\n</example>")
  6924. ("q" "#+begin_quote\n?\n#+end_quote"
  6925. "<quote>\n?\n</quote>")
  6926. ("v" "#+begin_verse\n?\n#+end_verse"
  6927. "<verse>\n?\n/verse>")
  6928. ("l" "#+begin_latex\n?\n#+end_latex"
  6929. "<literal style=\"latex\">\n?\n</literal>")
  6930. ("L" "#+latex: "
  6931. "<literal style=\"latex\">?</literal>")
  6932. ("h" "#+begin_html\n?\n#+end_html"
  6933. "<literal style=\"html\">\n?\n</literal>")
  6934. ("H" "#+html: "
  6935. "<literal style=\"html\">?</literal>")
  6936. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6937. ("A" "#+ascii: ")
  6938. ("i" "#+include %file ?"
  6939. "<include file=%file markup=\"?\">")
  6940. )
  6941. "Structure completion elements.
  6942. This is a list of abbreviation keys and values. The value gets inserted
  6943. it you type @samp{.} followed by the key and then the completion key,
  6944. usually `M-TAB'. %file will be replaced by a file name after prompting
  6945. for the file uning completion.
  6946. There are two templates for each key, the first uses the original Org syntax,
  6947. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6948. the default when the /org-mtags.el/ module has been loaded. See also the
  6949. variable `org-mtags-prefere-muse-templates'.
  6950. This is an experimental feature, it is undecided if it is going to stay in."
  6951. :group 'org-completion
  6952. :type '(repeat
  6953. (string :tag "Key")
  6954. (string :tag "Template")
  6955. (string :tag "Muse Template")))
  6956. (defun org-try-structure-completion ()
  6957. "Try to complete a structure template before point.
  6958. This looks for strings like \"<e\" on an otherwise empty line and
  6959. expands them."
  6960. (let ((l (buffer-substring (point-at-bol) (point)))
  6961. a)
  6962. (when (and (looking-at "[ \t]*$")
  6963. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  6964. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  6965. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  6966. (match-beginning 1)) a)
  6967. t)))
  6968. (defun org-complete-expand-structure-template (start cell)
  6969. "Expand a structure template."
  6970. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  6971. (rpl (nth (if musep 2 1) cell)))
  6972. (delete-region start (point))
  6973. (when (string-match "\\`#\\+" rpl)
  6974. (cond
  6975. ((bolp))
  6976. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6977. (delete-region (point-at-bol) (point)))
  6978. (t (newline))))
  6979. (setq start (point))
  6980. (if (string-match "%file" rpl)
  6981. (setq rpl (replace-match
  6982. (concat
  6983. "\""
  6984. (save-match-data
  6985. (abbreviate-file-name (read-file-name "Include file: ")))
  6986. "\"")
  6987. t t rpl)))
  6988. (insert rpl)
  6989. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6990. (defun org-complete (&optional arg)
  6991. "Perform completion on word at point.
  6992. At the beginning of a headline, this completes TODO keywords as given in
  6993. `org-todo-keywords'.
  6994. If the current word is preceded by a backslash, completes the TeX symbols
  6995. that are supported for HTML support.
  6996. If the current word is preceded by \"#+\", completes special words for
  6997. setting file options.
  6998. In the line after \"#+STARTUP:, complete valid keywords.\"
  6999. At all other locations, this simply calls the value of
  7000. `org-completion-fallback-command'."
  7001. (interactive "P")
  7002. (org-without-partial-completion
  7003. (catch 'exit
  7004. (let* ((a nil)
  7005. (end (point))
  7006. (beg1 (save-excursion
  7007. (skip-chars-backward (org-re "[:alnum:]_@"))
  7008. (point)))
  7009. (beg (save-excursion
  7010. (skip-chars-backward "a-zA-Z0-9_:$")
  7011. (point)))
  7012. (confirm (lambda (x) (stringp (car x))))
  7013. (searchhead (equal (char-before beg) ?*))
  7014. (struct
  7015. (when (and (member (char-before beg1) '(?. ?<))
  7016. (setq a (assoc (buffer-substring beg1 (point))
  7017. org-structure-template-alist)))
  7018. (org-complete-expand-structure-template (1- beg1) a)
  7019. (throw 'exit t)))
  7020. (tag (and (equal (char-before beg1) ?:)
  7021. (equal (char-after (point-at-bol)) ?*)))
  7022. (prop (and (equal (char-before beg1) ?:)
  7023. (not (equal (char-after (point-at-bol)) ?*))))
  7024. (texp (equal (char-before beg) ?\\))
  7025. (link (equal (char-before beg) ?\[))
  7026. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7027. beg)
  7028. "#+"))
  7029. (startup (string-match "^#\\+STARTUP:.*"
  7030. (buffer-substring (point-at-bol) (point))))
  7031. (completion-ignore-case opt)
  7032. (type nil)
  7033. (tbl nil)
  7034. (table (cond
  7035. (opt
  7036. (setq type :opt)
  7037. (require 'org-exp)
  7038. (append
  7039. (mapcar
  7040. (lambda (x)
  7041. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7042. (cons (match-string 2 x) (match-string 1 x)))
  7043. (org-split-string (org-get-current-options) "\n"))
  7044. (mapcar 'list org-additional-option-like-keywords)))
  7045. (startup
  7046. (setq type :startup)
  7047. org-startup-options)
  7048. (link (append org-link-abbrev-alist-local
  7049. org-link-abbrev-alist))
  7050. (texp
  7051. (setq type :tex)
  7052. org-html-entities)
  7053. ((string-match "\\`\\*+[ \t]+\\'"
  7054. (buffer-substring (point-at-bol) beg))
  7055. (setq type :todo)
  7056. (mapcar 'list org-todo-keywords-1))
  7057. (searchhead
  7058. (setq type :searchhead)
  7059. (save-excursion
  7060. (goto-char (point-min))
  7061. (while (re-search-forward org-todo-line-regexp nil t)
  7062. (push (list
  7063. (org-make-org-heading-search-string
  7064. (match-string 3) t))
  7065. tbl)))
  7066. tbl)
  7067. (tag (setq type :tag beg beg1)
  7068. (or org-tag-alist (org-get-buffer-tags)))
  7069. (prop (setq type :prop beg beg1)
  7070. (mapcar 'list (org-buffer-property-keys nil t t)))
  7071. (t (progn
  7072. (call-interactively org-completion-fallback-command)
  7073. (throw 'exit nil)))))
  7074. (pattern (buffer-substring-no-properties beg end))
  7075. (completion (try-completion pattern table confirm)))
  7076. (cond ((eq completion t)
  7077. (if (not (assoc (upcase pattern) table))
  7078. (message "Already complete")
  7079. (if (and (equal type :opt)
  7080. (not (member (car (assoc (upcase pattern) table))
  7081. org-additional-option-like-keywords)))
  7082. (insert (substring (cdr (assoc (upcase pattern) table))
  7083. (length pattern)))
  7084. (if (memq type '(:tag :prop)) (insert ":")))))
  7085. ((null completion)
  7086. (message "Can't find completion for \"%s\"" pattern)
  7087. (ding))
  7088. ((not (string= pattern completion))
  7089. (delete-region beg end)
  7090. (if (string-match " +$" completion)
  7091. (setq completion (replace-match "" t t completion)))
  7092. (insert completion)
  7093. (if (get-buffer-window "*Completions*")
  7094. (delete-window (get-buffer-window "*Completions*")))
  7095. (if (assoc completion table)
  7096. (if (eq type :todo) (insert " ")
  7097. (if (memq type '(:tag :prop)) (insert ":"))))
  7098. (if (and (equal type :opt) (assoc completion table))
  7099. (message "%s" (substitute-command-keys
  7100. "Press \\[org-complete] again to insert example settings"))))
  7101. (t
  7102. (message "Making completion list...")
  7103. (let ((list (sort (all-completions pattern table confirm)
  7104. 'string<)))
  7105. (with-output-to-temp-buffer "*Completions*"
  7106. (condition-case nil
  7107. ;; Protection needed for XEmacs and emacs 21
  7108. (display-completion-list list pattern)
  7109. (error (display-completion-list list)))))
  7110. (message "Making completion list...%s" "done")))))))
  7111. ;;;; TODO, DEADLINE, Comments
  7112. (defun org-toggle-comment ()
  7113. "Change the COMMENT state of an entry."
  7114. (interactive)
  7115. (save-excursion
  7116. (org-back-to-heading)
  7117. (let (case-fold-search)
  7118. (if (looking-at (concat outline-regexp
  7119. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7120. (replace-match "" t t nil 1)
  7121. (if (looking-at outline-regexp)
  7122. (progn
  7123. (goto-char (match-end 0))
  7124. (insert org-comment-string " ")))))))
  7125. (defvar org-last-todo-state-is-todo nil
  7126. "This is non-nil when the last TODO state change led to a TODO state.
  7127. If the last change removed the TODO tag or switched to DONE, then
  7128. this is nil.")
  7129. (defvar org-setting-tags nil) ; dynamically skiped
  7130. (defun org-parse-local-options (string var)
  7131. "Parse STRING for startup setting relevant for variable VAR."
  7132. (let ((rtn (symbol-value var))
  7133. e opts)
  7134. (save-match-data
  7135. (if (or (not string) (not (string-match "\\S-" string)))
  7136. rtn
  7137. (setq opts (delq nil (mapcar (lambda (x)
  7138. (setq e (assoc x org-startup-options))
  7139. (if (eq (nth 1 e) var) e nil))
  7140. (org-split-string string "[ \t]+"))))
  7141. (if (not opts)
  7142. rtn
  7143. (setq rtn nil)
  7144. (while (setq e (pop opts))
  7145. (if (not (nth 3 e))
  7146. (setq rtn (nth 2 e))
  7147. (if (not (listp rtn)) (setq rtn nil))
  7148. (push (nth 2 e) rtn)))
  7149. rtn)))))
  7150. (defvar org-blocker-hook nil
  7151. "Hook for functions that are allowed to block a state change.
  7152. Each function gets as its single argument a property list, see
  7153. `org-trigger-hook' for more information about this list.
  7154. If any of the functions in this hook returns nil, the state change
  7155. is blocked.")
  7156. (defvar org-trigger-hook nil
  7157. "Hook for functions that are triggered by a state change.
  7158. Each function gets as its single argument a property list with at least
  7159. the following elements:
  7160. (:type type-of-change :position pos-at-entry-start
  7161. :from old-state :to new-state)
  7162. Depending on the type, more properties may be present.
  7163. This mechanism is currently implemented for:
  7164. TODO state changes
  7165. ------------------
  7166. :type todo-state-change
  7167. :from previous state (keyword as a string), or nil
  7168. :to new state (keyword as a string), or nil")
  7169. (defvar org-agenda-headline-snapshot-before-repeat)
  7170. (defun org-todo (&optional arg)
  7171. "Change the TODO state of an item.
  7172. The state of an item is given by a keyword at the start of the heading,
  7173. like
  7174. *** TODO Write paper
  7175. *** DONE Call mom
  7176. The different keywords are specified in the variable `org-todo-keywords'.
  7177. By default the available states are \"TODO\" and \"DONE\".
  7178. So for this example: when the item starts with TODO, it is changed to DONE.
  7179. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7180. DONE are present, add TODO at the beginning of the heading.
  7181. With C-u prefix arg, use completion to determine the new state.
  7182. With numeric prefix arg, switch to that state.
  7183. For calling through lisp, arg is also interpreted in the following way:
  7184. 'none -> empty state
  7185. \"\"(empty string) -> switch to empty state
  7186. 'done -> switch to DONE
  7187. 'nextset -> switch to the next set of keywords
  7188. 'previousset -> switch to the previous set of keywords
  7189. \"WAITING\" -> switch to the specified keyword, but only if it
  7190. really is a member of `org-todo-keywords'."
  7191. (interactive "P")
  7192. (save-excursion
  7193. (catch 'exit
  7194. (org-back-to-heading)
  7195. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7196. (or (looking-at (concat " +" org-todo-regexp " *"))
  7197. (looking-at " *"))
  7198. (let* ((match-data (match-data))
  7199. (startpos (point-at-bol))
  7200. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7201. (org-log-done org-log-done)
  7202. (org-log-repeat org-log-repeat)
  7203. (org-todo-log-states org-todo-log-states)
  7204. (this (match-string 1))
  7205. (hl-pos (match-beginning 0))
  7206. (head (org-get-todo-sequence-head this))
  7207. (ass (assoc head org-todo-kwd-alist))
  7208. (interpret (nth 1 ass))
  7209. (done-word (nth 3 ass))
  7210. (final-done-word (nth 4 ass))
  7211. (last-state (or this ""))
  7212. (completion-ignore-case t)
  7213. (member (member this org-todo-keywords-1))
  7214. (tail (cdr member))
  7215. (state (cond
  7216. ((and org-todo-key-trigger
  7217. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7218. (and (not arg) org-use-fast-todo-selection
  7219. (not (eq org-use-fast-todo-selection 'prefix)))))
  7220. ;; Use fast selection
  7221. (org-fast-todo-selection))
  7222. ((and (equal arg '(4))
  7223. (or (not org-use-fast-todo-selection)
  7224. (not org-todo-key-trigger)))
  7225. ;; Read a state with completion
  7226. (completing-read "State: " (mapcar (lambda(x) (list x))
  7227. org-todo-keywords-1)
  7228. nil t))
  7229. ((eq arg 'right)
  7230. (if this
  7231. (if tail (car tail) nil)
  7232. (car org-todo-keywords-1)))
  7233. ((eq arg 'left)
  7234. (if (equal member org-todo-keywords-1)
  7235. nil
  7236. (if this
  7237. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7238. org-todo-keywords-1)
  7239. (org-last org-todo-keywords-1))))
  7240. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7241. (setq arg nil))) ; hack to fall back to cycling
  7242. (arg
  7243. ;; user or caller requests a specific state
  7244. (cond
  7245. ((equal arg "") nil)
  7246. ((eq arg 'none) nil)
  7247. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7248. ((eq arg 'nextset)
  7249. (or (car (cdr (member head org-todo-heads)))
  7250. (car org-todo-heads)))
  7251. ((eq arg 'previousset)
  7252. (let ((org-todo-heads (reverse org-todo-heads)))
  7253. (or (car (cdr (member head org-todo-heads)))
  7254. (car org-todo-heads))))
  7255. ((car (member arg org-todo-keywords-1)))
  7256. ((nth (1- (prefix-numeric-value arg))
  7257. org-todo-keywords-1))))
  7258. ((null member) (or head (car org-todo-keywords-1)))
  7259. ((equal this final-done-word) nil) ;; -> make empty
  7260. ((null tail) nil) ;; -> first entry
  7261. ((eq interpret 'sequence)
  7262. (car tail))
  7263. ((memq interpret '(type priority))
  7264. (if (eq this-command last-command)
  7265. (car tail)
  7266. (if (> (length tail) 0)
  7267. (or done-word (car org-done-keywords))
  7268. nil)))
  7269. (t nil)))
  7270. (next (if state (concat " " state " ") " "))
  7271. (change-plist (list :type 'todo-state-change :from this :to state
  7272. :position startpos))
  7273. dolog now-done-p)
  7274. (when org-blocker-hook
  7275. (unless (save-excursion
  7276. (save-match-data
  7277. (run-hook-with-args-until-failure
  7278. 'org-blocker-hook change-plist)))
  7279. (if (interactive-p)
  7280. (error "TODO state change from %s to %s blocked" this state)
  7281. ;; fail silently
  7282. (message "TODO state change from %s to %s blocked" this state)
  7283. (throw 'exit nil))))
  7284. (store-match-data match-data)
  7285. (replace-match next t t)
  7286. (unless (pos-visible-in-window-p hl-pos)
  7287. (message "TODO state changed to %s" (org-trim next)))
  7288. (unless head
  7289. (setq head (org-get-todo-sequence-head state)
  7290. ass (assoc head org-todo-kwd-alist)
  7291. interpret (nth 1 ass)
  7292. done-word (nth 3 ass)
  7293. final-done-word (nth 4 ass)))
  7294. (when (memq arg '(nextset previousset))
  7295. (message "Keyword-Set %d/%d: %s"
  7296. (- (length org-todo-sets) -1
  7297. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7298. (length org-todo-sets)
  7299. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7300. (setq org-last-todo-state-is-todo
  7301. (not (member state org-done-keywords)))
  7302. (setq now-done-p (and (member state org-done-keywords)
  7303. (not (member this org-done-keywords))))
  7304. (and logging (org-local-logging logging))
  7305. (when (and (or org-todo-log-states org-log-done)
  7306. (not (memq arg '(nextset previousset))))
  7307. ;; we need to look at recording a time and note
  7308. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7309. (nth 2 (assoc this org-todo-log-states))))
  7310. (when (and state
  7311. (member state org-not-done-keywords)
  7312. (not (member this org-not-done-keywords)))
  7313. ;; This is now a todo state and was not one before
  7314. ;; If there was a CLOSED time stamp, get rid of it.
  7315. (org-add-planning-info nil nil 'closed))
  7316. (when (and now-done-p org-log-done)
  7317. ;; It is now done, and it was not done before
  7318. (org-add-planning-info 'closed (org-current-time))
  7319. (if (and (not dolog) (eq 'note org-log-done))
  7320. (org-add-log-setup 'done state 'findpos 'note)))
  7321. (when (and state dolog)
  7322. ;; This is a non-nil state, and we need to log it
  7323. (org-add-log-setup 'state state 'findpos dolog)))
  7324. ;; Fixup tag positioning
  7325. (org-todo-trigger-tag-changes state)
  7326. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7327. (when org-provide-todo-statistics
  7328. (org-update-parent-todo-statistics))
  7329. (run-hooks 'org-after-todo-state-change-hook)
  7330. (if (and arg (not (member state org-done-keywords)))
  7331. (setq head (org-get-todo-sequence-head state)))
  7332. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7333. ;; Do we need to trigger a repeat?
  7334. (when now-done-p
  7335. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7336. ;; This is for the agenda, take a snapshot of the headline.
  7337. (save-match-data
  7338. (setq org-agenda-headline-snapshot-before-repeat
  7339. (org-get-heading))))
  7340. (org-auto-repeat-maybe state))
  7341. ;; Fixup cursor location if close to the keyword
  7342. (if (and (outline-on-heading-p)
  7343. (not (bolp))
  7344. (save-excursion (beginning-of-line 1)
  7345. (looking-at org-todo-line-regexp))
  7346. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7347. (progn
  7348. (goto-char (or (match-end 2) (match-end 1)))
  7349. (just-one-space)))
  7350. (when org-trigger-hook
  7351. (save-excursion
  7352. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7353. (defun org-update-parent-todo-statistics ()
  7354. "Update any statistics cookie in the parent of the current headline."
  7355. (interactive)
  7356. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7357. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7358. (catch 'exit
  7359. (save-excursion
  7360. (setq level (org-up-heading-safe))
  7361. (unless (and level
  7362. (re-search-forward box-re (point-at-eol) t))
  7363. (throw 'exit nil))
  7364. (setq is-percent (match-end 2))
  7365. (save-match-data
  7366. (unless (outline-next-heading) (throw 'exit nil))
  7367. (while (looking-at org-todo-line-regexp)
  7368. (setq kwd (match-string 2))
  7369. (and kwd (setq cnt-all (1+ cnt-all)))
  7370. (and (member kwd org-done-keywords)
  7371. (setq cnt-done (1+ cnt-done)))
  7372. (condition-case nil
  7373. (org-forward-same-level 1)
  7374. (error (end-of-line 1)))))
  7375. (replace-match
  7376. (if is-percent
  7377. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7378. (format "[%d/%d]" cnt-done cnt-all)))
  7379. (run-hook-with-args 'org-after-todo-statistics-hook
  7380. cnt-done (- cnt-all cnt-done))))))
  7381. (defvar org-after-todo-statistics-hook nil
  7382. "Hook that is called after a TODO statistics cookie has been updated.
  7383. Each function is called with two arguments: the number of not-done entries
  7384. and the number of done entries.
  7385. For example, the following function, when added to this hook, will switch
  7386. an entry to DONE when all children are done, and back to TODO when new
  7387. entries are set to a TODO status. Note that this hook is only called
  7388. when there is a statistics cookie in the headline!
  7389. (defun org-summary-todo (n-done n-not-done)
  7390. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7391. (let (org-log-done org-log-states) ; turn off logging
  7392. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7393. ")
  7394. (defun org-todo-trigger-tag-changes (state)
  7395. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7396. (let ((l org-todo-state-tags-triggers)
  7397. changes)
  7398. (when (or (not state) (equal state ""))
  7399. (setq changes (append changes (cdr (assoc "" l)))))
  7400. (when (and (stringp state) (> (length state) 0))
  7401. (setq changes (append changes (cdr (assoc state l)))))
  7402. (when (member state org-not-done-keywords)
  7403. (setq changes (append changes (cdr (assoc 'todo l)))))
  7404. (when (member state org-done-keywords)
  7405. (setq changes (append changes (cdr (assoc 'done l)))))
  7406. (dolist (c changes)
  7407. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7408. (defun org-local-logging (value)
  7409. "Get logging settings from a property VALUE."
  7410. (let* (words w a)
  7411. ;; directly set the variables, they are already local.
  7412. (setq org-log-done nil
  7413. org-log-repeat nil
  7414. org-todo-log-states nil)
  7415. (setq words (org-split-string value))
  7416. (while (setq w (pop words))
  7417. (cond
  7418. ((setq a (assoc w org-startup-options))
  7419. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7420. (set (nth 1 a) (nth 2 a))))
  7421. ((setq a (org-extract-log-state-settings w))
  7422. (and (member (car a) org-todo-keywords-1)
  7423. (push a org-todo-log-states)))))))
  7424. (defun org-get-todo-sequence-head (kwd)
  7425. "Return the head of the TODO sequence to which KWD belongs.
  7426. If KWD is not set, check if there is a text property remembering the
  7427. right sequence."
  7428. (let (p)
  7429. (cond
  7430. ((not kwd)
  7431. (or (get-text-property (point-at-bol) 'org-todo-head)
  7432. (progn
  7433. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7434. nil (point-at-eol)))
  7435. (get-text-property p 'org-todo-head))))
  7436. ((not (member kwd org-todo-keywords-1))
  7437. (car org-todo-keywords-1))
  7438. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7439. (defun org-fast-todo-selection ()
  7440. "Fast TODO keyword selection with single keys.
  7441. Returns the new TODO keyword, or nil if no state change should occur."
  7442. (let* ((fulltable org-todo-key-alist)
  7443. (done-keywords org-done-keywords) ;; needed for the faces.
  7444. (maxlen (apply 'max (mapcar
  7445. (lambda (x)
  7446. (if (stringp (car x)) (string-width (car x)) 0))
  7447. fulltable)))
  7448. (expert nil)
  7449. (fwidth (+ maxlen 3 1 3))
  7450. (ncol (/ (- (window-width) 4) fwidth))
  7451. tg cnt e c tbl
  7452. groups ingroup)
  7453. (save-window-excursion
  7454. (if expert
  7455. (set-buffer (get-buffer-create " *Org todo*"))
  7456. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7457. (erase-buffer)
  7458. (org-set-local 'org-done-keywords done-keywords)
  7459. (setq tbl fulltable cnt 0)
  7460. (while (setq e (pop tbl))
  7461. (cond
  7462. ((equal e '(:startgroup))
  7463. (push '() groups) (setq ingroup t)
  7464. (when (not (= cnt 0))
  7465. (setq cnt 0)
  7466. (insert "\n"))
  7467. (insert "{ "))
  7468. ((equal e '(:endgroup))
  7469. (setq ingroup nil cnt 0)
  7470. (insert "}\n"))
  7471. (t
  7472. (setq tg (car e) c (cdr e))
  7473. (if ingroup (push tg (car groups)))
  7474. (setq tg (org-add-props tg nil 'face
  7475. (org-get-todo-face tg)))
  7476. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7477. (insert "[" c "] " tg (make-string
  7478. (- fwidth 4 (length tg)) ?\ ))
  7479. (when (= (setq cnt (1+ cnt)) ncol)
  7480. (insert "\n")
  7481. (if ingroup (insert " "))
  7482. (setq cnt 0)))))
  7483. (insert "\n")
  7484. (goto-char (point-min))
  7485. (if (not expert) (org-fit-window-to-buffer))
  7486. (message "[a-z..]:Set [SPC]:clear")
  7487. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7488. (cond
  7489. ((or (= c ?\C-g)
  7490. (and (= c ?q) (not (rassoc c fulltable))))
  7491. (setq quit-flag t))
  7492. ((= c ?\ ) nil)
  7493. ((setq e (rassoc c fulltable) tg (car e))
  7494. tg)
  7495. (t (setq quit-flag t))))))
  7496. (defun org-entry-is-todo-p ()
  7497. (member (org-get-todo-state) org-not-done-keywords))
  7498. (defun org-entry-is-done-p ()
  7499. (member (org-get-todo-state) org-done-keywords))
  7500. (defun org-get-todo-state ()
  7501. (save-excursion
  7502. (org-back-to-heading t)
  7503. (and (looking-at org-todo-line-regexp)
  7504. (match-end 2)
  7505. (match-string 2))))
  7506. (defun org-at-date-range-p (&optional inactive-ok)
  7507. "Is the cursor inside a date range?"
  7508. (interactive)
  7509. (save-excursion
  7510. (catch 'exit
  7511. (let ((pos (point)))
  7512. (skip-chars-backward "^[<\r\n")
  7513. (skip-chars-backward "<[")
  7514. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7515. (>= (match-end 0) pos)
  7516. (throw 'exit t))
  7517. (skip-chars-backward "^<[\r\n")
  7518. (skip-chars-backward "<[")
  7519. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7520. (>= (match-end 0) pos)
  7521. (throw 'exit t)))
  7522. nil)))
  7523. (defun org-get-repeat ()
  7524. "Check if there is a deadline/schedule with repeater in this entry."
  7525. (save-match-data
  7526. (save-excursion
  7527. (org-back-to-heading t)
  7528. (if (re-search-forward
  7529. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7530. (match-string 1)))))
  7531. (defvar org-last-changed-timestamp)
  7532. (defvar org-last-inserted-timestamp)
  7533. (defvar org-log-post-message)
  7534. (defvar org-log-note-purpose)
  7535. (defvar org-log-note-how)
  7536. (defvar org-log-note-extra)
  7537. (defun org-auto-repeat-maybe (done-word)
  7538. "Check if the current headline contains a repeated deadline/schedule.
  7539. If yes, set TODO state back to what it was and change the base date
  7540. of repeating deadline/scheduled time stamps to new date.
  7541. This function is run automatically after each state change to a DONE state."
  7542. ;; last-state is dynamically scoped into this function
  7543. (let* ((repeat (org-get-repeat))
  7544. (aa (assoc last-state org-todo-kwd-alist))
  7545. (interpret (nth 1 aa))
  7546. (head (nth 2 aa))
  7547. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7548. (msg "Entry repeats: ")
  7549. (org-log-done nil)
  7550. (org-todo-log-states nil)
  7551. (nshiftmax 10) (nshift 0)
  7552. re type n what ts mb0 time)
  7553. (when repeat
  7554. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7555. (org-todo (if (eq interpret 'type) last-state head))
  7556. (when org-log-repeat
  7557. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7558. (memq 'org-add-log-note post-command-hook))
  7559. ;; OK, we are already setup for some record
  7560. (if (eq org-log-repeat 'note)
  7561. ;; make sure we take a note, not only a time stamp
  7562. (setq org-log-note-how 'note))
  7563. ;; Set up for taking a record
  7564. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7565. 'findpos org-log-repeat)))
  7566. (org-back-to-heading t)
  7567. (org-add-planning-info nil nil 'closed)
  7568. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7569. org-deadline-time-regexp "\\)\\|\\("
  7570. org-ts-regexp "\\)"))
  7571. (while (re-search-forward
  7572. re (save-excursion (outline-next-heading) (point)) t)
  7573. (setq type (if (match-end 1) org-scheduled-string
  7574. (if (match-end 3) org-deadline-string "Plain:"))
  7575. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7576. mb0 (match-beginning 0))
  7577. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7578. (setq n (string-to-number (match-string 2 ts))
  7579. what (match-string 3 ts))
  7580. (if (equal what "w") (setq n (* n 7) what "d"))
  7581. ;; Preparation, see if we need to modify the start date for the change
  7582. (when (match-end 1)
  7583. (setq time (save-match-data (org-time-string-to-time ts)))
  7584. (cond
  7585. ((equal (match-string 1 ts) ".")
  7586. ;; Shift starting date to today
  7587. (org-timestamp-change
  7588. (- (time-to-days (current-time)) (time-to-days time))
  7589. 'day))
  7590. ((equal (match-string 1 ts) "+")
  7591. (while (or (= nshift 0)
  7592. (<= (time-to-days time) (time-to-days (current-time))))
  7593. (when (= (incf nshift) nshiftmax)
  7594. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7595. (error "Abort")))
  7596. (org-timestamp-change n (cdr (assoc what whata)))
  7597. (org-at-timestamp-p t)
  7598. (setq ts (match-string 1))
  7599. (setq time (save-match-data (org-time-string-to-time ts))))
  7600. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7601. ;; rematch, so that we have everything in place for the real shift
  7602. (org-at-timestamp-p t)
  7603. (setq ts (match-string 1))
  7604. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7605. (org-timestamp-change n (cdr (assoc what whata)))
  7606. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7607. (setq org-log-post-message msg)
  7608. (message "%s" msg))))
  7609. (defun org-show-todo-tree (arg)
  7610. "Make a compact tree which shows all headlines marked with TODO.
  7611. The tree will show the lines where the regexp matches, and all higher
  7612. headlines above the match.
  7613. With a \\[universal-argument] prefix, also show the DONE entries.
  7614. With a numeric prefix N, construct a sparse tree for the Nth element
  7615. of `org-todo-keywords-1'."
  7616. (interactive "P")
  7617. (let ((case-fold-search nil)
  7618. (kwd-re
  7619. (cond ((null arg) org-not-done-regexp)
  7620. ((equal arg '(4))
  7621. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7622. (mapcar 'list org-todo-keywords-1))))
  7623. (concat "\\("
  7624. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7625. "\\)\\>")))
  7626. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7627. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7628. org-todo-keywords-1)))
  7629. (t (error "Invalid prefix argument: %s" arg)))))
  7630. (message "%d TODO entries found"
  7631. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7632. (defun org-deadline (&optional remove time)
  7633. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7634. With argument REMOVE, remove any deadline from the item.
  7635. When TIME is set, it should be an internal time specification, and the
  7636. scheduling will use the corresponding date."
  7637. (interactive "P")
  7638. (if remove
  7639. (progn
  7640. (org-remove-timestamp-with-keyword org-deadline-string)
  7641. (message "Item no longer has a deadline."))
  7642. (if (org-get-repeat)
  7643. (error "Cannot change deadline on task with repeater, please do that by hand")
  7644. (org-add-planning-info 'deadline time 'closed)
  7645. (message "Deadline on %s" org-last-inserted-timestamp))))
  7646. (defun org-schedule (&optional remove time)
  7647. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7648. With argument REMOVE, remove any scheduling date from the item.
  7649. When TIME is set, it should be an internal time specification, and the
  7650. scheduling will use the corresponding date."
  7651. (interactive "P")
  7652. (if remove
  7653. (progn
  7654. (org-remove-timestamp-with-keyword org-scheduled-string)
  7655. (message "Item is no longer scheduled."))
  7656. (if (org-get-repeat)
  7657. (error "Cannot reschedule task with repeater, please do that by hand")
  7658. (org-add-planning-info 'scheduled time 'closed)
  7659. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7660. (defun org-remove-timestamp-with-keyword (keyword)
  7661. "Remove all time stamps with KEYWORD in the current entry."
  7662. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7663. beg)
  7664. (save-excursion
  7665. (org-back-to-heading t)
  7666. (setq beg (point))
  7667. (org-end-of-subtree t t)
  7668. (while (re-search-backward re beg t)
  7669. (replace-match "")
  7670. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7671. (equal (char-before) ?\ ))
  7672. (backward-delete-char 1)
  7673. (if (string-match "^[ \t]*$" (buffer-substring
  7674. (point-at-bol) (point-at-eol)))
  7675. (delete-region (point-at-bol)
  7676. (min (point-max) (1+ (point-at-eol))))))))))
  7677. (defun org-add-planning-info (what &optional time &rest remove)
  7678. "Insert new timestamp with keyword in the line directly after the headline.
  7679. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7680. If non is given, the user is prompted for a date.
  7681. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7682. be removed."
  7683. (interactive)
  7684. (let (org-time-was-given org-end-time-was-given ts
  7685. end default-time default-input)
  7686. (when (and (not time) (memq what '(scheduled deadline)))
  7687. ;; Try to get a default date/time from existing timestamp
  7688. (save-excursion
  7689. (org-back-to-heading t)
  7690. (setq end (save-excursion (outline-next-heading) (point)))
  7691. (when (re-search-forward (if (eq what 'scheduled)
  7692. org-scheduled-time-regexp
  7693. org-deadline-time-regexp)
  7694. end t)
  7695. (setq ts (match-string 1)
  7696. default-time
  7697. (apply 'encode-time (org-parse-time-string ts))
  7698. default-input (and ts (org-get-compact-tod ts))))))
  7699. (when what
  7700. ;; If necessary, get the time from the user
  7701. (setq time (or time (org-read-date nil 'to-time nil nil
  7702. default-time default-input))))
  7703. (when (and org-insert-labeled-timestamps-at-point
  7704. (member what '(scheduled deadline)))
  7705. (insert
  7706. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7707. (org-insert-time-stamp time org-time-was-given
  7708. nil nil nil (list org-end-time-was-given))
  7709. (setq what nil))
  7710. (save-excursion
  7711. (save-restriction
  7712. (let (col list elt ts buffer-invisibility-spec)
  7713. (org-back-to-heading t)
  7714. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7715. (goto-char (match-end 1))
  7716. (setq col (current-column))
  7717. (goto-char (match-end 0))
  7718. (if (eobp) (insert "\n") (forward-char 1))
  7719. (if (and (not (looking-at outline-regexp))
  7720. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7721. "[^\r\n]*"))
  7722. (not (equal (match-string 1) org-clock-string)))
  7723. (narrow-to-region (match-beginning 0) (match-end 0))
  7724. (insert-before-markers "\n")
  7725. (backward-char 1)
  7726. (narrow-to-region (point) (point))
  7727. (and org-adapt-indentation (org-indent-to-column col)))
  7728. ;; Check if we have to remove something.
  7729. (setq list (cons what remove))
  7730. (while list
  7731. (setq elt (pop list))
  7732. (goto-char (point-min))
  7733. (when (or (and (eq elt 'scheduled)
  7734. (re-search-forward org-scheduled-time-regexp nil t))
  7735. (and (eq elt 'deadline)
  7736. (re-search-forward org-deadline-time-regexp nil t))
  7737. (and (eq elt 'closed)
  7738. (re-search-forward org-closed-time-regexp nil t)))
  7739. (replace-match "")
  7740. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7741. (if (looking-at " +") (replace-match ""))))
  7742. (goto-char (point-max))
  7743. (when what
  7744. (insert
  7745. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7746. (cond ((eq what 'scheduled) org-scheduled-string)
  7747. ((eq what 'deadline) org-deadline-string)
  7748. ((eq what 'closed) org-closed-string))
  7749. " ")
  7750. (setq ts (org-insert-time-stamp
  7751. time
  7752. (or org-time-was-given
  7753. (and (eq what 'closed) org-log-done-with-time))
  7754. (eq what 'closed)
  7755. nil nil (list org-end-time-was-given)))
  7756. (end-of-line 1))
  7757. (goto-char (point-min))
  7758. (widen)
  7759. (if (and (looking-at "[ \t]+\n")
  7760. (equal (char-before) ?\n))
  7761. (delete-region (1- (point)) (point-at-eol)))
  7762. ts)))))
  7763. (defvar org-log-note-marker (make-marker))
  7764. (defvar org-log-note-purpose nil)
  7765. (defvar org-log-note-state nil)
  7766. (defvar org-log-note-how nil)
  7767. (defvar org-log-note-extra nil)
  7768. (defvar org-log-note-window-configuration nil)
  7769. (defvar org-log-note-return-to (make-marker))
  7770. (defvar org-log-post-message nil
  7771. "Message to be displayed after a log note has been stored.
  7772. The auto-repeater uses this.")
  7773. (defun org-add-note ()
  7774. "Add a note to the current entry.
  7775. This is done in the same way as adding a state change note."
  7776. (interactive)
  7777. (org-add-log-setup 'note nil 'findpos nil))
  7778. (defvar org-property-end-re)
  7779. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7780. "Set up the post command hook to take a note.
  7781. If this is about to TODO state change, the new state is expected in STATE.
  7782. When FINDPOS is non-nil, find the correct position for the note in
  7783. the current entry. If not, assume that it can be inserted at point.
  7784. HOW is an indicator what kind of note should be created.
  7785. EXTRA is additional text that will be inserted into the notes buffer."
  7786. (save-restriction
  7787. (save-excursion
  7788. (when findpos
  7789. (org-back-to-heading t)
  7790. (narrow-to-region (point) (save-excursion
  7791. (outline-next-heading) (point)))
  7792. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7793. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7794. "[^\r\n]*\\)?"))
  7795. (goto-char (match-end 0))
  7796. (when (and org-log-state-notes-insert-after-drawers
  7797. (save-excursion
  7798. (forward-line) (looking-at org-drawer-regexp)))
  7799. (progn (forward-line)
  7800. (while (looking-at org-drawer-regexp)
  7801. (goto-char (match-end 0))
  7802. (re-search-forward org-property-end-re (point-max) t)
  7803. (forward-line))
  7804. (forward-line -1)))
  7805. (unless org-log-states-order-reversed
  7806. (and (= (char-after) ?\n) (forward-char 1))
  7807. (org-skip-over-state-notes)
  7808. (skip-chars-backward " \t\n\r")))
  7809. (move-marker org-log-note-marker (point))
  7810. (setq org-log-note-purpose purpose
  7811. org-log-note-state state
  7812. org-log-note-how how
  7813. org-log-note-extra extra)
  7814. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7815. (defun org-skip-over-state-notes ()
  7816. "Skip past the list of State notes in an entry."
  7817. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7818. (while (looking-at "[ \t]*- State")
  7819. (condition-case nil
  7820. (org-next-item)
  7821. (error (org-end-of-item)))))
  7822. (defun org-add-log-note (&optional purpose)
  7823. "Pop up a window for taking a note, and add this note later at point."
  7824. (remove-hook 'post-command-hook 'org-add-log-note)
  7825. (setq org-log-note-window-configuration (current-window-configuration))
  7826. (delete-other-windows)
  7827. (move-marker org-log-note-return-to (point))
  7828. (switch-to-buffer (marker-buffer org-log-note-marker))
  7829. (goto-char org-log-note-marker)
  7830. (org-switch-to-buffer-other-window "*Org Note*")
  7831. (erase-buffer)
  7832. (if (memq org-log-note-how '(time state))
  7833. (let (current-prefix-arg) (org-store-log-note))
  7834. (let ((org-inhibit-startup t)) (org-mode))
  7835. (insert (format "# Insert note for %s.
  7836. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7837. (cond
  7838. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7839. ((eq org-log-note-purpose 'done) "closed todo item")
  7840. ((eq org-log-note-purpose 'state)
  7841. (format "state change to \"%s\"" org-log-note-state))
  7842. ((eq org-log-note-purpose 'note)
  7843. "this entry")
  7844. (t (error "This should not happen")))))
  7845. (if org-log-note-extra (insert org-log-note-extra))
  7846. (org-set-local 'org-finish-function 'org-store-log-note)))
  7847. (defvar org-note-abort nil) ; dynamically scoped
  7848. (defun org-store-log-note ()
  7849. "Finish taking a log note, and insert it to where it belongs."
  7850. (let ((txt (buffer-string))
  7851. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7852. lines ind)
  7853. (kill-buffer (current-buffer))
  7854. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7855. (setq txt (replace-match "" t t txt)))
  7856. (if (string-match "\\s-+\\'" txt)
  7857. (setq txt (replace-match "" t t txt)))
  7858. (setq lines (org-split-string txt "\n"))
  7859. (when (and note (string-match "\\S-" note))
  7860. (setq note
  7861. (org-replace-escapes
  7862. note
  7863. (list (cons "%u" (user-login-name))
  7864. (cons "%U" user-full-name)
  7865. (cons "%t" (format-time-string
  7866. (org-time-stamp-format 'long 'inactive)
  7867. (current-time)))
  7868. (cons "%s" (if org-log-note-state
  7869. (concat "\"" org-log-note-state "\"")
  7870. "")))))
  7871. (if lines (setq note (concat note " \\\\")))
  7872. (push note lines))
  7873. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7874. (when lines
  7875. (save-excursion
  7876. (set-buffer (marker-buffer org-log-note-marker))
  7877. (save-excursion
  7878. (goto-char org-log-note-marker)
  7879. (move-marker org-log-note-marker nil)
  7880. (end-of-line 1)
  7881. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7882. (indent-relative nil)
  7883. (insert "- " (pop lines))
  7884. (org-indent-line-function)
  7885. (beginning-of-line 1)
  7886. (looking-at "[ \t]*")
  7887. (setq ind (concat (match-string 0) " "))
  7888. (end-of-line 1)
  7889. (while lines (insert "\n" ind (pop lines)))))))
  7890. (set-window-configuration org-log-note-window-configuration)
  7891. (with-current-buffer (marker-buffer org-log-note-return-to)
  7892. (goto-char org-log-note-return-to))
  7893. (move-marker org-log-note-return-to nil)
  7894. (and org-log-post-message (message "%s" org-log-post-message)))
  7895. (defun org-sparse-tree (&optional arg)
  7896. "Create a sparse tree, prompt for the details.
  7897. This command can create sparse trees. You first need to select the type
  7898. of match used to create the tree:
  7899. t Show entries with a specific TODO keyword.
  7900. T Show entries selected by a tags match.
  7901. p Enter a property name and its value (both with completion on existing
  7902. names/values) and show entries with that property.
  7903. r Show entries matching a regular expression
  7904. d Show deadlines due within `org-deadline-warning-days'."
  7905. (interactive "P")
  7906. (let (ans kwd value)
  7907. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7908. (setq ans (read-char-exclusive))
  7909. (cond
  7910. ((equal ans ?d)
  7911. (call-interactively 'org-check-deadlines))
  7912. ((equal ans ?b)
  7913. (call-interactively 'org-check-before-date))
  7914. ((equal ans ?t)
  7915. (org-show-todo-tree '(4)))
  7916. ((equal ans ?T)
  7917. (call-interactively 'org-tags-sparse-tree))
  7918. ((member ans '(?p ?P))
  7919. (setq kwd (completing-read "Property: "
  7920. (mapcar 'list (org-buffer-property-keys))))
  7921. (setq value (completing-read "Value: "
  7922. (mapcar 'list (org-property-values kwd))))
  7923. (unless (string-match "\\`{.*}\\'" value)
  7924. (setq value (concat "\"" value "\"")))
  7925. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7926. ((member ans '(?r ?R ?/))
  7927. (call-interactively 'org-occur))
  7928. (t (error "No such sparse tree command \"%c\"" ans)))))
  7929. (defvar org-occur-highlights nil
  7930. "List of overlays used for occur matches.")
  7931. (make-variable-buffer-local 'org-occur-highlights)
  7932. (defvar org-occur-parameters nil
  7933. "Parameters of the active org-occur calls.
  7934. This is a list, each call to org-occur pushes as cons cell,
  7935. containing the regular expression and the callback, onto the list.
  7936. The list can contain several entries if `org-occur' has been called
  7937. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7938. will only contain one set of parameters. When the highlights are
  7939. removed (for example with `C-c C-c', or with the next edit (depending
  7940. on `org-remove-highlights-with-change'), this variable is emptied
  7941. as well.")
  7942. (make-variable-buffer-local 'org-occur-parameters)
  7943. (defun org-occur (regexp &optional keep-previous callback)
  7944. "Make a compact tree which shows all matches of REGEXP.
  7945. The tree will show the lines where the regexp matches, and all higher
  7946. headlines above the match. It will also show the heading after the match,
  7947. to make sure editing the matching entry is easy.
  7948. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7949. call to `org-occur' will be kept, to allow stacking of calls to this
  7950. command.
  7951. If CALLBACK is non-nil, it is a function which is called to confirm
  7952. that the match should indeed be shown."
  7953. (interactive "sRegexp: \nP")
  7954. (unless keep-previous
  7955. (org-remove-occur-highlights nil nil t))
  7956. (push (cons regexp callback) org-occur-parameters)
  7957. (let ((cnt 0))
  7958. (save-excursion
  7959. (goto-char (point-min))
  7960. (if (or (not keep-previous) ; do not want to keep
  7961. (not org-occur-highlights)) ; no previous matches
  7962. ;; hide everything
  7963. (org-overview))
  7964. (while (re-search-forward regexp nil t)
  7965. (when (or (not callback)
  7966. (save-match-data (funcall callback)))
  7967. (setq cnt (1+ cnt))
  7968. (when org-highlight-sparse-tree-matches
  7969. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7970. (org-show-context 'occur-tree))))
  7971. (when org-remove-highlights-with-change
  7972. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7973. nil 'local))
  7974. (unless org-sparse-tree-open-archived-trees
  7975. (org-hide-archived-subtrees (point-min) (point-max)))
  7976. (run-hooks 'org-occur-hook)
  7977. (if (interactive-p)
  7978. (message "%d match(es) for regexp %s" cnt regexp))
  7979. cnt))
  7980. (defun org-show-context (&optional key)
  7981. "Make sure point and context and visible.
  7982. How much context is shown depends upon the variables
  7983. `org-show-hierarchy-above', `org-show-following-heading'. and
  7984. `org-show-siblings'."
  7985. (let ((heading-p (org-on-heading-p t))
  7986. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7987. (following-p (org-get-alist-option org-show-following-heading key))
  7988. (entry-p (org-get-alist-option org-show-entry-below key))
  7989. (siblings-p (org-get-alist-option org-show-siblings key)))
  7990. (catch 'exit
  7991. ;; Show heading or entry text
  7992. (if (and heading-p (not entry-p))
  7993. (org-flag-heading nil) ; only show the heading
  7994. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7995. (org-show-hidden-entry))) ; show entire entry
  7996. (when following-p
  7997. ;; Show next sibling, or heading below text
  7998. (save-excursion
  7999. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8000. (org-flag-heading nil))))
  8001. (when siblings-p (org-show-siblings))
  8002. (when hierarchy-p
  8003. ;; show all higher headings, possibly with siblings
  8004. (save-excursion
  8005. (while (and (condition-case nil
  8006. (progn (org-up-heading-all 1) t)
  8007. (error nil))
  8008. (not (bobp)))
  8009. (org-flag-heading nil)
  8010. (when siblings-p (org-show-siblings))))))))
  8011. (defun org-reveal (&optional siblings)
  8012. "Show current entry, hierarchy above it, and the following headline.
  8013. This can be used to show a consistent set of context around locations
  8014. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8015. not t for the search context.
  8016. With optional argument SIBLINGS, on each level of the hierarchy all
  8017. siblings are shown. This repairs the tree structure to what it would
  8018. look like when opened with hierarchical calls to `org-cycle'."
  8019. (interactive "P")
  8020. (let ((org-show-hierarchy-above t)
  8021. (org-show-following-heading t)
  8022. (org-show-siblings (if siblings t org-show-siblings)))
  8023. (org-show-context nil)))
  8024. (defun org-highlight-new-match (beg end)
  8025. "Highlight from BEG to END and mark the highlight is an occur headline."
  8026. (let ((ov (org-make-overlay beg end)))
  8027. (org-overlay-put ov 'face 'secondary-selection)
  8028. (push ov org-occur-highlights)))
  8029. (defun org-remove-occur-highlights (&optional beg end noremove)
  8030. "Remove the occur highlights from the buffer.
  8031. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8032. from the `before-change-functions' in the current buffer."
  8033. (interactive)
  8034. (unless org-inhibit-highlight-removal
  8035. (mapc 'org-delete-overlay org-occur-highlights)
  8036. (setq org-occur-highlights nil)
  8037. (setq org-occur-parameters nil)
  8038. (unless noremove
  8039. (remove-hook 'before-change-functions
  8040. 'org-remove-occur-highlights 'local))))
  8041. ;;;; Priorities
  8042. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8043. "Regular expression matching the priority indicator.")
  8044. (defvar org-remove-priority-next-time nil)
  8045. (defun org-priority-up ()
  8046. "Increase the priority of the current item."
  8047. (interactive)
  8048. (org-priority 'up))
  8049. (defun org-priority-down ()
  8050. "Decrease the priority of the current item."
  8051. (interactive)
  8052. (org-priority 'down))
  8053. (defun org-priority (&optional action)
  8054. "Change the priority of an item by ARG.
  8055. ACTION can be `set', `up', `down', or a character."
  8056. (interactive)
  8057. (setq action (or action 'set))
  8058. (let (current new news have remove)
  8059. (save-excursion
  8060. (org-back-to-heading)
  8061. (if (looking-at org-priority-regexp)
  8062. (setq current (string-to-char (match-string 2))
  8063. have t)
  8064. (setq current org-default-priority))
  8065. (cond
  8066. ((or (eq action 'set)
  8067. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8068. (if (not (eq action 'set))
  8069. (setq new action)
  8070. (message "Priority %c-%c, SPC to remove: "
  8071. org-highest-priority org-lowest-priority)
  8072. (setq new (read-char-exclusive)))
  8073. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8074. (= (upcase org-lowest-priority) org-lowest-priority))
  8075. (setq new (upcase new)))
  8076. (cond ((equal new ?\ ) (setq remove t))
  8077. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8078. (error "Priority must be between `%c' and `%c'"
  8079. org-highest-priority org-lowest-priority))))
  8080. ((eq action 'up)
  8081. (if (and (not have) (eq last-command this-command))
  8082. (setq new org-lowest-priority)
  8083. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8084. org-default-priority (1- current)))))
  8085. ((eq action 'down)
  8086. (if (and (not have) (eq last-command this-command))
  8087. (setq new org-highest-priority)
  8088. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8089. org-default-priority (1+ current)))))
  8090. (t (error "Invalid action")))
  8091. (if (or (< (upcase new) org-highest-priority)
  8092. (> (upcase new) org-lowest-priority))
  8093. (setq remove t))
  8094. (setq news (format "%c" new))
  8095. (if have
  8096. (if remove
  8097. (replace-match "" t t nil 1)
  8098. (replace-match news t t nil 2))
  8099. (if remove
  8100. (error "No priority cookie found in line")
  8101. (looking-at org-todo-line-regexp)
  8102. (if (match-end 2)
  8103. (progn
  8104. (goto-char (match-end 2))
  8105. (insert " [#" news "]"))
  8106. (goto-char (match-beginning 3))
  8107. (insert "[#" news "] ")))))
  8108. (org-preserve-lc (org-set-tags nil 'align))
  8109. (if remove
  8110. (message "Priority removed")
  8111. (message "Priority of current item set to %s" news))))
  8112. (defun org-get-priority (s)
  8113. "Find priority cookie and return priority."
  8114. (save-match-data
  8115. (if (not (string-match org-priority-regexp s))
  8116. (* 1000 (- org-lowest-priority org-default-priority))
  8117. (* 1000 (- org-lowest-priority
  8118. (string-to-char (match-string 2 s)))))))
  8119. ;;;; Tags
  8120. (defvar org-agenda-archives-mode)
  8121. (defun org-scan-tags (action matcher &optional todo-only)
  8122. "Scan headline tags with inheritance and produce output ACTION.
  8123. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8124. or `agenda' to produce an entry list for an agenda view. It can also be
  8125. a Lisp form or a function that should be called at each matched headline, in
  8126. this case the return value is a list of all return values from these calls.
  8127. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8128. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8129. only lines with a TODO keyword are included in the output."
  8130. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8131. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8132. (org-re
  8133. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8134. (props (list 'face 'default
  8135. 'done-face 'org-done
  8136. 'undone-face 'default
  8137. 'mouse-face 'highlight
  8138. 'org-not-done-regexp org-not-done-regexp
  8139. 'org-todo-regexp org-todo-regexp
  8140. 'keymap org-agenda-keymap
  8141. 'help-echo
  8142. (format "mouse-2 or RET jump to org file %s"
  8143. (abbreviate-file-name
  8144. (or (buffer-file-name (buffer-base-buffer))
  8145. (buffer-name (buffer-base-buffer)))))))
  8146. (case-fold-search nil)
  8147. lspos tags tags-list
  8148. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8149. (llast 0) rtn rtn1 level category i txt
  8150. todo marker entry priority)
  8151. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8152. (setq action (list 'lambda nil action)))
  8153. (save-excursion
  8154. (goto-char (point-min))
  8155. (when (eq action 'sparse-tree)
  8156. (org-overview)
  8157. (org-remove-occur-highlights))
  8158. (while (re-search-forward re nil t)
  8159. (catch :skip
  8160. (setq todo (if (match-end 1) (match-string 2))
  8161. tags (if (match-end 4) (match-string 4)))
  8162. (goto-char (setq lspos (1+ (match-beginning 0))))
  8163. (setq level (org-reduced-level (funcall outline-level))
  8164. category (org-get-category))
  8165. (setq i llast llast level)
  8166. ;; remove tag lists from same and sublevels
  8167. (while (>= i level)
  8168. (when (setq entry (assoc i tags-alist))
  8169. (setq tags-alist (delete entry tags-alist)))
  8170. (setq i (1- i)))
  8171. ;; add the next tags
  8172. (when tags
  8173. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8174. tags-alist
  8175. (cons (cons level tags) tags-alist)))
  8176. ;; compile tags for current headline
  8177. (setq tags-list
  8178. (if org-use-tag-inheritance
  8179. (apply 'append (mapcar 'cdr tags-alist))
  8180. tags))
  8181. (when (and tags org-use-tag-inheritance
  8182. (not (eq t org-use-tag-inheritance)))
  8183. ;; selective inheritance, remove uninherited ones
  8184. (setcdr (car tags-alist)
  8185. (org-remove-uniherited-tags (cdar tags-alist))))
  8186. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8187. (let ((case-fold-search t)) (eval matcher))
  8188. (or
  8189. (not (member org-archive-tag tags-list))
  8190. ;; we have an archive tag, should we use this anyway?
  8191. (or (not org-agenda-skip-archived-trees)
  8192. (and (eq action 'agenda) org-agenda-archives-mode))))
  8193. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8194. ;; select this headline
  8195. (cond
  8196. ((eq action 'sparse-tree)
  8197. (and org-highlight-sparse-tree-matches
  8198. (org-get-heading) (match-end 0)
  8199. (org-highlight-new-match
  8200. (match-beginning 0) (match-beginning 1)))
  8201. (org-show-context 'tags-tree))
  8202. ((eq action 'agenda)
  8203. (setq txt (org-format-agenda-item
  8204. ""
  8205. (concat
  8206. (if org-tags-match-list-sublevels
  8207. (make-string (1- level) ?.) "")
  8208. (org-get-heading))
  8209. category tags-list)
  8210. priority (org-get-priority txt))
  8211. (goto-char lspos)
  8212. (setq marker (org-agenda-new-marker))
  8213. (org-add-props txt props
  8214. 'org-marker marker 'org-hd-marker marker 'org-category category
  8215. 'priority priority 'type "tagsmatch")
  8216. (push txt rtn))
  8217. ((functionp action)
  8218. (save-excursion
  8219. (setq rtn1 (funcall action))
  8220. (push rtn1 rtn))
  8221. (goto-char (point-at-eol)))
  8222. (t (error "Invalid action")))
  8223. ;; if we are to skip sublevels, jump to end of subtree
  8224. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8225. (when (and (eq action 'sparse-tree)
  8226. (not org-sparse-tree-open-archived-trees))
  8227. (org-hide-archived-subtrees (point-min) (point-max)))
  8228. (nreverse rtn)))
  8229. (defun org-remove-uniherited-tags (tags)
  8230. "Remove all tags that are not inherited from the list TAGS."
  8231. (cond
  8232. ((eq org-use-tag-inheritance t) tags)
  8233. ((not org-use-tag-inheritance) nil)
  8234. ((stringp org-use-tag-inheritance)
  8235. (delq nil (mapcar
  8236. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8237. tags)))
  8238. ((listp org-use-tag-inheritance)
  8239. (delq nil (mapcar
  8240. (lambda (x) (if (member x org-use-tag-inheritance) x nil))
  8241. tags)))))
  8242. (defvar todo-only) ;; dynamically scoped
  8243. (defun org-tags-sparse-tree (&optional todo-only match)
  8244. "Create a sparse tree according to tags string MATCH.
  8245. MATCH can contain positive and negative selection of tags, like
  8246. \"+WORK+URGENT-WITHBOSS\".
  8247. If optional argument TODO_ONLY is non-nil, only select lines that are
  8248. also TODO lines."
  8249. (interactive "P")
  8250. (org-prepare-agenda-buffers (list (current-buffer)))
  8251. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8252. (defvar org-cached-props nil)
  8253. (defun org-cached-entry-get (pom property)
  8254. (if (or (eq t org-use-property-inheritance)
  8255. (and (stringp org-use-property-inheritance)
  8256. (string-match org-use-property-inheritance property))
  8257. (and (listp org-use-property-inheritance)
  8258. (member property org-use-property-inheritance)))
  8259. ;; Caching is not possible, check it directly
  8260. (org-entry-get pom property 'inherit)
  8261. ;; Get all properties, so that we can do complicated checks easily
  8262. (cdr (assoc property (or org-cached-props
  8263. (setq org-cached-props
  8264. (org-entry-properties pom)))))))
  8265. (defun org-global-tags-completion-table (&optional files)
  8266. "Return the list of all tags in all agenda buffer/files."
  8267. (save-excursion
  8268. (org-uniquify
  8269. (delq nil
  8270. (apply 'append
  8271. (mapcar
  8272. (lambda (file)
  8273. (set-buffer (find-file-noselect file))
  8274. (append (org-get-buffer-tags)
  8275. (mapcar (lambda (x) (if (stringp (car-safe x))
  8276. (list (car-safe x)) nil))
  8277. org-tag-alist)))
  8278. (if (and files (car files))
  8279. files
  8280. (org-agenda-files))))))))
  8281. (defun org-make-tags-matcher (match)
  8282. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8283. ;; todo-only is scoped dynamically into this function, and the function
  8284. ;; may change it it the matcher asksk for it.
  8285. (unless match
  8286. ;; Get a new match request, with completion
  8287. (let ((org-last-tags-completion-table
  8288. (org-global-tags-completion-table)))
  8289. (setq match (completing-read
  8290. "Match: " 'org-tags-completion-function nil nil nil
  8291. 'org-tags-history))))
  8292. ;; Parse the string and create a lisp form
  8293. (let ((match0 match)
  8294. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8295. minus tag mm
  8296. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8297. orterms term orlist re-p str-p level-p level-op time-p
  8298. prop-p pn pv po cat-p gv rest)
  8299. (if (string-match "/+" match)
  8300. ;; match contains also a todo-matching request
  8301. (progn
  8302. (setq tagsmatch (substring match 0 (match-beginning 0))
  8303. todomatch (substring match (match-end 0)))
  8304. (if (string-match "^!" todomatch)
  8305. (setq todo-only t todomatch (substring todomatch 1)))
  8306. (if (string-match "^\\s-*$" todomatch)
  8307. (setq todomatch nil)))
  8308. ;; only matching tags
  8309. (setq tagsmatch match todomatch nil))
  8310. ;; Make the tags matcher
  8311. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8312. (setq tagsmatcher t)
  8313. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8314. (while (setq term (pop orterms))
  8315. (while (and (equal (substring term -1) "\\") orterms)
  8316. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8317. (while (string-match re term)
  8318. (setq rest (substring term (match-end 0))
  8319. minus (and (match-end 1)
  8320. (equal (match-string 1 term) "-"))
  8321. tag (match-string 2 term)
  8322. re-p (equal (string-to-char tag) ?{)
  8323. level-p (match-end 4)
  8324. prop-p (match-end 5)
  8325. mm (cond
  8326. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8327. (level-p
  8328. (setq level-op (org-op-to-function (match-string 3 term)))
  8329. `(,level-op level ,(string-to-number
  8330. (match-string 4 term))))
  8331. (prop-p
  8332. (setq pn (match-string 5 term)
  8333. po (match-string 6 term)
  8334. pv (match-string 7 term)
  8335. cat-p (equal pn "CATEGORY")
  8336. re-p (equal (string-to-char pv) ?{)
  8337. str-p (equal (string-to-char pv) ?\")
  8338. time-p (save-match-data
  8339. (string-match "^\"[[<].*[]>]\"$" pv))
  8340. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8341. (if time-p (setq pv (org-matcher-time pv)))
  8342. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8343. (cond
  8344. ((equal pn "CATEGORY")
  8345. (setq gv '(get-text-property (point) 'org-category)))
  8346. ((equal pn "TODO")
  8347. (setq gv 'todo))
  8348. (t
  8349. (setq gv `(org-cached-entry-get nil ,pn))))
  8350. (if re-p
  8351. (if (eq po 'org<>)
  8352. `(not (string-match ,pv (or ,gv "")))
  8353. `(string-match ,pv (or ,gv "")))
  8354. (if str-p
  8355. `(,po (or ,gv "") ,pv)
  8356. `(,po (string-to-number (or ,gv ""))
  8357. ,(string-to-number pv) ))))
  8358. (t `(member ,(downcase tag) tags-list)))
  8359. mm (if minus (list 'not mm) mm)
  8360. term rest)
  8361. (push mm tagsmatcher))
  8362. (push (if (> (length tagsmatcher) 1)
  8363. (cons 'and tagsmatcher)
  8364. (car tagsmatcher))
  8365. orlist)
  8366. (setq tagsmatcher nil))
  8367. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8368. (setq tagsmatcher
  8369. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8370. ;; Make the todo matcher
  8371. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8372. (setq todomatcher t)
  8373. (setq orterms (org-split-string todomatch "|") orlist nil)
  8374. (while (setq term (pop orterms))
  8375. (while (string-match re term)
  8376. (setq minus (and (match-end 1)
  8377. (equal (match-string 1 term) "-"))
  8378. kwd (match-string 2 term)
  8379. re-p (equal (string-to-char kwd) ?{)
  8380. term (substring term (match-end 0))
  8381. mm (if re-p
  8382. `(string-match ,(substring kwd 1 -1) todo)
  8383. (list 'equal 'todo kwd))
  8384. mm (if minus (list 'not mm) mm))
  8385. (push mm todomatcher))
  8386. (push (if (> (length todomatcher) 1)
  8387. (cons 'and todomatcher)
  8388. (car todomatcher))
  8389. orlist)
  8390. (setq todomatcher nil))
  8391. (setq todomatcher (if (> (length orlist) 1)
  8392. (cons 'or orlist) (car orlist))))
  8393. ;; Return the string and lisp forms of the matcher
  8394. (setq matcher (if todomatcher
  8395. (list 'and tagsmatcher todomatcher)
  8396. tagsmatcher))
  8397. (cons match0 matcher)))
  8398. (defun org-op-to-function (op &optional stringp)
  8399. "Turn an operator into the appropriate function."
  8400. (setq op
  8401. (cond
  8402. ((equal op "<" ) '(< string< org-time<))
  8403. ((equal op ">" ) '(> org-string> org-time>))
  8404. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8405. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8406. ((member op '("=" "==")) '(= string= org-time=))
  8407. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8408. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8409. (defun org<> (a b) (not (= a b)))
  8410. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8411. (defun org-string>= (a b) (not (string< a b)))
  8412. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8413. (defun org-string<> (a b) (not (string= a b)))
  8414. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8415. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8416. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8417. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8418. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8419. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8420. (defun org-2ft (s)
  8421. "Convert S to a floating point time.
  8422. If S is already a number, just return it. If it is a string, parse
  8423. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8424. (cond
  8425. ((numberp s) s)
  8426. ((stringp s)
  8427. (condition-case nil
  8428. (float-time (apply 'encode-time (org-parse-time-string s)))
  8429. (error 0.)))
  8430. (t 0.)))
  8431. (defun org-matcher-time (s)
  8432. (cond
  8433. ((equal s "<now>") (float-time))
  8434. ((equal s "<today>")
  8435. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8436. (t (org-2ft s))))
  8437. (defun org-match-any-p (re list)
  8438. "Does re match any element of list?"
  8439. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8440. (delq nil list))
  8441. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8442. (defvar org-tags-overlay (org-make-overlay 1 1))
  8443. (org-detach-overlay org-tags-overlay)
  8444. (defun org-get-local-tags-at (&optional pos)
  8445. "Get a list of tags defined in the current headline."
  8446. (org-get-tags-at pos 'local))
  8447. (defun org-get-local-tags ()
  8448. "Get a list of tags defined in the current headline."
  8449. (org-get-tags-at nil 'local))
  8450. (defun org-get-tags-at (&optional pos local)
  8451. "Get a list of all headline tags applicable at POS.
  8452. POS defaults to point. If tags are inherited, the list contains
  8453. the targets in the same sequence as the headlines appear, i.e.
  8454. the tags of the current headline come last.
  8455. When LOCAL is non-nil, only return tags from the current headline,
  8456. ignore inherited ones."
  8457. (interactive)
  8458. (let (tags ltags lastpos parent)
  8459. (save-excursion
  8460. (save-restriction
  8461. (widen)
  8462. (goto-char (or pos (point)))
  8463. (save-match-data
  8464. (catch 'done
  8465. (condition-case nil
  8466. (progn
  8467. (org-back-to-heading t)
  8468. (while (not (equal lastpos (point)))
  8469. (setq lastpos (point))
  8470. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8471. (setq ltags (org-split-string
  8472. (org-match-string-no-properties 1) ":"))
  8473. (setq tags (append
  8474. (if parent
  8475. (org-remove-uniherited-tags ltags)
  8476. ltags)
  8477. tags)))
  8478. (or org-use-tag-inheritance (throw 'done t))
  8479. (if local (throw 'done t))
  8480. (org-up-heading-all 1)
  8481. (setq parent t)))
  8482. (error nil)))))
  8483. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8484. (defun org-toggle-tag (tag &optional onoff)
  8485. "Toggle the tag TAG for the current line.
  8486. If ONOFF is `on' or `off', don't toggle but set to this state."
  8487. (unless (org-on-heading-p t) (error "Not on headling"))
  8488. (let (res current)
  8489. (save-excursion
  8490. (beginning-of-line)
  8491. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8492. (point-at-eol) t)
  8493. (progn
  8494. (setq current (match-string 1))
  8495. (replace-match ""))
  8496. (setq current ""))
  8497. (setq current (nreverse (org-split-string current ":")))
  8498. (cond
  8499. ((eq onoff 'on)
  8500. (setq res t)
  8501. (or (member tag current) (push tag current)))
  8502. ((eq onoff 'off)
  8503. (or (not (member tag current)) (setq current (delete tag current))))
  8504. (t (if (member tag current)
  8505. (setq current (delete tag current))
  8506. (setq res t)
  8507. (push tag current))))
  8508. (end-of-line 1)
  8509. (if current
  8510. (progn
  8511. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8512. (org-set-tags nil t))
  8513. (delete-horizontal-space))
  8514. (run-hooks 'org-after-tags-change-hook))
  8515. res))
  8516. (defun org-align-tags-here (to-col)
  8517. ;; Assumes that this is a headline
  8518. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8519. (beginning-of-line 1)
  8520. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8521. (< pos (match-beginning 2)))
  8522. (progn
  8523. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8524. (goto-char (match-beginning 1))
  8525. (insert " ")
  8526. (delete-region (point) (1+ (match-beginning 2)))
  8527. (setq ncol (max (1+ (current-column))
  8528. (1+ col)
  8529. (if (> to-col 0)
  8530. to-col
  8531. (- (abs to-col) tags-l))))
  8532. (setq p (point))
  8533. (insert (make-string (- ncol (current-column)) ?\ ))
  8534. (setq ncol (current-column))
  8535. (when indent-tabs-mode (tabify p (point-at-eol)))
  8536. (org-move-to-column (min ncol col) t))
  8537. (goto-char pos))))
  8538. (defun org-set-tags-command (&optional arg just-align)
  8539. "Call the set-tags command for the current entry."
  8540. (interactive "P")
  8541. (if (org-on-heading-p)
  8542. (org-set-tags arg just-align)
  8543. (save-excursion
  8544. (org-back-to-heading t)
  8545. (org-set-tags arg just-align))))
  8546. (defun org-set-tags (&optional arg just-align)
  8547. "Set the tags for the current headline.
  8548. With prefix ARG, realign all tags in headings in the current buffer."
  8549. (interactive "P")
  8550. (let* ((re (concat "^" outline-regexp))
  8551. (current (org-get-tags-string))
  8552. (col (current-column))
  8553. (org-setting-tags t)
  8554. table current-tags inherited-tags ; computed below when needed
  8555. tags p0 c0 c1 rpl)
  8556. (if arg
  8557. (save-excursion
  8558. (goto-char (point-min))
  8559. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8560. (while (re-search-forward re nil t)
  8561. (org-set-tags nil t)
  8562. (end-of-line 1)))
  8563. (message "All tags realigned to column %d" org-tags-column))
  8564. (if just-align
  8565. (setq tags current)
  8566. ;; Get a new set of tags from the user
  8567. (save-excursion
  8568. (setq table (or org-tag-alist (org-get-buffer-tags))
  8569. org-last-tags-completion-table table
  8570. current-tags (org-split-string current ":")
  8571. inherited-tags (nreverse
  8572. (nthcdr (length current-tags)
  8573. (nreverse (org-get-tags-at))))
  8574. tags
  8575. (if (or (eq t org-use-fast-tag-selection)
  8576. (and org-use-fast-tag-selection
  8577. (delq nil (mapcar 'cdr table))))
  8578. (org-fast-tag-selection
  8579. current-tags inherited-tags table
  8580. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8581. (let ((org-add-colon-after-tag-completion t))
  8582. (org-trim
  8583. (org-without-partial-completion
  8584. (completing-read "Tags: " 'org-tags-completion-function
  8585. nil nil current 'org-tags-history)))))))
  8586. (while (string-match "[-+&]+" tags)
  8587. ;; No boolean logic, just a list
  8588. (setq tags (replace-match ":" t t tags))))
  8589. (if (string-match "\\`[\t ]*\\'" tags)
  8590. (setq tags "")
  8591. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8592. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8593. ;; Insert new tags at the correct column
  8594. (beginning-of-line 1)
  8595. (cond
  8596. ((and (equal current "") (equal tags "")))
  8597. ((re-search-forward
  8598. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8599. (point-at-eol) t)
  8600. (if (equal tags "")
  8601. (setq rpl "")
  8602. (goto-char (match-beginning 0))
  8603. (setq c0 (current-column) p0 (point)
  8604. c1 (max (1+ c0) (if (> org-tags-column 0)
  8605. org-tags-column
  8606. (- (- org-tags-column) (length tags))))
  8607. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8608. (replace-match rpl t t)
  8609. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8610. tags)
  8611. (t (error "Tags alignment failed")))
  8612. (org-move-to-column col)
  8613. (unless just-align
  8614. (run-hooks 'org-after-tags-change-hook)))))
  8615. (defun org-change-tag-in-region (beg end tag off)
  8616. "Add or remove TAG for each entry in the region.
  8617. This works in the agenda, and also in an org-mode buffer."
  8618. (interactive
  8619. (list (region-beginning) (region-end)
  8620. (let ((org-last-tags-completion-table
  8621. (if (org-mode-p)
  8622. (org-get-buffer-tags)
  8623. (org-global-tags-completion-table))))
  8624. (completing-read
  8625. "Tag: " 'org-tags-completion-function nil nil nil
  8626. 'org-tags-history))
  8627. (progn
  8628. (message "[s]et or [r]emove? ")
  8629. (equal (read-char-exclusive) ?r))))
  8630. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8631. (let ((agendap (equal major-mode 'org-agenda-mode))
  8632. l1 l2 m buf pos newhead (cnt 0))
  8633. (goto-char end)
  8634. (setq l2 (1- (org-current-line)))
  8635. (goto-char beg)
  8636. (setq l1 (org-current-line))
  8637. (loop for l from l1 to l2 do
  8638. (goto-line l)
  8639. (setq m (get-text-property (point) 'org-hd-marker))
  8640. (when (or (and (org-mode-p) (org-on-heading-p))
  8641. (and agendap m))
  8642. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8643. pos (if agendap m (point)))
  8644. (with-current-buffer buf
  8645. (save-excursion
  8646. (save-restriction
  8647. (goto-char pos)
  8648. (setq cnt (1+ cnt))
  8649. (org-toggle-tag tag (if off 'off 'on))
  8650. (setq newhead (org-get-heading)))))
  8651. (and agendap (org-agenda-change-all-lines newhead m))))
  8652. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8653. (defun org-tags-completion-function (string predicate &optional flag)
  8654. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8655. (confirm (lambda (x) (stringp (car x)))))
  8656. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8657. (setq s1 (match-string 1 string)
  8658. s2 (match-string 2 string))
  8659. (setq s1 "" s2 string))
  8660. (cond
  8661. ((eq flag nil)
  8662. ;; try completion
  8663. (setq rtn (try-completion s2 ctable confirm))
  8664. (if (stringp rtn)
  8665. (setq rtn
  8666. (concat s1 s2 (substring rtn (length s2))
  8667. (if (and org-add-colon-after-tag-completion
  8668. (assoc rtn ctable))
  8669. ":" ""))))
  8670. rtn)
  8671. ((eq flag t)
  8672. ;; all-completions
  8673. (all-completions s2 ctable confirm)
  8674. )
  8675. ((eq flag 'lambda)
  8676. ;; exact match?
  8677. (assoc s2 ctable)))
  8678. ))
  8679. (defun org-fast-tag-insert (kwd tags face &optional end)
  8680. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8681. (insert (format "%-12s" (concat kwd ":"))
  8682. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8683. (or end "")))
  8684. (defun org-fast-tag-show-exit (flag)
  8685. (save-excursion
  8686. (goto-line 3)
  8687. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8688. (replace-match ""))
  8689. (when flag
  8690. (end-of-line 1)
  8691. (org-move-to-column (- (window-width) 19) t)
  8692. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8693. (defun org-set-current-tags-overlay (current prefix)
  8694. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8695. (if (featurep 'xemacs)
  8696. (org-overlay-display org-tags-overlay (concat prefix s)
  8697. 'secondary-selection)
  8698. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8699. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8700. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8701. "Fast tag selection with single keys.
  8702. CURRENT is the current list of tags in the headline, INHERITED is the
  8703. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8704. possibly with grouping information. TODO-TABLE is a similar table with
  8705. TODO keywords, should these have keys assigned to them.
  8706. If the keys are nil, a-z are automatically assigned.
  8707. Returns the new tags string, or nil to not change the current settings."
  8708. (let* ((fulltable (append table todo-table))
  8709. (maxlen (apply 'max (mapcar
  8710. (lambda (x)
  8711. (if (stringp (car x)) (string-width (car x)) 0))
  8712. fulltable)))
  8713. (buf (current-buffer))
  8714. (expert (eq org-fast-tag-selection-single-key 'expert))
  8715. (buffer-tags nil)
  8716. (fwidth (+ maxlen 3 1 3))
  8717. (ncol (/ (- (window-width) 4) fwidth))
  8718. (i-face 'org-done)
  8719. (c-face 'org-todo)
  8720. tg cnt e c char c1 c2 ntable tbl rtn
  8721. ov-start ov-end ov-prefix
  8722. (exit-after-next org-fast-tag-selection-single-key)
  8723. (done-keywords org-done-keywords)
  8724. groups ingroup)
  8725. (save-excursion
  8726. (beginning-of-line 1)
  8727. (if (looking-at
  8728. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8729. (setq ov-start (match-beginning 1)
  8730. ov-end (match-end 1)
  8731. ov-prefix "")
  8732. (setq ov-start (1- (point-at-eol))
  8733. ov-end (1+ ov-start))
  8734. (skip-chars-forward "^\n\r")
  8735. (setq ov-prefix
  8736. (concat
  8737. (buffer-substring (1- (point)) (point))
  8738. (if (> (current-column) org-tags-column)
  8739. " "
  8740. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8741. (org-move-overlay org-tags-overlay ov-start ov-end)
  8742. (save-window-excursion
  8743. (if expert
  8744. (set-buffer (get-buffer-create " *Org tags*"))
  8745. (delete-other-windows)
  8746. (split-window-vertically)
  8747. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8748. (erase-buffer)
  8749. (org-set-local 'org-done-keywords done-keywords)
  8750. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8751. (org-fast-tag-insert "Current" current c-face "\n\n")
  8752. (org-fast-tag-show-exit exit-after-next)
  8753. (org-set-current-tags-overlay current ov-prefix)
  8754. (setq tbl fulltable char ?a cnt 0)
  8755. (while (setq e (pop tbl))
  8756. (cond
  8757. ((equal e '(:startgroup))
  8758. (push '() groups) (setq ingroup t)
  8759. (when (not (= cnt 0))
  8760. (setq cnt 0)
  8761. (insert "\n"))
  8762. (insert "{ "))
  8763. ((equal e '(:endgroup))
  8764. (setq ingroup nil cnt 0)
  8765. (insert "}\n"))
  8766. (t
  8767. (setq tg (car e) c2 nil)
  8768. (if (cdr e)
  8769. (setq c (cdr e))
  8770. ;; automatically assign a character.
  8771. (setq c1 (string-to-char
  8772. (downcase (substring
  8773. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8774. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8775. (while (or (rassoc char ntable) (rassoc char table))
  8776. (setq char (1+ char)))
  8777. (setq c2 c1))
  8778. (setq c (or c2 char)))
  8779. (if ingroup (push tg (car groups)))
  8780. (setq tg (org-add-props tg nil 'face
  8781. (cond
  8782. ((not (assoc tg table))
  8783. (org-get-todo-face tg))
  8784. ((member tg current) c-face)
  8785. ((member tg inherited) i-face)
  8786. (t nil))))
  8787. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8788. (insert "[" c "] " tg (make-string
  8789. (- fwidth 4 (length tg)) ?\ ))
  8790. (push (cons tg c) ntable)
  8791. (when (= (setq cnt (1+ cnt)) ncol)
  8792. (insert "\n")
  8793. (if ingroup (insert " "))
  8794. (setq cnt 0)))))
  8795. (setq ntable (nreverse ntable))
  8796. (insert "\n")
  8797. (goto-char (point-min))
  8798. (if (not expert) (org-fit-window-to-buffer))
  8799. (setq rtn
  8800. (catch 'exit
  8801. (while t
  8802. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8803. (if groups " [!] no groups" " [!]groups")
  8804. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8805. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8806. (cond
  8807. ((= c ?\r) (throw 'exit t))
  8808. ((= c ?!)
  8809. (setq groups (not groups))
  8810. (goto-char (point-min))
  8811. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8812. ((= c ?\C-c)
  8813. (if (not expert)
  8814. (org-fast-tag-show-exit
  8815. (setq exit-after-next (not exit-after-next)))
  8816. (setq expert nil)
  8817. (delete-other-windows)
  8818. (split-window-vertically)
  8819. (org-switch-to-buffer-other-window " *Org tags*")
  8820. (org-fit-window-to-buffer)))
  8821. ((or (= c ?\C-g)
  8822. (and (= c ?q) (not (rassoc c ntable))))
  8823. (org-detach-overlay org-tags-overlay)
  8824. (setq quit-flag t))
  8825. ((= c ?\ )
  8826. (setq current nil)
  8827. (if exit-after-next (setq exit-after-next 'now)))
  8828. ((= c ?\t)
  8829. (condition-case nil
  8830. (setq tg (completing-read
  8831. "Tag: "
  8832. (or buffer-tags
  8833. (with-current-buffer buf
  8834. (org-get-buffer-tags)))))
  8835. (quit (setq tg "")))
  8836. (when (string-match "\\S-" tg)
  8837. (add-to-list 'buffer-tags (list tg))
  8838. (if (member tg current)
  8839. (setq current (delete tg current))
  8840. (push tg current)))
  8841. (if exit-after-next (setq exit-after-next 'now)))
  8842. ((setq e (rassoc c todo-table) tg (car e))
  8843. (with-current-buffer buf
  8844. (save-excursion (org-todo tg)))
  8845. (if exit-after-next (setq exit-after-next 'now)))
  8846. ((setq e (rassoc c ntable) tg (car e))
  8847. (if (member tg current)
  8848. (setq current (delete tg current))
  8849. (loop for g in groups do
  8850. (if (member tg g)
  8851. (mapc (lambda (x)
  8852. (setq current (delete x current)))
  8853. g)))
  8854. (push tg current))
  8855. (if exit-after-next (setq exit-after-next 'now))))
  8856. ;; Create a sorted list
  8857. (setq current
  8858. (sort current
  8859. (lambda (a b)
  8860. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8861. (if (eq exit-after-next 'now) (throw 'exit t))
  8862. (goto-char (point-min))
  8863. (beginning-of-line 2)
  8864. (delete-region (point) (point-at-eol))
  8865. (org-fast-tag-insert "Current" current c-face)
  8866. (org-set-current-tags-overlay current ov-prefix)
  8867. (while (re-search-forward
  8868. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8869. (setq tg (match-string 1))
  8870. (add-text-properties
  8871. (match-beginning 1) (match-end 1)
  8872. (list 'face
  8873. (cond
  8874. ((member tg current) c-face)
  8875. ((member tg inherited) i-face)
  8876. (t (get-text-property (match-beginning 1) 'face))))))
  8877. (goto-char (point-min)))))
  8878. (org-detach-overlay org-tags-overlay)
  8879. (if rtn
  8880. (mapconcat 'identity current ":")
  8881. nil))))
  8882. (defun org-get-tags-string ()
  8883. "Get the TAGS string in the current headline."
  8884. (unless (org-on-heading-p t)
  8885. (error "Not on a heading"))
  8886. (save-excursion
  8887. (beginning-of-line 1)
  8888. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8889. (org-match-string-no-properties 1)
  8890. "")))
  8891. (defun org-get-tags ()
  8892. "Get the list of tags specified in the current headline."
  8893. (org-split-string (org-get-tags-string) ":"))
  8894. (defun org-get-buffer-tags ()
  8895. "Get a table of all tags used in the buffer, for completion."
  8896. (let (tags)
  8897. (save-excursion
  8898. (goto-char (point-min))
  8899. (while (re-search-forward
  8900. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8901. (when (equal (char-after (point-at-bol 0)) ?*)
  8902. (mapc (lambda (x) (add-to-list 'tags x))
  8903. (org-split-string (org-match-string-no-properties 1) ":")))))
  8904. (mapcar 'list tags)))
  8905. ;;;; The mapping API
  8906. ;;;###autoload
  8907. (defun org-map-entries (func &optional match scope &rest skip)
  8908. "Call FUNC at each headline selected by MATCH in SCOPE.
  8909. FUNC is a function or a lisp form. The function will be called without
  8910. arguments, with the cursor positioned at the beginning of the headline.
  8911. The return values of all calls to the function will be collected and
  8912. returned as a list.
  8913. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8914. Only headlines that are matched by this query will be considered during
  8915. the iteration. When MATCH is nil or t, all headlines will be
  8916. visited by the iteration.
  8917. SCOPE determines the scope of this command. It can be any of:
  8918. nil The current buffer, respecting the restriction if any
  8919. tree The subtree started with the entry at point
  8920. file The current buffer, without restriction
  8921. file-with-archives
  8922. The current buffer, and any archives associated with it
  8923. agenda All agenda files
  8924. agenda-with-archives
  8925. All agenda files with any archive files associated with them
  8926. \(file1 file2 ...)
  8927. If this is a list, all files in the list will be scanned
  8928. The remaining args are treated as settings for the skipping facilities of
  8929. the scanner. The following items can be given here:
  8930. archive skip trees with the archive tag.
  8931. comment skip trees with the COMMENT keyword
  8932. function or Emacs Lisp form:
  8933. will be used as value for `org-agenda-skip-function', so whenever
  8934. the the function returns t, FUNC will not be called for that
  8935. entry and search will continue from the point where the
  8936. function leaves it."
  8937. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8938. (org-agenda-skip-archived-trees (memq 'archive skip))
  8939. (org-agenda-skip-comment-trees (memq 'comment skip))
  8940. (org-agenda-skip-function
  8941. (car (org-delete-all '(comment archive) skip)))
  8942. (org-tags-match-list-sublevels t)
  8943. matcher pos file
  8944. org-todo-keywords-for-agenda
  8945. org-done-keywords-for-agenda
  8946. org-todo-keyword-alist-for-agenda
  8947. org-tag-alist-for-agenda)
  8948. (cond
  8949. ((eq match t) (setq matcher t))
  8950. ((eq match nil) (setq matcher t))
  8951. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  8952. (when (eq scope 'tree)
  8953. (org-back-to-heading t)
  8954. (org-narrow-to-subtree)
  8955. (setq scope nil))
  8956. (if (not scope)
  8957. (progn
  8958. (org-prepare-agenda-buffers
  8959. (list (buffer-file-name (current-buffer))))
  8960. (org-scan-tags func matcher))
  8961. ;; Get the right scope
  8962. (setq pos (point))
  8963. (cond
  8964. ((and scope (listp scope) (symbolp (car scope)))
  8965. (setq scope (eval scope)))
  8966. ((eq scope 'agenda)
  8967. (setq scope (org-agenda-files t)))
  8968. ((eq scope 'agenda-with-archives)
  8969. (setq scope (org-agenda-files t))
  8970. (setq scope (org-add-archive-files scope)))
  8971. ((eq scope 'file)
  8972. (setq scope (list (buffer-file-name))))
  8973. ((eq scope 'file-with-archives)
  8974. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  8975. (org-prepare-agenda-buffers scope)
  8976. (while (setq file (pop scope))
  8977. (with-current-buffer (org-find-base-buffer-visiting file)
  8978. (save-excursion
  8979. (save-restriction
  8980. (widen)
  8981. (goto-char (point-min))
  8982. (org-scan-tags func matcher))))))))
  8983. ;;;; Properties
  8984. ;;; Setting and retrieving properties
  8985. (defconst org-special-properties
  8986. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  8987. "TIMESTAMP" "TIMESTAMP_IA")
  8988. "The special properties valid in Org-mode.
  8989. These are properties that are not defined in the property drawer,
  8990. but in some other way.")
  8991. (defconst org-default-properties
  8992. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8993. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8994. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8995. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8996. "Some properties that are used by Org-mode for various purposes.
  8997. Being in this list makes sure that they are offered for completion.")
  8998. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8999. "Regular expression matching the first line of a property drawer.")
  9000. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9001. "Regular expression matching the first line of a property drawer.")
  9002. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9003. "Regular expression matching the first line of a property drawer.")
  9004. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9005. "Regular expression matching the first line of a property drawer.")
  9006. (defconst org-property-drawer-re
  9007. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9008. org-property-end-re "\\)\n?")
  9009. "Matches an entire property drawer.")
  9010. (defconst org-clock-drawer-re
  9011. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9012. org-property-end-re "\\)\n?")
  9013. "Matches an entire clock drawer.")
  9014. (defun org-property-action ()
  9015. "Do an action on properties."
  9016. (interactive)
  9017. (let (c)
  9018. (org-at-property-p)
  9019. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9020. (setq c (read-char-exclusive))
  9021. (cond
  9022. ((equal c ?s)
  9023. (call-interactively 'org-set-property))
  9024. ((equal c ?d)
  9025. (call-interactively 'org-delete-property))
  9026. ((equal c ?D)
  9027. (call-interactively 'org-delete-property-globally))
  9028. ((equal c ?c)
  9029. (call-interactively 'org-compute-property-at-point))
  9030. (t (error "No such property action %c" c)))))
  9031. (defun org-at-property-p ()
  9032. "Is the cursor in a property line?"
  9033. ;; FIXME: Does not check if we are actually in the drawer.
  9034. ;; FIXME: also returns true on any drawers.....
  9035. ;; This is used by C-c C-c for property action.
  9036. (save-excursion
  9037. (beginning-of-line 1)
  9038. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9039. (defun org-get-property-block (&optional beg end force)
  9040. "Return the (beg . end) range of the body of the property drawer.
  9041. BEG and END can be beginning and end of subtree, if not given
  9042. they will be found.
  9043. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9044. (catch 'exit
  9045. (save-excursion
  9046. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9047. (end (or end (progn (outline-next-heading) (point)))))
  9048. (goto-char beg)
  9049. (if (re-search-forward org-property-start-re end t)
  9050. (setq beg (1+ (match-end 0)))
  9051. (if force
  9052. (save-excursion
  9053. (org-insert-property-drawer)
  9054. (setq end (progn (outline-next-heading) (point))))
  9055. (throw 'exit nil))
  9056. (goto-char beg)
  9057. (if (re-search-forward org-property-start-re end t)
  9058. (setq beg (1+ (match-end 0)))))
  9059. (if (re-search-forward org-property-end-re end t)
  9060. (setq end (match-beginning 0))
  9061. (or force (throw 'exit nil))
  9062. (goto-char beg)
  9063. (setq end beg)
  9064. (org-indent-line-function)
  9065. (insert ":END:\n"))
  9066. (cons beg end)))))
  9067. (defun org-entry-properties (&optional pom which)
  9068. "Get all properties of the entry at point-or-marker POM.
  9069. This includes the TODO keyword, the tags, time strings for deadline,
  9070. scheduled, and clocking, and any additional properties defined in the
  9071. entry. The return value is an alist, keys may occur multiple times
  9072. if the property key was used several times.
  9073. POM may also be nil, in which case the current entry is used.
  9074. If WHICH is nil or `all', get all properties. If WHICH is
  9075. `special' or `standard', only get that subclass."
  9076. (setq which (or which 'all))
  9077. (org-with-point-at pom
  9078. (let ((clockstr (substring org-clock-string 0 -1))
  9079. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9080. beg end range props sum-props key value string clocksum)
  9081. (save-excursion
  9082. (when (condition-case nil (org-back-to-heading t) (error nil))
  9083. (setq beg (point))
  9084. (setq sum-props (get-text-property (point) 'org-summaries))
  9085. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9086. (outline-next-heading)
  9087. (setq end (point))
  9088. (when (memq which '(all special))
  9089. ;; Get the special properties, like TODO and tags
  9090. (goto-char beg)
  9091. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9092. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9093. (when (looking-at org-priority-regexp)
  9094. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9095. (when (and (setq value (org-get-tags-string))
  9096. (string-match "\\S-" value))
  9097. (push (cons "TAGS" value) props))
  9098. (when (setq value (org-get-tags-at))
  9099. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9100. props))
  9101. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9102. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9103. string (if (equal key clockstr)
  9104. (org-no-properties
  9105. (org-trim
  9106. (buffer-substring
  9107. (match-beginning 3) (goto-char (point-at-eol)))))
  9108. (substring (org-match-string-no-properties 3) 1 -1)))
  9109. (unless key
  9110. (if (= (char-after (match-beginning 3)) ?\[)
  9111. (setq key "TIMESTAMP_IA")
  9112. (setq key "TIMESTAMP")))
  9113. (when (or (equal key clockstr) (not (assoc key props)))
  9114. (push (cons key string) props)))
  9115. )
  9116. (when (memq which '(all standard))
  9117. ;; Get the standard properties, like :PORP: ...
  9118. (setq range (org-get-property-block beg end))
  9119. (when range
  9120. (goto-char (car range))
  9121. (while (re-search-forward
  9122. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9123. (cdr range) t)
  9124. (setq key (org-match-string-no-properties 1)
  9125. value (org-trim (or (org-match-string-no-properties 2) "")))
  9126. (unless (member key excluded)
  9127. (push (cons key (or value "")) props)))))
  9128. (if clocksum
  9129. (push (cons "CLOCKSUM"
  9130. (org-columns-number-to-string (/ (float clocksum) 60.)
  9131. 'add_times))
  9132. props))
  9133. (unless (assoc "CATEGORY" props)
  9134. (setq value (or (org-get-category)
  9135. (progn (org-refresh-category-properties)
  9136. (org-get-category))))
  9137. (push (cons "CATEGORY" value) props))
  9138. (append sum-props (nreverse props)))))))
  9139. (defun org-entry-get (pom property &optional inherit)
  9140. "Get value of PROPERTY for entry at point-or-marker POM.
  9141. If INHERIT is non-nil and the entry does not have the property,
  9142. then also check higher levels of the hierarchy.
  9143. If INHERIT is the symbol `selective', use inheritance only if the setting
  9144. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9145. If the property is present but empty, the return value is the empty string.
  9146. If the property is not present at all, nil is returned."
  9147. (org-with-point-at pom
  9148. (if (and inherit (if (eq inherit 'selective)
  9149. (org-property-inherit-p property)
  9150. t))
  9151. (org-entry-get-with-inheritance property)
  9152. (if (member property org-special-properties)
  9153. ;; We need a special property. Use brute force, get all properties.
  9154. (cdr (assoc property (org-entry-properties nil 'special)))
  9155. (let ((range (org-get-property-block)))
  9156. (if (and range
  9157. (goto-char (car range))
  9158. (re-search-forward
  9159. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9160. (cdr range) t))
  9161. ;; Found the property, return it.
  9162. (if (match-end 1)
  9163. (org-match-string-no-properties 1)
  9164. "")))))))
  9165. (defun org-property-or-variable-value (var &optional inherit)
  9166. "Check if there is a property fixing the value of VAR.
  9167. If yes, return this value. If not, return the current value of the variable."
  9168. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9169. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9170. (read prop)
  9171. (symbol-value var))))
  9172. (defun org-entry-delete (pom property)
  9173. "Delete the property PROPERTY from entry at point-or-marker POM."
  9174. (org-with-point-at pom
  9175. (if (member property org-special-properties)
  9176. nil ; cannot delete these properties.
  9177. (let ((range (org-get-property-block)))
  9178. (if (and range
  9179. (goto-char (car range))
  9180. (re-search-forward
  9181. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9182. (cdr range) t))
  9183. (progn
  9184. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9185. t)
  9186. nil)))))
  9187. ;; Multi-values properties are properties that contain multiple values
  9188. ;; These values are assumed to be single words, separated by whitespace.
  9189. (defun org-entry-add-to-multivalued-property (pom property value)
  9190. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9191. (let* ((old (org-entry-get pom property))
  9192. (values (and old (org-split-string old "[ \t]"))))
  9193. (setq value (org-entry-protect-space value))
  9194. (unless (member value values)
  9195. (setq values (cons value values))
  9196. (org-entry-put pom property
  9197. (mapconcat 'identity values " ")))))
  9198. (defun org-entry-remove-from-multivalued-property (pom property value)
  9199. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9200. (let* ((old (org-entry-get pom property))
  9201. (values (and old (org-split-string old "[ \t]"))))
  9202. (setq value (org-entry-protect-space value))
  9203. (when (member value values)
  9204. (setq values (delete value values))
  9205. (org-entry-put pom property
  9206. (mapconcat 'identity values " ")))))
  9207. (defun org-entry-member-in-multivalued-property (pom property value)
  9208. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9209. (let* ((old (org-entry-get pom property))
  9210. (values (and old (org-split-string old "[ \t]"))))
  9211. (setq value (org-entry-protect-space value))
  9212. (member value values)))
  9213. (defun org-entry-get-multivalued-property (pom property)
  9214. "Return a list of values in a multivalued property."
  9215. (let* ((value (org-entry-get pom property))
  9216. (values (and value (org-split-string value "[ \t]"))))
  9217. (mapcar 'org-entry-restore-space values)))
  9218. (defun org-entry-put-multivalued-property (pom property &rest values)
  9219. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9220. VALUES should be a list of strings. Spaces will be protected."
  9221. (org-entry-put pom property
  9222. (mapconcat 'org-entry-protect-space values " "))
  9223. (let* ((value (org-entry-get pom property))
  9224. (values (and value (org-split-string value "[ \t]"))))
  9225. (mapcar 'org-entry-restore-space values)))
  9226. (defun org-entry-protect-space (s)
  9227. "Protect spaces and newline in string S."
  9228. (while (string-match " " s)
  9229. (setq s (replace-match "%20" t t s)))
  9230. (while (string-match "\n" s)
  9231. (setq s (replace-match "%0A" t t s)))
  9232. s)
  9233. (defun org-entry-restore-space (s)
  9234. "Restore spaces and newline in string S."
  9235. (while (string-match "%20" s)
  9236. (setq s (replace-match " " t t s)))
  9237. (while (string-match "%0A" s)
  9238. (setq s (replace-match "\n" t t s)))
  9239. s)
  9240. (defvar org-entry-property-inherited-from (make-marker)
  9241. "Marker pointing to the entry from where a proerty was inherited.
  9242. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9243. location of the entry where the inheriance search matched. If there was
  9244. no match, the marker will point nowhere.
  9245. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9246. is set.")
  9247. (defun org-entry-get-with-inheritance (property)
  9248. "Get entry property, and search higher levels if not present."
  9249. (move-marker org-entry-property-inherited-from nil)
  9250. (let (tmp)
  9251. (save-excursion
  9252. (save-restriction
  9253. (widen)
  9254. (catch 'ex
  9255. (while t
  9256. (when (setq tmp (org-entry-get nil property))
  9257. (org-back-to-heading t)
  9258. (move-marker org-entry-property-inherited-from (point))
  9259. (throw 'ex tmp))
  9260. (or (org-up-heading-safe) (throw 'ex nil)))))
  9261. (or tmp
  9262. (cdr (assoc property org-file-properties))
  9263. (cdr (assoc property org-global-properties))
  9264. (cdr (assoc property org-global-properties-fixed))))))
  9265. (defun org-entry-put (pom property value)
  9266. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9267. (org-with-point-at pom
  9268. (org-back-to-heading t)
  9269. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9270. range)
  9271. (cond
  9272. ((equal property "TODO")
  9273. (when (and (stringp value) (string-match "\\S-" value)
  9274. (not (member value org-todo-keywords-1)))
  9275. (error "\"%s\" is not a valid TODO state" value))
  9276. (if (or (not value)
  9277. (not (string-match "\\S-" value)))
  9278. (setq value 'none))
  9279. (org-todo value)
  9280. (org-set-tags nil 'align))
  9281. ((equal property "PRIORITY")
  9282. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9283. (string-to-char value) ?\ ))
  9284. (org-set-tags nil 'align))
  9285. ((equal property "SCHEDULED")
  9286. (if (re-search-forward org-scheduled-time-regexp end t)
  9287. (cond
  9288. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9289. ((eq value 'later) (org-timestamp-change 1 'day))
  9290. (t (call-interactively 'org-schedule)))
  9291. (call-interactively 'org-schedule)))
  9292. ((equal property "DEADLINE")
  9293. (if (re-search-forward org-deadline-time-regexp end t)
  9294. (cond
  9295. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9296. ((eq value 'later) (org-timestamp-change 1 'day))
  9297. (t (call-interactively 'org-deadline)))
  9298. (call-interactively 'org-deadline)))
  9299. ((member property org-special-properties)
  9300. (error "The %s property can not yet be set with `org-entry-put'"
  9301. property))
  9302. (t ; a non-special property
  9303. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9304. (setq range (org-get-property-block beg end 'force))
  9305. (goto-char (car range))
  9306. (if (re-search-forward
  9307. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9308. (progn
  9309. (delete-region (match-beginning 1) (match-end 1))
  9310. (goto-char (match-beginning 1)))
  9311. (goto-char (cdr range))
  9312. (insert "\n")
  9313. (backward-char 1)
  9314. (org-indent-line-function)
  9315. (insert ":" property ":"))
  9316. (and value (insert " " value))
  9317. (org-indent-line-function)))))))
  9318. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9319. "Get all property keys in the current buffer.
  9320. With INCLUDE-SPECIALS, also list the special properties that relect things
  9321. like tags and TODO state.
  9322. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9323. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9324. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9325. formats in the current buffer."
  9326. (let (rtn range cfmt cols s p)
  9327. (save-excursion
  9328. (save-restriction
  9329. (widen)
  9330. (goto-char (point-min))
  9331. (while (re-search-forward org-property-start-re nil t)
  9332. (setq range (org-get-property-block))
  9333. (goto-char (car range))
  9334. (while (re-search-forward
  9335. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9336. (cdr range) t)
  9337. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9338. (outline-next-heading))))
  9339. (when include-specials
  9340. (setq rtn (append org-special-properties rtn)))
  9341. (when include-defaults
  9342. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9343. (when include-columns
  9344. (save-excursion
  9345. (save-restriction
  9346. (widen)
  9347. (goto-char (point-min))
  9348. (while (re-search-forward
  9349. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9350. nil t)
  9351. (setq cfmt (match-string 2) s 0)
  9352. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9353. cfmt s)
  9354. (setq s (match-end 0)
  9355. p (match-string 1 cfmt))
  9356. (unless (or (equal p "ITEM")
  9357. (member p org-special-properties))
  9358. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9359. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9360. (defun org-property-values (key)
  9361. "Return a list of all values of property KEY."
  9362. (save-excursion
  9363. (save-restriction
  9364. (widen)
  9365. (goto-char (point-min))
  9366. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9367. values)
  9368. (while (re-search-forward re nil t)
  9369. (add-to-list 'values (org-trim (match-string 1))))
  9370. (delete "" values)))))
  9371. (defun org-insert-property-drawer ()
  9372. "Insert a property drawer into the current entry."
  9373. (interactive)
  9374. (org-back-to-heading t)
  9375. (looking-at outline-regexp)
  9376. (let ((indent (- (match-end 0)(match-beginning 0)))
  9377. (beg (point))
  9378. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9379. end hiddenp)
  9380. (outline-next-heading)
  9381. (setq end (point))
  9382. (goto-char beg)
  9383. (while (re-search-forward re end t))
  9384. (setq hiddenp (org-invisible-p))
  9385. (end-of-line 1)
  9386. (and (equal (char-after) ?\n) (forward-char 1))
  9387. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9388. (beginning-of-line 2))
  9389. (org-skip-over-state-notes)
  9390. (skip-chars-backward " \t\n\r")
  9391. (if (eq (char-before) ?*) (forward-char 1))
  9392. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9393. (beginning-of-line 0)
  9394. (org-indent-to-column indent)
  9395. (beginning-of-line 2)
  9396. (org-indent-to-column indent)
  9397. (beginning-of-line 0)
  9398. (if hiddenp
  9399. (save-excursion
  9400. (org-back-to-heading t)
  9401. (hide-entry))
  9402. (org-flag-drawer t))))
  9403. (defun org-set-property (property value)
  9404. "In the current entry, set PROPERTY to VALUE.
  9405. When called interactively, this will prompt for a property name, offering
  9406. completion on existing and default properties. And then it will prompt
  9407. for a value, offering competion either on allowed values (via an inherited
  9408. xxx_ALL property) or on existing values in other instances of this property
  9409. in the current file."
  9410. (interactive
  9411. (let* ((completion-ignore-case t)
  9412. (keys (org-buffer-property-keys nil t t))
  9413. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9414. (prop (if (member prop0 keys)
  9415. prop0
  9416. (or (cdr (assoc (downcase prop0)
  9417. (mapcar (lambda (x) (cons (downcase x) x))
  9418. keys)))
  9419. prop0)))
  9420. (cur (org-entry-get nil prop))
  9421. (allowed (org-property-get-allowed-values nil prop 'table))
  9422. (existing (mapcar 'list (org-property-values prop)))
  9423. (val (if allowed
  9424. (org-completing-read "Value: " allowed nil 'req-match)
  9425. (org-completing-read
  9426. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9427. (concat "[" cur "]") "")
  9428. ": ")
  9429. existing nil nil "" nil cur))))
  9430. (list prop (if (equal val "") cur val))))
  9431. (unless (equal (org-entry-get nil property) value)
  9432. (org-entry-put nil property value)))
  9433. (defun org-delete-property (property)
  9434. "In the current entry, delete PROPERTY."
  9435. (interactive
  9436. (let* ((completion-ignore-case t)
  9437. (prop (completing-read
  9438. "Property: " (org-entry-properties nil 'standard))))
  9439. (list prop)))
  9440. (message "Property %s %s" property
  9441. (if (org-entry-delete nil property)
  9442. "deleted"
  9443. "was not present in the entry")))
  9444. (defun org-delete-property-globally (property)
  9445. "Remove PROPERTY globally, from all entries."
  9446. (interactive
  9447. (let* ((completion-ignore-case t)
  9448. (prop (completing-read
  9449. "Globally remove property: "
  9450. (mapcar 'list (org-buffer-property-keys)))))
  9451. (list prop)))
  9452. (save-excursion
  9453. (save-restriction
  9454. (widen)
  9455. (goto-char (point-min))
  9456. (let ((cnt 0))
  9457. (while (re-search-forward
  9458. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9459. nil t)
  9460. (setq cnt (1+ cnt))
  9461. (replace-match ""))
  9462. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9463. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9464. (defun org-compute-property-at-point ()
  9465. "Compute the property at point.
  9466. This looks for an enclosing column format, extracts the operator and
  9467. then applies it to the proerty in the column format's scope."
  9468. (interactive)
  9469. (unless (org-at-property-p)
  9470. (error "Not at a property"))
  9471. (let ((prop (org-match-string-no-properties 2)))
  9472. (org-columns-get-format-and-top-level)
  9473. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9474. (error "No operator defined for property %s" prop))
  9475. (org-columns-compute prop)))
  9476. (defun org-property-get-allowed-values (pom property &optional table)
  9477. "Get allowed values for the property PROPERTY.
  9478. When TABLE is non-nil, return an alist that can directly be used for
  9479. completion."
  9480. (let (vals)
  9481. (cond
  9482. ((equal property "TODO")
  9483. (setq vals (org-with-point-at pom
  9484. (append org-todo-keywords-1 '("")))))
  9485. ((equal property "PRIORITY")
  9486. (let ((n org-lowest-priority))
  9487. (while (>= n org-highest-priority)
  9488. (push (char-to-string n) vals)
  9489. (setq n (1- n)))))
  9490. ((member property org-special-properties))
  9491. (t
  9492. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9493. (when (and vals (string-match "\\S-" vals))
  9494. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9495. (setq vals (mapcar (lambda (x)
  9496. (cond ((stringp x) x)
  9497. ((numberp x) (number-to-string x))
  9498. ((symbolp x) (symbol-name x))
  9499. (t "???")))
  9500. vals)))))
  9501. (if table (mapcar 'list vals) vals)))
  9502. (defun org-property-previous-allowed-value (&optional previous)
  9503. "Switch to the next allowed value for this property."
  9504. (interactive)
  9505. (org-property-next-allowed-value t))
  9506. (defun org-property-next-allowed-value (&optional previous)
  9507. "Switch to the next allowed value for this property."
  9508. (interactive)
  9509. (unless (org-at-property-p)
  9510. (error "Not at a property"))
  9511. (let* ((key (match-string 2))
  9512. (value (match-string 3))
  9513. (allowed (or (org-property-get-allowed-values (point) key)
  9514. (and (member value '("[ ]" "[-]" "[X]"))
  9515. '("[ ]" "[X]"))))
  9516. nval)
  9517. (unless allowed
  9518. (error "Allowed values for this property have not been defined"))
  9519. (if previous (setq allowed (reverse allowed)))
  9520. (if (member value allowed)
  9521. (setq nval (car (cdr (member value allowed)))))
  9522. (setq nval (or nval (car allowed)))
  9523. (if (equal nval value)
  9524. (error "Only one allowed value for this property"))
  9525. (org-at-property-p)
  9526. (replace-match (concat " :" key ": " nval) t t)
  9527. (org-indent-line-function)
  9528. (beginning-of-line 1)
  9529. (skip-chars-forward " \t")))
  9530. (defun org-find-entry-with-id (ident)
  9531. "Locate the entry that contains the ID property with exact value IDENT.
  9532. IDENT can be a string, a symbol or a number, this function will search for
  9533. the string representation of it.
  9534. Return the position where this entry starts, or nil if there is no such entry."
  9535. (let ((id (cond
  9536. ((stringp ident) ident)
  9537. ((symbol-name ident) (symbol-name ident))
  9538. ((numberp ident) (number-to-string ident))
  9539. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9540. (case-fold-search nil))
  9541. (save-excursion
  9542. (save-restriction
  9543. (widen)
  9544. (goto-char (point-min))
  9545. (when (re-search-forward
  9546. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9547. nil t)
  9548. (org-back-to-heading)
  9549. (point))))))
  9550. ;;;; Timestamps
  9551. (defvar org-last-changed-timestamp nil)
  9552. (defvar org-last-inserted-timestamp nil
  9553. "The last time stamp inserted with `org-insert-time-stamp'.")
  9554. (defvar org-time-was-given) ; dynamically scoped parameter
  9555. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9556. (defvar org-ts-what) ; dynamically scoped parameter
  9557. (defun org-time-stamp (arg &optional inactive)
  9558. "Prompt for a date/time and insert a time stamp.
  9559. If the user specifies a time like HH:MM, or if this command is called
  9560. with a prefix argument, the time stamp will contain date and time.
  9561. Otherwise, only the date will be included. All parts of a date not
  9562. specified by the user will be filled in from the current date/time.
  9563. So if you press just return without typing anything, the time stamp
  9564. will represent the current date/time. If there is already a timestamp
  9565. at the cursor, it will be modified."
  9566. (interactive "P")
  9567. (let* ((ts nil)
  9568. (default-time
  9569. ;; Default time is either today, or, when entering a range,
  9570. ;; the range start.
  9571. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9572. (save-excursion
  9573. (re-search-backward
  9574. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9575. (- (point) 20) t)))
  9576. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9577. (current-time)))
  9578. (default-input (and ts (org-get-compact-tod ts)))
  9579. org-time-was-given org-end-time-was-given time)
  9580. (cond
  9581. ((and (org-at-timestamp-p t)
  9582. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9583. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9584. (insert "--")
  9585. (setq time (let ((this-command this-command))
  9586. (org-read-date arg 'totime nil nil
  9587. default-time default-input)))
  9588. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9589. ((org-at-timestamp-p t)
  9590. (setq time (let ((this-command this-command))
  9591. (org-read-date arg 'totime nil nil default-time default-input)))
  9592. (when (org-at-timestamp-p t) ; just to get the match data
  9593. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9594. (replace-match "")
  9595. (setq org-last-changed-timestamp
  9596. (org-insert-time-stamp
  9597. time (or org-time-was-given arg)
  9598. inactive nil nil (list org-end-time-was-given))))
  9599. (message "Timestamp updated"))
  9600. (t
  9601. (setq time (let ((this-command this-command))
  9602. (org-read-date arg 'totime nil nil default-time default-input)))
  9603. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9604. nil nil (list org-end-time-was-given))))))
  9605. ;; FIXME: can we use this for something else, like computing time differences?
  9606. (defun org-get-compact-tod (s)
  9607. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9608. (let* ((t1 (match-string 1 s))
  9609. (h1 (string-to-number (match-string 2 s)))
  9610. (m1 (string-to-number (match-string 3 s)))
  9611. (t2 (and (match-end 4) (match-string 5 s)))
  9612. (h2 (and t2 (string-to-number (match-string 6 s))))
  9613. (m2 (and t2 (string-to-number (match-string 7 s))))
  9614. dh dm)
  9615. (if (not t2)
  9616. t1
  9617. (setq dh (- h2 h1) dm (- m2 m1))
  9618. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9619. (concat t1 "+" (number-to-string dh)
  9620. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9621. (defun org-time-stamp-inactive (&optional arg)
  9622. "Insert an inactive time stamp.
  9623. An inactive time stamp is enclosed in square brackets instead of angle
  9624. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9625. does not link to the calendar and cannot be changed with the S-cursor keys.
  9626. So these are more for recording a certain time/date."
  9627. (interactive "P")
  9628. (org-time-stamp arg 'inactive))
  9629. (defvar org-date-ovl (org-make-overlay 1 1))
  9630. (org-overlay-put org-date-ovl 'face 'org-warning)
  9631. (org-detach-overlay org-date-ovl)
  9632. (defvar org-ans1) ; dynamically scoped parameter
  9633. (defvar org-ans2) ; dynamically scoped parameter
  9634. (defvar org-plain-time-of-day-regexp) ; defined below
  9635. (defvar org-overriding-default-time nil) ; dynamically scoped
  9636. (defvar org-read-date-overlay nil)
  9637. (defvar org-dcst nil) ; dynamically scoped
  9638. (defun org-read-date (&optional with-time to-time from-string prompt
  9639. default-time default-input)
  9640. "Read a date, possibly a time, and make things smooth for the user.
  9641. The prompt will suggest to enter an ISO date, but you can also enter anything
  9642. which will at least partially be understood by `parse-time-string'.
  9643. Unrecognized parts of the date will default to the current day, month, year,
  9644. hour and minute. If this command is called to replace a timestamp at point,
  9645. of to enter the second timestamp of a range, the default time is taken from the
  9646. existing stamp. For example,
  9647. 3-2-5 --> 2003-02-05
  9648. feb 15 --> currentyear-02-15
  9649. sep 12 9 --> 2009-09-12
  9650. 12:45 --> today 12:45
  9651. 22 sept 0:34 --> currentyear-09-22 0:34
  9652. 12 --> currentyear-currentmonth-12
  9653. Fri --> nearest Friday (today or later)
  9654. etc.
  9655. Furthermore you can specify a relative date by giving, as the *first* thing
  9656. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9657. change in days weeks, months, years.
  9658. With a single plus or minus, the date is relative to today. With a double
  9659. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9660. +4d --> four days from today
  9661. +4 --> same as above
  9662. +2w --> two weeks from today
  9663. ++5 --> five days from default date
  9664. The function understands only English month and weekday abbreviations,
  9665. but this can be configured with the variables `parse-time-months' and
  9666. `parse-time-weekdays'.
  9667. While prompting, a calendar is popped up - you can also select the
  9668. date with the mouse (button 1). The calendar shows a period of three
  9669. months. To scroll it to other months, use the keys `>' and `<'.
  9670. If you don't like the calendar, turn it off with
  9671. \(setq org-read-date-popup-calendar nil)
  9672. With optional argument TO-TIME, the date will immediately be converted
  9673. to an internal time.
  9674. With an optional argument WITH-TIME, the prompt will suggest to also
  9675. insert a time. Note that when WITH-TIME is not set, you can still
  9676. enter a time, and this function will inform the calling routine about
  9677. this change. The calling routine may then choose to change the format
  9678. used to insert the time stamp into the buffer to include the time.
  9679. With optional argument FROM-STRING, read from this string instead from
  9680. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9681. the time/date that is used for everything that is not specified by the
  9682. user."
  9683. (require 'parse-time)
  9684. (let* ((org-time-stamp-rounding-minutes
  9685. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9686. (org-dcst org-display-custom-times)
  9687. (ct (org-current-time))
  9688. (def (or org-overriding-default-time default-time ct))
  9689. (defdecode (decode-time def))
  9690. (dummy (progn
  9691. (when (< (nth 2 defdecode) org-extend-today-until)
  9692. (setcar (nthcdr 2 defdecode) -1)
  9693. (setcar (nthcdr 1 defdecode) 59)
  9694. (setq def (apply 'encode-time defdecode)
  9695. defdecode (decode-time def)))))
  9696. (calendar-move-hook nil)
  9697. (calendar-view-diary-initially-flag nil)
  9698. (view-diary-entries-initially nil)
  9699. (calendar-view-holidays-initially-flag nil)
  9700. (view-calendar-holidays-initially nil)
  9701. (timestr (format-time-string
  9702. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9703. (prompt (concat (if prompt (concat prompt " ") "")
  9704. (format "Date+time [%s]: " timestr)))
  9705. ans (org-ans0 "") org-ans1 org-ans2 final)
  9706. (cond
  9707. (from-string (setq ans from-string))
  9708. (org-read-date-popup-calendar
  9709. (save-excursion
  9710. (save-window-excursion
  9711. (calendar)
  9712. (calendar-forward-day (- (time-to-days def)
  9713. (calendar-absolute-from-gregorian
  9714. (calendar-current-date))))
  9715. (org-eval-in-calendar nil t)
  9716. (let* ((old-map (current-local-map))
  9717. (map (copy-keymap calendar-mode-map))
  9718. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9719. (org-defkey map (kbd "RET") 'org-calendar-select)
  9720. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9721. 'org-calendar-select-mouse)
  9722. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9723. 'org-calendar-select-mouse)
  9724. (org-defkey minibuffer-local-map [(meta shift left)]
  9725. (lambda () (interactive)
  9726. (org-eval-in-calendar '(calendar-backward-month 1))))
  9727. (org-defkey minibuffer-local-map [(meta shift right)]
  9728. (lambda () (interactive)
  9729. (org-eval-in-calendar '(calendar-forward-month 1))))
  9730. (org-defkey minibuffer-local-map [(meta shift up)]
  9731. (lambda () (interactive)
  9732. (org-eval-in-calendar '(calendar-backward-year 1))))
  9733. (org-defkey minibuffer-local-map [(meta shift down)]
  9734. (lambda () (interactive)
  9735. (org-eval-in-calendar '(calendar-forward-year 1))))
  9736. (org-defkey minibuffer-local-map [(shift up)]
  9737. (lambda () (interactive)
  9738. (org-eval-in-calendar '(calendar-backward-week 1))))
  9739. (org-defkey minibuffer-local-map [(shift down)]
  9740. (lambda () (interactive)
  9741. (org-eval-in-calendar '(calendar-forward-week 1))))
  9742. (org-defkey minibuffer-local-map [(shift left)]
  9743. (lambda () (interactive)
  9744. (org-eval-in-calendar '(calendar-backward-day 1))))
  9745. (org-defkey minibuffer-local-map [(shift right)]
  9746. (lambda () (interactive)
  9747. (org-eval-in-calendar '(calendar-forward-day 1))))
  9748. (org-defkey minibuffer-local-map ">"
  9749. (lambda () (interactive)
  9750. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9751. (org-defkey minibuffer-local-map "<"
  9752. (lambda () (interactive)
  9753. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9754. (unwind-protect
  9755. (progn
  9756. (use-local-map map)
  9757. (add-hook 'post-command-hook 'org-read-date-display)
  9758. (setq org-ans0 (read-string prompt default-input nil nil))
  9759. ;; org-ans0: from prompt
  9760. ;; org-ans1: from mouse click
  9761. ;; org-ans2: from calendar motion
  9762. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9763. (remove-hook 'post-command-hook 'org-read-date-display)
  9764. (use-local-map old-map)
  9765. (when org-read-date-overlay
  9766. (org-delete-overlay org-read-date-overlay)
  9767. (setq org-read-date-overlay nil)))))))
  9768. (t ; Naked prompt only
  9769. (unwind-protect
  9770. (setq ans (read-string prompt default-input nil timestr))
  9771. (when org-read-date-overlay
  9772. (org-delete-overlay org-read-date-overlay)
  9773. (setq org-read-date-overlay nil)))))
  9774. (setq final (org-read-date-analyze ans def defdecode))
  9775. (if to-time
  9776. (apply 'encode-time final)
  9777. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9778. (format "%04d-%02d-%02d %02d:%02d"
  9779. (nth 5 final) (nth 4 final) (nth 3 final)
  9780. (nth 2 final) (nth 1 final))
  9781. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9782. (defvar def)
  9783. (defvar defdecode)
  9784. (defvar with-time)
  9785. (defun org-read-date-display ()
  9786. "Display the currrent date prompt interpretation in the minibuffer."
  9787. (when org-read-date-display-live
  9788. (when org-read-date-overlay
  9789. (org-delete-overlay org-read-date-overlay))
  9790. (let ((p (point)))
  9791. (end-of-line 1)
  9792. (while (not (equal (buffer-substring
  9793. (max (point-min) (- (point) 4)) (point))
  9794. " "))
  9795. (insert " "))
  9796. (goto-char p))
  9797. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9798. " " (or org-ans1 org-ans2)))
  9799. (org-end-time-was-given nil)
  9800. (f (org-read-date-analyze ans def defdecode))
  9801. (fmts (if org-dcst
  9802. org-time-stamp-custom-formats
  9803. org-time-stamp-formats))
  9804. (fmt (if (or with-time
  9805. (and (boundp 'org-time-was-given) org-time-was-given))
  9806. (cdr fmts)
  9807. (car fmts)))
  9808. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9809. (when (and org-end-time-was-given
  9810. (string-match org-plain-time-of-day-regexp txt))
  9811. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9812. org-end-time-was-given
  9813. (substring txt (match-end 0)))))
  9814. (setq org-read-date-overlay
  9815. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9816. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9817. (defun org-read-date-analyze (ans def defdecode)
  9818. "Analyze the combined answer of the date prompt."
  9819. ;; FIXME: cleanup and comment
  9820. (let (delta deltan deltaw deltadef year month day
  9821. hour minute second wday pm h2 m2 tl wday1
  9822. iso-year iso-weekday iso-week iso-year iso-date)
  9823. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9824. (setq ans "+0"))
  9825. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9826. (setq ans (replace-match "" t t ans)
  9827. deltan (car delta)
  9828. deltaw (nth 1 delta)
  9829. deltadef (nth 2 delta)))
  9830. ;; Check if there is an iso week date in there
  9831. ;; If yes, sore the info and ostpone interpreting it until the rest
  9832. ;; of the parsing is done
  9833. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9834. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9835. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9836. iso-week (string-to-number (match-string 2 ans)))
  9837. (setq ans (replace-match "" t t ans)))
  9838. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9839. (when (string-match
  9840. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9841. (setq year (if (match-end 2)
  9842. (string-to-number (match-string 2 ans))
  9843. (string-to-number (format-time-string "%Y")))
  9844. month (string-to-number (match-string 3 ans))
  9845. day (string-to-number (match-string 4 ans)))
  9846. (if (< year 100) (setq year (+ 2000 year)))
  9847. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9848. t nil ans)))
  9849. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9850. ;; If there is a time with am/pm, and *no* time without it, we convert
  9851. ;; so that matching will be successful.
  9852. (loop for i from 1 to 2 do ; twice, for end time as well
  9853. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9854. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9855. (setq hour (string-to-number (match-string 1 ans))
  9856. minute (if (match-end 3)
  9857. (string-to-number (match-string 3 ans))
  9858. 0)
  9859. pm (equal ?p
  9860. (string-to-char (downcase (match-string 4 ans)))))
  9861. (if (and (= hour 12) (not pm))
  9862. (setq hour 0)
  9863. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9864. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9865. t t ans))))
  9866. ;; Check if a time range is given as a duration
  9867. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9868. (setq hour (string-to-number (match-string 1 ans))
  9869. h2 (+ hour (string-to-number (match-string 3 ans)))
  9870. minute (string-to-number (match-string 2 ans))
  9871. m2 (+ minute (if (match-end 5) (string-to-number
  9872. (match-string 5 ans))0)))
  9873. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9874. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9875. t t ans)))
  9876. ;; Check if there is a time range
  9877. (when (boundp 'org-end-time-was-given)
  9878. (setq org-time-was-given nil)
  9879. (when (and (string-match org-plain-time-of-day-regexp ans)
  9880. (match-end 8))
  9881. (setq org-end-time-was-given (match-string 8 ans))
  9882. (setq ans (concat (substring ans 0 (match-beginning 7))
  9883. (substring ans (match-end 7))))))
  9884. (setq tl (parse-time-string ans)
  9885. day (or (nth 3 tl) (nth 3 defdecode))
  9886. month (or (nth 4 tl)
  9887. (if (and org-read-date-prefer-future
  9888. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9889. (1+ (nth 4 defdecode))
  9890. (nth 4 defdecode)))
  9891. year (or (nth 5 tl)
  9892. (if (and org-read-date-prefer-future
  9893. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9894. (1+ (nth 5 defdecode))
  9895. (nth 5 defdecode)))
  9896. hour (or (nth 2 tl) (nth 2 defdecode))
  9897. minute (or (nth 1 tl) (nth 1 defdecode))
  9898. second (or (nth 0 tl) 0)
  9899. wday (nth 6 tl))
  9900. ;; Special date definitions below
  9901. (cond
  9902. (iso-week
  9903. ;; There was an iso week
  9904. (setq year (or iso-year year)
  9905. day (or iso-weekday wday 1)
  9906. wday nil ; to make sure that the trigger below does not match
  9907. iso-date (calendar-gregorian-from-absolute
  9908. (calendar-absolute-from-iso
  9909. (list iso-week day year))))
  9910. ; FIXME: Should we also push ISO weeks into the future?
  9911. ; (when (and org-read-date-prefer-future
  9912. ; (not iso-year)
  9913. ; (< (calendar-absolute-from-gregorian iso-date)
  9914. ; (time-to-days (current-time))))
  9915. ; (setq year (1+ year)
  9916. ; iso-date (calendar-gregorian-from-absolute
  9917. ; (calendar-absolute-from-iso
  9918. ; (list iso-week day year)))))
  9919. (setq month (car iso-date)
  9920. year (nth 2 iso-date)
  9921. day (nth 1 iso-date)))
  9922. (deltan
  9923. (unless deltadef
  9924. (let ((now (decode-time (current-time))))
  9925. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9926. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9927. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9928. ((equal deltaw "m") (setq month (+ month deltan)))
  9929. ((equal deltaw "y") (setq year (+ year deltan)))))
  9930. ((and wday (not (nth 3 tl)))
  9931. ;; Weekday was given, but no day, so pick that day in the week
  9932. ;; on or after the derived date.
  9933. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9934. (unless (equal wday wday1)
  9935. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9936. (if (and (boundp 'org-time-was-given)
  9937. (nth 2 tl))
  9938. (setq org-time-was-given t))
  9939. (if (< year 100) (setq year (+ 2000 year)))
  9940. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9941. (list second minute hour day month year)))
  9942. (defvar parse-time-weekdays)
  9943. (defun org-read-date-get-relative (s today default)
  9944. "Check string S for special relative date string.
  9945. TODAY and DEFAULT are internal times, for today and for a default.
  9946. Return shift list (N what def-flag)
  9947. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9948. N is the number of WHATs to shift.
  9949. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9950. the DEFAULT date rather than TODAY."
  9951. (when (and
  9952. (string-match
  9953. (concat
  9954. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9955. "\\([0-9]+\\)?"
  9956. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9957. "\\([ \t]\\|$\\)") s)
  9958. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9959. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9960. (string-to-char (substring (match-string 1 s) -1))
  9961. ?+))
  9962. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9963. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9964. (what (if (match-end 3) (match-string 3 s) "d"))
  9965. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9966. (date (if rel default today))
  9967. (wday (nth 6 (decode-time date)))
  9968. delta)
  9969. (if wday1
  9970. (progn
  9971. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9972. (if (= dir ?-) (setq delta (- delta 7)))
  9973. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9974. (list delta "d" rel))
  9975. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9976. (defun org-eval-in-calendar (form &optional keepdate)
  9977. "Eval FORM in the calendar window and return to current window.
  9978. Also, store the cursor date in variable org-ans2."
  9979. (let ((sw (selected-window)))
  9980. (select-window (get-buffer-window "*Calendar*"))
  9981. (eval form)
  9982. (when (and (not keepdate) (calendar-cursor-to-date))
  9983. (let* ((date (calendar-cursor-to-date))
  9984. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9985. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9986. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9987. (select-window sw)))
  9988. (defun org-calendar-select ()
  9989. "Return to `org-read-date' with the date currently selected.
  9990. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9991. (interactive)
  9992. (when (calendar-cursor-to-date)
  9993. (let* ((date (calendar-cursor-to-date))
  9994. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9995. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9996. (if (active-minibuffer-window) (exit-minibuffer))))
  9997. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9998. "Insert a date stamp for the date given by the internal TIME.
  9999. WITH-HM means, use the stamp format that includes the time of the day.
  10000. INACTIVE means use square brackets instead of angular ones, so that the
  10001. stamp will not contribute to the agenda.
  10002. PRE and POST are optional strings to be inserted before and after the
  10003. stamp.
  10004. The command returns the inserted time stamp."
  10005. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10006. stamp)
  10007. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10008. (insert-before-markers (or pre ""))
  10009. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10010. (when (listp extra)
  10011. (setq extra (car extra))
  10012. (if (and (stringp extra)
  10013. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10014. (setq extra (format "-%02d:%02d"
  10015. (string-to-number (match-string 1 extra))
  10016. (string-to-number (match-string 2 extra))))
  10017. (setq extra nil)))
  10018. (when extra
  10019. (backward-char 1)
  10020. (insert-before-markers extra)
  10021. (forward-char 1))
  10022. (insert-before-markers (or post ""))
  10023. (setq org-last-inserted-timestamp stamp)))
  10024. (defun org-toggle-time-stamp-overlays ()
  10025. "Toggle the use of custom time stamp formats."
  10026. (interactive)
  10027. (setq org-display-custom-times (not org-display-custom-times))
  10028. (unless org-display-custom-times
  10029. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10030. (while (setq p (next-single-property-change p 'display))
  10031. (if (and (get-text-property p 'display)
  10032. (eq (get-text-property p 'face) 'org-date))
  10033. (remove-text-properties
  10034. p (setq p (next-single-property-change p 'display))
  10035. '(display t))))
  10036. (set-buffer-modified-p bmp)))
  10037. (if (featurep 'xemacs)
  10038. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10039. (org-restart-font-lock)
  10040. (setq org-table-may-need-update t)
  10041. (if org-display-custom-times
  10042. (message "Time stamps are overlayed with custom format")
  10043. (message "Time stamp overlays removed")))
  10044. (defun org-display-custom-time (beg end)
  10045. "Overlay modified time stamp format over timestamp between BEG and END."
  10046. (let* ((ts (buffer-substring beg end))
  10047. t1 w1 with-hm tf time str w2 (off 0))
  10048. (save-match-data
  10049. (setq t1 (org-parse-time-string ts t))
  10050. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10051. (setq off (- (match-end 0) (match-beginning 0)))))
  10052. (setq end (- end off))
  10053. (setq w1 (- end beg)
  10054. with-hm (and (nth 1 t1) (nth 2 t1))
  10055. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10056. time (org-fix-decoded-time t1)
  10057. str (org-add-props
  10058. (format-time-string
  10059. (substring tf 1 -1) (apply 'encode-time time))
  10060. nil 'mouse-face 'highlight)
  10061. w2 (length str))
  10062. (if (not (= w2 w1))
  10063. (add-text-properties (1+ beg) (+ 2 beg)
  10064. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10065. (if (featurep 'xemacs)
  10066. (progn
  10067. (put-text-property beg end 'invisible t)
  10068. (put-text-property beg end 'end-glyph (make-glyph str)))
  10069. (put-text-property beg end 'display str))))
  10070. (defun org-translate-time (string)
  10071. "Translate all timestamps in STRING to custom format.
  10072. But do this only if the variable `org-display-custom-times' is set."
  10073. (when org-display-custom-times
  10074. (save-match-data
  10075. (let* ((start 0)
  10076. (re org-ts-regexp-both)
  10077. t1 with-hm inactive tf time str beg end)
  10078. (while (setq start (string-match re string start))
  10079. (setq beg (match-beginning 0)
  10080. end (match-end 0)
  10081. t1 (save-match-data
  10082. (org-parse-time-string (substring string beg end) t))
  10083. with-hm (and (nth 1 t1) (nth 2 t1))
  10084. inactive (equal (substring string beg (1+ beg)) "[")
  10085. tf (funcall (if with-hm 'cdr 'car)
  10086. org-time-stamp-custom-formats)
  10087. time (org-fix-decoded-time t1)
  10088. str (format-time-string
  10089. (concat
  10090. (if inactive "[" "<") (substring tf 1 -1)
  10091. (if inactive "]" ">"))
  10092. (apply 'encode-time time))
  10093. string (replace-match str t t string)
  10094. start (+ start (length str)))))))
  10095. string)
  10096. (defun org-fix-decoded-time (time)
  10097. "Set 0 instead of nil for the first 6 elements of time.
  10098. Don't touch the rest."
  10099. (let ((n 0))
  10100. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10101. (defun org-days-to-time (timestamp-string)
  10102. "Difference between TIMESTAMP-STRING and now in days."
  10103. (- (time-to-days (org-time-string-to-time timestamp-string))
  10104. (time-to-days (current-time))))
  10105. (defun org-deadline-close (timestamp-string &optional ndays)
  10106. "Is the time in TIMESTAMP-STRING close to the current date?"
  10107. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10108. (and (< (org-days-to-time timestamp-string) ndays)
  10109. (not (org-entry-is-done-p))))
  10110. (defun org-get-wdays (ts)
  10111. "Get the deadline lead time appropriate for timestring TS."
  10112. (cond
  10113. ((<= org-deadline-warning-days 0)
  10114. ;; 0 or negative, enforce this value no matter what
  10115. (- org-deadline-warning-days))
  10116. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10117. ;; lead time is specified.
  10118. (floor (* (string-to-number (match-string 1 ts))
  10119. (cdr (assoc (match-string 2 ts)
  10120. '(("d" . 1) ("w" . 7)
  10121. ("m" . 30.4) ("y" . 365.25)))))))
  10122. ;; go for the default.
  10123. (t org-deadline-warning-days)))
  10124. (defun org-calendar-select-mouse (ev)
  10125. "Return to `org-read-date' with the date currently selected.
  10126. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10127. (interactive "e")
  10128. (mouse-set-point ev)
  10129. (when (calendar-cursor-to-date)
  10130. (let* ((date (calendar-cursor-to-date))
  10131. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10132. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10133. (if (active-minibuffer-window) (exit-minibuffer))))
  10134. (defun org-check-deadlines (ndays)
  10135. "Check if there are any deadlines due or past due.
  10136. A deadline is considered due if it happens within `org-deadline-warning-days'
  10137. days from today's date. If the deadline appears in an entry marked DONE,
  10138. it is not shown. The prefix arg NDAYS can be used to test that many
  10139. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10140. (interactive "P")
  10141. (let* ((org-warn-days
  10142. (cond
  10143. ((equal ndays '(4)) 100000)
  10144. (ndays (prefix-numeric-value ndays))
  10145. (t (abs org-deadline-warning-days))))
  10146. (case-fold-search nil)
  10147. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10148. (callback
  10149. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10150. (message "%d deadlines past-due or due within %d days"
  10151. (org-occur regexp nil callback)
  10152. org-warn-days)))
  10153. (defun org-check-before-date (date)
  10154. "Check if there are deadlines or scheduled entries before DATE."
  10155. (interactive (list (org-read-date)))
  10156. (let ((case-fold-search nil)
  10157. (regexp (concat "\\<\\(" org-deadline-string
  10158. "\\|" org-scheduled-string
  10159. "\\) *<\\([^>]+\\)>"))
  10160. (callback
  10161. (lambda () (time-less-p
  10162. (org-time-string-to-time (match-string 2))
  10163. (org-time-string-to-time date)))))
  10164. (message "%d entries before %s"
  10165. (org-occur regexp nil callback) date)))
  10166. (defun org-evaluate-time-range (&optional to-buffer)
  10167. "Evaluate a time range by computing the difference between start and end.
  10168. Normally the result is just printed in the echo area, but with prefix arg
  10169. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10170. If the time range is actually in a table, the result is inserted into the
  10171. next column.
  10172. For time difference computation, a year is assumed to be exactly 365
  10173. days in order to avoid rounding problems."
  10174. (interactive "P")
  10175. (or
  10176. (org-clock-update-time-maybe)
  10177. (save-excursion
  10178. (unless (org-at-date-range-p t)
  10179. (goto-char (point-at-bol))
  10180. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10181. (if (not (org-at-date-range-p t))
  10182. (error "Not at a time-stamp range, and none found in current line")))
  10183. (let* ((ts1 (match-string 1))
  10184. (ts2 (match-string 2))
  10185. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10186. (match-end (match-end 0))
  10187. (time1 (org-time-string-to-time ts1))
  10188. (time2 (org-time-string-to-time ts2))
  10189. (t1 (time-to-seconds time1))
  10190. (t2 (time-to-seconds time2))
  10191. (diff (abs (- t2 t1)))
  10192. (negative (< (- t2 t1) 0))
  10193. ;; (ys (floor (* 365 24 60 60)))
  10194. (ds (* 24 60 60))
  10195. (hs (* 60 60))
  10196. (fy "%dy %dd %02d:%02d")
  10197. (fy1 "%dy %dd")
  10198. (fd "%dd %02d:%02d")
  10199. (fd1 "%dd")
  10200. (fh "%02d:%02d")
  10201. y d h m align)
  10202. (if havetime
  10203. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10204. y 0
  10205. d (floor (/ diff ds)) diff (mod diff ds)
  10206. h (floor (/ diff hs)) diff (mod diff hs)
  10207. m (floor (/ diff 60)))
  10208. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10209. y 0
  10210. d (floor (+ (/ diff ds) 0.5))
  10211. h 0 m 0))
  10212. (if (not to-buffer)
  10213. (message "%s" (org-make-tdiff-string y d h m))
  10214. (if (org-at-table-p)
  10215. (progn
  10216. (goto-char match-end)
  10217. (setq align t)
  10218. (and (looking-at " *|") (goto-char (match-end 0))))
  10219. (goto-char match-end))
  10220. (if (looking-at
  10221. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10222. (replace-match ""))
  10223. (if negative (insert " -"))
  10224. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10225. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10226. (insert " " (format fh h m))))
  10227. (if align (org-table-align))
  10228. (message "Time difference inserted")))))
  10229. (defun org-make-tdiff-string (y d h m)
  10230. (let ((fmt "")
  10231. (l nil))
  10232. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10233. l (push y l)))
  10234. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10235. l (push d l)))
  10236. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10237. l (push h l)))
  10238. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10239. l (push m l)))
  10240. (apply 'format fmt (nreverse l))))
  10241. (defun org-time-string-to-time (s)
  10242. (apply 'encode-time (org-parse-time-string s)))
  10243. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10244. "Convert a time stamp to an absolute day number.
  10245. If there is a specifyer for a cyclic time stamp, get the closest date to
  10246. DAYNR.
  10247. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10248. (cond
  10249. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10250. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10251. daynr
  10252. (+ daynr 1000)))
  10253. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10254. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10255. (time-to-days (current-time))) (match-string 0 s)
  10256. prefer show-all))
  10257. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10258. (defun org-days-to-iso-week (days)
  10259. "Return the iso week number."
  10260. (require 'cal-iso)
  10261. (car (calendar-iso-from-absolute days)))
  10262. (defun org-small-year-to-year (year)
  10263. "Convert 2-digit years into 4-digit years.
  10264. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10265. The year 2000 cannot be abbreviated. Any year lager than 99
  10266. is retrned unchanged."
  10267. (if (< year 38)
  10268. (setq year (+ 2000 year))
  10269. (if (< year 100)
  10270. (setq year (+ 1900 year))))
  10271. year)
  10272. (defun org-time-from-absolute (d)
  10273. "Return the time corresponding to date D.
  10274. D may be an absolute day number, or a calendar-type list (month day year)."
  10275. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10276. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10277. (defun org-calendar-holiday ()
  10278. "List of holidays, for Diary display in Org-mode."
  10279. (require 'holidays)
  10280. (let ((hl (funcall
  10281. (if (fboundp 'calendar-check-holidays)
  10282. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10283. (if hl (mapconcat 'identity hl "; "))))
  10284. (defun org-diary-sexp-entry (sexp entry date)
  10285. "Process a SEXP diary ENTRY for DATE."
  10286. (require 'diary-lib)
  10287. (let ((result (if calendar-debug-sexp
  10288. (let ((stack-trace-on-error t))
  10289. (eval (car (read-from-string sexp))))
  10290. (condition-case nil
  10291. (eval (car (read-from-string sexp)))
  10292. (error
  10293. (beep)
  10294. (message "Bad sexp at line %d in %s: %s"
  10295. (org-current-line)
  10296. (buffer-file-name) sexp)
  10297. (sleep-for 2))))))
  10298. (cond ((stringp result) result)
  10299. ((and (consp result)
  10300. (stringp (cdr result))) (cdr result))
  10301. (result entry)
  10302. (t nil))))
  10303. (defun org-diary-to-ical-string (frombuf)
  10304. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10305. This uses the icalendar.el library."
  10306. (let* ((tmpdir (if (featurep 'xemacs)
  10307. (temp-directory)
  10308. temporary-file-directory))
  10309. (tmpfile (make-temp-name
  10310. (expand-file-name "orgics" tmpdir)))
  10311. buf rtn b e)
  10312. (save-excursion
  10313. (set-buffer frombuf)
  10314. (icalendar-export-region (point-min) (point-max) tmpfile)
  10315. (setq buf (find-buffer-visiting tmpfile))
  10316. (set-buffer buf)
  10317. (goto-char (point-min))
  10318. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10319. (setq b (match-beginning 0)))
  10320. (goto-char (point-max))
  10321. (if (re-search-backward "^END:VEVENT" nil t)
  10322. (setq e (match-end 0)))
  10323. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10324. (kill-buffer buf)
  10325. (delete-file tmpfile)
  10326. rtn))
  10327. (defun org-closest-date (start current change prefer show-all)
  10328. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10329. When PREFER is `past' return a date that is either CURRENT or past.
  10330. When PREFER is `future', return a date that is either CURRENT or future.
  10331. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10332. ;; Make the proper lists from the dates
  10333. (catch 'exit
  10334. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10335. dn dw sday cday n1 n2
  10336. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10337. (setq start (org-date-to-gregorian start)
  10338. current (org-date-to-gregorian
  10339. (if show-all
  10340. current
  10341. (time-to-days (current-time))))
  10342. sday (calendar-absolute-from-gregorian start)
  10343. cday (calendar-absolute-from-gregorian current))
  10344. (if (<= cday sday) (throw 'exit sday))
  10345. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10346. (setq dn (string-to-number (match-string 1 change))
  10347. dw (cdr (assoc (match-string 2 change) a1)))
  10348. (error "Invalid change specifyer: %s" change))
  10349. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10350. (cond
  10351. ((eq dw 'day)
  10352. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10353. n2 (+ n1 dn)))
  10354. ((eq dw 'year)
  10355. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10356. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10357. (setq date1 (list m d y1)
  10358. n1 (calendar-absolute-from-gregorian date1)
  10359. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10360. n2 (calendar-absolute-from-gregorian date2)))
  10361. ((eq dw 'month)
  10362. ;; approx number of month between the two dates
  10363. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10364. ;; How often does dn fit in there?
  10365. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10366. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10367. m (+ m nm)
  10368. ny (floor (/ m 12))
  10369. y (+ y ny)
  10370. m (- m (* ny 12)))
  10371. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10372. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10373. (setq m2 (+ m dn) y2 y)
  10374. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10375. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10376. (while (<= n2 cday)
  10377. (setq n1 n2 m m2 y y2)
  10378. (setq m2 (+ m dn) y2 y)
  10379. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10380. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10381. (if show-all
  10382. (cond
  10383. ((eq prefer 'past) n1)
  10384. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10385. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10386. (cond
  10387. ((eq prefer 'past) n1)
  10388. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10389. (t (if (= cday n1) n1 n2)))))))
  10390. (defun org-date-to-gregorian (date)
  10391. "Turn any specification of DATE into a gregorian date for the calendar."
  10392. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10393. ((and (listp date) (= (length date) 3)) date)
  10394. ((stringp date)
  10395. (setq date (org-parse-time-string date))
  10396. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10397. ((listp date)
  10398. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10399. (defun org-parse-time-string (s &optional nodefault)
  10400. "Parse the standard Org-mode time string.
  10401. This should be a lot faster than the normal `parse-time-string'.
  10402. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10403. hour and minute fields will be nil if not given."
  10404. (if (string-match org-ts-regexp0 s)
  10405. (list 0
  10406. (if (or (match-beginning 8) (not nodefault))
  10407. (string-to-number (or (match-string 8 s) "0")))
  10408. (if (or (match-beginning 7) (not nodefault))
  10409. (string-to-number (or (match-string 7 s) "0")))
  10410. (string-to-number (match-string 4 s))
  10411. (string-to-number (match-string 3 s))
  10412. (string-to-number (match-string 2 s))
  10413. nil nil nil)
  10414. (make-list 9 0)))
  10415. (defun org-timestamp-up (&optional arg)
  10416. "Increase the date item at the cursor by one.
  10417. If the cursor is on the year, change the year. If it is on the month or
  10418. the day, change that.
  10419. With prefix ARG, change by that many units."
  10420. (interactive "p")
  10421. (org-timestamp-change (prefix-numeric-value arg)))
  10422. (defun org-timestamp-down (&optional arg)
  10423. "Decrease the date item at the cursor by one.
  10424. If the cursor is on the year, change the year. If it is on the month or
  10425. the day, change that.
  10426. With prefix ARG, change by that many units."
  10427. (interactive "p")
  10428. (org-timestamp-change (- (prefix-numeric-value arg))))
  10429. (defun org-timestamp-up-day (&optional arg)
  10430. "Increase the date in the time stamp by one day.
  10431. With prefix ARG, change that many days."
  10432. (interactive "p")
  10433. (if (and (not (org-at-timestamp-p t))
  10434. (org-on-heading-p))
  10435. (org-todo 'up)
  10436. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10437. (defun org-timestamp-down-day (&optional arg)
  10438. "Decrease the date in the time stamp by one day.
  10439. With prefix ARG, change that many days."
  10440. (interactive "p")
  10441. (if (and (not (org-at-timestamp-p t))
  10442. (org-on-heading-p))
  10443. (org-todo 'down)
  10444. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10445. (defun org-at-timestamp-p (&optional inactive-ok)
  10446. "Determine if the cursor is in or at a timestamp."
  10447. (interactive)
  10448. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10449. (pos (point))
  10450. (ans (or (looking-at tsr)
  10451. (save-excursion
  10452. (skip-chars-backward "^[<\n\r\t")
  10453. (if (> (point) (point-min)) (backward-char 1))
  10454. (and (looking-at tsr)
  10455. (> (- (match-end 0) pos) -1))))))
  10456. (and ans
  10457. (boundp 'org-ts-what)
  10458. (setq org-ts-what
  10459. (cond
  10460. ((= pos (match-beginning 0)) 'bracket)
  10461. ((= pos (1- (match-end 0))) 'bracket)
  10462. ((org-pos-in-match-range pos 2) 'year)
  10463. ((org-pos-in-match-range pos 3) 'month)
  10464. ((org-pos-in-match-range pos 7) 'hour)
  10465. ((org-pos-in-match-range pos 8) 'minute)
  10466. ((or (org-pos-in-match-range pos 4)
  10467. (org-pos-in-match-range pos 5)) 'day)
  10468. ((and (> pos (or (match-end 8) (match-end 5)))
  10469. (< pos (match-end 0)))
  10470. (- pos (or (match-end 8) (match-end 5))))
  10471. (t 'day))))
  10472. ans))
  10473. (defun org-toggle-timestamp-type ()
  10474. "Toggle the type (<active> or [inactive]) of a time stamp."
  10475. (interactive)
  10476. (when (org-at-timestamp-p t)
  10477. (let ((beg (match-beginning 0)) (end (match-end 0))
  10478. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10479. (save-excursion
  10480. (goto-char beg)
  10481. (while (re-search-forward "[][<>]" end t)
  10482. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10483. t t)))
  10484. (message "Timestamp is now %sactive"
  10485. (if (equal (char-after beg) ?<) "" "in")))))
  10486. (defun org-timestamp-change (n &optional what)
  10487. "Change the date in the time stamp at point.
  10488. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10489. `year', `minute', `second'. If WHAT is not given, the cursor position
  10490. in the timestamp determines what will be changed."
  10491. (let ((pos (point))
  10492. with-hm inactive
  10493. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10494. org-ts-what
  10495. extra rem
  10496. ts time time0)
  10497. (if (not (org-at-timestamp-p t))
  10498. (error "Not at a timestamp"))
  10499. (if (and (not what) (eq org-ts-what 'bracket))
  10500. (org-toggle-timestamp-type)
  10501. (if (and (not what) (not (eq org-ts-what 'day))
  10502. org-display-custom-times
  10503. (get-text-property (point) 'display)
  10504. (not (get-text-property (1- (point)) 'display)))
  10505. (setq org-ts-what 'day))
  10506. (setq org-ts-what (or what org-ts-what)
  10507. inactive (= (char-after (match-beginning 0)) ?\[)
  10508. ts (match-string 0))
  10509. (replace-match "")
  10510. (if (string-match
  10511. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10512. ts)
  10513. (setq extra (match-string 1 ts)))
  10514. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10515. (setq with-hm t))
  10516. (setq time0 (org-parse-time-string ts))
  10517. (when (and (eq org-ts-what 'minute)
  10518. (eq current-prefix-arg nil))
  10519. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10520. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10521. (setcar (cdr time0) (+ (nth 1 time0)
  10522. (if (> n 0) (- rem) (- dm rem))))))
  10523. (setq time
  10524. (encode-time (or (car time0) 0)
  10525. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10526. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10527. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10528. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10529. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10530. (nthcdr 6 time0)))
  10531. (when (integerp org-ts-what)
  10532. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10533. (if (eq what 'calendar)
  10534. (let ((cal-date (org-get-date-from-calendar)))
  10535. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10536. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10537. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10538. (setcar time0 (or (car time0) 0))
  10539. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10540. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10541. (setq time (apply 'encode-time time0))))
  10542. (setq org-last-changed-timestamp
  10543. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10544. (org-clock-update-time-maybe)
  10545. (goto-char pos)
  10546. ;; Try to recenter the calendar window, if any
  10547. (if (and org-calendar-follow-timestamp-change
  10548. (get-buffer-window "*Calendar*" t)
  10549. (memq org-ts-what '(day month year)))
  10550. (org-recenter-calendar (time-to-days time))))))
  10551. (defun org-modify-ts-extra (s pos n dm)
  10552. "Change the different parts of the lead-time and repeat fields in timestamp."
  10553. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10554. ng h m new rem)
  10555. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10556. (cond
  10557. ((or (org-pos-in-match-range pos 2)
  10558. (org-pos-in-match-range pos 3))
  10559. (setq m (string-to-number (match-string 3 s))
  10560. h (string-to-number (match-string 2 s)))
  10561. (if (org-pos-in-match-range pos 2)
  10562. (setq h (+ h n))
  10563. (setq n (* dm (org-no-warnings (signum n))))
  10564. (when (not (= 0 (setq rem (% m dm))))
  10565. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10566. (setq m (+ m n)))
  10567. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10568. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10569. (setq h (min 24 (max 0 h)))
  10570. (setq ng 1 new (format "-%02d:%02d" h m)))
  10571. ((org-pos-in-match-range pos 6)
  10572. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10573. ((org-pos-in-match-range pos 5)
  10574. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10575. ((org-pos-in-match-range pos 9)
  10576. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10577. ((org-pos-in-match-range pos 8)
  10578. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10579. (when ng
  10580. (setq s (concat
  10581. (substring s 0 (match-beginning ng))
  10582. new
  10583. (substring s (match-end ng))))))
  10584. s))
  10585. (defun org-recenter-calendar (date)
  10586. "If the calendar is visible, recenter it to DATE."
  10587. (let* ((win (selected-window))
  10588. (cwin (get-buffer-window "*Calendar*" t))
  10589. (calendar-move-hook nil))
  10590. (when cwin
  10591. (select-window cwin)
  10592. (calendar-goto-date (if (listp date) date
  10593. (calendar-gregorian-from-absolute date)))
  10594. (select-window win))))
  10595. (defun org-goto-calendar (&optional arg)
  10596. "Go to the Emacs calendar at the current date.
  10597. If there is a time stamp in the current line, go to that date.
  10598. A prefix ARG can be used to force the current date."
  10599. (interactive "P")
  10600. (let ((tsr org-ts-regexp) diff
  10601. (calendar-move-hook nil)
  10602. (calendar-view-holidays-initially-flag nil)
  10603. (view-calendar-holidays-initially nil)
  10604. (calendar-view-diary-initially-flag nil)
  10605. (view-diary-entries-initially nil))
  10606. (if (or (org-at-timestamp-p)
  10607. (save-excursion
  10608. (beginning-of-line 1)
  10609. (looking-at (concat ".*" tsr))))
  10610. (let ((d1 (time-to-days (current-time)))
  10611. (d2 (time-to-days
  10612. (org-time-string-to-time (match-string 1)))))
  10613. (setq diff (- d2 d1))))
  10614. (calendar)
  10615. (calendar-goto-today)
  10616. (if (and diff (not arg)) (calendar-forward-day diff))))
  10617. (defun org-get-date-from-calendar ()
  10618. "Return a list (month day year) of date at point in calendar."
  10619. (with-current-buffer "*Calendar*"
  10620. (save-match-data
  10621. (calendar-cursor-to-date))))
  10622. (defun org-date-from-calendar ()
  10623. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10624. If there is already a time stamp at the cursor position, update it."
  10625. (interactive)
  10626. (if (org-at-timestamp-p t)
  10627. (org-timestamp-change 0 'calendar)
  10628. (let ((cal-date (org-get-date-from-calendar)))
  10629. (org-insert-time-stamp
  10630. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10631. (defun org-minutes-to-hh:mm-string (m)
  10632. "Compute H:MM from a number of minutes."
  10633. (let ((h (/ m 60)))
  10634. (setq m (- m (* 60 h)))
  10635. (format org-time-clocksum-format h m)))
  10636. (defun org-hh:mm-string-to-minutes (s)
  10637. "Convert a string H:MM to a number of minutes."
  10638. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10639. (+ (* (string-to-number (match-string 1 s)) 60)
  10640. (string-to-number (match-string 2 s)))
  10641. 0))
  10642. ;;;; Agenda files
  10643. ;;;###autoload
  10644. (defun org-iswitchb (&optional arg)
  10645. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10646. With a prefix argument, restrict available to files.
  10647. With two prefix arguments, restrict available buffers to agenda files.
  10648. Due to some yet unresolved reason, the global function
  10649. `iswitchb-mode' needs to be active for this function to work."
  10650. (interactive "P")
  10651. (require 'iswitchb)
  10652. (let ((enabled iswitchb-mode) blist)
  10653. (or enabled (iswitchb-mode 1))
  10654. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10655. ((equal arg '(16)) (org-buffer-list 'agenda))
  10656. (t (org-buffer-list))))
  10657. (unwind-protect
  10658. (let ((iswitchb-make-buflist-hook
  10659. (lambda ()
  10660. (setq iswitchb-temp-buflist
  10661. (mapcar 'buffer-name blist)))))
  10662. (switch-to-buffer
  10663. (iswitchb-read-buffer
  10664. "Switch-to: " nil t))
  10665. (or enabled (iswitchb-mode -1))))))
  10666. (defun org-buffer-list (&optional predicate exclude-tmp)
  10667. "Return a list of Org buffers.
  10668. PREDICATE can be `export', `files' or `agenda'.
  10669. export restrict the list to Export buffers.
  10670. files restrict the list to buffers visiting Org files.
  10671. agenda restrict the list to buffers visiting agenda files.
  10672. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10673. (let* ((bfn nil)
  10674. (agenda-files (and (eq predicate 'agenda)
  10675. (mapcar 'file-truename (org-agenda-files t))))
  10676. (filter
  10677. (cond
  10678. ((eq predicate 'files)
  10679. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10680. ((eq predicate 'export)
  10681. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10682. ((eq predicate 'agenda)
  10683. (lambda (b)
  10684. (with-current-buffer b
  10685. (and (eq major-mode 'org-mode)
  10686. (setq bfn (buffer-file-name b))
  10687. (member (file-truename bfn) agenda-files)))))
  10688. (t (lambda (b) (with-current-buffer b
  10689. (or (eq major-mode 'org-mode)
  10690. (string-match "\*Org .*Export"
  10691. (buffer-name b)))))))))
  10692. (delq nil
  10693. (mapcar
  10694. (lambda(b)
  10695. (if (and (funcall filter b)
  10696. (or (not exclude-tmp)
  10697. (not (string-match "tmp" (buffer-name b)))))
  10698. b
  10699. nil))
  10700. (buffer-list)))))
  10701. (defun org-agenda-files (&optional unrestricted archives)
  10702. "Get the list of agenda files.
  10703. Optional UNRESTRICTED means return the full list even if a restriction
  10704. is currently in place.
  10705. When ARCHIVES is t, include all archive files hat are really being
  10706. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10707. `org-agenda-archives-mode' is t."
  10708. (let ((files
  10709. (cond
  10710. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10711. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10712. ((listp org-agenda-files) org-agenda-files)
  10713. (t (error "Invalid value of `org-agenda-files'")))))
  10714. (setq files (apply 'append
  10715. (mapcar (lambda (f)
  10716. (if (file-directory-p f)
  10717. (directory-files
  10718. f t org-agenda-file-regexp)
  10719. (list f)))
  10720. files)))
  10721. (when org-agenda-skip-unavailable-files
  10722. (setq files (delq nil
  10723. (mapcar (function
  10724. (lambda (file)
  10725. (and (file-readable-p file) file)))
  10726. files))))
  10727. (when (or (eq archives t)
  10728. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10729. (setq files (org-add-archive-files files)))
  10730. files))
  10731. (defun org-edit-agenda-file-list ()
  10732. "Edit the list of agenda files.
  10733. Depending on setup, this either uses customize to edit the variable
  10734. `org-agenda-files', or it visits the file that is holding the list. In the
  10735. latter case, the buffer is set up in a way that saving it automatically kills
  10736. the buffer and restores the previous window configuration."
  10737. (interactive)
  10738. (if (stringp org-agenda-files)
  10739. (let ((cw (current-window-configuration)))
  10740. (find-file org-agenda-files)
  10741. (org-set-local 'org-window-configuration cw)
  10742. (org-add-hook 'after-save-hook
  10743. (lambda ()
  10744. (set-window-configuration
  10745. (prog1 org-window-configuration
  10746. (kill-buffer (current-buffer))))
  10747. (org-install-agenda-files-menu)
  10748. (message "New agenda file list installed"))
  10749. nil 'local)
  10750. (message "%s" (substitute-command-keys
  10751. "Edit list and finish with \\[save-buffer]")))
  10752. (customize-variable 'org-agenda-files)))
  10753. (defun org-store-new-agenda-file-list (list)
  10754. "Set new value for the agenda file list and save it correcly."
  10755. (if (stringp org-agenda-files)
  10756. (let ((f org-agenda-files) b)
  10757. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10758. (with-temp-file f
  10759. (insert (mapconcat 'identity list "\n") "\n")))
  10760. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10761. (setq org-agenda-files list)
  10762. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10763. (defun org-read-agenda-file-list ()
  10764. "Read the list of agenda files from a file."
  10765. (when (file-directory-p org-agenda-files)
  10766. (error "`org-agenda-files' cannot be a single directory"))
  10767. (when (stringp org-agenda-files)
  10768. (with-temp-buffer
  10769. (insert-file-contents org-agenda-files)
  10770. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10771. ;;;###autoload
  10772. (defun org-cycle-agenda-files ()
  10773. "Cycle through the files in `org-agenda-files'.
  10774. If the current buffer visits an agenda file, find the next one in the list.
  10775. If the current buffer does not, find the first agenda file."
  10776. (interactive)
  10777. (let* ((fs (org-agenda-files t))
  10778. (files (append fs (list (car fs))))
  10779. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10780. file)
  10781. (unless files (error "No agenda files"))
  10782. (catch 'exit
  10783. (while (setq file (pop files))
  10784. (if (equal (file-truename file) tcf)
  10785. (when (car files)
  10786. (find-file (car files))
  10787. (throw 'exit t))))
  10788. (find-file (car fs)))
  10789. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10790. (defun org-agenda-file-to-front (&optional to-end)
  10791. "Move/add the current file to the top of the agenda file list.
  10792. If the file is not present in the list, it is added to the front. If it is
  10793. present, it is moved there. With optional argument TO-END, add/move to the
  10794. end of the list."
  10795. (interactive "P")
  10796. (let ((org-agenda-skip-unavailable-files nil)
  10797. (file-alist (mapcar (lambda (x)
  10798. (cons (file-truename x) x))
  10799. (org-agenda-files t)))
  10800. (ctf (file-truename buffer-file-name))
  10801. x had)
  10802. (setq x (assoc ctf file-alist) had x)
  10803. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10804. (if to-end
  10805. (setq file-alist (append (delq x file-alist) (list x)))
  10806. (setq file-alist (cons x (delq x file-alist))))
  10807. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10808. (org-install-agenda-files-menu)
  10809. (message "File %s to %s of agenda file list"
  10810. (if had "moved" "added") (if to-end "end" "front"))))
  10811. (defun org-remove-file (&optional file)
  10812. "Remove current file from the list of files in variable `org-agenda-files'.
  10813. These are the files which are being checked for agenda entries.
  10814. Optional argument FILE means, use this file instead of the current."
  10815. (interactive)
  10816. (let* ((org-agenda-skip-unavailable-files nil)
  10817. (file (or file buffer-file-name))
  10818. (true-file (file-truename file))
  10819. (afile (abbreviate-file-name file))
  10820. (files (delq nil (mapcar
  10821. (lambda (x)
  10822. (if (equal true-file
  10823. (file-truename x))
  10824. nil x))
  10825. (org-agenda-files t)))))
  10826. (if (not (= (length files) (length (org-agenda-files t))))
  10827. (progn
  10828. (org-store-new-agenda-file-list files)
  10829. (org-install-agenda-files-menu)
  10830. (message "Removed file: %s" afile))
  10831. (message "File was not in list: %s (not removed)" afile))))
  10832. (defun org-file-menu-entry (file)
  10833. (vector file (list 'find-file file) t))
  10834. (defun org-check-agenda-file (file)
  10835. "Make sure FILE exists. If not, ask user what to do."
  10836. (when (not (file-exists-p file))
  10837. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10838. (abbreviate-file-name file))
  10839. (let ((r (downcase (read-char-exclusive))))
  10840. (cond
  10841. ((equal r ?r)
  10842. (org-remove-file file)
  10843. (throw 'nextfile t))
  10844. (t (error "Abort"))))))
  10845. (defun org-get-agenda-file-buffer (file)
  10846. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10847. it to the list of buffers which might be released later."
  10848. (let ((buf (org-find-base-buffer-visiting file)))
  10849. (if buf
  10850. buf ; just return it
  10851. ;; Make a new buffer and remember it
  10852. (setq buf (find-file-noselect file))
  10853. (if buf (push buf org-agenda-new-buffers))
  10854. buf)))
  10855. (defun org-release-buffers (blist)
  10856. "Release all buffers in list, asking the user for confirmation when needed.
  10857. When a buffer is unmodified, it is just killed. When modified, it is saved
  10858. \(if the user agrees) and then killed."
  10859. (let (buf file)
  10860. (while (setq buf (pop blist))
  10861. (setq file (buffer-file-name buf))
  10862. (when (and (buffer-modified-p buf)
  10863. file
  10864. (y-or-n-p (format "Save file %s? " file)))
  10865. (with-current-buffer buf (save-buffer)))
  10866. (kill-buffer buf))))
  10867. (defun org-prepare-agenda-buffers (files)
  10868. "Create buffers for all agenda files, protect archived trees and comments."
  10869. (interactive)
  10870. (let ((pa '(:org-archived t))
  10871. (pc '(:org-comment t))
  10872. (pall '(:org-archived t :org-comment t))
  10873. (inhibit-read-only t)
  10874. (rea (concat ":" org-archive-tag ":"))
  10875. bmp file re)
  10876. (save-excursion
  10877. (save-restriction
  10878. (while (setq file (pop files))
  10879. (if (bufferp file)
  10880. (set-buffer file)
  10881. (org-check-agenda-file file)
  10882. (set-buffer (org-get-agenda-file-buffer file)))
  10883. (widen)
  10884. (setq bmp (buffer-modified-p))
  10885. (org-refresh-category-properties)
  10886. (setq org-todo-keywords-for-agenda
  10887. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10888. (setq org-done-keywords-for-agenda
  10889. (append org-done-keywords-for-agenda org-done-keywords))
  10890. (setq org-todo-keyword-alist-for-agenda
  10891. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10892. (setq org-tag-alist-for-agenda
  10893. (append org-tag-alist-for-agenda org-tag-alist))
  10894. (save-excursion
  10895. (remove-text-properties (point-min) (point-max) pall)
  10896. (when org-agenda-skip-archived-trees
  10897. (goto-char (point-min))
  10898. (while (re-search-forward rea nil t)
  10899. (if (org-on-heading-p t)
  10900. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10901. (goto-char (point-min))
  10902. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10903. (while (re-search-forward re nil t)
  10904. (add-text-properties
  10905. (match-beginning 0) (org-end-of-subtree t) pc)))
  10906. (set-buffer-modified-p bmp))))
  10907. (setq org-todo-keyword-alist-for-agenda
  10908. (org-uniquify org-todo-keyword-alist-for-agenda)
  10909. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10910. ;;;; Embedded LaTeX
  10911. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10912. "Keymap for the minor `org-cdlatex-mode'.")
  10913. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10914. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10915. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10916. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10917. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10918. (defvar org-cdlatex-texmathp-advice-is-done nil
  10919. "Flag remembering if we have applied the advice to texmathp already.")
  10920. (define-minor-mode org-cdlatex-mode
  10921. "Toggle the minor `org-cdlatex-mode'.
  10922. This mode supports entering LaTeX environment and math in LaTeX fragments
  10923. in Org-mode.
  10924. \\{org-cdlatex-mode-map}"
  10925. nil " OCDL" nil
  10926. (when org-cdlatex-mode (require 'cdlatex))
  10927. (unless org-cdlatex-texmathp-advice-is-done
  10928. (setq org-cdlatex-texmathp-advice-is-done t)
  10929. (defadvice texmathp (around org-math-always-on activate)
  10930. "Always return t in org-mode buffers.
  10931. This is because we want to insert math symbols without dollars even outside
  10932. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10933. en embedded LaTeX fragement, let texmathp do its job.
  10934. \\[org-cdlatex-mode-map]"
  10935. (interactive)
  10936. (let (p)
  10937. (cond
  10938. ((not (org-mode-p)) ad-do-it)
  10939. ((eq this-command 'cdlatex-math-symbol)
  10940. (setq ad-return-value t
  10941. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10942. (t
  10943. (let ((p (org-inside-LaTeX-fragment-p)))
  10944. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10945. (setq ad-return-value t
  10946. texmathp-why '("Org-mode embedded math" . 0))
  10947. (if p ad-do-it)))))))))
  10948. (defun turn-on-org-cdlatex ()
  10949. "Unconditionally turn on `org-cdlatex-mode'."
  10950. (org-cdlatex-mode 1))
  10951. (defun org-inside-LaTeX-fragment-p ()
  10952. "Test if point is inside a LaTeX fragment.
  10953. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10954. sequence appearing also before point.
  10955. Even though the matchers for math are configurable, this function assumes
  10956. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10957. delimiters are skipped when they have been removed by customization.
  10958. The return value is nil, or a cons cell with the delimiter and
  10959. and the position of this delimiter.
  10960. This function does a reasonably good job, but can locally be fooled by
  10961. for example currency specifications. For example it will assume being in
  10962. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10963. fragments that are properly closed, but during editing, we have to live
  10964. with the uncertainty caused by missing closing delimiters. This function
  10965. looks only before point, not after."
  10966. (catch 'exit
  10967. (let ((pos (point))
  10968. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10969. (lim (progn
  10970. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10971. (point)))
  10972. dd-on str (start 0) m re)
  10973. (goto-char pos)
  10974. (when dodollar
  10975. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10976. re (nth 1 (assoc "$" org-latex-regexps)))
  10977. (while (string-match re str start)
  10978. (cond
  10979. ((= (match-end 0) (length str))
  10980. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10981. ((= (match-end 0) (- (length str) 5))
  10982. (throw 'exit nil))
  10983. (t (setq start (match-end 0))))))
  10984. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10985. (goto-char pos)
  10986. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10987. (and (match-beginning 2) (throw 'exit nil))
  10988. ;; count $$
  10989. (while (re-search-backward "\\$\\$" lim t)
  10990. (setq dd-on (not dd-on)))
  10991. (goto-char pos)
  10992. (if dd-on (cons "$$" m))))))
  10993. (defun org-try-cdlatex-tab ()
  10994. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10995. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10996. - inside a LaTeX fragment, or
  10997. - after the first word in a line, where an abbreviation expansion could
  10998. insert a LaTeX environment."
  10999. (when org-cdlatex-mode
  11000. (cond
  11001. ((save-excursion
  11002. (skip-chars-backward "a-zA-Z0-9*")
  11003. (skip-chars-backward " \t")
  11004. (bolp))
  11005. (cdlatex-tab) t)
  11006. ((org-inside-LaTeX-fragment-p)
  11007. (cdlatex-tab) t)
  11008. (t nil))))
  11009. (defun org-cdlatex-underscore-caret (&optional arg)
  11010. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11011. Revert to the normal definition outside of these fragments."
  11012. (interactive "P")
  11013. (if (org-inside-LaTeX-fragment-p)
  11014. (call-interactively 'cdlatex-sub-superscript)
  11015. (let (org-cdlatex-mode)
  11016. (call-interactively (key-binding (vector last-input-event))))))
  11017. (defun org-cdlatex-math-modify (&optional arg)
  11018. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11019. Revert to the normal definition outside of these fragments."
  11020. (interactive "P")
  11021. (if (org-inside-LaTeX-fragment-p)
  11022. (call-interactively 'cdlatex-math-modify)
  11023. (let (org-cdlatex-mode)
  11024. (call-interactively (key-binding (vector last-input-event))))))
  11025. (defvar org-latex-fragment-image-overlays nil
  11026. "List of overlays carrying the images of latex fragments.")
  11027. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11028. (defun org-remove-latex-fragment-image-overlays ()
  11029. "Remove all overlays with LaTeX fragment images in current buffer."
  11030. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11031. (setq org-latex-fragment-image-overlays nil))
  11032. (defun org-preview-latex-fragment (&optional subtree)
  11033. "Preview the LaTeX fragment at point, or all locally or globally.
  11034. If the cursor is in a LaTeX fragment, create the image and overlay
  11035. it over the source code. If there is no fragment at point, display
  11036. all fragments in the current text, from one headline to the next. With
  11037. prefix SUBTREE, display all fragments in the current subtree. With a
  11038. double prefix `C-u C-u', or when the cursor is before the first headline,
  11039. display all fragments in the buffer.
  11040. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11041. (interactive "P")
  11042. (org-remove-latex-fragment-image-overlays)
  11043. (save-excursion
  11044. (save-restriction
  11045. (let (beg end at msg)
  11046. (cond
  11047. ((or (equal subtree '(16))
  11048. (not (save-excursion
  11049. (re-search-backward (concat "^" outline-regexp) nil t))))
  11050. (setq beg (point-min) end (point-max)
  11051. msg "Creating images for buffer...%s"))
  11052. ((equal subtree '(4))
  11053. (org-back-to-heading)
  11054. (setq beg (point) end (org-end-of-subtree t)
  11055. msg "Creating images for subtree...%s"))
  11056. (t
  11057. (if (setq at (org-inside-LaTeX-fragment-p))
  11058. (goto-char (max (point-min) (- (cdr at) 2)))
  11059. (org-back-to-heading))
  11060. (setq beg (point) end (progn (outline-next-heading) (point))
  11061. msg (if at "Creating image...%s"
  11062. "Creating images for entry...%s"))))
  11063. (message msg "")
  11064. (narrow-to-region beg end)
  11065. (goto-char beg)
  11066. (org-format-latex
  11067. (concat "ltxpng/" (file-name-sans-extension
  11068. (file-name-nondirectory
  11069. buffer-file-name)))
  11070. default-directory 'overlays msg at 'forbuffer)
  11071. (message msg "done. Use `C-c C-c' to remove images.")))))
  11072. (defvar org-latex-regexps
  11073. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11074. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11075. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11076. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11077. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11078. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11079. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11080. "Regular expressions for matching embedded LaTeX.")
  11081. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11082. "Replace LaTeX fragments with links to an image, and produce images."
  11083. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11084. (let* ((prefixnodir (file-name-nondirectory prefix))
  11085. (absprefix (expand-file-name prefix dir))
  11086. (todir (file-name-directory absprefix))
  11087. (opt org-format-latex-options)
  11088. (matchers (plist-get opt :matchers))
  11089. (re-list org-latex-regexps)
  11090. (cnt 0) txt link beg end re e checkdir
  11091. m n block linkfile movefile ov)
  11092. ;; Check if there are old images files with this prefix, and remove them
  11093. (when (file-directory-p todir)
  11094. (mapc 'delete-file
  11095. (directory-files
  11096. todir 'full
  11097. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11098. ;; Check the different regular expressions
  11099. (while (setq e (pop re-list))
  11100. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11101. block (if (nth 3 e) "\n\n" ""))
  11102. (when (member m matchers)
  11103. (goto-char (point-min))
  11104. (while (re-search-forward re nil t)
  11105. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11106. (setq txt (match-string n)
  11107. beg (match-beginning n) end (match-end n)
  11108. cnt (1+ cnt)
  11109. linkfile (format "%s_%04d.png" prefix cnt)
  11110. movefile (format "%s_%04d.png" absprefix cnt)
  11111. link (concat block "[[file:" linkfile "]]" block))
  11112. (if msg (message msg cnt))
  11113. (goto-char beg)
  11114. (unless checkdir ; make sure the directory exists
  11115. (setq checkdir t)
  11116. (or (file-directory-p todir) (make-directory todir)))
  11117. (org-create-formula-image
  11118. txt movefile opt forbuffer)
  11119. (if overlays
  11120. (progn
  11121. (setq ov (org-make-overlay beg end))
  11122. (if (featurep 'xemacs)
  11123. (progn
  11124. (org-overlay-put ov 'invisible t)
  11125. (org-overlay-put
  11126. ov 'end-glyph
  11127. (make-glyph (vector 'png :file movefile))))
  11128. (org-overlay-put
  11129. ov 'display
  11130. (list 'image :type 'png :file movefile :ascent 'center)))
  11131. (push ov org-latex-fragment-image-overlays)
  11132. (goto-char end))
  11133. (delete-region beg end)
  11134. (insert link))))))))
  11135. ;; This function borrows from Ganesh Swami's latex2png.el
  11136. (defun org-create-formula-image (string tofile options buffer)
  11137. (let* ((tmpdir (if (featurep 'xemacs)
  11138. (temp-directory)
  11139. temporary-file-directory))
  11140. (texfilebase (make-temp-name
  11141. (expand-file-name "orgtex" tmpdir)))
  11142. (texfile (concat texfilebase ".tex"))
  11143. (dvifile (concat texfilebase ".dvi"))
  11144. (pngfile (concat texfilebase ".png"))
  11145. (fnh (if (featurep 'xemacs)
  11146. (font-height (get-face-font 'default))
  11147. (face-attribute 'default :height nil)))
  11148. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11149. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11150. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11151. "Black"))
  11152. (bg (or (plist-get options (if buffer :background :html-background))
  11153. "Transparent")))
  11154. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11155. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11156. (with-temp-file texfile
  11157. (insert org-format-latex-header
  11158. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11159. (let ((dir default-directory))
  11160. (condition-case nil
  11161. (progn
  11162. (cd tmpdir)
  11163. (call-process "latex" nil nil nil texfile))
  11164. (error nil))
  11165. (cd dir))
  11166. (if (not (file-exists-p dvifile))
  11167. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11168. (condition-case nil
  11169. (call-process "dvipng" nil nil nil
  11170. "-E" "-fg" fg "-bg" bg
  11171. "-D" dpi
  11172. ;;"-x" scale "-y" scale
  11173. "-T" "tight"
  11174. "-o" pngfile
  11175. dvifile)
  11176. (error nil))
  11177. (if (not (file-exists-p pngfile))
  11178. (progn (message "Failed to create png file from %s" texfile) nil)
  11179. ;; Use the requested file name and clean up
  11180. (copy-file pngfile tofile 'replace)
  11181. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11182. (delete-file (concat texfilebase e)))
  11183. pngfile))))
  11184. (defun org-dvipng-color (attr)
  11185. "Return an rgb color specification for dvipng."
  11186. (apply 'format "rgb %s %s %s"
  11187. (mapcar 'org-normalize-color
  11188. (color-values (face-attribute 'default attr nil)))))
  11189. (defun org-normalize-color (value)
  11190. "Return string to be used as color value for an RGB component."
  11191. (format "%g" (/ value 65535.0)))
  11192. ;;;; Key bindings
  11193. ;; Make `C-c C-x' a prefix key
  11194. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11195. ;; TAB key with modifiers
  11196. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11197. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11198. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11199. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11200. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11201. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11202. ;; The following line is necessary under Suse GNU/Linux
  11203. (unless (featurep 'xemacs)
  11204. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11205. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11206. (define-key org-mode-map [backtab] 'org-shifttab)
  11207. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11208. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11209. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11210. ;; Cursor keys with modifiers
  11211. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11212. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11213. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11214. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11215. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11216. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11217. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11218. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11219. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11220. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11221. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11222. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11223. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11224. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11225. ;;; Extra keys for tty access.
  11226. ;; We only set them when really needed because otherwise the
  11227. ;; menus don't show the simple keys
  11228. (when (or org-use-extra-keys
  11229. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11230. (not window-system))
  11231. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11232. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11233. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11234. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11235. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11236. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11237. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11238. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11239. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11240. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11241. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11242. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11243. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11244. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11245. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11246. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11247. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11248. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11249. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11250. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11251. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11252. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11253. ;; All the other keys
  11254. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11255. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11256. (if (boundp 'narrow-map)
  11257. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11258. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11259. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11260. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11261. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11262. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11263. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11264. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11265. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11266. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11267. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11268. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11269. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11270. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11271. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11272. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11273. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11274. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11275. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11276. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11277. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11278. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11279. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11280. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11281. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11282. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11283. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11284. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11285. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11286. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11287. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11288. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11289. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11290. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11291. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11292. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11293. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11294. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11295. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11296. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11297. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11298. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11299. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11300. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11301. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11302. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11303. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11304. (org-defkey org-mode-map "\C-m" 'org-return)
  11305. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11306. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11307. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11308. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11309. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11310. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11311. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11312. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11313. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11314. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11315. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11316. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11317. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11318. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11319. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11320. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11321. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11322. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11323. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11324. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11325. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11326. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11327. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11328. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11329. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11330. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11331. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11332. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11333. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11334. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11335. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11336. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11337. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11338. (when (featurep 'xemacs)
  11339. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11340. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11341. (defun org-self-insert-command (N)
  11342. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11343. If the cursor is in a table looking at whitespace, the whitespace is
  11344. overwritten, and the table is not marked as requiring realignment."
  11345. (interactive "p")
  11346. (if (and (org-table-p)
  11347. (progn
  11348. ;; check if we blank the field, and if that triggers align
  11349. (and (featurep 'org-table) org-table-auto-blank-field
  11350. (member last-command
  11351. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11352. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11353. ;; got extra space, this field does not determine column width
  11354. (let (org-table-may-need-update) (org-table-blank-field))
  11355. ;; no extra space, this field may determine column width
  11356. (org-table-blank-field)))
  11357. t)
  11358. (eq N 1)
  11359. (looking-at "[^|\n]* |"))
  11360. (let (org-table-may-need-update)
  11361. (goto-char (1- (match-end 0)))
  11362. (delete-backward-char 1)
  11363. (goto-char (match-beginning 0))
  11364. (self-insert-command N))
  11365. (setq org-table-may-need-update t)
  11366. (self-insert-command N)
  11367. (org-fix-tags-on-the-fly)))
  11368. (defun org-fix-tags-on-the-fly ()
  11369. (when (and (equal (char-after (point-at-bol)) ?*)
  11370. (org-on-heading-p))
  11371. (org-align-tags-here org-tags-column)))
  11372. (defun org-delete-backward-char (N)
  11373. "Like `delete-backward-char', insert whitespace at field end in tables.
  11374. When deleting backwards, in tables this function will insert whitespace in
  11375. front of the next \"|\" separator, to keep the table aligned. The table will
  11376. still be marked for re-alignment if the field did fill the entire column,
  11377. because, in this case the deletion might narrow the column."
  11378. (interactive "p")
  11379. (if (and (org-table-p)
  11380. (eq N 1)
  11381. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11382. (looking-at ".*?|"))
  11383. (let ((pos (point))
  11384. (noalign (looking-at "[^|\n\r]* |"))
  11385. (c org-table-may-need-update))
  11386. (backward-delete-char N)
  11387. (skip-chars-forward "^|")
  11388. (insert " ")
  11389. (goto-char (1- pos))
  11390. ;; noalign: if there were two spaces at the end, this field
  11391. ;; does not determine the width of the column.
  11392. (if noalign (setq org-table-may-need-update c)))
  11393. (backward-delete-char N)
  11394. (org-fix-tags-on-the-fly)))
  11395. (defun org-delete-char (N)
  11396. "Like `delete-char', but insert whitespace at field end in tables.
  11397. When deleting characters, in tables this function will insert whitespace in
  11398. front of the next \"|\" separator, to keep the table aligned. The table will
  11399. still be marked for re-alignment if the field did fill the entire column,
  11400. because, in this case the deletion might narrow the column."
  11401. (interactive "p")
  11402. (if (and (org-table-p)
  11403. (not (bolp))
  11404. (not (= (char-after) ?|))
  11405. (eq N 1))
  11406. (if (looking-at ".*?|")
  11407. (let ((pos (point))
  11408. (noalign (looking-at "[^|\n\r]* |"))
  11409. (c org-table-may-need-update))
  11410. (replace-match (concat
  11411. (substring (match-string 0) 1 -1)
  11412. " |"))
  11413. (goto-char pos)
  11414. ;; noalign: if there were two spaces at the end, this field
  11415. ;; does not determine the width of the column.
  11416. (if noalign (setq org-table-may-need-update c)))
  11417. (delete-char N))
  11418. (delete-char N)
  11419. (org-fix-tags-on-the-fly)))
  11420. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11421. (put 'org-self-insert-command 'delete-selection t)
  11422. (put 'orgtbl-self-insert-command 'delete-selection t)
  11423. (put 'org-delete-char 'delete-selection 'supersede)
  11424. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11425. ;; Make `flyspell-mode' delay after some commands
  11426. (put 'org-self-insert-command 'flyspell-delayed t)
  11427. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11428. (put 'org-delete-char 'flyspell-delayed t)
  11429. (put 'org-delete-backward-char 'flyspell-delayed t)
  11430. ;; Make pabbrev-mode expand after org-mode commands
  11431. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11432. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11433. ;; How to do this: Measure non-white length of current string
  11434. ;; If equal to column width, we should realign.
  11435. (defun org-remap (map &rest commands)
  11436. "In MAP, remap the functions given in COMMANDS.
  11437. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11438. (let (new old)
  11439. (while commands
  11440. (setq old (pop commands) new (pop commands))
  11441. (if (fboundp 'command-remapping)
  11442. (org-defkey map (vector 'remap old) new)
  11443. (substitute-key-definition old new map global-map)))))
  11444. (when (eq org-enable-table-editor 'optimized)
  11445. ;; If the user wants maximum table support, we need to hijack
  11446. ;; some standard editing functions
  11447. (org-remap org-mode-map
  11448. 'self-insert-command 'org-self-insert-command
  11449. 'delete-char 'org-delete-char
  11450. 'delete-backward-char 'org-delete-backward-char)
  11451. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11452. (defun org-shiftcursor-error ()
  11453. "Throw an error because Shift-Cursor command was applied in wrong context."
  11454. (error "This command is active in special context like tables, headlines or timestamps"))
  11455. (defun org-shifttab (&optional arg)
  11456. "Global visibility cycling or move to previous table field.
  11457. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11458. on context.
  11459. See the individual commands for more information."
  11460. (interactive "P")
  11461. (cond
  11462. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11463. ((integerp arg)
  11464. (message "Content view to level: %d" arg)
  11465. (org-content (prefix-numeric-value arg))
  11466. (setq org-cycle-global-status 'overview))
  11467. (t (call-interactively 'org-global-cycle))))
  11468. (defun org-shiftmetaleft ()
  11469. "Promote subtree or delete table column.
  11470. Calls `org-promote-subtree', `org-outdent-item',
  11471. or `org-table-delete-column', depending on context.
  11472. See the individual commands for more information."
  11473. (interactive)
  11474. (cond
  11475. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11476. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11477. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11478. (t (org-shiftcursor-error))))
  11479. (defun org-shiftmetaright ()
  11480. "Demote subtree or insert table column.
  11481. Calls `org-demote-subtree', `org-indent-item',
  11482. or `org-table-insert-column', depending on context.
  11483. See the individual commands for more information."
  11484. (interactive)
  11485. (cond
  11486. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11487. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11488. ((org-at-item-p) (call-interactively 'org-indent-item))
  11489. (t (org-shiftcursor-error))))
  11490. (defun org-shiftmetaup (&optional arg)
  11491. "Move subtree up or kill table row.
  11492. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11493. `org-move-item-up' depending on context. See the individual commands
  11494. for more information."
  11495. (interactive "P")
  11496. (cond
  11497. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11498. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11499. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11500. (t (org-shiftcursor-error))))
  11501. (defun org-shiftmetadown (&optional arg)
  11502. "Move subtree down or insert table row.
  11503. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11504. `org-move-item-down', depending on context. See the individual
  11505. commands for more information."
  11506. (interactive "P")
  11507. (cond
  11508. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11509. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11510. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11511. (t (org-shiftcursor-error))))
  11512. (defun org-metaleft (&optional arg)
  11513. "Promote heading or move table column to left.
  11514. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11515. With no specific context, calls the Emacs default `backward-word'.
  11516. See the individual commands for more information."
  11517. (interactive "P")
  11518. (cond
  11519. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11520. ((or (org-on-heading-p) (org-region-active-p))
  11521. (call-interactively 'org-do-promote))
  11522. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11523. (t (call-interactively 'backward-word))))
  11524. (defun org-metaright (&optional arg)
  11525. "Demote subtree or move table column to right.
  11526. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11527. With no specific context, calls the Emacs default `forward-word'.
  11528. See the individual commands for more information."
  11529. (interactive "P")
  11530. (cond
  11531. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11532. ((or (org-on-heading-p) (org-region-active-p))
  11533. (call-interactively 'org-do-demote))
  11534. ((org-at-item-p) (call-interactively 'org-indent-item))
  11535. (t (call-interactively 'forward-word))))
  11536. (defun org-metaup (&optional arg)
  11537. "Move subtree up or move table row up.
  11538. Calls `org-move-subtree-up' or `org-table-move-row' or
  11539. `org-move-item-up', depending on context. See the individual commands
  11540. for more information."
  11541. (interactive "P")
  11542. (cond
  11543. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11544. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11545. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11546. (t (transpose-lines 1) (beginning-of-line -1))))
  11547. (defun org-metadown (&optional arg)
  11548. "Move subtree down or move table row down.
  11549. Calls `org-move-subtree-down' or `org-table-move-row' or
  11550. `org-move-item-down', depending on context. See the individual
  11551. commands for more information."
  11552. (interactive "P")
  11553. (cond
  11554. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11555. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11556. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11557. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11558. (defun org-shiftup (&optional arg)
  11559. "Increase item in timestamp or increase priority of current headline.
  11560. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11561. depending on context. See the individual commands for more information."
  11562. (interactive "P")
  11563. (cond
  11564. ((org-at-timestamp-p t)
  11565. (call-interactively (if org-edit-timestamp-down-means-later
  11566. 'org-timestamp-down 'org-timestamp-up)))
  11567. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11568. ((org-at-item-p) (call-interactively 'org-previous-item))
  11569. ((org-clocktable-try-shift 'up arg))
  11570. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11571. (defun org-shiftdown (&optional arg)
  11572. "Decrease item in timestamp or decrease priority of current headline.
  11573. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11574. depending on context. See the individual commands for more information."
  11575. (interactive "P")
  11576. (cond
  11577. ((org-at-timestamp-p t)
  11578. (call-interactively (if org-edit-timestamp-down-means-later
  11579. 'org-timestamp-up 'org-timestamp-down)))
  11580. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11581. ((org-clocktable-try-shift 'down arg))
  11582. (t (call-interactively 'org-next-item))))
  11583. (defun org-shiftright (&optional arg)
  11584. "Next TODO keyword or timestamp one day later, depending on context."
  11585. (interactive "P")
  11586. (cond
  11587. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11588. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11589. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11590. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11591. ((org-clocktable-try-shift 'right arg))
  11592. (t (org-shiftcursor-error))))
  11593. (defun org-shiftleft (&optional arg)
  11594. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11595. (interactive "P")
  11596. (cond
  11597. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11598. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11599. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11600. ((org-at-property-p)
  11601. (call-interactively 'org-property-previous-allowed-value))
  11602. ((org-clocktable-try-shift 'left arg))
  11603. (t (org-shiftcursor-error))))
  11604. (defun org-shiftcontrolright ()
  11605. "Switch to next TODO set."
  11606. (interactive)
  11607. (cond
  11608. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11609. (t (org-shiftcursor-error))))
  11610. (defun org-shiftcontrolleft ()
  11611. "Switch to previous TODO set."
  11612. (interactive)
  11613. (cond
  11614. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11615. (t (org-shiftcursor-error))))
  11616. (defun org-ctrl-c-ret ()
  11617. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11618. (interactive)
  11619. (cond
  11620. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11621. (t (call-interactively 'org-insert-heading))))
  11622. (defun org-copy-special ()
  11623. "Copy region in table or copy current subtree.
  11624. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11625. See the individual commands for more information."
  11626. (interactive)
  11627. (call-interactively
  11628. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11629. (defun org-cut-special ()
  11630. "Cut region in table or cut current subtree.
  11631. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11632. See the individual commands for more information."
  11633. (interactive)
  11634. (call-interactively
  11635. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11636. (defun org-paste-special (arg)
  11637. "Paste rectangular region into table, or past subtree relative to level.
  11638. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11639. See the individual commands for more information."
  11640. (interactive "P")
  11641. (if (org-at-table-p)
  11642. (org-table-paste-rectangle)
  11643. (org-paste-subtree arg)))
  11644. (defun org-edit-special ()
  11645. "Call a special editor for the stuff at point.
  11646. When at a table, call the formula editor with `org-table-edit-formulas'.
  11647. When at the first line of an src example, call `org-edit-src-code'.
  11648. When in an #+include line, visit the include file. Otherwise call
  11649. `ffap' to visit the file at point."
  11650. (interactive)
  11651. (cond
  11652. ((org-at-table-p)
  11653. (call-interactively 'org-table-edit-formulas))
  11654. ((save-excursion
  11655. (beginning-of-line 1)
  11656. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11657. (find-file (org-trim (match-string 1))))
  11658. ((org-edit-src-code))
  11659. ((org-edit-fixed-width-region))
  11660. (t (call-interactively 'ffap))))
  11661. (defun org-ctrl-c-ctrl-c (&optional arg)
  11662. "Set tags in headline, or update according to changed information at point.
  11663. This command does many different things, depending on context:
  11664. - If the cursor is in a headline, prompt for tags and insert them
  11665. into the current line, aligned to `org-tags-column'. When called
  11666. with prefix arg, realign all tags in the current buffer.
  11667. - If the cursor is in one of the special #+KEYWORD lines, this
  11668. triggers scanning the buffer for these lines and updating the
  11669. information.
  11670. - If the cursor is inside a table, realign the table. This command
  11671. works even if the automatic table editor has been turned off.
  11672. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11673. the entire table.
  11674. - If the cursor is a the beginning of a dynamic block, update it.
  11675. - If the cursor is inside a table created by the table.el package,
  11676. activate that table.
  11677. - If the current buffer is a remember buffer, close note and file
  11678. it. A prefix argument of 1 files to the default location
  11679. without further interaction. A prefix argument of 2 files to
  11680. the currently clocking task.
  11681. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11682. links in this buffer.
  11683. - If the cursor is on a numbered item in a plain list, renumber the
  11684. ordered list.
  11685. - If the cursor is on a checkbox, toggle it."
  11686. (interactive "P")
  11687. (let ((org-enable-table-editor t))
  11688. (cond
  11689. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11690. org-occur-highlights
  11691. org-latex-fragment-image-overlays)
  11692. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11693. (org-remove-occur-highlights)
  11694. (org-remove-latex-fragment-image-overlays)
  11695. (message "Temporary highlights/overlays removed from current buffer"))
  11696. ((and (local-variable-p 'org-finish-function (current-buffer))
  11697. (fboundp org-finish-function))
  11698. (funcall org-finish-function))
  11699. ((org-at-property-p)
  11700. (call-interactively 'org-property-action))
  11701. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11702. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11703. ((org-at-table.el-p)
  11704. (require 'table)
  11705. (beginning-of-line 1)
  11706. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11707. (call-interactively 'table-recognize-table))
  11708. ((org-at-table-p)
  11709. (org-table-maybe-eval-formula)
  11710. (if arg
  11711. (call-interactively 'org-table-recalculate)
  11712. (org-table-maybe-recalculate-line))
  11713. (call-interactively 'org-table-align))
  11714. ((org-at-item-checkbox-p)
  11715. (call-interactively 'org-toggle-checkbox))
  11716. ((org-at-item-p)
  11717. (call-interactively 'org-maybe-renumber-ordered-list))
  11718. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11719. ;; Dynamic block
  11720. (beginning-of-line 1)
  11721. (save-excursion (org-update-dblock)))
  11722. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11723. (cond
  11724. ((equal (match-string 1) "TBLFM")
  11725. ;; Recalculate the table before this line
  11726. (save-excursion
  11727. (beginning-of-line 1)
  11728. (skip-chars-backward " \r\n\t")
  11729. (if (org-at-table-p)
  11730. (org-call-with-arg 'org-table-recalculate t))))
  11731. (t
  11732. ; (org-set-regexps-and-options)
  11733. ; (org-restart-font-lock)
  11734. (let ((org-inhibit-startup t)) (org-mode-restart))
  11735. (message "Local setup has been refreshed"))))
  11736. (t (error "C-c C-c can do nothing useful at this location.")))))
  11737. (defun org-mode-restart ()
  11738. "Restart Org-mode, to scan again for special lines.
  11739. Also updates the keyword regular expressions."
  11740. (interactive)
  11741. (org-mode)
  11742. (message "Org-mode restarted"))
  11743. (defun org-kill-note-or-show-branches ()
  11744. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11745. (interactive)
  11746. (if (not org-finish-function)
  11747. (call-interactively 'show-branches)
  11748. (let ((org-note-abort t))
  11749. (funcall org-finish-function))))
  11750. (defun org-return (&optional indent)
  11751. "Goto next table row or insert a newline.
  11752. Calls `org-table-next-row' or `newline', depending on context.
  11753. See the individual commands for more information."
  11754. (interactive)
  11755. (cond
  11756. ((bobp) (if indent (newline-and-indent) (newline)))
  11757. ((and (org-at-heading-p)
  11758. (looking-at
  11759. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11760. (org-show-entry)
  11761. (end-of-line 1)
  11762. (newline))
  11763. ((org-at-table-p)
  11764. (org-table-justify-field-maybe)
  11765. (call-interactively 'org-table-next-row))
  11766. (t (if indent (newline-and-indent) (newline)))))
  11767. (defun org-return-indent ()
  11768. "Goto next table row or insert a newline and indent.
  11769. Calls `org-table-next-row' or `newline-and-indent', depending on
  11770. context. See the individual commands for more information."
  11771. (interactive)
  11772. (org-return t))
  11773. (defun org-ctrl-c-star ()
  11774. "Compute table, or change heading status of lines.
  11775. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11776. depending on context. This will also turn a plain list item or a normal
  11777. line into a subheading."
  11778. (interactive)
  11779. (cond
  11780. ((org-at-table-p)
  11781. (call-interactively 'org-table-recalculate))
  11782. ((org-region-active-p)
  11783. ;; Convert all lines in region to list items
  11784. (call-interactively 'org-toggle-region-headings))
  11785. ((org-on-heading-p)
  11786. (org-toggle-region-headings (point-at-bol)
  11787. (min (1+ (point-at-eol)) (point-max))))
  11788. ((org-at-item-p)
  11789. ;; Convert to heading
  11790. (let ((level (save-match-data
  11791. (save-excursion
  11792. (condition-case nil
  11793. (progn
  11794. (org-back-to-heading t)
  11795. (funcall outline-level))
  11796. (error 0))))))
  11797. (replace-match
  11798. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11799. (t (org-toggle-region-headings (point-at-bol)
  11800. (min (1+ (point-at-eol)) (point-max))))))
  11801. (defun org-ctrl-c-minus ()
  11802. "Insert separator line in table or modify bullet status of line.
  11803. Also turns a plain line or a region of lines into list items.
  11804. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11805. `org-cycle-list-bullet', depending on context."
  11806. (interactive)
  11807. (cond
  11808. ((org-at-table-p)
  11809. (call-interactively 'org-table-insert-hline))
  11810. ((org-on-heading-p)
  11811. ;; Convert to item
  11812. (save-excursion
  11813. (beginning-of-line 1)
  11814. (if (looking-at "\\*+ ")
  11815. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11816. ((org-region-active-p)
  11817. ;; Convert all lines in region to list items
  11818. (call-interactively 'org-toggle-region-items))
  11819. ((org-in-item-p)
  11820. (call-interactively 'org-cycle-list-bullet))
  11821. (t (org-toggle-region-items (point-at-bol)
  11822. (min (1+ (point-at-eol)) (point-max))))))
  11823. (defun org-toggle-region-items (beg end)
  11824. "Convert all lines in region to list items.
  11825. If the first line is already an item, convert all list items in the region
  11826. to normal lines."
  11827. (interactive "r")
  11828. (let (l2 l)
  11829. (save-excursion
  11830. (goto-char end)
  11831. (setq l2 (org-current-line))
  11832. (goto-char beg)
  11833. (beginning-of-line 1)
  11834. (setq l (1- (org-current-line)))
  11835. (if (org-at-item-p)
  11836. ;; We already have items, de-itemize
  11837. (while (< (setq l (1+ l)) l2)
  11838. (when (org-at-item-p)
  11839. (goto-char (match-beginning 2))
  11840. (delete-region (match-beginning 2) (match-end 2))
  11841. (and (looking-at "[ \t]+") (replace-match "")))
  11842. (beginning-of-line 2))
  11843. (while (< (setq l (1+ l)) l2)
  11844. (unless (org-at-item-p)
  11845. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11846. (replace-match "\\1- \\2")))
  11847. (beginning-of-line 2))))))
  11848. (defun org-toggle-region-headings (beg end)
  11849. "Convert all lines in region to list items.
  11850. If the first line is already an item, convert all list items in the region
  11851. to normal lines."
  11852. (interactive "r")
  11853. (let (l2 l)
  11854. (save-excursion
  11855. (goto-char end)
  11856. (setq l2 (org-current-line))
  11857. (goto-char beg)
  11858. (beginning-of-line 1)
  11859. (setq l (1- (org-current-line)))
  11860. (if (org-on-heading-p)
  11861. ;; We already have headlines, de-star them
  11862. (while (< (setq l (1+ l)) l2)
  11863. (when (org-on-heading-p t)
  11864. (and (looking-at outline-regexp) (replace-match "")))
  11865. (beginning-of-line 2))
  11866. (let* ((stars (save-excursion
  11867. (re-search-backward org-complex-heading-regexp nil t)
  11868. (or (match-string 1) "*")))
  11869. (add-stars (if org-odd-levels-only "**" "*"))
  11870. (rpl (concat stars add-stars " \\2")))
  11871. (while (< (setq l (1+ l)) l2)
  11872. (unless (org-on-heading-p)
  11873. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11874. (replace-match rpl)))
  11875. (beginning-of-line 2)))))))
  11876. (defun org-meta-return (&optional arg)
  11877. "Insert a new heading or wrap a region in a table.
  11878. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11879. See the individual commands for more information."
  11880. (interactive "P")
  11881. (cond
  11882. ((org-at-table-p)
  11883. (call-interactively 'org-table-wrap-region))
  11884. (t (call-interactively 'org-insert-heading))))
  11885. ;;; Menu entries
  11886. ;; Define the Org-mode menus
  11887. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11888. '("Tbl"
  11889. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11890. ["Next Field" org-cycle (org-at-table-p)]
  11891. ["Previous Field" org-shifttab (org-at-table-p)]
  11892. ["Next Row" org-return (org-at-table-p)]
  11893. "--"
  11894. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11895. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11896. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11897. "--"
  11898. ("Column"
  11899. ["Move Column Left" org-metaleft (org-at-table-p)]
  11900. ["Move Column Right" org-metaright (org-at-table-p)]
  11901. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11902. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11903. ("Row"
  11904. ["Move Row Up" org-metaup (org-at-table-p)]
  11905. ["Move Row Down" org-metadown (org-at-table-p)]
  11906. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11907. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11908. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11909. "--"
  11910. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11911. ("Rectangle"
  11912. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11913. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11914. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11915. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11916. "--"
  11917. ("Calculate"
  11918. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11919. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11920. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11921. "--"
  11922. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11923. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11924. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11925. "--"
  11926. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11927. "--"
  11928. ["Sum Column/Rectangle" org-table-sum
  11929. (or (org-at-table-p) (org-region-active-p))]
  11930. ["Which Column?" org-table-current-column (org-at-table-p)])
  11931. ["Debug Formulas"
  11932. org-table-toggle-formula-debugger
  11933. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11934. ["Show Col/Row Numbers"
  11935. org-table-toggle-coordinate-overlays
  11936. :style toggle
  11937. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11938. "--"
  11939. ["Create" org-table-create (and (not (org-at-table-p))
  11940. org-enable-table-editor)]
  11941. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11942. ["Import from File" org-table-import (not (org-at-table-p))]
  11943. ["Export to File" org-table-export (org-at-table-p)]
  11944. "--"
  11945. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11946. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11947. '("Org"
  11948. ("Show/Hide"
  11949. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11950. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11951. ["Sparse Tree..." org-sparse-tree t]
  11952. ["Reveal Context" org-reveal t]
  11953. ["Show All" show-all t]
  11954. "--"
  11955. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11956. "--"
  11957. ["New Heading" org-insert-heading t]
  11958. ("Navigate Headings"
  11959. ["Up" outline-up-heading t]
  11960. ["Next" outline-next-visible-heading t]
  11961. ["Previous" outline-previous-visible-heading t]
  11962. ["Next Same Level" outline-forward-same-level t]
  11963. ["Previous Same Level" outline-backward-same-level t]
  11964. "--"
  11965. ["Jump" org-goto t])
  11966. ("Edit Structure"
  11967. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11968. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11969. "--"
  11970. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11971. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11972. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11973. "--"
  11974. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11975. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11976. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11977. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11978. "--"
  11979. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11980. "--"
  11981. ["Convert to odd levels" org-convert-to-odd-levels t]
  11982. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11983. ("Editing"
  11984. ["Emphasis..." org-emphasize t]
  11985. ["Edit Source Example" org-edit-special t])
  11986. ("Archive"
  11987. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11988. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11989. ; :active t :keys "C-u C-c C-x C-a"]
  11990. ["Sparse trees open ARCHIVE trees"
  11991. (setq org-sparse-tree-open-archived-trees
  11992. (not org-sparse-tree-open-archived-trees))
  11993. :style toggle :selected org-sparse-tree-open-archived-trees]
  11994. ["Cycling opens ARCHIVE trees"
  11995. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11996. :style toggle :selected org-cycle-open-archived-trees]
  11997. "--"
  11998. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  11999. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12000. ; ["Check and Move Children" (org-archive-subtree '(4))
  12001. ; :active t :keys "C-u C-c C-x C-s"]
  12002. )
  12003. "--"
  12004. ("TODO Lists"
  12005. ["TODO/DONE/-" org-todo t]
  12006. ("Select keyword"
  12007. ["Next keyword" org-shiftright (org-on-heading-p)]
  12008. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12009. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12010. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12011. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12012. ["Show TODO Tree" org-show-todo-tree t]
  12013. ["Global TODO list" org-todo-list t]
  12014. "--"
  12015. ["Set Priority" org-priority t]
  12016. ["Priority Up" org-shiftup t]
  12017. ["Priority Down" org-shiftdown t])
  12018. ("TAGS and Properties"
  12019. ["Set Tags" 'org-set-tags-command t]
  12020. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12021. "--"
  12022. ["Set property" 'org-set-property t]
  12023. ["Column view of properties" org-columns t]
  12024. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12025. ("Dates and Scheduling"
  12026. ["Timestamp" org-time-stamp t]
  12027. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12028. ("Change Date"
  12029. ["1 Day Later" org-shiftright t]
  12030. ["1 Day Earlier" org-shiftleft t]
  12031. ["1 ... Later" org-shiftup t]
  12032. ["1 ... Earlier" org-shiftdown t])
  12033. ["Compute Time Range" org-evaluate-time-range t]
  12034. ["Schedule Item" org-schedule t]
  12035. ["Deadline" org-deadline t]
  12036. "--"
  12037. ["Custom time format" org-toggle-time-stamp-overlays
  12038. :style radio :selected org-display-custom-times]
  12039. "--"
  12040. ["Goto Calendar" org-goto-calendar t]
  12041. ["Date from Calendar" org-date-from-calendar t])
  12042. ("Logging work"
  12043. ["Clock in" org-clock-in t]
  12044. ["Clock out" org-clock-out t]
  12045. ["Clock cancel" org-clock-cancel t]
  12046. ["Goto running clock" org-clock-goto t]
  12047. ["Display times" org-clock-display t]
  12048. ["Create clock table" org-clock-report t]
  12049. "--"
  12050. ["Record DONE time"
  12051. (progn (setq org-log-done (not org-log-done))
  12052. (message "Switching to %s will %s record a timestamp"
  12053. (car org-done-keywords)
  12054. (if org-log-done "automatically" "not")))
  12055. :style toggle :selected org-log-done])
  12056. "--"
  12057. ["Agenda Command..." org-agenda t]
  12058. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12059. ("File List for Agenda")
  12060. ("Special views current file"
  12061. ["TODO Tree" org-show-todo-tree t]
  12062. ["Check Deadlines" org-check-deadlines t]
  12063. ["Timeline" org-timeline t]
  12064. ["Tags Tree" org-tags-sparse-tree t])
  12065. "--"
  12066. ("Hyperlinks"
  12067. ["Store Link (Global)" org-store-link t]
  12068. ["Insert Link" org-insert-link t]
  12069. ["Follow Link" org-open-at-point t]
  12070. "--"
  12071. ["Next link" org-next-link t]
  12072. ["Previous link" org-previous-link t]
  12073. "--"
  12074. ["Descriptive Links"
  12075. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12076. :style radio
  12077. :selected (member '(org-link) buffer-invisibility-spec)]
  12078. ["Literal Links"
  12079. (progn
  12080. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12081. :style radio
  12082. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12083. "--"
  12084. ["Export/Publish..." org-export t]
  12085. ("LaTeX"
  12086. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12087. :selected org-cdlatex-mode]
  12088. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12089. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12090. ["Modify math symbol" org-cdlatex-math-modify
  12091. (org-inside-LaTeX-fragment-p)]
  12092. ["Export LaTeX fragments as images"
  12093. (if (featurep 'org-exp)
  12094. (setq org-export-with-LaTeX-fragments
  12095. (not org-export-with-LaTeX-fragments))
  12096. (require 'org-exp))
  12097. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12098. org-export-with-LaTeX-fragments)])
  12099. "--"
  12100. ("Documentation"
  12101. ["Show Version" org-version t]
  12102. ["Info Documentation" org-info t])
  12103. ("Customize"
  12104. ["Browse Org Group" org-customize t]
  12105. "--"
  12106. ["Expand This Menu" org-create-customize-menu
  12107. (fboundp 'customize-menu-create)])
  12108. "--"
  12109. ["Refresh setup" org-mode-restart t]
  12110. ))
  12111. (defun org-info (&optional node)
  12112. "Read documentation for Org-mode in the info system.
  12113. With optional NODE, go directly to that node."
  12114. (interactive)
  12115. (info (format "(org)%s" (or node ""))))
  12116. (defun org-install-agenda-files-menu ()
  12117. (let ((bl (buffer-list)))
  12118. (save-excursion
  12119. (while bl
  12120. (set-buffer (pop bl))
  12121. (if (org-mode-p) (setq bl nil)))
  12122. (when (org-mode-p)
  12123. (easy-menu-change
  12124. '("Org") "File List for Agenda"
  12125. (append
  12126. (list
  12127. ["Edit File List" (org-edit-agenda-file-list) t]
  12128. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12129. ["Remove Current File from List" org-remove-file t]
  12130. ["Cycle through agenda files" org-cycle-agenda-files t]
  12131. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12132. "--")
  12133. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12134. ;;;; Documentation
  12135. ;;;###autoload
  12136. (defun org-require-autoloaded-modules ()
  12137. (interactive)
  12138. (mapc 'require
  12139. '(org-agenda org-archive org-clock org-colview
  12140. org-exp org-id org-export-latex org-publish
  12141. org-remember org-table)))
  12142. ;;;###autoload
  12143. (defun org-customize ()
  12144. "Call the customize function with org as argument."
  12145. (interactive)
  12146. (org-load-modules-maybe)
  12147. (org-require-autoloaded-modules)
  12148. (customize-browse 'org))
  12149. (defun org-create-customize-menu ()
  12150. "Create a full customization menu for Org-mode, insert it into the menu."
  12151. (interactive)
  12152. (org-load-modules-maybe)
  12153. (org-require-autoloaded-modules)
  12154. (if (fboundp 'customize-menu-create)
  12155. (progn
  12156. (easy-menu-change
  12157. '("Org") "Customize"
  12158. `(["Browse Org group" org-customize t]
  12159. "--"
  12160. ,(customize-menu-create 'org)
  12161. ["Set" Custom-set t]
  12162. ["Save" Custom-save t]
  12163. ["Reset to Current" Custom-reset-current t]
  12164. ["Reset to Saved" Custom-reset-saved t]
  12165. ["Reset to Standard Settings" Custom-reset-standard t]))
  12166. (message "\"Org\"-menu now contains full customization menu"))
  12167. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12168. ;;;; Miscellaneous stuff
  12169. ;;; Generally useful functions
  12170. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12171. "Display the given MESSAGE as a warning."
  12172. (if (fboundp 'display-warning)
  12173. (display-warning 'org message
  12174. (if (featurep 'xemacs)
  12175. 'warning
  12176. :warning))
  12177. (let ((buf (get-buffer-create "*Org warnings*")))
  12178. (with-current-buffer buf
  12179. (goto-char (point-max))
  12180. (insert "Warning (Org): " message)
  12181. (unless (bolp)
  12182. (newline)))
  12183. (display-buffer buf)
  12184. (sit-for 0))))
  12185. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12186. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12187. (if (and marker (marker-buffer marker)
  12188. (buffer-live-p (marker-buffer marker)))
  12189. (progn
  12190. (switch-to-buffer (marker-buffer marker))
  12191. (if (or (> marker (point-max)) (< marker (point-min)))
  12192. (widen))
  12193. (goto-char marker))
  12194. (if bookmark
  12195. (bookmark-jump bookmark)
  12196. (error "Cannot find location"))))
  12197. (defun org-quote-csv-field (s)
  12198. "Quote field for inclusion in CSV material."
  12199. (if (string-match "[\",]" s)
  12200. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12201. s))
  12202. (defun org-plist-delete (plist property)
  12203. "Delete PROPERTY from PLIST.
  12204. This is in contrast to merely setting it to 0."
  12205. (let (p)
  12206. (while plist
  12207. (if (not (eq property (car plist)))
  12208. (setq p (plist-put p (car plist) (nth 1 plist))))
  12209. (setq plist (cddr plist)))
  12210. p))
  12211. (defun org-force-self-insert (N)
  12212. "Needed to enforce self-insert under remapping."
  12213. (interactive "p")
  12214. (self-insert-command N))
  12215. (defun org-string-width (s)
  12216. "Compute width of string, ignoring invisible characters.
  12217. This ignores character with invisibility property `org-link', and also
  12218. characters with property `org-cwidth', because these will become invisible
  12219. upon the next fontification round."
  12220. (let (b l)
  12221. (when (or (eq t buffer-invisibility-spec)
  12222. (assq 'org-link buffer-invisibility-spec))
  12223. (while (setq b (text-property-any 0 (length s)
  12224. 'invisible 'org-link s))
  12225. (setq s (concat (substring s 0 b)
  12226. (substring s (or (next-single-property-change
  12227. b 'invisible s) (length s)))))))
  12228. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12229. (setq s (concat (substring s 0 b)
  12230. (substring s (or (next-single-property-change
  12231. b 'org-cwidth s) (length s))))))
  12232. (setq l (string-width s) b -1)
  12233. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12234. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12235. l))
  12236. (defun org-get-indentation (&optional line)
  12237. "Get the indentation of the current line, interpreting tabs.
  12238. When LINE is given, assume it represents a line and compute its indentation."
  12239. (if line
  12240. (if (string-match "^ *" (org-remove-tabs line))
  12241. (match-end 0))
  12242. (save-excursion
  12243. (beginning-of-line 1)
  12244. (skip-chars-forward " \t")
  12245. (current-column))))
  12246. (defun org-remove-tabs (s &optional width)
  12247. "Replace tabulators in S with spaces.
  12248. Assumes that s is a single line, starting in column 0."
  12249. (setq width (or width tab-width))
  12250. (while (string-match "\t" s)
  12251. (setq s (replace-match
  12252. (make-string
  12253. (- (* width (/ (+ (match-beginning 0) width) width))
  12254. (match-beginning 0)) ?\ )
  12255. t t s)))
  12256. s)
  12257. (defun org-fix-indentation (line ind)
  12258. "Fix indentation in LINE.
  12259. IND is a cons cell with target and minimum indentation.
  12260. If the current indenation in LINE is smaller than the minimum,
  12261. leave it alone. If it is larger than ind, set it to the target."
  12262. (let* ((l (org-remove-tabs line))
  12263. (i (org-get-indentation l))
  12264. (i1 (car ind)) (i2 (cdr ind)))
  12265. (if (>= i i2) (setq l (substring line i2)))
  12266. (if (> i1 0)
  12267. (concat (make-string i1 ?\ ) l)
  12268. l)))
  12269. (defun org-base-buffer (buffer)
  12270. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12271. (if (not buffer)
  12272. buffer
  12273. (or (buffer-base-buffer buffer)
  12274. buffer)))
  12275. (defun org-trim (s)
  12276. "Remove whitespace at beginning and end of string."
  12277. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12278. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12279. s)
  12280. (defun org-wrap (string &optional width lines)
  12281. "Wrap string to either a number of lines, or a width in characters.
  12282. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12283. that costs. If there is a word longer than WIDTH, the text is actually
  12284. wrapped to the length of that word.
  12285. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12286. many lines, whatever width that takes.
  12287. The return value is a list of lines, without newlines at the end."
  12288. (let* ((words (org-split-string string "[ \t\n]+"))
  12289. (maxword (apply 'max (mapcar 'org-string-width words)))
  12290. w ll)
  12291. (cond (width
  12292. (org-do-wrap words (max maxword width)))
  12293. (lines
  12294. (setq w maxword)
  12295. (setq ll (org-do-wrap words maxword))
  12296. (if (<= (length ll) lines)
  12297. ll
  12298. (setq ll words)
  12299. (while (> (length ll) lines)
  12300. (setq w (1+ w))
  12301. (setq ll (org-do-wrap words w)))
  12302. ll))
  12303. (t (error "Cannot wrap this")))))
  12304. (defun org-do-wrap (words width)
  12305. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12306. (let (lines line)
  12307. (while words
  12308. (setq line (pop words))
  12309. (while (and words (< (+ (length line) (length (car words))) width))
  12310. (setq line (concat line " " (pop words))))
  12311. (setq lines (push line lines)))
  12312. (nreverse lines)))
  12313. (defun org-split-string (string &optional separators)
  12314. "Splits STRING into substrings at SEPARATORS.
  12315. No empty strings are returned if there are matches at the beginning
  12316. and end of string."
  12317. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12318. (start 0)
  12319. notfirst
  12320. (list nil))
  12321. (while (and (string-match rexp string
  12322. (if (and notfirst
  12323. (= start (match-beginning 0))
  12324. (< start (length string)))
  12325. (1+ start) start))
  12326. (< (match-beginning 0) (length string)))
  12327. (setq notfirst t)
  12328. (or (eq (match-beginning 0) 0)
  12329. (and (eq (match-beginning 0) (match-end 0))
  12330. (eq (match-beginning 0) start))
  12331. (setq list
  12332. (cons (substring string start (match-beginning 0))
  12333. list)))
  12334. (setq start (match-end 0)))
  12335. (or (eq start (length string))
  12336. (setq list
  12337. (cons (substring string start)
  12338. list)))
  12339. (nreverse list)))
  12340. (defun org-context ()
  12341. "Return a list of contexts of the current cursor position.
  12342. If several contexts apply, all are returned.
  12343. Each context entry is a list with a symbol naming the context, and
  12344. two positions indicating start and end of the context. Possible
  12345. contexts are:
  12346. :headline anywhere in a headline
  12347. :headline-stars on the leading stars in a headline
  12348. :todo-keyword on a TODO keyword (including DONE) in a headline
  12349. :tags on the TAGS in a headline
  12350. :priority on the priority cookie in a headline
  12351. :item on the first line of a plain list item
  12352. :item-bullet on the bullet/number of a plain list item
  12353. :checkbox on the checkbox in a plain list item
  12354. :table in an org-mode table
  12355. :table-special on a special filed in a table
  12356. :table-table in a table.el table
  12357. :link on a hyperlink
  12358. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12359. :target on a <<target>>
  12360. :radio-target on a <<<radio-target>>>
  12361. :latex-fragment on a LaTeX fragment
  12362. :latex-preview on a LaTeX fragment with overlayed preview image
  12363. This function expects the position to be visible because it uses font-lock
  12364. faces as a help to recognize the following contexts: :table-special, :link,
  12365. and :keyword."
  12366. (let* ((f (get-text-property (point) 'face))
  12367. (faces (if (listp f) f (list f)))
  12368. (p (point)) clist o)
  12369. ;; First the large context
  12370. (cond
  12371. ((org-on-heading-p t)
  12372. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12373. (when (progn
  12374. (beginning-of-line 1)
  12375. (looking-at org-todo-line-tags-regexp))
  12376. (push (org-point-in-group p 1 :headline-stars) clist)
  12377. (push (org-point-in-group p 2 :todo-keyword) clist)
  12378. (push (org-point-in-group p 4 :tags) clist))
  12379. (goto-char p)
  12380. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12381. (if (looking-at "\\[#[A-Z0-9]\\]")
  12382. (push (org-point-in-group p 0 :priority) clist)))
  12383. ((org-at-item-p)
  12384. (push (org-point-in-group p 2 :item-bullet) clist)
  12385. (push (list :item (point-at-bol)
  12386. (save-excursion (org-end-of-item) (point)))
  12387. clist)
  12388. (and (org-at-item-checkbox-p)
  12389. (push (org-point-in-group p 0 :checkbox) clist)))
  12390. ((org-at-table-p)
  12391. (push (list :table (org-table-begin) (org-table-end)) clist)
  12392. (if (memq 'org-formula faces)
  12393. (push (list :table-special
  12394. (previous-single-property-change p 'face)
  12395. (next-single-property-change p 'face)) clist)))
  12396. ((org-at-table-p 'any)
  12397. (push (list :table-table) clist)))
  12398. (goto-char p)
  12399. ;; Now the small context
  12400. (cond
  12401. ((org-at-timestamp-p)
  12402. (push (org-point-in-group p 0 :timestamp) clist))
  12403. ((memq 'org-link faces)
  12404. (push (list :link
  12405. (previous-single-property-change p 'face)
  12406. (next-single-property-change p 'face)) clist))
  12407. ((memq 'org-special-keyword faces)
  12408. (push (list :keyword
  12409. (previous-single-property-change p 'face)
  12410. (next-single-property-change p 'face)) clist))
  12411. ((org-on-target-p)
  12412. (push (org-point-in-group p 0 :target) clist)
  12413. (goto-char (1- (match-beginning 0)))
  12414. (if (looking-at org-radio-target-regexp)
  12415. (push (org-point-in-group p 0 :radio-target) clist))
  12416. (goto-char p))
  12417. ((setq o (car (delq nil
  12418. (mapcar
  12419. (lambda (x)
  12420. (if (memq x org-latex-fragment-image-overlays) x))
  12421. (org-overlays-at (point))))))
  12422. (push (list :latex-fragment
  12423. (org-overlay-start o) (org-overlay-end o)) clist)
  12424. (push (list :latex-preview
  12425. (org-overlay-start o) (org-overlay-end o)) clist))
  12426. ((org-inside-LaTeX-fragment-p)
  12427. ;; FIXME: positions wrong.
  12428. (push (list :latex-fragment (point) (point)) clist)))
  12429. (setq clist (nreverse (delq nil clist)))
  12430. clist))
  12431. ;; FIXME: Compare with at-regexp-p Do we need both?
  12432. (defun org-in-regexp (re &optional nlines visually)
  12433. "Check if point is inside a match of regexp.
  12434. Normally only the current line is checked, but you can include NLINES extra
  12435. lines both before and after point into the search.
  12436. If VISUALLY is set, require that the cursor is not after the match but
  12437. really on, so that the block visually is on the match."
  12438. (catch 'exit
  12439. (let ((pos (point))
  12440. (eol (point-at-eol (+ 1 (or nlines 0))))
  12441. (inc (if visually 1 0)))
  12442. (save-excursion
  12443. (beginning-of-line (- 1 (or nlines 0)))
  12444. (while (re-search-forward re eol t)
  12445. (if (and (<= (match-beginning 0) pos)
  12446. (>= (+ inc (match-end 0)) pos))
  12447. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12448. (defun org-at-regexp-p (regexp)
  12449. "Is point inside a match of REGEXP in the current line?"
  12450. (catch 'exit
  12451. (save-excursion
  12452. (let ((pos (point)) (end (point-at-eol)))
  12453. (beginning-of-line 1)
  12454. (while (re-search-forward regexp end t)
  12455. (if (and (<= (match-beginning 0) pos)
  12456. (>= (match-end 0) pos))
  12457. (throw 'exit t)))
  12458. nil))))
  12459. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12460. "Call `multi-occur' with buffers for all agenda files."
  12461. (interactive "sOrg-files matching: \np")
  12462. (let* ((files (org-agenda-files))
  12463. (tnames (mapcar 'file-truename files))
  12464. (extra org-agenda-text-search-extra-files)
  12465. f)
  12466. (when (eq (car extra) 'agenda-archives)
  12467. (setq extra (cdr extra))
  12468. (setq files (org-add-archive-files files)))
  12469. (while (setq f (pop extra))
  12470. (unless (member (file-truename f) tnames)
  12471. (add-to-list 'files f 'append)
  12472. (add-to-list 'tnames (file-truename f) 'append)))
  12473. (multi-occur
  12474. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12475. regexp)))
  12476. (if (boundp 'occur-mode-find-occurrence-hook)
  12477. ;; Emacs 23
  12478. (add-hook 'occur-mode-find-occurrence-hook
  12479. (lambda ()
  12480. (when (org-mode-p)
  12481. (org-reveal))))
  12482. ;; Emacs 22
  12483. (defadvice occur-mode-goto-occurrence
  12484. (after org-occur-reveal activate)
  12485. (and (org-mode-p) (org-reveal)))
  12486. (defadvice occur-mode-goto-occurrence-other-window
  12487. (after org-occur-reveal activate)
  12488. (and (org-mode-p) (org-reveal)))
  12489. (defadvice occur-mode-display-occurrence
  12490. (after org-occur-reveal activate)
  12491. (when (org-mode-p)
  12492. (let ((pos (occur-mode-find-occurrence)))
  12493. (with-current-buffer (marker-buffer pos)
  12494. (save-excursion
  12495. (goto-char pos)
  12496. (org-reveal)))))))
  12497. (defun org-uniquify (list)
  12498. "Remove duplicate elements from LIST."
  12499. (let (res)
  12500. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12501. res))
  12502. (defun org-delete-all (elts list)
  12503. "Remove all elements in ELTS from LIST."
  12504. (while elts
  12505. (setq list (delete (pop elts) list)))
  12506. list)
  12507. (defun org-back-over-empty-lines ()
  12508. "Move backwards over witespace, to the beginning of the first empty line.
  12509. Returns the number of empty lines passed."
  12510. (let ((pos (point)))
  12511. (skip-chars-backward " \t\n\r")
  12512. (beginning-of-line 2)
  12513. (goto-char (min (point) pos))
  12514. (count-lines (point) pos)))
  12515. (defun org-skip-whitespace ()
  12516. (skip-chars-forward " \t\n\r"))
  12517. (defun org-point-in-group (point group &optional context)
  12518. "Check if POINT is in match-group GROUP.
  12519. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12520. match. If the match group does ot exist or point is not inside it,
  12521. return nil."
  12522. (and (match-beginning group)
  12523. (>= point (match-beginning group))
  12524. (<= point (match-end group))
  12525. (if context
  12526. (list context (match-beginning group) (match-end group))
  12527. t)))
  12528. (defun org-switch-to-buffer-other-window (&rest args)
  12529. "Switch to buffer in a second window on the current frame.
  12530. In particular, do not allow pop-up frames."
  12531. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12532. special-display-function)
  12533. (apply 'switch-to-buffer-other-window args)))
  12534. (defun org-combine-plists (&rest plists)
  12535. "Create a single property list from all plists in PLISTS.
  12536. The process starts by copying the first list, and then setting properties
  12537. from the other lists. Settings in the last list are the most significant
  12538. ones and overrule settings in the other lists."
  12539. (let ((rtn (copy-sequence (pop plists)))
  12540. p v ls)
  12541. (while plists
  12542. (setq ls (pop plists))
  12543. (while ls
  12544. (setq p (pop ls) v (pop ls))
  12545. (setq rtn (plist-put rtn p v))))
  12546. rtn))
  12547. (defun org-move-line-down (arg)
  12548. "Move the current line down. With prefix argument, move it past ARG lines."
  12549. (interactive "p")
  12550. (let ((col (current-column))
  12551. beg end pos)
  12552. (beginning-of-line 1) (setq beg (point))
  12553. (beginning-of-line 2) (setq end (point))
  12554. (beginning-of-line (+ 1 arg))
  12555. (setq pos (move-marker (make-marker) (point)))
  12556. (insert (delete-and-extract-region beg end))
  12557. (goto-char pos)
  12558. (org-move-to-column col)))
  12559. (defun org-move-line-up (arg)
  12560. "Move the current line up. With prefix argument, move it past ARG lines."
  12561. (interactive "p")
  12562. (let ((col (current-column))
  12563. beg end pos)
  12564. (beginning-of-line 1) (setq beg (point))
  12565. (beginning-of-line 2) (setq end (point))
  12566. (beginning-of-line (- arg))
  12567. (setq pos (move-marker (make-marker) (point)))
  12568. (insert (delete-and-extract-region beg end))
  12569. (goto-char pos)
  12570. (org-move-to-column col)))
  12571. (defun org-replace-escapes (string table)
  12572. "Replace %-escapes in STRING with values in TABLE.
  12573. TABLE is an association list with keys like \"%a\" and string values.
  12574. The sequences in STRING may contain normal field width and padding information,
  12575. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12576. so values can contain further %-escapes if they are define later in TABLE."
  12577. (let ((case-fold-search nil)
  12578. e re rpl)
  12579. (while (setq e (pop table))
  12580. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12581. (while (string-match re string)
  12582. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12583. (cdr e)))
  12584. (setq string (replace-match rpl t t string))))
  12585. string))
  12586. (defun org-sublist (list start end)
  12587. "Return a section of LIST, from START to END.
  12588. Counting starts at 1."
  12589. (let (rtn (c start))
  12590. (setq list (nthcdr (1- start) list))
  12591. (while (and list (<= c end))
  12592. (push (pop list) rtn)
  12593. (setq c (1+ c)))
  12594. (nreverse rtn)))
  12595. (defun org-find-base-buffer-visiting (file)
  12596. "Like `find-buffer-visiting' but alway return the base buffer and
  12597. not an indirect buffer."
  12598. (let ((buf (find-buffer-visiting file)))
  12599. (if buf
  12600. (or (buffer-base-buffer buf) buf)
  12601. nil)))
  12602. (defun org-image-file-name-regexp ()
  12603. "Return regexp matching the file names of images."
  12604. (if (fboundp 'image-file-name-regexp)
  12605. (image-file-name-regexp)
  12606. (let ((image-file-name-extensions
  12607. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12608. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12609. (concat "\\."
  12610. (regexp-opt (nconc (mapcar 'upcase
  12611. image-file-name-extensions)
  12612. image-file-name-extensions)
  12613. t)
  12614. "\\'"))))
  12615. (defun org-file-image-p (file)
  12616. "Return non-nil if FILE is an image."
  12617. (save-match-data
  12618. (string-match (org-image-file-name-regexp) file)))
  12619. (defun org-get-cursor-date ()
  12620. "Return the date at cursor in as a time.
  12621. This works in the calendar and in the agenda, anywhere else it just
  12622. returns the current time."
  12623. (let (date day defd)
  12624. (cond
  12625. ((eq major-mode 'calendar-mode)
  12626. (setq date (calendar-cursor-to-date)
  12627. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12628. ((eq major-mode 'org-agenda-mode)
  12629. (setq day (get-text-property (point) 'day))
  12630. (if day
  12631. (setq date (calendar-gregorian-from-absolute day)
  12632. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12633. (nth 2 date))))))
  12634. (or defd (current-time))))
  12635. (defvar org-agenda-action-marker (make-marker)
  12636. "Marker pointing to the entry for the next agenda action.")
  12637. (defun org-mark-entry-for-agenda-action ()
  12638. "Mark the current entry as target of an agenda action.
  12639. Agenda actions are actions executed from the agenda with the key `k',
  12640. which make use of the date at the cursor."
  12641. (interactive)
  12642. (move-marker org-agenda-action-marker
  12643. (save-excursion (org-back-to-heading t) (point))
  12644. (current-buffer))
  12645. (message
  12646. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12647. ;;; Paragraph filling stuff.
  12648. ;; We want this to be just right, so use the full arsenal.
  12649. (defun org-indent-line-function ()
  12650. "Indent line like previous, but further if previous was headline or item."
  12651. (interactive)
  12652. (let* ((pos (point))
  12653. (itemp (org-at-item-p))
  12654. column bpos bcol tpos tcol bullet btype bullet-type)
  12655. ;; Find the previous relevant line
  12656. (beginning-of-line 1)
  12657. (cond
  12658. ((looking-at "#") (setq column 0))
  12659. ((looking-at "\\*+ ") (setq column 0))
  12660. (t
  12661. (beginning-of-line 0)
  12662. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12663. (beginning-of-line 0))
  12664. (cond
  12665. ((looking-at "\\*+[ \t]+")
  12666. (if (not org-adapt-indentation)
  12667. (setq column 0)
  12668. (goto-char (match-end 0))
  12669. (setq column (current-column))))
  12670. ((org-in-item-p)
  12671. (org-beginning-of-item)
  12672. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12673. (setq bpos (match-beginning 1) tpos (match-end 0)
  12674. bcol (progn (goto-char bpos) (current-column))
  12675. tcol (progn (goto-char tpos) (current-column))
  12676. bullet (match-string 1)
  12677. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12678. (if (> tcol (+ bcol org-description-max-indent))
  12679. (setq tcol (+ bcol 5)))
  12680. (if (not itemp)
  12681. (setq column tcol)
  12682. (goto-char pos)
  12683. (beginning-of-line 1)
  12684. (if (looking-at "\\S-")
  12685. (progn
  12686. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12687. (setq bullet (match-string 1)
  12688. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12689. (setq column (if (equal btype bullet-type) bcol tcol)))
  12690. (setq column (org-get-indentation)))))
  12691. (t (setq column (org-get-indentation))))))
  12692. (goto-char pos)
  12693. (if (<= (current-column) (current-indentation))
  12694. (org-indent-line-to column)
  12695. (save-excursion (org-indent-line-to column)))
  12696. (setq column (current-column))
  12697. (beginning-of-line 1)
  12698. (if (looking-at
  12699. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12700. (replace-match (concat "\\1" (format org-property-format
  12701. (match-string 2) (match-string 3)))
  12702. t nil))
  12703. (org-move-to-column column)))
  12704. (defun org-set-autofill-regexps ()
  12705. (interactive)
  12706. ;; In the paragraph separator we include headlines, because filling
  12707. ;; text in a line directly attached to a headline would otherwise
  12708. ;; fill the headline as well.
  12709. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12710. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12711. ;; The paragraph starter includes hand-formatted lists.
  12712. (org-set-local 'paragraph-start
  12713. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12714. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12715. ;; But only if the user has not turned off tables or fixed-width regions
  12716. (org-set-local
  12717. 'auto-fill-inhibit-regexp
  12718. (concat "\\*+ \\|#\\+"
  12719. "\\|[ \t]*" org-keyword-time-regexp
  12720. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12721. (concat
  12722. "\\|[ \t]*["
  12723. (if org-enable-table-editor "|" "")
  12724. (if org-enable-fixed-width-editor ":" "")
  12725. "]"))))
  12726. ;; We use our own fill-paragraph function, to make sure that tables
  12727. ;; and fixed-width regions are not wrapped. That function will pass
  12728. ;; through to `fill-paragraph' when appropriate.
  12729. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12730. ; Adaptive filling: To get full control, first make sure that
  12731. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12732. (org-set-local 'adaptive-fill-regexp "\000")
  12733. (org-set-local 'adaptive-fill-function
  12734. 'org-adaptive-fill-function)
  12735. (org-set-local
  12736. 'align-mode-rules-list
  12737. '((org-in-buffer-settings
  12738. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12739. (modes . '(org-mode))))))
  12740. (defun org-fill-paragraph (&optional justify)
  12741. "Re-align a table, pass through to fill-paragraph if no table."
  12742. (let ((table-p (org-at-table-p))
  12743. (table.el-p (org-at-table.el-p)))
  12744. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12745. (save-excursion (goto-char (point-at-bol))
  12746. (looking-at outline-regexp)))
  12747. t) ; skip headlines
  12748. (table.el-p t) ; skip table.el tables
  12749. (table-p (org-table-align) t) ; align org-mode tables
  12750. (t nil)))) ; call paragraph-fill
  12751. ;; For reference, this is the default value of adaptive-fill-regexp
  12752. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12753. (defun org-adaptive-fill-function ()
  12754. "Return a fill prefix for org-mode files.
  12755. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12756. work correctly."
  12757. (cond ((looking-at "#[ \t]+")
  12758. (match-string 0))
  12759. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12760. (save-excursion
  12761. (if (> (match-end 1) (+ (match-beginning 1)
  12762. org-description-max-indent))
  12763. (goto-char (+ (match-beginning 1) 5))
  12764. (goto-char (match-end 0)))
  12765. (make-string (current-column) ?\ )))
  12766. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12767. (save-excursion
  12768. (goto-char (match-end 0))
  12769. (make-string (current-column) ?\ )))
  12770. (t nil)))
  12771. ;;; Other stuff.
  12772. (defun org-toggle-fixed-width-section (arg)
  12773. "Toggle the fixed-width export.
  12774. If there is no active region, the QUOTE keyword at the current headline is
  12775. inserted or removed. When present, it causes the text between this headline
  12776. and the next to be exported as fixed-width text, and unmodified.
  12777. If there is an active region, this command adds or removes a colon as the
  12778. first character of this line. If the first character of a line is a colon,
  12779. this line is also exported in fixed-width font."
  12780. (interactive "P")
  12781. (let* ((cc 0)
  12782. (regionp (org-region-active-p))
  12783. (beg (if regionp (region-beginning) (point)))
  12784. (end (if regionp (region-end)))
  12785. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12786. (case-fold-search nil)
  12787. (re "[ \t]*\\(:\\)")
  12788. off)
  12789. (if regionp
  12790. (save-excursion
  12791. (goto-char beg)
  12792. (setq cc (current-column))
  12793. (beginning-of-line 1)
  12794. (setq off (looking-at re))
  12795. (while (> nlines 0)
  12796. (setq nlines (1- nlines))
  12797. (beginning-of-line 1)
  12798. (cond
  12799. (arg
  12800. (org-move-to-column cc t)
  12801. (insert ":\n")
  12802. (forward-line -1))
  12803. ((and off (looking-at re))
  12804. (replace-match "" t t nil 1))
  12805. ((not off) (org-move-to-column cc t) (insert ":")))
  12806. (forward-line 1)))
  12807. (save-excursion
  12808. (org-back-to-heading)
  12809. (if (looking-at (concat outline-regexp
  12810. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12811. (replace-match "" t t nil 1)
  12812. (if (looking-at outline-regexp)
  12813. (progn
  12814. (goto-char (match-end 0))
  12815. (insert org-quote-string " "))))))))
  12816. ;;;; Functions extending outline functionality
  12817. (defun org-beginning-of-line (&optional arg)
  12818. "Go to the beginning of the current line. If that is invisible, continue
  12819. to a visible line beginning. This makes the function of C-a more intuitive.
  12820. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12821. first attempt, and only move to after the tags when the cursor is already
  12822. beyond the end of the headline."
  12823. (interactive "P")
  12824. (let ((pos (point)) refpos)
  12825. (beginning-of-line 1)
  12826. (if (bobp)
  12827. nil
  12828. (backward-char 1)
  12829. (if (org-invisible-p)
  12830. (while (and (not (bobp)) (org-invisible-p))
  12831. (backward-char 1)
  12832. (beginning-of-line 1))
  12833. (forward-char 1)))
  12834. (when org-special-ctrl-a/e
  12835. (cond
  12836. ((and (looking-at org-complex-heading-regexp)
  12837. (= (char-after (match-end 1)) ?\ ))
  12838. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12839. (point-at-eol)))
  12840. (goto-char
  12841. (if (eq org-special-ctrl-a/e t)
  12842. (cond ((> pos refpos) refpos)
  12843. ((= pos (point)) refpos)
  12844. (t (point)))
  12845. (cond ((> pos (point)) (point))
  12846. ((not (eq last-command this-command)) (point))
  12847. (t refpos)))))
  12848. ((org-at-item-p)
  12849. (goto-char
  12850. (if (eq org-special-ctrl-a/e t)
  12851. (cond ((> pos (match-end 4)) (match-end 4))
  12852. ((= pos (point)) (match-end 4))
  12853. (t (point)))
  12854. (cond ((> pos (point)) (point))
  12855. ((not (eq last-command this-command)) (point))
  12856. (t (match-end 4))))))))
  12857. (org-no-warnings
  12858. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12859. (defun org-end-of-line (&optional arg)
  12860. "Go to the end of the line.
  12861. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12862. first attempt, and only move to after the tags when the cursor is already
  12863. beyond the end of the headline."
  12864. (interactive "P")
  12865. (if (or (not org-special-ctrl-a/e)
  12866. (not (org-on-heading-p)))
  12867. (end-of-line arg)
  12868. (let ((pos (point)))
  12869. (beginning-of-line 1)
  12870. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12871. (if (eq org-special-ctrl-a/e t)
  12872. (if (or (< pos (match-beginning 1))
  12873. (= pos (match-end 0)))
  12874. (goto-char (match-beginning 1))
  12875. (goto-char (match-end 0)))
  12876. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12877. (goto-char (match-end 0))
  12878. (goto-char (match-beginning 1))))
  12879. (end-of-line arg))))
  12880. (org-no-warnings
  12881. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12882. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12883. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12884. (defun org-kill-line (&optional arg)
  12885. "Kill line, to tags or end of line."
  12886. (interactive "P")
  12887. (cond
  12888. ((or (not org-special-ctrl-k)
  12889. (bolp)
  12890. (not (org-on-heading-p)))
  12891. (call-interactively 'kill-line))
  12892. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12893. (kill-region (point) (match-beginning 1))
  12894. (org-set-tags nil t))
  12895. (t (kill-region (point) (point-at-eol)))))
  12896. (define-key org-mode-map "\C-k" 'org-kill-line)
  12897. (defun org-yank (&optional arg)
  12898. "Yank. If the kill is a subtree, treat it specially.
  12899. This command will look at the current kill and check if is a single
  12900. subtree, or a series of subtrees[1]. If it passes the test, and if the
  12901. cursor is at the beginning of a line or after the stars of a currently
  12902. empty headline, then the yank is handeled specially. How exactly depends
  12903. on the value of the following variables, both set by default.
  12904. org-yank-folded-subtrees
  12905. When set, the subree(s) will be folded after insertion, but only
  12906. if doing so would now swallow text after the yanked text.
  12907. org-yank-adjusted-subtrees
  12908. When set, the subtree will be promoted or demoted in order to
  12909. fit into the local outline tree structure, which means that the level
  12910. will be adjusted so that it becomes the smaller one of the two
  12911. *visible* surrounding headings.
  12912. Any prefix to this command will cause `yank' to be called directly with
  12913. no special treatment. In particular, a simple `C-u' prefix will just
  12914. plainly yank the text as it is.
  12915. \[1] Basically, the test checks if the first non-white line is a heading
  12916. and if there are no other headings with fewer stars."
  12917. (interactive "P")
  12918. (setq this-command 'yank)
  12919. (if arg
  12920. (call-interactively 'yank)
  12921. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  12922. (and (org-kill-is-subtree-p)
  12923. (or (bolp)
  12924. (and (looking-at "[ \t]*$")
  12925. (string-match
  12926. "\\`\\*+\\'"
  12927. (buffer-substring (point-at-bol) (point)))))))
  12928. swallowp)
  12929. (cond
  12930. ((and subtreep org-yank-folded-subtrees)
  12931. (let ((beg (point))
  12932. end)
  12933. (if (and subtreep org-yank-adjusted-subtrees)
  12934. (org-paste-subtree nil nil 'for-yank)
  12935. (call-interactively 'yank))
  12936. (setq end (point))
  12937. (goto-char beg)
  12938. (when (and (bolp) subtreep
  12939. (not (setq swallowp
  12940. (org-yank-folding-would-swallow-text beg end))))
  12941. (or (looking-at outline-regexp)
  12942. (re-search-forward (concat "^" outline-regexp) end t))
  12943. (while (and (< (point) end) (looking-at outline-regexp))
  12944. (hide-subtree)
  12945. (org-cycle-show-empty-lines 'folded)
  12946. (condition-case nil
  12947. (outline-forward-same-level 1)
  12948. (error (goto-char end)))))
  12949. (when swallowp
  12950. (message
  12951. "Yanked text not folded because that would swallow text"))
  12952. (goto-char end)
  12953. (skip-chars-forward " \t\n\r")
  12954. (beginning-of-line 1)
  12955. (push-mark beg 'nomsg)))
  12956. ((and subtreep org-yank-adjusted-subtrees)
  12957. (let ((beg (point-at-bol)))
  12958. (org-paste-subtree nil nil 'for-yank)
  12959. (push-mark beg 'nomsg)))
  12960. (t
  12961. (call-interactively 'yank))))))
  12962. (defun org-yank-folding-would-swallow-text (beg end)
  12963. "Would hide-subtree at BEG swallow any text after END?"
  12964. (let (level)
  12965. (save-excursion
  12966. (goto-char beg)
  12967. (when (or (looking-at outline-regexp)
  12968. (re-search-forward (concat "^" outline-regexp) end t))
  12969. (setq level (org-outline-level)))
  12970. (goto-char end)
  12971. (skip-chars-forward " \t\r\n\v\f")
  12972. (if (or (eobp)
  12973. (and (bolp) (looking-at org-outline-regexp)
  12974. (<= (org-outline-level) level)))
  12975. nil ; Nothing would be swallowed
  12976. t)))) ; something would swallow
  12977. (define-key org-mode-map "\C-y" 'org-yank)
  12978. (defun org-invisible-p ()
  12979. "Check if point is at a character currently not visible."
  12980. ;; Early versions of noutline don't have `outline-invisible-p'.
  12981. (if (fboundp 'outline-invisible-p)
  12982. (outline-invisible-p)
  12983. (get-char-property (point) 'invisible)))
  12984. (defun org-invisible-p2 ()
  12985. "Check if point is at a character currently not visible."
  12986. (save-excursion
  12987. (if (and (eolp) (not (bobp))) (backward-char 1))
  12988. ;; Early versions of noutline don't have `outline-invisible-p'.
  12989. (if (fboundp 'outline-invisible-p)
  12990. (outline-invisible-p)
  12991. (get-char-property (point) 'invisible))))
  12992. (defun org-back-to-heading (&optional invisible-ok)
  12993. "Call `outline-back-to-heading', but provide a better error message."
  12994. (condition-case nil
  12995. (outline-back-to-heading invisible-ok)
  12996. (error (error "Before first headline at position %d in buffer %s"
  12997. (point) (current-buffer)))))
  12998. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12999. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13000. (defun org-at-heading-or-item-p ()
  13001. (or (org-on-heading-p) (org-at-item-p)))
  13002. (defun org-on-target-p ()
  13003. (or (org-in-regexp org-radio-target-regexp)
  13004. (org-in-regexp org-target-regexp)))
  13005. (defun org-up-heading-all (arg)
  13006. "Move to the heading line of which the present line is a subheading.
  13007. This function considers both visible and invisible heading lines.
  13008. With argument, move up ARG levels."
  13009. (if (fboundp 'outline-up-heading-all)
  13010. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13011. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13012. (defun org-up-heading-safe ()
  13013. "Move to the heading line of which the present line is a subheading.
  13014. This version will not throw an error. It will return the level of the
  13015. headline found, or nil if no higher level is found."
  13016. (let ((pos (point)) start-level level
  13017. (re (concat "^" outline-regexp)))
  13018. (catch 'exit
  13019. (org-back-to-heading t)
  13020. (setq start-level (funcall outline-level))
  13021. (if (equal start-level 1) (throw 'exit nil))
  13022. (while (re-search-backward re nil t)
  13023. (setq level (funcall outline-level))
  13024. (if (< level start-level) (throw 'exit level)))
  13025. nil)))
  13026. (defun org-first-sibling-p ()
  13027. "Is this heading the first child of its parents?"
  13028. (interactive)
  13029. (let ((re (concat "^" outline-regexp))
  13030. level l)
  13031. (unless (org-at-heading-p t)
  13032. (error "Not at a heading"))
  13033. (setq level (funcall outline-level))
  13034. (save-excursion
  13035. (if (not (re-search-backward re nil t))
  13036. t
  13037. (setq l (funcall outline-level))
  13038. (< l level)))))
  13039. (defun org-goto-sibling (&optional previous)
  13040. "Goto the next sibling, even if it is invisible.
  13041. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13042. when a sibling was found. When none is found, return nil and don't
  13043. move point."
  13044. (let ((fun (if previous 're-search-backward 're-search-forward))
  13045. (pos (point))
  13046. (re (concat "^" outline-regexp))
  13047. level l)
  13048. (when (condition-case nil (org-back-to-heading t) (error nil))
  13049. (setq level (funcall outline-level))
  13050. (catch 'exit
  13051. (or previous (forward-char 1))
  13052. (while (funcall fun re nil t)
  13053. (setq l (funcall outline-level))
  13054. (when (< l level) (goto-char pos) (throw 'exit nil))
  13055. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13056. (goto-char pos)
  13057. nil))))
  13058. (defun org-show-siblings ()
  13059. "Show all siblings of the current headline."
  13060. (save-excursion
  13061. (while (org-goto-sibling) (org-flag-heading nil)))
  13062. (save-excursion
  13063. (while (org-goto-sibling 'previous)
  13064. (org-flag-heading nil))))
  13065. (defun org-show-hidden-entry ()
  13066. "Show an entry where even the heading is hidden."
  13067. (save-excursion
  13068. (org-show-entry)))
  13069. (defun org-flag-heading (flag &optional entry)
  13070. "Flag the current heading. FLAG non-nil means make invisible.
  13071. When ENTRY is non-nil, show the entire entry."
  13072. (save-excursion
  13073. (org-back-to-heading t)
  13074. ;; Check if we should show the entire entry
  13075. (if entry
  13076. (progn
  13077. (org-show-entry)
  13078. (save-excursion
  13079. (and (outline-next-heading)
  13080. (org-flag-heading nil))))
  13081. (outline-flag-region (max (point-min) (1- (point)))
  13082. (save-excursion (outline-end-of-heading) (point))
  13083. flag))))
  13084. (defun org-forward-same-level (arg)
  13085. "Move forward to the ARG'th subheading at same level as this one.
  13086. Stop at the first and last subheadings of a superior heading.
  13087. This is like outline-forward-same-level, but invisible headings are ok."
  13088. (interactive "p")
  13089. (org-back-to-heading t)
  13090. (while (> arg 0)
  13091. (let ((point-to-move-to (save-excursion
  13092. (org-get-next-sibling))))
  13093. (if point-to-move-to
  13094. (progn
  13095. (goto-char point-to-move-to)
  13096. (setq arg (1- arg)))
  13097. (progn
  13098. (setq arg 0)
  13099. (error "No following same-level heading"))))))
  13100. (defun org-get-next-sibling ()
  13101. "Move to next heading of the same level, and return point.
  13102. If there is no such heading, return nil.
  13103. This is like outline-next-sibling, but invisible headings are ok."
  13104. (let ((level (funcall outline-level)))
  13105. (outline-next-heading)
  13106. (while (and (not (eobp)) (> (funcall outline-level) level))
  13107. (outline-next-heading))
  13108. (if (or (eobp) (< (funcall outline-level) level))
  13109. nil
  13110. (point))))
  13111. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13112. ;; This is an exact copy of the original function, but it uses
  13113. ;; `org-back-to-heading', to make it work also in invisible
  13114. ;; trees. And is uses an invisible-OK argument.
  13115. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13116. (org-back-to-heading invisible-OK)
  13117. (let ((first t)
  13118. (level (funcall outline-level)))
  13119. (while (and (not (eobp))
  13120. (or first (> (funcall outline-level) level)))
  13121. (setq first nil)
  13122. (outline-next-heading))
  13123. (unless to-heading
  13124. (if (memq (preceding-char) '(?\n ?\^M))
  13125. (progn
  13126. ;; Go to end of line before heading
  13127. (forward-char -1)
  13128. (if (memq (preceding-char) '(?\n ?\^M))
  13129. ;; leave blank line before heading
  13130. (forward-char -1))))))
  13131. (point))
  13132. (defun org-show-subtree ()
  13133. "Show everything after this heading at deeper levels."
  13134. (outline-flag-region
  13135. (point)
  13136. (save-excursion
  13137. (outline-end-of-subtree) (outline-next-heading) (point))
  13138. nil))
  13139. (defun org-show-entry ()
  13140. "Show the body directly following this heading.
  13141. Show the heading too, if it is currently invisible."
  13142. (interactive)
  13143. (save-excursion
  13144. (condition-case nil
  13145. (progn
  13146. (org-back-to-heading t)
  13147. (outline-flag-region
  13148. (max (point-min) (1- (point)))
  13149. (save-excursion
  13150. (re-search-forward
  13151. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13152. (or (match-beginning 1) (point-max)))
  13153. nil))
  13154. (error nil))))
  13155. (defun org-make-options-regexp (kwds)
  13156. "Make a regular expression for keyword lines."
  13157. (concat
  13158. "^"
  13159. "#?[ \t]*\\+\\("
  13160. (mapconcat 'regexp-quote kwds "\\|")
  13161. "\\):[ \t]*"
  13162. "\\(.+\\)"))
  13163. ;; Make isearch reveal the necessary context
  13164. (defun org-isearch-end ()
  13165. "Reveal context after isearch exits."
  13166. (when isearch-success ; only if search was successful
  13167. (if (featurep 'xemacs)
  13168. ;; Under XEmacs, the hook is run in the correct place,
  13169. ;; we directly show the context.
  13170. (org-show-context 'isearch)
  13171. ;; In Emacs the hook runs *before* restoring the overlays.
  13172. ;; So we have to use a one-time post-command-hook to do this.
  13173. ;; (Emacs 22 has a special variable, see function `org-mode')
  13174. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13175. isearch-mode-end-hook-quit)
  13176. ;; Only when the isearch was not quitted.
  13177. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13178. 'append 'local)))))
  13179. (defun org-isearch-post-command ()
  13180. "Remove self from hook, and show context."
  13181. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13182. (org-show-context 'isearch))
  13183. ;;;; Integration with and fixes for other packages
  13184. ;;; Imenu support
  13185. (defvar org-imenu-markers nil
  13186. "All markers currently used by Imenu.")
  13187. (make-variable-buffer-local 'org-imenu-markers)
  13188. (defun org-imenu-new-marker (&optional pos)
  13189. "Return a new marker for use by Imenu, and remember the marker."
  13190. (let ((m (make-marker)))
  13191. (move-marker m (or pos (point)))
  13192. (push m org-imenu-markers)
  13193. m))
  13194. (defun org-imenu-get-tree ()
  13195. "Produce the index for Imenu."
  13196. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13197. (setq org-imenu-markers nil)
  13198. (let* ((n org-imenu-depth)
  13199. (re (concat "^" outline-regexp))
  13200. (subs (make-vector (1+ n) nil))
  13201. (last-level 0)
  13202. m tree level head)
  13203. (save-excursion
  13204. (save-restriction
  13205. (widen)
  13206. (goto-char (point-max))
  13207. (while (re-search-backward re nil t)
  13208. (setq level (org-reduced-level (funcall outline-level)))
  13209. (when (<= level n)
  13210. (looking-at org-complex-heading-regexp)
  13211. (setq head (org-link-display-format
  13212. (org-match-string-no-properties 4))
  13213. m (org-imenu-new-marker))
  13214. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13215. (if (>= level last-level)
  13216. (push (cons head m) (aref subs level))
  13217. (push (cons head (aref subs (1+ level))) (aref subs level))
  13218. (loop for i from (1+ level) to n do (aset subs i nil)))
  13219. (setq last-level level)))))
  13220. (aref subs 1)))
  13221. (eval-after-load "imenu"
  13222. '(progn
  13223. (add-hook 'imenu-after-jump-hook
  13224. (lambda ()
  13225. (if (eq major-mode 'org-mode)
  13226. (org-show-context 'org-goto))))))
  13227. (defun org-link-display-format (link)
  13228. "Replace a link with either the description, or the link target
  13229. if no description is present"
  13230. (save-match-data
  13231. (if (string-match org-bracket-link-analytic-regexp link)
  13232. (replace-match (or (match-string 5 link)
  13233. (concat (match-string 1 link)
  13234. (match-string 3 link)))
  13235. nil nil link)
  13236. link)))
  13237. ;; Speedbar support
  13238. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13239. "Overlay marking the agenda restriction line in speedbar.")
  13240. (org-overlay-put org-speedbar-restriction-lock-overlay
  13241. 'face 'org-agenda-restriction-lock)
  13242. (org-overlay-put org-speedbar-restriction-lock-overlay
  13243. 'help-echo "Agendas are currently limited to this item.")
  13244. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13245. (defun org-speedbar-set-agenda-restriction ()
  13246. "Restrict future agenda commands to the location at point in speedbar.
  13247. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13248. (interactive)
  13249. (require 'org-agenda)
  13250. (let (p m tp np dir txt w)
  13251. (cond
  13252. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13253. 'org-imenu t))
  13254. (setq m (get-text-property p 'org-imenu-marker))
  13255. (save-excursion
  13256. (save-restriction
  13257. (set-buffer (marker-buffer m))
  13258. (goto-char m)
  13259. (org-agenda-set-restriction-lock 'subtree))))
  13260. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13261. 'speedbar-function 'speedbar-find-file))
  13262. (setq tp (previous-single-property-change
  13263. (1+ p) 'speedbar-function)
  13264. np (next-single-property-change
  13265. tp 'speedbar-function)
  13266. dir (speedbar-line-directory)
  13267. txt (buffer-substring-no-properties (or tp (point-min))
  13268. (or np (point-max))))
  13269. (save-excursion
  13270. (save-restriction
  13271. (set-buffer (find-file-noselect
  13272. (let ((default-directory dir))
  13273. (expand-file-name txt))))
  13274. (unless (org-mode-p)
  13275. (error "Cannot restrict to non-Org-mode file"))
  13276. (org-agenda-set-restriction-lock 'file))))
  13277. (t (error "Don't know how to restrict Org-mode's agenda")))
  13278. (org-move-overlay org-speedbar-restriction-lock-overlay
  13279. (point-at-bol) (point-at-eol))
  13280. (setq current-prefix-arg nil)
  13281. (org-agenda-maybe-redo)))
  13282. (eval-after-load "speedbar"
  13283. '(progn
  13284. (speedbar-add-supported-extension ".org")
  13285. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13286. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13287. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13288. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13289. (add-hook 'speedbar-visiting-tag-hook
  13290. (lambda () (org-show-context 'org-goto)))))
  13291. ;;; Fixes and Hacks for problems with other packages
  13292. ;; Make flyspell not check words in links, to not mess up our keymap
  13293. (defun org-mode-flyspell-verify ()
  13294. "Don't let flyspell put overlays at active buttons."
  13295. (not (get-text-property (point) 'keymap)))
  13296. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13297. (eval-after-load "bookmark"
  13298. '(if (boundp 'bookmark-after-jump-hook)
  13299. ;; We can use the hook
  13300. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13301. ;; Hook not available, use advice
  13302. (defadvice bookmark-jump (after org-make-visible activate)
  13303. "Make the position visible."
  13304. (org-bookmark-jump-unhide))))
  13305. ;; Make sure saveplace show the location if it was hidden
  13306. (eval-after-load "saveplace"
  13307. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13308. "Make the position visible."
  13309. (org-bookmark-jump-unhide)))
  13310. (defun org-bookmark-jump-unhide ()
  13311. "Unhide the current position, to show the bookmark location."
  13312. (and (org-mode-p)
  13313. (or (org-invisible-p)
  13314. (save-excursion (goto-char (max (point-min) (1- (point))))
  13315. (org-invisible-p)))
  13316. (org-show-context 'bookmark-jump)))
  13317. ;; Make session.el ignore our circular variable
  13318. (eval-after-load "session"
  13319. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13320. ;;;; Experimental code
  13321. (defun org-closed-in-range ()
  13322. "Sparse tree of items closed in a certain time range.
  13323. Still experimental, may disappear in the future."
  13324. (interactive)
  13325. ;; Get the time interval from the user.
  13326. (let* ((time1 (time-to-seconds
  13327. (org-read-date nil 'to-time nil "Starting date: ")))
  13328. (time2 (time-to-seconds
  13329. (org-read-date nil 'to-time nil "End date:")))
  13330. ;; callback function
  13331. (callback (lambda ()
  13332. (let ((time
  13333. (time-to-seconds
  13334. (apply 'encode-time
  13335. (org-parse-time-string
  13336. (match-string 1))))))
  13337. ;; check if time in interval
  13338. (and (>= time time1) (<= time time2))))))
  13339. ;; make tree, check each match with the callback
  13340. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13341. ;;;; Finish up
  13342. (provide 'org)
  13343. (run-hooks 'org-load-hook)
  13344. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13345. ;;; org.el ends here