org.el 545 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706
  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.14
  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.14"
  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-w3m 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 " w3m: Special cut/past from w3m to Org." org-w3m)
  155. (const :tag " mouse: Additional mouse support" org-mouse)
  156. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  157. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  158. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  159. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  160. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  161. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  162. (const :tag "C eval: Include command output as text" org-eval)
  163. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  164. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  165. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  166. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  167. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  168. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  169. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  170. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  171. (const :tag "C registry: A registry for Org links" org-registry)
  172. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  173. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  174. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  175. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  176. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  177. (defgroup org-startup nil
  178. "Options concerning startup of Org-mode."
  179. :tag "Org Startup"
  180. :group 'org)
  181. (defcustom org-startup-folded t
  182. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  183. This can also be configured on a per-file basis by adding one of
  184. the following lines anywhere in the buffer:
  185. #+STARTUP: fold
  186. #+STARTUP: nofold
  187. #+STARTUP: content"
  188. :group 'org-startup
  189. :type '(choice
  190. (const :tag "nofold: show all" nil)
  191. (const :tag "fold: overview" t)
  192. (const :tag "content: all headlines" content)))
  193. (defcustom org-startup-truncated t
  194. "Non-nil means, entering Org-mode will set `truncate-lines'.
  195. This is useful since some lines containing links can be very long and
  196. uninteresting. Also tables look terrible when wrapped."
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-startup-align-all-tables nil
  200. "Non-nil means, align all tables when visiting a file.
  201. This is useful when the column width in tables is forced with <N> cookies
  202. in table fields. Such tables will look correct only after the first re-align.
  203. This can also be configured on a per-file basis by adding one of
  204. the following lines anywhere in the buffer:
  205. #+STARTUP: align
  206. #+STARTUP: noalign"
  207. :group 'org-startup
  208. :type 'boolean)
  209. (defcustom org-insert-mode-line-in-empty-file nil
  210. "Non-nil means insert the first line setting Org-mode in empty files.
  211. When the function `org-mode' is called interactively in an empty file, this
  212. normally means that the file name does not automatically trigger Org-mode.
  213. To ensure that the file will always be in Org-mode in the future, a
  214. line enforcing Org-mode will be inserted into the buffer, if this option
  215. has been set."
  216. :group 'org-startup
  217. :type 'boolean)
  218. (defcustom org-replace-disputed-keys nil
  219. "Non-nil means use alternative key bindings for some keys.
  220. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  221. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  222. If you want to use Org-mode together with one of these other modes,
  223. or more generally if you would like to move some Org-mode commands to
  224. other keys, set this variable and configure the keys with the variable
  225. `org-disputed-keys'.
  226. This option is only relevant at load-time of Org-mode, and must be set
  227. *before* org.el is loaded. Changing it requires a restart of Emacs to
  228. become effective."
  229. :group 'org-startup
  230. :type 'boolean)
  231. (defcustom org-use-extra-keys nil
  232. "Non-nil means use extra key sequence definitions for certain
  233. commands. This happens automatically if you run XEmacs or if
  234. window-system is nil. This variable lets you do the same
  235. manually. You must set it before loading org.
  236. Example: on Carbon Emacs 22 running graphically, with an external
  237. keyboard on a Powerbook, the default way of setting M-left might
  238. not work for either Alt or ESC. Setting this variable will make
  239. it work for ESC."
  240. :group 'org-startup
  241. :type 'boolean)
  242. (if (fboundp 'defvaralias)
  243. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  244. (defcustom org-disputed-keys
  245. '(([(shift up)] . [(meta p)])
  246. ([(shift down)] . [(meta n)])
  247. ([(shift left)] . [(meta -)])
  248. ([(shift right)] . [(meta +)])
  249. ([(control shift right)] . [(meta shift +)])
  250. ([(control shift left)] . [(meta shift -)]))
  251. "Keys for which Org-mode and other modes compete.
  252. This is an alist, cars are the default keys, second element specifies
  253. the alternative to use when `org-replace-disputed-keys' is t.
  254. Keys can be specified in any syntax supported by `define-key'.
  255. The value of this option takes effect only at Org-mode's startup,
  256. therefore you'll have to restart Emacs to apply it after changing."
  257. :group 'org-startup
  258. :type 'alist)
  259. (defun org-key (key)
  260. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  261. Or return the original if not disputed."
  262. (if org-replace-disputed-keys
  263. (let* ((nkey (key-description key))
  264. (x (org-find-if (lambda (x)
  265. (equal (key-description (car x)) nkey))
  266. org-disputed-keys)))
  267. (if x (cdr x) key))
  268. key))
  269. (defun org-find-if (predicate seq)
  270. (catch 'exit
  271. (while seq
  272. (if (funcall predicate (car seq))
  273. (throw 'exit (car seq))
  274. (pop seq)))))
  275. (defun org-defkey (keymap key def)
  276. "Define a key, possibly translated, as returned by `org-key'."
  277. (define-key keymap (org-key key) def))
  278. (defcustom org-ellipsis nil
  279. "The ellipsis to use in the Org-mode outline.
  280. When nil, just use the standard three dots. When a string, use that instead,
  281. When a face, use the standart 3 dots, but with the specified face.
  282. The change affects only Org-mode (which will then use its own display table).
  283. Changing this requires executing `M-x org-mode' in a buffer to become
  284. effective."
  285. :group 'org-startup
  286. :type '(choice (const :tag "Default" nil)
  287. (face :tag "Face" :value org-warning)
  288. (string :tag "String" :value "...#")))
  289. (defvar org-display-table nil
  290. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  291. (defgroup org-keywords nil
  292. "Keywords in Org-mode."
  293. :tag "Org Keywords"
  294. :group 'org)
  295. (defcustom org-deadline-string "DEADLINE:"
  296. "String to mark deadline entries.
  297. A deadline is this string, followed by a time stamp. Should be a word,
  298. terminated by a colon. You can insert a schedule keyword and
  299. a timestamp with \\[org-deadline].
  300. Changes become only effective after restarting Emacs."
  301. :group 'org-keywords
  302. :type 'string)
  303. (defcustom org-scheduled-string "SCHEDULED:"
  304. "String to mark scheduled TODO entries.
  305. A schedule is this string, followed by a time stamp. Should be a word,
  306. terminated by a colon. You can insert a schedule keyword and
  307. a timestamp with \\[org-schedule].
  308. Changes become only effective after restarting Emacs."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-closed-string "CLOSED:"
  312. "String used as the prefix for timestamps logging closing a TODO entry."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defcustom org-clock-string "CLOCK:"
  316. "String used as prefix for timestamps clocking work hours on an item."
  317. :group 'org-keywords
  318. :type 'string)
  319. (defcustom org-comment-string "COMMENT"
  320. "Entries starting with this keyword will never be exported.
  321. An entry can be toggled between COMMENT and normal with
  322. \\[org-toggle-comment].
  323. Changes become only effective after restarting Emacs."
  324. :group 'org-keywords
  325. :type 'string)
  326. (defcustom org-quote-string "QUOTE"
  327. "Entries starting with this keyword will be exported in fixed-width font.
  328. Quoting applies only to the text in the entry following the headline, and does
  329. not extend beyond the next headline, even if that is lower level.
  330. An entry can be toggled between QUOTE and normal with
  331. \\[org-toggle-fixed-width-section]."
  332. :group 'org-keywords
  333. :type 'string)
  334. (defconst org-repeat-re
  335. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  336. "Regular expression for specifying repeated events.
  337. After a match, group 1 contains the repeat expression.")
  338. (defgroup org-structure nil
  339. "Options concerning the general structure of Org-mode files."
  340. :tag "Org Structure"
  341. :group 'org)
  342. (defgroup org-reveal-location nil
  343. "Options about how to make context of a location visible."
  344. :tag "Org Reveal Location"
  345. :group 'org-structure)
  346. (defconst org-context-choice
  347. '(choice
  348. (const :tag "Always" t)
  349. (const :tag "Never" nil)
  350. (repeat :greedy t :tag "Individual contexts"
  351. (cons
  352. (choice :tag "Context"
  353. (const agenda)
  354. (const org-goto)
  355. (const occur-tree)
  356. (const tags-tree)
  357. (const link-search)
  358. (const mark-goto)
  359. (const bookmark-jump)
  360. (const isearch)
  361. (const default))
  362. (boolean))))
  363. "Contexts for the reveal options.")
  364. (defcustom org-show-hierarchy-above '((default . t))
  365. "Non-nil means, show full hierarchy when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the hierarchy of headings
  368. above the exposed location is shown.
  369. Turning this off for example for sparse trees makes them very compact.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. Valid contexts are
  372. agenda when exposing an entry from the agenda
  373. org-goto when using the command `org-goto' on key C-c C-j
  374. occur-tree when using the command `org-occur' on key C-c /
  375. tags-tree when constructing a sparse tree based on tags matches
  376. link-search when exposing search matches associated with a link
  377. mark-goto when exposing the jump goal of a mark
  378. bookmark-jump when exposing a bookmark location
  379. isearch when exiting from an incremental search
  380. default default for all contexts not set explicitly"
  381. :group 'org-reveal-location
  382. :type org-context-choice)
  383. (defcustom org-show-following-heading '((default . nil))
  384. "Non-nil means, show following heading when revealing a location.
  385. Org-mode often shows locations in an org-mode file which might have
  386. been invisible before. When this is set, the heading following the
  387. match is shown.
  388. Turning this off for example for sparse trees makes them very compact,
  389. but makes it harder to edit the location of the match. In such a case,
  390. use the command \\[org-reveal] to show more context.
  391. Instead of t, this can also be an alist specifying this option for different
  392. contexts. See `org-show-hierarchy-above' for valid contexts."
  393. :group 'org-reveal-location
  394. :type org-context-choice)
  395. (defcustom org-show-siblings '((default . nil) (isearch t))
  396. "Non-nil means, show all sibling heading when revealing a location.
  397. Org-mode often shows locations in an org-mode file which might have
  398. been invisible before. When this is set, the sibling of the current entry
  399. heading are all made visible. If `org-show-hierarchy-above' is t,
  400. the same happens on each level of the hierarchy above the current entry.
  401. By default this is on for the isearch context, off for all other contexts.
  402. Turning this off for example for sparse trees makes them very compact,
  403. but makes it harder to edit the location of the match. In such a case,
  404. use the command \\[org-reveal] to show more context.
  405. Instead of t, this can also be an alist specifying this option for different
  406. contexts. See `org-show-hierarchy-above' for valid contexts."
  407. :group 'org-reveal-location
  408. :type org-context-choice)
  409. (defcustom org-show-entry-below '((default . nil))
  410. "Non-nil means, show the entry below a headline when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the text below the headline that is
  413. exposed is also shown.
  414. By default this is off for all contexts.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. See `org-show-hierarchy-above' for valid contexts."
  417. :group 'org-reveal-location
  418. :type org-context-choice)
  419. (defcustom org-indirect-buffer-display 'other-window
  420. "How should indirect tree buffers be displayed?
  421. This applies to indirect buffers created with the commands
  422. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  423. Valid values are:
  424. current-window Display in the current window
  425. other-window Just display in another window.
  426. dedicated-frame Create one new frame, and re-use it each time.
  427. new-frame Make a new frame each time. Note that in this case
  428. previously-made indirect buffers are kept, and you need to
  429. kill these buffers yourself."
  430. :group 'org-structure
  431. :group 'org-agenda-windows
  432. :type '(choice
  433. (const :tag "In current window" current-window)
  434. (const :tag "In current frame, other window" other-window)
  435. (const :tag "Each time a new frame" new-frame)
  436. (const :tag "One dedicated frame" dedicated-frame)))
  437. (defgroup org-cycle nil
  438. "Options concerning visibility cycling in Org-mode."
  439. :tag "Org Cycle"
  440. :group 'org-structure)
  441. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  442. "Names of drawers. Drawers are not opened by cycling on the headline above.
  443. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  444. this:
  445. :DRAWERNAME:
  446. .....
  447. :END:
  448. The drawer \"PROPERTIES\" is special for capturing properties through
  449. the property API.
  450. Drawers can be defined on the per-file basis with a line like:
  451. #+DRAWERS: HIDDEN STATE PROPERTIES"
  452. :group 'org-structure
  453. :type '(repeat (string :tag "Drawer Name")))
  454. (defcustom org-cycle-global-at-bob nil
  455. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  456. This makes it possible to do global cycling without having to use S-TAB or
  457. C-u TAB. For this special case to work, the first line of the buffer
  458. must not be a headline - it may be empty ot some other text. When used in
  459. this way, `org-cycle-hook' is disables temporarily, to make sure the
  460. cursor stays at the beginning of the buffer.
  461. When this option is nil, don't do anything special at the beginning
  462. of the buffer."
  463. :group 'org-cycle
  464. :type 'boolean)
  465. (defcustom org-cycle-emulate-tab t
  466. "Where should `org-cycle' emulate TAB.
  467. nil Never
  468. white Only in completely white lines
  469. whitestart Only at the beginning of lines, before the first non-white char
  470. t Everywhere except in headlines
  471. exc-hl-bol Everywhere except at the start of a headline
  472. If TAB is used in a place where it does not emulate TAB, the current subtree
  473. visibility is cycled."
  474. :group 'org-cycle
  475. :type '(choice (const :tag "Never" nil)
  476. (const :tag "Only in completely white lines" white)
  477. (const :tag "Before first char in a line" whitestart)
  478. (const :tag "Everywhere except in headlines" t)
  479. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  480. ))
  481. (defcustom org-cycle-separator-lines 2
  482. "Number of empty lines needed to keep an empty line between collapsed trees.
  483. If you leave an empty line between the end of a subtree and the following
  484. headline, this empty line is hidden when the subtree is folded.
  485. Org-mode will leave (exactly) one empty line visible if the number of
  486. empty lines is equal or larger to the number given in this variable.
  487. So the default 2 means, at least 2 empty lines after the end of a subtree
  488. are needed to produce free space between a collapsed subtree and the
  489. following headline.
  490. Special case: when 0, never leave empty lines in collapsed view."
  491. :group 'org-cycle
  492. :type 'integer)
  493. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  494. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  495. org-cycle-hide-drawers
  496. org-cycle-show-empty-lines
  497. org-optimize-window-after-visibility-change)
  498. "Hook that is run after `org-cycle' has changed the buffer visibility.
  499. The function(s) in this hook must accept a single argument which indicates
  500. the new state that was set by the most recent `org-cycle' command. The
  501. argument is a symbol. After a global state change, it can have the values
  502. `overview', `content', or `all'. After a local state change, it can have
  503. the values `folded', `children', or `subtree'."
  504. :group 'org-cycle
  505. :type 'hook)
  506. (defgroup org-edit-structure nil
  507. "Options concerning structure editing in Org-mode."
  508. :tag "Org Edit Structure"
  509. :group 'org-structure)
  510. (defcustom org-odd-levels-only nil
  511. "Non-nil means, skip even levels and only use odd levels for the outline.
  512. This has the effect that two stars are being added/taken away in
  513. promotion/demotion commands. It also influences how levels are
  514. handled by the exporters.
  515. Changing it requires restart of `font-lock-mode' to become effective
  516. for fontification also in regions already fontified.
  517. You may also set this on a per-file basis by adding one of the following
  518. lines to the buffer:
  519. #+STARTUP: odd
  520. #+STARTUP: oddeven"
  521. :group 'org-edit-structure
  522. :group 'org-font-lock
  523. :type 'boolean)
  524. (defcustom org-adapt-indentation t
  525. "Non-nil means, adapt indentation when promoting and demoting.
  526. When this is set and the *entire* text in an entry is indented, the
  527. indentation is increased by one space in a demotion command, and
  528. decreased by one in a promotion command. If any line in the entry
  529. body starts at column 0, indentation is not changed at all."
  530. :group 'org-edit-structure
  531. :type 'boolean)
  532. (defcustom org-special-ctrl-a/e nil
  533. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  534. When t, `C-a' will bring back the cursor to the beginning of the
  535. headline text, i.e. after the stars and after a possible TODO keyword.
  536. In an item, this will be the position after the bullet.
  537. When the cursor is already at that position, another `C-a' will bring
  538. it to the beginning of the line.
  539. `C-e' will jump to the end of the headline, ignoring the presence of tags
  540. in the headline. A second `C-e' will then jump to the true end of the
  541. line, after any tags.
  542. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  543. and only a directly following, identical keypress will bring the cursor
  544. to the special positions."
  545. :group 'org-edit-structure
  546. :type '(choice
  547. (const :tag "off" nil)
  548. (const :tag "after bullet first" t)
  549. (const :tag "border first" reversed)))
  550. (if (fboundp 'defvaralias)
  551. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  552. (defcustom org-special-ctrl-k nil
  553. "Non-nil means `C-k' will behave specially in headlines.
  554. When nil, `C-k' will call the default `kill-line' command.
  555. When t, the following will happen while the cursor is in the headline:
  556. - When the cursor is at the beginning of a headline, kill the entire
  557. line and possible the folded subtree below the line.
  558. - When in the middle of the headline text, kill the headline up to the tags.
  559. - When after the headline text, kill the tags."
  560. :group 'org-edit-structure
  561. :type 'boolean)
  562. (defcustom org-yank-folded-subtrees t
  563. "Non-nil means, when yanking subtrees, fold them.
  564. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  565. it starts with a heading and all other headings in it are either children
  566. or siblings, then fold all the subtrees. However, do this only if no
  567. text after the yank would be swallowed into a folded tree by this action."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-yank-adjusted-subtrees t
  571. "Non-nil means, when yanking subtrees, adjust the level.
  572. With this setting, `org-paste-subtree' is used to insert the subtree, see
  573. this function for details."
  574. :group 'org-edit-structure
  575. :type 'boolean)
  576. (defcustom org-M-RET-may-split-line '((default . t))
  577. "Non-nil means, M-RET will split the line at the cursor position.
  578. When nil, it will go to the end of the line before making a
  579. new line.
  580. You may also set this option in a different way for different
  581. contexts. Valid contexts are:
  582. headline when creating a new headline
  583. item when creating a new item
  584. table in a table field
  585. default the value to be used for all contexts not explicitly
  586. customized"
  587. :group 'org-structure
  588. :group 'org-table
  589. :type '(choice
  590. (const :tag "Always" t)
  591. (const :tag "Never" nil)
  592. (repeat :greedy t :tag "Individual contexts"
  593. (cons
  594. (choice :tag "Context"
  595. (const headline)
  596. (const item)
  597. (const table)
  598. (const default))
  599. (boolean)))))
  600. (defcustom org-insert-heading-respect-content nil
  601. "Non-nil means, insert new headings after the current subtree.
  602. When nil, the new heading is created directly after the current line.
  603. The commands \\[org-insert-heading-respect-content] and
  604. \\[org-insert-todo-heading-respect-content] turn this variable on
  605. for the duration of the command."
  606. :group 'org-structure
  607. :type 'boolean)
  608. (defcustom org-blank-before-new-entry '((heading . nil)
  609. (plain-list-item . nil))
  610. "Should `org-insert-heading' leave a blank line before new heading/item?
  611. The value is an alist, with `heading' and `plain-list-item' as car,
  612. and a boolean flag as cdr."
  613. :group 'org-edit-structure
  614. :type '(list
  615. (cons (const heading) (boolean))
  616. (cons (const plain-list-item) (boolean))))
  617. (defcustom org-insert-heading-hook nil
  618. "Hook being run after inserting a new heading."
  619. :group 'org-edit-structure
  620. :type 'hook)
  621. (defcustom org-enable-fixed-width-editor t
  622. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  623. This currently only means, they are never auto-wrapped.
  624. When nil, such lines will be treated like ordinary lines.
  625. See also the QUOTE keyword."
  626. :group 'org-edit-structure
  627. :type 'boolean)
  628. (defcustom org-edit-src-region-extra nil
  629. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  630. For examples see the function `org-edit-src-find-region-and-lang'.
  631. The regular expression identifying the begin marker should end with a newline,
  632. and the regexp marking the end line should start with a newline, to make sure
  633. there are kept outside the narrowed region."
  634. :group 'org-edit-structure
  635. :type '(repeat
  636. (list
  637. (regexp :tag "begin regexp")
  638. (regexp :tag "end regexp")
  639. (choice :tag "language"
  640. (string :tag "specify")
  641. (integer :tag "from match group")
  642. (const :tag "from `lang' element")
  643. (const :tag "from `style' element")))))
  644. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  645. "The mode that should be used to edit fixed-width regions.
  646. These are the regions where each line starts with a colon."
  647. :group 'org-edit-structure
  648. :type '(choice
  649. (const artist-mode)
  650. (const picture-mode)
  651. (const fundamental-mode)
  652. (function :tag "Other (specify)")))
  653. (defcustom org-goto-auto-isearch t
  654. "Non-nil means, typing characters in org-goto starts incremental search."
  655. :group 'org-edit-structure
  656. :type 'boolean)
  657. (defgroup org-sparse-trees nil
  658. "Options concerning sparse trees in Org-mode."
  659. :tag "Org Sparse Trees"
  660. :group 'org-structure)
  661. (defcustom org-highlight-sparse-tree-matches t
  662. "Non-nil means, highlight all matches that define a sparse tree.
  663. The highlights will automatically disappear the next time the buffer is
  664. changed by an edit command."
  665. :group 'org-sparse-trees
  666. :type 'boolean)
  667. (defcustom org-remove-highlights-with-change t
  668. "Non-nil means, any change to the buffer will remove temporary highlights.
  669. Such highlights are created by `org-occur' and `org-clock-display'.
  670. When nil, `C-c C-c needs to be used to get rid of the highlights.
  671. The highlights created by `org-preview-latex-fragment' always need
  672. `C-c C-c' to be removed."
  673. :group 'org-sparse-trees
  674. :group 'org-time
  675. :type 'boolean)
  676. (defcustom org-occur-hook '(org-first-headline-recenter)
  677. "Hook that is run after `org-occur' has constructed a sparse tree.
  678. This can be used to recenter the window to show as much of the structure
  679. as possible."
  680. :group 'org-sparse-trees
  681. :type 'hook)
  682. (defgroup org-imenu-and-speedbar nil
  683. "Options concerning imenu and speedbar in Org-mode."
  684. :tag "Org Imenu and Speedbar"
  685. :group 'org-structure)
  686. (defcustom org-imenu-depth 2
  687. "The maximum level for Imenu access to Org-mode headlines.
  688. This also applied for speedbar access."
  689. :group 'org-imenu-and-speedbar
  690. :type 'number)
  691. (defgroup org-table nil
  692. "Options concerning tables in Org-mode."
  693. :tag "Org Table"
  694. :group 'org)
  695. (defcustom org-enable-table-editor 'optimized
  696. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  697. When nil, such lines will be treated like ordinary lines.
  698. When equal to the symbol `optimized', the table editor will be optimized to
  699. do the following:
  700. - Automatic overwrite mode in front of whitespace in table fields.
  701. This makes the structure of the table stay in tact as long as the edited
  702. field does not exceed the column width.
  703. - Minimize the number of realigns. Normally, the table is aligned each time
  704. TAB or RET are pressed to move to another field. With optimization this
  705. happens only if changes to a field might have changed the column width.
  706. Optimization requires replacing the functions `self-insert-command',
  707. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  708. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  709. very good at guessing when a re-align will be necessary, but you can always
  710. force one with \\[org-ctrl-c-ctrl-c].
  711. If you would like to use the optimized version in Org-mode, but the
  712. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  713. This variable can be used to turn on and off the table editor during a session,
  714. but in order to toggle optimization, a restart is required.
  715. See also the variable `org-table-auto-blank-field'."
  716. :group 'org-table
  717. :type '(choice
  718. (const :tag "off" nil)
  719. (const :tag "on" t)
  720. (const :tag "on, optimized" optimized)))
  721. (defcustom org-table-tab-recognizes-table.el t
  722. "Non-nil means, TAB will automatically notice a table.el table.
  723. When it sees such a table, it moves point into it and - if necessary -
  724. calls `table-recognize-table'."
  725. :group 'org-table-editing
  726. :type 'boolean)
  727. (defgroup org-link nil
  728. "Options concerning links in Org-mode."
  729. :tag "Org Link"
  730. :group 'org)
  731. (defvar org-link-abbrev-alist-local nil
  732. "Buffer-local version of `org-link-abbrev-alist', which see.
  733. The value of this is taken from the #+LINK lines.")
  734. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  735. (defcustom org-link-abbrev-alist nil
  736. "Alist of link abbreviations.
  737. The car of each element is a string, to be replaced at the start of a link.
  738. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  739. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  740. [[linkkey:tag][description]]
  741. If REPLACE is a string, the tag will simply be appended to create the link.
  742. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  743. the placeholder \"%h\" will cause a url-encoded version of the tag to
  744. be inserted at that point (see the function `url-hexify-string').
  745. REPLACE may also be a function that will be called with the tag as the
  746. only argument to create the link, which should be returned as a string.
  747. See the manual for examples."
  748. :group 'org-link
  749. :type '(repeat
  750. (cons
  751. (string :tag "Protocol")
  752. (choice
  753. (string :tag "Format")
  754. (function)))))
  755. (defcustom org-descriptive-links t
  756. "Non-nil means, hide link part and only show description of bracket links.
  757. Bracket links are like [[link][descritpion]]. This variable sets the initial
  758. state in new org-mode buffers. The setting can then be toggled on a
  759. per-buffer basis from the Org->Hyperlinks menu."
  760. :group 'org-link
  761. :type 'boolean)
  762. (defcustom org-link-file-path-type 'adaptive
  763. "How the path name in file links should be stored.
  764. Valid values are:
  765. relative Relative to the current directory, i.e. the directory of the file
  766. into which the link is being inserted.
  767. absolute Absolute path, if possible with ~ for home directory.
  768. noabbrev Absolute path, no abbreviation of home directory.
  769. adaptive Use relative path for files in the current directory and sub-
  770. directories of it. For other files, use an absolute path."
  771. :group 'org-link
  772. :type '(choice
  773. (const relative)
  774. (const absolute)
  775. (const noabbrev)
  776. (const adaptive)))
  777. (defcustom org-activate-links '(bracket angle plain radio tag date)
  778. "Types of links that should be activated in Org-mode files.
  779. This is a list of symbols, each leading to the activation of a certain link
  780. type. In principle, it does not hurt to turn on most link types - there may
  781. be a small gain when turning off unused link types. The types are:
  782. bracket The recommended [[link][description]] or [[link]] links with hiding.
  783. angular Links in angular brackes that may contain whitespace like
  784. <bbdb:Carsten Dominik>.
  785. plain Plain links in normal text, no whitespace, like http://google.com.
  786. radio Text that is matched by a radio target, see manual for details.
  787. tag Tag settings in a headline (link to tag search).
  788. date Time stamps (link to calendar).
  789. Changing this variable requires a restart of Emacs to become effective."
  790. :group 'org-link
  791. :type '(set (const :tag "Double bracket links (new style)" bracket)
  792. (const :tag "Angular bracket links (old style)" angular)
  793. (const :tag "Plain text links" plain)
  794. (const :tag "Radio target matches" radio)
  795. (const :tag "Tags" tag)
  796. (const :tag "Timestamps" date)))
  797. (defcustom org-make-link-description-function nil
  798. "Function to use to generate link descriptions from links. If
  799. nil the link location will be used. This function must take two
  800. parameters; the first is the link and the second the description
  801. org-insert-link has generated, and should return the description
  802. to use."
  803. :group 'org-link
  804. :type 'function)
  805. (defgroup org-link-store nil
  806. "Options concerning storing links in Org-mode."
  807. :tag "Org Store Link"
  808. :group 'org-link)
  809. (defcustom org-email-link-description-format "Email %c: %.30s"
  810. "Format of the description part of a link to an email or usenet message.
  811. The following %-excapes will be replaced by corresponding information:
  812. %F full \"From\" field
  813. %f name, taken from \"From\" field, address if no name
  814. %T full \"To\" field
  815. %t first name in \"To\" field, address if no name
  816. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  817. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  818. %s subject
  819. %m message-id.
  820. You may use normal field width specification between the % and the letter.
  821. This is for example useful to limit the length of the subject.
  822. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  823. :group 'org-link-store
  824. :type 'string)
  825. (defcustom org-from-is-user-regexp
  826. (let (r1 r2)
  827. (when (and user-mail-address (not (string= user-mail-address "")))
  828. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  829. (when (and user-full-name (not (string= user-full-name "")))
  830. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  831. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  832. "Regexp mached against the \"From:\" header of an email or usenet message.
  833. It should match if the message is from the user him/herself."
  834. :group 'org-link-store
  835. :type 'regexp)
  836. (defcustom org-context-in-file-links t
  837. "Non-nil means, file links from `org-store-link' contain context.
  838. A search string will be added to the file name with :: as separator and
  839. used to find the context when the link is activated by the command
  840. `org-open-at-point'.
  841. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  842. negates this setting for the duration of the command."
  843. :group 'org-link-store
  844. :type 'boolean)
  845. (defcustom org-keep-stored-link-after-insertion nil
  846. "Non-nil means, keep link in list for entire session.
  847. The command `org-store-link' adds a link pointing to the current
  848. location to an internal list. These links accumulate during a session.
  849. The command `org-insert-link' can be used to insert links into any
  850. Org-mode file (offering completion for all stored links). When this
  851. option is nil, every link which has been inserted once using \\[org-insert-link]
  852. will be removed from the list, to make completing the unused links
  853. more efficient."
  854. :group 'org-link-store
  855. :type 'boolean)
  856. (defgroup org-link-follow nil
  857. "Options concerning following links in Org-mode."
  858. :tag "Org Follow Link"
  859. :group 'org-link)
  860. (defcustom org-link-translation-function nil
  861. "Function to translate links with different syntax to Org syntax.
  862. This can be used to translate links created for example by the Planner
  863. or emacs-wiki packages to Org syntax.
  864. The function must accept two parameters, a TYPE containing the link
  865. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  866. which is everything after the link protocol. It should return a cons
  867. with possibly modifed values of type and path.
  868. Org contains a function for this, so if you set this variable to
  869. `org-translate-link-from-planner', you should be able follow many
  870. links created by planner."
  871. :group 'org-link-follow
  872. :type 'function)
  873. (defcustom org-follow-link-hook nil
  874. "Hook that is run after a link has been followed."
  875. :group 'org-link-follow
  876. :type 'hook)
  877. (defcustom org-tab-follows-link nil
  878. "Non-nil means, on links TAB will follow the link.
  879. Needs to be set before org.el is loaded."
  880. :group 'org-link-follow
  881. :type 'boolean)
  882. (defcustom org-return-follows-link nil
  883. "Non-nil means, on links RET will follow the link.
  884. Needs to be set before org.el is loaded."
  885. :group 'org-link-follow
  886. :type 'boolean)
  887. (defcustom org-mouse-1-follows-link
  888. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  889. "Non-nil means, mouse-1 on a link will follow the link.
  890. A longer mouse click will still set point. Does not work on XEmacs.
  891. Needs to be set before org.el is loaded."
  892. :group 'org-link-follow
  893. :type 'boolean)
  894. (defcustom org-mark-ring-length 4
  895. "Number of different positions to be recorded in the ring
  896. Changing this requires a restart of Emacs to work correctly."
  897. :group 'org-link-follow
  898. :type 'interger)
  899. (defcustom org-link-frame-setup
  900. '((vm . vm-visit-folder-other-frame)
  901. (gnus . gnus-other-frame)
  902. (file . find-file-other-window))
  903. "Setup the frame configuration for following links.
  904. When following a link with Emacs, it may often be useful to display
  905. this link in another window or frame. This variable can be used to
  906. set this up for the different types of links.
  907. For VM, use any of
  908. `vm-visit-folder'
  909. `vm-visit-folder-other-frame'
  910. For Gnus, use any of
  911. `gnus'
  912. `gnus-other-frame'
  913. `org-gnus-no-new-news'
  914. For FILE, use any of
  915. `find-file'
  916. `find-file-other-window'
  917. `find-file-other-frame'
  918. For the calendar, use the variable `calendar-setup'.
  919. For BBDB, it is currently only possible to display the matches in
  920. another window."
  921. :group 'org-link-follow
  922. :type '(list
  923. (cons (const vm)
  924. (choice
  925. (const vm-visit-folder)
  926. (const vm-visit-folder-other-window)
  927. (const vm-visit-folder-other-frame)))
  928. (cons (const gnus)
  929. (choice
  930. (const gnus)
  931. (const gnus-other-frame)
  932. (const org-gnus-no-new-news)))
  933. (cons (const file)
  934. (choice
  935. (const find-file)
  936. (const find-file-other-window)
  937. (const find-file-other-frame)))))
  938. (defcustom org-display-internal-link-with-indirect-buffer nil
  939. "Non-nil means, use indirect buffer to display infile links.
  940. Activating internal links (from one location in a file to another location
  941. in the same file) normally just jumps to the location. When the link is
  942. activated with a C-u prefix (or with mouse-3), the link is displayed in
  943. another window. When this option is set, the other window actually displays
  944. an indirect buffer clone of the current buffer, to avoid any visibility
  945. changes to the current buffer."
  946. :group 'org-link-follow
  947. :type 'boolean)
  948. (defcustom org-open-non-existing-files nil
  949. "Non-nil means, `org-open-file' will open non-existing files.
  950. When nil, an error will be generated."
  951. :group 'org-link-follow
  952. :type 'boolean)
  953. (defcustom org-open-directory-means-index-dot-org nil
  954. "Non-nil means, a link to a directory really means to index.org.
  955. When nil, following a directory link will run dired or open a finder/explorer
  956. window on that directory."
  957. :group 'org-link-follow
  958. :type 'boolean)
  959. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  960. "Function and arguments to call for following mailto links.
  961. This is a list with the first element being a lisp function, and the
  962. remaining elements being arguments to the function. In string arguments,
  963. %a will be replaced by the address, and %s will be replaced by the subject
  964. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  965. :group 'org-link-follow
  966. :type '(choice
  967. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  968. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  969. (const :tag "message-mail" (message-mail "%a" "%s"))
  970. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  971. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  972. "Non-nil means, ask for confirmation before executing shell links.
  973. Shell links can be dangerous: just think about a link
  974. [[shell:rm -rf ~/*][Google Search]]
  975. This link would show up in your Org-mode document as \"Google Search\",
  976. but really it would remove your entire home directory.
  977. Therefore we advise against setting this variable to nil.
  978. Just change it to `y-or-n-p' of you want to confirm with a
  979. single keystroke rather than having to type \"yes\"."
  980. :group 'org-link-follow
  981. :type '(choice
  982. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  983. (const :tag "with y-or-n (faster)" y-or-n-p)
  984. (const :tag "no confirmation (dangerous)" nil)))
  985. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  986. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  987. Elisp links can be dangerous: just think about a link
  988. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  989. This link would show up in your Org-mode document as \"Google Search\",
  990. but really it would remove your entire home directory.
  991. Therefore we advise against setting this variable to nil.
  992. Just change it to `y-or-n-p' of you want to confirm with a
  993. single keystroke rather than having to type \"yes\"."
  994. :group 'org-link-follow
  995. :type '(choice
  996. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  997. (const :tag "with y-or-n (faster)" y-or-n-p)
  998. (const :tag "no confirmation (dangerous)" nil)))
  999. (defconst org-file-apps-defaults-gnu
  1000. '((remote . emacs)
  1001. (system . mailcap)
  1002. (t . mailcap))
  1003. "Default file applications on a UNIX or GNU/Linux system.
  1004. See `org-file-apps'.")
  1005. (defconst org-file-apps-defaults-macosx
  1006. '((remote . emacs)
  1007. (t . "open %s")
  1008. (system . "open %s")
  1009. ("ps.gz" . "gv %s")
  1010. ("eps.gz" . "gv %s")
  1011. ("dvi" . "xdvi %s")
  1012. ("fig" . "xfig %s"))
  1013. "Default file applications on a MacOS X system.
  1014. The system \"open\" is known as a default, but we use X11 applications
  1015. for some files for which the OS does not have a good default.
  1016. See `org-file-apps'.")
  1017. (defconst org-file-apps-defaults-windowsnt
  1018. (list
  1019. '(remote . emacs)
  1020. (cons t
  1021. (list (if (featurep 'xemacs)
  1022. 'mswindows-shell-execute
  1023. 'w32-shell-execute)
  1024. "open" 'file))
  1025. (cons 'system
  1026. (list (if (featurep 'xemacs)
  1027. 'mswindows-shell-execute
  1028. 'w32-shell-execute)
  1029. "open" 'file)))
  1030. "Default file applications on a Windows NT system.
  1031. The system \"open\" is used for most files.
  1032. See `org-file-apps'.")
  1033. (defcustom org-file-apps
  1034. '(
  1035. (auto-mode . emacs)
  1036. ("\\.x?html?\\'" . default)
  1037. ("\\.pdf\\'" . default)
  1038. )
  1039. "External applications for opening `file:path' items in a document.
  1040. Org-mode uses system defaults for different file types, but
  1041. you can use this variable to set the application for a given file
  1042. extension. The entries in this list are cons cells where the car identifies
  1043. files and the cdr the corresponding command. Possible values for the
  1044. file identifier are
  1045. \"regex\" Regular expression matched against the file name. For backward
  1046. compatibility, this can also be a string with only alphanumeric
  1047. characters, which is then interpreted as an extension.
  1048. `directory' Matches a directory
  1049. `remote' Matches a remote file, accessible through tramp or efs.
  1050. Remote files most likely should be visited through Emacs
  1051. because external applications cannot handle such paths.
  1052. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1053. so all files Emacs knows how to handle. Using this with
  1054. command `emacs' will open most files in Emacs. Beware that this
  1055. will also open html files insite Emacs, unless you add
  1056. (\"html\" . default) to the list as well.
  1057. t Default for files not matched by any of the other options.
  1058. `system' The system command to open files, like `open' on Windows
  1059. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1060. that will be selected if you call `C-c C-o' with a double
  1061. `C-u C-u' prefix.
  1062. Possible values for the command are:
  1063. `emacs' The file will be visited by the current Emacs process.
  1064. `default' Use the default application for this file type, which is the
  1065. association for t in the list, most likely in the system-specific
  1066. part.
  1067. This can be used to overrule an unwanted seting in the
  1068. system-specific variable.
  1069. `system' Use the system command for opening files, like \"open\".
  1070. This command is specified by the entry whose car is `system'.
  1071. Most likely, the system-specific version of this variable
  1072. does define this command, but you can overrule/replace it
  1073. here.
  1074. string A command to be executed by a shell; %s will be replaced
  1075. by the path to the file.
  1076. sexp A Lisp form which will be evaluated. The file path will
  1077. be available in the Lisp variable `file'.
  1078. For more examples, see the system specific constants
  1079. `org-file-apps-defaults-macosx'
  1080. `org-file-apps-defaults-windowsnt'
  1081. `org-file-apps-defaults-gnu'."
  1082. :group 'org-link-follow
  1083. :type '(repeat
  1084. (cons (choice :value ""
  1085. (string :tag "Extension")
  1086. (const :tag "System command to open files" system)
  1087. (const :tag "Default for unrecognized files" t)
  1088. (const :tag "Remote file" remote)
  1089. (const :tag "Links to a directory" directory)
  1090. (const :tag "Any files that have Emacs modes"
  1091. auto-mode))
  1092. (choice :value ""
  1093. (const :tag "Visit with Emacs" emacs)
  1094. (const :tag "Use default" default)
  1095. (const :tag "Use the system command" system)
  1096. (string :tag "Command")
  1097. (sexp :tag "Lisp form")))))
  1098. (defgroup org-refile nil
  1099. "Options concerning refiling entries in Org-mode."
  1100. :tag "Org Refile"
  1101. :group 'org)
  1102. (defcustom org-directory "~/org"
  1103. "Directory with org files.
  1104. This directory will be used as default to prompt for org files.
  1105. Used by the hooks for remember.el."
  1106. :group 'org-refile
  1107. :group 'org-remember
  1108. :type 'directory)
  1109. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1110. "Default target for storing notes.
  1111. Used by the hooks for remember.el. This can be a string, or nil to mean
  1112. the value of `remember-data-file'.
  1113. You can set this on a per-template basis with the variable
  1114. `org-remember-templates'."
  1115. :group 'org-refile
  1116. :group 'org-remember
  1117. :type '(choice
  1118. (const :tag "Default from remember-data-file" nil)
  1119. file))
  1120. (defcustom org-goto-interface 'outline
  1121. "The default interface to be used for `org-goto'.
  1122. Allowed vaues are:
  1123. outline The interface shows an outline of the relevant file
  1124. and the correct heading is found by moving through
  1125. the outline or by searching with incremental search.
  1126. outline-path-completion Headlines in the current buffer are offered via
  1127. completion. This is the interface also used by
  1128. the refile command."
  1129. :group 'org-refile
  1130. :type '(choice
  1131. (const :tag "Outline" outline)
  1132. (const :tag "Outline-path-completion" outline-path-completion)))
  1133. (defcustom org-reverse-note-order nil
  1134. "Non-nil means, store new notes at the beginning of a file or entry.
  1135. When nil, new notes will be filed to the end of a file or entry.
  1136. This can also be a list with cons cells of regular expressions that
  1137. are matched against file names, and values."
  1138. :group 'org-remember
  1139. :group 'org-refile
  1140. :type '(choice
  1141. (const :tag "Reverse always" t)
  1142. (const :tag "Reverse never" nil)
  1143. (repeat :tag "By file name regexp"
  1144. (cons regexp boolean))))
  1145. (defcustom org-refile-targets nil
  1146. "Targets for refiling entries with \\[org-refile].
  1147. This is list of cons cells. Each cell contains:
  1148. - a specification of the files to be considered, either a list of files,
  1149. or a symbol whose function or variable value will be used to retrieve
  1150. a file name or a list of file names. Nil means, refile to a different
  1151. heading in the current buffer.
  1152. - A specification of how to find candidate refile targets. This may be
  1153. any of
  1154. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1155. This tag has to be present in all target headlines, inheritance will
  1156. not be considered.
  1157. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1158. todo keyword.
  1159. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1160. headlines that are refiling targets.
  1161. - a cons cell (:level . N). Any headline of level N is considered a target.
  1162. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1163. When this variable is nil, all top-level headlines in the current buffer
  1164. are used, equivalent to the value `((nil . (:level . 1))'."
  1165. :group 'org-refile
  1166. :type '(repeat
  1167. (cons
  1168. (choice :value org-agenda-files
  1169. (const :tag "All agenda files" org-agenda-files)
  1170. (const :tag "Current buffer" nil)
  1171. (function) (variable) (file))
  1172. (choice :tag "Identify target headline by"
  1173. (cons :tag "Specific tag" (const :value :tag) (string))
  1174. (cons :tag "TODO keyword" (const :value :todo) (string))
  1175. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1176. (cons :tag "Level number" (const :value :level) (integer))
  1177. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1178. (defcustom org-refile-use-outline-path nil
  1179. "Non-nil means, provide refile targets as paths.
  1180. So a level 3 headline will be available as level1/level2/level3.
  1181. When the value is `file', also include the file name (without directory)
  1182. into the path. When `full-file-path', include the full file path."
  1183. :group 'org-refile
  1184. :type '(choice
  1185. (const :tag "Not" nil)
  1186. (const :tag "Yes" t)
  1187. (const :tag "Start with file name" file)
  1188. (const :tag "Start with full file path" full-file-path)))
  1189. (defcustom org-outline-path-complete-in-steps t
  1190. "Non-nil means, complete the outline path in hierarchical steps.
  1191. When Org-mode uses the refile interface to select an outline path
  1192. \(see variable `org-refile-use-outline-path'), the completion of
  1193. the path can be done is a single go, or if can be done in steps down
  1194. the headline hierarchy. Going in steps is probably the best if you
  1195. do not use a special completion package like `ido' or `icicles'.
  1196. However, when using these packages, going in one step can be very
  1197. fast, while still showing the whole path to the entry."
  1198. :group 'org-refile
  1199. :type 'boolean)
  1200. (defgroup org-todo nil
  1201. "Options concerning TODO items in Org-mode."
  1202. :tag "Org TODO"
  1203. :group 'org)
  1204. (defgroup org-progress nil
  1205. "Options concerning Progress logging in Org-mode."
  1206. :tag "Org Progress"
  1207. :group 'org-time)
  1208. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1209. "List of TODO entry keyword sequences and their interpretation.
  1210. \\<org-mode-map>This is a list of sequences.
  1211. Each sequence starts with a symbol, either `sequence' or `type',
  1212. indicating if the keywords should be interpreted as a sequence of
  1213. action steps, or as different types of TODO items. The first
  1214. keywords are states requiring action - these states will select a headline
  1215. for inclusion into the global TODO list Org-mode produces. If one of
  1216. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1217. signify that no further action is necessary. If \"|\" is not found,
  1218. the last keyword is treated as the only DONE state of the sequence.
  1219. The command \\[org-todo] cycles an entry through these states, and one
  1220. additional state where no keyword is present. For details about this
  1221. cycling, see the manual.
  1222. TODO keywords and interpretation can also be set on a per-file basis with
  1223. the special #+SEQ_TODO and #+TYP_TODO lines.
  1224. Each keyword can optionally specify a character for fast state selection
  1225. \(in combination with the variable `org-use-fast-todo-selection')
  1226. and specifiers for state change logging, using the same syntax
  1227. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1228. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1229. indicates to record a time stamp each time this state is selected.
  1230. Each keyword may also specify if a timestamp or a note should be
  1231. recorded when entering or leaving the state, by adding additional
  1232. characters in the parenthesis after the keyword. This looks like this:
  1233. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1234. record only the time of the state change. With X and Y being either
  1235. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1236. Y when leaving the state if and only if the *target* state does not
  1237. define X. You may omit any of the fast-selection key or X or /Y,
  1238. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1239. For backward compatibility, this variable may also be just a list
  1240. of keywords - in this case the interptetation (sequence or type) will be
  1241. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1242. :group 'org-todo
  1243. :group 'org-keywords
  1244. :type '(choice
  1245. (repeat :tag "Old syntax, just keywords"
  1246. (string :tag "Keyword"))
  1247. (repeat :tag "New syntax"
  1248. (cons
  1249. (choice
  1250. :tag "Interpretation"
  1251. (const :tag "Sequence (cycling hits every state)" sequence)
  1252. (const :tag "Type (cycling directly to DONE)" type))
  1253. (repeat
  1254. (string :tag "Keyword"))))))
  1255. (defvar org-todo-keywords-1 nil
  1256. "All TODO and DONE keywords active in a buffer.")
  1257. (make-variable-buffer-local 'org-todo-keywords-1)
  1258. (defvar org-todo-keywords-for-agenda nil)
  1259. (defvar org-done-keywords-for-agenda nil)
  1260. (defvar org-todo-keyword-alist-for-agenda nil)
  1261. (defvar org-tag-alist-for-agenda nil)
  1262. (defvar org-agenda-contributing-files nil)
  1263. (defvar org-not-done-keywords nil)
  1264. (make-variable-buffer-local 'org-not-done-keywords)
  1265. (defvar org-done-keywords nil)
  1266. (make-variable-buffer-local 'org-done-keywords)
  1267. (defvar org-todo-heads nil)
  1268. (make-variable-buffer-local 'org-todo-heads)
  1269. (defvar org-todo-sets nil)
  1270. (make-variable-buffer-local 'org-todo-sets)
  1271. (defvar org-todo-log-states nil)
  1272. (make-variable-buffer-local 'org-todo-log-states)
  1273. (defvar org-todo-kwd-alist nil)
  1274. (make-variable-buffer-local 'org-todo-kwd-alist)
  1275. (defvar org-todo-key-alist nil)
  1276. (make-variable-buffer-local 'org-todo-key-alist)
  1277. (defvar org-todo-key-trigger nil)
  1278. (make-variable-buffer-local 'org-todo-key-trigger)
  1279. (defcustom org-todo-interpretation 'sequence
  1280. "Controls how TODO keywords are interpreted.
  1281. This variable is in principle obsolete and is only used for
  1282. backward compatibility, if the interpretation of todo keywords is
  1283. not given already in `org-todo-keywords'. See that variable for
  1284. more information."
  1285. :group 'org-todo
  1286. :group 'org-keywords
  1287. :type '(choice (const sequence)
  1288. (const type)))
  1289. (defcustom org-use-fast-todo-selection 'prefix
  1290. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1291. This variable describes if and under what circumstances the cycling
  1292. mechanism for TODO keywords will be replaced by a single-key, direct
  1293. selection scheme.
  1294. When nil, fast selection is never used.
  1295. When the symbol `prefix', it will be used when `org-todo' is called with
  1296. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1297. in an agenda buffer.
  1298. When t, fast selection is used by default. In this case, the prefix
  1299. argument forces cycling instead.
  1300. In all cases, the special interface is only used if access keys have actually
  1301. been assigned by the user, i.e. if keywords in the configuration are followed
  1302. by a letter in parenthesis, like TODO(t)."
  1303. :group 'org-todo
  1304. :type '(choice
  1305. (const :tag "Never" nil)
  1306. (const :tag "By default" t)
  1307. (const :tag "Only with C-u C-c C-t" prefix)))
  1308. (defcustom org-provide-todo-statistics t
  1309. "Non-nil means, update todo statistics after insert and toggle.
  1310. When this is set, todo statistics is updated in the parent of the current
  1311. entry each time a todo state is changed."
  1312. :group 'org-todo
  1313. :type 'boolean)
  1314. (defcustom org-after-todo-state-change-hook nil
  1315. "Hook which is run after the state of a TODO item was changed.
  1316. The new state (a string with a TODO keyword, or nil) is available in the
  1317. Lisp variable `state'."
  1318. :group 'org-todo
  1319. :type 'hook)
  1320. (defcustom org-todo-state-tags-triggers nil
  1321. "Tag changes that should be triggered by TODO state changes.
  1322. This is a list. Each entry is
  1323. (state-change (tag . flag) .......)
  1324. State-change can be a string with a state, and empty string to indicate the
  1325. state that has no TODO keyword, or it can be one of the symbols `todo'
  1326. or `done', meaning any not-done or done state, respectively."
  1327. :group 'org-todo
  1328. :group 'org-tags
  1329. :type '(repeat
  1330. (cons (choice :tag "When changing to"
  1331. (const :tag "Not-done state" todo)
  1332. (const :tag "Done state" done)
  1333. (string :tag "State"))
  1334. (repeat
  1335. (cons :tag "Tag action"
  1336. (string :tag "Tag")
  1337. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1338. (defcustom org-log-done nil
  1339. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1340. When equal to the list (done), also prompt for a closing note.
  1341. This can also be configured on a per-file basis by adding one of
  1342. the following lines anywhere in the buffer:
  1343. #+STARTUP: logdone
  1344. #+STARTUP: lognotedone
  1345. #+STARTUP: nologdone"
  1346. :group 'org-todo
  1347. :group 'org-progress
  1348. :type '(choice
  1349. (const :tag "No logging" nil)
  1350. (const :tag "Record CLOSED timestamp" time)
  1351. (const :tag "Record CLOSED timestamp with closing note." note)))
  1352. ;; Normalize old uses of org-log-done.
  1353. (cond
  1354. ((eq org-log-done t) (setq org-log-done 'time))
  1355. ((and (listp org-log-done) (memq 'done org-log-done))
  1356. (setq org-log-done 'note)))
  1357. (defcustom org-log-note-clock-out nil
  1358. "Non-nil means, record a note when clocking out of an item.
  1359. This can also be configured on a per-file basis by adding one of
  1360. the following lines anywhere in the buffer:
  1361. #+STARTUP: lognoteclock-out
  1362. #+STARTUP: nolognoteclock-out"
  1363. :group 'org-todo
  1364. :group 'org-progress
  1365. :type 'boolean)
  1366. (defcustom org-log-done-with-time t
  1367. "Non-nil means, the CLOSED time stamp will contain date and time.
  1368. When nil, only the date will be recorded."
  1369. :group 'org-progress
  1370. :type 'boolean)
  1371. (defcustom org-log-note-headings
  1372. '((done . "CLOSING NOTE %t")
  1373. (state . "State %-12s %t")
  1374. (note . "Note taken on %t")
  1375. (clock-out . ""))
  1376. "Headings for notes added to entries.
  1377. The value is an alist, with the car being a symbol indicating the note
  1378. context, and the cdr is the heading to be used. The heading may also be the
  1379. empty string.
  1380. %t in the heading will be replaced by a time stamp.
  1381. %s will be replaced by the new TODO state, in double quotes.
  1382. %u will be replaced by the user name.
  1383. %U will be replaced by the full user name."
  1384. :group 'org-todo
  1385. :group 'org-progress
  1386. :type '(list :greedy t
  1387. (cons (const :tag "Heading when closing an item" done) string)
  1388. (cons (const :tag
  1389. "Heading when changing todo state (todo sequence only)"
  1390. state) string)
  1391. (cons (const :tag "Heading when just taking a note" note) string)
  1392. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1393. (unless (assq 'note org-log-note-headings)
  1394. (push '(note . "%t") org-log-note-headings))
  1395. (defcustom org-log-state-notes-insert-after-drawers nil
  1396. "Non-nil means, insert state change notes after any drawers in entry.
  1397. Only the drawers that *immediately* follow the headline and the
  1398. deadline/scheduled line are skipped.
  1399. When nil, insert notes right after the heading and perhaps the line
  1400. with deadline/scheduling if present."
  1401. :group 'org-todo
  1402. :group 'org-progress
  1403. :type 'boolean)
  1404. (defcustom org-log-states-order-reversed t
  1405. "Non-nil means, the latest state change note will be directly after heading.
  1406. When nil, the notes will be orderer according to time."
  1407. :group 'org-todo
  1408. :group 'org-progress
  1409. :type 'boolean)
  1410. (defcustom org-log-repeat 'time
  1411. "Non-nil means, record moving through the DONE state when triggering repeat.
  1412. An auto-repeating tasks is immediately switched back to TODO when marked
  1413. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1414. the TODO keyword definition, or recording a closing note by setting
  1415. `org-log-done', there will be no record of the task moving through DONE.
  1416. This variable forces taking a note anyway. Possible values are:
  1417. nil Don't force a record
  1418. time Record a time stamp
  1419. note Record a note
  1420. This option can also be set with on a per-file-basis with
  1421. #+STARTUP: logrepeat
  1422. #+STARTUP: lognoterepeat
  1423. #+STARTUP: nologrepeat
  1424. You can have local logging settings for a subtree by setting the LOGGING
  1425. property to one or more of these keywords."
  1426. :group 'org-todo
  1427. :group 'org-progress
  1428. :type '(choice
  1429. (const :tag "Don't force a record" nil)
  1430. (const :tag "Force recording the DONE state" time)
  1431. (const :tag "Force recording a note with the DONE state" note)))
  1432. (defgroup org-priorities nil
  1433. "Priorities in Org-mode."
  1434. :tag "Org Priorities"
  1435. :group 'org-todo)
  1436. (defcustom org-highest-priority ?A
  1437. "The highest priority of TODO items. A character like ?A, ?B etc.
  1438. Must have a smaller ASCII number than `org-lowest-priority'."
  1439. :group 'org-priorities
  1440. :type 'character)
  1441. (defcustom org-lowest-priority ?C
  1442. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1443. Must have a larger ASCII number than `org-highest-priority'."
  1444. :group 'org-priorities
  1445. :type 'character)
  1446. (defcustom org-default-priority ?B
  1447. "The default priority of TODO items.
  1448. This is the priority an item get if no explicit priority is given."
  1449. :group 'org-priorities
  1450. :type 'character)
  1451. (defcustom org-priority-start-cycle-with-default t
  1452. "Non-nil means, start with default priority when starting to cycle.
  1453. When this is nil, the first step in the cycle will be (depending on the
  1454. command used) one higher or lower that the default priority."
  1455. :group 'org-priorities
  1456. :type 'boolean)
  1457. (defgroup org-time nil
  1458. "Options concerning time stamps and deadlines in Org-mode."
  1459. :tag "Org Time"
  1460. :group 'org)
  1461. (defcustom org-insert-labeled-timestamps-at-point nil
  1462. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1463. When nil, these labeled time stamps are forces into the second line of an
  1464. entry, just after the headline. When scheduling from the global TODO list,
  1465. the time stamp will always be forced into the second line."
  1466. :group 'org-time
  1467. :type 'boolean)
  1468. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1469. "Formats for `format-time-string' which are used for time stamps.
  1470. It is not recommended to change this constant.")
  1471. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1472. "Number of minutes to round time stamps to.
  1473. These are two values, the first applies when first creating a time stamp.
  1474. The second applies when changing it with the commands `S-up' and `S-down'.
  1475. When changing the time stamp, this means that it will change in steps
  1476. of N minutes, as given by the second value.
  1477. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1478. numbers should be factors of 60, so for example 5, 10, 15.
  1479. When this is larger than 1, you can still force an exact time-stamp by using
  1480. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1481. and by using a prefix arg to `S-up/down' to specify the exact number
  1482. of minutes to shift."
  1483. :group 'org-time
  1484. :get '(lambda (var) ; Make sure all entries have 5 elements
  1485. (if (integerp (default-value var))
  1486. (list (default-value var) 5)
  1487. (default-value var)))
  1488. :type '(list
  1489. (integer :tag "when inserting times")
  1490. (integer :tag "when modifying times")))
  1491. ;; Normalize old customizations of this variable.
  1492. (when (integerp org-time-stamp-rounding-minutes)
  1493. (setq org-time-stamp-rounding-minutes
  1494. (list org-time-stamp-rounding-minutes
  1495. org-time-stamp-rounding-minutes)))
  1496. (defcustom org-display-custom-times nil
  1497. "Non-nil means, overlay custom formats over all time stamps.
  1498. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1499. To turn this on on a per-file basis, insert anywhere in the file:
  1500. #+STARTUP: customtime"
  1501. :group 'org-time
  1502. :set 'set-default
  1503. :type 'sexp)
  1504. (make-variable-buffer-local 'org-display-custom-times)
  1505. (defcustom org-time-stamp-custom-formats
  1506. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1507. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1508. These are overlayed over the default ISO format if the variable
  1509. `org-display-custom-times' is set. Time like %H:%M should be at the
  1510. end of the second format."
  1511. :group 'org-time
  1512. :type 'sexp)
  1513. (defun org-time-stamp-format (&optional long inactive)
  1514. "Get the right format for a time string."
  1515. (let ((f (if long (cdr org-time-stamp-formats)
  1516. (car org-time-stamp-formats))))
  1517. (if inactive
  1518. (concat "[" (substring f 1 -1) "]")
  1519. f)))
  1520. (defcustom org-time-clocksum-format "%d:%02d"
  1521. "The format string used when creating CLOCKSUM lines, or when
  1522. org-mode generates a time duration."
  1523. :group 'org-time
  1524. :type 'string)
  1525. (defcustom org-deadline-warning-days 14
  1526. "No. of days before expiration during which a deadline becomes active.
  1527. This variable governs the display in sparse trees and in the agenda.
  1528. When 0 or negative, it means use this number (the absolute value of it)
  1529. even if a deadline has a different individual lead time specified."
  1530. :group 'org-time
  1531. :group 'org-agenda-daily/weekly
  1532. :type 'number)
  1533. (defcustom org-read-date-prefer-future t
  1534. "Non-nil means, assume future for incomplete date input from user.
  1535. This affects the following situations:
  1536. 1. The user gives a day, but no month.
  1537. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1538. read this as the third of *next* month. However, if you enter \"17\",
  1539. it will be considered as *this* month.
  1540. 2. The user gives a month but not a year.
  1541. For example, if it is april and you enter \"feb 2\", this will be read
  1542. as feb 2, *next* year. \"May 5\", however, will be this year.
  1543. Currently this does not work for ISO week specifications.
  1544. When this option is nil, the current month and year will always be used
  1545. as defaults."
  1546. :group 'org-time
  1547. :type 'boolean)
  1548. (defcustom org-read-date-display-live t
  1549. "Non-nil means, display current interpretation of date prompt live.
  1550. This display will be in an overlay, in the minibuffer."
  1551. :group 'org-time
  1552. :type 'boolean)
  1553. (defcustom org-read-date-popup-calendar t
  1554. "Non-nil means, pop up a calendar when prompting for a date.
  1555. In the calendar, the date can be selected with mouse-1. However, the
  1556. minibuffer will also be active, and you can simply enter the date as well.
  1557. When nil, only the minibuffer will be available."
  1558. :group 'org-time
  1559. :type 'boolean)
  1560. (if (fboundp 'defvaralias)
  1561. (defvaralias 'org-popup-calendar-for-date-prompt
  1562. 'org-read-date-popup-calendar))
  1563. (defcustom org-extend-today-until 0
  1564. "The hour when your day really ends. Must be an integer.
  1565. This has influence for the following applications:
  1566. - When switching the agenda to \"today\". It it is still earlier than
  1567. the time given here, the day recognized as TODAY is actually yesterday.
  1568. - When a date is read from the user and it is still before the time given
  1569. here, the current date and time will be assumed to be yesterday, 23:59.
  1570. Also, timestamps inserted in remember templates follow this rule.
  1571. IMPORTANT: This is a feature whose implementation is and likely will
  1572. remain incomplete. Really, it is only here because past midnight seems to
  1573. be the favorite working time of John Wiegley :-)"
  1574. :group 'org-time
  1575. :type 'number)
  1576. (defcustom org-edit-timestamp-down-means-later nil
  1577. "Non-nil means, S-down will increase the time in a time stamp.
  1578. When nil, S-up will increase."
  1579. :group 'org-time
  1580. :type 'boolean)
  1581. (defcustom org-calendar-follow-timestamp-change t
  1582. "Non-nil means, make the calendar window follow timestamp changes.
  1583. When a timestamp is modified and the calendar window is visible, it will be
  1584. moved to the new date."
  1585. :group 'org-time
  1586. :type 'boolean)
  1587. (defgroup org-tags nil
  1588. "Options concerning tags in Org-mode."
  1589. :tag "Org Tags"
  1590. :group 'org)
  1591. (defcustom org-tag-alist nil
  1592. "List of tags allowed in Org-mode files.
  1593. When this list is nil, Org-mode will base TAG input on what is already in the
  1594. buffer.
  1595. The value of this variable is an alist, the car of each entry must be a
  1596. keyword as a string, the cdr may be a character that is used to select
  1597. that tag through the fast-tag-selection interface.
  1598. See the manual for details."
  1599. :group 'org-tags
  1600. :type '(repeat
  1601. (choice
  1602. (cons (string :tag "Tag name")
  1603. (character :tag "Access char"))
  1604. (const :tag "Start radio group" (:startgroup))
  1605. (const :tag "End radio group" (:endgroup)))))
  1606. (defvar org-file-tags nil
  1607. "List of tags that can be inherited by all entries in the file.
  1608. The tags will be inherited if the variable `org-use-tag-inheritance'
  1609. says they should be.
  1610. This variable is populated from #+TAG lines.")
  1611. (defcustom org-use-fast-tag-selection 'auto
  1612. "Non-nil means, use fast tag selection scheme.
  1613. This is a special interface to select and deselect tags with single keys.
  1614. When nil, fast selection is never used.
  1615. When the symbol `auto', fast selection is used if and only if selection
  1616. characters for tags have been configured, either through the variable
  1617. `org-tag-alist' or through a #+TAGS line in the buffer.
  1618. When t, fast selection is always used and selection keys are assigned
  1619. automatically if necessary."
  1620. :group 'org-tags
  1621. :type '(choice
  1622. (const :tag "Always" t)
  1623. (const :tag "Never" nil)
  1624. (const :tag "When selection characters are configured" 'auto)))
  1625. (defcustom org-fast-tag-selection-single-key nil
  1626. "Non-nil means, fast tag selection exits after first change.
  1627. When nil, you have to press RET to exit it.
  1628. During fast tag selection, you can toggle this flag with `C-c'.
  1629. This variable can also have the value `expert'. In this case, the window
  1630. displaying the tags menu is not even shown, until you press C-c again."
  1631. :group 'org-tags
  1632. :type '(choice
  1633. (const :tag "No" nil)
  1634. (const :tag "Yes" t)
  1635. (const :tag "Expert" expert)))
  1636. (defvar org-fast-tag-selection-include-todo nil
  1637. "Non-nil means, fast tags selection interface will also offer TODO states.
  1638. This is an undocumented feature, you should not rely on it.")
  1639. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1640. "The column to which tags should be indented in a headline.
  1641. If this number is positive, it specifies the column. If it is negative,
  1642. it means that the tags should be flushright to that column. For example,
  1643. -80 works well for a normal 80 character screen."
  1644. :group 'org-tags
  1645. :type 'integer)
  1646. (defcustom org-auto-align-tags t
  1647. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1648. These operations change the length of a headline and therefore shift
  1649. the tags around. With this options turned on, after each such operation
  1650. the tags are again aligned to `org-tags-column'."
  1651. :group 'org-tags
  1652. :type 'boolean)
  1653. (defcustom org-use-tag-inheritance t
  1654. "Non-nil means, tags in levels apply also for sublevels.
  1655. When nil, only the tags directly given in a specific line apply there.
  1656. This may also be a list of tags that should be inherited, or a regexp that
  1657. matches tags that should be inherited. Additional control is possible
  1658. with the variable `org-tags-exclude-from-inheritance' which gives an
  1659. explicit list of tags to be excluded from inheritance., even if the value of
  1660. `org-use-tag-inheritance' would select it for inheritance.
  1661. If this option is t, a match early-on in a tree can lead to a large
  1662. number of matches in the subtree when constructing the agenda or creating
  1663. a sparse tree. If you only want to see the first match in a tree during
  1664. a search, check out the variable `org-tags-match-list-sublevels'."
  1665. :group 'org-tags
  1666. :type '(choice
  1667. (const :tag "Not" nil)
  1668. (const :tag "Always" t)
  1669. (repeat :tag "Specific tags" (string :tag "Tag"))
  1670. (regexp :tag "Tags matched by regexp")))
  1671. (defcustom org-tags-exclude-from-inheritance nil
  1672. "List of tags that should never be inherited.
  1673. This is a way to exclude a few tags from inheritance. For way to do
  1674. the opposite, to actively allow inheritance for selected tags,
  1675. see the variable `org-use-tag-inheritance'."
  1676. :group 'org-tags
  1677. :type '(repeat (string :tag "Tag")))
  1678. (defun org-tag-inherit-p (tag)
  1679. "Check if TAG is one that should be inherited."
  1680. (cond
  1681. ((member tag org-tags-exclude-from-inheritance) nil)
  1682. ((eq org-use-tag-inheritance t) t)
  1683. ((not org-use-tag-inheritance) nil)
  1684. ((stringp org-use-tag-inheritance)
  1685. (string-match org-use-tag-inheritance tag))
  1686. ((listp org-use-tag-inheritance)
  1687. (member tag org-use-tag-inheritance))
  1688. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1689. (defcustom org-tags-match-list-sublevels t
  1690. "Non-nil means list also sublevels of headlines matching tag search.
  1691. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1692. the sublevels of a headline matching a tag search often also match
  1693. the same search. Listing all of them can create very long lists.
  1694. Setting this variable to nil causes subtrees of a match to be skipped.
  1695. This option is off by default, because inheritance in on. If you turn
  1696. inheritance off, you very likely want to turn this option on.
  1697. As a special case, if the tag search is restricted to TODO items, the
  1698. value of this variable is ignored and sublevels are always checked, to
  1699. make sure all corresponding TODO items find their way into the list.
  1700. This variable is semi-obsolete and probably should always be true. It
  1701. is better to limit inheritance to certain tags using the variables
  1702. `org-use-tag-inheritanc'e and `org-tags-exclude-from-inheritance'."
  1703. :group 'org-tags
  1704. :type 'boolean)
  1705. (defvar org-tags-history nil
  1706. "History of minibuffer reads for tags.")
  1707. (defvar org-last-tags-completion-table nil
  1708. "The last used completion table for tags.")
  1709. (defvar org-after-tags-change-hook nil
  1710. "Hook that is run after the tags in a line have changed.")
  1711. (defgroup org-properties nil
  1712. "Options concerning properties in Org-mode."
  1713. :tag "Org Properties"
  1714. :group 'org)
  1715. (defcustom org-property-format "%-10s %s"
  1716. "How property key/value pairs should be formatted by `indent-line'.
  1717. When `indent-line' hits a property definition, it will format the line
  1718. according to this format, mainly to make sure that the values are
  1719. lined-up with respect to each other."
  1720. :group 'org-properties
  1721. :type 'string)
  1722. (defcustom org-use-property-inheritance nil
  1723. "Non-nil means, properties apply also for sublevels.
  1724. This setting is chiefly used during property searches. Turning it on can
  1725. cause significant overhead when doing a search, which is why it is not
  1726. on by default.
  1727. When nil, only the properties directly given in the current entry count.
  1728. When t, every property is inherited. The value may also be a list of
  1729. properties that should have inheritance, or a regular expression matching
  1730. properties that should be inherited.
  1731. However, note that some special properties use inheritance under special
  1732. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1733. and the properties ending in \"_ALL\" when they are used as descriptor
  1734. for valid values of a property.
  1735. Note for programmers:
  1736. When querying an entry with `org-entry-get', you can control if inheritance
  1737. should be used. By default, `org-entry-get' looks only at the local
  1738. properties. You can request inheritance by setting the inherit argument
  1739. to t (to force inheritance) or to `selective' (to respect the setting
  1740. in this variable)."
  1741. :group 'org-properties
  1742. :type '(choice
  1743. (const :tag "Not" nil)
  1744. (const :tag "Always" t)
  1745. (repeat :tag "Specific properties" (string :tag "Property"))
  1746. (regexp :tag "Properties matched by regexp")))
  1747. (defun org-property-inherit-p (property)
  1748. "Check if PROPERTY is one that should be inherited."
  1749. (cond
  1750. ((eq org-use-property-inheritance t) t)
  1751. ((not org-use-property-inheritance) nil)
  1752. ((stringp org-use-property-inheritance)
  1753. (string-match org-use-property-inheritance property))
  1754. ((listp org-use-property-inheritance)
  1755. (member property org-use-property-inheritance))
  1756. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1757. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1758. "The default column format, if no other format has been defined.
  1759. This variable can be set on the per-file basis by inserting a line
  1760. #+COLUMNS: %25ITEM ....."
  1761. :group 'org-properties
  1762. :type 'string)
  1763. (defcustom org-columns-ellipses ".."
  1764. "The ellipses to be used when a field in column view is truncated.
  1765. When this is the empty string, as many characters as possible are shown,
  1766. but then there will be no visual indication that the field has been truncated.
  1767. When this is a string of length N, the last N characters of a truncated
  1768. field are replaced by this string. If the column is narrower than the
  1769. ellipses string, only part of the ellipses string will be shown."
  1770. :group 'org-properties
  1771. :type 'string)
  1772. (defcustom org-columns-modify-value-for-display-function nil
  1773. "Function that modifies values for display in column view.
  1774. For example, it can be used to cut out a certain part from a time stamp.
  1775. The function must take 2 arguments:
  1776. column-title The tite of the column (*not* the property name)
  1777. value The value that should be modified.
  1778. The function should return the value that should be displayed,
  1779. or nil if the normal value should be used."
  1780. :group 'org-properties
  1781. :type 'function)
  1782. (defcustom org-effort-property "Effort"
  1783. "The property that is being used to keep track of effort estimates.
  1784. Effort estimates given in this property need to have the format H:MM."
  1785. :group 'org-properties
  1786. :group 'org-progress
  1787. :type '(string :tag "Property"))
  1788. (defconst org-global-properties-fixed
  1789. '(("VISIBILITY_ALL" . "folded children content all"))
  1790. "List of property/value pairs that can be inherited by any entry.
  1791. These are fixed values, for the preset properties.")
  1792. (defcustom org-global-properties nil
  1793. "List of property/value pairs that can be inherited by any entry.
  1794. You can set buffer-local values for the same purpose in the variable
  1795. `org-file-properties' this by adding lines like
  1796. #+PROPERTY: NAME VALUE"
  1797. :group 'org-properties
  1798. :type '(repeat
  1799. (cons (string :tag "Property")
  1800. (string :tag "Value"))))
  1801. (defvar org-file-properties nil
  1802. "List of property/value pairs that can be inherited by any entry.
  1803. Valid for the current buffer.
  1804. This variable is populated from #+PROPERTY lines.")
  1805. (make-variable-buffer-local 'org-file-properties)
  1806. (defgroup org-agenda nil
  1807. "Options concerning agenda views in Org-mode."
  1808. :tag "Org Agenda"
  1809. :group 'org)
  1810. (defvar org-category nil
  1811. "Variable used by org files to set a category for agenda display.
  1812. Such files should use a file variable to set it, for example
  1813. # -*- mode: org; org-category: \"ELisp\"
  1814. or contain a special line
  1815. #+CATEGORY: ELisp
  1816. If the file does not specify a category, then file's base name
  1817. is used instead.")
  1818. (make-variable-buffer-local 'org-category)
  1819. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1820. (defcustom org-agenda-files nil
  1821. "The files to be used for agenda display.
  1822. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1823. \\[org-remove-file]. You can also use customize to edit the list.
  1824. If an entry is a directory, all files in that directory that are matched by
  1825. `org-agenda-file-regexp' will be part of the file list.
  1826. If the value of the variable is not a list but a single file name, then
  1827. the list of agenda files is actually stored and maintained in that file, one
  1828. agenda file per line."
  1829. :group 'org-agenda
  1830. :type '(choice
  1831. (repeat :tag "List of files and directories" file)
  1832. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1833. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1834. "Regular expression to match files for `org-agenda-files'.
  1835. If any element in the list in that variable contains a directory instead
  1836. of a normal file, all files in that directory that are matched by this
  1837. regular expression will be included."
  1838. :group 'org-agenda
  1839. :type 'regexp)
  1840. (defcustom org-agenda-text-search-extra-files nil
  1841. "List of extra files to be searched by text search commands.
  1842. These files will be search in addition to the agenda files by the
  1843. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1844. Note that these files will only be searched for text search commands,
  1845. not for the other agenda views like todo lists, tag searches or the weekly
  1846. agenda. This variable is intended to list notes and possibly archive files
  1847. that should also be searched by these two commands.
  1848. In fact, if the first element in the list is the symbol `agenda-archives',
  1849. than all archive files of all agenda files will be added to the search
  1850. scope."
  1851. :group 'org-agenda
  1852. :type '(set :greedy t
  1853. (const :tag "Agenda Archives" agenda-archives)
  1854. (repeat :inline t (file))))
  1855. (if (fboundp 'defvaralias)
  1856. (defvaralias 'org-agenda-multi-occur-extra-files
  1857. 'org-agenda-text-search-extra-files))
  1858. (defcustom org-agenda-skip-unavailable-files nil
  1859. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1860. A nil value means to remove them, after a query, from the list."
  1861. :group 'org-agenda
  1862. :type 'boolean)
  1863. (defcustom org-calendar-to-agenda-key [?c]
  1864. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1865. The command `org-calendar-goto-agenda' will be bound to this key. The
  1866. default is the character `c' because then `c' can be used to switch back and
  1867. forth between agenda and calendar."
  1868. :group 'org-agenda
  1869. :type 'sexp)
  1870. (defcustom org-calendar-agenda-action-key [?k]
  1871. "The key to be installed in `calendar-mode-map' for agenda-action.
  1872. The command `org-agenda-action' will be bound to this key. The
  1873. default is the character `k' because we use the same key in the agenda."
  1874. :group 'org-agenda
  1875. :type 'sexp)
  1876. (eval-after-load "calendar"
  1877. '(progn
  1878. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1879. 'org-calendar-goto-agenda)
  1880. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1881. 'org-agenda-action)))
  1882. (defgroup org-latex nil
  1883. "Options for embedding LaTeX code into Org-mode."
  1884. :tag "Org LaTeX"
  1885. :group 'org)
  1886. (defcustom org-format-latex-options
  1887. '(:foreground default :background default :scale 1.0
  1888. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1889. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1890. "Options for creating images from LaTeX fragments.
  1891. This is a property list with the following properties:
  1892. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1893. `default' means use the foreground of the default face.
  1894. :background the background color, or \"Transparent\".
  1895. `default' means use the background of the default face.
  1896. :scale a scaling factor for the size of the images.
  1897. :html-foreground, :html-background, :html-scale
  1898. the same numbers for HTML export.
  1899. :matchers a list indicating which matchers should be used to
  1900. find LaTeX fragments. Valid members of this list are:
  1901. \"begin\" find environments
  1902. \"$\" find math expressions surrounded by $...$
  1903. \"$$\" find math expressions surrounded by $$....$$
  1904. \"\\(\" find math expressions surrounded by \\(...\\)
  1905. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1906. :group 'org-latex
  1907. :type 'plist)
  1908. (defcustom org-format-latex-header "\\documentclass{article}
  1909. \\usepackage{fullpage} % do not remove
  1910. \\usepackage{amssymb}
  1911. \\usepackage[usenames]{color}
  1912. \\usepackage{amsmath}
  1913. \\usepackage{latexsym}
  1914. \\usepackage[mathscr]{eucal}
  1915. \\pagestyle{empty} % do not remove"
  1916. "The document header used for processing LaTeX fragments."
  1917. :group 'org-latex
  1918. :type 'string)
  1919. (defgroup org-font-lock nil
  1920. "Font-lock settings for highlighting in Org-mode."
  1921. :tag "Org Font Lock"
  1922. :group 'org)
  1923. (defcustom org-level-color-stars-only nil
  1924. "Non-nil means fontify only the stars in each headline.
  1925. When nil, the entire headline is fontified.
  1926. Changing it requires restart of `font-lock-mode' to become effective
  1927. also in regions already fontified."
  1928. :group 'org-font-lock
  1929. :type 'boolean)
  1930. (defcustom org-hide-leading-stars nil
  1931. "Non-nil means, hide the first N-1 stars in a headline.
  1932. This works by using the face `org-hide' for these stars. This
  1933. face is white for a light background, and black for a dark
  1934. background. You may have to customize the face `org-hide' to
  1935. make this work.
  1936. Changing it requires restart of `font-lock-mode' to become effective
  1937. also in regions already fontified.
  1938. You may also set this on a per-file basis by adding one of the following
  1939. lines to the buffer:
  1940. #+STARTUP: hidestars
  1941. #+STARTUP: showstars"
  1942. :group 'org-font-lock
  1943. :type 'boolean)
  1944. (defcustom org-fontify-done-headline nil
  1945. "Non-nil means, change the face of a headline if it is marked DONE.
  1946. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1947. When this is non-nil, the headline after the keyword is set to the
  1948. `org-headline-done' as an additional indication."
  1949. :group 'org-font-lock
  1950. :type 'boolean)
  1951. (defcustom org-fontify-emphasized-text t
  1952. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1953. Changing this variable requires a restart of Emacs to take effect."
  1954. :group 'org-font-lock
  1955. :type 'boolean)
  1956. (defcustom org-highlight-latex-fragments-and-specials nil
  1957. "Non-nil means, fontify what is treated specially by the exporters."
  1958. :group 'org-font-lock
  1959. :type 'boolean)
  1960. (defcustom org-hide-emphasis-markers nil
  1961. "Non-nil mean font-lock should hide the emphasis marker characters."
  1962. :group 'org-font-lock
  1963. :type 'boolean)
  1964. (defvar org-emph-re nil
  1965. "Regular expression for matching emphasis.")
  1966. (defvar org-verbatim-re nil
  1967. "Regular expression for matching verbatim text.")
  1968. (defvar org-emphasis-regexp-components) ; defined just below
  1969. (defvar org-emphasis-alist) ; defined just below
  1970. (defun org-set-emph-re (var val)
  1971. "Set variable and compute the emphasis regular expression."
  1972. (set var val)
  1973. (when (and (boundp 'org-emphasis-alist)
  1974. (boundp 'org-emphasis-regexp-components)
  1975. org-emphasis-alist org-emphasis-regexp-components)
  1976. (let* ((e org-emphasis-regexp-components)
  1977. (pre (car e))
  1978. (post (nth 1 e))
  1979. (border (nth 2 e))
  1980. (body (nth 3 e))
  1981. (nl (nth 4 e))
  1982. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1983. (body1 (concat body "*?"))
  1984. (markers (mapconcat 'car org-emphasis-alist ""))
  1985. (vmarkers (mapconcat
  1986. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1987. org-emphasis-alist "")))
  1988. ;; make sure special characters appear at the right position in the class
  1989. (if (string-match "\\^" markers)
  1990. (setq markers (concat (replace-match "" t t markers) "^")))
  1991. (if (string-match "-" markers)
  1992. (setq markers (concat (replace-match "" t t markers) "-")))
  1993. (if (string-match "\\^" vmarkers)
  1994. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1995. (if (string-match "-" vmarkers)
  1996. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1997. (if (> nl 0)
  1998. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1999. (int-to-string nl) "\\}")))
  2000. ;; Make the regexp
  2001. (setq org-emph-re
  2002. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  2003. "\\("
  2004. "\\([" markers "]\\)"
  2005. "\\("
  2006. "[^" border "]\\|"
  2007. "[^" border (if (and nil stacked) markers) "]"
  2008. body1
  2009. "[^" border (if (and nil stacked) markers) "]"
  2010. "\\)"
  2011. "\\3\\)"
  2012. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  2013. (setq org-verbatim-re
  2014. (concat "\\([" pre "]\\|^\\)"
  2015. "\\("
  2016. "\\([" vmarkers "]\\)"
  2017. "\\("
  2018. "[^" border "]\\|"
  2019. "[^" border "]"
  2020. body1
  2021. "[^" border "]"
  2022. "\\)"
  2023. "\\3\\)"
  2024. "\\([" post "]\\|$\\)")))))
  2025. (defcustom org-emphasis-regexp-components
  2026. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2027. "Components used to build the regular expression for emphasis.
  2028. This is a list with 6 entries. Terminology: In an emphasis string
  2029. like \" *strong word* \", we call the initial space PREMATCH, the final
  2030. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2031. and \"trong wor\" is the body. The different components in this variable
  2032. specify what is allowed/forbidden in each part:
  2033. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2034. post Chars allowed as postmatch. End of line will be allowed too.
  2035. border The chars *forbidden* as border characters.
  2036. body-regexp A regexp like \".\" to match a body character. Don't use
  2037. non-shy groups here, and don't allow newline here.
  2038. newline The maximum number of newlines allowed in an emphasis exp.
  2039. Use customize to modify this, or restart Emacs after changing it."
  2040. :group 'org-font-lock
  2041. :set 'org-set-emph-re
  2042. :type '(list
  2043. (sexp :tag "Allowed chars in pre ")
  2044. (sexp :tag "Allowed chars in post ")
  2045. (sexp :tag "Forbidden chars in border ")
  2046. (sexp :tag "Regexp for body ")
  2047. (integer :tag "number of newlines allowed")
  2048. (option (boolean :tag "Please ignore this button"))))
  2049. (defcustom org-emphasis-alist
  2050. `(("*" bold "<b>" "</b>")
  2051. ("/" italic "<i>" "</i>")
  2052. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2053. ("=" org-code "<code>" "</code>" verbatim)
  2054. ("~" org-verbatim "<code>" "</code>" verbatim)
  2055. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2056. "<del>" "</del>")
  2057. )
  2058. "Special syntax for emphasized text.
  2059. Text starting and ending with a special character will be emphasized, for
  2060. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2061. characters, the face to be used by font-lock for highlighting in Org-mode
  2062. Emacs buffers, and the HTML tags to be used for this.
  2063. Use customize to modify this, or restart Emacs after changing it."
  2064. :group 'org-font-lock
  2065. :set 'org-set-emph-re
  2066. :type '(repeat
  2067. (list
  2068. (string :tag "Marker character")
  2069. (choice
  2070. (face :tag "Font-lock-face")
  2071. (plist :tag "Face property list"))
  2072. (string :tag "HTML start tag")
  2073. (string :tag "HTML end tag")
  2074. (option (const verbatim)))))
  2075. ;;; Miscellaneous options
  2076. (defgroup org-completion nil
  2077. "Completion in Org-mode."
  2078. :tag "Org Completion"
  2079. :group 'org)
  2080. (defcustom org-completion-use-ido nil
  2081. "Non-nil means, use ido completion wherever possible."
  2082. :group 'org-completion
  2083. :type 'boolean)
  2084. (defcustom org-completion-fallback-command 'hippie-expand
  2085. "The expansion command called by \\[org-complete] in normal context.
  2086. Normal means, no org-mode-specific context."
  2087. :group 'org-completion
  2088. :type 'function)
  2089. ;;; Functions and variables from ther packages
  2090. ;; Declared here to avoid compiler warnings
  2091. ;; XEmacs only
  2092. (defvar outline-mode-menu-heading)
  2093. (defvar outline-mode-menu-show)
  2094. (defvar outline-mode-menu-hide)
  2095. (defvar zmacs-regions) ; XEmacs regions
  2096. ;; Emacs only
  2097. (defvar mark-active)
  2098. ;; Various packages
  2099. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2100. (declare-function calendar-forward-day "cal-move" (arg))
  2101. (declare-function calendar-goto-date "cal-move" (date))
  2102. (declare-function calendar-goto-today "cal-move" ())
  2103. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2104. (defvar calc-embedded-close-formula)
  2105. (defvar calc-embedded-open-formula)
  2106. (declare-function cdlatex-tab "ext:cdlatex" ())
  2107. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2108. (defvar font-lock-unfontify-region-function)
  2109. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2110. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2111. (defvar iswitchb-temp-buflist)
  2112. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2113. (declare-function org-agenda-skip "org-agenda" ())
  2114. (declare-function org-format-agenda-item "org-agenda"
  2115. (extra txt &optional category tags dotime noprefix remove-re))
  2116. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2117. (declare-function org-agenda-change-all-lines "org-agenda"
  2118. (newhead hdmarker &optional fixface just-this))
  2119. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2120. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2121. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2122. (beg end))
  2123. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2124. (declare-function parse-time-string "parse-time" (string))
  2125. (declare-function remember "remember" (&optional initial))
  2126. (declare-function remember-buffer-desc "remember" ())
  2127. (declare-function remember-finalize "remember" ())
  2128. (defvar remember-save-after-remembering)
  2129. (defvar remember-data-file)
  2130. (defvar remember-register)
  2131. (defvar remember-buffer)
  2132. (defvar remember-handler-functions)
  2133. (defvar remember-annotation-functions)
  2134. (defvar texmathp-why)
  2135. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2136. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2137. (defvar w3m-current-url)
  2138. (defvar w3m-current-title)
  2139. (defvar org-latex-regexps)
  2140. ;;; Autoload and prepare some org modules
  2141. ;; Some table stuff that needs to be defined here, because it is used
  2142. ;; by the functions setting up org-mode or checking for table context.
  2143. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2144. "Detects an org-type or table-type table.")
  2145. (defconst org-table-line-regexp "^[ \t]*|"
  2146. "Detects an org-type table line.")
  2147. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2148. "Detects an org-type table line.")
  2149. (defconst org-table-hline-regexp "^[ \t]*|-"
  2150. "Detects an org-type table hline.")
  2151. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2152. "Detects a table-type table hline.")
  2153. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2154. "Searching from within a table (any type) this finds the first line
  2155. outside the table.")
  2156. ;; Autoload the functions in org-table.el that are needed by functions here.
  2157. (eval-and-compile
  2158. (org-autoload "org-table"
  2159. '(org-table-align org-table-begin org-table-blank-field
  2160. org-table-convert org-table-convert-region org-table-copy-down
  2161. org-table-copy-region org-table-create
  2162. org-table-create-or-convert-from-region
  2163. org-table-create-with-table.el org-table-current-dline
  2164. org-table-cut-region org-table-delete-column org-table-edit-field
  2165. org-table-edit-formulas org-table-end org-table-eval-formula
  2166. org-table-export org-table-field-info
  2167. org-table-get-stored-formulas org-table-goto-column
  2168. org-table-hline-and-move org-table-import org-table-insert-column
  2169. org-table-insert-hline org-table-insert-row org-table-iterate
  2170. org-table-justify-field-maybe org-table-kill-row
  2171. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2172. org-table-move-column org-table-move-column-left
  2173. org-table-move-column-right org-table-move-row
  2174. org-table-move-row-down org-table-move-row-up
  2175. org-table-next-field org-table-next-row org-table-paste-rectangle
  2176. org-table-previous-field org-table-recalculate
  2177. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2178. org-table-toggle-coordinate-overlays
  2179. org-table-toggle-formula-debugger org-table-wrap-region
  2180. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2181. (defun org-at-table-p (&optional table-type)
  2182. "Return t if the cursor is inside an org-type table.
  2183. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2184. (if org-enable-table-editor
  2185. (save-excursion
  2186. (beginning-of-line 1)
  2187. (looking-at (if table-type org-table-any-line-regexp
  2188. org-table-line-regexp)))
  2189. nil))
  2190. (defsubst org-table-p () (org-at-table-p))
  2191. (defun org-at-table.el-p ()
  2192. "Return t if and only if we are at a table.el table."
  2193. (and (org-at-table-p 'any)
  2194. (save-excursion
  2195. (goto-char (org-table-begin 'any))
  2196. (looking-at org-table1-hline-regexp))))
  2197. (defun org-table-recognize-table.el ()
  2198. "If there is a table.el table nearby, recognize it and move into it."
  2199. (if org-table-tab-recognizes-table.el
  2200. (if (org-at-table.el-p)
  2201. (progn
  2202. (beginning-of-line 1)
  2203. (if (looking-at org-table-dataline-regexp)
  2204. nil
  2205. (if (looking-at org-table1-hline-regexp)
  2206. (progn
  2207. (beginning-of-line 2)
  2208. (if (looking-at org-table-any-border-regexp)
  2209. (beginning-of-line -1)))))
  2210. (if (re-search-forward "|" (org-table-end t) t)
  2211. (progn
  2212. (require 'table)
  2213. (if (table--at-cell-p (point))
  2214. t
  2215. (message "recognizing table.el table...")
  2216. (table-recognize-table)
  2217. (message "recognizing table.el table...done")))
  2218. (error "This should not happen..."))
  2219. t)
  2220. nil)
  2221. nil))
  2222. (defun org-at-table-hline-p ()
  2223. "Return t if the cursor is inside a hline in a table."
  2224. (if org-enable-table-editor
  2225. (save-excursion
  2226. (beginning-of-line 1)
  2227. (looking-at org-table-hline-regexp))
  2228. nil))
  2229. (defvar org-table-clean-did-remove-column nil)
  2230. (defun org-table-map-tables (function)
  2231. "Apply FUNCTION to the start of all tables in the buffer."
  2232. (save-excursion
  2233. (save-restriction
  2234. (widen)
  2235. (goto-char (point-min))
  2236. (while (re-search-forward org-table-any-line-regexp nil t)
  2237. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2238. (beginning-of-line 1)
  2239. (if (looking-at org-table-line-regexp)
  2240. (save-excursion (funcall function)))
  2241. (re-search-forward org-table-any-border-regexp nil 1))))
  2242. (message "Mapping tables: done"))
  2243. ;; Declare and autoload functions from org-exp.el
  2244. (declare-function org-default-export-plist "org-exp")
  2245. (declare-function org-infile-export-plist "org-exp")
  2246. (declare-function org-get-current-options "org-exp")
  2247. (eval-and-compile
  2248. (org-autoload "org-exp"
  2249. '(org-export org-export-as-ascii org-export-visible
  2250. org-insert-export-options-template org-export-as-html-and-open
  2251. org-export-as-html-batch org-export-as-html-to-buffer
  2252. org-replace-region-by-html org-export-region-as-html
  2253. org-export-as-html org-export-icalendar-this-file
  2254. org-export-icalendar-all-agenda-files
  2255. org-table-clean-before-export
  2256. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2257. ;; Declare and autoload functions from org-agenda.el
  2258. (eval-and-compile
  2259. (org-autoload "org-agenda"
  2260. '(org-agenda org-agenda-list org-search-view
  2261. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2262. org-diary org-agenda-to-appt)))
  2263. ;; Autoload org-remember
  2264. (eval-and-compile
  2265. (org-autoload "org-remember"
  2266. '(org-remember-insinuate org-remember-annotation
  2267. org-remember-apply-template org-remember org-remember-handler)))
  2268. ;; Autoload org-clock.el
  2269. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2270. (beg end))
  2271. (declare-function org-update-mode-line "org-clock" ())
  2272. (defvar org-clock-start-time)
  2273. (defvar org-clock-marker (make-marker)
  2274. "Marker recording the last clock-in.")
  2275. (eval-and-compile
  2276. (org-autoload
  2277. "org-clock"
  2278. '(org-clock-in org-clock-out org-clock-cancel
  2279. org-clock-goto org-clock-sum org-clock-display
  2280. org-remove-clock-overlays org-clock-report
  2281. org-clocktable-shift org-dblock-write:clocktable
  2282. org-get-clocktable)))
  2283. (defun org-clock-update-time-maybe ()
  2284. "If this is a CLOCK line, update it and return t.
  2285. Otherwise, return nil."
  2286. (interactive)
  2287. (save-excursion
  2288. (beginning-of-line 1)
  2289. (skip-chars-forward " \t")
  2290. (when (looking-at org-clock-string)
  2291. (let ((re (concat "[ \t]*" org-clock-string
  2292. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2293. "\\([ \t]*=>.*\\)?\\)?"))
  2294. ts te h m s neg)
  2295. (cond
  2296. ((not (looking-at re))
  2297. nil)
  2298. ((not (match-end 2))
  2299. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2300. (> org-clock-marker (point))
  2301. (<= org-clock-marker (point-at-eol)))
  2302. ;; The clock is running here
  2303. (setq org-clock-start-time
  2304. (apply 'encode-time
  2305. (org-parse-time-string (match-string 1))))
  2306. (org-update-mode-line)))
  2307. (t
  2308. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2309. (end-of-line 1)
  2310. (setq ts (match-string 1)
  2311. te (match-string 3))
  2312. (setq s (- (time-to-seconds
  2313. (apply 'encode-time (org-parse-time-string te)))
  2314. (time-to-seconds
  2315. (apply 'encode-time (org-parse-time-string ts))))
  2316. neg (< s 0)
  2317. s (abs s)
  2318. h (floor (/ s 3600))
  2319. s (- s (* 3600 h))
  2320. m (floor (/ s 60))
  2321. s (- s (* 60 s)))
  2322. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2323. t))))))
  2324. (defun org-check-running-clock ()
  2325. "Check if the current buffer contains the running clock.
  2326. If yes, offer to stop it and to save the buffer with the changes."
  2327. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2328. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2329. (buffer-name))))
  2330. (org-clock-out)
  2331. (when (y-or-n-p "Save changed buffer?")
  2332. (save-buffer))))
  2333. (defun org-clocktable-try-shift (dir n)
  2334. "Check if this line starts a clock table, if yes, shift the time block."
  2335. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2336. (org-clocktable-shift dir n)))
  2337. ;; Autoload org-timer.el
  2338. ;(declare-function org-timer "org-timer")
  2339. (eval-and-compile
  2340. (org-autoload
  2341. "org-timer"
  2342. '(org-timer-start org-timer org-timer-item
  2343. org-timer-change-times-in-region)))
  2344. ;; Autoload archiving code
  2345. ;; The stuff that is needed for cycling and tags has to be defined here.
  2346. (defgroup org-archive nil
  2347. "Options concerning archiving in Org-mode."
  2348. :tag "Org Archive"
  2349. :group 'org-structure)
  2350. (defcustom org-archive-location "%s_archive::"
  2351. "The location where subtrees should be archived.
  2352. The value of this variable is a string, consisting of two parts,
  2353. separated by a double-colon. The first part is a filename and
  2354. the second part is a headline.
  2355. When the filename is omitted, archiving happens in the same file.
  2356. %s in the filename will be replaced by the current file
  2357. name (without the directory part). Archiving to a different file
  2358. is useful to keep archived entries from contributing to the
  2359. Org-mode Agenda.
  2360. The archived entries will be filed as subtrees of the specified
  2361. headline. When the headline is omitted, the subtrees are simply
  2362. filed away at the end of the file, as top-level entries.
  2363. Here are a few examples:
  2364. \"%s_archive::\"
  2365. If the current file is Projects.org, archive in file
  2366. Projects.org_archive, as top-level trees. This is the default.
  2367. \"::* Archived Tasks\"
  2368. Archive in the current file, under the top-level headline
  2369. \"* Archived Tasks\".
  2370. \"~/org/archive.org::\"
  2371. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2372. \"basement::** Finished Tasks\"
  2373. Archive in file ./basement (relative path), as level 3 trees
  2374. below the level 2 heading \"** Finished Tasks\".
  2375. You may set this option on a per-file basis by adding to the buffer a
  2376. line like
  2377. #+ARCHIVE: basement::** Finished Tasks
  2378. You may also define it locally for a subtree by setting an ARCHIVE property
  2379. in the entry. If such a property is found in an entry, or anywhere up
  2380. the hierarchy, it will be used."
  2381. :group 'org-archive
  2382. :type 'string)
  2383. (defcustom org-archive-tag "ARCHIVE"
  2384. "The tag that marks a subtree as archived.
  2385. An archived subtree does not open during visibility cycling, and does
  2386. not contribute to the agenda listings.
  2387. After changing this, font-lock must be restarted in the relevant buffers to
  2388. get the proper fontification."
  2389. :group 'org-archive
  2390. :group 'org-keywords
  2391. :type 'string)
  2392. (defcustom org-agenda-skip-archived-trees t
  2393. "Non-nil means, the agenda will skip any items located in archived trees.
  2394. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2395. variable is no longer recommended, you should leave it at the value t.
  2396. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2397. :group 'org-archive
  2398. :group 'org-agenda-skip
  2399. :type 'boolean)
  2400. (defcustom org-cycle-open-archived-trees nil
  2401. "Non-nil means, `org-cycle' will open archived trees.
  2402. An archived tree is a tree marked with the tag ARCHIVE.
  2403. When nil, archived trees will stay folded. You can still open them with
  2404. normal outline commands like `show-all', but not with the cycling commands."
  2405. :group 'org-archive
  2406. :group 'org-cycle
  2407. :type 'boolean)
  2408. (defcustom org-sparse-tree-open-archived-trees nil
  2409. "Non-nil means sparse tree construction shows matches in archived trees.
  2410. When nil, matches in these trees are highlighted, but the trees are kept in
  2411. collapsed state."
  2412. :group 'org-archive
  2413. :group 'org-sparse-trees
  2414. :type 'boolean)
  2415. (defun org-cycle-hide-archived-subtrees (state)
  2416. "Re-hide all archived subtrees after a visibility state change."
  2417. (when (and (not org-cycle-open-archived-trees)
  2418. (not (memq state '(overview folded))))
  2419. (save-excursion
  2420. (let* ((globalp (memq state '(contents all)))
  2421. (beg (if globalp (point-min) (point)))
  2422. (end (if globalp (point-max) (org-end-of-subtree t))))
  2423. (org-hide-archived-subtrees beg end)
  2424. (goto-char beg)
  2425. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2426. (message "%s" (substitute-command-keys
  2427. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2428. (defun org-force-cycle-archived ()
  2429. "Cycle subtree even if it is archived."
  2430. (interactive)
  2431. (setq this-command 'org-cycle)
  2432. (let ((org-cycle-open-archived-trees t))
  2433. (call-interactively 'org-cycle)))
  2434. (defun org-hide-archived-subtrees (beg end)
  2435. "Re-hide all archived subtrees after a visibility state change."
  2436. (save-excursion
  2437. (let* ((re (concat ":" org-archive-tag ":")))
  2438. (goto-char beg)
  2439. (while (re-search-forward re end t)
  2440. (and (org-on-heading-p) (hide-subtree))
  2441. (org-end-of-subtree t)))))
  2442. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2443. (eval-and-compile
  2444. (org-autoload "org-archive"
  2445. '(org-add-archive-files org-archive-subtree
  2446. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2447. ;; Autoload Column View Code
  2448. (declare-function org-columns-number-to-string "org-colview")
  2449. (declare-function org-columns-get-format-and-top-level "org-colview")
  2450. (declare-function org-columns-compute "org-colview")
  2451. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2452. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2453. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2454. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2455. ;; Autoload ID code
  2456. (org-autoload "org-id"
  2457. '(org-id-get-create org-id-new org-id-copy org-id-get
  2458. org-id-get-with-outline-path-completion
  2459. org-id-get-with-outline-drilling
  2460. org-id-goto org-id-find))
  2461. ;;; Variables for pre-computed regular expressions, all buffer local
  2462. (defvar org-drawer-regexp nil
  2463. "Matches first line of a hidden block.")
  2464. (make-variable-buffer-local 'org-drawer-regexp)
  2465. (defvar org-todo-regexp nil
  2466. "Matches any of the TODO state keywords.")
  2467. (make-variable-buffer-local 'org-todo-regexp)
  2468. (defvar org-not-done-regexp nil
  2469. "Matches any of the TODO state keywords except the last one.")
  2470. (make-variable-buffer-local 'org-not-done-regexp)
  2471. (defvar org-todo-line-regexp nil
  2472. "Matches a headline and puts TODO state into group 2 if present.")
  2473. (make-variable-buffer-local 'org-todo-line-regexp)
  2474. (defvar org-complex-heading-regexp nil
  2475. "Matches a headline and puts everything into groups:
  2476. group 1: the stars
  2477. group 2: The todo keyword, maybe
  2478. group 3: Priority cookie
  2479. group 4: True headline
  2480. group 5: Tags")
  2481. (make-variable-buffer-local 'org-complex-heading-regexp)
  2482. (defvar org-todo-line-tags-regexp nil
  2483. "Matches a headline and puts TODO state into group 2 if present.
  2484. Also put tags into group 4 if tags are present.")
  2485. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2486. (defvar org-nl-done-regexp nil
  2487. "Matches newline followed by a headline with the DONE keyword.")
  2488. (make-variable-buffer-local 'org-nl-done-regexp)
  2489. (defvar org-looking-at-done-regexp nil
  2490. "Matches the DONE keyword a point.")
  2491. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2492. (defvar org-ds-keyword-length 12
  2493. "Maximum length of the Deadline and SCHEDULED keywords.")
  2494. (make-variable-buffer-local 'org-ds-keyword-length)
  2495. (defvar org-deadline-regexp nil
  2496. "Matches the DEADLINE keyword.")
  2497. (make-variable-buffer-local 'org-deadline-regexp)
  2498. (defvar org-deadline-time-regexp nil
  2499. "Matches the DEADLINE keyword together with a time stamp.")
  2500. (make-variable-buffer-local 'org-deadline-time-regexp)
  2501. (defvar org-deadline-line-regexp nil
  2502. "Matches the DEADLINE keyword and the rest of the line.")
  2503. (make-variable-buffer-local 'org-deadline-line-regexp)
  2504. (defvar org-scheduled-regexp nil
  2505. "Matches the SCHEDULED keyword.")
  2506. (make-variable-buffer-local 'org-scheduled-regexp)
  2507. (defvar org-scheduled-time-regexp nil
  2508. "Matches the SCHEDULED keyword together with a time stamp.")
  2509. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2510. (defvar org-closed-time-regexp nil
  2511. "Matches the CLOSED keyword together with a time stamp.")
  2512. (make-variable-buffer-local 'org-closed-time-regexp)
  2513. (defvar org-keyword-time-regexp nil
  2514. "Matches any of the 4 keywords, together with the time stamp.")
  2515. (make-variable-buffer-local 'org-keyword-time-regexp)
  2516. (defvar org-keyword-time-not-clock-regexp nil
  2517. "Matches any of the 3 keywords, together with the time stamp.")
  2518. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2519. (defvar org-maybe-keyword-time-regexp nil
  2520. "Matches a timestamp, possibly preceeded by a keyword.")
  2521. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2522. (defvar org-planning-or-clock-line-re nil
  2523. "Matches a line with planning or clock info.")
  2524. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2525. (defconst org-plain-time-of-day-regexp
  2526. (concat
  2527. "\\(\\<[012]?[0-9]"
  2528. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2529. "\\(--?"
  2530. "\\(\\<[012]?[0-9]"
  2531. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2532. "\\)?")
  2533. "Regular expression to match a plain time or time range.
  2534. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2535. groups carry important information:
  2536. 0 the full match
  2537. 1 the first time, range or not
  2538. 8 the second time, if it is a range.")
  2539. (defconst org-plain-time-extension-regexp
  2540. (concat
  2541. "\\(\\<[012]?[0-9]"
  2542. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2543. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2544. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2545. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2546. groups carry important information:
  2547. 0 the full match
  2548. 7 hours of duration
  2549. 9 minutes of duration")
  2550. (defconst org-stamp-time-of-day-regexp
  2551. (concat
  2552. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2553. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2554. "\\(--?"
  2555. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2556. "Regular expression to match a timestamp time or time range.
  2557. After a match, the following groups carry important information:
  2558. 0 the full match
  2559. 1 date plus weekday, for backreferencing to make sure both times on same day
  2560. 2 the first time, range or not
  2561. 4 the second time, if it is a range.")
  2562. (defconst org-startup-options
  2563. '(("fold" org-startup-folded t)
  2564. ("overview" org-startup-folded t)
  2565. ("nofold" org-startup-folded nil)
  2566. ("showall" org-startup-folded nil)
  2567. ("content" org-startup-folded content)
  2568. ("hidestars" org-hide-leading-stars t)
  2569. ("showstars" org-hide-leading-stars nil)
  2570. ("odd" org-odd-levels-only t)
  2571. ("oddeven" org-odd-levels-only nil)
  2572. ("align" org-startup-align-all-tables t)
  2573. ("noalign" org-startup-align-all-tables nil)
  2574. ("customtime" org-display-custom-times t)
  2575. ("logdone" org-log-done time)
  2576. ("lognotedone" org-log-done note)
  2577. ("nologdone" org-log-done nil)
  2578. ("lognoteclock-out" org-log-note-clock-out t)
  2579. ("nolognoteclock-out" org-log-note-clock-out nil)
  2580. ("logrepeat" org-log-repeat state)
  2581. ("lognoterepeat" org-log-repeat note)
  2582. ("nologrepeat" org-log-repeat nil)
  2583. ("constcgs" constants-unit-system cgs)
  2584. ("constSI" constants-unit-system SI))
  2585. "Variable associated with STARTUP options for org-mode.
  2586. Each element is a list of three items: The startup options as written
  2587. in the #+STARTUP line, the corresponding variable, and the value to
  2588. set this variable to if the option is found. An optional forth element PUSH
  2589. means to push this value onto the list in the variable.")
  2590. (defun org-set-regexps-and-options ()
  2591. "Precompute regular expressions for current buffer."
  2592. (when (org-mode-p)
  2593. (org-set-local 'org-todo-kwd-alist nil)
  2594. (org-set-local 'org-todo-key-alist nil)
  2595. (org-set-local 'org-todo-key-trigger nil)
  2596. (org-set-local 'org-todo-keywords-1 nil)
  2597. (org-set-local 'org-done-keywords nil)
  2598. (org-set-local 'org-todo-heads nil)
  2599. (org-set-local 'org-todo-sets nil)
  2600. (org-set-local 'org-todo-log-states nil)
  2601. (org-set-local 'org-file-properties nil)
  2602. (org-set-local 'org-file-tags nil)
  2603. (let ((re (org-make-options-regexp
  2604. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2605. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2606. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2607. (splitre "[ \t]+")
  2608. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2609. tail sep kws1 prio props ftags drawers
  2610. ext-setup-or-nil setup-contents (start 0))
  2611. (save-excursion
  2612. (save-restriction
  2613. (widen)
  2614. (goto-char (point-min))
  2615. (while (or (and ext-setup-or-nil
  2616. (string-match re ext-setup-or-nil start)
  2617. (setq start (match-end 0)))
  2618. (and (setq ext-setup-or-nil nil start 0)
  2619. (re-search-forward re nil t)))
  2620. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2621. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2622. (cond
  2623. ((equal key "CATEGORY")
  2624. (if (string-match "[ \t]+$" value)
  2625. (setq value (replace-match "" t t value)))
  2626. (setq cat value))
  2627. ((member key '("SEQ_TODO" "TODO"))
  2628. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2629. ((equal key "TYP_TODO")
  2630. (push (cons 'type (org-split-string value splitre)) kwds))
  2631. ((equal key "TAGS")
  2632. (setq tags (append tags (org-split-string value splitre))))
  2633. ((equal key "COLUMNS")
  2634. (org-set-local 'org-columns-default-format value))
  2635. ((equal key "LINK")
  2636. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2637. (push (cons (match-string 1 value)
  2638. (org-trim (match-string 2 value)))
  2639. links)))
  2640. ((equal key "PRIORITIES")
  2641. (setq prio (org-split-string value " +")))
  2642. ((equal key "PROPERTY")
  2643. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2644. (push (cons (match-string 1 value) (match-string 2 value))
  2645. props)))
  2646. ((equal key "FILETAGS")
  2647. (when (string-match "\\S-" value)
  2648. (setq ftags
  2649. (append
  2650. ftags
  2651. (apply 'append
  2652. (mapcar (lambda (x) (org-split-string x ":"))
  2653. (org-split-string value)))))))
  2654. ((equal key "DRAWERS")
  2655. (setq drawers (org-split-string value splitre)))
  2656. ((equal key "CONSTANTS")
  2657. (setq const (append const (org-split-string value splitre))))
  2658. ((equal key "STARTUP")
  2659. (let ((opts (org-split-string value splitre))
  2660. l var val)
  2661. (while (setq l (pop opts))
  2662. (when (setq l (assoc l org-startup-options))
  2663. (setq var (nth 1 l) val (nth 2 l))
  2664. (if (not (nth 3 l))
  2665. (set (make-local-variable var) val)
  2666. (if (not (listp (symbol-value var)))
  2667. (set (make-local-variable var) nil))
  2668. (set (make-local-variable var) (symbol-value var))
  2669. (add-to-list var val))))))
  2670. ((equal key "ARCHIVE")
  2671. (string-match " *$" value)
  2672. (setq arch (replace-match "" t t value))
  2673. (remove-text-properties 0 (length arch)
  2674. '(face t fontified t) arch))
  2675. ((equal key "SETUPFILE")
  2676. (setq setup-contents (org-file-contents
  2677. (expand-file-name
  2678. (org-remove-double-quotes value))
  2679. 'noerror))
  2680. (if (not ext-setup-or-nil)
  2681. (setq ext-setup-or-nil setup-contents start 0)
  2682. (setq ext-setup-or-nil
  2683. (concat (substring ext-setup-or-nil 0 start)
  2684. "\n" setup-contents "\n"
  2685. (substring ext-setup-or-nil start)))))
  2686. ))))
  2687. (when cat
  2688. (org-set-local 'org-category (intern cat))
  2689. (push (cons "CATEGORY" cat) props))
  2690. (when prio
  2691. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2692. (setq prio (mapcar 'string-to-char prio))
  2693. (org-set-local 'org-highest-priority (nth 0 prio))
  2694. (org-set-local 'org-lowest-priority (nth 1 prio))
  2695. (org-set-local 'org-default-priority (nth 2 prio)))
  2696. (and props (org-set-local 'org-file-properties (nreverse props)))
  2697. (and ftags (org-set-local 'org-file-tags ftags))
  2698. (and drawers (org-set-local 'org-drawers drawers))
  2699. (and arch (org-set-local 'org-archive-location arch))
  2700. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2701. ;; Process the TODO keywords
  2702. (unless kwds
  2703. ;; Use the global values as if they had been given locally.
  2704. (setq kwds (default-value 'org-todo-keywords))
  2705. (if (stringp (car kwds))
  2706. (setq kwds (list (cons org-todo-interpretation
  2707. (default-value 'org-todo-keywords)))))
  2708. (setq kwds (reverse kwds)))
  2709. (setq kwds (nreverse kwds))
  2710. (let (inter kws kw)
  2711. (while (setq kws (pop kwds))
  2712. (setq inter (pop kws) sep (member "|" kws)
  2713. kws0 (delete "|" (copy-sequence kws))
  2714. kwsa nil
  2715. kws1 (mapcar
  2716. (lambda (x)
  2717. ;; 1 2
  2718. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2719. (progn
  2720. (setq kw (match-string 1 x)
  2721. key (and (match-end 2) (match-string 2 x))
  2722. log (org-extract-log-state-settings x))
  2723. (push (cons kw (and key (string-to-char key))) kwsa)
  2724. (and log (push log org-todo-log-states))
  2725. kw)
  2726. (error "Invalid TODO keyword %s" x)))
  2727. kws0)
  2728. kwsa (if kwsa (append '((:startgroup))
  2729. (nreverse kwsa)
  2730. '((:endgroup))))
  2731. hw (car kws1)
  2732. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2733. tail (list inter hw (car dws) (org-last dws)))
  2734. (add-to-list 'org-todo-heads hw 'append)
  2735. (push kws1 org-todo-sets)
  2736. (setq org-done-keywords (append org-done-keywords dws nil))
  2737. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2738. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2739. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2740. (setq org-todo-sets (nreverse org-todo-sets)
  2741. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2742. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2743. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2744. ;; Process the constants
  2745. (when const
  2746. (let (e cst)
  2747. (while (setq e (pop const))
  2748. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2749. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2750. (setq org-table-formula-constants-local cst)))
  2751. ;; Process the tags.
  2752. (when tags
  2753. (let (e tgs)
  2754. (while (setq e (pop tags))
  2755. (cond
  2756. ((equal e "{") (push '(:startgroup) tgs))
  2757. ((equal e "}") (push '(:endgroup) tgs))
  2758. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2759. (push (cons (match-string 1 e)
  2760. (string-to-char (match-string 2 e)))
  2761. tgs))
  2762. (t (push (list e) tgs))))
  2763. (org-set-local 'org-tag-alist nil)
  2764. (while (setq e (pop tgs))
  2765. (or (and (stringp (car e))
  2766. (assoc (car e) org-tag-alist))
  2767. (push e org-tag-alist)))))
  2768. ;; Compute the regular expressions and other local variables
  2769. (if (not org-done-keywords)
  2770. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2771. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2772. (length org-scheduled-string)
  2773. (length org-clock-string)
  2774. (length org-closed-string)))
  2775. org-drawer-regexp
  2776. (concat "^[ \t]*:\\("
  2777. (mapconcat 'regexp-quote org-drawers "\\|")
  2778. "\\):[ \t]*$")
  2779. org-not-done-keywords
  2780. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2781. org-todo-regexp
  2782. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2783. "\\|") "\\)\\>")
  2784. org-not-done-regexp
  2785. (concat "\\<\\("
  2786. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2787. "\\)\\>")
  2788. org-todo-line-regexp
  2789. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2790. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2791. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2792. org-complex-heading-regexp
  2793. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2794. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2795. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2796. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2797. org-nl-done-regexp
  2798. (concat "\n\\*+[ \t]+"
  2799. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2800. "\\)" "\\>")
  2801. org-todo-line-tags-regexp
  2802. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2803. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2804. (org-re
  2805. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2806. org-looking-at-done-regexp
  2807. (concat "^" "\\(?:"
  2808. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2809. "\\>")
  2810. org-deadline-regexp (concat "\\<" org-deadline-string)
  2811. org-deadline-time-regexp
  2812. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2813. org-deadline-line-regexp
  2814. (concat "\\<\\(" org-deadline-string "\\).*")
  2815. org-scheduled-regexp
  2816. (concat "\\<" org-scheduled-string)
  2817. org-scheduled-time-regexp
  2818. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2819. org-closed-time-regexp
  2820. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2821. org-keyword-time-regexp
  2822. (concat "\\<\\(" org-scheduled-string
  2823. "\\|" org-deadline-string
  2824. "\\|" org-closed-string
  2825. "\\|" org-clock-string "\\)"
  2826. " *[[<]\\([^]>]+\\)[]>]")
  2827. org-keyword-time-not-clock-regexp
  2828. (concat "\\<\\(" org-scheduled-string
  2829. "\\|" org-deadline-string
  2830. "\\|" org-closed-string
  2831. "\\)"
  2832. " *[[<]\\([^]>]+\\)[]>]")
  2833. org-maybe-keyword-time-regexp
  2834. (concat "\\(\\<\\(" org-scheduled-string
  2835. "\\|" org-deadline-string
  2836. "\\|" org-closed-string
  2837. "\\|" org-clock-string "\\)\\)?"
  2838. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2839. org-planning-or-clock-line-re
  2840. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2841. "\\|" org-deadline-string
  2842. "\\|" org-closed-string "\\|" org-clock-string
  2843. "\\)\\>\\)")
  2844. )
  2845. (org-compute-latex-and-specials-regexp)
  2846. (org-set-font-lock-defaults))))
  2847. (defun org-file-contents (file &optional noerror)
  2848. "Return the contents of FILE, as a string."
  2849. (if (or (not file)
  2850. (not (file-readable-p file)))
  2851. (if noerror
  2852. (progn
  2853. (message "Cannot read file %s" file)
  2854. (ding) (sit-for 2)
  2855. "")
  2856. (error "Cannot read file %s" file))
  2857. (with-temp-buffer
  2858. (insert-file-contents file)
  2859. (buffer-string))))
  2860. (defun org-extract-log-state-settings (x)
  2861. "Extract the log state setting from a TODO keyword string.
  2862. This will extract info from a string like \"WAIT(w@/!)\"."
  2863. (let (kw key log1 log2)
  2864. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2865. (setq kw (match-string 1 x)
  2866. key (and (match-end 2) (match-string 2 x))
  2867. log1 (and (match-end 3) (match-string 3 x))
  2868. log2 (and (match-end 4) (match-string 4 x)))
  2869. (and (or log1 log2)
  2870. (list kw
  2871. (and log1 (if (equal log1 "!") 'time 'note))
  2872. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2873. (defun org-remove-keyword-keys (list)
  2874. "Remove a pair of parenthesis at the end of each string in LIST."
  2875. (mapcar (lambda (x)
  2876. (if (string-match "(.*)$" x)
  2877. (substring x 0 (match-beginning 0))
  2878. x))
  2879. list))
  2880. ;; FIXME: this could be done much better, using second characters etc.
  2881. (defun org-assign-fast-keys (alist)
  2882. "Assign fast keys to a keyword-key alist.
  2883. Respect keys that are already there."
  2884. (let (new e k c c1 c2 (char ?a))
  2885. (while (setq e (pop alist))
  2886. (cond
  2887. ((equal e '(:startgroup)) (push e new))
  2888. ((equal e '(:endgroup)) (push e new))
  2889. (t
  2890. (setq k (car e) c2 nil)
  2891. (if (cdr e)
  2892. (setq c (cdr e))
  2893. ;; automatically assign a character.
  2894. (setq c1 (string-to-char
  2895. (downcase (substring
  2896. k (if (= (string-to-char k) ?@) 1 0)))))
  2897. (if (or (rassoc c1 new) (rassoc c1 alist))
  2898. (while (or (rassoc char new) (rassoc char alist))
  2899. (setq char (1+ char)))
  2900. (setq c2 c1))
  2901. (setq c (or c2 char)))
  2902. (push (cons k c) new))))
  2903. (nreverse new)))
  2904. ;;; Some variables used in various places
  2905. (defvar org-window-configuration nil
  2906. "Used in various places to store a window configuration.")
  2907. (defvar org-finish-function nil
  2908. "Function to be called when `C-c C-c' is used.
  2909. This is for getting out of special buffers like remember.")
  2910. ;; FIXME: Occasionally check by commenting these, to make sure
  2911. ;; no other functions uses these, forgetting to let-bind them.
  2912. (defvar entry)
  2913. (defvar state)
  2914. (defvar last-state)
  2915. (defvar date)
  2916. (defvar description)
  2917. ;; Defined somewhere in this file, but used before definition.
  2918. (defvar org-html-entities)
  2919. (defvar org-struct-menu)
  2920. (defvar org-org-menu)
  2921. (defvar org-tbl-menu)
  2922. (defvar org-agenda-keymap)
  2923. ;;;; Define the Org-mode
  2924. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2925. (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."))
  2926. ;; We use a before-change function to check if a table might need
  2927. ;; an update.
  2928. (defvar org-table-may-need-update t
  2929. "Indicates that a table might need an update.
  2930. This variable is set by `org-before-change-function'.
  2931. `org-table-align' sets it back to nil.")
  2932. (defun org-before-change-function (beg end)
  2933. "Every change indicates that a table might need an update."
  2934. (setq org-table-may-need-update t))
  2935. (defvar org-mode-map)
  2936. (defvar org-mode-hook nil)
  2937. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2938. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2939. (defvar org-table-buffer-is-an nil)
  2940. (defconst org-outline-regexp "\\*+ ")
  2941. ;;;###autoload
  2942. (define-derived-mode org-mode outline-mode "Org"
  2943. "Outline-based notes management and organizer, alias
  2944. \"Carsten's outline-mode for keeping track of everything.\"
  2945. Org-mode develops organizational tasks around a NOTES file which
  2946. contains information about projects as plain text. Org-mode is
  2947. implemented on top of outline-mode, which is ideal to keep the content
  2948. of large files well structured. It supports ToDo items, deadlines and
  2949. time stamps, which magically appear in the diary listing of the Emacs
  2950. calendar. Tables are easily created with a built-in table editor.
  2951. Plain text URL-like links connect to websites, emails (VM), Usenet
  2952. messages (Gnus), BBDB entries, and any files related to the project.
  2953. For printing and sharing of notes, an Org-mode file (or a part of it)
  2954. can be exported as a structured ASCII or HTML file.
  2955. The following commands are available:
  2956. \\{org-mode-map}"
  2957. ;; Get rid of Outline menus, they are not needed
  2958. ;; Need to do this here because define-derived-mode sets up
  2959. ;; the keymap so late. Still, it is a waste to call this each time
  2960. ;; we switch another buffer into org-mode.
  2961. (if (featurep 'xemacs)
  2962. (when (boundp 'outline-mode-menu-heading)
  2963. ;; Assume this is Greg's port, it used easymenu
  2964. (easy-menu-remove outline-mode-menu-heading)
  2965. (easy-menu-remove outline-mode-menu-show)
  2966. (easy-menu-remove outline-mode-menu-hide))
  2967. (define-key org-mode-map [menu-bar headings] 'undefined)
  2968. (define-key org-mode-map [menu-bar hide] 'undefined)
  2969. (define-key org-mode-map [menu-bar show] 'undefined))
  2970. (org-load-modules-maybe)
  2971. (easy-menu-add org-org-menu)
  2972. (easy-menu-add org-tbl-menu)
  2973. (org-install-agenda-files-menu)
  2974. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2975. (org-add-to-invisibility-spec '(org-cwidth))
  2976. (when (featurep 'xemacs)
  2977. (org-set-local 'line-move-ignore-invisible t))
  2978. (org-set-local 'outline-regexp org-outline-regexp)
  2979. (org-set-local 'outline-level 'org-outline-level)
  2980. (when (and org-ellipsis
  2981. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2982. (fboundp 'make-glyph-code))
  2983. (unless org-display-table
  2984. (setq org-display-table (make-display-table)))
  2985. (set-display-table-slot
  2986. org-display-table 4
  2987. (vconcat (mapcar
  2988. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2989. org-ellipsis)))
  2990. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2991. (setq buffer-display-table org-display-table))
  2992. (org-set-regexps-and-options)
  2993. ;; Calc embedded
  2994. (org-set-local 'calc-embedded-open-mode "# ")
  2995. (modify-syntax-entry ?# "<")
  2996. (modify-syntax-entry ?@ "w")
  2997. (if org-startup-truncated (setq truncate-lines t))
  2998. (org-set-local 'font-lock-unfontify-region-function
  2999. 'org-unfontify-region)
  3000. ;; Activate before-change-function
  3001. (org-set-local 'org-table-may-need-update t)
  3002. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3003. 'local)
  3004. ;; Check for running clock before killing a buffer
  3005. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3006. ;; Paragraphs and auto-filling
  3007. (org-set-autofill-regexps)
  3008. (setq indent-line-function 'org-indent-line-function)
  3009. (org-update-radio-target-regexp)
  3010. ;; Comment characters
  3011. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3012. (org-set-local 'comment-padding " ")
  3013. ;; Align options lines
  3014. (org-set-local
  3015. 'align-mode-rules-list
  3016. '((org-in-buffer-settings
  3017. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3018. (modes . '(org-mode)))))
  3019. ;; Imenu
  3020. (org-set-local 'imenu-create-index-function
  3021. 'org-imenu-get-tree)
  3022. ;; Make isearch reveal context
  3023. (if (or (featurep 'xemacs)
  3024. (not (boundp 'outline-isearch-open-invisible-function)))
  3025. ;; Emacs 21 and XEmacs make use of the hook
  3026. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3027. ;; Emacs 22 deals with this through a special variable
  3028. (org-set-local 'outline-isearch-open-invisible-function
  3029. (lambda (&rest ignore) (org-show-context 'isearch))))
  3030. ;; If empty file that did not turn on org-mode automatically, make it to.
  3031. (if (and org-insert-mode-line-in-empty-file
  3032. (interactive-p)
  3033. (= (point-min) (point-max)))
  3034. (insert "# -*- mode: org -*-\n\n"))
  3035. (unless org-inhibit-startup
  3036. (when org-startup-align-all-tables
  3037. (let ((bmp (buffer-modified-p)))
  3038. (org-table-map-tables 'org-table-align)
  3039. (set-buffer-modified-p bmp)))
  3040. (org-set-startup-visibility)))
  3041. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3042. (defun org-current-time ()
  3043. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3044. (if (> (car org-time-stamp-rounding-minutes) 1)
  3045. (let ((r (car org-time-stamp-rounding-minutes))
  3046. (time (decode-time)))
  3047. (apply 'encode-time
  3048. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3049. (nthcdr 2 time))))
  3050. (current-time)))
  3051. ;;;; Font-Lock stuff, including the activators
  3052. (defvar org-mouse-map (make-sparse-keymap))
  3053. (org-defkey org-mouse-map
  3054. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3055. (org-defkey org-mouse-map
  3056. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3057. (when org-mouse-1-follows-link
  3058. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3059. (when org-tab-follows-link
  3060. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3061. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3062. (when org-return-follows-link
  3063. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3064. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3065. (require 'font-lock)
  3066. (defconst org-non-link-chars "]\t\n\r<>")
  3067. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3068. "shell" "elisp"))
  3069. (defvar org-link-types-re nil
  3070. "Matches a link that has a url-like prefix like \"http:\"")
  3071. (defvar org-link-re-with-space nil
  3072. "Matches a link with spaces, optional angular brackets around it.")
  3073. (defvar org-link-re-with-space2 nil
  3074. "Matches a link with spaces, optional angular brackets around it.")
  3075. (defvar org-link-re-with-space3 nil
  3076. "Matches a link with spaces, only for internal part in bracket links.")
  3077. (defvar org-angle-link-re nil
  3078. "Matches link with angular brackets, spaces are allowed.")
  3079. (defvar org-plain-link-re nil
  3080. "Matches plain link, without spaces.")
  3081. (defvar org-bracket-link-regexp nil
  3082. "Matches a link in double brackets.")
  3083. (defvar org-bracket-link-analytic-regexp nil
  3084. "Regular expression used to analyze links.
  3085. Here is what the match groups contain after a match:
  3086. 1: http:
  3087. 2: http
  3088. 3: path
  3089. 4: [desc]
  3090. 5: desc")
  3091. (defvar org-any-link-re nil
  3092. "Regular expression matching any link.")
  3093. (defun org-make-link-regexps ()
  3094. "Update the link regular expressions.
  3095. This should be called after the variable `org-link-types' has changed."
  3096. (setq org-link-types-re
  3097. (concat
  3098. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3099. org-link-re-with-space
  3100. (concat
  3101. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3102. "\\([^" org-non-link-chars " ]"
  3103. "[^" org-non-link-chars "]*"
  3104. "[^" org-non-link-chars " ]\\)>?")
  3105. org-link-re-with-space2
  3106. (concat
  3107. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3108. "\\([^" org-non-link-chars " ]"
  3109. "[^\t\n\r]*"
  3110. "[^" org-non-link-chars " ]\\)>?")
  3111. org-link-re-with-space3
  3112. (concat
  3113. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3114. "\\([^" org-non-link-chars " ]"
  3115. "[^\t\n\r]*\\)")
  3116. org-angle-link-re
  3117. (concat
  3118. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3119. "\\([^" org-non-link-chars " ]"
  3120. "[^" org-non-link-chars "]*"
  3121. "\\)>")
  3122. org-plain-link-re
  3123. (concat
  3124. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3125. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3126. org-bracket-link-regexp
  3127. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3128. org-bracket-link-analytic-regexp
  3129. (concat
  3130. "\\[\\["
  3131. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3132. "\\([^]]+\\)"
  3133. "\\]"
  3134. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3135. "\\]")
  3136. org-any-link-re
  3137. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3138. org-angle-link-re "\\)\\|\\("
  3139. org-plain-link-re "\\)")))
  3140. (org-make-link-regexps)
  3141. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3142. "Regular expression for fast time stamp matching.")
  3143. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3144. "Regular expression for fast time stamp matching.")
  3145. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3146. "Regular expression matching time strings for analysis.
  3147. This one does not require the space after the date, so it can be used
  3148. on a string that terminates immediately after the date.")
  3149. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3150. "Regular expression matching time strings for analysis.")
  3151. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3152. "Regular expression matching time stamps, with groups.")
  3153. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3154. "Regular expression matching time stamps (also [..]), with groups.")
  3155. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3156. "Regular expression matching a time stamp range.")
  3157. (defconst org-tr-regexp-both
  3158. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3159. "Regular expression matching a time stamp range.")
  3160. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3161. org-ts-regexp "\\)?")
  3162. "Regular expression matching a time stamp or time stamp range.")
  3163. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3164. org-ts-regexp-both "\\)?")
  3165. "Regular expression matching a time stamp or time stamp range.
  3166. The time stamps may be either active or inactive.")
  3167. (defvar org-emph-face nil)
  3168. (defun org-do-emphasis-faces (limit)
  3169. "Run through the buffer and add overlays to links."
  3170. (let (rtn)
  3171. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3172. (if (not (= (char-after (match-beginning 3))
  3173. (char-after (match-beginning 4))))
  3174. (progn
  3175. (setq rtn t)
  3176. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3177. 'face
  3178. (nth 1 (assoc (match-string 3)
  3179. org-emphasis-alist)))
  3180. (add-text-properties (match-beginning 2) (match-end 2)
  3181. '(font-lock-multiline t))
  3182. (when org-hide-emphasis-markers
  3183. (add-text-properties (match-end 4) (match-beginning 5)
  3184. '(invisible org-link))
  3185. (add-text-properties (match-beginning 3) (match-end 3)
  3186. '(invisible org-link)))))
  3187. (backward-char 1))
  3188. rtn))
  3189. (defun org-emphasize (&optional char)
  3190. "Insert or change an emphasis, i.e. a font like bold or italic.
  3191. If there is an active region, change that region to a new emphasis.
  3192. If there is no region, just insert the marker characters and position
  3193. the cursor between them.
  3194. CHAR should be either the marker character, or the first character of the
  3195. HTML tag associated with that emphasis. If CHAR is a space, the means
  3196. to remove the emphasis of the selected region.
  3197. If char is not given (for example in an interactive call) it
  3198. will be prompted for."
  3199. (interactive)
  3200. (let ((eal org-emphasis-alist) e det
  3201. (erc org-emphasis-regexp-components)
  3202. (prompt "")
  3203. (string "") beg end move tag c s)
  3204. (if (org-region-active-p)
  3205. (setq beg (region-beginning) end (region-end)
  3206. string (buffer-substring beg end))
  3207. (setq move t))
  3208. (while (setq e (pop eal))
  3209. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3210. c (aref tag 0))
  3211. (push (cons c (string-to-char (car e))) det)
  3212. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3213. (substring tag 1)))))
  3214. (setq det (nreverse det))
  3215. (unless char
  3216. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3217. (setq char (read-char-exclusive)))
  3218. (setq char (or (cdr (assoc char det)) char))
  3219. (if (equal char ?\ )
  3220. (setq s "" move nil)
  3221. (unless (assoc (char-to-string char) org-emphasis-alist)
  3222. (error "No such emphasis marker: \"%c\"" char))
  3223. (setq s (char-to-string char)))
  3224. (while (and (> (length string) 1)
  3225. (equal (substring string 0 1) (substring string -1))
  3226. (assoc (substring string 0 1) org-emphasis-alist))
  3227. (setq string (substring string 1 -1)))
  3228. (setq string (concat s string s))
  3229. (if beg (delete-region beg end))
  3230. (unless (or (bolp)
  3231. (string-match (concat "[" (nth 0 erc) "\n]")
  3232. (char-to-string (char-before (point)))))
  3233. (insert " "))
  3234. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3235. (char-to-string (char-after (point))))
  3236. (insert " ") (backward-char 1))
  3237. (insert string)
  3238. (and move (backward-char 1))))
  3239. (defconst org-nonsticky-props
  3240. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3241. (defun org-activate-plain-links (limit)
  3242. "Run through the buffer and add overlays to links."
  3243. (catch 'exit
  3244. (let (f)
  3245. (while (re-search-forward org-plain-link-re limit t)
  3246. (setq f (get-text-property (match-beginning 0) 'face))
  3247. (if (or (eq f 'org-tag)
  3248. (and (listp f) (memq 'org-tag f)))
  3249. nil
  3250. (add-text-properties (match-beginning 0) (match-end 0)
  3251. (list 'mouse-face 'highlight
  3252. 'rear-nonsticky org-nonsticky-props
  3253. 'keymap org-mouse-map
  3254. ))
  3255. (throw 'exit t))))))
  3256. (defun org-activate-code (limit)
  3257. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3258. (progn
  3259. (remove-text-properties (match-beginning 0) (match-end 0)
  3260. '(display t invisible t intangible t))
  3261. t)))
  3262. (defun org-activate-angle-links (limit)
  3263. "Run through the buffer and add overlays to links."
  3264. (if (re-search-forward org-angle-link-re limit t)
  3265. (progn
  3266. (add-text-properties (match-beginning 0) (match-end 0)
  3267. (list 'mouse-face 'highlight
  3268. 'rear-nonsticky org-nonsticky-props
  3269. 'keymap org-mouse-map
  3270. ))
  3271. t)))
  3272. (defun org-activate-bracket-links (limit)
  3273. "Run through the buffer and add overlays to bracketed links."
  3274. (if (re-search-forward org-bracket-link-regexp limit t)
  3275. (let* ((help (concat "LINK: "
  3276. (org-match-string-no-properties 1)))
  3277. ;; FIXME: above we should remove the escapes.
  3278. ;; but that requires another match, protecting match data,
  3279. ;; a lot of overhead for font-lock.
  3280. (ip (org-maybe-intangible
  3281. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3282. 'keymap org-mouse-map 'mouse-face 'highlight
  3283. 'font-lock-multiline t 'help-echo help)))
  3284. (vp (list 'rear-nonsticky org-nonsticky-props
  3285. 'keymap org-mouse-map 'mouse-face 'highlight
  3286. ' font-lock-multiline t 'help-echo help)))
  3287. ;; We need to remove the invisible property here. Table narrowing
  3288. ;; may have made some of this invisible.
  3289. (remove-text-properties (match-beginning 0) (match-end 0)
  3290. '(invisible nil))
  3291. (if (match-end 3)
  3292. (progn
  3293. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3294. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3295. (add-text-properties (match-end 3) (match-end 0) ip))
  3296. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3297. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3298. (add-text-properties (match-end 1) (match-end 0) ip))
  3299. t)))
  3300. (defun org-activate-dates (limit)
  3301. "Run through the buffer and add overlays to dates."
  3302. (if (re-search-forward org-tsr-regexp-both limit t)
  3303. (progn
  3304. (add-text-properties (match-beginning 0) (match-end 0)
  3305. (list 'mouse-face 'highlight
  3306. 'rear-nonsticky org-nonsticky-props
  3307. 'keymap org-mouse-map))
  3308. (when org-display-custom-times
  3309. (if (match-end 3)
  3310. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3311. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3312. t)))
  3313. (defvar org-target-link-regexp nil
  3314. "Regular expression matching radio targets in plain text.")
  3315. (make-variable-buffer-local 'org-target-link-regexp)
  3316. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3317. "Regular expression matching a link target.")
  3318. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3319. "Regular expression matching a radio target.")
  3320. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3321. "Regular expression matching any target.")
  3322. (defun org-activate-target-links (limit)
  3323. "Run through the buffer and add overlays to target matches."
  3324. (when org-target-link-regexp
  3325. (let ((case-fold-search t))
  3326. (if (re-search-forward org-target-link-regexp limit t)
  3327. (progn
  3328. (add-text-properties (match-beginning 0) (match-end 0)
  3329. (list 'mouse-face 'highlight
  3330. 'rear-nonsticky org-nonsticky-props
  3331. 'keymap org-mouse-map
  3332. 'help-echo "Radio target link"
  3333. 'org-linked-text t))
  3334. t)))))
  3335. (defun org-update-radio-target-regexp ()
  3336. "Find all radio targets in this file and update the regular expression."
  3337. (interactive)
  3338. (when (memq 'radio org-activate-links)
  3339. (setq org-target-link-regexp
  3340. (org-make-target-link-regexp (org-all-targets 'radio)))
  3341. (org-restart-font-lock)))
  3342. (defun org-hide-wide-columns (limit)
  3343. (let (s e)
  3344. (setq s (text-property-any (point) (or limit (point-max))
  3345. 'org-cwidth t))
  3346. (when s
  3347. (setq e (next-single-property-change s 'org-cwidth))
  3348. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3349. (goto-char e)
  3350. t)))
  3351. (defvar org-latex-and-specials-regexp nil
  3352. "Regular expression for highlighting export special stuff.")
  3353. (defvar org-match-substring-regexp)
  3354. (defvar org-match-substring-with-braces-regexp)
  3355. (defvar org-export-html-special-string-regexps)
  3356. (defun org-compute-latex-and-specials-regexp ()
  3357. "Compute regular expression for stuff treated specially by exporters."
  3358. (if (not org-highlight-latex-fragments-and-specials)
  3359. (org-set-local 'org-latex-and-specials-regexp nil)
  3360. (require 'org-exp)
  3361. (let*
  3362. ((matchers (plist-get org-format-latex-options :matchers))
  3363. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3364. org-latex-regexps)))
  3365. (options (org-combine-plists (org-default-export-plist)
  3366. (org-infile-export-plist)))
  3367. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3368. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3369. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3370. (org-export-html-expand (plist-get options :expand-quoted-html))
  3371. (org-export-with-special-strings (plist-get options :special-strings))
  3372. (re-sub
  3373. (cond
  3374. ((equal org-export-with-sub-superscripts '{})
  3375. (list org-match-substring-with-braces-regexp))
  3376. (org-export-with-sub-superscripts
  3377. (list org-match-substring-regexp))
  3378. (t nil)))
  3379. (re-latex
  3380. (if org-export-with-LaTeX-fragments
  3381. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3382. (re-macros
  3383. (if org-export-with-TeX-macros
  3384. (list (concat "\\\\"
  3385. (regexp-opt
  3386. (append (mapcar 'car org-html-entities)
  3387. (if (boundp 'org-latex-entities)
  3388. org-latex-entities nil))
  3389. 'words))) ; FIXME
  3390. ))
  3391. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3392. (re-special (if org-export-with-special-strings
  3393. (mapcar (lambda (x) (car x))
  3394. org-export-html-special-string-regexps)))
  3395. (re-rest
  3396. (delq nil
  3397. (list
  3398. (if org-export-html-expand "@<[^>\n]+>")
  3399. ))))
  3400. (org-set-local
  3401. 'org-latex-and-specials-regexp
  3402. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3403. re-rest) "\\|")))))
  3404. (defun org-do-latex-and-special-faces (limit)
  3405. "Run through the buffer and add overlays to links."
  3406. (when org-latex-and-specials-regexp
  3407. (let (rtn d)
  3408. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3409. limit t))
  3410. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3411. 'face))
  3412. '(org-code org-verbatim underline)))
  3413. (progn
  3414. (setq rtn t
  3415. d (cond ((member (char-after (1+ (match-beginning 0)))
  3416. '(?_ ?^)) 1)
  3417. (t 0)))
  3418. (font-lock-prepend-text-property
  3419. (+ d (match-beginning 0)) (match-end 0)
  3420. 'face 'org-latex-and-export-specials)
  3421. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3422. '(font-lock-multiline t)))))
  3423. rtn)))
  3424. (defun org-restart-font-lock ()
  3425. "Restart font-lock-mode, to force refontification."
  3426. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3427. (font-lock-mode -1)
  3428. (font-lock-mode 1)))
  3429. (defun org-all-targets (&optional radio)
  3430. "Return a list of all targets in this file.
  3431. With optional argument RADIO, only find radio targets."
  3432. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3433. rtn)
  3434. (save-excursion
  3435. (goto-char (point-min))
  3436. (while (re-search-forward re nil t)
  3437. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3438. rtn)))
  3439. (defun org-make-target-link-regexp (targets)
  3440. "Make regular expression matching all strings in TARGETS.
  3441. The regular expression finds the targets also if there is a line break
  3442. between words."
  3443. (and targets
  3444. (concat
  3445. "\\<\\("
  3446. (mapconcat
  3447. (lambda (x)
  3448. (while (string-match " +" x)
  3449. (setq x (replace-match "\\s-+" t t x)))
  3450. x)
  3451. targets
  3452. "\\|")
  3453. "\\)\\>")))
  3454. (defun org-activate-tags (limit)
  3455. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3456. (progn
  3457. (add-text-properties (match-beginning 1) (match-end 1)
  3458. (list 'mouse-face 'highlight
  3459. 'rear-nonsticky org-nonsticky-props
  3460. 'keymap org-mouse-map))
  3461. t)))
  3462. (defun org-outline-level ()
  3463. (save-excursion
  3464. (looking-at outline-regexp)
  3465. (if (match-beginning 1)
  3466. (+ (org-get-string-indentation (match-string 1)) 1000)
  3467. (1- (- (match-end 0) (match-beginning 0))))))
  3468. (defvar org-font-lock-keywords nil)
  3469. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3470. "Regular expression matching a property line.")
  3471. (defvar org-font-lock-hook nil
  3472. "Functions to be called for special font lock stuff.")
  3473. (defun org-font-lock-hook (limit)
  3474. (run-hook-with-args 'org-font-lock-hook limit))
  3475. (defun org-set-font-lock-defaults ()
  3476. (let* ((em org-fontify-emphasized-text)
  3477. (lk org-activate-links)
  3478. (org-font-lock-extra-keywords
  3479. (list
  3480. ;; Call the hook
  3481. '(org-font-lock-hook)
  3482. ;; Headlines
  3483. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3484. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3485. ;; Table lines
  3486. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3487. (1 'org-table t))
  3488. ;; Table internals
  3489. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3490. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3491. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3492. ;; Drawers
  3493. (list org-drawer-regexp '(0 'org-special-keyword t))
  3494. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3495. ;; Properties
  3496. (list org-property-re
  3497. '(1 'org-special-keyword t)
  3498. '(3 'org-property-value t))
  3499. (if org-format-transports-properties-p
  3500. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3501. ;; Links
  3502. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3503. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3504. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3505. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3506. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3507. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3508. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3509. '(org-hide-wide-columns (0 nil append))
  3510. ;; TODO lines
  3511. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3512. '(1 (org-get-todo-face 1) t))
  3513. ;; DONE
  3514. (if org-fontify-done-headline
  3515. (list (concat "^[*]+ +\\<\\("
  3516. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3517. "\\)\\(.*\\)")
  3518. '(2 'org-headline-done t))
  3519. nil)
  3520. ;; Priorities
  3521. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3522. ;; Tags
  3523. '(org-font-lock-add-tag-faces)
  3524. ;; Special keywords
  3525. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3526. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3527. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3528. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3529. ;; Emphasis
  3530. (if em
  3531. (if (featurep 'xemacs)
  3532. '(org-do-emphasis-faces (0 nil append))
  3533. '(org-do-emphasis-faces)))
  3534. ;; Checkboxes
  3535. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3536. 2 'bold prepend)
  3537. (if org-provide-checkbox-statistics
  3538. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3539. (0 (org-get-checkbox-statistics-face) t)))
  3540. ;; Description list items
  3541. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3542. 2 'bold prepend)
  3543. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3544. '(1 'org-archived prepend))
  3545. ;; Specials
  3546. '(org-do-latex-and-special-faces)
  3547. ;; Code
  3548. '(org-activate-code (1 'org-code t))
  3549. ;; COMMENT
  3550. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3551. "\\|" org-quote-string "\\)\\>")
  3552. '(1 'org-special-keyword t))
  3553. '("^#.*" (0 'font-lock-comment-face t))
  3554. )))
  3555. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3556. ;; Now set the full font-lock-keywords
  3557. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3558. (org-set-local 'font-lock-defaults
  3559. '(org-font-lock-keywords t nil nil backward-paragraph))
  3560. (kill-local-variable 'font-lock-keywords) nil))
  3561. (defvar org-m nil)
  3562. (defvar org-l nil)
  3563. (defvar org-f nil)
  3564. (defun org-get-level-face (n)
  3565. "Get the right face for match N in font-lock matching of healdines."
  3566. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3567. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3568. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3569. (cond
  3570. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3571. ((eq n 2) org-f)
  3572. (t (if org-level-color-stars-only nil org-f))))
  3573. (defun org-get-todo-face (kwd)
  3574. "Get the right face for a TODO keyword KWD.
  3575. If KWD is a number, get the corresponding match group."
  3576. (if (numberp kwd) (setq kwd (match-string kwd)))
  3577. (or (cdr (assoc kwd org-todo-keyword-faces))
  3578. (and (member kwd org-done-keywords) 'org-done)
  3579. 'org-todo))
  3580. (defun org-font-lock-add-tag-faces (limit)
  3581. "Add the special tag faces."
  3582. (when (and org-tag-faces org-tags-special-faces-re)
  3583. (while (re-search-forward org-tags-special-faces-re limit t)
  3584. (add-text-properties (match-beginning 1) (match-end 1)
  3585. (list 'face (org-get-tag-face 1)
  3586. 'font-lock-fontified t))
  3587. (backward-char 1))))
  3588. (defun org-get-tag-face (kwd)
  3589. "Get the right face for a TODO keyword KWD.
  3590. If KWD is a number, get the corresponding match group."
  3591. (if (numberp kwd) (setq kwd (match-string kwd)))
  3592. (or (cdr (assoc kwd org-tag-faces))
  3593. 'org-tag))
  3594. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3595. "Remove fontification and activation overlays from links."
  3596. (font-lock-default-unfontify-region beg end)
  3597. (let* ((buffer-undo-list t)
  3598. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3599. (inhibit-modification-hooks t)
  3600. deactivate-mark buffer-file-name buffer-file-truename)
  3601. (remove-text-properties beg end
  3602. '(mouse-face t keymap t org-linked-text t
  3603. invisible t intangible t))))
  3604. ;;;; Visibility cycling, including org-goto and indirect buffer
  3605. ;;; Cycling
  3606. (defvar org-cycle-global-status nil)
  3607. (make-variable-buffer-local 'org-cycle-global-status)
  3608. (defvar org-cycle-subtree-status nil)
  3609. (make-variable-buffer-local 'org-cycle-subtree-status)
  3610. ;;;###autoload
  3611. (defun org-cycle (&optional arg)
  3612. "Visibility cycling for Org-mode.
  3613. - When this function is called with a prefix argument, rotate the entire
  3614. buffer through 3 states (global cycling)
  3615. 1. OVERVIEW: Show only top-level headlines.
  3616. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3617. 3. SHOW ALL: Show everything.
  3618. When called with two C-u C-u prefixes, switch to the startup visibility,
  3619. determined by the variable `org-startup-folded', and by any VISIBILITY
  3620. properties in the buffer.
  3621. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3622. including drawers.
  3623. - When point is at the beginning of a headline, rotate the subtree started
  3624. by this line through 3 different states (local cycling)
  3625. 1. FOLDED: Only the main headline is shown.
  3626. 2. CHILDREN: The main headline and the direct children are shown.
  3627. From this state, you can move to one of the children
  3628. and zoom in further.
  3629. 3. SUBTREE: Show the entire subtree, including body text.
  3630. - When there is a numeric prefix, go up to a heading with level ARG, do
  3631. a `show-subtree' and return to the previous cursor position. If ARG
  3632. is negative, go up that many levels.
  3633. - When point is not at the beginning of a headline, execute the global
  3634. binding for TAB, which is re-indenting the line. See the option
  3635. `org-cycle-emulate-tab' for details.
  3636. - Special case: if point is at the beginning of the buffer and there is
  3637. no headline in line 1, this function will act as if called with prefix arg.
  3638. But only if also the variable `org-cycle-global-at-bob' is t."
  3639. (interactive "P")
  3640. (org-load-modules-maybe)
  3641. (let* ((outline-regexp
  3642. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3643. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3644. outline-regexp))
  3645. (bob-special (and org-cycle-global-at-bob (bobp)
  3646. (not (looking-at outline-regexp))))
  3647. (org-cycle-hook
  3648. (if bob-special
  3649. (delq 'org-optimize-window-after-visibility-change
  3650. (copy-sequence org-cycle-hook))
  3651. org-cycle-hook))
  3652. (pos (point)))
  3653. (if (or bob-special (equal arg '(4)))
  3654. ;; special case: use global cycling
  3655. (setq arg t))
  3656. (cond
  3657. ((equal arg '(16))
  3658. (org-set-startup-visibility)
  3659. (message "Startup visibility, plus VISIBILITY properties"))
  3660. ((equal arg '(64))
  3661. (show-all)
  3662. (message "Entire buffer visible, including drawers"))
  3663. ((org-at-table-p 'any)
  3664. ;; Enter the table or move to the next field in the table
  3665. (or (org-table-recognize-table.el)
  3666. (progn
  3667. (if arg (org-table-edit-field t)
  3668. (org-table-justify-field-maybe)
  3669. (call-interactively 'org-table-next-field)))))
  3670. ((eq arg t) ;; Global cycling
  3671. (cond
  3672. ((and (eq last-command this-command)
  3673. (eq org-cycle-global-status 'overview))
  3674. ;; We just created the overview - now do table of contents
  3675. ;; This can be slow in very large buffers, so indicate action
  3676. (message "CONTENTS...")
  3677. (org-content)
  3678. (message "CONTENTS...done")
  3679. (setq org-cycle-global-status 'contents)
  3680. (run-hook-with-args 'org-cycle-hook 'contents))
  3681. ((and (eq last-command this-command)
  3682. (eq org-cycle-global-status 'contents))
  3683. ;; We just showed the table of contents - now show everything
  3684. (show-all)
  3685. (message "SHOW ALL")
  3686. (setq org-cycle-global-status 'all)
  3687. (run-hook-with-args 'org-cycle-hook 'all))
  3688. (t
  3689. ;; Default action: go to overview
  3690. (org-overview)
  3691. (message "OVERVIEW")
  3692. (setq org-cycle-global-status 'overview)
  3693. (run-hook-with-args 'org-cycle-hook 'overview))))
  3694. ((and org-drawers org-drawer-regexp
  3695. (save-excursion
  3696. (beginning-of-line 1)
  3697. (looking-at org-drawer-regexp)))
  3698. ;; Toggle block visibility
  3699. (org-flag-drawer
  3700. (not (get-char-property (match-end 0) 'invisible))))
  3701. ((integerp arg)
  3702. ;; Show-subtree, ARG levels up from here.
  3703. (save-excursion
  3704. (org-back-to-heading)
  3705. (outline-up-heading (if (< arg 0) (- arg)
  3706. (- (funcall outline-level) arg)))
  3707. (org-show-subtree)))
  3708. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3709. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3710. ;; At a heading: rotate between three different views
  3711. (org-back-to-heading)
  3712. (let ((goal-column 0) eoh eol eos)
  3713. ;; First, some boundaries
  3714. (save-excursion
  3715. (org-back-to-heading)
  3716. (save-excursion
  3717. (beginning-of-line 2)
  3718. (while (and (not (eobp)) ;; this is like `next-line'
  3719. (get-char-property (1- (point)) 'invisible))
  3720. (beginning-of-line 2)) (setq eol (point)))
  3721. (outline-end-of-heading) (setq eoh (point))
  3722. (org-end-of-subtree t)
  3723. (unless (eobp)
  3724. (skip-chars-forward " \t\n")
  3725. (beginning-of-line 1) ; in case this is an item
  3726. )
  3727. (setq eos (1- (point))))
  3728. ;; Find out what to do next and set `this-command'
  3729. (cond
  3730. ((= eos eoh)
  3731. ;; Nothing is hidden behind this heading
  3732. (message "EMPTY ENTRY")
  3733. (setq org-cycle-subtree-status nil)
  3734. (save-excursion
  3735. (goto-char eos)
  3736. (outline-next-heading)
  3737. (if (org-invisible-p) (org-flag-heading nil))))
  3738. ((or (>= eol eos)
  3739. (not (string-match "\\S-" (buffer-substring eol eos))))
  3740. ;; Entire subtree is hidden in one line: open it
  3741. (org-show-entry)
  3742. (show-children)
  3743. (message "CHILDREN")
  3744. (save-excursion
  3745. (goto-char eos)
  3746. (outline-next-heading)
  3747. (if (org-invisible-p) (org-flag-heading nil)))
  3748. (setq org-cycle-subtree-status 'children)
  3749. (run-hook-with-args 'org-cycle-hook 'children))
  3750. ((and (eq last-command this-command)
  3751. (eq org-cycle-subtree-status 'children))
  3752. ;; We just showed the children, now show everything.
  3753. (org-show-subtree)
  3754. (message "SUBTREE")
  3755. (setq org-cycle-subtree-status 'subtree)
  3756. (run-hook-with-args 'org-cycle-hook 'subtree))
  3757. (t
  3758. ;; Default action: hide the subtree.
  3759. (hide-subtree)
  3760. (message "FOLDED")
  3761. (setq org-cycle-subtree-status 'folded)
  3762. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3763. ;; TAB emulation and template completion
  3764. (buffer-read-only (org-back-to-heading))
  3765. ((org-try-structure-completion))
  3766. ((org-try-cdlatex-tab))
  3767. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3768. (or (not (bolp))
  3769. (not (looking-at outline-regexp))))
  3770. (call-interactively (global-key-binding "\t")))
  3771. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3772. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3773. (or (and (eq org-cycle-emulate-tab 'white)
  3774. (= (match-end 0) (point-at-eol)))
  3775. (and (eq org-cycle-emulate-tab 'whitestart)
  3776. (>= (match-end 0) pos))))
  3777. t
  3778. (eq org-cycle-emulate-tab t))
  3779. (call-interactively (global-key-binding "\t")))
  3780. (t (save-excursion
  3781. (org-back-to-heading)
  3782. (org-cycle))))))
  3783. ;;;###autoload
  3784. (defun org-global-cycle (&optional arg)
  3785. "Cycle the global visibility. For details see `org-cycle'.
  3786. With C-u prefix arg, switch to startup visibility.
  3787. With a numeric prefix, show all headlines up to that level."
  3788. (interactive "P")
  3789. (let ((org-cycle-include-plain-lists
  3790. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3791. (cond
  3792. ((integerp arg)
  3793. (show-all)
  3794. (hide-sublevels arg)
  3795. (setq org-cycle-global-status 'contents))
  3796. ((equal arg '(4))
  3797. (org-set-startup-visibility)
  3798. (message "Startup visibility, plus VISIBILITY properties."))
  3799. (t
  3800. (org-cycle '(4))))))
  3801. (defun org-set-startup-visibility ()
  3802. "Set the visibility required by startup options and properties."
  3803. (cond
  3804. ((eq org-startup-folded t)
  3805. (org-cycle '(4)))
  3806. ((eq org-startup-folded 'content)
  3807. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3808. (org-cycle '(4)) (org-cycle '(4)))))
  3809. (org-set-visibility-according-to-property 'no-cleanup)
  3810. (org-cycle-hide-archived-subtrees 'all)
  3811. (org-cycle-hide-drawers 'all)
  3812. (org-cycle-show-empty-lines 'all))
  3813. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3814. "Switch subtree visibilities according to :VISIBILITY: property."
  3815. (interactive)
  3816. (let (state)
  3817. (save-excursion
  3818. (goto-char (point-min))
  3819. (while (re-search-forward
  3820. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3821. nil t)
  3822. (setq state (match-string 1))
  3823. (save-excursion
  3824. (org-back-to-heading t)
  3825. (hide-subtree)
  3826. (org-reveal)
  3827. (cond
  3828. ((equal state '("fold" "folded"))
  3829. (hide-subtree))
  3830. ((equal state "children")
  3831. (org-show-hidden-entry)
  3832. (show-children))
  3833. ((equal state "content")
  3834. (save-excursion
  3835. (save-restriction
  3836. (org-narrow-to-subtree)
  3837. (org-content))))
  3838. ((member state '("all" "showall"))
  3839. (show-subtree)))))
  3840. (unless no-cleanup
  3841. (org-cycle-hide-archived-subtrees 'all)
  3842. (org-cycle-hide-drawers 'all)
  3843. (org-cycle-show-empty-lines 'all)))))
  3844. (defun org-overview ()
  3845. "Switch to overview mode, shoing only top-level headlines.
  3846. Really, this shows all headlines with level equal or greater than the level
  3847. of the first headline in the buffer. This is important, because if the
  3848. first headline is not level one, then (hide-sublevels 1) gives confusing
  3849. results."
  3850. (interactive)
  3851. (let ((level (save-excursion
  3852. (goto-char (point-min))
  3853. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3854. (progn
  3855. (goto-char (match-beginning 0))
  3856. (funcall outline-level))))))
  3857. (and level (hide-sublevels level))))
  3858. (defun org-content (&optional arg)
  3859. "Show all headlines in the buffer, like a table of contents.
  3860. With numerical argument N, show content up to level N."
  3861. (interactive "P")
  3862. (save-excursion
  3863. ;; Visit all headings and show their offspring
  3864. (and (integerp arg) (org-overview))
  3865. (goto-char (point-max))
  3866. (catch 'exit
  3867. (while (and (progn (condition-case nil
  3868. (outline-previous-visible-heading 1)
  3869. (error (goto-char (point-min))))
  3870. t)
  3871. (looking-at outline-regexp))
  3872. (if (integerp arg)
  3873. (show-children (1- arg))
  3874. (show-branches))
  3875. (if (bobp) (throw 'exit nil))))))
  3876. (defun org-optimize-window-after-visibility-change (state)
  3877. "Adjust the window after a change in outline visibility.
  3878. This function is the default value of the hook `org-cycle-hook'."
  3879. (when (get-buffer-window (current-buffer))
  3880. (cond
  3881. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3882. ; ((eq state 'overview) (org-beginning-of-line))
  3883. ((eq state 'content) nil)
  3884. ((eq state 'all) nil)
  3885. ((eq state 'folded) nil)
  3886. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3887. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3888. (defun org-compact-display-after-subtree-move ()
  3889. (let (beg end)
  3890. (save-excursion
  3891. (if (org-up-heading-safe)
  3892. (progn
  3893. (hide-subtree)
  3894. (show-entry)
  3895. (show-children)
  3896. (org-cycle-show-empty-lines 'children)
  3897. (org-cycle-hide-drawers 'children))
  3898. (org-overview)))))
  3899. (defun org-cycle-show-empty-lines (state)
  3900. "Show empty lines above all visible headlines.
  3901. The region to be covered depends on STATE when called through
  3902. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3903. entire buffer covered. Note that an empty line is only shown if there
  3904. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3905. (when (> org-cycle-separator-lines 0)
  3906. (save-excursion
  3907. (let* ((n org-cycle-separator-lines)
  3908. (re (cond
  3909. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3910. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3911. (t (let ((ns (number-to-string (- n 2))))
  3912. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3913. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3914. beg end)
  3915. (cond
  3916. ((memq state '(overview contents t))
  3917. (setq beg (point-min) end (point-max)))
  3918. ((memq state '(children folded))
  3919. (setq beg (point) end (progn (org-end-of-subtree t t)
  3920. (beginning-of-line 2)
  3921. (point)))))
  3922. (when beg
  3923. (goto-char beg)
  3924. (while (re-search-forward re end t)
  3925. (if (not (get-char-property (match-end 1) 'invisible))
  3926. (outline-flag-region
  3927. (match-beginning 1) (match-end 1) nil)))))))
  3928. ;; Never hide empty lines at the end of the file.
  3929. (save-excursion
  3930. (goto-char (point-max))
  3931. (outline-previous-heading)
  3932. (outline-end-of-heading)
  3933. (if (and (looking-at "[ \t\n]+")
  3934. (= (match-end 0) (point-max)))
  3935. (outline-flag-region (point) (match-end 0) nil))))
  3936. (defun org-show-empty-lines-in-parent ()
  3937. "Move to the parent and re-show empty lines before visible headlines."
  3938. (save-excursion
  3939. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3940. (org-cycle-show-empty-lines context))))
  3941. (defun org-cycle-hide-drawers (state)
  3942. "Re-hide all drawers after a visibility state change."
  3943. (when (and (org-mode-p)
  3944. (not (memq state '(overview folded))))
  3945. (save-excursion
  3946. (let* ((globalp (memq state '(contents all)))
  3947. (beg (if globalp (point-min) (point)))
  3948. (end (if globalp (point-max) (org-end-of-subtree t))))
  3949. (goto-char beg)
  3950. (while (re-search-forward org-drawer-regexp end t)
  3951. (org-flag-drawer t))))))
  3952. (defun org-flag-drawer (flag)
  3953. (save-excursion
  3954. (beginning-of-line 1)
  3955. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3956. (let ((b (match-end 0))
  3957. (outline-regexp org-outline-regexp))
  3958. (if (re-search-forward
  3959. "^[ \t]*:END:"
  3960. (save-excursion (outline-next-heading) (point)) t)
  3961. (outline-flag-region b (point-at-eol) flag)
  3962. (error ":END: line missing"))))))
  3963. (defun org-subtree-end-visible-p ()
  3964. "Is the end of the current subtree visible?"
  3965. (pos-visible-in-window-p
  3966. (save-excursion (org-end-of-subtree t) (point))))
  3967. (defun org-first-headline-recenter (&optional N)
  3968. "Move cursor to the first headline and recenter the headline.
  3969. Optional argument N means, put the headline into the Nth line of the window."
  3970. (goto-char (point-min))
  3971. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3972. (beginning-of-line)
  3973. (recenter (prefix-numeric-value N))))
  3974. ;;; Org-goto
  3975. (defvar org-goto-window-configuration nil)
  3976. (defvar org-goto-marker nil)
  3977. (defvar org-goto-map
  3978. (let ((map (make-sparse-keymap)))
  3979. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3980. (while (setq cmd (pop cmds))
  3981. (substitute-key-definition cmd cmd map global-map)))
  3982. (suppress-keymap map)
  3983. (org-defkey map "\C-m" 'org-goto-ret)
  3984. (org-defkey map [(return)] 'org-goto-ret)
  3985. (org-defkey map [(left)] 'org-goto-left)
  3986. (org-defkey map [(right)] 'org-goto-right)
  3987. (org-defkey map [(control ?g)] 'org-goto-quit)
  3988. (org-defkey map "\C-i" 'org-cycle)
  3989. (org-defkey map [(tab)] 'org-cycle)
  3990. (org-defkey map [(down)] 'outline-next-visible-heading)
  3991. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3992. (if org-goto-auto-isearch
  3993. (if (fboundp 'define-key-after)
  3994. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3995. nil)
  3996. (org-defkey map "q" 'org-goto-quit)
  3997. (org-defkey map "n" 'outline-next-visible-heading)
  3998. (org-defkey map "p" 'outline-previous-visible-heading)
  3999. (org-defkey map "f" 'outline-forward-same-level)
  4000. (org-defkey map "b" 'outline-backward-same-level)
  4001. (org-defkey map "u" 'outline-up-heading))
  4002. (org-defkey map "/" 'org-occur)
  4003. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4004. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4005. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4006. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4007. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4008. map))
  4009. (defconst org-goto-help
  4010. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4011. RET=jump to location [Q]uit and return to previous location
  4012. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4013. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4014. ;; FIXME: Docstring doe not mention both interfaces
  4015. (defun org-goto (&optional alternative-interface)
  4016. "Look up a different location in the current file, keeping current visibility.
  4017. When you want look-up or go to a different location in a document, the
  4018. fastest way is often to fold the entire buffer and then dive into the tree.
  4019. This method has the disadvantage, that the previous location will be folded,
  4020. which may not be what you want.
  4021. This command works around this by showing a copy of the current buffer
  4022. in an indirect buffer, in overview mode. You can dive into the tree in
  4023. that copy, use org-occur and incremental search to find a location.
  4024. When pressing RET or `Q', the command returns to the original buffer in
  4025. which the visibility is still unchanged. After RET is will also jump to
  4026. the location selected in the indirect buffer and expose the
  4027. the headline hierarchy above."
  4028. (interactive "P")
  4029. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  4030. (org-refile-use-outline-path t)
  4031. (interface
  4032. (if (not alternative-interface)
  4033. org-goto-interface
  4034. (if (eq org-goto-interface 'outline)
  4035. 'outline-path-completion
  4036. 'outline)))
  4037. (org-goto-start-pos (point))
  4038. (selected-point
  4039. (if (eq interface 'outline)
  4040. (car (org-get-location (current-buffer) org-goto-help))
  4041. (nth 3 (org-refile-get-location "Goto: ")))))
  4042. (if selected-point
  4043. (progn
  4044. (org-mark-ring-push org-goto-start-pos)
  4045. (goto-char selected-point)
  4046. (if (or (org-invisible-p) (org-invisible-p2))
  4047. (org-show-context 'org-goto)))
  4048. (message "Quit"))))
  4049. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4050. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4051. (defvar org-goto-local-auto-isearch-map) ; defined below
  4052. (defun org-get-location (buf help)
  4053. "Let the user select a location in the Org-mode buffer BUF.
  4054. This function uses a recursive edit. It returns the selected position
  4055. or nil."
  4056. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4057. (isearch-hide-immediately nil)
  4058. (isearch-search-fun-function
  4059. (lambda () 'org-goto-local-search-headings))
  4060. (org-goto-selected-point org-goto-exit-command))
  4061. (save-excursion
  4062. (save-window-excursion
  4063. (delete-other-windows)
  4064. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4065. (switch-to-buffer
  4066. (condition-case nil
  4067. (make-indirect-buffer (current-buffer) "*org-goto*")
  4068. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4069. (with-output-to-temp-buffer "*Help*"
  4070. (princ help))
  4071. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4072. (setq buffer-read-only nil)
  4073. (let ((org-startup-truncated t)
  4074. (org-startup-folded nil)
  4075. (org-startup-align-all-tables nil))
  4076. (org-mode)
  4077. (org-overview))
  4078. (setq buffer-read-only t)
  4079. (if (and (boundp 'org-goto-start-pos)
  4080. (integer-or-marker-p org-goto-start-pos))
  4081. (let ((org-show-hierarchy-above t)
  4082. (org-show-siblings t)
  4083. (org-show-following-heading t))
  4084. (goto-char org-goto-start-pos)
  4085. (and (org-invisible-p) (org-show-context)))
  4086. (goto-char (point-min)))
  4087. (org-beginning-of-line)
  4088. (message "Select location and press RET")
  4089. (use-local-map org-goto-map)
  4090. (recursive-edit)
  4091. ))
  4092. (kill-buffer "*org-goto*")
  4093. (cons org-goto-selected-point org-goto-exit-command)))
  4094. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4095. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4096. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4097. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4098. (defun org-goto-local-search-headings (string bound noerror)
  4099. "Search and make sure that any matches are in headlines."
  4100. (catch 'return
  4101. (while (if isearch-forward
  4102. (search-forward string bound noerror)
  4103. (search-backward string bound noerror))
  4104. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4105. (and (member :headline context)
  4106. (not (member :tags context))))
  4107. (throw 'return (point))))))
  4108. (defun org-goto-local-auto-isearch ()
  4109. "Start isearch."
  4110. (interactive)
  4111. (goto-char (point-min))
  4112. (let ((keys (this-command-keys)))
  4113. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4114. (isearch-mode t)
  4115. (isearch-process-search-char (string-to-char keys)))))
  4116. (defun org-goto-ret (&optional arg)
  4117. "Finish `org-goto' by going to the new location."
  4118. (interactive "P")
  4119. (setq org-goto-selected-point (point)
  4120. org-goto-exit-command 'return)
  4121. (throw 'exit nil))
  4122. (defun org-goto-left ()
  4123. "Finish `org-goto' by going to the new location."
  4124. (interactive)
  4125. (if (org-on-heading-p)
  4126. (progn
  4127. (beginning-of-line 1)
  4128. (setq org-goto-selected-point (point)
  4129. org-goto-exit-command 'left)
  4130. (throw 'exit nil))
  4131. (error "Not on a heading")))
  4132. (defun org-goto-right ()
  4133. "Finish `org-goto' by going to the new location."
  4134. (interactive)
  4135. (if (org-on-heading-p)
  4136. (progn
  4137. (setq org-goto-selected-point (point)
  4138. org-goto-exit-command 'right)
  4139. (throw 'exit nil))
  4140. (error "Not on a heading")))
  4141. (defun org-goto-quit ()
  4142. "Finish `org-goto' without cursor motion."
  4143. (interactive)
  4144. (setq org-goto-selected-point nil)
  4145. (setq org-goto-exit-command 'quit)
  4146. (throw 'exit nil))
  4147. ;;; Indirect buffer display of subtrees
  4148. (defvar org-indirect-dedicated-frame nil
  4149. "This is the frame being used for indirect tree display.")
  4150. (defvar org-last-indirect-buffer nil)
  4151. (defun org-tree-to-indirect-buffer (&optional arg)
  4152. "Create indirect buffer and narrow it to current subtree.
  4153. With numerical prefix ARG, go up to this level and then take that tree.
  4154. If ARG is negative, go up that many levels.
  4155. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4156. indirect buffer previously made with this command, to avoid proliferation of
  4157. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4158. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4159. is kept so that you can work with several indirect buffers at the same time.
  4160. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4161. requests that a new frame be made for the new buffer, so that the dedicated
  4162. frame is not changed."
  4163. (interactive "P")
  4164. (let ((cbuf (current-buffer))
  4165. (cwin (selected-window))
  4166. (pos (point))
  4167. beg end level heading ibuf)
  4168. (save-excursion
  4169. (org-back-to-heading t)
  4170. (when (numberp arg)
  4171. (setq level (org-outline-level))
  4172. (if (< arg 0) (setq arg (+ level arg)))
  4173. (while (> (setq level (org-outline-level)) arg)
  4174. (outline-up-heading 1 t)))
  4175. (setq beg (point)
  4176. heading (org-get-heading))
  4177. (org-end-of-subtree t) (setq end (point)))
  4178. (if (and (buffer-live-p org-last-indirect-buffer)
  4179. (not (eq org-indirect-buffer-display 'new-frame))
  4180. (not arg))
  4181. (kill-buffer org-last-indirect-buffer))
  4182. (setq ibuf (org-get-indirect-buffer cbuf)
  4183. org-last-indirect-buffer ibuf)
  4184. (cond
  4185. ((or (eq org-indirect-buffer-display 'new-frame)
  4186. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4187. (select-frame (make-frame))
  4188. (delete-other-windows)
  4189. (switch-to-buffer ibuf)
  4190. (org-set-frame-title heading))
  4191. ((eq org-indirect-buffer-display 'dedicated-frame)
  4192. (raise-frame
  4193. (select-frame (or (and org-indirect-dedicated-frame
  4194. (frame-live-p org-indirect-dedicated-frame)
  4195. org-indirect-dedicated-frame)
  4196. (setq org-indirect-dedicated-frame (make-frame)))))
  4197. (delete-other-windows)
  4198. (switch-to-buffer ibuf)
  4199. (org-set-frame-title (concat "Indirect: " heading)))
  4200. ((eq org-indirect-buffer-display 'current-window)
  4201. (switch-to-buffer ibuf))
  4202. ((eq org-indirect-buffer-display 'other-window)
  4203. (pop-to-buffer ibuf))
  4204. (t (error "Invalid value.")))
  4205. (if (featurep 'xemacs)
  4206. (save-excursion (org-mode) (turn-on-font-lock)))
  4207. (narrow-to-region beg end)
  4208. (show-all)
  4209. (goto-char pos)
  4210. (and (window-live-p cwin) (select-window cwin))))
  4211. (defun org-get-indirect-buffer (&optional buffer)
  4212. (setq buffer (or buffer (current-buffer)))
  4213. (let ((n 1) (base (buffer-name buffer)) bname)
  4214. (while (buffer-live-p
  4215. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4216. (setq n (1+ n)))
  4217. (condition-case nil
  4218. (make-indirect-buffer buffer bname 'clone)
  4219. (error (make-indirect-buffer buffer bname)))))
  4220. (defun org-set-frame-title (title)
  4221. "Set the title of the current frame to the string TITLE."
  4222. ;; FIXME: how to name a single frame in XEmacs???
  4223. (unless (featurep 'xemacs)
  4224. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4225. ;;;; Structure editing
  4226. ;;; Inserting headlines
  4227. (defun org-insert-heading (&optional force-heading)
  4228. "Insert a new heading or item with same depth at point.
  4229. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4230. If point is at the beginning of a headline, insert a sibling before the
  4231. current headline. If point is not at the beginning, do not split the line,
  4232. but create the new headline after the current line."
  4233. (interactive "P")
  4234. (if (= (buffer-size) 0)
  4235. (insert "\n* ")
  4236. (when (or force-heading (not (org-insert-item)))
  4237. (let* ((head (save-excursion
  4238. (condition-case nil
  4239. (progn
  4240. (org-back-to-heading)
  4241. (match-string 0))
  4242. (error "*"))))
  4243. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4244. pos hide-previous previous-pos)
  4245. (cond
  4246. ((and (org-on-heading-p) (bolp)
  4247. (or (bobp)
  4248. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4249. ;; insert before the current line
  4250. (open-line (if blank 2 1)))
  4251. ((and (bolp)
  4252. (or (bobp)
  4253. (save-excursion
  4254. (backward-char 1) (not (org-invisible-p)))))
  4255. ;; insert right here
  4256. nil)
  4257. (t
  4258. ;; somewhere in the line
  4259. (save-excursion
  4260. (setq previous-pos (point-at-bol))
  4261. (end-of-line)
  4262. (setq hide-previous (org-invisible-p)))
  4263. (and org-insert-heading-respect-content (org-show-subtree))
  4264. (let ((split
  4265. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4266. (save-excursion
  4267. (let ((p (point)))
  4268. (goto-char (point-at-bol))
  4269. (and (looking-at org-complex-heading-regexp)
  4270. (> p (match-beginning 4)))))))
  4271. tags pos)
  4272. (cond
  4273. (org-insert-heading-respect-content
  4274. (org-end-of-subtree nil t)
  4275. (or (bolp) (newline))
  4276. (open-line 1))
  4277. ((org-on-heading-p)
  4278. (when hide-previous
  4279. (show-children)
  4280. (org-show-entry))
  4281. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4282. (setq tags (and (match-end 2) (match-string 2)))
  4283. (and (match-end 1)
  4284. (delete-region (match-beginning 1) (match-end 1)))
  4285. (setq pos (point-at-bol))
  4286. (or split (end-of-line 1))
  4287. (delete-horizontal-space)
  4288. (newline (if blank 2 1))
  4289. (when tags
  4290. (save-excursion
  4291. (goto-char pos)
  4292. (end-of-line 1)
  4293. (insert " " tags)
  4294. (org-set-tags nil 'align))))
  4295. (t
  4296. (or split (end-of-line 1))
  4297. (newline (if blank 2 1)))))))
  4298. (insert head) (just-one-space)
  4299. (setq pos (point))
  4300. (end-of-line 1)
  4301. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4302. (when (and org-insert-heading-respect-content hide-previous)
  4303. (save-excursion
  4304. (goto-char previous-pos)
  4305. (hide-subtree)))
  4306. (run-hooks 'org-insert-heading-hook)))))
  4307. (defun org-get-heading (&optional no-tags)
  4308. "Return the heading of the current entry, without the stars."
  4309. (save-excursion
  4310. (org-back-to-heading t)
  4311. (if (looking-at
  4312. (if no-tags
  4313. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4314. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4315. (match-string 1) "")))
  4316. (defun org-insert-heading-after-current ()
  4317. "Insert a new heading with same level as current, after current subtree."
  4318. (interactive)
  4319. (org-back-to-heading)
  4320. (org-insert-heading)
  4321. (org-move-subtree-down)
  4322. (end-of-line 1))
  4323. (defun org-insert-heading-respect-content ()
  4324. (interactive)
  4325. (let ((org-insert-heading-respect-content t))
  4326. (org-insert-heading t)))
  4327. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4328. (interactive "P")
  4329. (let ((org-insert-heading-respect-content t))
  4330. (org-insert-todo-heading force-state t)))
  4331. (defun org-insert-todo-heading (arg &optional force-heading)
  4332. "Insert a new heading with the same level and TODO state as current heading.
  4333. If the heading has no TODO state, or if the state is DONE, use the first
  4334. state (TODO by default). Also with prefix arg, force first state."
  4335. (interactive "P")
  4336. (when (or force-heading (not (org-insert-item 'checkbox)))
  4337. (org-insert-heading force-heading)
  4338. (save-excursion
  4339. (org-back-to-heading)
  4340. (outline-previous-heading)
  4341. (looking-at org-todo-line-regexp))
  4342. (if (or arg
  4343. (not (match-beginning 2))
  4344. (member (match-string 2) org-done-keywords))
  4345. (insert (car org-todo-keywords-1) " ")
  4346. (insert (match-string 2) " "))
  4347. (when org-provide-todo-statistics
  4348. (org-update-parent-todo-statistics))))
  4349. (defun org-insert-subheading (arg)
  4350. "Insert a new subheading and demote it.
  4351. Works for outline headings and for plain lists alike."
  4352. (interactive "P")
  4353. (org-insert-heading arg)
  4354. (cond
  4355. ((org-on-heading-p) (org-do-demote))
  4356. ((org-at-item-p) (org-indent-item 1))))
  4357. (defun org-insert-todo-subheading (arg)
  4358. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4359. Works for outline headings and for plain lists alike."
  4360. (interactive "P")
  4361. (org-insert-todo-heading arg)
  4362. (cond
  4363. ((org-on-heading-p) (org-do-demote))
  4364. ((org-at-item-p) (org-indent-item 1))))
  4365. ;;; Promotion and Demotion
  4366. (defun org-promote-subtree ()
  4367. "Promote the entire subtree.
  4368. See also `org-promote'."
  4369. (interactive)
  4370. (save-excursion
  4371. (org-map-tree 'org-promote))
  4372. (org-fix-position-after-promote))
  4373. (defun org-demote-subtree ()
  4374. "Demote the entire subtree. See `org-demote'.
  4375. See also `org-promote'."
  4376. (interactive)
  4377. (save-excursion
  4378. (org-map-tree 'org-demote))
  4379. (org-fix-position-after-promote))
  4380. (defun org-do-promote ()
  4381. "Promote the current heading higher up the tree.
  4382. If the region is active in `transient-mark-mode', promote all headings
  4383. in the region."
  4384. (interactive)
  4385. (save-excursion
  4386. (if (org-region-active-p)
  4387. (org-map-region 'org-promote (region-beginning) (region-end))
  4388. (org-promote)))
  4389. (org-fix-position-after-promote))
  4390. (defun org-do-demote ()
  4391. "Demote the current heading lower down the tree.
  4392. If the region is active in `transient-mark-mode', demote all headings
  4393. in the region."
  4394. (interactive)
  4395. (save-excursion
  4396. (if (org-region-active-p)
  4397. (org-map-region 'org-demote (region-beginning) (region-end))
  4398. (org-demote)))
  4399. (org-fix-position-after-promote))
  4400. (defun org-fix-position-after-promote ()
  4401. "Make sure that after pro/demotion cursor position is right."
  4402. (let ((pos (point)))
  4403. (when (save-excursion
  4404. (beginning-of-line 1)
  4405. (looking-at org-todo-line-regexp)
  4406. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4407. (cond ((eobp) (insert " "))
  4408. ((eolp) (insert " "))
  4409. ((equal (char-after) ?\ ) (forward-char 1))))))
  4410. (defun org-reduced-level (l)
  4411. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4412. (defun org-get-valid-level (level &optional change)
  4413. "Rectify a level change under the influence of `org-odd-levels-only'
  4414. LEVEL is a current level, CHANGE is by how much the level should be
  4415. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4416. even level numbers will become the next higher odd number."
  4417. (if org-odd-levels-only
  4418. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4419. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4420. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4421. (max 1 (+ level change))))
  4422. (if (boundp 'define-obsolete-function-alias)
  4423. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4424. (define-obsolete-function-alias 'org-get-legal-level
  4425. 'org-get-valid-level)
  4426. (define-obsolete-function-alias 'org-get-legal-level
  4427. 'org-get-valid-level "23.1")))
  4428. (defun org-promote ()
  4429. "Promote the current heading higher up the tree.
  4430. If the region is active in `transient-mark-mode', promote all headings
  4431. in the region."
  4432. (org-back-to-heading t)
  4433. (let* ((level (save-match-data (funcall outline-level)))
  4434. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4435. (diff (abs (- level (length up-head) -1))))
  4436. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4437. (replace-match up-head nil t)
  4438. ;; Fixup tag positioning
  4439. (and org-auto-align-tags (org-set-tags nil t))
  4440. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4441. (defun org-demote ()
  4442. "Demote the current heading lower down the tree.
  4443. If the region is active in `transient-mark-mode', demote all headings
  4444. in the region."
  4445. (org-back-to-heading t)
  4446. (let* ((level (save-match-data (funcall outline-level)))
  4447. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4448. (diff (abs (- level (length down-head) -1))))
  4449. (replace-match down-head nil t)
  4450. ;; Fixup tag positioning
  4451. (and org-auto-align-tags (org-set-tags nil t))
  4452. (if org-adapt-indentation (org-fixup-indentation diff))))
  4453. (defun org-map-tree (fun)
  4454. "Call FUN for every heading underneath the current one."
  4455. (org-back-to-heading)
  4456. (let ((level (funcall outline-level)))
  4457. (save-excursion
  4458. (funcall fun)
  4459. (while (and (progn
  4460. (outline-next-heading)
  4461. (> (funcall outline-level) level))
  4462. (not (eobp)))
  4463. (funcall fun)))))
  4464. (defun org-map-region (fun beg end)
  4465. "Call FUN for every heading between BEG and END."
  4466. (let ((org-ignore-region t))
  4467. (save-excursion
  4468. (setq end (copy-marker end))
  4469. (goto-char beg)
  4470. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4471. (< (point) end))
  4472. (funcall fun))
  4473. (while (and (progn
  4474. (outline-next-heading)
  4475. (< (point) end))
  4476. (not (eobp)))
  4477. (funcall fun)))))
  4478. (defun org-fixup-indentation (diff)
  4479. "Change the indentation in the current entry by DIFF
  4480. However, if any line in the current entry has no indentation, or if it
  4481. would end up with no indentation after the change, nothing at all is done."
  4482. (save-excursion
  4483. (let ((end (save-excursion (outline-next-heading)
  4484. (point-marker)))
  4485. (prohibit (if (> diff 0)
  4486. "^\\S-"
  4487. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4488. col)
  4489. (unless (save-excursion (end-of-line 1)
  4490. (re-search-forward prohibit end t))
  4491. (while (and (< (point) end)
  4492. (re-search-forward "^[ \t]+" end t))
  4493. (goto-char (match-end 0))
  4494. (setq col (current-column))
  4495. (if (< diff 0) (replace-match ""))
  4496. (org-indent-to-column (+ diff col))))
  4497. (move-marker end nil))))
  4498. (defun org-convert-to-odd-levels ()
  4499. "Convert an org-mode file with all levels allowed to one with odd levels.
  4500. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4501. level 5 etc."
  4502. (interactive)
  4503. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4504. (let ((org-odd-levels-only nil) n)
  4505. (save-excursion
  4506. (goto-char (point-min))
  4507. (while (re-search-forward "^\\*\\*+ " nil t)
  4508. (setq n (- (length (match-string 0)) 2))
  4509. (while (>= (setq n (1- n)) 0)
  4510. (org-demote))
  4511. (end-of-line 1))))))
  4512. (defun org-convert-to-oddeven-levels ()
  4513. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4514. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4515. section with an even level, conversion would destroy the structure of the file. An error
  4516. is signaled in this case."
  4517. (interactive)
  4518. (goto-char (point-min))
  4519. ;; First check if there are no even levels
  4520. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4521. (org-show-context t)
  4522. (error "Not all levels are odd in this file. Conversion not possible."))
  4523. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4524. (let ((org-odd-levels-only nil) n)
  4525. (save-excursion
  4526. (goto-char (point-min))
  4527. (while (re-search-forward "^\\*\\*+ " nil t)
  4528. (setq n (/ (1- (length (match-string 0))) 2))
  4529. (while (>= (setq n (1- n)) 0)
  4530. (org-promote))
  4531. (end-of-line 1))))))
  4532. (defun org-tr-level (n)
  4533. "Make N odd if required."
  4534. (if org-odd-levels-only (1+ (/ n 2)) n))
  4535. ;;; Vertical tree motion, cutting and pasting of subtrees
  4536. (defun org-move-subtree-up (&optional arg)
  4537. "Move the current subtree up past ARG headlines of the same level."
  4538. (interactive "p")
  4539. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4540. (defun org-move-subtree-down (&optional arg)
  4541. "Move the current subtree down past ARG headlines of the same level."
  4542. (interactive "p")
  4543. (setq arg (prefix-numeric-value arg))
  4544. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4545. 'outline-get-last-sibling))
  4546. (ins-point (make-marker))
  4547. (cnt (abs arg))
  4548. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4549. ;; Select the tree
  4550. (org-back-to-heading)
  4551. (setq beg0 (point))
  4552. (save-excursion
  4553. (setq ne-beg (org-back-over-empty-lines))
  4554. (setq beg (point)))
  4555. (save-match-data
  4556. (save-excursion (outline-end-of-heading)
  4557. (setq folded (org-invisible-p)))
  4558. (outline-end-of-subtree))
  4559. (outline-next-heading)
  4560. (setq ne-end (org-back-over-empty-lines))
  4561. (setq end (point))
  4562. (goto-char beg0)
  4563. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4564. ;; include less whitespace
  4565. (save-excursion
  4566. (goto-char beg)
  4567. (forward-line (- ne-beg ne-end))
  4568. (setq beg (point))))
  4569. ;; Find insertion point, with error handling
  4570. (while (> cnt 0)
  4571. (or (and (funcall movfunc) (looking-at outline-regexp))
  4572. (progn (goto-char beg0)
  4573. (error "Cannot move past superior level or buffer limit")))
  4574. (setq cnt (1- cnt)))
  4575. (if (> arg 0)
  4576. ;; Moving forward - still need to move over subtree
  4577. (progn (org-end-of-subtree t t)
  4578. (save-excursion
  4579. (org-back-over-empty-lines)
  4580. (or (bolp) (newline)))))
  4581. (setq ne-ins (org-back-over-empty-lines))
  4582. (move-marker ins-point (point))
  4583. (setq txt (buffer-substring beg end))
  4584. (org-save-markers-in-region beg end)
  4585. (delete-region beg end)
  4586. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4587. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4588. (let ((bbb (point)))
  4589. (insert-before-markers txt)
  4590. (org-reinstall-markers-in-region bbb)
  4591. (move-marker ins-point bbb))
  4592. (or (bolp) (insert "\n"))
  4593. (setq ins-end (point))
  4594. (goto-char ins-point)
  4595. (org-skip-whitespace)
  4596. (when (and (< arg 0)
  4597. (org-first-sibling-p)
  4598. (> ne-ins ne-beg))
  4599. ;; Move whitespace back to beginning
  4600. (save-excursion
  4601. (goto-char ins-end)
  4602. (let ((kill-whole-line t))
  4603. (kill-line (- ne-ins ne-beg)) (point)))
  4604. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4605. (move-marker ins-point nil)
  4606. (org-compact-display-after-subtree-move)
  4607. (org-show-empty-lines-in-parent)
  4608. (unless folded
  4609. (org-show-entry)
  4610. (show-children)
  4611. (org-cycle-hide-drawers 'children))))
  4612. (defvar org-subtree-clip ""
  4613. "Clipboard for cut and paste of subtrees.
  4614. This is actually only a copy of the kill, because we use the normal kill
  4615. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4616. (defvar org-subtree-clip-folded nil
  4617. "Was the last copied subtree folded?
  4618. This is used to fold the tree back after pasting.")
  4619. (defun org-cut-subtree (&optional n)
  4620. "Cut the current subtree into the clipboard.
  4621. With prefix arg N, cut this many sequential subtrees.
  4622. This is a short-hand for marking the subtree and then cutting it."
  4623. (interactive "p")
  4624. (org-copy-subtree n 'cut))
  4625. (defun org-copy-subtree (&optional n cut force-store-markers)
  4626. "Cut the current subtree into the clipboard.
  4627. With prefix arg N, cut this many sequential subtrees.
  4628. This is a short-hand for marking the subtree and then copying it.
  4629. If CUT is non-nil, actually cut the subtree.
  4630. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4631. of some markers in the region, even if CUT is non-nil. This is
  4632. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4633. (interactive "p")
  4634. (let (beg end folded (beg0 (point)))
  4635. (if (interactive-p)
  4636. (org-back-to-heading nil) ; take what looks like a subtree
  4637. (org-back-to-heading t)) ; take what is really there
  4638. (org-back-over-empty-lines)
  4639. (setq beg (point))
  4640. (skip-chars-forward " \t\r\n")
  4641. (save-match-data
  4642. (save-excursion (outline-end-of-heading)
  4643. (setq folded (org-invisible-p)))
  4644. (condition-case nil
  4645. (outline-forward-same-level (1- n))
  4646. (error nil))
  4647. (org-end-of-subtree t t))
  4648. (org-back-over-empty-lines)
  4649. (setq end (point))
  4650. (goto-char beg0)
  4651. (when (> end beg)
  4652. (setq org-subtree-clip-folded folded)
  4653. (when (or cut force-store-markers)
  4654. (org-save-markers-in-region beg end))
  4655. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4656. (setq org-subtree-clip (current-kill 0))
  4657. (message "%s: Subtree(s) with %d characters"
  4658. (if cut "Cut" "Copied")
  4659. (length org-subtree-clip)))))
  4660. (defun org-paste-subtree (&optional level tree for-yank)
  4661. "Paste the clipboard as a subtree, with modification of headline level.
  4662. The entire subtree is promoted or demoted in order to match a new headline
  4663. level.
  4664. If the cursor is at the beginning of a headline, the same level as
  4665. that headline is used to paste the tree
  4666. If not, the new level is derived from the *visible* headings
  4667. before and after the insertion point, and taken to be the inferior headline
  4668. level of the two. So if the previous visible heading is level 3 and the
  4669. next is level 4 (or vice versa), level 4 will be used for insertion.
  4670. This makes sure that the subtree remains an independent subtree and does
  4671. not swallow low level entries.
  4672. You can also force a different level, either by using a numeric prefix
  4673. argument, or by inserting the heading marker by hand. For example, if the
  4674. cursor is after \"*****\", then the tree will be shifted to level 5.
  4675. If optional TREE is given, use this text instead of the kill ring.
  4676. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4677. move back over whitespace before inserting, and move point to the end of
  4678. the inserted text when done."
  4679. (interactive "P")
  4680. (unless (org-kill-is-subtree-p tree)
  4681. (error "%s"
  4682. (substitute-command-keys
  4683. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4684. (let* ((visp (not (org-invisible-p)))
  4685. (txt (or tree (and kill-ring (current-kill 0))))
  4686. (^re (concat "^\\(" outline-regexp "\\)"))
  4687. (re (concat "\\(" outline-regexp "\\)"))
  4688. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4689. (old-level (if (string-match ^re txt)
  4690. (- (match-end 0) (match-beginning 0) 1)
  4691. -1))
  4692. (force-level (cond (level (prefix-numeric-value level))
  4693. ((and (looking-at "[ \t]*$")
  4694. (string-match
  4695. ^re_ (buffer-substring
  4696. (point-at-bol) (point))))
  4697. (- (match-end 1) (match-beginning 1)))
  4698. ((and (bolp)
  4699. (looking-at org-outline-regexp))
  4700. (- (match-end 0) (point) 1))
  4701. (t nil)))
  4702. (previous-level (save-excursion
  4703. (condition-case nil
  4704. (progn
  4705. (outline-previous-visible-heading 1)
  4706. (if (looking-at re)
  4707. (- (match-end 0) (match-beginning 0) 1)
  4708. 1))
  4709. (error 1))))
  4710. (next-level (save-excursion
  4711. (condition-case nil
  4712. (progn
  4713. (or (looking-at outline-regexp)
  4714. (outline-next-visible-heading 1))
  4715. (if (looking-at re)
  4716. (- (match-end 0) (match-beginning 0) 1)
  4717. 1))
  4718. (error 1))))
  4719. (new-level (or force-level (max previous-level next-level)))
  4720. (shift (if (or (= old-level -1)
  4721. (= new-level -1)
  4722. (= old-level new-level))
  4723. 0
  4724. (- new-level old-level)))
  4725. (delta (if (> shift 0) -1 1))
  4726. (func (if (> shift 0) 'org-demote 'org-promote))
  4727. (org-odd-levels-only nil)
  4728. beg end newend)
  4729. ;; Remove the forced level indicator
  4730. (if force-level
  4731. (delete-region (point-at-bol) (point)))
  4732. ;; Paste
  4733. (beginning-of-line 1)
  4734. (unless for-yank (org-back-over-empty-lines))
  4735. (setq beg (point))
  4736. (insert-before-markers txt)
  4737. (unless (string-match "\n\\'" txt) (insert "\n"))
  4738. (setq newend (point))
  4739. (org-reinstall-markers-in-region beg)
  4740. (setq end (point))
  4741. (goto-char beg)
  4742. (skip-chars-forward " \t\n\r")
  4743. (setq beg (point))
  4744. (if (and (org-invisible-p) visp)
  4745. (save-excursion (outline-show-heading)))
  4746. ;; Shift if necessary
  4747. (unless (= shift 0)
  4748. (save-restriction
  4749. (narrow-to-region beg end)
  4750. (while (not (= shift 0))
  4751. (org-map-region func (point-min) (point-max))
  4752. (setq shift (+ delta shift)))
  4753. (goto-char (point-min))
  4754. (setq newend (point-max))))
  4755. (when (or (interactive-p) for-yank)
  4756. (message "Clipboard pasted as level %d subtree" new-level))
  4757. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4758. kill-ring
  4759. (eq org-subtree-clip (current-kill 0))
  4760. org-subtree-clip-folded)
  4761. ;; The tree was folded before it was killed/copied
  4762. (hide-subtree))
  4763. (and for-yank (goto-char newend))))
  4764. (defun org-kill-is-subtree-p (&optional txt)
  4765. "Check if the current kill is an outline subtree, or a set of trees.
  4766. Returns nil if kill does not start with a headline, or if the first
  4767. headline level is not the largest headline level in the tree.
  4768. So this will actually accept several entries of equal levels as well,
  4769. which is OK for `org-paste-subtree'.
  4770. If optional TXT is given, check this string instead of the current kill."
  4771. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4772. (start-level (and kill
  4773. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4774. org-outline-regexp "\\)")
  4775. kill)
  4776. (- (match-end 2) (match-beginning 2) 1)))
  4777. (re (concat "^" org-outline-regexp))
  4778. (start (1+ (or (match-beginning 2) -1))))
  4779. (if (not start-level)
  4780. (progn
  4781. nil) ;; does not even start with a heading
  4782. (catch 'exit
  4783. (while (setq start (string-match re kill (1+ start)))
  4784. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4785. (throw 'exit nil)))
  4786. t))))
  4787. (defvar org-markers-to-move nil
  4788. "Markers that should be moved with a cut-and-paste operation.
  4789. Those markers are stored together with their positions relative to
  4790. the start of the region.")
  4791. (defun org-save-markers-in-region (beg end)
  4792. "Check markers in region.
  4793. If these markers are between BEG and END, record their position relative
  4794. to BEG, so that after moving the block of text, we can put the markers back
  4795. into place.
  4796. This function gets called just before an entry or tree gets cut from the
  4797. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4798. called immediately, to move the markers with the entries."
  4799. (setq org-markers-to-move nil)
  4800. (when (featurep 'org-clock)
  4801. (org-clock-save-markers-for-cut-and-paste beg end))
  4802. (when (featurep 'org-agenda)
  4803. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4804. (defun org-check-and-save-marker (marker beg end)
  4805. "Check if MARKER is between BEG and END.
  4806. If yes, remember the marker and the distance to BEG."
  4807. (when (and (marker-buffer marker)
  4808. (equal (marker-buffer marker) (current-buffer)))
  4809. (if (and (>= marker beg) (< marker end))
  4810. (push (cons marker (- marker beg)) org-markers-to-move))))
  4811. (defun org-reinstall-markers-in-region (beg)
  4812. "Move all remembered markers to their position relative to BEG."
  4813. (mapc (lambda (x)
  4814. (move-marker (car x) (+ beg (cdr x))))
  4815. org-markers-to-move)
  4816. (setq org-markers-to-move nil))
  4817. (defun org-narrow-to-subtree ()
  4818. "Narrow buffer to the current subtree."
  4819. (interactive)
  4820. (save-excursion
  4821. (save-match-data
  4822. (narrow-to-region
  4823. (progn (org-back-to-heading) (point))
  4824. (progn (org-end-of-subtree t) (point))))))
  4825. ;;; Outline Sorting
  4826. (defun org-sort (with-case)
  4827. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4828. Optional argument WITH-CASE means sort case-sensitively."
  4829. (interactive "P")
  4830. (if (org-at-table-p)
  4831. (org-call-with-arg 'org-table-sort-lines with-case)
  4832. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4833. (defun org-sort-remove-invisible (s)
  4834. (remove-text-properties 0 (length s) org-rm-props s)
  4835. (while (string-match org-bracket-link-regexp s)
  4836. (setq s (replace-match (if (match-end 2)
  4837. (match-string 3 s)
  4838. (match-string 1 s)) t t s)))
  4839. s)
  4840. (defvar org-priority-regexp) ; defined later in the file
  4841. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4842. "Sort entries on a certain level of an outline tree.
  4843. If there is an active region, the entries in the region are sorted.
  4844. Else, if the cursor is before the first entry, sort the top-level items.
  4845. Else, the children of the entry at point are sorted.
  4846. Sorting can be alphabetically, numerically, and by date/time as given by
  4847. the first time stamp in the entry. The command prompts for the sorting
  4848. type unless it has been given to the function through the SORTING-TYPE
  4849. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4850. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4851. called with point at the beginning of the record. It must return either
  4852. a string or a number that should serve as the sorting key for that record.
  4853. Comparing entries ignores case by default. However, with an optional argument
  4854. WITH-CASE, the sorting considers case as well."
  4855. (interactive "P")
  4856. (let ((case-func (if with-case 'identity 'downcase))
  4857. start beg end stars re re2
  4858. txt what tmp plain-list-p)
  4859. ;; Find beginning and end of region to sort
  4860. (cond
  4861. ((org-region-active-p)
  4862. ;; we will sort the region
  4863. (setq end (region-end)
  4864. what "region")
  4865. (goto-char (region-beginning))
  4866. (if (not (org-on-heading-p)) (outline-next-heading))
  4867. (setq start (point)))
  4868. ((org-at-item-p)
  4869. ;; we will sort this plain list
  4870. (org-beginning-of-item-list) (setq start (point))
  4871. (org-end-of-item-list) (setq end (point))
  4872. (goto-char start)
  4873. (setq plain-list-p t
  4874. what "plain list"))
  4875. ((or (org-on-heading-p)
  4876. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4877. ;; we will sort the children of the current headline
  4878. (org-back-to-heading)
  4879. (setq start (point)
  4880. end (progn (org-end-of-subtree t t)
  4881. (org-back-over-empty-lines)
  4882. (point))
  4883. what "children")
  4884. (goto-char start)
  4885. (show-subtree)
  4886. (outline-next-heading))
  4887. (t
  4888. ;; we will sort the top-level entries in this file
  4889. (goto-char (point-min))
  4890. (or (org-on-heading-p) (outline-next-heading))
  4891. (setq start (point) end (point-max) what "top-level")
  4892. (goto-char start)
  4893. (show-all)))
  4894. (setq beg (point))
  4895. (if (>= beg end) (error "Nothing to sort"))
  4896. (unless plain-list-p
  4897. (looking-at "\\(\\*+\\)")
  4898. (setq stars (match-string 1)
  4899. re (concat "^" (regexp-quote stars) " +")
  4900. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4901. txt (buffer-substring beg end))
  4902. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4903. (if (and (not (equal stars "*")) (string-match re2 txt))
  4904. (error "Region to sort contains a level above the first entry")))
  4905. (unless sorting-type
  4906. (message
  4907. (if plain-list-p
  4908. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4909. "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:")
  4910. what)
  4911. (setq sorting-type (read-char-exclusive))
  4912. (and (= (downcase sorting-type) ?f)
  4913. (setq getkey-func
  4914. (org-ido-completing-read "Sort using function: "
  4915. obarray 'fboundp t nil nil))
  4916. (setq getkey-func (intern getkey-func)))
  4917. (and (= (downcase sorting-type) ?r)
  4918. (setq property
  4919. (org-ido-completing-read "Property: "
  4920. (mapcar 'list (org-buffer-property-keys t))
  4921. nil t))))
  4922. (message "Sorting entries...")
  4923. (save-restriction
  4924. (narrow-to-region start end)
  4925. (let ((dcst (downcase sorting-type))
  4926. (now (current-time)))
  4927. (sort-subr
  4928. (/= dcst sorting-type)
  4929. ;; This function moves to the beginning character of the "record" to
  4930. ;; be sorted.
  4931. (if plain-list-p
  4932. (lambda nil
  4933. (if (org-at-item-p) t (goto-char (point-max))))
  4934. (lambda nil
  4935. (if (re-search-forward re nil t)
  4936. (goto-char (match-beginning 0))
  4937. (goto-char (point-max)))))
  4938. ;; This function moves to the last character of the "record" being
  4939. ;; sorted.
  4940. (if plain-list-p
  4941. 'org-end-of-item
  4942. (lambda nil
  4943. (save-match-data
  4944. (condition-case nil
  4945. (outline-forward-same-level 1)
  4946. (error
  4947. (goto-char (point-max)))))))
  4948. ;; This function returns the value that gets sorted against.
  4949. (if plain-list-p
  4950. (lambda nil
  4951. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4952. (cond
  4953. ((= dcst ?n)
  4954. (string-to-number (buffer-substring (match-end 0)
  4955. (point-at-eol))))
  4956. ((= dcst ?a)
  4957. (buffer-substring (match-end 0) (point-at-eol)))
  4958. ((= dcst ?t)
  4959. (if (re-search-forward org-ts-regexp
  4960. (point-at-eol) t)
  4961. (org-time-string-to-time (match-string 0))
  4962. now))
  4963. ((= dcst ?f)
  4964. (if getkey-func
  4965. (progn
  4966. (setq tmp (funcall getkey-func))
  4967. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4968. tmp)
  4969. (error "Invalid key function `%s'" getkey-func)))
  4970. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4971. (lambda nil
  4972. (cond
  4973. ((= dcst ?n)
  4974. (if (looking-at org-complex-heading-regexp)
  4975. (string-to-number (match-string 4))
  4976. nil))
  4977. ((= dcst ?a)
  4978. (if (looking-at org-complex-heading-regexp)
  4979. (funcall case-func (match-string 4))
  4980. nil))
  4981. ((= dcst ?t)
  4982. (if (re-search-forward org-ts-regexp
  4983. (save-excursion
  4984. (forward-line 2)
  4985. (point)) t)
  4986. (org-time-string-to-time (match-string 0))
  4987. now))
  4988. ((= dcst ?p)
  4989. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4990. (string-to-char (match-string 2))
  4991. org-default-priority))
  4992. ((= dcst ?r)
  4993. (or (org-entry-get nil property) ""))
  4994. ((= dcst ?o)
  4995. (if (looking-at org-complex-heading-regexp)
  4996. (- 9999 (length (member (match-string 2)
  4997. org-todo-keywords-1)))))
  4998. ((= dcst ?f)
  4999. (if getkey-func
  5000. (progn
  5001. (setq tmp (funcall getkey-func))
  5002. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5003. tmp)
  5004. (error "Invalid key function `%s'" getkey-func)))
  5005. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5006. nil
  5007. (cond
  5008. ((= dcst ?a) 'string<)
  5009. ((= dcst ?t) 'time-less-p)
  5010. (t nil)))))
  5011. (message "Sorting entries...done")))
  5012. (defun org-do-sort (table what &optional with-case sorting-type)
  5013. "Sort TABLE of WHAT according to SORTING-TYPE.
  5014. The user will be prompted for the SORTING-TYPE if the call to this
  5015. function does not specify it. WHAT is only for the prompt, to indicate
  5016. what is being sorted. The sorting key will be extracted from
  5017. the car of the elements of the table.
  5018. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5019. (unless sorting-type
  5020. (message
  5021. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5022. what)
  5023. (setq sorting-type (read-char-exclusive)))
  5024. (let ((dcst (downcase sorting-type))
  5025. extractfun comparefun)
  5026. ;; Define the appropriate functions
  5027. (cond
  5028. ((= dcst ?n)
  5029. (setq extractfun 'string-to-number
  5030. comparefun (if (= dcst sorting-type) '< '>)))
  5031. ((= dcst ?a)
  5032. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5033. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5034. comparefun (if (= dcst sorting-type)
  5035. 'string<
  5036. (lambda (a b) (and (not (string< a b))
  5037. (not (string= a b)))))))
  5038. ((= dcst ?t)
  5039. (setq extractfun
  5040. (lambda (x)
  5041. (if (string-match org-ts-regexp x)
  5042. (time-to-seconds
  5043. (org-time-string-to-time (match-string 0 x)))
  5044. 0))
  5045. comparefun (if (= dcst sorting-type) '< '>)))
  5046. (t (error "Invalid sorting type `%c'" sorting-type)))
  5047. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5048. table)
  5049. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5050. ;;; Editing source examples
  5051. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5052. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5053. (defvar org-edit-src-force-single-line nil)
  5054. (defvar org-edit-src-from-org-mode nil)
  5055. (defvar org-edit-src-picture nil)
  5056. (define-minor-mode org-exit-edit-mode
  5057. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5058. (defun org-edit-src-code ()
  5059. "Edit the source code example at point.
  5060. An indirect buffer is created, and that buffer is then narrowed to the
  5061. example at point and switched to the correct language mode. When done,
  5062. exit by killing the buffer with \\[org-edit-src-exit]."
  5063. (interactive)
  5064. (let ((line (org-current-line))
  5065. (case-fold-search t)
  5066. (msg (substitute-command-keys
  5067. "Edit, then exit with C-c ' (C-c and single quote)"))
  5068. (info (org-edit-src-find-region-and-lang))
  5069. (org-mode-p (eq major-mode 'org-mode))
  5070. beg end lang lang-f single)
  5071. (if (not info)
  5072. nil
  5073. (setq beg (nth 0 info)
  5074. end (nth 1 info)
  5075. lang (nth 2 info)
  5076. single (nth 3 info)
  5077. lang-f (intern (concat lang "-mode")))
  5078. (unless (functionp lang-f)
  5079. (error "No such language mode: %s" lang-f))
  5080. (goto-line line)
  5081. (if (get-buffer "*Org Edit Src Example*")
  5082. (kill-buffer "*Org Edit Src Example*"))
  5083. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5084. "*Org Edit Src Example*"))
  5085. (narrow-to-region beg end)
  5086. (remove-text-properties beg end '(display nil invisible nil
  5087. intangible nil))
  5088. (let ((org-inhibit-startup t))
  5089. (funcall lang-f))
  5090. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5091. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5092. (when org-mode-p
  5093. (goto-char (point-min))
  5094. (while (re-search-forward "^," nil t)
  5095. (replace-match "")))
  5096. (goto-line line)
  5097. (org-exit-edit-mode)
  5098. (org-set-local 'header-line-format msg)
  5099. (message "%s" msg)
  5100. t)))
  5101. (defun org-edit-fixed-width-region ()
  5102. "Edit the fixed-width ascii drawing at point.
  5103. This must be a region where each line starts with ca colon followed by
  5104. a space character.
  5105. An indirect buffer is created, and that buffer is then narrowed to the
  5106. example at point and switched to artist-mode. When done,
  5107. exit by killing the buffer with \\[org-edit-src-exit]."
  5108. (interactive)
  5109. (let ((line (org-current-line))
  5110. (case-fold-search t)
  5111. (msg (substitute-command-keys
  5112. "Edit, then exit with C-c ' (C-c and single quote)"))
  5113. (org-mode-p (eq major-mode 'org-mode))
  5114. beg end lang lang-f)
  5115. (beginning-of-line 1)
  5116. (if (looking-at "[ \t]*[^:\n \t]")
  5117. nil
  5118. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5119. (setq beg (point) end beg)
  5120. (save-excursion
  5121. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5122. (setq beg (point-at-bol 2))
  5123. (setq beg (point))))
  5124. (save-excursion
  5125. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5126. (setq end (1- (match-beginning 0)))
  5127. (setq end (point))))
  5128. (goto-line line))
  5129. (if (get-buffer "*Org Edit Picture*")
  5130. (kill-buffer "*Org Edit Picture*"))
  5131. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5132. "*Org Edit Picture*"))
  5133. (narrow-to-region beg end)
  5134. (remove-text-properties beg end '(display nil invisible nil
  5135. intangible nil))
  5136. (when (fboundp 'font-lock-unfontify-region)
  5137. (font-lock-unfontify-region (point-min) (point-max)))
  5138. (cond
  5139. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5140. (fundamental-mode)
  5141. (artist-mode 1))
  5142. (t (funcall org-edit-fixed-width-region-mode)))
  5143. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5144. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5145. (set (make-local-variable 'org-edit-src-picture) t)
  5146. (goto-char (point-min))
  5147. (while (re-search-forward "^[ \t]*: ?" nil t)
  5148. (replace-match ""))
  5149. (goto-line line)
  5150. (org-exit-edit-mode)
  5151. (org-set-local 'header-line-format msg)
  5152. (message "%s" msg)
  5153. t)))
  5154. (defun org-edit-src-find-region-and-lang ()
  5155. "Find the region and language for a local edit.
  5156. Return a list with beginning and end of the region, a string representing
  5157. the language, a switch telling of the content should be in a single line."
  5158. (let ((re-list
  5159. (append
  5160. org-edit-src-region-extra
  5161. '(
  5162. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5163. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5164. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5165. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5166. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5167. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5168. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5169. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5170. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5171. ("^#\\+html:" "\n" "html" single-line)
  5172. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5173. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5174. ("^#\\+latex:" "\n" "latex" single-line)
  5175. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5176. ("^#\\+ascii:" "\n" "ascii" single-line)
  5177. )))
  5178. (pos (point))
  5179. re re1 re2 single beg end lang)
  5180. (catch 'exit
  5181. (while (setq entry (pop re-list))
  5182. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5183. single (nth 3 entry))
  5184. (save-excursion
  5185. (if (or (looking-at re1)
  5186. (re-search-backward re1 nil t))
  5187. (progn
  5188. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5189. (if (and (re-search-forward re2 nil t)
  5190. (>= (match-end 0) pos))
  5191. (throw 'exit (list beg (match-beginning 0) lang single))))
  5192. (if (or (looking-at re2)
  5193. (re-search-forward re2 nil t))
  5194. (progn
  5195. (setq end (match-beginning 0))
  5196. (if (and (re-search-backward re1 nil t)
  5197. (<= (match-beginning 0) pos))
  5198. (throw 'exit
  5199. (list (match-end 0) end
  5200. (org-edit-src-get-lang lang) single)))))))))))
  5201. (defun org-edit-src-get-lang (lang)
  5202. "Extract the src language."
  5203. (let ((m (match-string 0)))
  5204. (cond
  5205. ((stringp lang) lang)
  5206. ((integerp lang) (match-string lang))
  5207. ((and (eq lang 'lang)
  5208. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5209. (match-string 1 m))
  5210. ((and (eq lang 'style)
  5211. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5212. (match-string 1 m))
  5213. (t "fundamental"))))
  5214. (defun org-edit-src-exit ()
  5215. "Exit special edit and protect problematic lines."
  5216. (interactive)
  5217. (unless (buffer-base-buffer (current-buffer))
  5218. (error "This is not an indirect buffer, something is wrong..."))
  5219. (unless (> (point-min) 1)
  5220. (error "This buffer is not narrowed, something is wrong..."))
  5221. (goto-char (point-min))
  5222. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5223. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5224. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5225. (goto-char (point-min))
  5226. (while (re-search-forward "\n" nil t)
  5227. (replace-match " "))
  5228. (goto-char (point-min))
  5229. (if (looking-at "\\s-*") (replace-match " "))
  5230. (if (re-search-forward "\\s-+\\'" nil t)
  5231. (replace-match "")))
  5232. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5233. (goto-char (point-min))
  5234. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5235. (replace-match ",\\1"))
  5236. (when font-lock-mode
  5237. (font-lock-unfontify-region (point-min) (point-max)))
  5238. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5239. (when (org-bound-and-true-p org-edit-src-picture)
  5240. (untabify (point-min) (point-max))
  5241. (goto-char (point-min))
  5242. (while (re-search-forward "^" nil t)
  5243. (replace-match ": "))
  5244. (when font-lock-mode
  5245. (font-lock-unfontify-region (point-min) (point-max)))
  5246. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5247. (kill-buffer (current-buffer))
  5248. (and (org-mode-p) (org-restart-font-lock)))
  5249. ;;; The orgstruct minor mode
  5250. ;; Define a minor mode which can be used in other modes in order to
  5251. ;; integrate the org-mode structure editing commands.
  5252. ;; This is really a hack, because the org-mode structure commands use
  5253. ;; keys which normally belong to the major mode. Here is how it
  5254. ;; works: The minor mode defines all the keys necessary to operate the
  5255. ;; structure commands, but wraps the commands into a function which
  5256. ;; tests if the cursor is currently at a headline or a plain list
  5257. ;; item. If that is the case, the structure command is used,
  5258. ;; temporarily setting many Org-mode variables like regular
  5259. ;; expressions for filling etc. However, when any of those keys is
  5260. ;; used at a different location, function uses `key-binding' to look
  5261. ;; up if the key has an associated command in another currently active
  5262. ;; keymap (minor modes, major mode, global), and executes that
  5263. ;; command. There might be problems if any of the keys is otherwise
  5264. ;; used as a prefix key.
  5265. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5266. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5267. ;; addresses this by checking explicitly for both bindings.
  5268. (defvar orgstruct-mode-map (make-sparse-keymap)
  5269. "Keymap for the minor `orgstruct-mode'.")
  5270. (defvar org-local-vars nil
  5271. "List of local variables, for use by `orgstruct-mode'")
  5272. ;;;###autoload
  5273. (define-minor-mode orgstruct-mode
  5274. "Toggle the minor more `orgstruct-mode'.
  5275. This mode is for using Org-mode structure commands in other modes.
  5276. The following key behave as if Org-mode was active, if the cursor
  5277. is on a headline, or on a plain list item (both in the definition
  5278. of Org-mode).
  5279. M-up Move entry/item up
  5280. M-down Move entry/item down
  5281. M-left Promote
  5282. M-right Demote
  5283. M-S-up Move entry/item up
  5284. M-S-down Move entry/item down
  5285. M-S-left Promote subtree
  5286. M-S-right Demote subtree
  5287. M-q Fill paragraph and items like in Org-mode
  5288. C-c ^ Sort entries
  5289. C-c - Cycle list bullet
  5290. TAB Cycle item visibility
  5291. M-RET Insert new heading/item
  5292. S-M-RET Insert new TODO heading / Chekbox item
  5293. C-c C-c Set tags / toggle checkbox"
  5294. nil " OrgStruct" nil
  5295. (org-load-modules-maybe)
  5296. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5297. ;;;###autoload
  5298. (defun turn-on-orgstruct ()
  5299. "Unconditionally turn on `orgstruct-mode'."
  5300. (orgstruct-mode 1))
  5301. ;;;###autoload
  5302. (defun turn-on-orgstruct++ ()
  5303. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5304. In addition to setting orgstruct-mode, this also exports all indentation and
  5305. autofilling variables from org-mode into the buffer. Note that turning
  5306. off orgstruct-mode will *not* remove these additional settings."
  5307. (orgstruct-mode 1)
  5308. (let (var val)
  5309. (mapc
  5310. (lambda (x)
  5311. (when (string-match
  5312. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5313. (symbol-name (car x)))
  5314. (setq var (car x) val (nth 1 x))
  5315. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5316. org-local-vars)))
  5317. (defun orgstruct-error ()
  5318. "Error when there is no default binding for a structure key."
  5319. (interactive)
  5320. (error "This key has no function outside structure elements"))
  5321. (defun orgstruct-setup ()
  5322. "Setup orgstruct keymaps."
  5323. (let ((nfunc 0)
  5324. (bindings
  5325. (list
  5326. '([(meta up)] org-metaup)
  5327. '([(meta down)] org-metadown)
  5328. '([(meta left)] org-metaleft)
  5329. '([(meta right)] org-metaright)
  5330. '([(meta shift up)] org-shiftmetaup)
  5331. '([(meta shift down)] org-shiftmetadown)
  5332. '([(meta shift left)] org-shiftmetaleft)
  5333. '([(meta shift right)] org-shiftmetaright)
  5334. '([(shift up)] org-shiftup)
  5335. '([(shift down)] org-shiftdown)
  5336. '([(shift left)] org-shiftleft)
  5337. '([(shift right)] org-shiftright)
  5338. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5339. '("\M-q" fill-paragraph)
  5340. '("\C-c^" org-sort)
  5341. '("\C-c-" org-cycle-list-bullet)))
  5342. elt key fun cmd)
  5343. (while (setq elt (pop bindings))
  5344. (setq nfunc (1+ nfunc))
  5345. (setq key (org-key (car elt))
  5346. fun (nth 1 elt)
  5347. cmd (orgstruct-make-binding fun nfunc key))
  5348. (org-defkey orgstruct-mode-map key cmd))
  5349. ;; Special treatment needed for TAB and RET
  5350. (org-defkey orgstruct-mode-map [(tab)]
  5351. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5352. (org-defkey orgstruct-mode-map "\C-i"
  5353. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5354. (org-defkey orgstruct-mode-map "\M-\C-m"
  5355. (orgstruct-make-binding 'org-insert-heading 105
  5356. "\M-\C-m" [(meta return)]))
  5357. (org-defkey orgstruct-mode-map [(meta return)]
  5358. (orgstruct-make-binding 'org-insert-heading 106
  5359. [(meta return)] "\M-\C-m"))
  5360. (org-defkey orgstruct-mode-map [(shift meta return)]
  5361. (orgstruct-make-binding 'org-insert-todo-heading 107
  5362. [(meta return)] "\M-\C-m"))
  5363. (unless org-local-vars
  5364. (setq org-local-vars (org-get-local-variables)))
  5365. t))
  5366. (defun orgstruct-make-binding (fun n &rest keys)
  5367. "Create a function for binding in the structure minor mode.
  5368. FUN is the command to call inside a table. N is used to create a unique
  5369. command name. KEYS are keys that should be checked in for a command
  5370. to execute outside of tables."
  5371. (eval
  5372. (list 'defun
  5373. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5374. '(arg)
  5375. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5376. "Outside of structure, run the binding of `"
  5377. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5378. "'.")
  5379. '(interactive "p")
  5380. (list 'if
  5381. '(org-context-p 'headline 'item)
  5382. (list 'org-run-like-in-org-mode (list 'quote fun))
  5383. (list 'let '(orgstruct-mode)
  5384. (list 'call-interactively
  5385. (append '(or)
  5386. (mapcar (lambda (k)
  5387. (list 'key-binding k))
  5388. keys)
  5389. '('orgstruct-error))))))))
  5390. (defun org-context-p (&rest contexts)
  5391. "Check if local context is any of CONTEXTS.
  5392. Possible values in the list of contexts are `table', `headline', and `item'."
  5393. (let ((pos (point)))
  5394. (goto-char (point-at-bol))
  5395. (prog1 (or (and (memq 'table contexts)
  5396. (looking-at "[ \t]*|"))
  5397. (and (memq 'headline contexts)
  5398. ;;????????? (looking-at "\\*+"))
  5399. (looking-at outline-regexp))
  5400. (and (memq 'item contexts)
  5401. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5402. (goto-char pos))))
  5403. (defun org-get-local-variables ()
  5404. "Return a list of all local variables in an org-mode buffer."
  5405. (let (varlist)
  5406. (with-current-buffer (get-buffer-create "*Org tmp*")
  5407. (erase-buffer)
  5408. (org-mode)
  5409. (setq varlist (buffer-local-variables)))
  5410. (kill-buffer "*Org tmp*")
  5411. (delq nil
  5412. (mapcar
  5413. (lambda (x)
  5414. (setq x
  5415. (if (symbolp x)
  5416. (list x)
  5417. (list (car x) (list 'quote (cdr x)))))
  5418. (if (string-match
  5419. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5420. (symbol-name (car x)))
  5421. x nil))
  5422. varlist))))
  5423. ;;;###autoload
  5424. (defun org-run-like-in-org-mode (cmd)
  5425. (org-load-modules-maybe)
  5426. (unless org-local-vars
  5427. (setq org-local-vars (org-get-local-variables)))
  5428. (eval (list 'let org-local-vars
  5429. (list 'call-interactively (list 'quote cmd)))))
  5430. ;;;; Archiving
  5431. (defun org-get-category (&optional pos)
  5432. "Get the category applying to position POS."
  5433. (get-text-property (or pos (point)) 'org-category))
  5434. (defun org-refresh-category-properties ()
  5435. "Refresh category text properties in the buffer."
  5436. (let ((def-cat (cond
  5437. ((null org-category)
  5438. (if buffer-file-name
  5439. (file-name-sans-extension
  5440. (file-name-nondirectory buffer-file-name))
  5441. "???"))
  5442. ((symbolp org-category) (symbol-name org-category))
  5443. (t org-category)))
  5444. beg end cat pos optionp)
  5445. (org-unmodified
  5446. (save-excursion
  5447. (save-restriction
  5448. (widen)
  5449. (goto-char (point-min))
  5450. (put-text-property (point) (point-max) 'org-category def-cat)
  5451. (while (re-search-forward
  5452. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5453. (setq pos (match-end 0)
  5454. optionp (equal (char-after (match-beginning 0)) ?#)
  5455. cat (org-trim (match-string 2)))
  5456. (if optionp
  5457. (setq beg (point-at-bol) end (point-max))
  5458. (org-back-to-heading t)
  5459. (setq beg (point) end (org-end-of-subtree t t)))
  5460. (put-text-property beg end 'org-category cat)
  5461. (goto-char pos)))))))
  5462. ;;;; Link Stuff
  5463. ;;; Link abbreviations
  5464. (defun org-link-expand-abbrev (link)
  5465. "Apply replacements as defined in `org-link-abbrev-alist."
  5466. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5467. (let* ((key (match-string 1 link))
  5468. (as (or (assoc key org-link-abbrev-alist-local)
  5469. (assoc key org-link-abbrev-alist)))
  5470. (tag (and (match-end 2) (match-string 3 link)))
  5471. rpl)
  5472. (if (not as)
  5473. link
  5474. (setq rpl (cdr as))
  5475. (cond
  5476. ((symbolp rpl) (funcall rpl tag))
  5477. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5478. ((string-match "%h" rpl)
  5479. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5480. (t (concat rpl tag)))))
  5481. link))
  5482. ;;; Storing and inserting links
  5483. (defvar org-insert-link-history nil
  5484. "Minibuffer history for links inserted with `org-insert-link'.")
  5485. (defvar org-stored-links nil
  5486. "Contains the links stored with `org-store-link'.")
  5487. (defvar org-store-link-plist nil
  5488. "Plist with info about the most recently link created with `org-store-link'.")
  5489. (defvar org-link-protocols nil
  5490. "Link protocols added to Org-mode using `org-add-link-type'.")
  5491. (defvar org-store-link-functions nil
  5492. "List of functions that are called to create and store a link.
  5493. Each function will be called in turn until one returns a non-nil
  5494. value. Each function should check if it is responsible for creating
  5495. this link (for example by looking at the major mode).
  5496. If not, it must exit and return nil.
  5497. If yes, it should return a non-nil value after a calling
  5498. `org-store-link-props' with a list of properties and values.
  5499. Special properties are:
  5500. :type The link prefix. like \"http\". This must be given.
  5501. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5502. This is obligatory as well.
  5503. :description Optional default description for the second pair
  5504. of brackets in an Org-mode link. The user can still change
  5505. this when inserting this link into an Org-mode buffer.
  5506. In addition to these, any additional properties can be specified
  5507. and then used in remember templates.")
  5508. (defun org-add-link-type (type &optional follow export)
  5509. "Add TYPE to the list of `org-link-types'.
  5510. Re-compute all regular expressions depending on `org-link-types'
  5511. FOLLOW and EXPORT are two functions.
  5512. FOLLOW should take the link path as the single argument and do whatever
  5513. is necessary to follow the link, for example find a file or display
  5514. a mail message.
  5515. EXPORT should format the link path for export to one of the export formats.
  5516. It should be a function accepting three arguments:
  5517. path the path of the link, the text after the prefix (like \"http:\")
  5518. desc the description of the link, if any, nil if there was no descripton
  5519. format the export format, a symbol like `html' or `latex'.
  5520. The function may use the FORMAT information to return different values
  5521. depending on the format. The return value will be put literally into
  5522. the exported file.
  5523. Org-mode has a built-in default for exporting links. If you are happy with
  5524. this default, there is no need to define an export function for the link
  5525. type. For a simple example of an export function, see `org-bbdb.el'."
  5526. (add-to-list 'org-link-types type t)
  5527. (org-make-link-regexps)
  5528. (if (assoc type org-link-protocols)
  5529. (setcdr (assoc type org-link-protocols) (list follow export))
  5530. (push (list type follow export) org-link-protocols)))
  5531. ;;;###autoload
  5532. (defun org-store-link (arg)
  5533. "\\<org-mode-map>Store an org-link to the current location.
  5534. This link is added to `org-stored-links' and can later be inserted
  5535. into an org-buffer with \\[org-insert-link].
  5536. For some link types, a prefix arg is interpreted:
  5537. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5538. For file links, arg negates `org-context-in-file-links'."
  5539. (interactive "P")
  5540. (org-load-modules-maybe)
  5541. (setq org-store-link-plist nil) ; reset
  5542. (let (link cpltxt desc description search txt)
  5543. (cond
  5544. ((run-hook-with-args-until-success 'org-store-link-functions)
  5545. (setq link (plist-get org-store-link-plist :link)
  5546. desc (or (plist-get org-store-link-plist :description) link)))
  5547. ((eq major-mode 'calendar-mode)
  5548. (let ((cd (calendar-cursor-to-date)))
  5549. (setq link
  5550. (format-time-string
  5551. (car org-time-stamp-formats)
  5552. (apply 'encode-time
  5553. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5554. nil nil nil))))
  5555. (org-store-link-props :type "calendar" :date cd)))
  5556. ((eq major-mode 'w3-mode)
  5557. (setq cpltxt (url-view-url t)
  5558. link (org-make-link cpltxt))
  5559. (org-store-link-props :type "w3" :url (url-view-url t)))
  5560. ((eq major-mode 'w3m-mode)
  5561. (setq cpltxt (or w3m-current-title w3m-current-url)
  5562. link (org-make-link w3m-current-url))
  5563. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5564. ((setq search (run-hook-with-args-until-success
  5565. 'org-create-file-search-functions))
  5566. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5567. "::" search))
  5568. (setq cpltxt (or description link)))
  5569. ((eq major-mode 'image-mode)
  5570. (setq cpltxt (concat "file:"
  5571. (abbreviate-file-name buffer-file-name))
  5572. link (org-make-link cpltxt))
  5573. (org-store-link-props :type "image" :file buffer-file-name))
  5574. ((eq major-mode 'dired-mode)
  5575. ;; link to the file in the current line
  5576. (setq cpltxt (concat "file:"
  5577. (abbreviate-file-name
  5578. (expand-file-name
  5579. (dired-get-filename nil t))))
  5580. link (org-make-link cpltxt)))
  5581. ((and buffer-file-name (org-mode-p))
  5582. ;; Just link to current headline
  5583. (setq cpltxt (concat "file:"
  5584. (abbreviate-file-name buffer-file-name)))
  5585. ;; Add a context search string
  5586. (when (org-xor org-context-in-file-links arg)
  5587. ;; Check if we are on a target
  5588. (if (org-in-regexp "<<\\(.*?\\)>>")
  5589. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5590. (setq txt (cond
  5591. ((org-on-heading-p) nil)
  5592. ((org-region-active-p)
  5593. (buffer-substring (region-beginning) (region-end)))
  5594. (t nil)))
  5595. (when (or (null txt) (string-match "\\S-" txt))
  5596. (setq cpltxt
  5597. (concat cpltxt "::"
  5598. (condition-case nil
  5599. (org-make-org-heading-search-string txt)
  5600. (error "")))
  5601. desc "NONE"))))
  5602. (if (string-match "::\\'" cpltxt)
  5603. (setq cpltxt (substring cpltxt 0 -2)))
  5604. (setq link (org-make-link cpltxt)))
  5605. ((buffer-file-name (buffer-base-buffer))
  5606. ;; Just link to this file here.
  5607. (setq cpltxt (concat "file:"
  5608. (abbreviate-file-name
  5609. (buffer-file-name (buffer-base-buffer)))))
  5610. ;; Add a context string
  5611. (when (org-xor org-context-in-file-links arg)
  5612. (setq txt (if (org-region-active-p)
  5613. (buffer-substring (region-beginning) (region-end))
  5614. (buffer-substring (point-at-bol) (point-at-eol))))
  5615. ;; Only use search option if there is some text.
  5616. (when (string-match "\\S-" txt)
  5617. (setq cpltxt
  5618. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5619. desc "NONE")))
  5620. (setq link (org-make-link cpltxt)))
  5621. ((interactive-p)
  5622. (error "Cannot link to a buffer which is not visiting a file"))
  5623. (t (setq link nil)))
  5624. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5625. (setq link (or link cpltxt)
  5626. desc (or desc cpltxt))
  5627. (if (equal desc "NONE") (setq desc nil))
  5628. (if (and (interactive-p) link)
  5629. (progn
  5630. (setq org-stored-links
  5631. (cons (list link desc) org-stored-links))
  5632. (message "Stored: %s" (or desc link)))
  5633. (and link (org-make-link-string link desc)))))
  5634. (defun org-store-link-props (&rest plist)
  5635. "Store link properties, extract names and addresses."
  5636. (let (x adr)
  5637. (when (setq x (plist-get plist :from))
  5638. (setq adr (mail-extract-address-components x))
  5639. (setq plist (plist-put plist :fromname (car adr)))
  5640. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5641. (when (setq x (plist-get plist :to))
  5642. (setq adr (mail-extract-address-components x))
  5643. (setq plist (plist-put plist :toname (car adr)))
  5644. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5645. (let ((from (plist-get plist :from))
  5646. (to (plist-get plist :to)))
  5647. (when (and from to org-from-is-user-regexp)
  5648. (setq plist
  5649. (plist-put plist :fromto
  5650. (if (string-match org-from-is-user-regexp from)
  5651. (concat "to %t")
  5652. (concat "from %f"))))))
  5653. (setq org-store-link-plist plist))
  5654. (defun org-add-link-props (&rest plist)
  5655. "Add these properties to the link property list."
  5656. (let (key value)
  5657. (while plist
  5658. (setq key (pop plist) value (pop plist))
  5659. (setq org-store-link-plist
  5660. (plist-put org-store-link-plist key value)))))
  5661. (defun org-email-link-description (&optional fmt)
  5662. "Return the description part of an email link.
  5663. This takes information from `org-store-link-plist' and formats it
  5664. according to FMT (default from `org-email-link-description-format')."
  5665. (setq fmt (or fmt org-email-link-description-format))
  5666. (let* ((p org-store-link-plist)
  5667. (to (plist-get p :toaddress))
  5668. (from (plist-get p :fromaddress))
  5669. (table
  5670. (list
  5671. (cons "%c" (plist-get p :fromto))
  5672. (cons "%F" (plist-get p :from))
  5673. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5674. (cons "%T" (plist-get p :to))
  5675. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5676. (cons "%s" (plist-get p :subject))
  5677. (cons "%m" (plist-get p :message-id)))))
  5678. (when (string-match "%c" fmt)
  5679. ;; Check if the user wrote this message
  5680. (if (and org-from-is-user-regexp from to
  5681. (save-match-data (string-match org-from-is-user-regexp from)))
  5682. (setq fmt (replace-match "to %t" t t fmt))
  5683. (setq fmt (replace-match "from %f" t t fmt))))
  5684. (org-replace-escapes fmt table)))
  5685. (defun org-make-org-heading-search-string (&optional string heading)
  5686. "Make search string for STRING or current headline."
  5687. (interactive)
  5688. (let ((s (or string (org-get-heading))))
  5689. (unless (and string (not heading))
  5690. ;; We are using a headline, clean up garbage in there.
  5691. (if (string-match org-todo-regexp s)
  5692. (setq s (replace-match "" t t s)))
  5693. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5694. (setq s (replace-match "" t t s)))
  5695. (setq s (org-trim s))
  5696. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5697. org-comment-string "\\)") s)
  5698. (setq s (replace-match "" t t s)))
  5699. (while (string-match org-ts-regexp s)
  5700. (setq s (replace-match "" t t s))))
  5701. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5702. (setq s (replace-match " " t t s)))
  5703. (or string (setq s (concat "*" s))) ; Add * for headlines
  5704. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5705. (defun org-make-link (&rest strings)
  5706. "Concatenate STRINGS."
  5707. (apply 'concat strings))
  5708. (defun org-make-link-string (link &optional description)
  5709. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5710. (unless (string-match "\\S-" link)
  5711. (error "Empty link"))
  5712. (when (stringp description)
  5713. ;; Remove brackets from the description, they are fatal.
  5714. (while (string-match "\\[" description)
  5715. (setq description (replace-match "{" t t description)))
  5716. (while (string-match "\\]" description)
  5717. (setq description (replace-match "}" t t description))))
  5718. (when (equal (org-link-escape link) description)
  5719. ;; No description needed, it is identical
  5720. (setq description nil))
  5721. (when (and (not description)
  5722. (not (equal link (org-link-escape link))))
  5723. (setq description (org-extract-attributes link)))
  5724. (concat "[[" (org-link-escape link) "]"
  5725. (if description (concat "[" description "]") "")
  5726. "]"))
  5727. (defconst org-link-escape-chars
  5728. '((?\ . "%20")
  5729. (?\[ . "%5B")
  5730. (?\] . "%5D")
  5731. (?\340 . "%E0") ; `a
  5732. (?\342 . "%E2") ; ^a
  5733. (?\347 . "%E7") ; ,c
  5734. (?\350 . "%E8") ; `e
  5735. (?\351 . "%E9") ; 'e
  5736. (?\352 . "%EA") ; ^e
  5737. (?\356 . "%EE") ; ^i
  5738. (?\364 . "%F4") ; ^o
  5739. (?\371 . "%F9") ; `u
  5740. (?\373 . "%FB") ; ^u
  5741. (?\; . "%3B")
  5742. (?? . "%3F")
  5743. (?= . "%3D")
  5744. (?+ . "%2B")
  5745. )
  5746. "Association list of escapes for some characters problematic in links.
  5747. This is the list that is used for internal purposes.")
  5748. (defconst org-link-escape-chars-browser
  5749. '((?\ . "%20")) ; 32 for the SPC char
  5750. "Association list of escapes for some characters problematic in links.
  5751. This is the list that is used before handing over to the browser.")
  5752. (defun org-link-escape (text &optional table)
  5753. "Escape characters in TEXT that are problematic for links."
  5754. (setq table (or table org-link-escape-chars))
  5755. (when text
  5756. (let ((re (mapconcat (lambda (x) (regexp-quote
  5757. (char-to-string (car x))))
  5758. table "\\|")))
  5759. (while (string-match re text)
  5760. (setq text
  5761. (replace-match
  5762. (cdr (assoc (string-to-char (match-string 0 text))
  5763. table))
  5764. t t text)))
  5765. text)))
  5766. (defun org-link-unescape (text &optional table)
  5767. "Reverse the action of `org-link-escape'."
  5768. (setq table (or table org-link-escape-chars))
  5769. (when text
  5770. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5771. table "\\|")))
  5772. (while (string-match re text)
  5773. (setq text
  5774. (replace-match
  5775. (char-to-string (car (rassoc (match-string 0 text) table)))
  5776. t t text)))
  5777. text)))
  5778. (defun org-xor (a b)
  5779. "Exclusive or."
  5780. (if a (not b) b))
  5781. (defun org-get-header (header)
  5782. "Find a header field in the current buffer."
  5783. (save-excursion
  5784. (goto-char (point-min))
  5785. (let ((case-fold-search t) s)
  5786. (cond
  5787. ((eq header 'from)
  5788. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5789. (setq s (match-string 1)))
  5790. (while (string-match "\"" s)
  5791. (setq s (replace-match "" t t s)))
  5792. (if (string-match "[<(].*" s)
  5793. (setq s (replace-match "" t t s))))
  5794. ((eq header 'message-id)
  5795. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5796. (setq s (match-string 1))))
  5797. ((eq header 'subject)
  5798. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5799. (setq s (match-string 1)))))
  5800. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5801. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5802. s)))
  5803. (defun org-fixup-message-id-for-http (s)
  5804. "Replace special characters in a message id, so it can be used in an http query."
  5805. (while (string-match "<" s)
  5806. (setq s (replace-match "%3C" t t s)))
  5807. (while (string-match ">" s)
  5808. (setq s (replace-match "%3E" t t s)))
  5809. (while (string-match "@" s)
  5810. (setq s (replace-match "%40" t t s)))
  5811. s)
  5812. ;;;###autoload
  5813. (defun org-insert-link-global ()
  5814. "Insert a link like Org-mode does.
  5815. This command can be called in any mode to insert a link in Org-mode syntax."
  5816. (interactive)
  5817. (org-load-modules-maybe)
  5818. (org-run-like-in-org-mode 'org-insert-link))
  5819. (defun org-insert-link (&optional complete-file link-location)
  5820. "Insert a link. At the prompt, enter the link.
  5821. Completion can be used to insert any of the link protocol prefixes like
  5822. http or ftp in use.
  5823. The history can be used to select a link previously stored with
  5824. `org-store-link'. When the empty string is entered (i.e. if you just
  5825. press RET at the prompt), the link defaults to the most recently
  5826. stored link. As SPC triggers completion in the minibuffer, you need to
  5827. use M-SPC or C-q SPC to force the insertion of a space character.
  5828. You will also be prompted for a description, and if one is given, it will
  5829. be displayed in the buffer instead of the link.
  5830. If there is already a link at point, this command will allow you to edit link
  5831. and description parts.
  5832. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5833. be selected using completion. The path to the file will be relative to the
  5834. current directory if the file is in the current directory or a subdirectory.
  5835. Otherwise, the link will be the absolute path as completed in the minibuffer
  5836. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5837. option `org-link-file-path-type'.
  5838. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5839. the current directory or below.
  5840. With three \\[universal-argument] prefixes, negate the meaning of
  5841. `org-keep-stored-link-after-insertion'.
  5842. If `org-make-link-description-function' is non-nil, this function will be
  5843. called with the link target, and the result will be the default
  5844. link description.
  5845. If the LINK-LOCATION parameter is non-nil, this value will be
  5846. used as the link location instead of reading one interactively."
  5847. (interactive "P")
  5848. (let* ((wcf (current-window-configuration))
  5849. (region (if (org-region-active-p)
  5850. (buffer-substring (region-beginning) (region-end))))
  5851. (remove (and region (list (region-beginning) (region-end))))
  5852. (desc region)
  5853. tmphist ; byte-compile incorrectly complains about this
  5854. (link link-location)
  5855. entry file)
  5856. (cond
  5857. (link-location) ; specified by arg, just use it.
  5858. ((org-in-regexp org-bracket-link-regexp 1)
  5859. ;; We do have a link at point, and we are going to edit it.
  5860. (setq remove (list (match-beginning 0) (match-end 0)))
  5861. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5862. (setq link (read-string "Link: "
  5863. (org-link-unescape
  5864. (org-match-string-no-properties 1)))))
  5865. ((or (org-in-regexp org-angle-link-re)
  5866. (org-in-regexp org-plain-link-re))
  5867. ;; Convert to bracket link
  5868. (setq remove (list (match-beginning 0) (match-end 0))
  5869. link (read-string "Link: "
  5870. (org-remove-angle-brackets (match-string 0)))))
  5871. ((member complete-file '((4) (16)))
  5872. ;; Completing read for file names.
  5873. (setq file (read-file-name "File: "))
  5874. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5875. (pwd1 (file-name-as-directory (abbreviate-file-name
  5876. (expand-file-name ".")))))
  5877. (cond
  5878. ((equal complete-file '(16))
  5879. (setq link (org-make-link
  5880. "file:"
  5881. (abbreviate-file-name (expand-file-name file)))))
  5882. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5883. (setq link (org-make-link "file:" (match-string 1 file))))
  5884. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5885. (expand-file-name file))
  5886. (setq link (org-make-link
  5887. "file:" (match-string 1 (expand-file-name file)))))
  5888. (t (setq link (org-make-link "file:" file))))))
  5889. (t
  5890. ;; Read link, with completion for stored links.
  5891. (with-output-to-temp-buffer "*Org Links*"
  5892. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5893. (when org-stored-links
  5894. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5895. (princ (mapconcat
  5896. (lambda (x)
  5897. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5898. (reverse org-stored-links) "\n"))))
  5899. (let ((cw (selected-window)))
  5900. (select-window (get-buffer-window "*Org Links*"))
  5901. (org-fit-window-to-buffer)
  5902. (setq truncate-lines t)
  5903. (select-window cw))
  5904. ;; Fake a link history, containing the stored links.
  5905. (setq tmphist (append (mapcar 'car org-stored-links)
  5906. org-insert-link-history))
  5907. (unwind-protect
  5908. (setq link (org-completing-read
  5909. "Link: "
  5910. (append
  5911. (mapcar (lambda (x) (list (concat (car x) ":")))
  5912. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5913. (mapcar (lambda (x) (list (concat x ":")))
  5914. org-link-types))
  5915. nil nil nil
  5916. 'tmphist
  5917. (or (car (car org-stored-links)))))
  5918. (set-window-configuration wcf)
  5919. (kill-buffer "*Org Links*"))
  5920. (setq entry (assoc link org-stored-links))
  5921. (or entry (push link org-insert-link-history))
  5922. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5923. (not org-keep-stored-link-after-insertion))
  5924. (setq org-stored-links (delq (assoc link org-stored-links)
  5925. org-stored-links)))
  5926. (setq desc (or desc (nth 1 entry)))))
  5927. (if (string-match org-plain-link-re link)
  5928. ;; URL-like link, normalize the use of angular brackets.
  5929. (setq link (org-make-link (org-remove-angle-brackets link))))
  5930. ;; Check if we are linking to the current file with a search option
  5931. ;; If yes, simplify the link by using only the search option.
  5932. (when (and buffer-file-name
  5933. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  5934. (let* ((path (match-string 1 link))
  5935. (case-fold-search nil)
  5936. (search (match-string 2 link)))
  5937. (save-match-data
  5938. (if (equal (file-truename buffer-file-name) (file-truename path))
  5939. ;; We are linking to this same file, with a search option
  5940. (setq link search)))))
  5941. ;; Check if we can/should use a relative path. If yes, simplify the link
  5942. (when (string-match "^file:\\(.*\\)" link)
  5943. (let* ((path (match-string 1 link))
  5944. (origpath path)
  5945. (case-fold-search nil))
  5946. (cond
  5947. ((or (eq org-link-file-path-type 'absolute)
  5948. (equal complete-file '(16)))
  5949. (setq path (abbreviate-file-name (expand-file-name path))))
  5950. ((eq org-link-file-path-type 'noabbrev)
  5951. (setq path (expand-file-name path)))
  5952. ((eq org-link-file-path-type 'relative)
  5953. (setq path (file-relative-name path)))
  5954. (t
  5955. (save-match-data
  5956. (if (string-match (concat "^" (regexp-quote
  5957. (file-name-as-directory
  5958. (expand-file-name "."))))
  5959. (expand-file-name path))
  5960. ;; We are linking a file with relative path name.
  5961. (setq path (substring (expand-file-name path)
  5962. (match-end 0)))
  5963. (setq path (abbreviate-file-name (expand-file-name path)))))))
  5964. (setq link (concat "file:" path))
  5965. (if (equal desc origpath)
  5966. (setq desc path))))
  5967. (if org-make-link-description-function
  5968. (setq desc (funcall org-make-link-description-function link desc)))
  5969. (setq desc (read-string "Description: " desc))
  5970. (unless (string-match "\\S-" desc) (setq desc nil))
  5971. (if remove (apply 'delete-region remove))
  5972. (insert (org-make-link-string link desc))))
  5973. (defun org-completing-read (&rest args)
  5974. "Completing-read with SPACE being a normal character."
  5975. (let ((minibuffer-local-completion-map
  5976. (copy-keymap minibuffer-local-completion-map)))
  5977. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5978. (apply 'org-ido-completing-read args)))
  5979. (defun org-ido-completing-read (&rest args)
  5980. "Completing-read using `ido-mode' speedups if available"
  5981. (if (and org-completion-use-ido
  5982. (fboundp 'ido-completing-read)
  5983. (boundp 'ido-mode) ido-mode
  5984. (listp (second args)))
  5985. (apply 'ido-completing-read (concat (car args)) (cdr args))
  5986. (apply 'completing-read args)))
  5987. (defun org-extract-attributes (s)
  5988. "Extract the attributes cookie from a string and set as text property."
  5989. (let (a attr (start 0) key value)
  5990. (save-match-data
  5991. (when (string-match "{{\\([^}]+\\)}}$" s)
  5992. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5993. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5994. (setq key (match-string 1 a) value (match-string 2 a)
  5995. start (match-end 0)
  5996. attr (plist-put attr (intern key) value))))
  5997. (org-add-props s nil 'org-attributes attr))
  5998. s))
  5999. (defun org-attributes-to-string (plist)
  6000. "Format a property list into an HTML attribute list."
  6001. (let ((s "") key value)
  6002. (while plist
  6003. (setq key (pop plist) value (pop plist))
  6004. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  6005. s))
  6006. ;;; Opening/following a link
  6007. (defvar org-link-search-failed nil)
  6008. (defun org-next-link ()
  6009. "Move forward to the next link.
  6010. If the link is in hidden text, expose it."
  6011. (interactive)
  6012. (when (and org-link-search-failed (eq this-command last-command))
  6013. (goto-char (point-min))
  6014. (message "Link search wrapped back to beginning of buffer"))
  6015. (setq org-link-search-failed nil)
  6016. (let* ((pos (point))
  6017. (ct (org-context))
  6018. (a (assoc :link ct)))
  6019. (if a (goto-char (nth 2 a)))
  6020. (if (re-search-forward org-any-link-re nil t)
  6021. (progn
  6022. (goto-char (match-beginning 0))
  6023. (if (org-invisible-p) (org-show-context)))
  6024. (goto-char pos)
  6025. (setq org-link-search-failed t)
  6026. (error "No further link found"))))
  6027. (defun org-previous-link ()
  6028. "Move backward to the previous link.
  6029. If the link is in hidden text, expose it."
  6030. (interactive)
  6031. (when (and org-link-search-failed (eq this-command last-command))
  6032. (goto-char (point-max))
  6033. (message "Link search wrapped back to end of buffer"))
  6034. (setq org-link-search-failed nil)
  6035. (let* ((pos (point))
  6036. (ct (org-context))
  6037. (a (assoc :link ct)))
  6038. (if a (goto-char (nth 1 a)))
  6039. (if (re-search-backward org-any-link-re nil t)
  6040. (progn
  6041. (goto-char (match-beginning 0))
  6042. (if (org-invisible-p) (org-show-context)))
  6043. (goto-char pos)
  6044. (setq org-link-search-failed t)
  6045. (error "No further link found"))))
  6046. (defun org-translate-link (s)
  6047. "Translate a link string if a translation function has been defined."
  6048. (if (and org-link-translation-function
  6049. (fboundp org-link-translation-function)
  6050. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6051. (progn
  6052. (setq s (funcall org-link-translation-function
  6053. (match-string 1) (match-string 2)))
  6054. (concat (car s) ":" (cdr s)))
  6055. s))
  6056. (defun org-translate-link-from-planner (type path)
  6057. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6058. This is still an experimental function, your mileage may vary."
  6059. (cond
  6060. ((member type '("http" "https" "news" "ftp"))
  6061. ;; standard Internet links are the same.
  6062. nil)
  6063. ((and (equal type "irc") (string-match "^//" path))
  6064. ;; Planner has two / at the beginning of an irc link, we have 1.
  6065. ;; We should have zero, actually....
  6066. (setq path (substring path 1)))
  6067. ((and (equal type "lisp") (string-match "^/" path))
  6068. ;; Planner has a slash, we do not.
  6069. (setq type "elisp" path (substring path 1)))
  6070. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6071. ;; A typical message link. Planner has the id after the fina slash,
  6072. ;; we separate it with a hash mark
  6073. (setq path (concat (match-string 1 path) "#"
  6074. (org-remove-angle-brackets (match-string 2 path)))))
  6075. )
  6076. (cons type path))
  6077. (defun org-find-file-at-mouse (ev)
  6078. "Open file link or URL at mouse."
  6079. (interactive "e")
  6080. (mouse-set-point ev)
  6081. (org-open-at-point 'in-emacs))
  6082. (defun org-open-at-mouse (ev)
  6083. "Open file link or URL at mouse."
  6084. (interactive "e")
  6085. (mouse-set-point ev)
  6086. (if (eq major-mode 'org-agenda-mode)
  6087. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6088. (org-open-at-point))
  6089. (defvar org-window-config-before-follow-link nil
  6090. "The window configuration before following a link.
  6091. This is saved in case the need arises to restore it.")
  6092. (defvar org-open-link-marker (make-marker)
  6093. "Marker pointing to the location where `org-open-at-point; was called.")
  6094. ;;;###autoload
  6095. (defun org-open-at-point-global ()
  6096. "Follow a link like Org-mode does.
  6097. This command can be called in any mode to follow a link that has
  6098. Org-mode syntax."
  6099. (interactive)
  6100. (org-run-like-in-org-mode 'org-open-at-point))
  6101. ;;;###autoload
  6102. (defun org-open-link-from-string (s &optional arg)
  6103. "Open a link in the string S, as if it was in Org-mode."
  6104. (interactive "sLink: \nP")
  6105. (with-temp-buffer
  6106. (let ((org-inhibit-startup t))
  6107. (org-mode)
  6108. (insert s)
  6109. (goto-char (point-min))
  6110. (org-open-at-point arg))))
  6111. (defun org-open-at-point (&optional in-emacs)
  6112. "Open link at or after point.
  6113. If there is no link at point, this function will search forward up to
  6114. the end of the current subtree.
  6115. Normally, files will be opened by an appropriate application. If the
  6116. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6117. With a double prefix argument, try to open outside of Emacs, in the
  6118. application the system uses for this file type."
  6119. (interactive "P")
  6120. (org-load-modules-maybe)
  6121. (move-marker org-open-link-marker (point))
  6122. (setq org-window-config-before-follow-link (current-window-configuration))
  6123. (org-remove-occur-highlights nil nil t)
  6124. (if (org-at-timestamp-p t)
  6125. (org-follow-timestamp-link)
  6126. (let (type path link line search (pos (point)))
  6127. (catch 'match
  6128. (save-excursion
  6129. (skip-chars-forward "^]\n\r")
  6130. (when (org-in-regexp org-bracket-link-regexp)
  6131. (setq link (org-extract-attributes
  6132. (org-link-unescape (org-match-string-no-properties 1))))
  6133. (while (string-match " *\n *" link)
  6134. (setq link (replace-match " " t t link)))
  6135. (setq link (org-link-expand-abbrev link))
  6136. (cond
  6137. ((or (file-name-absolute-p link)
  6138. (string-match "^\\.\\.?/" link))
  6139. (setq type "file" path link))
  6140. ((string-match org-link-re-with-space3 link)
  6141. (setq type (match-string 1 link) path (match-string 2 link)))
  6142. (t (setq type "thisfile" path link)))
  6143. (throw 'match t)))
  6144. (when (get-text-property (point) 'org-linked-text)
  6145. (setq type "thisfile"
  6146. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6147. (1+ (point)) (point))
  6148. path (buffer-substring
  6149. (previous-single-property-change pos 'org-linked-text)
  6150. (next-single-property-change pos 'org-linked-text)))
  6151. (throw 'match t))
  6152. (save-excursion
  6153. (when (or (org-in-regexp org-angle-link-re)
  6154. (org-in-regexp org-plain-link-re))
  6155. (setq type (match-string 1) path (match-string 2))
  6156. (throw 'match t)))
  6157. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6158. (setq type "tree-match"
  6159. path (match-string 1))
  6160. (throw 'match t))
  6161. (save-excursion
  6162. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6163. (setq type "tags"
  6164. path (match-string 1))
  6165. (while (string-match ":" path)
  6166. (setq path (replace-match "+" t t path)))
  6167. (throw 'match t))))
  6168. (unless path
  6169. (error "No link found"))
  6170. ;; Remove any trailing spaces in path
  6171. (if (string-match " +\\'" path)
  6172. (setq path (replace-match "" t t path)))
  6173. (if (and org-link-translation-function
  6174. (fboundp org-link-translation-function))
  6175. ;; Check if we need to translate the link
  6176. (let ((tmp (funcall org-link-translation-function type path)))
  6177. (setq type (car tmp) path (cdr tmp))))
  6178. (cond
  6179. ((assoc type org-link-protocols)
  6180. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6181. ((equal type "mailto")
  6182. (let ((cmd (car org-link-mailto-program))
  6183. (args (cdr org-link-mailto-program)) args1
  6184. (address path) (subject "") a)
  6185. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6186. (setq address (match-string 1 path)
  6187. subject (org-link-escape (match-string 2 path))))
  6188. (while args
  6189. (cond
  6190. ((not (stringp (car args))) (push (pop args) args1))
  6191. (t (setq a (pop args))
  6192. (if (string-match "%a" a)
  6193. (setq a (replace-match address t t a)))
  6194. (if (string-match "%s" a)
  6195. (setq a (replace-match subject t t a)))
  6196. (push a args1))))
  6197. (apply cmd (nreverse args1))))
  6198. ((member type '("http" "https" "ftp" "news"))
  6199. (browse-url (concat type ":" (org-link-escape
  6200. path org-link-escape-chars-browser))))
  6201. ((member type '("message"))
  6202. (browse-url (concat type ":" path)))
  6203. ((string= type "tags")
  6204. (org-tags-view in-emacs path))
  6205. ((string= type "thisfile")
  6206. (if in-emacs
  6207. (switch-to-buffer-other-window
  6208. (org-get-buffer-for-internal-link (current-buffer)))
  6209. (org-mark-ring-push))
  6210. (let ((cmd `(org-link-search
  6211. ,path
  6212. ,(cond ((equal in-emacs '(4)) 'occur)
  6213. ((equal in-emacs '(16)) 'org-occur)
  6214. (t nil))
  6215. ,pos)))
  6216. (condition-case nil (eval cmd)
  6217. (error (progn (widen) (eval cmd))))))
  6218. ((string= type "tree-match")
  6219. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6220. ((string= type "file")
  6221. (if (string-match "::\\([0-9]+\\)\\'" path)
  6222. (setq line (string-to-number (match-string 1 path))
  6223. path (substring path 0 (match-beginning 0)))
  6224. (if (string-match "::\\(.+\\)\\'" path)
  6225. (setq search (match-string 1 path)
  6226. path (substring path 0 (match-beginning 0)))))
  6227. (if (string-match "[*?{]" (file-name-nondirectory path))
  6228. (dired path)
  6229. (org-open-file path in-emacs line search)))
  6230. ((string= type "news")
  6231. (require 'org-gnus)
  6232. (org-gnus-follow-link path))
  6233. ((string= type "shell")
  6234. (let ((cmd path))
  6235. (if (or (not org-confirm-shell-link-function)
  6236. (funcall org-confirm-shell-link-function
  6237. (format "Execute \"%s\" in shell? "
  6238. (org-add-props cmd nil
  6239. 'face 'org-warning))))
  6240. (progn
  6241. (message "Executing %s" cmd)
  6242. (shell-command cmd))
  6243. (error "Abort"))))
  6244. ((string= type "elisp")
  6245. (let ((cmd path))
  6246. (if (or (not org-confirm-elisp-link-function)
  6247. (funcall org-confirm-elisp-link-function
  6248. (format "Execute \"%s\" as elisp? "
  6249. (org-add-props cmd nil
  6250. 'face 'org-warning))))
  6251. (message "%s => %s" cmd
  6252. (if (equal (string-to-char cmd) ?\()
  6253. (eval (read cmd))
  6254. (call-interactively (read cmd))))
  6255. (error "Abort"))))
  6256. (t
  6257. (browse-url-at-point)))))
  6258. (move-marker org-open-link-marker nil)
  6259. (run-hook-with-args 'org-follow-link-hook))
  6260. ;;;; Time estimates
  6261. (defun org-get-effort (&optional pom)
  6262. "Get the effort estimate for the current entry."
  6263. (org-entry-get pom org-effort-property))
  6264. ;;; File search
  6265. (defvar org-create-file-search-functions nil
  6266. "List of functions to construct the right search string for a file link.
  6267. These functions are called in turn with point at the location to
  6268. which the link should point.
  6269. A function in the hook should first test if it would like to
  6270. handle this file type, for example by checking the major-mode or
  6271. the file extension. If it decides not to handle this file, it
  6272. should just return nil to give other functions a chance. If it
  6273. does handle the file, it must return the search string to be used
  6274. when following the link. The search string will be part of the
  6275. file link, given after a double colon, and `org-open-at-point'
  6276. will automatically search for it. If special measures must be
  6277. taken to make the search successful, another function should be
  6278. added to the companion hook `org-execute-file-search-functions',
  6279. which see.
  6280. A function in this hook may also use `setq' to set the variable
  6281. `description' to provide a suggestion for the descriptive text to
  6282. be used for this link when it gets inserted into an Org-mode
  6283. buffer with \\[org-insert-link].")
  6284. (defvar org-execute-file-search-functions nil
  6285. "List of functions to execute a file search triggered by a link.
  6286. Functions added to this hook must accept a single argument, the
  6287. search string that was part of the file link, the part after the
  6288. double colon. The function must first check if it would like to
  6289. handle this search, for example by checking the major-mode or the
  6290. file extension. If it decides not to handle this search, it
  6291. should just return nil to give other functions a chance. If it
  6292. does handle the search, it must return a non-nil value to keep
  6293. other functions from trying.
  6294. Each function can access the current prefix argument through the
  6295. variable `current-prefix-argument'. Note that a single prefix is
  6296. used to force opening a link in Emacs, so it may be good to only
  6297. use a numeric or double prefix to guide the search function.
  6298. In case this is needed, a function in this hook can also restore
  6299. the window configuration before `org-open-at-point' was called using:
  6300. (set-window-configuration org-window-config-before-follow-link)")
  6301. (defun org-link-search (s &optional type avoid-pos)
  6302. "Search for a link search option.
  6303. If S is surrounded by forward slashes, it is interpreted as a
  6304. regular expression. In org-mode files, this will create an `org-occur'
  6305. sparse tree. In ordinary files, `occur' will be used to list matches.
  6306. If the current buffer is in `dired-mode', grep will be used to search
  6307. in all files. If AVOID-POS is given, ignore matches near that position."
  6308. (let ((case-fold-search t)
  6309. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6310. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6311. (append '(("") (" ") ("\t") ("\n"))
  6312. org-emphasis-alist)
  6313. "\\|") "\\)"))
  6314. (pos (point))
  6315. (pre nil) (post nil)
  6316. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6317. (cond
  6318. ;; First check if there are any special
  6319. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6320. ;; Now try the builtin stuff
  6321. ((save-excursion
  6322. (goto-char (point-min))
  6323. (and
  6324. (re-search-forward
  6325. (concat "<<" (regexp-quote s0) ">>") nil t)
  6326. (setq type 'dedicated
  6327. pos (match-beginning 0))))
  6328. ;; There is an exact target for this
  6329. (goto-char pos))
  6330. ((string-match "^/\\(.*\\)/$" s)
  6331. ;; A regular expression
  6332. (cond
  6333. ((org-mode-p)
  6334. (org-occur (match-string 1 s)))
  6335. ;;((eq major-mode 'dired-mode)
  6336. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6337. (t (org-do-occur (match-string 1 s)))))
  6338. (t
  6339. ;; A normal search strings
  6340. (when (equal (string-to-char s) ?*)
  6341. ;; Anchor on headlines, post may include tags.
  6342. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6343. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6344. s (substring s 1)))
  6345. (remove-text-properties
  6346. 0 (length s)
  6347. '(face nil mouse-face nil keymap nil fontified nil) s)
  6348. ;; Make a series of regular expressions to find a match
  6349. (setq words (org-split-string s "[ \n\r\t]+")
  6350. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6351. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6352. "\\)" markers)
  6353. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6354. re2a (concat "[ \t\r\n]" re2a_)
  6355. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6356. re4 (concat "[^a-zA-Z_]" re4_)
  6357. re1 (concat pre re2 post)
  6358. re3 (concat pre (if pre re4_ re4) post)
  6359. re5 (concat pre ".*" re4)
  6360. re2 (concat pre re2)
  6361. re2a (concat pre (if pre re2a_ re2a))
  6362. re4 (concat pre (if pre re4_ re4))
  6363. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6364. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6365. re5 "\\)"
  6366. ))
  6367. (cond
  6368. ((eq type 'org-occur) (org-occur reall))
  6369. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6370. (t (goto-char (point-min))
  6371. (setq type 'fuzzy)
  6372. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6373. (org-search-not-self 1 re1 nil t)
  6374. (org-search-not-self 1 re2 nil t)
  6375. (org-search-not-self 1 re2a nil t)
  6376. (org-search-not-self 1 re3 nil t)
  6377. (org-search-not-self 1 re4 nil t)
  6378. (org-search-not-self 1 re5 nil t)
  6379. )
  6380. (goto-char (match-beginning 1))
  6381. (goto-char pos)
  6382. (error "No match")))))
  6383. (t
  6384. ;; Normal string-search
  6385. (goto-char (point-min))
  6386. (if (search-forward s nil t)
  6387. (goto-char (match-beginning 0))
  6388. (error "No match"))))
  6389. (and (org-mode-p) (org-show-context 'link-search))
  6390. type))
  6391. (defun org-search-not-self (group &rest args)
  6392. "Execute `re-search-forward', but only accept matches that do not
  6393. enclose the position of `org-open-link-marker'."
  6394. (let ((m org-open-link-marker))
  6395. (catch 'exit
  6396. (while (apply 're-search-forward args)
  6397. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6398. (goto-char (match-end group))
  6399. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6400. (> (match-beginning 0) (marker-position m))
  6401. (< (match-end 0) (marker-position m)))
  6402. (save-match-data
  6403. (or (not (org-in-regexp
  6404. org-bracket-link-analytic-regexp 1))
  6405. (not (match-end 4)) ; no description
  6406. (and (<= (match-beginning 4) (point))
  6407. (>= (match-end 4) (point))))))
  6408. (throw 'exit (point))))))))
  6409. (defun org-get-buffer-for-internal-link (buffer)
  6410. "Return a buffer to be used for displaying the link target of internal links."
  6411. (cond
  6412. ((not org-display-internal-link-with-indirect-buffer)
  6413. buffer)
  6414. ((string-match "(Clone)$" (buffer-name buffer))
  6415. (message "Buffer is already a clone, not making another one")
  6416. ;; we also do not modify visibility in this case
  6417. buffer)
  6418. (t ; make a new indirect buffer for displaying the link
  6419. (let* ((bn (buffer-name buffer))
  6420. (ibn (concat bn "(Clone)"))
  6421. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6422. (with-current-buffer ib (org-overview))
  6423. ib))))
  6424. (defun org-do-occur (regexp &optional cleanup)
  6425. "Call the Emacs command `occur'.
  6426. If CLEANUP is non-nil, remove the printout of the regular expression
  6427. in the *Occur* buffer. This is useful if the regex is long and not useful
  6428. to read."
  6429. (occur regexp)
  6430. (when cleanup
  6431. (let ((cwin (selected-window)) win beg end)
  6432. (when (setq win (get-buffer-window "*Occur*"))
  6433. (select-window win))
  6434. (goto-char (point-min))
  6435. (when (re-search-forward "match[a-z]+" nil t)
  6436. (setq beg (match-end 0))
  6437. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6438. (setq end (1- (match-beginning 0)))))
  6439. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6440. (goto-char (point-min))
  6441. (select-window cwin))))
  6442. ;;; The mark ring for links jumps
  6443. (defvar org-mark-ring nil
  6444. "Mark ring for positions before jumps in Org-mode.")
  6445. (defvar org-mark-ring-last-goto nil
  6446. "Last position in the mark ring used to go back.")
  6447. ;; Fill and close the ring
  6448. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6449. (loop for i from 1 to org-mark-ring-length do
  6450. (push (make-marker) org-mark-ring))
  6451. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6452. org-mark-ring)
  6453. (defun org-mark-ring-push (&optional pos buffer)
  6454. "Put the current position or POS into the mark ring and rotate it."
  6455. (interactive)
  6456. (setq pos (or pos (point)))
  6457. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6458. (move-marker (car org-mark-ring)
  6459. (or pos (point))
  6460. (or buffer (current-buffer)))
  6461. (message "%s"
  6462. (substitute-command-keys
  6463. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6464. (defun org-mark-ring-goto (&optional n)
  6465. "Jump to the previous position in the mark ring.
  6466. With prefix arg N, jump back that many stored positions. When
  6467. called several times in succession, walk through the entire ring.
  6468. Org-mode commands jumping to a different position in the current file,
  6469. or to another Org-mode file, automatically push the old position
  6470. onto the ring."
  6471. (interactive "p")
  6472. (let (p m)
  6473. (if (eq last-command this-command)
  6474. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6475. (setq p org-mark-ring))
  6476. (setq org-mark-ring-last-goto p)
  6477. (setq m (car p))
  6478. (switch-to-buffer (marker-buffer m))
  6479. (goto-char m)
  6480. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6481. (defun org-remove-angle-brackets (s)
  6482. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6483. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6484. s)
  6485. (defun org-add-angle-brackets (s)
  6486. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6487. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6488. s)
  6489. (defun org-remove-double-quotes (s)
  6490. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6491. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6492. s)
  6493. ;;; Following specific links
  6494. (defun org-follow-timestamp-link ()
  6495. (cond
  6496. ((org-at-date-range-p t)
  6497. (let ((org-agenda-start-on-weekday)
  6498. (t1 (match-string 1))
  6499. (t2 (match-string 2)))
  6500. (setq t1 (time-to-days (org-time-string-to-time t1))
  6501. t2 (time-to-days (org-time-string-to-time t2)))
  6502. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6503. ((org-at-timestamp-p t)
  6504. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6505. (substring (match-string 1) 0 10)))
  6506. 1))
  6507. (t (error "This should not happen"))))
  6508. ;;; Following file links
  6509. (defvar org-wait nil)
  6510. (defun org-open-file (path &optional in-emacs line search)
  6511. "Open the file at PATH.
  6512. First, this expands any special file name abbreviations. Then the
  6513. configuration variable `org-file-apps' is checked if it contains an
  6514. entry for this file type, and if yes, the corresponding command is launched.
  6515. If no application is found, Emacs simply visits the file.
  6516. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6517. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6518. and o use an external application to visit the file.
  6519. Optional LINE specifies a line to go to, optional SEARCH a string to
  6520. search for. If LINE or SEARCH is given, the file will always be
  6521. opened in Emacs.
  6522. If the file does not exist, an error is thrown."
  6523. (setq in-emacs (or in-emacs line search))
  6524. (let* ((file (if (equal path "")
  6525. buffer-file-name
  6526. (substitute-in-file-name (expand-file-name path))))
  6527. (apps (append org-file-apps (org-default-apps)))
  6528. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6529. (dirp (if remp nil (file-directory-p file)))
  6530. (file (if (and dirp org-open-directory-means-index-dot-org)
  6531. (concat (file-name-as-directory file) "index.org")
  6532. file))
  6533. (a-m-a-p (assq 'auto-mode apps))
  6534. (dfile (downcase file))
  6535. (old-buffer (current-buffer))
  6536. (old-pos (point))
  6537. (old-mode major-mode)
  6538. ext cmd)
  6539. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6540. (setq ext (match-string 1 dfile))
  6541. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6542. (setq ext (match-string 1 dfile))))
  6543. (cond
  6544. ((equal in-emacs '(16))
  6545. (setq cmd (cdr (assoc 'system apps))))
  6546. (in-emacs (setq cmd 'emacs))
  6547. (t
  6548. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6549. (and dirp (cdr (assoc 'directory apps)))
  6550. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6551. 'string-match)
  6552. (cdr (assoc ext apps))
  6553. (cdr (assoc t apps))))))
  6554. (when (eq cmd 'system)
  6555. (setq cmd (cdr (assoc 'system apps))))
  6556. (when (eq cmd 'default)
  6557. (setq cmd (cdr (assoc t apps))))
  6558. (when (eq cmd 'mailcap)
  6559. (require 'mailcap)
  6560. (mailcap-parse-mailcaps)
  6561. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6562. (command (mailcap-mime-info mime-type)))
  6563. (if (stringp command)
  6564. (setq cmd command)
  6565. (setq cmd 'emacs))))
  6566. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6567. (not (file-exists-p file))
  6568. (not org-open-non-existing-files))
  6569. (error "No such file: %s" file))
  6570. (cond
  6571. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6572. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6573. (while (string-match "['\"]%s['\"]" cmd)
  6574. (setq cmd (replace-match "%s" t t cmd)))
  6575. (while (string-match "%s" cmd)
  6576. (setq cmd (replace-match
  6577. (save-match-data
  6578. (shell-quote-argument
  6579. (convert-standard-filename file)))
  6580. t t cmd)))
  6581. (save-window-excursion
  6582. (start-process-shell-command cmd nil cmd)
  6583. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6584. ))
  6585. ((or (stringp cmd)
  6586. (eq cmd 'emacs))
  6587. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6588. (widen)
  6589. (if line (goto-line line)
  6590. (if search (org-link-search search))))
  6591. ((consp cmd)
  6592. (let ((file (convert-standard-filename file)))
  6593. (eval cmd)))
  6594. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6595. (and (org-mode-p) (eq old-mode 'org-mode)
  6596. (or (not (equal old-buffer (current-buffer)))
  6597. (not (equal old-pos (point))))
  6598. (org-mark-ring-push old-pos old-buffer))))
  6599. (defun org-default-apps ()
  6600. "Return the default applications for this operating system."
  6601. (cond
  6602. ((eq system-type 'darwin)
  6603. org-file-apps-defaults-macosx)
  6604. ((eq system-type 'windows-nt)
  6605. org-file-apps-defaults-windowsnt)
  6606. (t org-file-apps-defaults-gnu)))
  6607. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6608. "Convert extensions to regular expressions in the cars of LIST.
  6609. Also, weed out any non-string entries, because the return value is used
  6610. only for regexp matching.
  6611. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6612. point to the symbol `emacs', indicating that the file should
  6613. be opened in Emacs."
  6614. (append
  6615. (delq nil
  6616. (mapcar (lambda (x)
  6617. (if (not (stringp (car x)))
  6618. nil
  6619. (if (string-match "\\W" (car x))
  6620. x
  6621. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6622. list))
  6623. (if add-auto-mode
  6624. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6625. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6626. (defun org-file-remote-p (file)
  6627. "Test whether FILE specifies a location on a remote system.
  6628. Return non-nil if the location is indeed remote.
  6629. For example, the filename \"/user@host:/foo\" specifies a location
  6630. on the system \"/user@host:\"."
  6631. (cond ((fboundp 'file-remote-p)
  6632. (file-remote-p file))
  6633. ((fboundp 'tramp-handle-file-remote-p)
  6634. (tramp-handle-file-remote-p file))
  6635. ((and (boundp 'ange-ftp-name-format)
  6636. (string-match (car ange-ftp-name-format) file))
  6637. t)
  6638. (t nil)))
  6639. ;;;; Refiling
  6640. (defun org-get-org-file ()
  6641. "Read a filename, with default directory `org-directory'."
  6642. (let ((default (or org-default-notes-file remember-data-file)))
  6643. (read-file-name (format "File name [%s]: " default)
  6644. (file-name-as-directory org-directory)
  6645. default)))
  6646. (defun org-notes-order-reversed-p ()
  6647. "Check if the current file should receive notes in reversed order."
  6648. (cond
  6649. ((not org-reverse-note-order) nil)
  6650. ((eq t org-reverse-note-order) t)
  6651. ((not (listp org-reverse-note-order)) nil)
  6652. (t (catch 'exit
  6653. (let ((all org-reverse-note-order)
  6654. entry)
  6655. (while (setq entry (pop all))
  6656. (if (string-match (car entry) buffer-file-name)
  6657. (throw 'exit (cdr entry))))
  6658. nil)))))
  6659. (defvar org-refile-target-table nil
  6660. "The list of refile targets, created by `org-refile'.")
  6661. (defvar org-agenda-new-buffers nil
  6662. "Buffers created to visit agenda files.")
  6663. (defun org-get-refile-targets (&optional default-buffer)
  6664. "Produce a table with refile targets."
  6665. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6666. targets txt re files f desc descre)
  6667. (with-current-buffer (or default-buffer (current-buffer))
  6668. (while (setq entry (pop entries))
  6669. (setq files (car entry) desc (cdr entry))
  6670. (cond
  6671. ((null files) (setq files (list (current-buffer))))
  6672. ((eq files 'org-agenda-files)
  6673. (setq files (org-agenda-files 'unrestricted)))
  6674. ((and (symbolp files) (fboundp files))
  6675. (setq files (funcall files)))
  6676. ((and (symbolp files) (boundp files))
  6677. (setq files (symbol-value files))))
  6678. (if (stringp files) (setq files (list files)))
  6679. (cond
  6680. ((eq (car desc) :tag)
  6681. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6682. ((eq (car desc) :todo)
  6683. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6684. ((eq (car desc) :regexp)
  6685. (setq descre (cdr desc)))
  6686. ((eq (car desc) :level)
  6687. (setq descre (concat "^\\*\\{" (number-to-string
  6688. (if org-odd-levels-only
  6689. (1- (* 2 (cdr desc)))
  6690. (cdr desc)))
  6691. "\\}[ \t]")))
  6692. ((eq (car desc) :maxlevel)
  6693. (setq descre (concat "^\\*\\{1," (number-to-string
  6694. (if org-odd-levels-only
  6695. (1- (* 2 (cdr desc)))
  6696. (cdr desc)))
  6697. "\\}[ \t]")))
  6698. (t (error "Bad refiling target description %s" desc)))
  6699. (while (setq f (pop files))
  6700. (save-excursion
  6701. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6702. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6703. (save-excursion
  6704. (save-restriction
  6705. (widen)
  6706. (goto-char (point-min))
  6707. (while (re-search-forward descre nil t)
  6708. (goto-char (point-at-bol))
  6709. (when (looking-at org-complex-heading-regexp)
  6710. (setq txt (org-link-display-format (match-string 4))
  6711. re (concat "^" (regexp-quote
  6712. (buffer-substring (match-beginning 1)
  6713. (match-end 4)))))
  6714. (if (match-end 5) (setq re (concat re "[ \t]+"
  6715. (regexp-quote
  6716. (match-string 5)))))
  6717. (setq re (concat re "[ \t]*$"))
  6718. (when org-refile-use-outline-path
  6719. (setq txt (mapconcat 'org-protect-slash
  6720. (append
  6721. (if (eq org-refile-use-outline-path 'file)
  6722. (list (file-name-nondirectory
  6723. (buffer-file-name (buffer-base-buffer))))
  6724. (if (eq org-refile-use-outline-path 'full-file-path)
  6725. (list (buffer-file-name (buffer-base-buffer)))))
  6726. (org-get-outline-path)
  6727. (list txt))
  6728. "/")))
  6729. (push (list txt f re (point)) targets))
  6730. (goto-char (point-at-eol))))))))
  6731. (nreverse targets))))
  6732. (defun org-protect-slash (s)
  6733. (while (string-match "/" s)
  6734. (setq s (replace-match "\\" t t s)))
  6735. s)
  6736. (defun org-get-outline-path ()
  6737. "Return the outline path to the current entry, as a list."
  6738. (let (rtn)
  6739. (save-excursion
  6740. (while (org-up-heading-safe)
  6741. (when (looking-at org-complex-heading-regexp)
  6742. (push (org-match-string-no-properties 4) rtn)))
  6743. rtn)))
  6744. (defvar org-refile-history nil
  6745. "History for refiling operations.")
  6746. (defun org-refile (&optional goto default-buffer)
  6747. "Move the entry at point to another heading.
  6748. The list of target headings is compiled using the information in
  6749. `org-refile-targets', which see. This list is created before each use
  6750. and will therefore always be up-to-date.
  6751. At the target location, the entry is filed as a subitem of the target heading.
  6752. Depending on `org-reverse-note-order', the new subitem will either be the
  6753. first or the last subitem.
  6754. If there is an active region, all entries in that region will be moved.
  6755. However, the region must fulfil the requirement that the first heading
  6756. is the first one sets the top-level of the moved text - at most siblings
  6757. below it are allowed.
  6758. With prefix arg GOTO, the command will only visit the target location,
  6759. not actually move anything.
  6760. With a double prefix `C-u C-u', go to the location where the last refiling
  6761. operation has put the subtree."
  6762. (interactive "P")
  6763. (let* ((cbuf (current-buffer))
  6764. (regionp (org-region-active-p))
  6765. (region-start (and regionp (region-beginning)))
  6766. (region-end (and regionp (region-end)))
  6767. (region-length (and regionp (- region-end region-start)))
  6768. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6769. pos it nbuf file re level reversed)
  6770. (when regionp (goto-char region-start)
  6771. (unless (org-kill-is-subtree-p
  6772. (buffer-substring region-start region-end))
  6773. (error "The region is not a (sequence of) subtree(s)")))
  6774. (if (equal goto '(16))
  6775. (org-refile-goto-last-stored)
  6776. (when (setq it (org-refile-get-location
  6777. (if goto "Goto: " "Refile to: ") default-buffer))
  6778. (setq file (nth 1 it)
  6779. re (nth 2 it)
  6780. pos (nth 3 it))
  6781. (setq nbuf (or (find-buffer-visiting file)
  6782. (find-file-noselect file)))
  6783. (if goto
  6784. (progn
  6785. (switch-to-buffer nbuf)
  6786. (goto-char pos)
  6787. (org-show-context 'org-goto))
  6788. (if regionp
  6789. (progn
  6790. (kill-new (buffer-substring region-start region-end))
  6791. (org-save-markers-in-region region-start region-end))
  6792. (org-copy-subtree 1 nil t))
  6793. (save-excursion
  6794. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6795. (find-file-noselect file))))
  6796. (setq reversed (org-notes-order-reversed-p))
  6797. (save-excursion
  6798. (save-restriction
  6799. (widen)
  6800. (goto-char pos)
  6801. (looking-at outline-regexp)
  6802. (setq level (org-get-valid-level (funcall outline-level) 1))
  6803. (goto-char
  6804. (if reversed
  6805. (or (outline-next-heading) (point-max))
  6806. (or (save-excursion (outline-get-next-sibling))
  6807. (org-end-of-subtree t t)
  6808. (point-max))))
  6809. (if (not (bolp)) (newline))
  6810. (bookmark-set "org-refile-last-stored")
  6811. (org-paste-subtree level))))
  6812. (if regionp
  6813. (delete-region (point) (+ (point) region-length))
  6814. (org-cut-subtree))
  6815. (setq org-markers-to-move nil)
  6816. (message "Refiled to \"%s\"" (car it)))))))
  6817. (defun org-refile-goto-last-stored ()
  6818. "Go to the location where the last refile was stored."
  6819. (interactive)
  6820. (bookmark-jump "org-refile-last-stored")
  6821. (message "This is the location of the last refile"))
  6822. (defun org-refile-get-location (&optional prompt default-buffer)
  6823. "Prompt the user for a refile location, using PROMPT."
  6824. (let ((org-refile-targets org-refile-targets)
  6825. (org-refile-use-outline-path org-refile-use-outline-path))
  6826. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6827. (unless org-refile-target-table
  6828. (error "No refile targets"))
  6829. (let* ((cbuf (current-buffer))
  6830. (cfunc (if (and org-refile-use-outline-path
  6831. org-outline-path-complete-in-steps)
  6832. 'org-olpath-completing-read
  6833. 'org-ido-completing-read))
  6834. (extra (if org-refile-use-outline-path "/" ""))
  6835. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6836. (fname (and filename (file-truename filename)))
  6837. (tbl (mapcar
  6838. (lambda (x)
  6839. (if (not (equal fname (file-truename (nth 1 x))))
  6840. (cons (concat (car x) extra " ("
  6841. (file-name-nondirectory (nth 1 x)) ")")
  6842. (cdr x))
  6843. (cons (concat (car x) extra) (cdr x))))
  6844. org-refile-target-table))
  6845. (completion-ignore-case t))
  6846. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6847. tbl)))
  6848. (defun org-olpath-completing-read (prompt collection &rest args)
  6849. "Read an outline path like a file name."
  6850. (let ((thetable collection))
  6851. (apply
  6852. 'org-ido-completing-read prompt
  6853. (lambda (string predicate &optional flag)
  6854. (let (rtn r s f (l (length string)))
  6855. (cond
  6856. ((eq flag nil)
  6857. ;; try completion
  6858. (try-completion string thetable))
  6859. ((eq flag t)
  6860. ;; all-completions
  6861. (setq rtn (all-completions string thetable predicate))
  6862. (mapcar
  6863. (lambda (x)
  6864. (setq r (substring x l))
  6865. (if (string-match " ([^)]*)$" x)
  6866. (setq f (match-string 0 x))
  6867. (setq f ""))
  6868. (if (string-match "/" r)
  6869. (concat string (substring r 0 (match-end 0)) f)
  6870. x))
  6871. rtn))
  6872. ((eq flag 'lambda)
  6873. ;; exact match?
  6874. (assoc string thetable)))
  6875. ))
  6876. args)))
  6877. ;;;; Dynamic blocks
  6878. (defun org-find-dblock (name)
  6879. "Find the first dynamic block with name NAME in the buffer.
  6880. If not found, stay at current position and return nil."
  6881. (let (pos)
  6882. (save-excursion
  6883. (goto-char (point-min))
  6884. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6885. nil t)
  6886. (match-beginning 0))))
  6887. (if pos (goto-char pos))
  6888. pos))
  6889. (defconst org-dblock-start-re
  6890. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6891. "Matches the startline of a dynamic block, with parameters.")
  6892. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6893. "Matches the end of a dyhamic block.")
  6894. (defun org-create-dblock (plist)
  6895. "Create a dynamic block section, with parameters taken from PLIST.
  6896. PLIST must containe a :name entry which is used as name of the block."
  6897. (unless (bolp) (newline))
  6898. (let ((name (plist-get plist :name)))
  6899. (insert "#+BEGIN: " name)
  6900. (while plist
  6901. (if (eq (car plist) :name)
  6902. (setq plist (cddr plist))
  6903. (insert " " (prin1-to-string (pop plist)))))
  6904. (insert "\n\n#+END:\n")
  6905. (beginning-of-line -2)))
  6906. (defun org-prepare-dblock ()
  6907. "Prepare dynamic block for refresh.
  6908. This empties the block, puts the cursor at the insert position and returns
  6909. the property list including an extra property :name with the block name."
  6910. (unless (looking-at org-dblock-start-re)
  6911. (error "Not at a dynamic block"))
  6912. (let* ((begdel (1+ (match-end 0)))
  6913. (name (org-no-properties (match-string 1)))
  6914. (params (append (list :name name)
  6915. (read (concat "(" (match-string 3) ")")))))
  6916. (unless (re-search-forward org-dblock-end-re nil t)
  6917. (error "Dynamic block not terminated"))
  6918. (setq params
  6919. (append params
  6920. (list :content (buffer-substring
  6921. begdel (match-beginning 0)))))
  6922. (delete-region begdel (match-beginning 0))
  6923. (goto-char begdel)
  6924. (open-line 1)
  6925. params))
  6926. (defun org-map-dblocks (&optional command)
  6927. "Apply COMMAND to all dynamic blocks in the current buffer.
  6928. If COMMAND is not given, use `org-update-dblock'."
  6929. (let ((cmd (or command 'org-update-dblock))
  6930. pos)
  6931. (save-excursion
  6932. (goto-char (point-min))
  6933. (while (re-search-forward org-dblock-start-re nil t)
  6934. (goto-char (setq pos (match-beginning 0)))
  6935. (condition-case nil
  6936. (funcall cmd)
  6937. (error (message "Error during update of dynamic block")))
  6938. (goto-char pos)
  6939. (unless (re-search-forward org-dblock-end-re nil t)
  6940. (error "Dynamic block not terminated"))))))
  6941. (defun org-dblock-update (&optional arg)
  6942. "User command for updating dynamic blocks.
  6943. Update the dynamic block at point. With prefix ARG, update all dynamic
  6944. blocks in the buffer."
  6945. (interactive "P")
  6946. (if arg
  6947. (org-update-all-dblocks)
  6948. (or (looking-at org-dblock-start-re)
  6949. (org-beginning-of-dblock))
  6950. (org-update-dblock)))
  6951. (defun org-update-dblock ()
  6952. "Update the dynamic block at point
  6953. This means to empty the block, parse for parameters and then call
  6954. the correct writing function."
  6955. (save-window-excursion
  6956. (let* ((pos (point))
  6957. (line (org-current-line))
  6958. (params (org-prepare-dblock))
  6959. (name (plist-get params :name))
  6960. (cmd (intern (concat "org-dblock-write:" name))))
  6961. (message "Updating dynamic block `%s' at line %d..." name line)
  6962. (funcall cmd params)
  6963. (message "Updating dynamic block `%s' at line %d...done" name line)
  6964. (goto-char pos))))
  6965. (defun org-beginning-of-dblock ()
  6966. "Find the beginning of the dynamic block at point.
  6967. Error if there is no scuh block at point."
  6968. (let ((pos (point))
  6969. beg)
  6970. (end-of-line 1)
  6971. (if (and (re-search-backward org-dblock-start-re nil t)
  6972. (setq beg (match-beginning 0))
  6973. (re-search-forward org-dblock-end-re nil t)
  6974. (> (match-end 0) pos))
  6975. (goto-char beg)
  6976. (goto-char pos)
  6977. (error "Not in a dynamic block"))))
  6978. (defun org-update-all-dblocks ()
  6979. "Update all dynamic blocks in the buffer.
  6980. This function can be used in a hook."
  6981. (when (org-mode-p)
  6982. (org-map-dblocks 'org-update-dblock)))
  6983. ;;;; Completion
  6984. (defconst org-additional-option-like-keywords
  6985. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6986. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6987. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6988. "BEGIN_QUOTE" "END_QUOTE"
  6989. "BEGIN_VERSE" "END_VERSE"
  6990. "BEGIN_SRC" "END_SRC"))
  6991. (defcustom org-structure-template-alist
  6992. '(
  6993. ("s" "#+begin_src ?\n\n#+end_src"
  6994. "<src lang=\"?\">\n\n</src>")
  6995. ("e" "#+begin_example\n?\n#+end_example"
  6996. "<example>\n?\n</example>")
  6997. ("q" "#+begin_quote\n?\n#+end_quote"
  6998. "<quote>\n?\n</quote>")
  6999. ("v" "#+begin_verse\n?\n#+end_verse"
  7000. "<verse>\n?\n/verse>")
  7001. ("l" "#+begin_latex\n?\n#+end_latex"
  7002. "<literal style=\"latex\">\n?\n</literal>")
  7003. ("L" "#+latex: "
  7004. "<literal style=\"latex\">?</literal>")
  7005. ("h" "#+begin_html\n?\n#+end_html"
  7006. "<literal style=\"html\">\n?\n</literal>")
  7007. ("H" "#+html: "
  7008. "<literal style=\"html\">?</literal>")
  7009. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7010. ("A" "#+ascii: ")
  7011. ("i" "#+include %file ?"
  7012. "<include file=%file markup=\"?\">")
  7013. )
  7014. "Structure completion elements.
  7015. This is a list of abbreviation keys and values. The value gets inserted
  7016. it you type @samp{.} followed by the key and then the completion key,
  7017. usually `M-TAB'. %file will be replaced by a file name after prompting
  7018. for the file uning completion.
  7019. There are two templates for each key, the first uses the original Org syntax,
  7020. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7021. the default when the /org-mtags.el/ module has been loaded. See also the
  7022. variable `org-mtags-prefer-muse-templates'.
  7023. This is an experimental feature, it is undecided if it is going to stay in."
  7024. :group 'org-completion
  7025. :type '(repeat
  7026. (string :tag "Key")
  7027. (string :tag "Template")
  7028. (string :tag "Muse Template")))
  7029. (defun org-try-structure-completion ()
  7030. "Try to complete a structure template before point.
  7031. This looks for strings like \"<e\" on an otherwise empty line and
  7032. expands them."
  7033. (let ((l (buffer-substring (point-at-bol) (point)))
  7034. a)
  7035. (when (and (looking-at "[ \t]*$")
  7036. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7037. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7038. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7039. (match-beginning 1)) a)
  7040. t)))
  7041. (defun org-complete-expand-structure-template (start cell)
  7042. "Expand a structure template."
  7043. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7044. (rpl (nth (if musep 2 1) cell)))
  7045. (delete-region start (point))
  7046. (when (string-match "\\`#\\+" rpl)
  7047. (cond
  7048. ((bolp))
  7049. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7050. (delete-region (point-at-bol) (point)))
  7051. (t (newline))))
  7052. (setq start (point))
  7053. (if (string-match "%file" rpl)
  7054. (setq rpl (replace-match
  7055. (concat
  7056. "\""
  7057. (save-match-data
  7058. (abbreviate-file-name (read-file-name "Include file: ")))
  7059. "\"")
  7060. t t rpl)))
  7061. (insert rpl)
  7062. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7063. (defun org-complete (&optional arg)
  7064. "Perform completion on word at point.
  7065. At the beginning of a headline, this completes TODO keywords as given in
  7066. `org-todo-keywords'.
  7067. If the current word is preceded by a backslash, completes the TeX symbols
  7068. that are supported for HTML support.
  7069. If the current word is preceded by \"#+\", completes special words for
  7070. setting file options.
  7071. In the line after \"#+STARTUP:, complete valid keywords.\"
  7072. At all other locations, this simply calls the value of
  7073. `org-completion-fallback-command'."
  7074. (interactive "P")
  7075. (org-without-partial-completion
  7076. (catch 'exit
  7077. (let* ((a nil)
  7078. (end (point))
  7079. (beg1 (save-excursion
  7080. (skip-chars-backward (org-re "[:alnum:]_@"))
  7081. (point)))
  7082. (beg (save-excursion
  7083. (skip-chars-backward "a-zA-Z0-9_:$")
  7084. (point)))
  7085. (confirm (lambda (x) (stringp (car x))))
  7086. (searchhead (equal (char-before beg) ?*))
  7087. (struct
  7088. (when (and (member (char-before beg1) '(?. ?<))
  7089. (setq a (assoc (buffer-substring beg1 (point))
  7090. org-structure-template-alist)))
  7091. (org-complete-expand-structure-template (1- beg1) a)
  7092. (throw 'exit t)))
  7093. (tag (and (equal (char-before beg1) ?:)
  7094. (equal (char-after (point-at-bol)) ?*)))
  7095. (prop (and (equal (char-before beg1) ?:)
  7096. (not (equal (char-after (point-at-bol)) ?*))))
  7097. (texp (equal (char-before beg) ?\\))
  7098. (link (equal (char-before beg) ?\[))
  7099. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7100. beg)
  7101. "#+"))
  7102. (startup (string-match "^#\\+STARTUP:.*"
  7103. (buffer-substring (point-at-bol) (point))))
  7104. (completion-ignore-case opt)
  7105. (type nil)
  7106. (tbl nil)
  7107. (table (cond
  7108. (opt
  7109. (setq type :opt)
  7110. (require 'org-exp)
  7111. (append
  7112. (mapcar
  7113. (lambda (x)
  7114. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7115. (cons (match-string 2 x) (match-string 1 x)))
  7116. (org-split-string (org-get-current-options) "\n"))
  7117. (mapcar 'list org-additional-option-like-keywords)))
  7118. (startup
  7119. (setq type :startup)
  7120. org-startup-options)
  7121. (link (append org-link-abbrev-alist-local
  7122. org-link-abbrev-alist))
  7123. (texp
  7124. (setq type :tex)
  7125. org-html-entities)
  7126. ((string-match "\\`\\*+[ \t]+\\'"
  7127. (buffer-substring (point-at-bol) beg))
  7128. (setq type :todo)
  7129. (mapcar 'list org-todo-keywords-1))
  7130. (searchhead
  7131. (setq type :searchhead)
  7132. (save-excursion
  7133. (goto-char (point-min))
  7134. (while (re-search-forward org-todo-line-regexp nil t)
  7135. (push (list
  7136. (org-make-org-heading-search-string
  7137. (match-string 3) t))
  7138. tbl)))
  7139. tbl)
  7140. (tag (setq type :tag beg beg1)
  7141. (or org-tag-alist (org-get-buffer-tags)))
  7142. (prop (setq type :prop beg beg1)
  7143. (mapcar 'list (org-buffer-property-keys nil t t)))
  7144. (t (progn
  7145. (call-interactively org-completion-fallback-command)
  7146. (throw 'exit nil)))))
  7147. (pattern (buffer-substring-no-properties beg end))
  7148. (completion (try-completion pattern table confirm)))
  7149. (cond ((eq completion t)
  7150. (if (not (assoc (upcase pattern) table))
  7151. (message "Already complete")
  7152. (if (and (equal type :opt)
  7153. (not (member (car (assoc (upcase pattern) table))
  7154. org-additional-option-like-keywords)))
  7155. (insert (substring (cdr (assoc (upcase pattern) table))
  7156. (length pattern)))
  7157. (if (memq type '(:tag :prop)) (insert ":")))))
  7158. ((null completion)
  7159. (message "Can't find completion for \"%s\"" pattern)
  7160. (ding))
  7161. ((not (string= pattern completion))
  7162. (delete-region beg end)
  7163. (if (string-match " +$" completion)
  7164. (setq completion (replace-match "" t t completion)))
  7165. (insert completion)
  7166. (if (get-buffer-window "*Completions*")
  7167. (delete-window (get-buffer-window "*Completions*")))
  7168. (if (assoc completion table)
  7169. (if (eq type :todo) (insert " ")
  7170. (if (memq type '(:tag :prop)) (insert ":"))))
  7171. (if (and (equal type :opt) (assoc completion table))
  7172. (message "%s" (substitute-command-keys
  7173. "Press \\[org-complete] again to insert example settings"))))
  7174. (t
  7175. (message "Making completion list...")
  7176. (let ((list (sort (all-completions pattern table confirm)
  7177. 'string<)))
  7178. (with-output-to-temp-buffer "*Completions*"
  7179. (condition-case nil
  7180. ;; Protection needed for XEmacs and emacs 21
  7181. (display-completion-list list pattern)
  7182. (error (display-completion-list list)))))
  7183. (message "Making completion list...%s" "done")))))))
  7184. ;;;; TODO, DEADLINE, Comments
  7185. (defun org-toggle-comment ()
  7186. "Change the COMMENT state of an entry."
  7187. (interactive)
  7188. (save-excursion
  7189. (org-back-to-heading)
  7190. (let (case-fold-search)
  7191. (if (looking-at (concat outline-regexp
  7192. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7193. (replace-match "" t t nil 1)
  7194. (if (looking-at outline-regexp)
  7195. (progn
  7196. (goto-char (match-end 0))
  7197. (insert org-comment-string " ")))))))
  7198. (defvar org-last-todo-state-is-todo nil
  7199. "This is non-nil when the last TODO state change led to a TODO state.
  7200. If the last change removed the TODO tag or switched to DONE, then
  7201. this is nil.")
  7202. (defvar org-setting-tags nil) ; dynamically skiped
  7203. (defun org-parse-local-options (string var)
  7204. "Parse STRING for startup setting relevant for variable VAR."
  7205. (let ((rtn (symbol-value var))
  7206. e opts)
  7207. (save-match-data
  7208. (if (or (not string) (not (string-match "\\S-" string)))
  7209. rtn
  7210. (setq opts (delq nil (mapcar (lambda (x)
  7211. (setq e (assoc x org-startup-options))
  7212. (if (eq (nth 1 e) var) e nil))
  7213. (org-split-string string "[ \t]+"))))
  7214. (if (not opts)
  7215. rtn
  7216. (setq rtn nil)
  7217. (while (setq e (pop opts))
  7218. (if (not (nth 3 e))
  7219. (setq rtn (nth 2 e))
  7220. (if (not (listp rtn)) (setq rtn nil))
  7221. (push (nth 2 e) rtn)))
  7222. rtn)))))
  7223. (defvar org-blocker-hook nil
  7224. "Hook for functions that are allowed to block a state change.
  7225. Each function gets as its single argument a property list, see
  7226. `org-trigger-hook' for more information about this list.
  7227. If any of the functions in this hook returns nil, the state change
  7228. is blocked.")
  7229. (defvar org-trigger-hook nil
  7230. "Hook for functions that are triggered by a state change.
  7231. Each function gets as its single argument a property list with at least
  7232. the following elements:
  7233. (:type type-of-change :position pos-at-entry-start
  7234. :from old-state :to new-state)
  7235. Depending on the type, more properties may be present.
  7236. This mechanism is currently implemented for:
  7237. TODO state changes
  7238. ------------------
  7239. :type todo-state-change
  7240. :from previous state (keyword as a string), or nil
  7241. :to new state (keyword as a string), or nil")
  7242. (defvar org-agenda-headline-snapshot-before-repeat)
  7243. (defun org-todo (&optional arg)
  7244. "Change the TODO state of an item.
  7245. The state of an item is given by a keyword at the start of the heading,
  7246. like
  7247. *** TODO Write paper
  7248. *** DONE Call mom
  7249. The different keywords are specified in the variable `org-todo-keywords'.
  7250. By default the available states are \"TODO\" and \"DONE\".
  7251. So for this example: when the item starts with TODO, it is changed to DONE.
  7252. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7253. DONE are present, add TODO at the beginning of the heading.
  7254. With C-u prefix arg, use completion to determine the new state.
  7255. With numeric prefix arg, switch to that state.
  7256. For calling through lisp, arg is also interpreted in the following way:
  7257. 'none -> empty state
  7258. \"\"(empty string) -> switch to empty state
  7259. 'done -> switch to DONE
  7260. 'nextset -> switch to the next set of keywords
  7261. 'previousset -> switch to the previous set of keywords
  7262. \"WAITING\" -> switch to the specified keyword, but only if it
  7263. really is a member of `org-todo-keywords'."
  7264. (interactive "P")
  7265. (save-excursion
  7266. (catch 'exit
  7267. (org-back-to-heading)
  7268. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7269. (or (looking-at (concat " +" org-todo-regexp " *"))
  7270. (looking-at " *"))
  7271. (let* ((match-data (match-data))
  7272. (startpos (point-at-bol))
  7273. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7274. (org-log-done org-log-done)
  7275. (org-log-repeat org-log-repeat)
  7276. (org-todo-log-states org-todo-log-states)
  7277. (this (match-string 1))
  7278. (hl-pos (match-beginning 0))
  7279. (head (org-get-todo-sequence-head this))
  7280. (ass (assoc head org-todo-kwd-alist))
  7281. (interpret (nth 1 ass))
  7282. (done-word (nth 3 ass))
  7283. (final-done-word (nth 4 ass))
  7284. (last-state (or this ""))
  7285. (completion-ignore-case t)
  7286. (member (member this org-todo-keywords-1))
  7287. (tail (cdr member))
  7288. (state (cond
  7289. ((and org-todo-key-trigger
  7290. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7291. (and (not arg) org-use-fast-todo-selection
  7292. (not (eq org-use-fast-todo-selection 'prefix)))))
  7293. ;; Use fast selection
  7294. (org-fast-todo-selection))
  7295. ((and (equal arg '(4))
  7296. (or (not org-use-fast-todo-selection)
  7297. (not org-todo-key-trigger)))
  7298. ;; Read a state with completion
  7299. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7300. org-todo-keywords-1)
  7301. nil t))
  7302. ((eq arg 'right)
  7303. (if this
  7304. (if tail (car tail) nil)
  7305. (car org-todo-keywords-1)))
  7306. ((eq arg 'left)
  7307. (if (equal member org-todo-keywords-1)
  7308. nil
  7309. (if this
  7310. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7311. org-todo-keywords-1)
  7312. (org-last org-todo-keywords-1))))
  7313. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7314. (setq arg nil))) ; hack to fall back to cycling
  7315. (arg
  7316. ;; user or caller requests a specific state
  7317. (cond
  7318. ((equal arg "") nil)
  7319. ((eq arg 'none) nil)
  7320. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7321. ((eq arg 'nextset)
  7322. (or (car (cdr (member head org-todo-heads)))
  7323. (car org-todo-heads)))
  7324. ((eq arg 'previousset)
  7325. (let ((org-todo-heads (reverse org-todo-heads)))
  7326. (or (car (cdr (member head org-todo-heads)))
  7327. (car org-todo-heads))))
  7328. ((car (member arg org-todo-keywords-1)))
  7329. ((nth (1- (prefix-numeric-value arg))
  7330. org-todo-keywords-1))))
  7331. ((null member) (or head (car org-todo-keywords-1)))
  7332. ((equal this final-done-word) nil) ;; -> make empty
  7333. ((null tail) nil) ;; -> first entry
  7334. ((eq interpret 'sequence)
  7335. (car tail))
  7336. ((memq interpret '(type priority))
  7337. (if (eq this-command last-command)
  7338. (car tail)
  7339. (if (> (length tail) 0)
  7340. (or done-word (car org-done-keywords))
  7341. nil)))
  7342. (t nil)))
  7343. (next (if state (concat " " state " ") " "))
  7344. (change-plist (list :type 'todo-state-change :from this :to state
  7345. :position startpos))
  7346. dolog now-done-p)
  7347. (when org-blocker-hook
  7348. (unless (save-excursion
  7349. (save-match-data
  7350. (run-hook-with-args-until-failure
  7351. 'org-blocker-hook change-plist)))
  7352. (if (interactive-p)
  7353. (error "TODO state change from %s to %s blocked" this state)
  7354. ;; fail silently
  7355. (message "TODO state change from %s to %s blocked" this state)
  7356. (throw 'exit nil))))
  7357. (store-match-data match-data)
  7358. (replace-match next t t)
  7359. (unless (pos-visible-in-window-p hl-pos)
  7360. (message "TODO state changed to %s" (org-trim next)))
  7361. (unless head
  7362. (setq head (org-get-todo-sequence-head state)
  7363. ass (assoc head org-todo-kwd-alist)
  7364. interpret (nth 1 ass)
  7365. done-word (nth 3 ass)
  7366. final-done-word (nth 4 ass)))
  7367. (when (memq arg '(nextset previousset))
  7368. (message "Keyword-Set %d/%d: %s"
  7369. (- (length org-todo-sets) -1
  7370. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7371. (length org-todo-sets)
  7372. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7373. (setq org-last-todo-state-is-todo
  7374. (not (member state org-done-keywords)))
  7375. (setq now-done-p (and (member state org-done-keywords)
  7376. (not (member this org-done-keywords))))
  7377. (and logging (org-local-logging logging))
  7378. (when (and (or org-todo-log-states org-log-done)
  7379. (not (memq arg '(nextset previousset))))
  7380. ;; we need to look at recording a time and note
  7381. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7382. (nth 2 (assoc this org-todo-log-states))))
  7383. (when (and state
  7384. (member state org-not-done-keywords)
  7385. (not (member this org-not-done-keywords)))
  7386. ;; This is now a todo state and was not one before
  7387. ;; If there was a CLOSED time stamp, get rid of it.
  7388. (org-add-planning-info nil nil 'closed))
  7389. (when (and now-done-p org-log-done)
  7390. ;; It is now done, and it was not done before
  7391. (org-add-planning-info 'closed (org-current-time))
  7392. (if (and (not dolog) (eq 'note org-log-done))
  7393. (org-add-log-setup 'done state 'findpos 'note)))
  7394. (when (and state dolog)
  7395. ;; This is a non-nil state, and we need to log it
  7396. (org-add-log-setup 'state state 'findpos dolog)))
  7397. ;; Fixup tag positioning
  7398. (org-todo-trigger-tag-changes state)
  7399. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7400. (when org-provide-todo-statistics
  7401. (org-update-parent-todo-statistics))
  7402. (run-hooks 'org-after-todo-state-change-hook)
  7403. (if (and arg (not (member state org-done-keywords)))
  7404. (setq head (org-get-todo-sequence-head state)))
  7405. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7406. ;; Do we need to trigger a repeat?
  7407. (when now-done-p
  7408. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7409. ;; This is for the agenda, take a snapshot of the headline.
  7410. (save-match-data
  7411. (setq org-agenda-headline-snapshot-before-repeat
  7412. (org-get-heading))))
  7413. (org-auto-repeat-maybe state))
  7414. ;; Fixup cursor location if close to the keyword
  7415. (if (and (outline-on-heading-p)
  7416. (not (bolp))
  7417. (save-excursion (beginning-of-line 1)
  7418. (looking-at org-todo-line-regexp))
  7419. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7420. (progn
  7421. (goto-char (or (match-end 2) (match-end 1)))
  7422. (just-one-space)))
  7423. (when org-trigger-hook
  7424. (save-excursion
  7425. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7426. (defun org-update-parent-todo-statistics ()
  7427. "Update any statistics cookie in the parent of the current headline."
  7428. (interactive)
  7429. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7430. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7431. (catch 'exit
  7432. (save-excursion
  7433. (setq level (org-up-heading-safe))
  7434. (unless (and level
  7435. (re-search-forward box-re (point-at-eol) t))
  7436. (throw 'exit nil))
  7437. (setq is-percent (match-end 2))
  7438. (save-match-data
  7439. (unless (outline-next-heading) (throw 'exit nil))
  7440. (while (looking-at org-todo-line-regexp)
  7441. (setq kwd (match-string 2))
  7442. (and kwd (setq cnt-all (1+ cnt-all)))
  7443. (and (member kwd org-done-keywords)
  7444. (setq cnt-done (1+ cnt-done)))
  7445. (condition-case nil
  7446. (org-forward-same-level 1)
  7447. (error (end-of-line 1)))))
  7448. (replace-match
  7449. (if is-percent
  7450. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7451. (format "[%d/%d]" cnt-done cnt-all)))
  7452. (run-hook-with-args 'org-after-todo-statistics-hook
  7453. cnt-done (- cnt-all cnt-done))))))
  7454. (defvar org-after-todo-statistics-hook nil
  7455. "Hook that is called after a TODO statistics cookie has been updated.
  7456. Each function is called with two arguments: the number of not-done entries
  7457. and the number of done entries.
  7458. For example, the following function, when added to this hook, will switch
  7459. an entry to DONE when all children are done, and back to TODO when new
  7460. entries are set to a TODO status. Note that this hook is only called
  7461. when there is a statistics cookie in the headline!
  7462. (defun org-summary-todo (n-done n-not-done)
  7463. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7464. (let (org-log-done org-log-states) ; turn off logging
  7465. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7466. ")
  7467. (defun org-todo-trigger-tag-changes (state)
  7468. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7469. (let ((l org-todo-state-tags-triggers)
  7470. changes)
  7471. (when (or (not state) (equal state ""))
  7472. (setq changes (append changes (cdr (assoc "" l)))))
  7473. (when (and (stringp state) (> (length state) 0))
  7474. (setq changes (append changes (cdr (assoc state l)))))
  7475. (when (member state org-not-done-keywords)
  7476. (setq changes (append changes (cdr (assoc 'todo l)))))
  7477. (when (member state org-done-keywords)
  7478. (setq changes (append changes (cdr (assoc 'done l)))))
  7479. (dolist (c changes)
  7480. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7481. (defun org-local-logging (value)
  7482. "Get logging settings from a property VALUE."
  7483. (let* (words w a)
  7484. ;; directly set the variables, they are already local.
  7485. (setq org-log-done nil
  7486. org-log-repeat nil
  7487. org-todo-log-states nil)
  7488. (setq words (org-split-string value))
  7489. (while (setq w (pop words))
  7490. (cond
  7491. ((setq a (assoc w org-startup-options))
  7492. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7493. (set (nth 1 a) (nth 2 a))))
  7494. ((setq a (org-extract-log-state-settings w))
  7495. (and (member (car a) org-todo-keywords-1)
  7496. (push a org-todo-log-states)))))))
  7497. (defun org-get-todo-sequence-head (kwd)
  7498. "Return the head of the TODO sequence to which KWD belongs.
  7499. If KWD is not set, check if there is a text property remembering the
  7500. right sequence."
  7501. (let (p)
  7502. (cond
  7503. ((not kwd)
  7504. (or (get-text-property (point-at-bol) 'org-todo-head)
  7505. (progn
  7506. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7507. nil (point-at-eol)))
  7508. (get-text-property p 'org-todo-head))))
  7509. ((not (member kwd org-todo-keywords-1))
  7510. (car org-todo-keywords-1))
  7511. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7512. (defun org-fast-todo-selection ()
  7513. "Fast TODO keyword selection with single keys.
  7514. Returns the new TODO keyword, or nil if no state change should occur."
  7515. (let* ((fulltable org-todo-key-alist)
  7516. (done-keywords org-done-keywords) ;; needed for the faces.
  7517. (maxlen (apply 'max (mapcar
  7518. (lambda (x)
  7519. (if (stringp (car x)) (string-width (car x)) 0))
  7520. fulltable)))
  7521. (expert nil)
  7522. (fwidth (+ maxlen 3 1 3))
  7523. (ncol (/ (- (window-width) 4) fwidth))
  7524. tg cnt e c tbl
  7525. groups ingroup)
  7526. (save-window-excursion
  7527. (if expert
  7528. (set-buffer (get-buffer-create " *Org todo*"))
  7529. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7530. (erase-buffer)
  7531. (org-set-local 'org-done-keywords done-keywords)
  7532. (setq tbl fulltable cnt 0)
  7533. (while (setq e (pop tbl))
  7534. (cond
  7535. ((equal e '(:startgroup))
  7536. (push '() groups) (setq ingroup t)
  7537. (when (not (= cnt 0))
  7538. (setq cnt 0)
  7539. (insert "\n"))
  7540. (insert "{ "))
  7541. ((equal e '(:endgroup))
  7542. (setq ingroup nil cnt 0)
  7543. (insert "}\n"))
  7544. (t
  7545. (setq tg (car e) c (cdr e))
  7546. (if ingroup (push tg (car groups)))
  7547. (setq tg (org-add-props tg nil 'face
  7548. (org-get-todo-face tg)))
  7549. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7550. (insert "[" c "] " tg (make-string
  7551. (- fwidth 4 (length tg)) ?\ ))
  7552. (when (= (setq cnt (1+ cnt)) ncol)
  7553. (insert "\n")
  7554. (if ingroup (insert " "))
  7555. (setq cnt 0)))))
  7556. (insert "\n")
  7557. (goto-char (point-min))
  7558. (if (not expert) (org-fit-window-to-buffer))
  7559. (message "[a-z..]:Set [SPC]:clear")
  7560. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7561. (cond
  7562. ((or (= c ?\C-g)
  7563. (and (= c ?q) (not (rassoc c fulltable))))
  7564. (setq quit-flag t))
  7565. ((= c ?\ ) nil)
  7566. ((setq e (rassoc c fulltable) tg (car e))
  7567. tg)
  7568. (t (setq quit-flag t))))))
  7569. (defun org-entry-is-todo-p ()
  7570. (member (org-get-todo-state) org-not-done-keywords))
  7571. (defun org-entry-is-done-p ()
  7572. (member (org-get-todo-state) org-done-keywords))
  7573. (defun org-get-todo-state ()
  7574. (save-excursion
  7575. (org-back-to-heading t)
  7576. (and (looking-at org-todo-line-regexp)
  7577. (match-end 2)
  7578. (match-string 2))))
  7579. (defun org-at-date-range-p (&optional inactive-ok)
  7580. "Is the cursor inside a date range?"
  7581. (interactive)
  7582. (save-excursion
  7583. (catch 'exit
  7584. (let ((pos (point)))
  7585. (skip-chars-backward "^[<\r\n")
  7586. (skip-chars-backward "<[")
  7587. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7588. (>= (match-end 0) pos)
  7589. (throw 'exit t))
  7590. (skip-chars-backward "^<[\r\n")
  7591. (skip-chars-backward "<[")
  7592. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7593. (>= (match-end 0) pos)
  7594. (throw 'exit t)))
  7595. nil)))
  7596. (defun org-get-repeat ()
  7597. "Check if there is a deadline/schedule with repeater in this entry."
  7598. (save-match-data
  7599. (save-excursion
  7600. (org-back-to-heading t)
  7601. (if (re-search-forward
  7602. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7603. (match-string 1)))))
  7604. (defvar org-last-changed-timestamp)
  7605. (defvar org-last-inserted-timestamp)
  7606. (defvar org-log-post-message)
  7607. (defvar org-log-note-purpose)
  7608. (defvar org-log-note-how)
  7609. (defvar org-log-note-extra)
  7610. (defun org-auto-repeat-maybe (done-word)
  7611. "Check if the current headline contains a repeated deadline/schedule.
  7612. If yes, set TODO state back to what it was and change the base date
  7613. of repeating deadline/scheduled time stamps to new date.
  7614. This function is run automatically after each state change to a DONE state."
  7615. ;; last-state is dynamically scoped into this function
  7616. (let* ((repeat (org-get-repeat))
  7617. (aa (assoc last-state org-todo-kwd-alist))
  7618. (interpret (nth 1 aa))
  7619. (head (nth 2 aa))
  7620. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7621. (msg "Entry repeats: ")
  7622. (org-log-done nil)
  7623. (org-todo-log-states nil)
  7624. (nshiftmax 10) (nshift 0)
  7625. re type n what ts mb0 time)
  7626. (when repeat
  7627. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7628. (org-todo (if (eq interpret 'type) last-state head))
  7629. (when org-log-repeat
  7630. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7631. (memq 'org-add-log-note post-command-hook))
  7632. ;; OK, we are already setup for some record
  7633. (if (eq org-log-repeat 'note)
  7634. ;; make sure we take a note, not only a time stamp
  7635. (setq org-log-note-how 'note))
  7636. ;; Set up for taking a record
  7637. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7638. 'findpos org-log-repeat)))
  7639. (org-back-to-heading t)
  7640. (org-add-planning-info nil nil 'closed)
  7641. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7642. org-deadline-time-regexp "\\)\\|\\("
  7643. org-ts-regexp "\\)"))
  7644. (while (re-search-forward
  7645. re (save-excursion (outline-next-heading) (point)) t)
  7646. (setq type (if (match-end 1) org-scheduled-string
  7647. (if (match-end 3) org-deadline-string "Plain:"))
  7648. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7649. mb0 (match-beginning 0))
  7650. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7651. (setq n (string-to-number (match-string 2 ts))
  7652. what (match-string 3 ts))
  7653. (if (equal what "w") (setq n (* n 7) what "d"))
  7654. ;; Preparation, see if we need to modify the start date for the change
  7655. (when (match-end 1)
  7656. (setq time (save-match-data (org-time-string-to-time ts)))
  7657. (cond
  7658. ((equal (match-string 1 ts) ".")
  7659. ;; Shift starting date to today
  7660. (org-timestamp-change
  7661. (- (time-to-days (current-time)) (time-to-days time))
  7662. 'day))
  7663. ((equal (match-string 1 ts) "+")
  7664. (while (or (= nshift 0)
  7665. (<= (time-to-days time) (time-to-days (current-time))))
  7666. (when (= (incf nshift) nshiftmax)
  7667. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7668. (error "Abort")))
  7669. (org-timestamp-change n (cdr (assoc what whata)))
  7670. (org-at-timestamp-p t)
  7671. (setq ts (match-string 1))
  7672. (setq time (save-match-data (org-time-string-to-time ts))))
  7673. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7674. ;; rematch, so that we have everything in place for the real shift
  7675. (org-at-timestamp-p t)
  7676. (setq ts (match-string 1))
  7677. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7678. (org-timestamp-change n (cdr (assoc what whata)))
  7679. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7680. (setq org-log-post-message msg)
  7681. (message "%s" msg))))
  7682. (defun org-show-todo-tree (arg)
  7683. "Make a compact tree which shows all headlines marked with TODO.
  7684. The tree will show the lines where the regexp matches, and all higher
  7685. headlines above the match.
  7686. With a \\[universal-argument] prefix, also show the DONE entries.
  7687. With a numeric prefix N, construct a sparse tree for the Nth element
  7688. of `org-todo-keywords-1'."
  7689. (interactive "P")
  7690. (let ((case-fold-search nil)
  7691. (kwd-re
  7692. (cond ((null arg) org-not-done-regexp)
  7693. ((equal arg '(4))
  7694. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7695. (mapcar 'list org-todo-keywords-1))))
  7696. (concat "\\("
  7697. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7698. "\\)\\>")))
  7699. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7700. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7701. org-todo-keywords-1)))
  7702. (t (error "Invalid prefix argument: %s" arg)))))
  7703. (message "%d TODO entries found"
  7704. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7705. (defun org-deadline (&optional remove time)
  7706. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7707. With argument REMOVE, remove any deadline from the item.
  7708. When TIME is set, it should be an internal time specification, and the
  7709. scheduling will use the corresponding date."
  7710. (interactive "P")
  7711. (if remove
  7712. (progn
  7713. (org-remove-timestamp-with-keyword org-deadline-string)
  7714. (message "Item no longer has a deadline."))
  7715. (if (org-get-repeat)
  7716. (error "Cannot change deadline on task with repeater, please do that by hand")
  7717. (org-add-planning-info 'deadline time 'closed)
  7718. (message "Deadline on %s" org-last-inserted-timestamp))))
  7719. (defun org-schedule (&optional remove time)
  7720. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7721. With argument REMOVE, remove any scheduling date from the item.
  7722. When TIME is set, it should be an internal time specification, and the
  7723. scheduling will use the corresponding date."
  7724. (interactive "P")
  7725. (if remove
  7726. (progn
  7727. (org-remove-timestamp-with-keyword org-scheduled-string)
  7728. (message "Item is no longer scheduled."))
  7729. (if (org-get-repeat)
  7730. (error "Cannot reschedule task with repeater, please do that by hand")
  7731. (org-add-planning-info 'scheduled time 'closed)
  7732. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7733. (defun org-remove-timestamp-with-keyword (keyword)
  7734. "Remove all time stamps with KEYWORD in the current entry."
  7735. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7736. beg)
  7737. (save-excursion
  7738. (org-back-to-heading t)
  7739. (setq beg (point))
  7740. (org-end-of-subtree t t)
  7741. (while (re-search-backward re beg t)
  7742. (replace-match "")
  7743. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7744. (equal (char-before) ?\ ))
  7745. (backward-delete-char 1)
  7746. (if (string-match "^[ \t]*$" (buffer-substring
  7747. (point-at-bol) (point-at-eol)))
  7748. (delete-region (point-at-bol)
  7749. (min (point-max) (1+ (point-at-eol))))))))))
  7750. (defun org-add-planning-info (what &optional time &rest remove)
  7751. "Insert new timestamp with keyword in the line directly after the headline.
  7752. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7753. If non is given, the user is prompted for a date.
  7754. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7755. be removed."
  7756. (interactive)
  7757. (let (org-time-was-given org-end-time-was-given ts
  7758. end default-time default-input)
  7759. (when (and (not time) (memq what '(scheduled deadline)))
  7760. ;; Try to get a default date/time from existing timestamp
  7761. (save-excursion
  7762. (org-back-to-heading t)
  7763. (setq end (save-excursion (outline-next-heading) (point)))
  7764. (when (re-search-forward (if (eq what 'scheduled)
  7765. org-scheduled-time-regexp
  7766. org-deadline-time-regexp)
  7767. end t)
  7768. (setq ts (match-string 1)
  7769. default-time
  7770. (apply 'encode-time (org-parse-time-string ts))
  7771. default-input (and ts (org-get-compact-tod ts))))))
  7772. (when what
  7773. ;; If necessary, get the time from the user
  7774. (setq time (or time (org-read-date nil 'to-time nil nil
  7775. default-time default-input))))
  7776. (when (and org-insert-labeled-timestamps-at-point
  7777. (member what '(scheduled deadline)))
  7778. (insert
  7779. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7780. (org-insert-time-stamp time org-time-was-given
  7781. nil nil nil (list org-end-time-was-given))
  7782. (setq what nil))
  7783. (save-excursion
  7784. (save-restriction
  7785. (let (col list elt ts buffer-invisibility-spec)
  7786. (org-back-to-heading t)
  7787. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7788. (goto-char (match-end 1))
  7789. (setq col (current-column))
  7790. (goto-char (match-end 0))
  7791. (if (eobp) (insert "\n") (forward-char 1))
  7792. (if (and (not (looking-at outline-regexp))
  7793. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7794. "[^\r\n]*"))
  7795. (not (equal (match-string 1) org-clock-string)))
  7796. (narrow-to-region (match-beginning 0) (match-end 0))
  7797. (insert-before-markers "\n")
  7798. (backward-char 1)
  7799. (narrow-to-region (point) (point))
  7800. (and org-adapt-indentation (org-indent-to-column col)))
  7801. ;; Check if we have to remove something.
  7802. (setq list (cons what remove))
  7803. (while list
  7804. (setq elt (pop list))
  7805. (goto-char (point-min))
  7806. (when (or (and (eq elt 'scheduled)
  7807. (re-search-forward org-scheduled-time-regexp nil t))
  7808. (and (eq elt 'deadline)
  7809. (re-search-forward org-deadline-time-regexp nil t))
  7810. (and (eq elt 'closed)
  7811. (re-search-forward org-closed-time-regexp nil t)))
  7812. (replace-match "")
  7813. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7814. (if (looking-at " +") (replace-match ""))))
  7815. (goto-char (point-max))
  7816. (when what
  7817. (insert
  7818. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7819. (cond ((eq what 'scheduled) org-scheduled-string)
  7820. ((eq what 'deadline) org-deadline-string)
  7821. ((eq what 'closed) org-closed-string))
  7822. " ")
  7823. (setq ts (org-insert-time-stamp
  7824. time
  7825. (or org-time-was-given
  7826. (and (eq what 'closed) org-log-done-with-time))
  7827. (eq what 'closed)
  7828. nil nil (list org-end-time-was-given)))
  7829. (end-of-line 1))
  7830. (goto-char (point-min))
  7831. (widen)
  7832. (if (and (looking-at "[ \t]+\n")
  7833. (equal (char-before) ?\n))
  7834. (delete-region (1- (point)) (point-at-eol)))
  7835. ts)))))
  7836. (defvar org-log-note-marker (make-marker))
  7837. (defvar org-log-note-purpose nil)
  7838. (defvar org-log-note-state nil)
  7839. (defvar org-log-note-how nil)
  7840. (defvar org-log-note-extra nil)
  7841. (defvar org-log-note-window-configuration nil)
  7842. (defvar org-log-note-return-to (make-marker))
  7843. (defvar org-log-post-message nil
  7844. "Message to be displayed after a log note has been stored.
  7845. The auto-repeater uses this.")
  7846. (defun org-add-note ()
  7847. "Add a note to the current entry.
  7848. This is done in the same way as adding a state change note."
  7849. (interactive)
  7850. (org-add-log-setup 'note nil 'findpos nil))
  7851. (defvar org-property-end-re)
  7852. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7853. "Set up the post command hook to take a note.
  7854. If this is about to TODO state change, the new state is expected in STATE.
  7855. When FINDPOS is non-nil, find the correct position for the note in
  7856. the current entry. If not, assume that it can be inserted at point.
  7857. HOW is an indicator what kind of note should be created.
  7858. EXTRA is additional text that will be inserted into the notes buffer."
  7859. (save-restriction
  7860. (save-excursion
  7861. (when findpos
  7862. (org-back-to-heading t)
  7863. (narrow-to-region (point) (save-excursion
  7864. (outline-next-heading) (point)))
  7865. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7866. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7867. "[^\r\n]*\\)?"))
  7868. (goto-char (match-end 0))
  7869. (when (and org-log-state-notes-insert-after-drawers
  7870. (save-excursion
  7871. (forward-line) (looking-at org-drawer-regexp)))
  7872. (progn (forward-line)
  7873. (while (looking-at org-drawer-regexp)
  7874. (goto-char (match-end 0))
  7875. (re-search-forward org-property-end-re (point-max) t)
  7876. (forward-line))
  7877. (forward-line -1)))
  7878. (unless org-log-states-order-reversed
  7879. (and (= (char-after) ?\n) (forward-char 1))
  7880. (org-skip-over-state-notes)
  7881. (skip-chars-backward " \t\n\r")))
  7882. (move-marker org-log-note-marker (point))
  7883. (setq org-log-note-purpose purpose
  7884. org-log-note-state state
  7885. org-log-note-how how
  7886. org-log-note-extra extra)
  7887. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7888. (defun org-skip-over-state-notes ()
  7889. "Skip past the list of State notes in an entry."
  7890. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7891. (while (looking-at "[ \t]*- State")
  7892. (condition-case nil
  7893. (org-next-item)
  7894. (error (org-end-of-item)))))
  7895. (defun org-add-log-note (&optional purpose)
  7896. "Pop up a window for taking a note, and add this note later at point."
  7897. (remove-hook 'post-command-hook 'org-add-log-note)
  7898. (setq org-log-note-window-configuration (current-window-configuration))
  7899. (delete-other-windows)
  7900. (move-marker org-log-note-return-to (point))
  7901. (switch-to-buffer (marker-buffer org-log-note-marker))
  7902. (goto-char org-log-note-marker)
  7903. (org-switch-to-buffer-other-window "*Org Note*")
  7904. (erase-buffer)
  7905. (if (memq org-log-note-how '(time state))
  7906. (let (current-prefix-arg) (org-store-log-note))
  7907. (let ((org-inhibit-startup t)) (org-mode))
  7908. (insert (format "# Insert note for %s.
  7909. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7910. (cond
  7911. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7912. ((eq org-log-note-purpose 'done) "closed todo item")
  7913. ((eq org-log-note-purpose 'state)
  7914. (format "state change to \"%s\"" org-log-note-state))
  7915. ((eq org-log-note-purpose 'note)
  7916. "this entry")
  7917. (t (error "This should not happen")))))
  7918. (if org-log-note-extra (insert org-log-note-extra))
  7919. (org-set-local 'org-finish-function 'org-store-log-note)))
  7920. (defvar org-note-abort nil) ; dynamically scoped
  7921. (defun org-store-log-note ()
  7922. "Finish taking a log note, and insert it to where it belongs."
  7923. (let ((txt (buffer-string))
  7924. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7925. lines ind)
  7926. (kill-buffer (current-buffer))
  7927. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7928. (setq txt (replace-match "" t t txt)))
  7929. (if (string-match "\\s-+\\'" txt)
  7930. (setq txt (replace-match "" t t txt)))
  7931. (setq lines (org-split-string txt "\n"))
  7932. (when (and note (string-match "\\S-" note))
  7933. (setq note
  7934. (org-replace-escapes
  7935. note
  7936. (list (cons "%u" (user-login-name))
  7937. (cons "%U" user-full-name)
  7938. (cons "%t" (format-time-string
  7939. (org-time-stamp-format 'long 'inactive)
  7940. (current-time)))
  7941. (cons "%s" (if org-log-note-state
  7942. (concat "\"" org-log-note-state "\"")
  7943. "")))))
  7944. (if lines (setq note (concat note " \\\\")))
  7945. (push note lines))
  7946. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7947. (when lines
  7948. (save-excursion
  7949. (set-buffer (marker-buffer org-log-note-marker))
  7950. (save-excursion
  7951. (goto-char org-log-note-marker)
  7952. (move-marker org-log-note-marker nil)
  7953. (end-of-line 1)
  7954. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7955. (indent-relative nil)
  7956. (insert "- " (pop lines))
  7957. (org-indent-line-function)
  7958. (beginning-of-line 1)
  7959. (looking-at "[ \t]*")
  7960. (setq ind (concat (match-string 0) " "))
  7961. (end-of-line 1)
  7962. (while lines (insert "\n" ind (pop lines)))))))
  7963. (set-window-configuration org-log-note-window-configuration)
  7964. (with-current-buffer (marker-buffer org-log-note-return-to)
  7965. (goto-char org-log-note-return-to))
  7966. (move-marker org-log-note-return-to nil)
  7967. (and org-log-post-message (message "%s" org-log-post-message)))
  7968. (defun org-sparse-tree (&optional arg)
  7969. "Create a sparse tree, prompt for the details.
  7970. This command can create sparse trees. You first need to select the type
  7971. of match used to create the tree:
  7972. t Show entries with a specific TODO keyword.
  7973. T Show entries selected by a tags match.
  7974. p Enter a property name and its value (both with completion on existing
  7975. names/values) and show entries with that property.
  7976. r Show entries matching a regular expression
  7977. d Show deadlines due within `org-deadline-warning-days'."
  7978. (interactive "P")
  7979. (let (ans kwd value)
  7980. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7981. (setq ans (read-char-exclusive))
  7982. (cond
  7983. ((equal ans ?d)
  7984. (call-interactively 'org-check-deadlines))
  7985. ((equal ans ?b)
  7986. (call-interactively 'org-check-before-date))
  7987. ((equal ans ?t)
  7988. (org-show-todo-tree '(4)))
  7989. ((equal ans ?T)
  7990. (call-interactively 'org-tags-sparse-tree))
  7991. ((member ans '(?p ?P))
  7992. (setq kwd (org-ido-completing-read "Property: "
  7993. (mapcar 'list (org-buffer-property-keys))))
  7994. (setq value (org-ido-completing-read "Value: "
  7995. (mapcar 'list (org-property-values kwd))))
  7996. (unless (string-match "\\`{.*}\\'" value)
  7997. (setq value (concat "\"" value "\"")))
  7998. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7999. ((member ans '(?r ?R ?/))
  8000. (call-interactively 'org-occur))
  8001. (t (error "No such sparse tree command \"%c\"" ans)))))
  8002. (defvar org-occur-highlights nil
  8003. "List of overlays used for occur matches.")
  8004. (make-variable-buffer-local 'org-occur-highlights)
  8005. (defvar org-occur-parameters nil
  8006. "Parameters of the active org-occur calls.
  8007. This is a list, each call to org-occur pushes as cons cell,
  8008. containing the regular expression and the callback, onto the list.
  8009. The list can contain several entries if `org-occur' has been called
  8010. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8011. will only contain one set of parameters. When the highlights are
  8012. removed (for example with `C-c C-c', or with the next edit (depending
  8013. on `org-remove-highlights-with-change'), this variable is emptied
  8014. as well.")
  8015. (make-variable-buffer-local 'org-occur-parameters)
  8016. (defun org-occur (regexp &optional keep-previous callback)
  8017. "Make a compact tree which shows all matches of REGEXP.
  8018. The tree will show the lines where the regexp matches, and all higher
  8019. headlines above the match. It will also show the heading after the match,
  8020. to make sure editing the matching entry is easy.
  8021. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8022. call to `org-occur' will be kept, to allow stacking of calls to this
  8023. command.
  8024. If CALLBACK is non-nil, it is a function which is called to confirm
  8025. that the match should indeed be shown."
  8026. (interactive "sRegexp: \nP")
  8027. (unless keep-previous
  8028. (org-remove-occur-highlights nil nil t))
  8029. (push (cons regexp callback) org-occur-parameters)
  8030. (let ((cnt 0))
  8031. (save-excursion
  8032. (goto-char (point-min))
  8033. (if (or (not keep-previous) ; do not want to keep
  8034. (not org-occur-highlights)) ; no previous matches
  8035. ;; hide everything
  8036. (org-overview))
  8037. (while (re-search-forward regexp nil t)
  8038. (when (or (not callback)
  8039. (save-match-data (funcall callback)))
  8040. (setq cnt (1+ cnt))
  8041. (when org-highlight-sparse-tree-matches
  8042. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8043. (org-show-context 'occur-tree))))
  8044. (when org-remove-highlights-with-change
  8045. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8046. nil 'local))
  8047. (unless org-sparse-tree-open-archived-trees
  8048. (org-hide-archived-subtrees (point-min) (point-max)))
  8049. (run-hooks 'org-occur-hook)
  8050. (if (interactive-p)
  8051. (message "%d match(es) for regexp %s" cnt regexp))
  8052. cnt))
  8053. (defun org-show-context (&optional key)
  8054. "Make sure point and context and visible.
  8055. How much context is shown depends upon the variables
  8056. `org-show-hierarchy-above', `org-show-following-heading'. and
  8057. `org-show-siblings'."
  8058. (let ((heading-p (org-on-heading-p t))
  8059. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8060. (following-p (org-get-alist-option org-show-following-heading key))
  8061. (entry-p (org-get-alist-option org-show-entry-below key))
  8062. (siblings-p (org-get-alist-option org-show-siblings key)))
  8063. (catch 'exit
  8064. ;; Show heading or entry text
  8065. (if (and heading-p (not entry-p))
  8066. (org-flag-heading nil) ; only show the heading
  8067. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8068. (org-show-hidden-entry))) ; show entire entry
  8069. (when following-p
  8070. ;; Show next sibling, or heading below text
  8071. (save-excursion
  8072. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8073. (org-flag-heading nil))))
  8074. (when siblings-p (org-show-siblings))
  8075. (when hierarchy-p
  8076. ;; show all higher headings, possibly with siblings
  8077. (save-excursion
  8078. (while (and (condition-case nil
  8079. (progn (org-up-heading-all 1) t)
  8080. (error nil))
  8081. (not (bobp)))
  8082. (org-flag-heading nil)
  8083. (when siblings-p (org-show-siblings))))))))
  8084. (defun org-reveal (&optional siblings)
  8085. "Show current entry, hierarchy above it, and the following headline.
  8086. This can be used to show a consistent set of context around locations
  8087. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8088. not t for the search context.
  8089. With optional argument SIBLINGS, on each level of the hierarchy all
  8090. siblings are shown. This repairs the tree structure to what it would
  8091. look like when opened with hierarchical calls to `org-cycle'."
  8092. (interactive "P")
  8093. (let ((org-show-hierarchy-above t)
  8094. (org-show-following-heading t)
  8095. (org-show-siblings (if siblings t org-show-siblings)))
  8096. (org-show-context nil)))
  8097. (defun org-highlight-new-match (beg end)
  8098. "Highlight from BEG to END and mark the highlight is an occur headline."
  8099. (let ((ov (org-make-overlay beg end)))
  8100. (org-overlay-put ov 'face 'secondary-selection)
  8101. (push ov org-occur-highlights)))
  8102. (defun org-remove-occur-highlights (&optional beg end noremove)
  8103. "Remove the occur highlights from the buffer.
  8104. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8105. from the `before-change-functions' in the current buffer."
  8106. (interactive)
  8107. (unless org-inhibit-highlight-removal
  8108. (mapc 'org-delete-overlay org-occur-highlights)
  8109. (setq org-occur-highlights nil)
  8110. (setq org-occur-parameters nil)
  8111. (unless noremove
  8112. (remove-hook 'before-change-functions
  8113. 'org-remove-occur-highlights 'local))))
  8114. ;;;; Priorities
  8115. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8116. "Regular expression matching the priority indicator.")
  8117. (defvar org-remove-priority-next-time nil)
  8118. (defun org-priority-up ()
  8119. "Increase the priority of the current item."
  8120. (interactive)
  8121. (org-priority 'up))
  8122. (defun org-priority-down ()
  8123. "Decrease the priority of the current item."
  8124. (interactive)
  8125. (org-priority 'down))
  8126. (defun org-priority (&optional action)
  8127. "Change the priority of an item by ARG.
  8128. ACTION can be `set', `up', `down', or a character."
  8129. (interactive)
  8130. (setq action (or action 'set))
  8131. (let (current new news have remove)
  8132. (save-excursion
  8133. (org-back-to-heading)
  8134. (if (looking-at org-priority-regexp)
  8135. (setq current (string-to-char (match-string 2))
  8136. have t)
  8137. (setq current org-default-priority))
  8138. (cond
  8139. ((or (eq action 'set)
  8140. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8141. (if (not (eq action 'set))
  8142. (setq new action)
  8143. (message "Priority %c-%c, SPC to remove: "
  8144. org-highest-priority org-lowest-priority)
  8145. (setq new (read-char-exclusive)))
  8146. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8147. (= (upcase org-lowest-priority) org-lowest-priority))
  8148. (setq new (upcase new)))
  8149. (cond ((equal new ?\ ) (setq remove t))
  8150. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8151. (error "Priority must be between `%c' and `%c'"
  8152. org-highest-priority org-lowest-priority))))
  8153. ((eq action 'up)
  8154. (if (and (not have) (eq last-command this-command))
  8155. (setq new org-lowest-priority)
  8156. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8157. org-default-priority (1- current)))))
  8158. ((eq action 'down)
  8159. (if (and (not have) (eq last-command this-command))
  8160. (setq new org-highest-priority)
  8161. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8162. org-default-priority (1+ current)))))
  8163. (t (error "Invalid action")))
  8164. (if (or (< (upcase new) org-highest-priority)
  8165. (> (upcase new) org-lowest-priority))
  8166. (setq remove t))
  8167. (setq news (format "%c" new))
  8168. (if have
  8169. (if remove
  8170. (replace-match "" t t nil 1)
  8171. (replace-match news t t nil 2))
  8172. (if remove
  8173. (error "No priority cookie found in line")
  8174. (looking-at org-todo-line-regexp)
  8175. (if (match-end 2)
  8176. (progn
  8177. (goto-char (match-end 2))
  8178. (insert " [#" news "]"))
  8179. (goto-char (match-beginning 3))
  8180. (insert "[#" news "] ")))))
  8181. (org-preserve-lc (org-set-tags nil 'align))
  8182. (if remove
  8183. (message "Priority removed")
  8184. (message "Priority of current item set to %s" news))))
  8185. (defun org-get-priority (s)
  8186. "Find priority cookie and return priority."
  8187. (save-match-data
  8188. (if (not (string-match org-priority-regexp s))
  8189. (* 1000 (- org-lowest-priority org-default-priority))
  8190. (* 1000 (- org-lowest-priority
  8191. (string-to-char (match-string 2 s)))))))
  8192. ;;;; Tags
  8193. (defvar org-agenda-archives-mode)
  8194. (defun org-scan-tags (action matcher &optional todo-only)
  8195. "Scan headline tags with inheritance and produce output ACTION.
  8196. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8197. or `agenda' to produce an entry list for an agenda view. It can also be
  8198. a Lisp form or a function that should be called at each matched headline, in
  8199. this case the return value is a list of all return values from these calls.
  8200. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8201. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8202. only lines with a TODO keyword are included in the output."
  8203. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8204. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8205. (org-re
  8206. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8207. (props (list 'face 'default
  8208. 'done-face 'org-done
  8209. 'undone-face 'default
  8210. 'mouse-face 'highlight
  8211. 'org-not-done-regexp org-not-done-regexp
  8212. 'org-todo-regexp org-todo-regexp
  8213. 'keymap org-agenda-keymap
  8214. 'help-echo
  8215. (format "mouse-2 or RET jump to org file %s"
  8216. (abbreviate-file-name
  8217. (or (buffer-file-name (buffer-base-buffer))
  8218. (buffer-name (buffer-base-buffer)))))))
  8219. (case-fold-search nil)
  8220. lspos tags tags-list
  8221. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8222. (llast 0) rtn rtn1 level category i txt
  8223. todo marker entry priority)
  8224. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8225. (setq action (list 'lambda nil action)))
  8226. (save-excursion
  8227. (goto-char (point-min))
  8228. (when (eq action 'sparse-tree)
  8229. (org-overview)
  8230. (org-remove-occur-highlights))
  8231. (while (re-search-forward re nil t)
  8232. (catch :skip
  8233. (setq todo (if (match-end 1) (match-string 2))
  8234. tags (if (match-end 4) (match-string 4)))
  8235. (goto-char (setq lspos (1+ (match-beginning 0))))
  8236. (setq level (org-reduced-level (funcall outline-level))
  8237. category (org-get-category))
  8238. (setq i llast llast level)
  8239. ;; remove tag lists from same and sublevels
  8240. (while (>= i level)
  8241. (when (setq entry (assoc i tags-alist))
  8242. (setq tags-alist (delete entry tags-alist)))
  8243. (setq i (1- i)))
  8244. ;; add the next tags
  8245. (when tags
  8246. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8247. tags-alist
  8248. (cons (cons level tags) tags-alist)))
  8249. ;; compile tags for current headline
  8250. (setq tags-list
  8251. (if org-use-tag-inheritance
  8252. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8253. tags))
  8254. (when org-use-tag-inheritance
  8255. (setcdr (car tags-alist)
  8256. (mapcar (lambda (x)
  8257. (setq x (copy-sequence x))
  8258. (org-add-prop-inherited x))
  8259. (cdar tags-alist))))
  8260. (when (and tags org-use-tag-inheritance
  8261. (not (eq t org-use-tag-inheritance)))
  8262. ;; selective inheritance, remove uninherited ones
  8263. (setcdr (car tags-alist)
  8264. (org-remove-uniherited-tags (cdar tags-alist))))
  8265. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8266. (let ((case-fold-search t)) (eval matcher))
  8267. (or
  8268. (not (member org-archive-tag tags-list))
  8269. ;; we have an archive tag, should we use this anyway?
  8270. (or (not org-agenda-skip-archived-trees)
  8271. (and (eq action 'agenda) org-agenda-archives-mode))))
  8272. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8273. ;; select this headline
  8274. (cond
  8275. ((eq action 'sparse-tree)
  8276. (and org-highlight-sparse-tree-matches
  8277. (org-get-heading) (match-end 0)
  8278. (org-highlight-new-match
  8279. (match-beginning 0) (match-beginning 1)))
  8280. (org-show-context 'tags-tree))
  8281. ((eq action 'agenda)
  8282. (setq txt (org-format-agenda-item
  8283. ""
  8284. (concat
  8285. (if org-tags-match-list-sublevels
  8286. (make-string (1- level) ?.) "")
  8287. (org-get-heading))
  8288. category tags-list)
  8289. priority (org-get-priority txt))
  8290. (goto-char lspos)
  8291. (setq marker (org-agenda-new-marker))
  8292. (org-add-props txt props
  8293. 'org-marker marker 'org-hd-marker marker 'org-category category
  8294. 'priority priority 'type "tagsmatch")
  8295. (push txt rtn))
  8296. ((functionp action)
  8297. (save-excursion
  8298. (setq rtn1 (funcall action))
  8299. (push rtn1 rtn))
  8300. (goto-char (point-at-eol)))
  8301. (t (error "Invalid action")))
  8302. ;; if we are to skip sublevels, jump to end of subtree
  8303. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8304. (when (and (eq action 'sparse-tree)
  8305. (not org-sparse-tree-open-archived-trees))
  8306. (org-hide-archived-subtrees (point-min) (point-max)))
  8307. (nreverse rtn)))
  8308. (defun org-remove-uniherited-tags (tags)
  8309. "Remove all tags that are not inherited from the list TAGS."
  8310. (cond
  8311. ((eq org-use-tag-inheritance t)
  8312. (if org-tags-exclude-from-inheritance
  8313. (org-delete-all org-tags-exclude-from-inheritance tags)
  8314. tags))
  8315. ((not org-use-tag-inheritance) nil)
  8316. ((stringp org-use-tag-inheritance)
  8317. (delq nil (mapcar
  8318. (lambda (x)
  8319. (if (and (string-match org-use-tag-inheritance x)
  8320. (not (member x org-tags-exclude-from-inheritance)))
  8321. x nil))
  8322. tags)))
  8323. ((listp org-use-tag-inheritance)
  8324. (delq nil (mapcar
  8325. (lambda (x)
  8326. (if (member x org-use-tag-inheritance) x nil))
  8327. tags)))))
  8328. (defvar todo-only) ;; dynamically scoped
  8329. (defun org-tags-sparse-tree (&optional todo-only match)
  8330. "Create a sparse tree according to tags string MATCH.
  8331. MATCH can contain positive and negative selection of tags, like
  8332. \"+WORK+URGENT-WITHBOSS\".
  8333. If optional argument TODO-ONLY is non-nil, only select lines that are
  8334. also TODO lines."
  8335. (interactive "P")
  8336. (org-prepare-agenda-buffers (list (current-buffer)))
  8337. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8338. (defvar org-cached-props nil)
  8339. (defun org-cached-entry-get (pom property)
  8340. (if (or (eq t org-use-property-inheritance)
  8341. (and (stringp org-use-property-inheritance)
  8342. (string-match org-use-property-inheritance property))
  8343. (and (listp org-use-property-inheritance)
  8344. (member property org-use-property-inheritance)))
  8345. ;; Caching is not possible, check it directly
  8346. (org-entry-get pom property 'inherit)
  8347. ;; Get all properties, so that we can do complicated checks easily
  8348. (cdr (assoc property (or org-cached-props
  8349. (setq org-cached-props
  8350. (org-entry-properties pom)))))))
  8351. (defun org-global-tags-completion-table (&optional files)
  8352. "Return the list of all tags in all agenda buffer/files."
  8353. (save-excursion
  8354. (org-uniquify
  8355. (delq nil
  8356. (apply 'append
  8357. (mapcar
  8358. (lambda (file)
  8359. (set-buffer (find-file-noselect file))
  8360. (append (org-get-buffer-tags)
  8361. (mapcar (lambda (x) (if (stringp (car-safe x))
  8362. (list (car-safe x)) nil))
  8363. org-tag-alist)))
  8364. (if (and files (car files))
  8365. files
  8366. (org-agenda-files))))))))
  8367. (defun org-make-tags-matcher (match)
  8368. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8369. ;; todo-only is scoped dynamically into this function, and the function
  8370. ;; may change it it the matcher asksk for it.
  8371. (unless match
  8372. ;; Get a new match request, with completion
  8373. (let ((org-last-tags-completion-table
  8374. (org-global-tags-completion-table)))
  8375. (setq match (org-ido-completing-read
  8376. "Match: " 'org-tags-completion-function nil nil nil
  8377. 'org-tags-history))))
  8378. ;; Parse the string and create a lisp form
  8379. (let ((match0 match)
  8380. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8381. minus tag mm
  8382. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8383. orterms term orlist re-p str-p level-p level-op time-p
  8384. prop-p pn pv po cat-p gv rest)
  8385. (if (string-match "/+" match)
  8386. ;; match contains also a todo-matching request
  8387. (progn
  8388. (setq tagsmatch (substring match 0 (match-beginning 0))
  8389. todomatch (substring match (match-end 0)))
  8390. (if (string-match "^!" todomatch)
  8391. (setq todo-only t todomatch (substring todomatch 1)))
  8392. (if (string-match "^\\s-*$" todomatch)
  8393. (setq todomatch nil)))
  8394. ;; only matching tags
  8395. (setq tagsmatch match todomatch nil))
  8396. ;; Make the tags matcher
  8397. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8398. (setq tagsmatcher t)
  8399. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8400. (while (setq term (pop orterms))
  8401. (while (and (equal (substring term -1) "\\") orterms)
  8402. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8403. (while (string-match re term)
  8404. (setq rest (substring term (match-end 0))
  8405. minus (and (match-end 1)
  8406. (equal (match-string 1 term) "-"))
  8407. tag (match-string 2 term)
  8408. re-p (equal (string-to-char tag) ?{)
  8409. level-p (match-end 4)
  8410. prop-p (match-end 5)
  8411. mm (cond
  8412. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8413. (level-p
  8414. (setq level-op (org-op-to-function (match-string 3 term)))
  8415. `(,level-op level ,(string-to-number
  8416. (match-string 4 term))))
  8417. (prop-p
  8418. (setq pn (match-string 5 term)
  8419. po (match-string 6 term)
  8420. pv (match-string 7 term)
  8421. cat-p (equal pn "CATEGORY")
  8422. re-p (equal (string-to-char pv) ?{)
  8423. str-p (equal (string-to-char pv) ?\")
  8424. time-p (save-match-data
  8425. (string-match "^\"[[<].*[]>]\"$" pv))
  8426. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8427. (if time-p (setq pv (org-matcher-time pv)))
  8428. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8429. (cond
  8430. ((equal pn "CATEGORY")
  8431. (setq gv '(get-text-property (point) 'org-category)))
  8432. ((equal pn "TODO")
  8433. (setq gv 'todo))
  8434. (t
  8435. (setq gv `(org-cached-entry-get nil ,pn))))
  8436. (if re-p
  8437. (if (eq po 'org<>)
  8438. `(not (string-match ,pv (or ,gv "")))
  8439. `(string-match ,pv (or ,gv "")))
  8440. (if str-p
  8441. `(,po (or ,gv "") ,pv)
  8442. `(,po (string-to-number (or ,gv ""))
  8443. ,(string-to-number pv) ))))
  8444. (t `(member ,(downcase tag) tags-list)))
  8445. mm (if minus (list 'not mm) mm)
  8446. term rest)
  8447. (push mm tagsmatcher))
  8448. (push (if (> (length tagsmatcher) 1)
  8449. (cons 'and tagsmatcher)
  8450. (car tagsmatcher))
  8451. orlist)
  8452. (setq tagsmatcher nil))
  8453. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8454. (setq tagsmatcher
  8455. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8456. ;; Make the todo matcher
  8457. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8458. (setq todomatcher t)
  8459. (setq orterms (org-split-string todomatch "|") orlist nil)
  8460. (while (setq term (pop orterms))
  8461. (while (string-match re term)
  8462. (setq minus (and (match-end 1)
  8463. (equal (match-string 1 term) "-"))
  8464. kwd (match-string 2 term)
  8465. re-p (equal (string-to-char kwd) ?{)
  8466. term (substring term (match-end 0))
  8467. mm (if re-p
  8468. `(string-match ,(substring kwd 1 -1) todo)
  8469. (list 'equal 'todo kwd))
  8470. mm (if minus (list 'not mm) mm))
  8471. (push mm todomatcher))
  8472. (push (if (> (length todomatcher) 1)
  8473. (cons 'and todomatcher)
  8474. (car todomatcher))
  8475. orlist)
  8476. (setq todomatcher nil))
  8477. (setq todomatcher (if (> (length orlist) 1)
  8478. (cons 'or orlist) (car orlist))))
  8479. ;; Return the string and lisp forms of the matcher
  8480. (setq matcher (if todomatcher
  8481. (list 'and tagsmatcher todomatcher)
  8482. tagsmatcher))
  8483. (cons match0 matcher)))
  8484. (defun org-op-to-function (op &optional stringp)
  8485. "Turn an operator into the appropriate function."
  8486. (setq op
  8487. (cond
  8488. ((equal op "<" ) '(< string< org-time<))
  8489. ((equal op ">" ) '(> org-string> org-time>))
  8490. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8491. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8492. ((member op '("=" "==")) '(= string= org-time=))
  8493. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8494. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8495. (defun org<> (a b) (not (= a b)))
  8496. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8497. (defun org-string>= (a b) (not (string< a b)))
  8498. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8499. (defun org-string<> (a b) (not (string= a b)))
  8500. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8501. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8502. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8503. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8504. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8505. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8506. (defun org-2ft (s)
  8507. "Convert S to a floating point time.
  8508. If S is already a number, just return it. If it is a string, parse
  8509. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8510. (cond
  8511. ((numberp s) s)
  8512. ((stringp s)
  8513. (condition-case nil
  8514. (float-time (apply 'encode-time (org-parse-time-string s)))
  8515. (error 0.)))
  8516. (t 0.)))
  8517. (defun org-time-today ()
  8518. "Time in seconds today at 0:00.
  8519. Returns the float number of seconds since the beginning of the
  8520. epoch to the beginning of today (00:00)."
  8521. (float-time (apply 'encode-time
  8522. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8523. (defun org-matcher-time (s)
  8524. "Interprete a time comparison value."
  8525. (save-match-data
  8526. (cond
  8527. ((string= s "<now>") (float-time))
  8528. ((string= s "<today>") (org-time-today))
  8529. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8530. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8531. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8532. (+ (org-time-today)
  8533. (* (string-to-number (match-string 1 s))
  8534. (cdr (assoc (match-string 2 s)
  8535. '(("d" . 86400.0) ("w" . 604800.0)
  8536. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8537. (t (org-2ft s)))))
  8538. (defun org-match-any-p (re list)
  8539. "Does re match any element of list?"
  8540. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8541. (delq nil list))
  8542. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8543. (defvar org-tags-overlay (org-make-overlay 1 1))
  8544. (org-detach-overlay org-tags-overlay)
  8545. (defun org-get-local-tags-at (&optional pos)
  8546. "Get a list of tags defined in the current headline."
  8547. (org-get-tags-at pos 'local))
  8548. (defun org-get-local-tags ()
  8549. "Get a list of tags defined in the current headline."
  8550. (org-get-tags-at nil 'local))
  8551. (defun org-get-tags-at (&optional pos local)
  8552. "Get a list of all headline tags applicable at POS.
  8553. POS defaults to point. If tags are inherited, the list contains
  8554. the targets in the same sequence as the headlines appear, i.e.
  8555. the tags of the current headline come last.
  8556. When LOCAL is non-nil, only return tags from the current headline,
  8557. ignore inherited ones."
  8558. (interactive)
  8559. (let (tags ltags lastpos parent)
  8560. (save-excursion
  8561. (save-restriction
  8562. (widen)
  8563. (goto-char (or pos (point)))
  8564. (save-match-data
  8565. (catch 'done
  8566. (condition-case nil
  8567. (progn
  8568. (org-back-to-heading t)
  8569. (while (not (equal lastpos (point)))
  8570. (setq lastpos (point))
  8571. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8572. (setq ltags (org-split-string
  8573. (org-match-string-no-properties 1) ":"))
  8574. (when parent
  8575. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8576. (setq tags (append
  8577. (if parent
  8578. (org-remove-uniherited-tags ltags)
  8579. ltags)
  8580. tags)))
  8581. (or org-use-tag-inheritance (throw 'done t))
  8582. (if local (throw 'done t))
  8583. (org-up-heading-all 1)
  8584. (setq parent t)))
  8585. (error nil)))))
  8586. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8587. (defun org-add-prop-inherited (s)
  8588. (add-text-properties 0 (length s) '(inherited t) s)
  8589. s)
  8590. (defun org-toggle-tag (tag &optional onoff)
  8591. "Toggle the tag TAG for the current line.
  8592. If ONOFF is `on' or `off', don't toggle but set to this state."
  8593. (unless (org-on-heading-p t) (error "Not on headling"))
  8594. (let (res current)
  8595. (save-excursion
  8596. (beginning-of-line)
  8597. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8598. (point-at-eol) t)
  8599. (progn
  8600. (setq current (match-string 1))
  8601. (replace-match ""))
  8602. (setq current ""))
  8603. (setq current (nreverse (org-split-string current ":")))
  8604. (cond
  8605. ((eq onoff 'on)
  8606. (setq res t)
  8607. (or (member tag current) (push tag current)))
  8608. ((eq onoff 'off)
  8609. (or (not (member tag current)) (setq current (delete tag current))))
  8610. (t (if (member tag current)
  8611. (setq current (delete tag current))
  8612. (setq res t)
  8613. (push tag current))))
  8614. (end-of-line 1)
  8615. (if current
  8616. (progn
  8617. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8618. (org-set-tags nil t))
  8619. (delete-horizontal-space))
  8620. (run-hooks 'org-after-tags-change-hook))
  8621. res))
  8622. (defun org-align-tags-here (to-col)
  8623. ;; Assumes that this is a headline
  8624. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8625. (beginning-of-line 1)
  8626. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8627. (< pos (match-beginning 2)))
  8628. (progn
  8629. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8630. (goto-char (match-beginning 1))
  8631. (insert " ")
  8632. (delete-region (point) (1+ (match-beginning 2)))
  8633. (setq ncol (max (1+ (current-column))
  8634. (1+ col)
  8635. (if (> to-col 0)
  8636. to-col
  8637. (- (abs to-col) tags-l))))
  8638. (setq p (point))
  8639. (insert (make-string (- ncol (current-column)) ?\ ))
  8640. (setq ncol (current-column))
  8641. (when indent-tabs-mode (tabify p (point-at-eol)))
  8642. (org-move-to-column (min ncol col) t))
  8643. (goto-char pos))))
  8644. (defun org-set-tags-command (&optional arg just-align)
  8645. "Call the set-tags command for the current entry."
  8646. (interactive "P")
  8647. (if (org-on-heading-p)
  8648. (org-set-tags arg just-align)
  8649. (save-excursion
  8650. (org-back-to-heading t)
  8651. (org-set-tags arg just-align))))
  8652. (defun org-set-tags (&optional arg just-align)
  8653. "Set the tags for the current headline.
  8654. With prefix ARG, realign all tags in headings in the current buffer."
  8655. (interactive "P")
  8656. (let* ((re (concat "^" outline-regexp))
  8657. (current (org-get-tags-string))
  8658. (col (current-column))
  8659. (org-setting-tags t)
  8660. table current-tags inherited-tags ; computed below when needed
  8661. tags p0 c0 c1 rpl)
  8662. (if arg
  8663. (save-excursion
  8664. (goto-char (point-min))
  8665. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8666. (while (re-search-forward re nil t)
  8667. (org-set-tags nil t)
  8668. (end-of-line 1)))
  8669. (message "All tags realigned to column %d" org-tags-column))
  8670. (if just-align
  8671. (setq tags current)
  8672. ;; Get a new set of tags from the user
  8673. (save-excursion
  8674. (setq table (or org-tag-alist (org-get-buffer-tags))
  8675. org-last-tags-completion-table table
  8676. current-tags (org-split-string current ":")
  8677. inherited-tags (nreverse
  8678. (nthcdr (length current-tags)
  8679. (nreverse (org-get-tags-at))))
  8680. tags
  8681. (if (or (eq t org-use-fast-tag-selection)
  8682. (and org-use-fast-tag-selection
  8683. (delq nil (mapcar 'cdr table))))
  8684. (org-fast-tag-selection
  8685. current-tags inherited-tags table
  8686. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8687. (let ((org-add-colon-after-tag-completion t))
  8688. (org-trim
  8689. (org-without-partial-completion
  8690. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8691. nil nil current 'org-tags-history)))))))
  8692. (while (string-match "[-+&]+" tags)
  8693. ;; No boolean logic, just a list
  8694. (setq tags (replace-match ":" t t tags))))
  8695. (if (string-match "\\`[\t ]*\\'" tags)
  8696. (setq tags "")
  8697. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8698. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8699. ;; Insert new tags at the correct column
  8700. (beginning-of-line 1)
  8701. (cond
  8702. ((and (equal current "") (equal tags "")))
  8703. ((re-search-forward
  8704. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8705. (point-at-eol) t)
  8706. (if (equal tags "")
  8707. (setq rpl "")
  8708. (goto-char (match-beginning 0))
  8709. (setq c0 (current-column) p0 (point)
  8710. c1 (max (1+ c0) (if (> org-tags-column 0)
  8711. org-tags-column
  8712. (- (- org-tags-column) (length tags))))
  8713. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8714. (replace-match rpl t t)
  8715. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8716. tags)
  8717. (t (error "Tags alignment failed")))
  8718. (org-move-to-column col)
  8719. (unless just-align
  8720. (run-hooks 'org-after-tags-change-hook)))))
  8721. (defun org-change-tag-in-region (beg end tag off)
  8722. "Add or remove TAG for each entry in the region.
  8723. This works in the agenda, and also in an org-mode buffer."
  8724. (interactive
  8725. (list (region-beginning) (region-end)
  8726. (let ((org-last-tags-completion-table
  8727. (if (org-mode-p)
  8728. (org-get-buffer-tags)
  8729. (org-global-tags-completion-table))))
  8730. (org-ido-completing-read
  8731. "Tag: " 'org-tags-completion-function nil nil nil
  8732. 'org-tags-history))
  8733. (progn
  8734. (message "[s]et or [r]emove? ")
  8735. (equal (read-char-exclusive) ?r))))
  8736. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8737. (let ((agendap (equal major-mode 'org-agenda-mode))
  8738. l1 l2 m buf pos newhead (cnt 0))
  8739. (goto-char end)
  8740. (setq l2 (1- (org-current-line)))
  8741. (goto-char beg)
  8742. (setq l1 (org-current-line))
  8743. (loop for l from l1 to l2 do
  8744. (goto-line l)
  8745. (setq m (get-text-property (point) 'org-hd-marker))
  8746. (when (or (and (org-mode-p) (org-on-heading-p))
  8747. (and agendap m))
  8748. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8749. pos (if agendap m (point)))
  8750. (with-current-buffer buf
  8751. (save-excursion
  8752. (save-restriction
  8753. (goto-char pos)
  8754. (setq cnt (1+ cnt))
  8755. (org-toggle-tag tag (if off 'off 'on))
  8756. (setq newhead (org-get-heading)))))
  8757. (and agendap (org-agenda-change-all-lines newhead m))))
  8758. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8759. (defun org-tags-completion-function (string predicate &optional flag)
  8760. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8761. (confirm (lambda (x) (stringp (car x)))))
  8762. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8763. (setq s1 (match-string 1 string)
  8764. s2 (match-string 2 string))
  8765. (setq s1 "" s2 string))
  8766. (cond
  8767. ((eq flag nil)
  8768. ;; try completion
  8769. (setq rtn (try-completion s2 ctable confirm))
  8770. (if (stringp rtn)
  8771. (setq rtn
  8772. (concat s1 s2 (substring rtn (length s2))
  8773. (if (and org-add-colon-after-tag-completion
  8774. (assoc rtn ctable))
  8775. ":" ""))))
  8776. rtn)
  8777. ((eq flag t)
  8778. ;; all-completions
  8779. (all-completions s2 ctable confirm)
  8780. )
  8781. ((eq flag 'lambda)
  8782. ;; exact match?
  8783. (assoc s2 ctable)))
  8784. ))
  8785. (defun org-fast-tag-insert (kwd tags face &optional end)
  8786. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8787. (insert (format "%-12s" (concat kwd ":"))
  8788. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8789. (or end "")))
  8790. (defun org-fast-tag-show-exit (flag)
  8791. (save-excursion
  8792. (goto-line 3)
  8793. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8794. (replace-match ""))
  8795. (when flag
  8796. (end-of-line 1)
  8797. (org-move-to-column (- (window-width) 19) t)
  8798. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8799. (defun org-set-current-tags-overlay (current prefix)
  8800. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8801. (if (featurep 'xemacs)
  8802. (org-overlay-display org-tags-overlay (concat prefix s)
  8803. 'secondary-selection)
  8804. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8805. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8806. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8807. "Fast tag selection with single keys.
  8808. CURRENT is the current list of tags in the headline, INHERITED is the
  8809. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8810. possibly with grouping information. TODO-TABLE is a similar table with
  8811. TODO keywords, should these have keys assigned to them.
  8812. If the keys are nil, a-z are automatically assigned.
  8813. Returns the new tags string, or nil to not change the current settings."
  8814. (let* ((fulltable (append table todo-table))
  8815. (maxlen (apply 'max (mapcar
  8816. (lambda (x)
  8817. (if (stringp (car x)) (string-width (car x)) 0))
  8818. fulltable)))
  8819. (buf (current-buffer))
  8820. (expert (eq org-fast-tag-selection-single-key 'expert))
  8821. (buffer-tags nil)
  8822. (fwidth (+ maxlen 3 1 3))
  8823. (ncol (/ (- (window-width) 4) fwidth))
  8824. (i-face 'org-done)
  8825. (c-face 'org-todo)
  8826. tg cnt e c char c1 c2 ntable tbl rtn
  8827. ov-start ov-end ov-prefix
  8828. (exit-after-next org-fast-tag-selection-single-key)
  8829. (done-keywords org-done-keywords)
  8830. groups ingroup)
  8831. (save-excursion
  8832. (beginning-of-line 1)
  8833. (if (looking-at
  8834. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8835. (setq ov-start (match-beginning 1)
  8836. ov-end (match-end 1)
  8837. ov-prefix "")
  8838. (setq ov-start (1- (point-at-eol))
  8839. ov-end (1+ ov-start))
  8840. (skip-chars-forward "^\n\r")
  8841. (setq ov-prefix
  8842. (concat
  8843. (buffer-substring (1- (point)) (point))
  8844. (if (> (current-column) org-tags-column)
  8845. " "
  8846. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8847. (org-move-overlay org-tags-overlay ov-start ov-end)
  8848. (save-window-excursion
  8849. (if expert
  8850. (set-buffer (get-buffer-create " *Org tags*"))
  8851. (delete-other-windows)
  8852. (split-window-vertically)
  8853. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8854. (erase-buffer)
  8855. (org-set-local 'org-done-keywords done-keywords)
  8856. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8857. (org-fast-tag-insert "Current" current c-face "\n\n")
  8858. (org-fast-tag-show-exit exit-after-next)
  8859. (org-set-current-tags-overlay current ov-prefix)
  8860. (setq tbl fulltable char ?a cnt 0)
  8861. (while (setq e (pop tbl))
  8862. (cond
  8863. ((equal e '(:startgroup))
  8864. (push '() groups) (setq ingroup t)
  8865. (when (not (= cnt 0))
  8866. (setq cnt 0)
  8867. (insert "\n"))
  8868. (insert "{ "))
  8869. ((equal e '(:endgroup))
  8870. (setq ingroup nil cnt 0)
  8871. (insert "}\n"))
  8872. (t
  8873. (setq tg (car e) c2 nil)
  8874. (if (cdr e)
  8875. (setq c (cdr e))
  8876. ;; automatically assign a character.
  8877. (setq c1 (string-to-char
  8878. (downcase (substring
  8879. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8880. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8881. (while (or (rassoc char ntable) (rassoc char table))
  8882. (setq char (1+ char)))
  8883. (setq c2 c1))
  8884. (setq c (or c2 char)))
  8885. (if ingroup (push tg (car groups)))
  8886. (setq tg (org-add-props tg nil 'face
  8887. (cond
  8888. ((not (assoc tg table))
  8889. (org-get-todo-face tg))
  8890. ((member tg current) c-face)
  8891. ((member tg inherited) i-face)
  8892. (t nil))))
  8893. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8894. (insert "[" c "] " tg (make-string
  8895. (- fwidth 4 (length tg)) ?\ ))
  8896. (push (cons tg c) ntable)
  8897. (when (= (setq cnt (1+ cnt)) ncol)
  8898. (insert "\n")
  8899. (if ingroup (insert " "))
  8900. (setq cnt 0)))))
  8901. (setq ntable (nreverse ntable))
  8902. (insert "\n")
  8903. (goto-char (point-min))
  8904. (if (not expert) (org-fit-window-to-buffer))
  8905. (setq rtn
  8906. (catch 'exit
  8907. (while t
  8908. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8909. (if groups " [!] no groups" " [!]groups")
  8910. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8911. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8912. (cond
  8913. ((= c ?\r) (throw 'exit t))
  8914. ((= c ?!)
  8915. (setq groups (not groups))
  8916. (goto-char (point-min))
  8917. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8918. ((= c ?\C-c)
  8919. (if (not expert)
  8920. (org-fast-tag-show-exit
  8921. (setq exit-after-next (not exit-after-next)))
  8922. (setq expert nil)
  8923. (delete-other-windows)
  8924. (split-window-vertically)
  8925. (org-switch-to-buffer-other-window " *Org tags*")
  8926. (org-fit-window-to-buffer)))
  8927. ((or (= c ?\C-g)
  8928. (and (= c ?q) (not (rassoc c ntable))))
  8929. (org-detach-overlay org-tags-overlay)
  8930. (setq quit-flag t))
  8931. ((= c ?\ )
  8932. (setq current nil)
  8933. (if exit-after-next (setq exit-after-next 'now)))
  8934. ((= c ?\t)
  8935. (condition-case nil
  8936. (setq tg (org-ido-completing-read
  8937. "Tag: "
  8938. (or buffer-tags
  8939. (with-current-buffer buf
  8940. (org-get-buffer-tags)))))
  8941. (quit (setq tg "")))
  8942. (when (string-match "\\S-" tg)
  8943. (add-to-list 'buffer-tags (list tg))
  8944. (if (member tg current)
  8945. (setq current (delete tg current))
  8946. (push tg current)))
  8947. (if exit-after-next (setq exit-after-next 'now)))
  8948. ((setq e (rassoc c todo-table) tg (car e))
  8949. (with-current-buffer buf
  8950. (save-excursion (org-todo tg)))
  8951. (if exit-after-next (setq exit-after-next 'now)))
  8952. ((setq e (rassoc c ntable) tg (car e))
  8953. (if (member tg current)
  8954. (setq current (delete tg current))
  8955. (loop for g in groups do
  8956. (if (member tg g)
  8957. (mapc (lambda (x)
  8958. (setq current (delete x current)))
  8959. g)))
  8960. (push tg current))
  8961. (if exit-after-next (setq exit-after-next 'now))))
  8962. ;; Create a sorted list
  8963. (setq current
  8964. (sort current
  8965. (lambda (a b)
  8966. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8967. (if (eq exit-after-next 'now) (throw 'exit t))
  8968. (goto-char (point-min))
  8969. (beginning-of-line 2)
  8970. (delete-region (point) (point-at-eol))
  8971. (org-fast-tag-insert "Current" current c-face)
  8972. (org-set-current-tags-overlay current ov-prefix)
  8973. (while (re-search-forward
  8974. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8975. (setq tg (match-string 1))
  8976. (add-text-properties
  8977. (match-beginning 1) (match-end 1)
  8978. (list 'face
  8979. (cond
  8980. ((member tg current) c-face)
  8981. ((member tg inherited) i-face)
  8982. (t (get-text-property (match-beginning 1) 'face))))))
  8983. (goto-char (point-min)))))
  8984. (org-detach-overlay org-tags-overlay)
  8985. (if rtn
  8986. (mapconcat 'identity current ":")
  8987. nil))))
  8988. (defun org-get-tags-string ()
  8989. "Get the TAGS string in the current headline."
  8990. (unless (org-on-heading-p t)
  8991. (error "Not on a heading"))
  8992. (save-excursion
  8993. (beginning-of-line 1)
  8994. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8995. (org-match-string-no-properties 1)
  8996. "")))
  8997. (defun org-get-tags ()
  8998. "Get the list of tags specified in the current headline."
  8999. (org-split-string (org-get-tags-string) ":"))
  9000. (defun org-get-buffer-tags ()
  9001. "Get a table of all tags used in the buffer, for completion."
  9002. (let (tags)
  9003. (save-excursion
  9004. (goto-char (point-min))
  9005. (while (re-search-forward
  9006. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9007. (when (equal (char-after (point-at-bol 0)) ?*)
  9008. (mapc (lambda (x) (add-to-list 'tags x))
  9009. (org-split-string (org-match-string-no-properties 1) ":")))))
  9010. (mapcar 'list tags)))
  9011. ;;;; The mapping API
  9012. ;;;###autoload
  9013. (defun org-map-entries (func &optional match scope &rest skip)
  9014. "Call FUNC at each headline selected by MATCH in SCOPE.
  9015. FUNC is a function or a lisp form. The function will be called without
  9016. arguments, with the cursor positioned at the beginning of the headline.
  9017. The return values of all calls to the function will be collected and
  9018. returned as a list.
  9019. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9020. Only headlines that are matched by this query will be considered during
  9021. the iteration. When MATCH is nil or t, all headlines will be
  9022. visited by the iteration.
  9023. SCOPE determines the scope of this command. It can be any of:
  9024. nil The current buffer, respecting the restriction if any
  9025. tree The subtree started with the entry at point
  9026. file The current buffer, without restriction
  9027. file-with-archives
  9028. The current buffer, and any archives associated with it
  9029. agenda All agenda files
  9030. agenda-with-archives
  9031. All agenda files with any archive files associated with them
  9032. \(file1 file2 ...)
  9033. If this is a list, all files in the list will be scanned
  9034. The remaining args are treated as settings for the skipping facilities of
  9035. the scanner. The following items can be given here:
  9036. archive skip trees with the archive tag.
  9037. comment skip trees with the COMMENT keyword
  9038. function or Emacs Lisp form:
  9039. will be used as value for `org-agenda-skip-function', so whenever
  9040. the the function returns t, FUNC will not be called for that
  9041. entry and search will continue from the point where the
  9042. function leaves it."
  9043. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9044. (org-agenda-skip-archived-trees (memq 'archive skip))
  9045. (org-agenda-skip-comment-trees (memq 'comment skip))
  9046. (org-agenda-skip-function
  9047. (car (org-delete-all '(comment archive) skip)))
  9048. (org-tags-match-list-sublevels t)
  9049. matcher pos file res
  9050. org-todo-keywords-for-agenda
  9051. org-done-keywords-for-agenda
  9052. org-todo-keyword-alist-for-agenda
  9053. org-tag-alist-for-agenda)
  9054. (cond
  9055. ((eq match t) (setq matcher t))
  9056. ((eq match nil) (setq matcher t))
  9057. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9058. (when (eq scope 'tree)
  9059. (org-back-to-heading t)
  9060. (org-narrow-to-subtree)
  9061. (setq scope nil))
  9062. (if (not scope)
  9063. (progn
  9064. (org-prepare-agenda-buffers
  9065. (list (buffer-file-name (current-buffer))))
  9066. (org-scan-tags func matcher))
  9067. ;; Get the right scope
  9068. (setq pos (point))
  9069. (cond
  9070. ((and scope (listp scope) (symbolp (car scope)))
  9071. (setq scope (eval scope)))
  9072. ((eq scope 'agenda)
  9073. (setq scope (org-agenda-files t)))
  9074. ((eq scope 'agenda-with-archives)
  9075. (setq scope (org-agenda-files t))
  9076. (setq scope (org-add-archive-files scope)))
  9077. ((eq scope 'file)
  9078. (setq scope (list (buffer-file-name))))
  9079. ((eq scope 'file-with-archives)
  9080. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9081. (org-prepare-agenda-buffers scope)
  9082. (while (setq file (pop scope))
  9083. (with-current-buffer (org-find-base-buffer-visiting file)
  9084. (save-excursion
  9085. (save-restriction
  9086. (widen)
  9087. (goto-char (point-min))
  9088. (setq res (append res (org-scan-tags func matcher)))))))
  9089. res)))
  9090. ;;;; Properties
  9091. ;;; Setting and retrieving properties
  9092. (defconst org-special-properties
  9093. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9094. "TIMESTAMP" "TIMESTAMP_IA")
  9095. "The special properties valid in Org-mode.
  9096. These are properties that are not defined in the property drawer,
  9097. but in some other way.")
  9098. (defconst org-default-properties
  9099. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9100. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9101. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9102. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9103. "Some properties that are used by Org-mode for various purposes.
  9104. Being in this list makes sure that they are offered for completion.")
  9105. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9106. "Regular expression matching the first line of a property drawer.")
  9107. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9108. "Regular expression matching the first line of a property drawer.")
  9109. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9110. "Regular expression matching the first line of a property drawer.")
  9111. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9112. "Regular expression matching the first line of a property drawer.")
  9113. (defconst org-property-drawer-re
  9114. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9115. org-property-end-re "\\)\n?")
  9116. "Matches an entire property drawer.")
  9117. (defconst org-clock-drawer-re
  9118. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9119. org-property-end-re "\\)\n?")
  9120. "Matches an entire clock drawer.")
  9121. (defun org-property-action ()
  9122. "Do an action on properties."
  9123. (interactive)
  9124. (let (c)
  9125. (org-at-property-p)
  9126. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9127. (setq c (read-char-exclusive))
  9128. (cond
  9129. ((equal c ?s)
  9130. (call-interactively 'org-set-property))
  9131. ((equal c ?d)
  9132. (call-interactively 'org-delete-property))
  9133. ((equal c ?D)
  9134. (call-interactively 'org-delete-property-globally))
  9135. ((equal c ?c)
  9136. (call-interactively 'org-compute-property-at-point))
  9137. (t (error "No such property action %c" c)))))
  9138. (defun org-at-property-p ()
  9139. "Is the cursor in a property line?"
  9140. ;; FIXME: Does not check if we are actually in the drawer.
  9141. ;; FIXME: also returns true on any drawers.....
  9142. ;; This is used by C-c C-c for property action.
  9143. (save-excursion
  9144. (beginning-of-line 1)
  9145. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9146. (defun org-get-property-block (&optional beg end force)
  9147. "Return the (beg . end) range of the body of the property drawer.
  9148. BEG and END can be beginning and end of subtree, if not given
  9149. they will be found.
  9150. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9151. (catch 'exit
  9152. (save-excursion
  9153. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9154. (end (or end (progn (outline-next-heading) (point)))))
  9155. (goto-char beg)
  9156. (if (re-search-forward org-property-start-re end t)
  9157. (setq beg (1+ (match-end 0)))
  9158. (if force
  9159. (save-excursion
  9160. (org-insert-property-drawer)
  9161. (setq end (progn (outline-next-heading) (point))))
  9162. (throw 'exit nil))
  9163. (goto-char beg)
  9164. (if (re-search-forward org-property-start-re end t)
  9165. (setq beg (1+ (match-end 0)))))
  9166. (if (re-search-forward org-property-end-re end t)
  9167. (setq end (match-beginning 0))
  9168. (or force (throw 'exit nil))
  9169. (goto-char beg)
  9170. (setq end beg)
  9171. (org-indent-line-function)
  9172. (insert ":END:\n"))
  9173. (cons beg end)))))
  9174. (defun org-entry-properties (&optional pom which)
  9175. "Get all properties of the entry at point-or-marker POM.
  9176. This includes the TODO keyword, the tags, time strings for deadline,
  9177. scheduled, and clocking, and any additional properties defined in the
  9178. entry. The return value is an alist, keys may occur multiple times
  9179. if the property key was used several times.
  9180. POM may also be nil, in which case the current entry is used.
  9181. If WHICH is nil or `all', get all properties. If WHICH is
  9182. `special' or `standard', only get that subclass."
  9183. (setq which (or which 'all))
  9184. (org-with-point-at pom
  9185. (let ((clockstr (substring org-clock-string 0 -1))
  9186. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9187. beg end range props sum-props key value string clocksum)
  9188. (save-excursion
  9189. (when (condition-case nil (org-back-to-heading t) (error nil))
  9190. (setq beg (point))
  9191. (setq sum-props (get-text-property (point) 'org-summaries))
  9192. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9193. (outline-next-heading)
  9194. (setq end (point))
  9195. (when (memq which '(all special))
  9196. ;; Get the special properties, like TODO and tags
  9197. (goto-char beg)
  9198. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9199. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9200. (when (looking-at org-priority-regexp)
  9201. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9202. (when (and (setq value (org-get-tags-string))
  9203. (string-match "\\S-" value))
  9204. (push (cons "TAGS" value) props))
  9205. (when (setq value (org-get-tags-at))
  9206. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9207. props))
  9208. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9209. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9210. string (if (equal key clockstr)
  9211. (org-no-properties
  9212. (org-trim
  9213. (buffer-substring
  9214. (match-beginning 3) (goto-char (point-at-eol)))))
  9215. (substring (org-match-string-no-properties 3) 1 -1)))
  9216. (unless key
  9217. (if (= (char-after (match-beginning 3)) ?\[)
  9218. (setq key "TIMESTAMP_IA")
  9219. (setq key "TIMESTAMP")))
  9220. (when (or (equal key clockstr) (not (assoc key props)))
  9221. (push (cons key string) props)))
  9222. )
  9223. (when (memq which '(all standard))
  9224. ;; Get the standard properties, like :PORP: ...
  9225. (setq range (org-get-property-block beg end))
  9226. (when range
  9227. (goto-char (car range))
  9228. (while (re-search-forward
  9229. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9230. (cdr range) t)
  9231. (setq key (org-match-string-no-properties 1)
  9232. value (org-trim (or (org-match-string-no-properties 2) "")))
  9233. (unless (member key excluded)
  9234. (push (cons key (or value "")) props)))))
  9235. (if clocksum
  9236. (push (cons "CLOCKSUM"
  9237. (org-columns-number-to-string (/ (float clocksum) 60.)
  9238. 'add_times))
  9239. props))
  9240. (unless (assoc "CATEGORY" props)
  9241. (setq value (or (org-get-category)
  9242. (progn (org-refresh-category-properties)
  9243. (org-get-category))))
  9244. (push (cons "CATEGORY" value) props))
  9245. (append sum-props (nreverse props)))))))
  9246. (defun org-entry-get (pom property &optional inherit)
  9247. "Get value of PROPERTY for entry at point-or-marker POM.
  9248. If INHERIT is non-nil and the entry does not have the property,
  9249. then also check higher levels of the hierarchy.
  9250. If INHERIT is the symbol `selective', use inheritance only if the setting
  9251. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9252. If the property is present but empty, the return value is the empty string.
  9253. If the property is not present at all, nil is returned."
  9254. (org-with-point-at pom
  9255. (if (and inherit (if (eq inherit 'selective)
  9256. (org-property-inherit-p property)
  9257. t))
  9258. (org-entry-get-with-inheritance property)
  9259. (if (member property org-special-properties)
  9260. ;; We need a special property. Use brute force, get all properties.
  9261. (cdr (assoc property (org-entry-properties nil 'special)))
  9262. (let ((range (org-get-property-block)))
  9263. (if (and range
  9264. (goto-char (car range))
  9265. (re-search-forward
  9266. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9267. (cdr range) t))
  9268. ;; Found the property, return it.
  9269. (if (match-end 1)
  9270. (org-match-string-no-properties 1)
  9271. "")))))))
  9272. (defun org-property-or-variable-value (var &optional inherit)
  9273. "Check if there is a property fixing the value of VAR.
  9274. If yes, return this value. If not, return the current value of the variable."
  9275. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9276. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9277. (read prop)
  9278. (symbol-value var))))
  9279. (defun org-entry-delete (pom property)
  9280. "Delete the property PROPERTY from entry at point-or-marker POM."
  9281. (org-with-point-at pom
  9282. (if (member property org-special-properties)
  9283. nil ; cannot delete these properties.
  9284. (let ((range (org-get-property-block)))
  9285. (if (and range
  9286. (goto-char (car range))
  9287. (re-search-forward
  9288. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9289. (cdr range) t))
  9290. (progn
  9291. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9292. t)
  9293. nil)))))
  9294. ;; Multi-values properties are properties that contain multiple values
  9295. ;; These values are assumed to be single words, separated by whitespace.
  9296. (defun org-entry-add-to-multivalued-property (pom property value)
  9297. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9298. (let* ((old (org-entry-get pom property))
  9299. (values (and old (org-split-string old "[ \t]"))))
  9300. (setq value (org-entry-protect-space value))
  9301. (unless (member value values)
  9302. (setq values (cons value values))
  9303. (org-entry-put pom property
  9304. (mapconcat 'identity values " ")))))
  9305. (defun org-entry-remove-from-multivalued-property (pom property value)
  9306. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9307. (let* ((old (org-entry-get pom property))
  9308. (values (and old (org-split-string old "[ \t]"))))
  9309. (setq value (org-entry-protect-space value))
  9310. (when (member value values)
  9311. (setq values (delete value values))
  9312. (org-entry-put pom property
  9313. (mapconcat 'identity values " ")))))
  9314. (defun org-entry-member-in-multivalued-property (pom property value)
  9315. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9316. (let* ((old (org-entry-get pom property))
  9317. (values (and old (org-split-string old "[ \t]"))))
  9318. (setq value (org-entry-protect-space value))
  9319. (member value values)))
  9320. (defun org-entry-get-multivalued-property (pom property)
  9321. "Return a list of values in a multivalued property."
  9322. (let* ((value (org-entry-get pom property))
  9323. (values (and value (org-split-string value "[ \t]"))))
  9324. (mapcar 'org-entry-restore-space values)))
  9325. (defun org-entry-put-multivalued-property (pom property &rest values)
  9326. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9327. VALUES should be a list of strings. Spaces will be protected."
  9328. (org-entry-put pom property
  9329. (mapconcat 'org-entry-protect-space values " "))
  9330. (let* ((value (org-entry-get pom property))
  9331. (values (and value (org-split-string value "[ \t]"))))
  9332. (mapcar 'org-entry-restore-space values)))
  9333. (defun org-entry-protect-space (s)
  9334. "Protect spaces and newline in string S."
  9335. (while (string-match " " s)
  9336. (setq s (replace-match "%20" t t s)))
  9337. (while (string-match "\n" s)
  9338. (setq s (replace-match "%0A" t t s)))
  9339. s)
  9340. (defun org-entry-restore-space (s)
  9341. "Restore spaces and newline in string S."
  9342. (while (string-match "%20" s)
  9343. (setq s (replace-match " " t t s)))
  9344. (while (string-match "%0A" s)
  9345. (setq s (replace-match "\n" t t s)))
  9346. s)
  9347. (defvar org-entry-property-inherited-from (make-marker)
  9348. "Marker pointing to the entry from where a proerty was inherited.
  9349. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9350. location of the entry where the inheriance search matched. If there was
  9351. no match, the marker will point nowhere.
  9352. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9353. is set.")
  9354. (defun org-entry-get-with-inheritance (property)
  9355. "Get entry property, and search higher levels if not present."
  9356. (move-marker org-entry-property-inherited-from nil)
  9357. (let (tmp)
  9358. (save-excursion
  9359. (save-restriction
  9360. (widen)
  9361. (catch 'ex
  9362. (while t
  9363. (when (setq tmp (org-entry-get nil property))
  9364. (org-back-to-heading t)
  9365. (move-marker org-entry-property-inherited-from (point))
  9366. (throw 'ex tmp))
  9367. (or (org-up-heading-safe) (throw 'ex nil)))))
  9368. (or tmp
  9369. (cdr (assoc property org-file-properties))
  9370. (cdr (assoc property org-global-properties))
  9371. (cdr (assoc property org-global-properties-fixed))))))
  9372. (defun org-entry-put (pom property value)
  9373. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9374. (org-with-point-at pom
  9375. (org-back-to-heading t)
  9376. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9377. range)
  9378. (cond
  9379. ((equal property "TODO")
  9380. (when (and (stringp value) (string-match "\\S-" value)
  9381. (not (member value org-todo-keywords-1)))
  9382. (error "\"%s\" is not a valid TODO state" value))
  9383. (if (or (not value)
  9384. (not (string-match "\\S-" value)))
  9385. (setq value 'none))
  9386. (org-todo value)
  9387. (org-set-tags nil 'align))
  9388. ((equal property "PRIORITY")
  9389. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9390. (string-to-char value) ?\ ))
  9391. (org-set-tags nil 'align))
  9392. ((equal property "SCHEDULED")
  9393. (if (re-search-forward org-scheduled-time-regexp end t)
  9394. (cond
  9395. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9396. ((eq value 'later) (org-timestamp-change 1 'day))
  9397. (t (call-interactively 'org-schedule)))
  9398. (call-interactively 'org-schedule)))
  9399. ((equal property "DEADLINE")
  9400. (if (re-search-forward org-deadline-time-regexp end t)
  9401. (cond
  9402. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9403. ((eq value 'later) (org-timestamp-change 1 'day))
  9404. (t (call-interactively 'org-deadline)))
  9405. (call-interactively 'org-deadline)))
  9406. ((member property org-special-properties)
  9407. (error "The %s property can not yet be set with `org-entry-put'"
  9408. property))
  9409. (t ; a non-special property
  9410. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9411. (setq range (org-get-property-block beg end 'force))
  9412. (goto-char (car range))
  9413. (if (re-search-forward
  9414. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9415. (progn
  9416. (delete-region (match-beginning 1) (match-end 1))
  9417. (goto-char (match-beginning 1)))
  9418. (goto-char (cdr range))
  9419. (insert "\n")
  9420. (backward-char 1)
  9421. (org-indent-line-function)
  9422. (insert ":" property ":"))
  9423. (and value (insert " " value))
  9424. (org-indent-line-function)))))))
  9425. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9426. "Get all property keys in the current buffer.
  9427. With INCLUDE-SPECIALS, also list the special properties that relect things
  9428. like tags and TODO state.
  9429. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9430. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9431. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9432. formats in the current buffer."
  9433. (let (rtn range cfmt cols s p)
  9434. (save-excursion
  9435. (save-restriction
  9436. (widen)
  9437. (goto-char (point-min))
  9438. (while (re-search-forward org-property-start-re nil t)
  9439. (setq range (org-get-property-block))
  9440. (goto-char (car range))
  9441. (while (re-search-forward
  9442. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9443. (cdr range) t)
  9444. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9445. (outline-next-heading))))
  9446. (when include-specials
  9447. (setq rtn (append org-special-properties rtn)))
  9448. (when include-defaults
  9449. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9450. (when include-columns
  9451. (save-excursion
  9452. (save-restriction
  9453. (widen)
  9454. (goto-char (point-min))
  9455. (while (re-search-forward
  9456. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9457. nil t)
  9458. (setq cfmt (match-string 2) s 0)
  9459. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9460. cfmt s)
  9461. (setq s (match-end 0)
  9462. p (match-string 1 cfmt))
  9463. (unless (or (equal p "ITEM")
  9464. (member p org-special-properties))
  9465. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9466. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9467. (defun org-property-values (key)
  9468. "Return a list of all values of property KEY."
  9469. (save-excursion
  9470. (save-restriction
  9471. (widen)
  9472. (goto-char (point-min))
  9473. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9474. values)
  9475. (while (re-search-forward re nil t)
  9476. (add-to-list 'values (org-trim (match-string 1))))
  9477. (delete "" values)))))
  9478. (defun org-insert-property-drawer ()
  9479. "Insert a property drawer into the current entry."
  9480. (interactive)
  9481. (org-back-to-heading t)
  9482. (looking-at outline-regexp)
  9483. (let ((indent (- (match-end 0)(match-beginning 0)))
  9484. (beg (point))
  9485. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9486. end hiddenp)
  9487. (outline-next-heading)
  9488. (setq end (point))
  9489. (goto-char beg)
  9490. (while (re-search-forward re end t))
  9491. (setq hiddenp (org-invisible-p))
  9492. (end-of-line 1)
  9493. (and (equal (char-after) ?\n) (forward-char 1))
  9494. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9495. (beginning-of-line 2))
  9496. (org-skip-over-state-notes)
  9497. (skip-chars-backward " \t\n\r")
  9498. (if (eq (char-before) ?*) (forward-char 1))
  9499. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9500. (beginning-of-line 0)
  9501. (org-indent-to-column indent)
  9502. (beginning-of-line 2)
  9503. (org-indent-to-column indent)
  9504. (beginning-of-line 0)
  9505. (if hiddenp
  9506. (save-excursion
  9507. (org-back-to-heading t)
  9508. (hide-entry))
  9509. (org-flag-drawer t))))
  9510. (defun org-set-property (property value)
  9511. "In the current entry, set PROPERTY to VALUE.
  9512. When called interactively, this will prompt for a property name, offering
  9513. completion on existing and default properties. And then it will prompt
  9514. for a value, offering competion either on allowed values (via an inherited
  9515. xxx_ALL property) or on existing values in other instances of this property
  9516. in the current file."
  9517. (interactive
  9518. (let* ((completion-ignore-case t)
  9519. (keys (org-buffer-property-keys nil t t))
  9520. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9521. (prop (if (member prop0 keys)
  9522. prop0
  9523. (or (cdr (assoc (downcase prop0)
  9524. (mapcar (lambda (x) (cons (downcase x) x))
  9525. keys)))
  9526. prop0)))
  9527. (cur (org-entry-get nil prop))
  9528. (allowed (org-property-get-allowed-values nil prop 'table))
  9529. (existing (mapcar 'list (org-property-values prop)))
  9530. (val (if allowed
  9531. (org-completing-read "Value: " allowed nil 'req-match)
  9532. (org-completing-read
  9533. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9534. (concat "[" cur "]") "")
  9535. ": ")
  9536. existing nil nil "" nil cur))))
  9537. (list prop (if (equal val "") cur val))))
  9538. (unless (equal (org-entry-get nil property) value)
  9539. (org-entry-put nil property value)))
  9540. (defun org-delete-property (property)
  9541. "In the current entry, delete PROPERTY."
  9542. (interactive
  9543. (let* ((completion-ignore-case t)
  9544. (prop (org-ido-completing-read
  9545. "Property: " (org-entry-properties nil 'standard))))
  9546. (list prop)))
  9547. (message "Property %s %s" property
  9548. (if (org-entry-delete nil property)
  9549. "deleted"
  9550. "was not present in the entry")))
  9551. (defun org-delete-property-globally (property)
  9552. "Remove PROPERTY globally, from all entries."
  9553. (interactive
  9554. (let* ((completion-ignore-case t)
  9555. (prop (org-ido-completing-read
  9556. "Globally remove property: "
  9557. (mapcar 'list (org-buffer-property-keys)))))
  9558. (list prop)))
  9559. (save-excursion
  9560. (save-restriction
  9561. (widen)
  9562. (goto-char (point-min))
  9563. (let ((cnt 0))
  9564. (while (re-search-forward
  9565. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9566. nil t)
  9567. (setq cnt (1+ cnt))
  9568. (replace-match ""))
  9569. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9570. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9571. (defun org-compute-property-at-point ()
  9572. "Compute the property at point.
  9573. This looks for an enclosing column format, extracts the operator and
  9574. then applies it to the proerty in the column format's scope."
  9575. (interactive)
  9576. (unless (org-at-property-p)
  9577. (error "Not at a property"))
  9578. (let ((prop (org-match-string-no-properties 2)))
  9579. (org-columns-get-format-and-top-level)
  9580. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9581. (error "No operator defined for property %s" prop))
  9582. (org-columns-compute prop)))
  9583. (defun org-property-get-allowed-values (pom property &optional table)
  9584. "Get allowed values for the property PROPERTY.
  9585. When TABLE is non-nil, return an alist that can directly be used for
  9586. completion."
  9587. (let (vals)
  9588. (cond
  9589. ((equal property "TODO")
  9590. (setq vals (org-with-point-at pom
  9591. (append org-todo-keywords-1 '("")))))
  9592. ((equal property "PRIORITY")
  9593. (let ((n org-lowest-priority))
  9594. (while (>= n org-highest-priority)
  9595. (push (char-to-string n) vals)
  9596. (setq n (1- n)))))
  9597. ((member property org-special-properties))
  9598. (t
  9599. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9600. (when (and vals (string-match "\\S-" vals))
  9601. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9602. (setq vals (mapcar (lambda (x)
  9603. (cond ((stringp x) x)
  9604. ((numberp x) (number-to-string x))
  9605. ((symbolp x) (symbol-name x))
  9606. (t "???")))
  9607. vals)))))
  9608. (if table (mapcar 'list vals) vals)))
  9609. (defun org-property-previous-allowed-value (&optional previous)
  9610. "Switch to the next allowed value for this property."
  9611. (interactive)
  9612. (org-property-next-allowed-value t))
  9613. (defun org-property-next-allowed-value (&optional previous)
  9614. "Switch to the next allowed value for this property."
  9615. (interactive)
  9616. (unless (org-at-property-p)
  9617. (error "Not at a property"))
  9618. (let* ((key (match-string 2))
  9619. (value (match-string 3))
  9620. (allowed (or (org-property-get-allowed-values (point) key)
  9621. (and (member value '("[ ]" "[-]" "[X]"))
  9622. '("[ ]" "[X]"))))
  9623. nval)
  9624. (unless allowed
  9625. (error "Allowed values for this property have not been defined"))
  9626. (if previous (setq allowed (reverse allowed)))
  9627. (if (member value allowed)
  9628. (setq nval (car (cdr (member value allowed)))))
  9629. (setq nval (or nval (car allowed)))
  9630. (if (equal nval value)
  9631. (error "Only one allowed value for this property"))
  9632. (org-at-property-p)
  9633. (replace-match (concat " :" key ": " nval) t t)
  9634. (org-indent-line-function)
  9635. (beginning-of-line 1)
  9636. (skip-chars-forward " \t")))
  9637. (defun org-find-entry-with-id (ident)
  9638. "Locate the entry that contains the ID property with exact value IDENT.
  9639. IDENT can be a string, a symbol or a number, this function will search for
  9640. the string representation of it.
  9641. Return the position where this entry starts, or nil if there is no such entry."
  9642. (let ((id (cond
  9643. ((stringp ident) ident)
  9644. ((symbol-name ident) (symbol-name ident))
  9645. ((numberp ident) (number-to-string ident))
  9646. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9647. (case-fold-search nil))
  9648. (save-excursion
  9649. (save-restriction
  9650. (widen)
  9651. (goto-char (point-min))
  9652. (when (re-search-forward
  9653. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9654. nil t)
  9655. (org-back-to-heading)
  9656. (point))))))
  9657. ;;;; Timestamps
  9658. (defvar org-last-changed-timestamp nil)
  9659. (defvar org-last-inserted-timestamp nil
  9660. "The last time stamp inserted with `org-insert-time-stamp'.")
  9661. (defvar org-time-was-given) ; dynamically scoped parameter
  9662. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9663. (defvar org-ts-what) ; dynamically scoped parameter
  9664. (defun org-time-stamp (arg &optional inactive)
  9665. "Prompt for a date/time and insert a time stamp.
  9666. If the user specifies a time like HH:MM, or if this command is called
  9667. with a prefix argument, the time stamp will contain date and time.
  9668. Otherwise, only the date will be included. All parts of a date not
  9669. specified by the user will be filled in from the current date/time.
  9670. So if you press just return without typing anything, the time stamp
  9671. will represent the current date/time. If there is already a timestamp
  9672. at the cursor, it will be modified."
  9673. (interactive "P")
  9674. (let* ((ts nil)
  9675. (default-time
  9676. ;; Default time is either today, or, when entering a range,
  9677. ;; the range start.
  9678. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9679. (save-excursion
  9680. (re-search-backward
  9681. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9682. (- (point) 20) t)))
  9683. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9684. (current-time)))
  9685. (default-input (and ts (org-get-compact-tod ts)))
  9686. org-time-was-given org-end-time-was-given time)
  9687. (cond
  9688. ((and (org-at-timestamp-p t)
  9689. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9690. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9691. (insert "--")
  9692. (setq time (let ((this-command this-command))
  9693. (org-read-date arg 'totime nil nil
  9694. default-time default-input)))
  9695. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9696. ((org-at-timestamp-p t)
  9697. (setq time (let ((this-command this-command))
  9698. (org-read-date arg 'totime nil nil default-time default-input)))
  9699. (when (org-at-timestamp-p t) ; just to get the match data
  9700. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9701. (replace-match "")
  9702. (setq org-last-changed-timestamp
  9703. (org-insert-time-stamp
  9704. time (or org-time-was-given arg)
  9705. inactive nil nil (list org-end-time-was-given))))
  9706. (message "Timestamp updated"))
  9707. (t
  9708. (setq time (let ((this-command this-command))
  9709. (org-read-date arg 'totime nil nil default-time default-input)))
  9710. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9711. nil nil (list org-end-time-was-given))))))
  9712. ;; FIXME: can we use this for something else, like computing time differences?
  9713. (defun org-get-compact-tod (s)
  9714. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9715. (let* ((t1 (match-string 1 s))
  9716. (h1 (string-to-number (match-string 2 s)))
  9717. (m1 (string-to-number (match-string 3 s)))
  9718. (t2 (and (match-end 4) (match-string 5 s)))
  9719. (h2 (and t2 (string-to-number (match-string 6 s))))
  9720. (m2 (and t2 (string-to-number (match-string 7 s))))
  9721. dh dm)
  9722. (if (not t2)
  9723. t1
  9724. (setq dh (- h2 h1) dm (- m2 m1))
  9725. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9726. (concat t1 "+" (number-to-string dh)
  9727. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9728. (defun org-time-stamp-inactive (&optional arg)
  9729. "Insert an inactive time stamp.
  9730. An inactive time stamp is enclosed in square brackets instead of angle
  9731. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9732. does not link to the calendar and cannot be changed with the S-cursor keys.
  9733. So these are more for recording a certain time/date."
  9734. (interactive "P")
  9735. (org-time-stamp arg 'inactive))
  9736. (defvar org-date-ovl (org-make-overlay 1 1))
  9737. (org-overlay-put org-date-ovl 'face 'org-warning)
  9738. (org-detach-overlay org-date-ovl)
  9739. (defvar org-ans1) ; dynamically scoped parameter
  9740. (defvar org-ans2) ; dynamically scoped parameter
  9741. (defvar org-plain-time-of-day-regexp) ; defined below
  9742. (defvar org-overriding-default-time nil) ; dynamically scoped
  9743. (defvar org-read-date-overlay nil)
  9744. (defvar org-dcst nil) ; dynamically scoped
  9745. (defun org-read-date (&optional with-time to-time from-string prompt
  9746. default-time default-input)
  9747. "Read a date, possibly a time, and make things smooth for the user.
  9748. The prompt will suggest to enter an ISO date, but you can also enter anything
  9749. which will at least partially be understood by `parse-time-string'.
  9750. Unrecognized parts of the date will default to the current day, month, year,
  9751. hour and minute. If this command is called to replace a timestamp at point,
  9752. of to enter the second timestamp of a range, the default time is taken from the
  9753. existing stamp. For example,
  9754. 3-2-5 --> 2003-02-05
  9755. feb 15 --> currentyear-02-15
  9756. sep 12 9 --> 2009-09-12
  9757. 12:45 --> today 12:45
  9758. 22 sept 0:34 --> currentyear-09-22 0:34
  9759. 12 --> currentyear-currentmonth-12
  9760. Fri --> nearest Friday (today or later)
  9761. etc.
  9762. Furthermore you can specify a relative date by giving, as the *first* thing
  9763. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9764. change in days weeks, months, years.
  9765. With a single plus or minus, the date is relative to today. With a double
  9766. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9767. +4d --> four days from today
  9768. +4 --> same as above
  9769. +2w --> two weeks from today
  9770. ++5 --> five days from default date
  9771. The function understands only English month and weekday abbreviations,
  9772. but this can be configured with the variables `parse-time-months' and
  9773. `parse-time-weekdays'.
  9774. While prompting, a calendar is popped up - you can also select the
  9775. date with the mouse (button 1). The calendar shows a period of three
  9776. months. To scroll it to other months, use the keys `>' and `<'.
  9777. If you don't like the calendar, turn it off with
  9778. \(setq org-read-date-popup-calendar nil)
  9779. With optional argument TO-TIME, the date will immediately be converted
  9780. to an internal time.
  9781. With an optional argument WITH-TIME, the prompt will suggest to also
  9782. insert a time. Note that when WITH-TIME is not set, you can still
  9783. enter a time, and this function will inform the calling routine about
  9784. this change. The calling routine may then choose to change the format
  9785. used to insert the time stamp into the buffer to include the time.
  9786. With optional argument FROM-STRING, read from this string instead from
  9787. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9788. the time/date that is used for everything that is not specified by the
  9789. user."
  9790. (require 'parse-time)
  9791. (let* ((org-time-stamp-rounding-minutes
  9792. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9793. (org-dcst org-display-custom-times)
  9794. (ct (org-current-time))
  9795. (def (or org-overriding-default-time default-time ct))
  9796. (defdecode (decode-time def))
  9797. (dummy (progn
  9798. (when (< (nth 2 defdecode) org-extend-today-until)
  9799. (setcar (nthcdr 2 defdecode) -1)
  9800. (setcar (nthcdr 1 defdecode) 59)
  9801. (setq def (apply 'encode-time defdecode)
  9802. defdecode (decode-time def)))))
  9803. (calendar-move-hook nil)
  9804. (calendar-view-diary-initially-flag nil)
  9805. (view-diary-entries-initially nil)
  9806. (calendar-view-holidays-initially-flag nil)
  9807. (view-calendar-holidays-initially nil)
  9808. (timestr (format-time-string
  9809. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9810. (prompt (concat (if prompt (concat prompt " ") "")
  9811. (format "Date+time [%s]: " timestr)))
  9812. ans (org-ans0 "") org-ans1 org-ans2 final)
  9813. (cond
  9814. (from-string (setq ans from-string))
  9815. (org-read-date-popup-calendar
  9816. (save-excursion
  9817. (save-window-excursion
  9818. (calendar)
  9819. (calendar-forward-day (- (time-to-days def)
  9820. (calendar-absolute-from-gregorian
  9821. (calendar-current-date))))
  9822. (org-eval-in-calendar nil t)
  9823. (let* ((old-map (current-local-map))
  9824. (map (copy-keymap calendar-mode-map))
  9825. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9826. (org-defkey map (kbd "RET") 'org-calendar-select)
  9827. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9828. 'org-calendar-select-mouse)
  9829. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9830. 'org-calendar-select-mouse)
  9831. (org-defkey minibuffer-local-map [(meta shift left)]
  9832. (lambda () (interactive)
  9833. (org-eval-in-calendar '(calendar-backward-month 1))))
  9834. (org-defkey minibuffer-local-map [(meta shift right)]
  9835. (lambda () (interactive)
  9836. (org-eval-in-calendar '(calendar-forward-month 1))))
  9837. (org-defkey minibuffer-local-map [(meta shift up)]
  9838. (lambda () (interactive)
  9839. (org-eval-in-calendar '(calendar-backward-year 1))))
  9840. (org-defkey minibuffer-local-map [(meta shift down)]
  9841. (lambda () (interactive)
  9842. (org-eval-in-calendar '(calendar-forward-year 1))))
  9843. (org-defkey minibuffer-local-map [(shift up)]
  9844. (lambda () (interactive)
  9845. (org-eval-in-calendar '(calendar-backward-week 1))))
  9846. (org-defkey minibuffer-local-map [(shift down)]
  9847. (lambda () (interactive)
  9848. (org-eval-in-calendar '(calendar-forward-week 1))))
  9849. (org-defkey minibuffer-local-map [(shift left)]
  9850. (lambda () (interactive)
  9851. (org-eval-in-calendar '(calendar-backward-day 1))))
  9852. (org-defkey minibuffer-local-map [(shift right)]
  9853. (lambda () (interactive)
  9854. (org-eval-in-calendar '(calendar-forward-day 1))))
  9855. (org-defkey minibuffer-local-map ">"
  9856. (lambda () (interactive)
  9857. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9858. (org-defkey minibuffer-local-map "<"
  9859. (lambda () (interactive)
  9860. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9861. (unwind-protect
  9862. (progn
  9863. (use-local-map map)
  9864. (add-hook 'post-command-hook 'org-read-date-display)
  9865. (setq org-ans0 (read-string prompt default-input nil nil))
  9866. ;; org-ans0: from prompt
  9867. ;; org-ans1: from mouse click
  9868. ;; org-ans2: from calendar motion
  9869. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9870. (remove-hook 'post-command-hook 'org-read-date-display)
  9871. (use-local-map old-map)
  9872. (when org-read-date-overlay
  9873. (org-delete-overlay org-read-date-overlay)
  9874. (setq org-read-date-overlay nil)))))))
  9875. (t ; Naked prompt only
  9876. (unwind-protect
  9877. (setq ans (read-string prompt default-input nil timestr))
  9878. (when org-read-date-overlay
  9879. (org-delete-overlay org-read-date-overlay)
  9880. (setq org-read-date-overlay nil)))))
  9881. (setq final (org-read-date-analyze ans def defdecode))
  9882. (if to-time
  9883. (apply 'encode-time final)
  9884. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9885. (format "%04d-%02d-%02d %02d:%02d"
  9886. (nth 5 final) (nth 4 final) (nth 3 final)
  9887. (nth 2 final) (nth 1 final))
  9888. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9889. (defvar def)
  9890. (defvar defdecode)
  9891. (defvar with-time)
  9892. (defun org-read-date-display ()
  9893. "Display the currrent date prompt interpretation in the minibuffer."
  9894. (when org-read-date-display-live
  9895. (when org-read-date-overlay
  9896. (org-delete-overlay org-read-date-overlay))
  9897. (let ((p (point)))
  9898. (end-of-line 1)
  9899. (while (not (equal (buffer-substring
  9900. (max (point-min) (- (point) 4)) (point))
  9901. " "))
  9902. (insert " "))
  9903. (goto-char p))
  9904. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9905. " " (or org-ans1 org-ans2)))
  9906. (org-end-time-was-given nil)
  9907. (f (org-read-date-analyze ans def defdecode))
  9908. (fmts (if org-dcst
  9909. org-time-stamp-custom-formats
  9910. org-time-stamp-formats))
  9911. (fmt (if (or with-time
  9912. (and (boundp 'org-time-was-given) org-time-was-given))
  9913. (cdr fmts)
  9914. (car fmts)))
  9915. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9916. (when (and org-end-time-was-given
  9917. (string-match org-plain-time-of-day-regexp txt))
  9918. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9919. org-end-time-was-given
  9920. (substring txt (match-end 0)))))
  9921. (setq org-read-date-overlay
  9922. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9923. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9924. (defun org-read-date-analyze (ans def defdecode)
  9925. "Analyze the combined answer of the date prompt."
  9926. ;; FIXME: cleanup and comment
  9927. (let (delta deltan deltaw deltadef year month day
  9928. hour minute second wday pm h2 m2 tl wday1
  9929. iso-year iso-weekday iso-week iso-year iso-date)
  9930. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9931. (setq ans "+0"))
  9932. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9933. (setq ans (replace-match "" t t ans)
  9934. deltan (car delta)
  9935. deltaw (nth 1 delta)
  9936. deltadef (nth 2 delta)))
  9937. ;; Check if there is an iso week date in there
  9938. ;; If yes, sore the info and ostpone interpreting it until the rest
  9939. ;; of the parsing is done
  9940. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9941. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9942. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9943. iso-week (string-to-number (match-string 2 ans)))
  9944. (setq ans (replace-match "" t t ans)))
  9945. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9946. (when (string-match
  9947. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9948. (setq year (if (match-end 2)
  9949. (string-to-number (match-string 2 ans))
  9950. (string-to-number (format-time-string "%Y")))
  9951. month (string-to-number (match-string 3 ans))
  9952. day (string-to-number (match-string 4 ans)))
  9953. (if (< year 100) (setq year (+ 2000 year)))
  9954. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9955. t nil ans)))
  9956. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9957. ;; If there is a time with am/pm, and *no* time without it, we convert
  9958. ;; so that matching will be successful.
  9959. (loop for i from 1 to 2 do ; twice, for end time as well
  9960. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9961. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9962. (setq hour (string-to-number (match-string 1 ans))
  9963. minute (if (match-end 3)
  9964. (string-to-number (match-string 3 ans))
  9965. 0)
  9966. pm (equal ?p
  9967. (string-to-char (downcase (match-string 4 ans)))))
  9968. (if (and (= hour 12) (not pm))
  9969. (setq hour 0)
  9970. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9971. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9972. t t ans))))
  9973. ;; Check if a time range is given as a duration
  9974. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9975. (setq hour (string-to-number (match-string 1 ans))
  9976. h2 (+ hour (string-to-number (match-string 3 ans)))
  9977. minute (string-to-number (match-string 2 ans))
  9978. m2 (+ minute (if (match-end 5) (string-to-number
  9979. (match-string 5 ans))0)))
  9980. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9981. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9982. t t ans)))
  9983. ;; Check if there is a time range
  9984. (when (boundp 'org-end-time-was-given)
  9985. (setq org-time-was-given nil)
  9986. (when (and (string-match org-plain-time-of-day-regexp ans)
  9987. (match-end 8))
  9988. (setq org-end-time-was-given (match-string 8 ans))
  9989. (setq ans (concat (substring ans 0 (match-beginning 7))
  9990. (substring ans (match-end 7))))))
  9991. (setq tl (parse-time-string ans)
  9992. day (or (nth 3 tl) (nth 3 defdecode))
  9993. month (or (nth 4 tl)
  9994. (if (and org-read-date-prefer-future
  9995. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9996. (1+ (nth 4 defdecode))
  9997. (nth 4 defdecode)))
  9998. year (or (nth 5 tl)
  9999. (if (and org-read-date-prefer-future
  10000. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10001. (1+ (nth 5 defdecode))
  10002. (nth 5 defdecode)))
  10003. hour (or (nth 2 tl) (nth 2 defdecode))
  10004. minute (or (nth 1 tl) (nth 1 defdecode))
  10005. second (or (nth 0 tl) 0)
  10006. wday (nth 6 tl))
  10007. ;; Special date definitions below
  10008. (cond
  10009. (iso-week
  10010. ;; There was an iso week
  10011. (setq year (or iso-year year)
  10012. day (or iso-weekday wday 1)
  10013. wday nil ; to make sure that the trigger below does not match
  10014. iso-date (calendar-gregorian-from-absolute
  10015. (calendar-absolute-from-iso
  10016. (list iso-week day year))))
  10017. ; FIXME: Should we also push ISO weeks into the future?
  10018. ; (when (and org-read-date-prefer-future
  10019. ; (not iso-year)
  10020. ; (< (calendar-absolute-from-gregorian iso-date)
  10021. ; (time-to-days (current-time))))
  10022. ; (setq year (1+ year)
  10023. ; iso-date (calendar-gregorian-from-absolute
  10024. ; (calendar-absolute-from-iso
  10025. ; (list iso-week day year)))))
  10026. (setq month (car iso-date)
  10027. year (nth 2 iso-date)
  10028. day (nth 1 iso-date)))
  10029. (deltan
  10030. (unless deltadef
  10031. (let ((now (decode-time (current-time))))
  10032. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10033. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10034. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10035. ((equal deltaw "m") (setq month (+ month deltan)))
  10036. ((equal deltaw "y") (setq year (+ year deltan)))))
  10037. ((and wday (not (nth 3 tl)))
  10038. ;; Weekday was given, but no day, so pick that day in the week
  10039. ;; on or after the derived date.
  10040. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10041. (unless (equal wday wday1)
  10042. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10043. (if (and (boundp 'org-time-was-given)
  10044. (nth 2 tl))
  10045. (setq org-time-was-given t))
  10046. (if (< year 100) (setq year (+ 2000 year)))
  10047. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10048. (list second minute hour day month year)))
  10049. (defvar parse-time-weekdays)
  10050. (defun org-read-date-get-relative (s today default)
  10051. "Check string S for special relative date string.
  10052. TODAY and DEFAULT are internal times, for today and for a default.
  10053. Return shift list (N what def-flag)
  10054. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10055. N is the number of WHATs to shift.
  10056. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10057. the DEFAULT date rather than TODAY."
  10058. (when (and
  10059. (string-match
  10060. (concat
  10061. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10062. "\\([0-9]+\\)?"
  10063. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10064. "\\([ \t]\\|$\\)") s)
  10065. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10066. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10067. (string-to-char (substring (match-string 1 s) -1))
  10068. ?+))
  10069. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10070. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10071. (what (if (match-end 3) (match-string 3 s) "d"))
  10072. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10073. (date (if rel default today))
  10074. (wday (nth 6 (decode-time date)))
  10075. delta)
  10076. (if wday1
  10077. (progn
  10078. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10079. (if (= dir ?-) (setq delta (- delta 7)))
  10080. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10081. (list delta "d" rel))
  10082. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10083. (defun org-eval-in-calendar (form &optional keepdate)
  10084. "Eval FORM in the calendar window and return to current window.
  10085. Also, store the cursor date in variable org-ans2."
  10086. (let ((sw (selected-window)))
  10087. (select-window (get-buffer-window "*Calendar*"))
  10088. (eval form)
  10089. (when (and (not keepdate) (calendar-cursor-to-date))
  10090. (let* ((date (calendar-cursor-to-date))
  10091. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10092. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10093. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10094. (select-window sw)))
  10095. (defun org-calendar-select ()
  10096. "Return to `org-read-date' with the date currently selected.
  10097. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10098. (interactive)
  10099. (when (calendar-cursor-to-date)
  10100. (let* ((date (calendar-cursor-to-date))
  10101. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10102. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10103. (if (active-minibuffer-window) (exit-minibuffer))))
  10104. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10105. "Insert a date stamp for the date given by the internal TIME.
  10106. WITH-HM means, use the stamp format that includes the time of the day.
  10107. INACTIVE means use square brackets instead of angular ones, so that the
  10108. stamp will not contribute to the agenda.
  10109. PRE and POST are optional strings to be inserted before and after the
  10110. stamp.
  10111. The command returns the inserted time stamp."
  10112. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10113. stamp)
  10114. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10115. (insert-before-markers (or pre ""))
  10116. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10117. (when (listp extra)
  10118. (setq extra (car extra))
  10119. (if (and (stringp extra)
  10120. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10121. (setq extra (format "-%02d:%02d"
  10122. (string-to-number (match-string 1 extra))
  10123. (string-to-number (match-string 2 extra))))
  10124. (setq extra nil)))
  10125. (when extra
  10126. (backward-char 1)
  10127. (insert-before-markers extra)
  10128. (forward-char 1))
  10129. (insert-before-markers (or post ""))
  10130. (setq org-last-inserted-timestamp stamp)))
  10131. (defun org-toggle-time-stamp-overlays ()
  10132. "Toggle the use of custom time stamp formats."
  10133. (interactive)
  10134. (setq org-display-custom-times (not org-display-custom-times))
  10135. (unless org-display-custom-times
  10136. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10137. (while (setq p (next-single-property-change p 'display))
  10138. (if (and (get-text-property p 'display)
  10139. (eq (get-text-property p 'face) 'org-date))
  10140. (remove-text-properties
  10141. p (setq p (next-single-property-change p 'display))
  10142. '(display t))))
  10143. (set-buffer-modified-p bmp)))
  10144. (if (featurep 'xemacs)
  10145. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10146. (org-restart-font-lock)
  10147. (setq org-table-may-need-update t)
  10148. (if org-display-custom-times
  10149. (message "Time stamps are overlayed with custom format")
  10150. (message "Time stamp overlays removed")))
  10151. (defun org-display-custom-time (beg end)
  10152. "Overlay modified time stamp format over timestamp between BEG and END."
  10153. (let* ((ts (buffer-substring beg end))
  10154. t1 w1 with-hm tf time str w2 (off 0))
  10155. (save-match-data
  10156. (setq t1 (org-parse-time-string ts t))
  10157. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10158. (setq off (- (match-end 0) (match-beginning 0)))))
  10159. (setq end (- end off))
  10160. (setq w1 (- end beg)
  10161. with-hm (and (nth 1 t1) (nth 2 t1))
  10162. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10163. time (org-fix-decoded-time t1)
  10164. str (org-add-props
  10165. (format-time-string
  10166. (substring tf 1 -1) (apply 'encode-time time))
  10167. nil 'mouse-face 'highlight)
  10168. w2 (length str))
  10169. (if (not (= w2 w1))
  10170. (add-text-properties (1+ beg) (+ 2 beg)
  10171. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10172. (if (featurep 'xemacs)
  10173. (progn
  10174. (put-text-property beg end 'invisible t)
  10175. (put-text-property beg end 'end-glyph (make-glyph str)))
  10176. (put-text-property beg end 'display str))))
  10177. (defun org-translate-time (string)
  10178. "Translate all timestamps in STRING to custom format.
  10179. But do this only if the variable `org-display-custom-times' is set."
  10180. (when org-display-custom-times
  10181. (save-match-data
  10182. (let* ((start 0)
  10183. (re org-ts-regexp-both)
  10184. t1 with-hm inactive tf time str beg end)
  10185. (while (setq start (string-match re string start))
  10186. (setq beg (match-beginning 0)
  10187. end (match-end 0)
  10188. t1 (save-match-data
  10189. (org-parse-time-string (substring string beg end) t))
  10190. with-hm (and (nth 1 t1) (nth 2 t1))
  10191. inactive (equal (substring string beg (1+ beg)) "[")
  10192. tf (funcall (if with-hm 'cdr 'car)
  10193. org-time-stamp-custom-formats)
  10194. time (org-fix-decoded-time t1)
  10195. str (format-time-string
  10196. (concat
  10197. (if inactive "[" "<") (substring tf 1 -1)
  10198. (if inactive "]" ">"))
  10199. (apply 'encode-time time))
  10200. string (replace-match str t t string)
  10201. start (+ start (length str)))))))
  10202. string)
  10203. (defun org-fix-decoded-time (time)
  10204. "Set 0 instead of nil for the first 6 elements of time.
  10205. Don't touch the rest."
  10206. (let ((n 0))
  10207. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10208. (defun org-days-to-time (timestamp-string)
  10209. "Difference between TIMESTAMP-STRING and now in days."
  10210. (- (time-to-days (org-time-string-to-time timestamp-string))
  10211. (time-to-days (current-time))))
  10212. (defun org-deadline-close (timestamp-string &optional ndays)
  10213. "Is the time in TIMESTAMP-STRING close to the current date?"
  10214. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10215. (and (< (org-days-to-time timestamp-string) ndays)
  10216. (not (org-entry-is-done-p))))
  10217. (defun org-get-wdays (ts)
  10218. "Get the deadline lead time appropriate for timestring TS."
  10219. (cond
  10220. ((<= org-deadline-warning-days 0)
  10221. ;; 0 or negative, enforce this value no matter what
  10222. (- org-deadline-warning-days))
  10223. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10224. ;; lead time is specified.
  10225. (floor (* (string-to-number (match-string 1 ts))
  10226. (cdr (assoc (match-string 2 ts)
  10227. '(("d" . 1) ("w" . 7)
  10228. ("m" . 30.4) ("y" . 365.25)))))))
  10229. ;; go for the default.
  10230. (t org-deadline-warning-days)))
  10231. (defun org-calendar-select-mouse (ev)
  10232. "Return to `org-read-date' with the date currently selected.
  10233. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10234. (interactive "e")
  10235. (mouse-set-point ev)
  10236. (when (calendar-cursor-to-date)
  10237. (let* ((date (calendar-cursor-to-date))
  10238. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10239. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10240. (if (active-minibuffer-window) (exit-minibuffer))))
  10241. (defun org-check-deadlines (ndays)
  10242. "Check if there are any deadlines due or past due.
  10243. A deadline is considered due if it happens within `org-deadline-warning-days'
  10244. days from today's date. If the deadline appears in an entry marked DONE,
  10245. it is not shown. The prefix arg NDAYS can be used to test that many
  10246. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10247. (interactive "P")
  10248. (let* ((org-warn-days
  10249. (cond
  10250. ((equal ndays '(4)) 100000)
  10251. (ndays (prefix-numeric-value ndays))
  10252. (t (abs org-deadline-warning-days))))
  10253. (case-fold-search nil)
  10254. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10255. (callback
  10256. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10257. (message "%d deadlines past-due or due within %d days"
  10258. (org-occur regexp nil callback)
  10259. org-warn-days)))
  10260. (defun org-check-before-date (date)
  10261. "Check if there are deadlines or scheduled entries before DATE."
  10262. (interactive (list (org-read-date)))
  10263. (let ((case-fold-search nil)
  10264. (regexp (concat "\\<\\(" org-deadline-string
  10265. "\\|" org-scheduled-string
  10266. "\\) *<\\([^>]+\\)>"))
  10267. (callback
  10268. (lambda () (time-less-p
  10269. (org-time-string-to-time (match-string 2))
  10270. (org-time-string-to-time date)))))
  10271. (message "%d entries before %s"
  10272. (org-occur regexp nil callback) date)))
  10273. (defun org-evaluate-time-range (&optional to-buffer)
  10274. "Evaluate a time range by computing the difference between start and end.
  10275. Normally the result is just printed in the echo area, but with prefix arg
  10276. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10277. If the time range is actually in a table, the result is inserted into the
  10278. next column.
  10279. For time difference computation, a year is assumed to be exactly 365
  10280. days in order to avoid rounding problems."
  10281. (interactive "P")
  10282. (or
  10283. (org-clock-update-time-maybe)
  10284. (save-excursion
  10285. (unless (org-at-date-range-p t)
  10286. (goto-char (point-at-bol))
  10287. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10288. (if (not (org-at-date-range-p t))
  10289. (error "Not at a time-stamp range, and none found in current line")))
  10290. (let* ((ts1 (match-string 1))
  10291. (ts2 (match-string 2))
  10292. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10293. (match-end (match-end 0))
  10294. (time1 (org-time-string-to-time ts1))
  10295. (time2 (org-time-string-to-time ts2))
  10296. (t1 (time-to-seconds time1))
  10297. (t2 (time-to-seconds time2))
  10298. (diff (abs (- t2 t1)))
  10299. (negative (< (- t2 t1) 0))
  10300. ;; (ys (floor (* 365 24 60 60)))
  10301. (ds (* 24 60 60))
  10302. (hs (* 60 60))
  10303. (fy "%dy %dd %02d:%02d")
  10304. (fy1 "%dy %dd")
  10305. (fd "%dd %02d:%02d")
  10306. (fd1 "%dd")
  10307. (fh "%02d:%02d")
  10308. y d h m align)
  10309. (if havetime
  10310. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10311. y 0
  10312. d (floor (/ diff ds)) diff (mod diff ds)
  10313. h (floor (/ diff hs)) diff (mod diff hs)
  10314. m (floor (/ diff 60)))
  10315. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10316. y 0
  10317. d (floor (+ (/ diff ds) 0.5))
  10318. h 0 m 0))
  10319. (if (not to-buffer)
  10320. (message "%s" (org-make-tdiff-string y d h m))
  10321. (if (org-at-table-p)
  10322. (progn
  10323. (goto-char match-end)
  10324. (setq align t)
  10325. (and (looking-at " *|") (goto-char (match-end 0))))
  10326. (goto-char match-end))
  10327. (if (looking-at
  10328. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10329. (replace-match ""))
  10330. (if negative (insert " -"))
  10331. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10332. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10333. (insert " " (format fh h m))))
  10334. (if align (org-table-align))
  10335. (message "Time difference inserted")))))
  10336. (defun org-make-tdiff-string (y d h m)
  10337. (let ((fmt "")
  10338. (l nil))
  10339. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10340. l (push y l)))
  10341. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10342. l (push d l)))
  10343. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10344. l (push h l)))
  10345. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10346. l (push m l)))
  10347. (apply 'format fmt (nreverse l))))
  10348. (defun org-time-string-to-time (s)
  10349. (apply 'encode-time (org-parse-time-string s)))
  10350. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10351. "Convert a time stamp to an absolute day number.
  10352. If there is a specifyer for a cyclic time stamp, get the closest date to
  10353. DAYNR.
  10354. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10355. (cond
  10356. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10357. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10358. daynr
  10359. (+ daynr 1000)))
  10360. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10361. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10362. (time-to-days (current-time))) (match-string 0 s)
  10363. prefer show-all))
  10364. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10365. (defun org-days-to-iso-week (days)
  10366. "Return the iso week number."
  10367. (require 'cal-iso)
  10368. (car (calendar-iso-from-absolute days)))
  10369. (defun org-small-year-to-year (year)
  10370. "Convert 2-digit years into 4-digit years.
  10371. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10372. The year 2000 cannot be abbreviated. Any year larger than 99
  10373. is returned unchanged."
  10374. (if (< year 38)
  10375. (setq year (+ 2000 year))
  10376. (if (< year 100)
  10377. (setq year (+ 1900 year))))
  10378. year)
  10379. (defun org-time-from-absolute (d)
  10380. "Return the time corresponding to date D.
  10381. D may be an absolute day number, or a calendar-type list (month day year)."
  10382. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10383. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10384. (defun org-calendar-holiday ()
  10385. "List of holidays, for Diary display in Org-mode."
  10386. (require 'holidays)
  10387. (let ((hl (funcall
  10388. (if (fboundp 'calendar-check-holidays)
  10389. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10390. (if hl (mapconcat 'identity hl "; "))))
  10391. (defun org-diary-sexp-entry (sexp entry date)
  10392. "Process a SEXP diary ENTRY for DATE."
  10393. (require 'diary-lib)
  10394. (let ((result (if calendar-debug-sexp
  10395. (let ((stack-trace-on-error t))
  10396. (eval (car (read-from-string sexp))))
  10397. (condition-case nil
  10398. (eval (car (read-from-string sexp)))
  10399. (error
  10400. (beep)
  10401. (message "Bad sexp at line %d in %s: %s"
  10402. (org-current-line)
  10403. (buffer-file-name) sexp)
  10404. (sleep-for 2))))))
  10405. (cond ((stringp result) result)
  10406. ((and (consp result)
  10407. (stringp (cdr result))) (cdr result))
  10408. (result entry)
  10409. (t nil))))
  10410. (defun org-diary-to-ical-string (frombuf)
  10411. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10412. This uses the icalendar.el library."
  10413. (let* ((tmpdir (if (featurep 'xemacs)
  10414. (temp-directory)
  10415. temporary-file-directory))
  10416. (tmpfile (make-temp-name
  10417. (expand-file-name "orgics" tmpdir)))
  10418. buf rtn b e)
  10419. (save-excursion
  10420. (set-buffer frombuf)
  10421. (icalendar-export-region (point-min) (point-max) tmpfile)
  10422. (setq buf (find-buffer-visiting tmpfile))
  10423. (set-buffer buf)
  10424. (goto-char (point-min))
  10425. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10426. (setq b (match-beginning 0)))
  10427. (goto-char (point-max))
  10428. (if (re-search-backward "^END:VEVENT" nil t)
  10429. (setq e (match-end 0)))
  10430. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10431. (kill-buffer buf)
  10432. (delete-file tmpfile)
  10433. rtn))
  10434. (defun org-closest-date (start current change prefer show-all)
  10435. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10436. When PREFER is `past' return a date that is either CURRENT or past.
  10437. When PREFER is `future', return a date that is either CURRENT or future.
  10438. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10439. ;; Make the proper lists from the dates
  10440. (catch 'exit
  10441. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10442. dn dw sday cday n1 n2
  10443. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10444. (setq start (org-date-to-gregorian start)
  10445. current (org-date-to-gregorian
  10446. (if show-all
  10447. current
  10448. (time-to-days (current-time))))
  10449. sday (calendar-absolute-from-gregorian start)
  10450. cday (calendar-absolute-from-gregorian current))
  10451. (if (<= cday sday) (throw 'exit sday))
  10452. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10453. (setq dn (string-to-number (match-string 1 change))
  10454. dw (cdr (assoc (match-string 2 change) a1)))
  10455. (error "Invalid change specifyer: %s" change))
  10456. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10457. (cond
  10458. ((eq dw 'day)
  10459. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10460. n2 (+ n1 dn)))
  10461. ((eq dw 'year)
  10462. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10463. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10464. (setq date1 (list m d y1)
  10465. n1 (calendar-absolute-from-gregorian date1)
  10466. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10467. n2 (calendar-absolute-from-gregorian date2)))
  10468. ((eq dw 'month)
  10469. ;; approx number of month between the two dates
  10470. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10471. ;; How often does dn fit in there?
  10472. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10473. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10474. m (+ m nm)
  10475. ny (floor (/ m 12))
  10476. y (+ y ny)
  10477. m (- m (* ny 12)))
  10478. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10479. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10480. (setq m2 (+ m dn) y2 y)
  10481. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10482. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10483. (while (<= n2 cday)
  10484. (setq n1 n2 m m2 y y2)
  10485. (setq m2 (+ m dn) y2 y)
  10486. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10487. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10488. (if show-all
  10489. (cond
  10490. ((eq prefer 'past) n1)
  10491. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10492. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10493. (cond
  10494. ((eq prefer 'past) n1)
  10495. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10496. (t (if (= cday n1) n1 n2)))))))
  10497. (defun org-date-to-gregorian (date)
  10498. "Turn any specification of DATE into a gregorian date for the calendar."
  10499. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10500. ((and (listp date) (= (length date) 3)) date)
  10501. ((stringp date)
  10502. (setq date (org-parse-time-string date))
  10503. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10504. ((listp date)
  10505. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10506. (defun org-parse-time-string (s &optional nodefault)
  10507. "Parse the standard Org-mode time string.
  10508. This should be a lot faster than the normal `parse-time-string'.
  10509. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10510. hour and minute fields will be nil if not given."
  10511. (if (string-match org-ts-regexp0 s)
  10512. (list 0
  10513. (if (or (match-beginning 8) (not nodefault))
  10514. (string-to-number (or (match-string 8 s) "0")))
  10515. (if (or (match-beginning 7) (not nodefault))
  10516. (string-to-number (or (match-string 7 s) "0")))
  10517. (string-to-number (match-string 4 s))
  10518. (string-to-number (match-string 3 s))
  10519. (string-to-number (match-string 2 s))
  10520. nil nil nil)
  10521. (make-list 9 0)))
  10522. (defun org-timestamp-up (&optional arg)
  10523. "Increase the date item at the cursor by one.
  10524. If the cursor is on the year, change the year. If it is on the month or
  10525. the day, change that.
  10526. With prefix ARG, change by that many units."
  10527. (interactive "p")
  10528. (org-timestamp-change (prefix-numeric-value arg)))
  10529. (defun org-timestamp-down (&optional arg)
  10530. "Decrease the date item at the cursor by one.
  10531. If the cursor is on the year, change the year. If it is on the month or
  10532. the day, change that.
  10533. With prefix ARG, change by that many units."
  10534. (interactive "p")
  10535. (org-timestamp-change (- (prefix-numeric-value arg))))
  10536. (defun org-timestamp-up-day (&optional arg)
  10537. "Increase the date in the time stamp by one day.
  10538. With prefix ARG, change that many days."
  10539. (interactive "p")
  10540. (if (and (not (org-at-timestamp-p t))
  10541. (org-on-heading-p))
  10542. (org-todo 'up)
  10543. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10544. (defun org-timestamp-down-day (&optional arg)
  10545. "Decrease the date in the time stamp by one day.
  10546. With prefix ARG, change that many days."
  10547. (interactive "p")
  10548. (if (and (not (org-at-timestamp-p t))
  10549. (org-on-heading-p))
  10550. (org-todo 'down)
  10551. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10552. (defun org-at-timestamp-p (&optional inactive-ok)
  10553. "Determine if the cursor is in or at a timestamp."
  10554. (interactive)
  10555. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10556. (pos (point))
  10557. (ans (or (looking-at tsr)
  10558. (save-excursion
  10559. (skip-chars-backward "^[<\n\r\t")
  10560. (if (> (point) (point-min)) (backward-char 1))
  10561. (and (looking-at tsr)
  10562. (> (- (match-end 0) pos) -1))))))
  10563. (and ans
  10564. (boundp 'org-ts-what)
  10565. (setq org-ts-what
  10566. (cond
  10567. ((= pos (match-beginning 0)) 'bracket)
  10568. ((= pos (1- (match-end 0))) 'bracket)
  10569. ((org-pos-in-match-range pos 2) 'year)
  10570. ((org-pos-in-match-range pos 3) 'month)
  10571. ((org-pos-in-match-range pos 7) 'hour)
  10572. ((org-pos-in-match-range pos 8) 'minute)
  10573. ((or (org-pos-in-match-range pos 4)
  10574. (org-pos-in-match-range pos 5)) 'day)
  10575. ((and (> pos (or (match-end 8) (match-end 5)))
  10576. (< pos (match-end 0)))
  10577. (- pos (or (match-end 8) (match-end 5))))
  10578. (t 'day))))
  10579. ans))
  10580. (defun org-toggle-timestamp-type ()
  10581. "Toggle the type (<active> or [inactive]) of a time stamp."
  10582. (interactive)
  10583. (when (org-at-timestamp-p t)
  10584. (let ((beg (match-beginning 0)) (end (match-end 0))
  10585. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10586. (save-excursion
  10587. (goto-char beg)
  10588. (while (re-search-forward "[][<>]" end t)
  10589. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10590. t t)))
  10591. (message "Timestamp is now %sactive"
  10592. (if (equal (char-after beg) ?<) "" "in")))))
  10593. (defun org-timestamp-change (n &optional what)
  10594. "Change the date in the time stamp at point.
  10595. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10596. `year', `minute', `second'. If WHAT is not given, the cursor position
  10597. in the timestamp determines what will be changed."
  10598. (let ((pos (point))
  10599. with-hm inactive
  10600. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10601. org-ts-what
  10602. extra rem
  10603. ts time time0)
  10604. (if (not (org-at-timestamp-p t))
  10605. (error "Not at a timestamp"))
  10606. (if (and (not what) (eq org-ts-what 'bracket))
  10607. (org-toggle-timestamp-type)
  10608. (if (and (not what) (not (eq org-ts-what 'day))
  10609. org-display-custom-times
  10610. (get-text-property (point) 'display)
  10611. (not (get-text-property (1- (point)) 'display)))
  10612. (setq org-ts-what 'day))
  10613. (setq org-ts-what (or what org-ts-what)
  10614. inactive (= (char-after (match-beginning 0)) ?\[)
  10615. ts (match-string 0))
  10616. (replace-match "")
  10617. (if (string-match
  10618. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10619. ts)
  10620. (setq extra (match-string 1 ts)))
  10621. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10622. (setq with-hm t))
  10623. (setq time0 (org-parse-time-string ts))
  10624. (when (and (eq org-ts-what 'minute)
  10625. (eq current-prefix-arg nil))
  10626. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10627. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10628. (setcar (cdr time0) (+ (nth 1 time0)
  10629. (if (> n 0) (- rem) (- dm rem))))))
  10630. (setq time
  10631. (encode-time (or (car time0) 0)
  10632. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10633. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10634. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10635. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10636. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10637. (nthcdr 6 time0)))
  10638. (when (integerp org-ts-what)
  10639. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10640. (if (eq what 'calendar)
  10641. (let ((cal-date (org-get-date-from-calendar)))
  10642. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10643. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10644. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10645. (setcar time0 (or (car time0) 0))
  10646. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10647. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10648. (setq time (apply 'encode-time time0))))
  10649. (setq org-last-changed-timestamp
  10650. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10651. (org-clock-update-time-maybe)
  10652. (goto-char pos)
  10653. ;; Try to recenter the calendar window, if any
  10654. (if (and org-calendar-follow-timestamp-change
  10655. (get-buffer-window "*Calendar*" t)
  10656. (memq org-ts-what '(day month year)))
  10657. (org-recenter-calendar (time-to-days time))))))
  10658. (defun org-modify-ts-extra (s pos n dm)
  10659. "Change the different parts of the lead-time and repeat fields in timestamp."
  10660. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10661. ng h m new rem)
  10662. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10663. (cond
  10664. ((or (org-pos-in-match-range pos 2)
  10665. (org-pos-in-match-range pos 3))
  10666. (setq m (string-to-number (match-string 3 s))
  10667. h (string-to-number (match-string 2 s)))
  10668. (if (org-pos-in-match-range pos 2)
  10669. (setq h (+ h n))
  10670. (setq n (* dm (org-no-warnings (signum n))))
  10671. (when (not (= 0 (setq rem (% m dm))))
  10672. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10673. (setq m (+ m n)))
  10674. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10675. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10676. (setq h (min 24 (max 0 h)))
  10677. (setq ng 1 new (format "-%02d:%02d" h m)))
  10678. ((org-pos-in-match-range pos 6)
  10679. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10680. ((org-pos-in-match-range pos 5)
  10681. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10682. ((org-pos-in-match-range pos 9)
  10683. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10684. ((org-pos-in-match-range pos 8)
  10685. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10686. (when ng
  10687. (setq s (concat
  10688. (substring s 0 (match-beginning ng))
  10689. new
  10690. (substring s (match-end ng))))))
  10691. s))
  10692. (defun org-recenter-calendar (date)
  10693. "If the calendar is visible, recenter it to DATE."
  10694. (let* ((win (selected-window))
  10695. (cwin (get-buffer-window "*Calendar*" t))
  10696. (calendar-move-hook nil))
  10697. (when cwin
  10698. (select-window cwin)
  10699. (calendar-goto-date (if (listp date) date
  10700. (calendar-gregorian-from-absolute date)))
  10701. (select-window win))))
  10702. (defun org-goto-calendar (&optional arg)
  10703. "Go to the Emacs calendar at the current date.
  10704. If there is a time stamp in the current line, go to that date.
  10705. A prefix ARG can be used to force the current date."
  10706. (interactive "P")
  10707. (let ((tsr org-ts-regexp) diff
  10708. (calendar-move-hook nil)
  10709. (calendar-view-holidays-initially-flag nil)
  10710. (view-calendar-holidays-initially nil)
  10711. (calendar-view-diary-initially-flag nil)
  10712. (view-diary-entries-initially nil))
  10713. (if (or (org-at-timestamp-p)
  10714. (save-excursion
  10715. (beginning-of-line 1)
  10716. (looking-at (concat ".*" tsr))))
  10717. (let ((d1 (time-to-days (current-time)))
  10718. (d2 (time-to-days
  10719. (org-time-string-to-time (match-string 1)))))
  10720. (setq diff (- d2 d1))))
  10721. (calendar)
  10722. (calendar-goto-today)
  10723. (if (and diff (not arg)) (calendar-forward-day diff))))
  10724. (defun org-get-date-from-calendar ()
  10725. "Return a list (month day year) of date at point in calendar."
  10726. (with-current-buffer "*Calendar*"
  10727. (save-match-data
  10728. (calendar-cursor-to-date))))
  10729. (defun org-date-from-calendar ()
  10730. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10731. If there is already a time stamp at the cursor position, update it."
  10732. (interactive)
  10733. (if (org-at-timestamp-p t)
  10734. (org-timestamp-change 0 'calendar)
  10735. (let ((cal-date (org-get-date-from-calendar)))
  10736. (org-insert-time-stamp
  10737. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10738. (defun org-minutes-to-hh:mm-string (m)
  10739. "Compute H:MM from a number of minutes."
  10740. (let ((h (/ m 60)))
  10741. (setq m (- m (* 60 h)))
  10742. (format org-time-clocksum-format h m)))
  10743. (defun org-hh:mm-string-to-minutes (s)
  10744. "Convert a string H:MM to a number of minutes."
  10745. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10746. (+ (* (string-to-number (match-string 1 s)) 60)
  10747. (string-to-number (match-string 2 s)))
  10748. 0))
  10749. ;;;; Agenda files
  10750. ;;;###autoload
  10751. (defun org-iswitchb (&optional arg)
  10752. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10753. With a prefix argument, restrict available to files.
  10754. With two prefix arguments, restrict available buffers to agenda files.
  10755. Due to some yet unresolved reason, the global function
  10756. `iswitchb-mode' needs to be active for this function to work."
  10757. (interactive "P")
  10758. (require 'iswitchb)
  10759. (let ((enabled iswitchb-mode) blist)
  10760. (or enabled (iswitchb-mode 1))
  10761. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10762. ((equal arg '(16)) (org-buffer-list 'agenda))
  10763. (t (org-buffer-list))))
  10764. (unwind-protect
  10765. (let ((iswitchb-make-buflist-hook
  10766. (lambda ()
  10767. (setq iswitchb-temp-buflist
  10768. (mapcar 'buffer-name blist)))))
  10769. (switch-to-buffer
  10770. (iswitchb-read-buffer
  10771. "Switch-to: " nil t))
  10772. (or enabled (iswitchb-mode -1))))))
  10773. (defun org-buffer-list (&optional predicate exclude-tmp)
  10774. "Return a list of Org buffers.
  10775. PREDICATE can be `export', `files' or `agenda'.
  10776. export restrict the list to Export buffers.
  10777. files restrict the list to buffers visiting Org files.
  10778. agenda restrict the list to buffers visiting agenda files.
  10779. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10780. (let* ((bfn nil)
  10781. (agenda-files (and (eq predicate 'agenda)
  10782. (mapcar 'file-truename (org-agenda-files t))))
  10783. (filter
  10784. (cond
  10785. ((eq predicate 'files)
  10786. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10787. ((eq predicate 'export)
  10788. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10789. ((eq predicate 'agenda)
  10790. (lambda (b)
  10791. (with-current-buffer b
  10792. (and (eq major-mode 'org-mode)
  10793. (setq bfn (buffer-file-name b))
  10794. (member (file-truename bfn) agenda-files)))))
  10795. (t (lambda (b) (with-current-buffer b
  10796. (or (eq major-mode 'org-mode)
  10797. (string-match "\*Org .*Export"
  10798. (buffer-name b)))))))))
  10799. (delq nil
  10800. (mapcar
  10801. (lambda(b)
  10802. (if (and (funcall filter b)
  10803. (or (not exclude-tmp)
  10804. (not (string-match "tmp" (buffer-name b)))))
  10805. b
  10806. nil))
  10807. (buffer-list)))))
  10808. (defun org-agenda-files (&optional unrestricted archives)
  10809. "Get the list of agenda files.
  10810. Optional UNRESTRICTED means return the full list even if a restriction
  10811. is currently in place.
  10812. When ARCHIVES is t, include all archive files hat are really being
  10813. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10814. `org-agenda-archives-mode' is t."
  10815. (let ((files
  10816. (cond
  10817. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10818. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10819. ((listp org-agenda-files) org-agenda-files)
  10820. (t (error "Invalid value of `org-agenda-files'")))))
  10821. (setq files (apply 'append
  10822. (mapcar (lambda (f)
  10823. (if (file-directory-p f)
  10824. (directory-files
  10825. f t org-agenda-file-regexp)
  10826. (list f)))
  10827. files)))
  10828. (when org-agenda-skip-unavailable-files
  10829. (setq files (delq nil
  10830. (mapcar (function
  10831. (lambda (file)
  10832. (and (file-readable-p file) file)))
  10833. files))))
  10834. (when (or (eq archives t)
  10835. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10836. (setq files (org-add-archive-files files)))
  10837. files))
  10838. (defun org-edit-agenda-file-list ()
  10839. "Edit the list of agenda files.
  10840. Depending on setup, this either uses customize to edit the variable
  10841. `org-agenda-files', or it visits the file that is holding the list. In the
  10842. latter case, the buffer is set up in a way that saving it automatically kills
  10843. the buffer and restores the previous window configuration."
  10844. (interactive)
  10845. (if (stringp org-agenda-files)
  10846. (let ((cw (current-window-configuration)))
  10847. (find-file org-agenda-files)
  10848. (org-set-local 'org-window-configuration cw)
  10849. (org-add-hook 'after-save-hook
  10850. (lambda ()
  10851. (set-window-configuration
  10852. (prog1 org-window-configuration
  10853. (kill-buffer (current-buffer))))
  10854. (org-install-agenda-files-menu)
  10855. (message "New agenda file list installed"))
  10856. nil 'local)
  10857. (message "%s" (substitute-command-keys
  10858. "Edit list and finish with \\[save-buffer]")))
  10859. (customize-variable 'org-agenda-files)))
  10860. (defun org-store-new-agenda-file-list (list)
  10861. "Set new value for the agenda file list and save it correcly."
  10862. (if (stringp org-agenda-files)
  10863. (let ((f org-agenda-files) b)
  10864. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10865. (with-temp-file f
  10866. (insert (mapconcat 'identity list "\n") "\n")))
  10867. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10868. (setq org-agenda-files list)
  10869. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10870. (defun org-read-agenda-file-list ()
  10871. "Read the list of agenda files from a file."
  10872. (when (file-directory-p org-agenda-files)
  10873. (error "`org-agenda-files' cannot be a single directory"))
  10874. (when (stringp org-agenda-files)
  10875. (with-temp-buffer
  10876. (insert-file-contents org-agenda-files)
  10877. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10878. ;;;###autoload
  10879. (defun org-cycle-agenda-files ()
  10880. "Cycle through the files in `org-agenda-files'.
  10881. If the current buffer visits an agenda file, find the next one in the list.
  10882. If the current buffer does not, find the first agenda file."
  10883. (interactive)
  10884. (let* ((fs (org-agenda-files t))
  10885. (files (append fs (list (car fs))))
  10886. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10887. file)
  10888. (unless files (error "No agenda files"))
  10889. (catch 'exit
  10890. (while (setq file (pop files))
  10891. (if (equal (file-truename file) tcf)
  10892. (when (car files)
  10893. (find-file (car files))
  10894. (throw 'exit t))))
  10895. (find-file (car fs)))
  10896. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10897. (defun org-agenda-file-to-front (&optional to-end)
  10898. "Move/add the current file to the top of the agenda file list.
  10899. If the file is not present in the list, it is added to the front. If it is
  10900. present, it is moved there. With optional argument TO-END, add/move to the
  10901. end of the list."
  10902. (interactive "P")
  10903. (let ((org-agenda-skip-unavailable-files nil)
  10904. (file-alist (mapcar (lambda (x)
  10905. (cons (file-truename x) x))
  10906. (org-agenda-files t)))
  10907. (ctf (file-truename buffer-file-name))
  10908. x had)
  10909. (setq x (assoc ctf file-alist) had x)
  10910. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10911. (if to-end
  10912. (setq file-alist (append (delq x file-alist) (list x)))
  10913. (setq file-alist (cons x (delq x file-alist))))
  10914. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10915. (org-install-agenda-files-menu)
  10916. (message "File %s to %s of agenda file list"
  10917. (if had "moved" "added") (if to-end "end" "front"))))
  10918. (defun org-remove-file (&optional file)
  10919. "Remove current file from the list of files in variable `org-agenda-files'.
  10920. These are the files which are being checked for agenda entries.
  10921. Optional argument FILE means, use this file instead of the current."
  10922. (interactive)
  10923. (let* ((org-agenda-skip-unavailable-files nil)
  10924. (file (or file buffer-file-name))
  10925. (true-file (file-truename file))
  10926. (afile (abbreviate-file-name file))
  10927. (files (delq nil (mapcar
  10928. (lambda (x)
  10929. (if (equal true-file
  10930. (file-truename x))
  10931. nil x))
  10932. (org-agenda-files t)))))
  10933. (if (not (= (length files) (length (org-agenda-files t))))
  10934. (progn
  10935. (org-store-new-agenda-file-list files)
  10936. (org-install-agenda-files-menu)
  10937. (message "Removed file: %s" afile))
  10938. (message "File was not in list: %s (not removed)" afile))))
  10939. (defun org-file-menu-entry (file)
  10940. (vector file (list 'find-file file) t))
  10941. (defun org-check-agenda-file (file)
  10942. "Make sure FILE exists. If not, ask user what to do."
  10943. (when (not (file-exists-p file))
  10944. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10945. (abbreviate-file-name file))
  10946. (let ((r (downcase (read-char-exclusive))))
  10947. (cond
  10948. ((equal r ?r)
  10949. (org-remove-file file)
  10950. (throw 'nextfile t))
  10951. (t (error "Abort"))))))
  10952. (defun org-get-agenda-file-buffer (file)
  10953. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10954. it to the list of buffers which might be released later."
  10955. (let ((buf (org-find-base-buffer-visiting file)))
  10956. (if buf
  10957. buf ; just return it
  10958. ;; Make a new buffer and remember it
  10959. (setq buf (find-file-noselect file))
  10960. (if buf (push buf org-agenda-new-buffers))
  10961. buf)))
  10962. (defun org-release-buffers (blist)
  10963. "Release all buffers in list, asking the user for confirmation when needed.
  10964. When a buffer is unmodified, it is just killed. When modified, it is saved
  10965. \(if the user agrees) and then killed."
  10966. (let (buf file)
  10967. (while (setq buf (pop blist))
  10968. (setq file (buffer-file-name buf))
  10969. (when (and (buffer-modified-p buf)
  10970. file
  10971. (y-or-n-p (format "Save file %s? " file)))
  10972. (with-current-buffer buf (save-buffer)))
  10973. (kill-buffer buf))))
  10974. (defun org-prepare-agenda-buffers (files)
  10975. "Create buffers for all agenda files, protect archived trees and comments."
  10976. (interactive)
  10977. (let ((pa '(:org-archived t))
  10978. (pc '(:org-comment t))
  10979. (pall '(:org-archived t :org-comment t))
  10980. (inhibit-read-only t)
  10981. (rea (concat ":" org-archive-tag ":"))
  10982. bmp file re)
  10983. (save-excursion
  10984. (save-restriction
  10985. (while (setq file (pop files))
  10986. (if (bufferp file)
  10987. (set-buffer file)
  10988. (org-check-agenda-file file)
  10989. (set-buffer (org-get-agenda-file-buffer file)))
  10990. (widen)
  10991. (setq bmp (buffer-modified-p))
  10992. (org-refresh-category-properties)
  10993. (setq org-todo-keywords-for-agenda
  10994. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10995. (setq org-done-keywords-for-agenda
  10996. (append org-done-keywords-for-agenda org-done-keywords))
  10997. (setq org-todo-keyword-alist-for-agenda
  10998. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10999. (setq org-tag-alist-for-agenda
  11000. (append org-tag-alist-for-agenda org-tag-alist))
  11001. (save-excursion
  11002. (remove-text-properties (point-min) (point-max) pall)
  11003. (when org-agenda-skip-archived-trees
  11004. (goto-char (point-min))
  11005. (while (re-search-forward rea nil t)
  11006. (if (org-on-heading-p t)
  11007. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11008. (goto-char (point-min))
  11009. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11010. (while (re-search-forward re nil t)
  11011. (add-text-properties
  11012. (match-beginning 0) (org-end-of-subtree t) pc)))
  11013. (set-buffer-modified-p bmp))))
  11014. (setq org-todo-keyword-alist-for-agenda
  11015. (org-uniquify org-todo-keyword-alist-for-agenda)
  11016. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11017. ;;;; Embedded LaTeX
  11018. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11019. "Keymap for the minor `org-cdlatex-mode'.")
  11020. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11021. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11022. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11023. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11024. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11025. (defvar org-cdlatex-texmathp-advice-is-done nil
  11026. "Flag remembering if we have applied the advice to texmathp already.")
  11027. (define-minor-mode org-cdlatex-mode
  11028. "Toggle the minor `org-cdlatex-mode'.
  11029. This mode supports entering LaTeX environment and math in LaTeX fragments
  11030. in Org-mode.
  11031. \\{org-cdlatex-mode-map}"
  11032. nil " OCDL" nil
  11033. (when org-cdlatex-mode (require 'cdlatex))
  11034. (unless org-cdlatex-texmathp-advice-is-done
  11035. (setq org-cdlatex-texmathp-advice-is-done t)
  11036. (defadvice texmathp (around org-math-always-on activate)
  11037. "Always return t in org-mode buffers.
  11038. This is because we want to insert math symbols without dollars even outside
  11039. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11040. en embedded LaTeX fragement, let texmathp do its job.
  11041. \\[org-cdlatex-mode-map]"
  11042. (interactive)
  11043. (let (p)
  11044. (cond
  11045. ((not (org-mode-p)) ad-do-it)
  11046. ((eq this-command 'cdlatex-math-symbol)
  11047. (setq ad-return-value t
  11048. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11049. (t
  11050. (let ((p (org-inside-LaTeX-fragment-p)))
  11051. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11052. (setq ad-return-value t
  11053. texmathp-why '("Org-mode embedded math" . 0))
  11054. (if p ad-do-it)))))))))
  11055. (defun turn-on-org-cdlatex ()
  11056. "Unconditionally turn on `org-cdlatex-mode'."
  11057. (org-cdlatex-mode 1))
  11058. (defun org-inside-LaTeX-fragment-p ()
  11059. "Test if point is inside a LaTeX fragment.
  11060. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11061. sequence appearing also before point.
  11062. Even though the matchers for math are configurable, this function assumes
  11063. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11064. delimiters are skipped when they have been removed by customization.
  11065. The return value is nil, or a cons cell with the delimiter and
  11066. and the position of this delimiter.
  11067. This function does a reasonably good job, but can locally be fooled by
  11068. for example currency specifications. For example it will assume being in
  11069. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11070. fragments that are properly closed, but during editing, we have to live
  11071. with the uncertainty caused by missing closing delimiters. This function
  11072. looks only before point, not after."
  11073. (catch 'exit
  11074. (let ((pos (point))
  11075. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11076. (lim (progn
  11077. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11078. (point)))
  11079. dd-on str (start 0) m re)
  11080. (goto-char pos)
  11081. (when dodollar
  11082. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11083. re (nth 1 (assoc "$" org-latex-regexps)))
  11084. (while (string-match re str start)
  11085. (cond
  11086. ((= (match-end 0) (length str))
  11087. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11088. ((= (match-end 0) (- (length str) 5))
  11089. (throw 'exit nil))
  11090. (t (setq start (match-end 0))))))
  11091. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11092. (goto-char pos)
  11093. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11094. (and (match-beginning 2) (throw 'exit nil))
  11095. ;; count $$
  11096. (while (re-search-backward "\\$\\$" lim t)
  11097. (setq dd-on (not dd-on)))
  11098. (goto-char pos)
  11099. (if dd-on (cons "$$" m))))))
  11100. (defun org-try-cdlatex-tab ()
  11101. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11102. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11103. - inside a LaTeX fragment, or
  11104. - after the first word in a line, where an abbreviation expansion could
  11105. insert a LaTeX environment."
  11106. (when org-cdlatex-mode
  11107. (cond
  11108. ((save-excursion
  11109. (skip-chars-backward "a-zA-Z0-9*")
  11110. (skip-chars-backward " \t")
  11111. (bolp))
  11112. (cdlatex-tab) t)
  11113. ((org-inside-LaTeX-fragment-p)
  11114. (cdlatex-tab) t)
  11115. (t nil))))
  11116. (defun org-cdlatex-underscore-caret (&optional arg)
  11117. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11118. Revert to the normal definition outside of these fragments."
  11119. (interactive "P")
  11120. (if (org-inside-LaTeX-fragment-p)
  11121. (call-interactively 'cdlatex-sub-superscript)
  11122. (let (org-cdlatex-mode)
  11123. (call-interactively (key-binding (vector last-input-event))))))
  11124. (defun org-cdlatex-math-modify (&optional arg)
  11125. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11126. Revert to the normal definition outside of these fragments."
  11127. (interactive "P")
  11128. (if (org-inside-LaTeX-fragment-p)
  11129. (call-interactively 'cdlatex-math-modify)
  11130. (let (org-cdlatex-mode)
  11131. (call-interactively (key-binding (vector last-input-event))))))
  11132. (defvar org-latex-fragment-image-overlays nil
  11133. "List of overlays carrying the images of latex fragments.")
  11134. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11135. (defun org-remove-latex-fragment-image-overlays ()
  11136. "Remove all overlays with LaTeX fragment images in current buffer."
  11137. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11138. (setq org-latex-fragment-image-overlays nil))
  11139. (defun org-preview-latex-fragment (&optional subtree)
  11140. "Preview the LaTeX fragment at point, or all locally or globally.
  11141. If the cursor is in a LaTeX fragment, create the image and overlay
  11142. it over the source code. If there is no fragment at point, display
  11143. all fragments in the current text, from one headline to the next. With
  11144. prefix SUBTREE, display all fragments in the current subtree. With a
  11145. double prefix `C-u C-u', or when the cursor is before the first headline,
  11146. display all fragments in the buffer.
  11147. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11148. (interactive "P")
  11149. (org-remove-latex-fragment-image-overlays)
  11150. (save-excursion
  11151. (save-restriction
  11152. (let (beg end at msg)
  11153. (cond
  11154. ((or (equal subtree '(16))
  11155. (not (save-excursion
  11156. (re-search-backward (concat "^" outline-regexp) nil t))))
  11157. (setq beg (point-min) end (point-max)
  11158. msg "Creating images for buffer...%s"))
  11159. ((equal subtree '(4))
  11160. (org-back-to-heading)
  11161. (setq beg (point) end (org-end-of-subtree t)
  11162. msg "Creating images for subtree...%s"))
  11163. (t
  11164. (if (setq at (org-inside-LaTeX-fragment-p))
  11165. (goto-char (max (point-min) (- (cdr at) 2)))
  11166. (org-back-to-heading))
  11167. (setq beg (point) end (progn (outline-next-heading) (point))
  11168. msg (if at "Creating image...%s"
  11169. "Creating images for entry...%s"))))
  11170. (message msg "")
  11171. (narrow-to-region beg end)
  11172. (goto-char beg)
  11173. (org-format-latex
  11174. (concat "ltxpng/" (file-name-sans-extension
  11175. (file-name-nondirectory
  11176. buffer-file-name)))
  11177. default-directory 'overlays msg at 'forbuffer)
  11178. (message msg "done. Use `C-c C-c' to remove images.")))))
  11179. (defvar org-latex-regexps
  11180. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11181. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11182. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11183. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11184. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11185. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11186. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11187. "Regular expressions for matching embedded LaTeX.")
  11188. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11189. "Replace LaTeX fragments with links to an image, and produce images."
  11190. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11191. (let* ((prefixnodir (file-name-nondirectory prefix))
  11192. (absprefix (expand-file-name prefix dir))
  11193. (todir (file-name-directory absprefix))
  11194. (opt org-format-latex-options)
  11195. (matchers (plist-get opt :matchers))
  11196. (re-list org-latex-regexps)
  11197. (cnt 0) txt link beg end re e checkdir
  11198. m n block linkfile movefile ov)
  11199. ;; Check if there are old images files with this prefix, and remove them
  11200. (when (file-directory-p todir)
  11201. (mapc 'delete-file
  11202. (directory-files
  11203. todir 'full
  11204. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11205. ;; Check the different regular expressions
  11206. (while (setq e (pop re-list))
  11207. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11208. block (if (nth 3 e) "\n\n" ""))
  11209. (when (member m matchers)
  11210. (goto-char (point-min))
  11211. (while (re-search-forward re nil t)
  11212. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11213. (setq txt (match-string n)
  11214. beg (match-beginning n) end (match-end n)
  11215. cnt (1+ cnt)
  11216. linkfile (format "%s_%04d.png" prefix cnt)
  11217. movefile (format "%s_%04d.png" absprefix cnt)
  11218. link (concat block "[[file:" linkfile "]]" block))
  11219. (if msg (message msg cnt))
  11220. (goto-char beg)
  11221. (unless checkdir ; make sure the directory exists
  11222. (setq checkdir t)
  11223. (or (file-directory-p todir) (make-directory todir)))
  11224. (org-create-formula-image
  11225. txt movefile opt forbuffer)
  11226. (if overlays
  11227. (progn
  11228. (setq ov (org-make-overlay beg end))
  11229. (if (featurep 'xemacs)
  11230. (progn
  11231. (org-overlay-put ov 'invisible t)
  11232. (org-overlay-put
  11233. ov 'end-glyph
  11234. (make-glyph (vector 'png :file movefile))))
  11235. (org-overlay-put
  11236. ov 'display
  11237. (list 'image :type 'png :file movefile :ascent 'center)))
  11238. (push ov org-latex-fragment-image-overlays)
  11239. (goto-char end))
  11240. (delete-region beg end)
  11241. (insert link))))))))
  11242. ;; This function borrows from Ganesh Swami's latex2png.el
  11243. (defun org-create-formula-image (string tofile options buffer)
  11244. (let* ((tmpdir (if (featurep 'xemacs)
  11245. (temp-directory)
  11246. temporary-file-directory))
  11247. (texfilebase (make-temp-name
  11248. (expand-file-name "orgtex" tmpdir)))
  11249. (texfile (concat texfilebase ".tex"))
  11250. (dvifile (concat texfilebase ".dvi"))
  11251. (pngfile (concat texfilebase ".png"))
  11252. (fnh (if (featurep 'xemacs)
  11253. (font-height (get-face-font 'default))
  11254. (face-attribute 'default :height nil)))
  11255. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11256. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11257. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11258. "Black"))
  11259. (bg (or (plist-get options (if buffer :background :html-background))
  11260. "Transparent")))
  11261. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11262. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11263. (with-temp-file texfile
  11264. (insert org-format-latex-header
  11265. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11266. (let ((dir default-directory))
  11267. (condition-case nil
  11268. (progn
  11269. (cd tmpdir)
  11270. (call-process "latex" nil nil nil texfile))
  11271. (error nil))
  11272. (cd dir))
  11273. (if (not (file-exists-p dvifile))
  11274. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11275. (condition-case nil
  11276. (call-process "dvipng" nil nil nil
  11277. "-E" "-fg" fg "-bg" bg
  11278. "-D" dpi
  11279. ;;"-x" scale "-y" scale
  11280. "-T" "tight"
  11281. "-o" pngfile
  11282. dvifile)
  11283. (error nil))
  11284. (if (not (file-exists-p pngfile))
  11285. (progn (message "Failed to create png file from %s" texfile) nil)
  11286. ;; Use the requested file name and clean up
  11287. (copy-file pngfile tofile 'replace)
  11288. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11289. (delete-file (concat texfilebase e)))
  11290. pngfile))))
  11291. (defun org-dvipng-color (attr)
  11292. "Return an rgb color specification for dvipng."
  11293. (apply 'format "rgb %s %s %s"
  11294. (mapcar 'org-normalize-color
  11295. (color-values (face-attribute 'default attr nil)))))
  11296. (defun org-normalize-color (value)
  11297. "Return string to be used as color value for an RGB component."
  11298. (format "%g" (/ value 65535.0)))
  11299. ;;;; Key bindings
  11300. ;; Make `C-c C-x' a prefix key
  11301. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11302. ;; TAB key with modifiers
  11303. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11304. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11305. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11306. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11307. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11308. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11309. ;; The following line is necessary under Suse GNU/Linux
  11310. (unless (featurep 'xemacs)
  11311. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11312. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11313. (define-key org-mode-map [backtab] 'org-shifttab)
  11314. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11315. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11316. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11317. ;; Cursor keys with modifiers
  11318. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11319. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11320. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11321. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11322. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11323. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11324. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11325. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11326. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11327. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11328. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11329. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11330. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11331. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11332. ;;; Extra keys for tty access.
  11333. ;; We only set them when really needed because otherwise the
  11334. ;; menus don't show the simple keys
  11335. (when (or org-use-extra-keys
  11336. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11337. (not window-system))
  11338. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11339. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11340. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11341. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11342. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11343. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11344. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11345. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11346. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11347. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11348. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11349. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11350. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11351. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11352. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11353. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11354. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11355. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11356. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11357. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11358. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11359. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11360. ;; All the other keys
  11361. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11362. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11363. (if (boundp 'narrow-map)
  11364. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11365. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11366. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11367. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11368. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11369. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11370. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11371. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11372. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11373. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11374. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11375. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11376. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11377. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11378. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11379. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11380. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11381. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11382. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11383. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11384. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11385. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11386. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11387. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11388. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11389. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11390. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11391. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11392. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11393. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11394. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11395. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11396. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11397. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11398. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11399. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11400. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11401. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11402. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11403. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11404. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11405. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11406. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11407. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11408. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11409. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11410. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11411. (org-defkey org-mode-map "\C-m" 'org-return)
  11412. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11413. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11414. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11415. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11416. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11417. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11418. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11419. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11420. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11421. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11422. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11423. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11424. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11425. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11426. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11427. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11428. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11429. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11430. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11431. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11432. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11433. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11434. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11435. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11436. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11437. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11438. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11439. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11440. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11441. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11442. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11443. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11444. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11445. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11446. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11447. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11448. (when (featurep 'xemacs)
  11449. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11450. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11451. (defun org-self-insert-command (N)
  11452. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11453. If the cursor is in a table looking at whitespace, the whitespace is
  11454. overwritten, and the table is not marked as requiring realignment."
  11455. (interactive "p")
  11456. (if (and (org-table-p)
  11457. (progn
  11458. ;; check if we blank the field, and if that triggers align
  11459. (and (featurep 'org-table) org-table-auto-blank-field
  11460. (member last-command
  11461. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11462. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11463. ;; got extra space, this field does not determine column width
  11464. (let (org-table-may-need-update) (org-table-blank-field))
  11465. ;; no extra space, this field may determine column width
  11466. (org-table-blank-field)))
  11467. t)
  11468. (eq N 1)
  11469. (looking-at "[^|\n]* |"))
  11470. (let (org-table-may-need-update)
  11471. (goto-char (1- (match-end 0)))
  11472. (delete-backward-char 1)
  11473. (goto-char (match-beginning 0))
  11474. (self-insert-command N))
  11475. (setq org-table-may-need-update t)
  11476. (self-insert-command N)
  11477. (org-fix-tags-on-the-fly)))
  11478. (defun org-fix-tags-on-the-fly ()
  11479. (when (and (equal (char-after (point-at-bol)) ?*)
  11480. (org-on-heading-p))
  11481. (org-align-tags-here org-tags-column)))
  11482. (defun org-delete-backward-char (N)
  11483. "Like `delete-backward-char', insert whitespace at field end in tables.
  11484. When deleting backwards, in tables this function will insert whitespace in
  11485. front of the next \"|\" separator, to keep the table aligned. The table will
  11486. still be marked for re-alignment if the field did fill the entire column,
  11487. because, in this case the deletion might narrow the column."
  11488. (interactive "p")
  11489. (if (and (org-table-p)
  11490. (eq N 1)
  11491. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11492. (looking-at ".*?|"))
  11493. (let ((pos (point))
  11494. (noalign (looking-at "[^|\n\r]* |"))
  11495. (c org-table-may-need-update))
  11496. (backward-delete-char N)
  11497. (skip-chars-forward "^|")
  11498. (insert " ")
  11499. (goto-char (1- pos))
  11500. ;; noalign: if there were two spaces at the end, this field
  11501. ;; does not determine the width of the column.
  11502. (if noalign (setq org-table-may-need-update c)))
  11503. (backward-delete-char N)
  11504. (org-fix-tags-on-the-fly)))
  11505. (defun org-delete-char (N)
  11506. "Like `delete-char', but insert whitespace at field end in tables.
  11507. When deleting characters, in tables this function will insert whitespace in
  11508. front of the next \"|\" separator, to keep the table aligned. The table will
  11509. still be marked for re-alignment if the field did fill the entire column,
  11510. because, in this case the deletion might narrow the column."
  11511. (interactive "p")
  11512. (if (and (org-table-p)
  11513. (not (bolp))
  11514. (not (= (char-after) ?|))
  11515. (eq N 1))
  11516. (if (looking-at ".*?|")
  11517. (let ((pos (point))
  11518. (noalign (looking-at "[^|\n\r]* |"))
  11519. (c org-table-may-need-update))
  11520. (replace-match (concat
  11521. (substring (match-string 0) 1 -1)
  11522. " |"))
  11523. (goto-char pos)
  11524. ;; noalign: if there were two spaces at the end, this field
  11525. ;; does not determine the width of the column.
  11526. (if noalign (setq org-table-may-need-update c)))
  11527. (delete-char N))
  11528. (delete-char N)
  11529. (org-fix-tags-on-the-fly)))
  11530. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11531. (put 'org-self-insert-command 'delete-selection t)
  11532. (put 'orgtbl-self-insert-command 'delete-selection t)
  11533. (put 'org-delete-char 'delete-selection 'supersede)
  11534. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11535. ;; Make `flyspell-mode' delay after some commands
  11536. (put 'org-self-insert-command 'flyspell-delayed t)
  11537. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11538. (put 'org-delete-char 'flyspell-delayed t)
  11539. (put 'org-delete-backward-char 'flyspell-delayed t)
  11540. ;; Make pabbrev-mode expand after org-mode commands
  11541. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11542. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11543. ;; How to do this: Measure non-white length of current string
  11544. ;; If equal to column width, we should realign.
  11545. (defun org-remap (map &rest commands)
  11546. "In MAP, remap the functions given in COMMANDS.
  11547. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11548. (let (new old)
  11549. (while commands
  11550. (setq old (pop commands) new (pop commands))
  11551. (if (fboundp 'command-remapping)
  11552. (org-defkey map (vector 'remap old) new)
  11553. (substitute-key-definition old new map global-map)))))
  11554. (when (eq org-enable-table-editor 'optimized)
  11555. ;; If the user wants maximum table support, we need to hijack
  11556. ;; some standard editing functions
  11557. (org-remap org-mode-map
  11558. 'self-insert-command 'org-self-insert-command
  11559. 'delete-char 'org-delete-char
  11560. 'delete-backward-char 'org-delete-backward-char)
  11561. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11562. (defun org-shiftcursor-error ()
  11563. "Throw an error because Shift-Cursor command was applied in wrong context."
  11564. (error "This command is active in special context like tables, headlines or timestamps"))
  11565. (defun org-shifttab (&optional arg)
  11566. "Global visibility cycling or move to previous table field.
  11567. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11568. on context.
  11569. See the individual commands for more information."
  11570. (interactive "P")
  11571. (cond
  11572. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11573. ((integerp arg)
  11574. (message "Content view to level: %d" arg)
  11575. (org-content (prefix-numeric-value arg))
  11576. (setq org-cycle-global-status 'overview))
  11577. (t (call-interactively 'org-global-cycle))))
  11578. (defun org-shiftmetaleft ()
  11579. "Promote subtree or delete table column.
  11580. Calls `org-promote-subtree', `org-outdent-item',
  11581. or `org-table-delete-column', depending on context.
  11582. See the individual commands for more information."
  11583. (interactive)
  11584. (cond
  11585. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11586. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11587. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11588. (t (org-shiftcursor-error))))
  11589. (defun org-shiftmetaright ()
  11590. "Demote subtree or insert table column.
  11591. Calls `org-demote-subtree', `org-indent-item',
  11592. or `org-table-insert-column', depending on context.
  11593. See the individual commands for more information."
  11594. (interactive)
  11595. (cond
  11596. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11597. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11598. ((org-at-item-p) (call-interactively 'org-indent-item))
  11599. (t (org-shiftcursor-error))))
  11600. (defun org-shiftmetaup (&optional arg)
  11601. "Move subtree up or kill table row.
  11602. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11603. `org-move-item-up' depending on context. See the individual commands
  11604. for more information."
  11605. (interactive "P")
  11606. (cond
  11607. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11608. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11609. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11610. (t (org-shiftcursor-error))))
  11611. (defun org-shiftmetadown (&optional arg)
  11612. "Move subtree down or insert table row.
  11613. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11614. `org-move-item-down', depending on context. See the individual
  11615. commands for more information."
  11616. (interactive "P")
  11617. (cond
  11618. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11619. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11620. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11621. (t (org-shiftcursor-error))))
  11622. (defun org-metaleft (&optional arg)
  11623. "Promote heading or move table column to left.
  11624. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11625. With no specific context, calls the Emacs default `backward-word'.
  11626. See the individual commands for more information."
  11627. (interactive "P")
  11628. (cond
  11629. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11630. ((or (org-on-heading-p) (org-region-active-p))
  11631. (call-interactively 'org-do-promote))
  11632. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11633. (t (call-interactively 'backward-word))))
  11634. (defun org-metaright (&optional arg)
  11635. "Demote subtree or move table column to right.
  11636. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11637. With no specific context, calls the Emacs default `forward-word'.
  11638. See the individual commands for more information."
  11639. (interactive "P")
  11640. (cond
  11641. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11642. ((or (org-on-heading-p) (org-region-active-p))
  11643. (call-interactively 'org-do-demote))
  11644. ((org-at-item-p) (call-interactively 'org-indent-item))
  11645. (t (call-interactively 'forward-word))))
  11646. (defun org-metaup (&optional arg)
  11647. "Move subtree up or move table row up.
  11648. Calls `org-move-subtree-up' or `org-table-move-row' or
  11649. `org-move-item-up', depending on context. See the individual commands
  11650. for more information."
  11651. (interactive "P")
  11652. (cond
  11653. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11654. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11655. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11656. (t (transpose-lines 1) (beginning-of-line -1))))
  11657. (defun org-metadown (&optional arg)
  11658. "Move subtree down or move table row down.
  11659. Calls `org-move-subtree-down' or `org-table-move-row' or
  11660. `org-move-item-down', depending on context. See the individual
  11661. commands for more information."
  11662. (interactive "P")
  11663. (cond
  11664. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11665. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11666. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11667. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11668. (defun org-shiftup (&optional arg)
  11669. "Increase item in timestamp or increase priority of current headline.
  11670. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11671. depending on context. See the individual commands for more information."
  11672. (interactive "P")
  11673. (cond
  11674. ((org-at-timestamp-p t)
  11675. (call-interactively (if org-edit-timestamp-down-means-later
  11676. 'org-timestamp-down 'org-timestamp-up)))
  11677. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11678. ((org-at-item-p) (call-interactively 'org-previous-item))
  11679. ((org-clocktable-try-shift 'up arg))
  11680. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11681. (defun org-shiftdown (&optional arg)
  11682. "Decrease item in timestamp or decrease priority of current headline.
  11683. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11684. depending on context. See the individual commands for more information."
  11685. (interactive "P")
  11686. (cond
  11687. ((org-at-timestamp-p t)
  11688. (call-interactively (if org-edit-timestamp-down-means-later
  11689. 'org-timestamp-up 'org-timestamp-down)))
  11690. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11691. ((org-clocktable-try-shift 'down arg))
  11692. (t (call-interactively 'org-next-item))))
  11693. (defun org-shiftright (&optional arg)
  11694. "Cycle the thing at point or in the current line, depending on context.
  11695. Depending on context, this does one of the following:
  11696. - switch a timestamp at point one day into the future
  11697. - on a headline, switch to the next TODO keyword.
  11698. - on an item, switch entire list to the next bullet type
  11699. - on a property line, switch to the next allowed value
  11700. - on a clocktable definition line, move time block into the future"
  11701. (interactive "P")
  11702. (cond
  11703. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11704. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11705. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11706. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11707. ((org-clocktable-try-shift 'right arg))
  11708. (t (org-shiftcursor-error))))
  11709. (defun org-shiftleft (&optional arg)
  11710. "Cycle the thing at point or in the current line, depending on context.
  11711. Depending on context, this does one of the following:
  11712. - switch a timestamp at point one day into the past
  11713. - on a headline, switch to the previous TODO keyword.
  11714. - on an item, switch entire list to the previous bullet type
  11715. - on a property line, switch to the previous allowed value
  11716. - on a clocktable definition line, move time block into the past"
  11717. (interactive "P")
  11718. (cond
  11719. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11720. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11721. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11722. ((org-at-property-p)
  11723. (call-interactively 'org-property-previous-allowed-value))
  11724. ((org-clocktable-try-shift 'left arg))
  11725. (t (org-shiftcursor-error))))
  11726. (defun org-shiftcontrolright ()
  11727. "Switch to next TODO set."
  11728. (interactive)
  11729. (cond
  11730. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11731. (t (org-shiftcursor-error))))
  11732. (defun org-shiftcontrolleft ()
  11733. "Switch to previous TODO set."
  11734. (interactive)
  11735. (cond
  11736. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11737. (t (org-shiftcursor-error))))
  11738. (defun org-ctrl-c-ret ()
  11739. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11740. (interactive)
  11741. (cond
  11742. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11743. (t (call-interactively 'org-insert-heading))))
  11744. (defun org-copy-special ()
  11745. "Copy region in table or copy current subtree.
  11746. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11747. See the individual commands for more information."
  11748. (interactive)
  11749. (call-interactively
  11750. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11751. (defun org-cut-special ()
  11752. "Cut region in table or cut current subtree.
  11753. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11754. See the individual commands for more information."
  11755. (interactive)
  11756. (call-interactively
  11757. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11758. (defun org-paste-special (arg)
  11759. "Paste rectangular region into table, or past subtree relative to level.
  11760. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11761. See the individual commands for more information."
  11762. (interactive "P")
  11763. (if (org-at-table-p)
  11764. (org-table-paste-rectangle)
  11765. (org-paste-subtree arg)))
  11766. (defun org-edit-special ()
  11767. "Call a special editor for the stuff at point.
  11768. When at a table, call the formula editor with `org-table-edit-formulas'.
  11769. When at the first line of an src example, call `org-edit-src-code'.
  11770. When in an #+include line, visit the include file. Otherwise call
  11771. `ffap' to visit the file at point."
  11772. (interactive)
  11773. (cond
  11774. ((org-at-table-p)
  11775. (call-interactively 'org-table-edit-formulas))
  11776. ((save-excursion
  11777. (beginning-of-line 1)
  11778. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11779. (find-file (org-trim (match-string 1))))
  11780. ((org-edit-src-code))
  11781. ((org-edit-fixed-width-region))
  11782. (t (call-interactively 'ffap))))
  11783. (defun org-ctrl-c-ctrl-c (&optional arg)
  11784. "Set tags in headline, or update according to changed information at point.
  11785. This command does many different things, depending on context:
  11786. - If the cursor is in a headline, prompt for tags and insert them
  11787. into the current line, aligned to `org-tags-column'. When called
  11788. with prefix arg, realign all tags in the current buffer.
  11789. - If the cursor is in one of the special #+KEYWORD lines, this
  11790. triggers scanning the buffer for these lines and updating the
  11791. information.
  11792. - If the cursor is inside a table, realign the table. This command
  11793. works even if the automatic table editor has been turned off.
  11794. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11795. the entire table.
  11796. - If the cursor is a the beginning of a dynamic block, update it.
  11797. - If the cursor is inside a table created by the table.el package,
  11798. activate that table.
  11799. - If the current buffer is a remember buffer, close note and file
  11800. it. A prefix argument of 1 files to the default location
  11801. without further interaction. A prefix argument of 2 files to
  11802. the currently clocking task.
  11803. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11804. links in this buffer.
  11805. - If the cursor is on a numbered item in a plain list, renumber the
  11806. ordered list.
  11807. - If the cursor is on a checkbox, toggle it."
  11808. (interactive "P")
  11809. (let ((org-enable-table-editor t))
  11810. (cond
  11811. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11812. org-occur-highlights
  11813. org-latex-fragment-image-overlays)
  11814. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11815. (org-remove-occur-highlights)
  11816. (org-remove-latex-fragment-image-overlays)
  11817. (message "Temporary highlights/overlays removed from current buffer"))
  11818. ((and (local-variable-p 'org-finish-function (current-buffer))
  11819. (fboundp org-finish-function))
  11820. (funcall org-finish-function))
  11821. ((org-at-property-p)
  11822. (call-interactively 'org-property-action))
  11823. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11824. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11825. ((org-at-table.el-p)
  11826. (require 'table)
  11827. (beginning-of-line 1)
  11828. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11829. (call-interactively 'table-recognize-table))
  11830. ((org-at-table-p)
  11831. (org-table-maybe-eval-formula)
  11832. (if arg
  11833. (call-interactively 'org-table-recalculate)
  11834. (org-table-maybe-recalculate-line))
  11835. (call-interactively 'org-table-align))
  11836. ((org-at-item-checkbox-p)
  11837. (call-interactively 'org-toggle-checkbox))
  11838. ((org-at-item-p)
  11839. (call-interactively 'org-maybe-renumber-ordered-list))
  11840. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11841. ;; Dynamic block
  11842. (beginning-of-line 1)
  11843. (save-excursion (org-update-dblock)))
  11844. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11845. (cond
  11846. ((equal (match-string 1) "TBLFM")
  11847. ;; Recalculate the table before this line
  11848. (save-excursion
  11849. (beginning-of-line 1)
  11850. (skip-chars-backward " \r\n\t")
  11851. (if (org-at-table-p)
  11852. (org-call-with-arg 'org-table-recalculate t))))
  11853. (t
  11854. ; (org-set-regexps-and-options)
  11855. ; (org-restart-font-lock)
  11856. (let ((org-inhibit-startup t)) (org-mode-restart))
  11857. (message "Local setup has been refreshed"))))
  11858. (t (error "C-c C-c can do nothing useful at this location.")))))
  11859. (defun org-mode-restart ()
  11860. "Restart Org-mode, to scan again for special lines.
  11861. Also updates the keyword regular expressions."
  11862. (interactive)
  11863. (org-mode)
  11864. (message "Org-mode restarted"))
  11865. (defun org-kill-note-or-show-branches ()
  11866. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11867. (interactive)
  11868. (if (not org-finish-function)
  11869. (call-interactively 'show-branches)
  11870. (let ((org-note-abort t))
  11871. (funcall org-finish-function))))
  11872. (defun org-return (&optional indent)
  11873. "Goto next table row or insert a newline.
  11874. Calls `org-table-next-row' or `newline', depending on context.
  11875. See the individual commands for more information."
  11876. (interactive)
  11877. (cond
  11878. ((bobp) (if indent (newline-and-indent) (newline)))
  11879. ((and (org-at-heading-p)
  11880. (looking-at
  11881. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11882. (org-show-entry)
  11883. (end-of-line 1)
  11884. (newline))
  11885. ((org-at-table-p)
  11886. (org-table-justify-field-maybe)
  11887. (call-interactively 'org-table-next-row))
  11888. (t (if indent (newline-and-indent) (newline)))))
  11889. (defun org-return-indent ()
  11890. "Goto next table row or insert a newline and indent.
  11891. Calls `org-table-next-row' or `newline-and-indent', depending on
  11892. context. See the individual commands for more information."
  11893. (interactive)
  11894. (org-return t))
  11895. (defun org-ctrl-c-star ()
  11896. "Compute table, or change heading status of lines.
  11897. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11898. depending on context. This will also turn a plain list item or a normal
  11899. line into a subheading."
  11900. (interactive)
  11901. (cond
  11902. ((org-at-table-p)
  11903. (call-interactively 'org-table-recalculate))
  11904. ((org-region-active-p)
  11905. ;; Convert all lines in region to list items
  11906. (call-interactively 'org-toggle-region-headings))
  11907. ((org-on-heading-p)
  11908. (org-toggle-region-headings (point-at-bol)
  11909. (min (1+ (point-at-eol)) (point-max))))
  11910. ((org-at-item-p)
  11911. ;; Convert to heading
  11912. (let ((level (save-match-data
  11913. (save-excursion
  11914. (condition-case nil
  11915. (progn
  11916. (org-back-to-heading t)
  11917. (funcall outline-level))
  11918. (error 0))))))
  11919. (replace-match
  11920. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11921. (t (org-toggle-region-headings (point-at-bol)
  11922. (min (1+ (point-at-eol)) (point-max))))))
  11923. (defun org-ctrl-c-minus ()
  11924. "Insert separator line in table or modify bullet status of line.
  11925. Also turns a plain line or a region of lines into list items.
  11926. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11927. `org-cycle-list-bullet', depending on context."
  11928. (interactive)
  11929. (cond
  11930. ((org-at-table-p)
  11931. (call-interactively 'org-table-insert-hline))
  11932. ((org-on-heading-p)
  11933. ;; Convert to item
  11934. (save-excursion
  11935. (beginning-of-line 1)
  11936. (if (looking-at "\\*+ ")
  11937. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11938. ((org-region-active-p)
  11939. ;; Convert all lines in region to list items
  11940. (call-interactively 'org-toggle-region-items))
  11941. ((org-in-item-p)
  11942. (call-interactively 'org-cycle-list-bullet))
  11943. (t (org-toggle-region-items (point-at-bol)
  11944. (min (1+ (point-at-eol)) (point-max))))))
  11945. (defun org-toggle-region-items (beg end)
  11946. "Convert all lines in region to list items.
  11947. If the first line is already an item, convert all list items in the region
  11948. to normal lines."
  11949. (interactive "r")
  11950. (let (l2 l)
  11951. (save-excursion
  11952. (goto-char end)
  11953. (setq l2 (org-current-line))
  11954. (goto-char beg)
  11955. (beginning-of-line 1)
  11956. (setq l (1- (org-current-line)))
  11957. (if (org-at-item-p)
  11958. ;; We already have items, de-itemize
  11959. (while (< (setq l (1+ l)) l2)
  11960. (when (org-at-item-p)
  11961. (goto-char (match-beginning 2))
  11962. (delete-region (match-beginning 2) (match-end 2))
  11963. (and (looking-at "[ \t]+") (replace-match "")))
  11964. (beginning-of-line 2))
  11965. (while (< (setq l (1+ l)) l2)
  11966. (unless (org-at-item-p)
  11967. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11968. (replace-match "\\1- \\2")))
  11969. (beginning-of-line 2))))))
  11970. (defun org-toggle-region-headings (beg end)
  11971. "Convert all lines in region to list items.
  11972. If the first line is already an item, convert all list items in the region
  11973. to normal lines."
  11974. (interactive "r")
  11975. (let (l2 l)
  11976. (save-excursion
  11977. (goto-char end)
  11978. (setq l2 (org-current-line))
  11979. (goto-char beg)
  11980. (beginning-of-line 1)
  11981. (setq l (1- (org-current-line)))
  11982. (if (org-on-heading-p)
  11983. ;; We already have headlines, de-star them
  11984. (while (< (setq l (1+ l)) l2)
  11985. (when (org-on-heading-p t)
  11986. (and (looking-at outline-regexp) (replace-match "")))
  11987. (beginning-of-line 2))
  11988. (let* ((stars (save-excursion
  11989. (re-search-backward org-complex-heading-regexp nil t)
  11990. (or (match-string 1) "*")))
  11991. (add-stars (if org-odd-levels-only "**" "*"))
  11992. (rpl (concat stars add-stars " \\2")))
  11993. (while (< (setq l (1+ l)) l2)
  11994. (unless (org-on-heading-p)
  11995. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11996. (replace-match rpl)))
  11997. (beginning-of-line 2)))))))
  11998. (defun org-meta-return (&optional arg)
  11999. "Insert a new heading or wrap a region in a table.
  12000. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12001. See the individual commands for more information."
  12002. (interactive "P")
  12003. (cond
  12004. ((org-at-table-p)
  12005. (call-interactively 'org-table-wrap-region))
  12006. (t (call-interactively 'org-insert-heading))))
  12007. ;;; Menu entries
  12008. ;; Define the Org-mode menus
  12009. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12010. '("Tbl"
  12011. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12012. ["Next Field" org-cycle (org-at-table-p)]
  12013. ["Previous Field" org-shifttab (org-at-table-p)]
  12014. ["Next Row" org-return (org-at-table-p)]
  12015. "--"
  12016. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12017. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12018. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12019. "--"
  12020. ("Column"
  12021. ["Move Column Left" org-metaleft (org-at-table-p)]
  12022. ["Move Column Right" org-metaright (org-at-table-p)]
  12023. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12024. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12025. ("Row"
  12026. ["Move Row Up" org-metaup (org-at-table-p)]
  12027. ["Move Row Down" org-metadown (org-at-table-p)]
  12028. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12029. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12030. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12031. "--"
  12032. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12033. ("Rectangle"
  12034. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12035. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12036. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12037. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12038. "--"
  12039. ("Calculate"
  12040. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12041. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12042. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12043. "--"
  12044. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12045. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12046. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12047. "--"
  12048. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12049. "--"
  12050. ["Sum Column/Rectangle" org-table-sum
  12051. (or (org-at-table-p) (org-region-active-p))]
  12052. ["Which Column?" org-table-current-column (org-at-table-p)])
  12053. ["Debug Formulas"
  12054. org-table-toggle-formula-debugger
  12055. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12056. ["Show Col/Row Numbers"
  12057. org-table-toggle-coordinate-overlays
  12058. :style toggle
  12059. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12060. "--"
  12061. ["Create" org-table-create (and (not (org-at-table-p))
  12062. org-enable-table-editor)]
  12063. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12064. ["Import from File" org-table-import (not (org-at-table-p))]
  12065. ["Export to File" org-table-export (org-at-table-p)]
  12066. "--"
  12067. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12068. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12069. '("Org"
  12070. ("Show/Hide"
  12071. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12072. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12073. ["Sparse Tree..." org-sparse-tree t]
  12074. ["Reveal Context" org-reveal t]
  12075. ["Show All" show-all t]
  12076. "--"
  12077. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12078. "--"
  12079. ["New Heading" org-insert-heading t]
  12080. ("Navigate Headings"
  12081. ["Up" outline-up-heading t]
  12082. ["Next" outline-next-visible-heading t]
  12083. ["Previous" outline-previous-visible-heading t]
  12084. ["Next Same Level" outline-forward-same-level t]
  12085. ["Previous Same Level" outline-backward-same-level t]
  12086. "--"
  12087. ["Jump" org-goto t])
  12088. ("Edit Structure"
  12089. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12090. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12091. "--"
  12092. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12093. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12094. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12095. "--"
  12096. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12097. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12098. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12099. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12100. "--"
  12101. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12102. "--"
  12103. ["Convert to odd levels" org-convert-to-odd-levels t]
  12104. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12105. ("Editing"
  12106. ["Emphasis..." org-emphasize t]
  12107. ["Edit Source Example" org-edit-special t])
  12108. ("Archive"
  12109. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12110. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12111. ; :active t :keys "C-u C-c C-x C-a"]
  12112. ["Sparse trees open ARCHIVE trees"
  12113. (setq org-sparse-tree-open-archived-trees
  12114. (not org-sparse-tree-open-archived-trees))
  12115. :style toggle :selected org-sparse-tree-open-archived-trees]
  12116. ["Cycling opens ARCHIVE trees"
  12117. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12118. :style toggle :selected org-cycle-open-archived-trees]
  12119. "--"
  12120. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12121. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12122. ; ["Check and Move Children" (org-archive-subtree '(4))
  12123. ; :active t :keys "C-u C-c C-x C-s"]
  12124. )
  12125. "--"
  12126. ("TODO Lists"
  12127. ["TODO/DONE/-" org-todo t]
  12128. ("Select keyword"
  12129. ["Next keyword" org-shiftright (org-on-heading-p)]
  12130. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12131. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12132. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12133. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12134. ["Show TODO Tree" org-show-todo-tree t]
  12135. ["Global TODO list" org-todo-list t]
  12136. "--"
  12137. ["Set Priority" org-priority t]
  12138. ["Priority Up" org-shiftup t]
  12139. ["Priority Down" org-shiftdown t])
  12140. ("TAGS and Properties"
  12141. ["Set Tags" 'org-set-tags-command t]
  12142. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12143. "--"
  12144. ["Set property" 'org-set-property t]
  12145. ["Column view of properties" org-columns t]
  12146. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12147. ("Dates and Scheduling"
  12148. ["Timestamp" org-time-stamp t]
  12149. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12150. ("Change Date"
  12151. ["1 Day Later" org-shiftright t]
  12152. ["1 Day Earlier" org-shiftleft t]
  12153. ["1 ... Later" org-shiftup t]
  12154. ["1 ... Earlier" org-shiftdown t])
  12155. ["Compute Time Range" org-evaluate-time-range t]
  12156. ["Schedule Item" org-schedule t]
  12157. ["Deadline" org-deadline t]
  12158. "--"
  12159. ["Custom time format" org-toggle-time-stamp-overlays
  12160. :style radio :selected org-display-custom-times]
  12161. "--"
  12162. ["Goto Calendar" org-goto-calendar t]
  12163. ["Date from Calendar" org-date-from-calendar t]
  12164. "--"
  12165. ["Start/restart timer" org-timer-start t]
  12166. ["Insert timer string" org-timer t]
  12167. ["Insert timer item" org-timer-item t])
  12168. ("Logging work"
  12169. ["Clock in" org-clock-in t]
  12170. ["Clock out" org-clock-out t]
  12171. ["Clock cancel" org-clock-cancel t]
  12172. ["Goto running clock" org-clock-goto t]
  12173. ["Display times" org-clock-display t]
  12174. ["Create clock table" org-clock-report t]
  12175. "--"
  12176. ["Record DONE time"
  12177. (progn (setq org-log-done (not org-log-done))
  12178. (message "Switching to %s will %s record a timestamp"
  12179. (car org-done-keywords)
  12180. (if org-log-done "automatically" "not")))
  12181. :style toggle :selected org-log-done])
  12182. "--"
  12183. ["Agenda Command..." org-agenda t]
  12184. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12185. ("File List for Agenda")
  12186. ("Special views current file"
  12187. ["TODO Tree" org-show-todo-tree t]
  12188. ["Check Deadlines" org-check-deadlines t]
  12189. ["Timeline" org-timeline t]
  12190. ["Tags Tree" org-tags-sparse-tree t])
  12191. "--"
  12192. ("Hyperlinks"
  12193. ["Store Link (Global)" org-store-link t]
  12194. ["Insert Link" org-insert-link t]
  12195. ["Follow Link" org-open-at-point t]
  12196. "--"
  12197. ["Next link" org-next-link t]
  12198. ["Previous link" org-previous-link t]
  12199. "--"
  12200. ["Descriptive Links"
  12201. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12202. :style radio
  12203. :selected (member '(org-link) buffer-invisibility-spec)]
  12204. ["Literal Links"
  12205. (progn
  12206. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12207. :style radio
  12208. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12209. "--"
  12210. ["Export/Publish..." org-export t]
  12211. ("LaTeX"
  12212. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12213. :selected org-cdlatex-mode]
  12214. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12215. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12216. ["Modify math symbol" org-cdlatex-math-modify
  12217. (org-inside-LaTeX-fragment-p)]
  12218. ["Export LaTeX fragments as images"
  12219. (if (featurep 'org-exp)
  12220. (setq org-export-with-LaTeX-fragments
  12221. (not org-export-with-LaTeX-fragments))
  12222. (require 'org-exp))
  12223. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12224. org-export-with-LaTeX-fragments)])
  12225. "--"
  12226. ("Documentation"
  12227. ["Show Version" org-version t]
  12228. ["Info Documentation" org-info t])
  12229. ("Customize"
  12230. ["Browse Org Group" org-customize t]
  12231. "--"
  12232. ["Expand This Menu" org-create-customize-menu
  12233. (fboundp 'customize-menu-create)])
  12234. "--"
  12235. ["Refresh setup" org-mode-restart t]
  12236. ))
  12237. (defun org-info (&optional node)
  12238. "Read documentation for Org-mode in the info system.
  12239. With optional NODE, go directly to that node."
  12240. (interactive)
  12241. (info (format "(org)%s" (or node ""))))
  12242. (defun org-install-agenda-files-menu ()
  12243. (let ((bl (buffer-list)))
  12244. (save-excursion
  12245. (while bl
  12246. (set-buffer (pop bl))
  12247. (if (org-mode-p) (setq bl nil)))
  12248. (when (org-mode-p)
  12249. (easy-menu-change
  12250. '("Org") "File List for Agenda"
  12251. (append
  12252. (list
  12253. ["Edit File List" (org-edit-agenda-file-list) t]
  12254. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12255. ["Remove Current File from List" org-remove-file t]
  12256. ["Cycle through agenda files" org-cycle-agenda-files t]
  12257. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12258. "--")
  12259. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12260. ;;;; Documentation
  12261. ;;;###autoload
  12262. (defun org-require-autoloaded-modules ()
  12263. (interactive)
  12264. (mapc 'require
  12265. '(org-agenda org-archive org-clock org-colview
  12266. org-exp org-id org-export-latex org-publish
  12267. org-remember org-table)))
  12268. ;;;###autoload
  12269. (defun org-customize ()
  12270. "Call the customize function with org as argument."
  12271. (interactive)
  12272. (org-load-modules-maybe)
  12273. (org-require-autoloaded-modules)
  12274. (customize-browse 'org))
  12275. (defun org-create-customize-menu ()
  12276. "Create a full customization menu for Org-mode, insert it into the menu."
  12277. (interactive)
  12278. (org-load-modules-maybe)
  12279. (org-require-autoloaded-modules)
  12280. (if (fboundp 'customize-menu-create)
  12281. (progn
  12282. (easy-menu-change
  12283. '("Org") "Customize"
  12284. `(["Browse Org group" org-customize t]
  12285. "--"
  12286. ,(customize-menu-create 'org)
  12287. ["Set" Custom-set t]
  12288. ["Save" Custom-save t]
  12289. ["Reset to Current" Custom-reset-current t]
  12290. ["Reset to Saved" Custom-reset-saved t]
  12291. ["Reset to Standard Settings" Custom-reset-standard t]))
  12292. (message "\"Org\"-menu now contains full customization menu"))
  12293. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12294. ;;;; Miscellaneous stuff
  12295. ;;; Generally useful functions
  12296. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12297. "Display the given MESSAGE as a warning."
  12298. (if (fboundp 'display-warning)
  12299. (display-warning 'org message
  12300. (if (featurep 'xemacs)
  12301. 'warning
  12302. :warning))
  12303. (let ((buf (get-buffer-create "*Org warnings*")))
  12304. (with-current-buffer buf
  12305. (goto-char (point-max))
  12306. (insert "Warning (Org): " message)
  12307. (unless (bolp)
  12308. (newline)))
  12309. (display-buffer buf)
  12310. (sit-for 0))))
  12311. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12312. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12313. (if (and marker (marker-buffer marker)
  12314. (buffer-live-p (marker-buffer marker)))
  12315. (progn
  12316. (switch-to-buffer (marker-buffer marker))
  12317. (if (or (> marker (point-max)) (< marker (point-min)))
  12318. (widen))
  12319. (goto-char marker))
  12320. (if bookmark
  12321. (bookmark-jump bookmark)
  12322. (error "Cannot find location"))))
  12323. (defun org-quote-csv-field (s)
  12324. "Quote field for inclusion in CSV material."
  12325. (if (string-match "[\",]" s)
  12326. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12327. s))
  12328. (defun org-plist-delete (plist property)
  12329. "Delete PROPERTY from PLIST.
  12330. This is in contrast to merely setting it to 0."
  12331. (let (p)
  12332. (while plist
  12333. (if (not (eq property (car plist)))
  12334. (setq p (plist-put p (car plist) (nth 1 plist))))
  12335. (setq plist (cddr plist)))
  12336. p))
  12337. (defun org-force-self-insert (N)
  12338. "Needed to enforce self-insert under remapping."
  12339. (interactive "p")
  12340. (self-insert-command N))
  12341. (defun org-string-width (s)
  12342. "Compute width of string, ignoring invisible characters.
  12343. This ignores character with invisibility property `org-link', and also
  12344. characters with property `org-cwidth', because these will become invisible
  12345. upon the next fontification round."
  12346. (let (b l)
  12347. (when (or (eq t buffer-invisibility-spec)
  12348. (assq 'org-link buffer-invisibility-spec))
  12349. (while (setq b (text-property-any 0 (length s)
  12350. 'invisible 'org-link s))
  12351. (setq s (concat (substring s 0 b)
  12352. (substring s (or (next-single-property-change
  12353. b 'invisible s) (length s)))))))
  12354. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12355. (setq s (concat (substring s 0 b)
  12356. (substring s (or (next-single-property-change
  12357. b 'org-cwidth s) (length s))))))
  12358. (setq l (string-width s) b -1)
  12359. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12360. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12361. l))
  12362. (defun org-get-indentation (&optional line)
  12363. "Get the indentation of the current line, interpreting tabs.
  12364. When LINE is given, assume it represents a line and compute its indentation."
  12365. (if line
  12366. (if (string-match "^ *" (org-remove-tabs line))
  12367. (match-end 0))
  12368. (save-excursion
  12369. (beginning-of-line 1)
  12370. (skip-chars-forward " \t")
  12371. (current-column))))
  12372. (defun org-remove-tabs (s &optional width)
  12373. "Replace tabulators in S with spaces.
  12374. Assumes that s is a single line, starting in column 0."
  12375. (setq width (or width tab-width))
  12376. (while (string-match "\t" s)
  12377. (setq s (replace-match
  12378. (make-string
  12379. (- (* width (/ (+ (match-beginning 0) width) width))
  12380. (match-beginning 0)) ?\ )
  12381. t t s)))
  12382. s)
  12383. (defun org-fix-indentation (line ind)
  12384. "Fix indentation in LINE.
  12385. IND is a cons cell with target and minimum indentation.
  12386. If the current indenation in LINE is smaller than the minimum,
  12387. leave it alone. If it is larger than ind, set it to the target."
  12388. (let* ((l (org-remove-tabs line))
  12389. (i (org-get-indentation l))
  12390. (i1 (car ind)) (i2 (cdr ind)))
  12391. (if (>= i i2) (setq l (substring line i2)))
  12392. (if (> i1 0)
  12393. (concat (make-string i1 ?\ ) l)
  12394. l)))
  12395. (defun org-base-buffer (buffer)
  12396. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12397. (if (not buffer)
  12398. buffer
  12399. (or (buffer-base-buffer buffer)
  12400. buffer)))
  12401. (defun org-trim (s)
  12402. "Remove whitespace at beginning and end of string."
  12403. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12404. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12405. s)
  12406. (defun org-wrap (string &optional width lines)
  12407. "Wrap string to either a number of lines, or a width in characters.
  12408. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12409. that costs. If there is a word longer than WIDTH, the text is actually
  12410. wrapped to the length of that word.
  12411. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12412. many lines, whatever width that takes.
  12413. The return value is a list of lines, without newlines at the end."
  12414. (let* ((words (org-split-string string "[ \t\n]+"))
  12415. (maxword (apply 'max (mapcar 'org-string-width words)))
  12416. w ll)
  12417. (cond (width
  12418. (org-do-wrap words (max maxword width)))
  12419. (lines
  12420. (setq w maxword)
  12421. (setq ll (org-do-wrap words maxword))
  12422. (if (<= (length ll) lines)
  12423. ll
  12424. (setq ll words)
  12425. (while (> (length ll) lines)
  12426. (setq w (1+ w))
  12427. (setq ll (org-do-wrap words w)))
  12428. ll))
  12429. (t (error "Cannot wrap this")))))
  12430. (defun org-do-wrap (words width)
  12431. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12432. (let (lines line)
  12433. (while words
  12434. (setq line (pop words))
  12435. (while (and words (< (+ (length line) (length (car words))) width))
  12436. (setq line (concat line " " (pop words))))
  12437. (setq lines (push line lines)))
  12438. (nreverse lines)))
  12439. (defun org-split-string (string &optional separators)
  12440. "Splits STRING into substrings at SEPARATORS.
  12441. No empty strings are returned if there are matches at the beginning
  12442. and end of string."
  12443. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12444. (start 0)
  12445. notfirst
  12446. (list nil))
  12447. (while (and (string-match rexp string
  12448. (if (and notfirst
  12449. (= start (match-beginning 0))
  12450. (< start (length string)))
  12451. (1+ start) start))
  12452. (< (match-beginning 0) (length string)))
  12453. (setq notfirst t)
  12454. (or (eq (match-beginning 0) 0)
  12455. (and (eq (match-beginning 0) (match-end 0))
  12456. (eq (match-beginning 0) start))
  12457. (setq list
  12458. (cons (substring string start (match-beginning 0))
  12459. list)))
  12460. (setq start (match-end 0)))
  12461. (or (eq start (length string))
  12462. (setq list
  12463. (cons (substring string start)
  12464. list)))
  12465. (nreverse list)))
  12466. (defun org-context ()
  12467. "Return a list of contexts of the current cursor position.
  12468. If several contexts apply, all are returned.
  12469. Each context entry is a list with a symbol naming the context, and
  12470. two positions indicating start and end of the context. Possible
  12471. contexts are:
  12472. :headline anywhere in a headline
  12473. :headline-stars on the leading stars in a headline
  12474. :todo-keyword on a TODO keyword (including DONE) in a headline
  12475. :tags on the TAGS in a headline
  12476. :priority on the priority cookie in a headline
  12477. :item on the first line of a plain list item
  12478. :item-bullet on the bullet/number of a plain list item
  12479. :checkbox on the checkbox in a plain list item
  12480. :table in an org-mode table
  12481. :table-special on a special filed in a table
  12482. :table-table in a table.el table
  12483. :link on a hyperlink
  12484. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12485. :target on a <<target>>
  12486. :radio-target on a <<<radio-target>>>
  12487. :latex-fragment on a LaTeX fragment
  12488. :latex-preview on a LaTeX fragment with overlayed preview image
  12489. This function expects the position to be visible because it uses font-lock
  12490. faces as a help to recognize the following contexts: :table-special, :link,
  12491. and :keyword."
  12492. (let* ((f (get-text-property (point) 'face))
  12493. (faces (if (listp f) f (list f)))
  12494. (p (point)) clist o)
  12495. ;; First the large context
  12496. (cond
  12497. ((org-on-heading-p t)
  12498. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12499. (when (progn
  12500. (beginning-of-line 1)
  12501. (looking-at org-todo-line-tags-regexp))
  12502. (push (org-point-in-group p 1 :headline-stars) clist)
  12503. (push (org-point-in-group p 2 :todo-keyword) clist)
  12504. (push (org-point-in-group p 4 :tags) clist))
  12505. (goto-char p)
  12506. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12507. (if (looking-at "\\[#[A-Z0-9]\\]")
  12508. (push (org-point-in-group p 0 :priority) clist)))
  12509. ((org-at-item-p)
  12510. (push (org-point-in-group p 2 :item-bullet) clist)
  12511. (push (list :item (point-at-bol)
  12512. (save-excursion (org-end-of-item) (point)))
  12513. clist)
  12514. (and (org-at-item-checkbox-p)
  12515. (push (org-point-in-group p 0 :checkbox) clist)))
  12516. ((org-at-table-p)
  12517. (push (list :table (org-table-begin) (org-table-end)) clist)
  12518. (if (memq 'org-formula faces)
  12519. (push (list :table-special
  12520. (previous-single-property-change p 'face)
  12521. (next-single-property-change p 'face)) clist)))
  12522. ((org-at-table-p 'any)
  12523. (push (list :table-table) clist)))
  12524. (goto-char p)
  12525. ;; Now the small context
  12526. (cond
  12527. ((org-at-timestamp-p)
  12528. (push (org-point-in-group p 0 :timestamp) clist))
  12529. ((memq 'org-link faces)
  12530. (push (list :link
  12531. (previous-single-property-change p 'face)
  12532. (next-single-property-change p 'face)) clist))
  12533. ((memq 'org-special-keyword faces)
  12534. (push (list :keyword
  12535. (previous-single-property-change p 'face)
  12536. (next-single-property-change p 'face)) clist))
  12537. ((org-on-target-p)
  12538. (push (org-point-in-group p 0 :target) clist)
  12539. (goto-char (1- (match-beginning 0)))
  12540. (if (looking-at org-radio-target-regexp)
  12541. (push (org-point-in-group p 0 :radio-target) clist))
  12542. (goto-char p))
  12543. ((setq o (car (delq nil
  12544. (mapcar
  12545. (lambda (x)
  12546. (if (memq x org-latex-fragment-image-overlays) x))
  12547. (org-overlays-at (point))))))
  12548. (push (list :latex-fragment
  12549. (org-overlay-start o) (org-overlay-end o)) clist)
  12550. (push (list :latex-preview
  12551. (org-overlay-start o) (org-overlay-end o)) clist))
  12552. ((org-inside-LaTeX-fragment-p)
  12553. ;; FIXME: positions wrong.
  12554. (push (list :latex-fragment (point) (point)) clist)))
  12555. (setq clist (nreverse (delq nil clist)))
  12556. clist))
  12557. ;; FIXME: Compare with at-regexp-p Do we need both?
  12558. (defun org-in-regexp (re &optional nlines visually)
  12559. "Check if point is inside a match of regexp.
  12560. Normally only the current line is checked, but you can include NLINES extra
  12561. lines both before and after point into the search.
  12562. If VISUALLY is set, require that the cursor is not after the match but
  12563. really on, so that the block visually is on the match."
  12564. (catch 'exit
  12565. (let ((pos (point))
  12566. (eol (point-at-eol (+ 1 (or nlines 0))))
  12567. (inc (if visually 1 0)))
  12568. (save-excursion
  12569. (beginning-of-line (- 1 (or nlines 0)))
  12570. (while (re-search-forward re eol t)
  12571. (if (and (<= (match-beginning 0) pos)
  12572. (>= (+ inc (match-end 0)) pos))
  12573. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12574. (defun org-at-regexp-p (regexp)
  12575. "Is point inside a match of REGEXP in the current line?"
  12576. (catch 'exit
  12577. (save-excursion
  12578. (let ((pos (point)) (end (point-at-eol)))
  12579. (beginning-of-line 1)
  12580. (while (re-search-forward regexp end t)
  12581. (if (and (<= (match-beginning 0) pos)
  12582. (>= (match-end 0) pos))
  12583. (throw 'exit t)))
  12584. nil))))
  12585. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12586. "Call `multi-occur' with buffers for all agenda files."
  12587. (interactive "sOrg-files matching: \np")
  12588. (let* ((files (org-agenda-files))
  12589. (tnames (mapcar 'file-truename files))
  12590. (extra org-agenda-text-search-extra-files)
  12591. f)
  12592. (when (eq (car extra) 'agenda-archives)
  12593. (setq extra (cdr extra))
  12594. (setq files (org-add-archive-files files)))
  12595. (while (setq f (pop extra))
  12596. (unless (member (file-truename f) tnames)
  12597. (add-to-list 'files f 'append)
  12598. (add-to-list 'tnames (file-truename f) 'append)))
  12599. (multi-occur
  12600. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12601. regexp)))
  12602. (if (boundp 'occur-mode-find-occurrence-hook)
  12603. ;; Emacs 23
  12604. (add-hook 'occur-mode-find-occurrence-hook
  12605. (lambda ()
  12606. (when (org-mode-p)
  12607. (org-reveal))))
  12608. ;; Emacs 22
  12609. (defadvice occur-mode-goto-occurrence
  12610. (after org-occur-reveal activate)
  12611. (and (org-mode-p) (org-reveal)))
  12612. (defadvice occur-mode-goto-occurrence-other-window
  12613. (after org-occur-reveal activate)
  12614. (and (org-mode-p) (org-reveal)))
  12615. (defadvice occur-mode-display-occurrence
  12616. (after org-occur-reveal activate)
  12617. (when (org-mode-p)
  12618. (let ((pos (occur-mode-find-occurrence)))
  12619. (with-current-buffer (marker-buffer pos)
  12620. (save-excursion
  12621. (goto-char pos)
  12622. (org-reveal)))))))
  12623. (defun org-uniquify (list)
  12624. "Remove duplicate elements from LIST."
  12625. (let (res)
  12626. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12627. res))
  12628. (defun org-delete-all (elts list)
  12629. "Remove all elements in ELTS from LIST."
  12630. (while elts
  12631. (setq list (delete (pop elts) list)))
  12632. list)
  12633. (defun org-back-over-empty-lines ()
  12634. "Move backwards over witespace, to the beginning of the first empty line.
  12635. Returns the number of empty lines passed."
  12636. (let ((pos (point)))
  12637. (skip-chars-backward " \t\n\r")
  12638. (beginning-of-line 2)
  12639. (goto-char (min (point) pos))
  12640. (count-lines (point) pos)))
  12641. (defun org-skip-whitespace ()
  12642. (skip-chars-forward " \t\n\r"))
  12643. (defun org-point-in-group (point group &optional context)
  12644. "Check if POINT is in match-group GROUP.
  12645. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12646. match. If the match group does ot exist or point is not inside it,
  12647. return nil."
  12648. (and (match-beginning group)
  12649. (>= point (match-beginning group))
  12650. (<= point (match-end group))
  12651. (if context
  12652. (list context (match-beginning group) (match-end group))
  12653. t)))
  12654. (defun org-switch-to-buffer-other-window (&rest args)
  12655. "Switch to buffer in a second window on the current frame.
  12656. In particular, do not allow pop-up frames."
  12657. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12658. special-display-function)
  12659. (apply 'switch-to-buffer-other-window args)))
  12660. (defun org-combine-plists (&rest plists)
  12661. "Create a single property list from all plists in PLISTS.
  12662. The process starts by copying the first list, and then setting properties
  12663. from the other lists. Settings in the last list are the most significant
  12664. ones and overrule settings in the other lists."
  12665. (let ((rtn (copy-sequence (pop plists)))
  12666. p v ls)
  12667. (while plists
  12668. (setq ls (pop plists))
  12669. (while ls
  12670. (setq p (pop ls) v (pop ls))
  12671. (setq rtn (plist-put rtn p v))))
  12672. rtn))
  12673. (defun org-move-line-down (arg)
  12674. "Move the current line down. With prefix argument, move it past ARG lines."
  12675. (interactive "p")
  12676. (let ((col (current-column))
  12677. beg end pos)
  12678. (beginning-of-line 1) (setq beg (point))
  12679. (beginning-of-line 2) (setq end (point))
  12680. (beginning-of-line (+ 1 arg))
  12681. (setq pos (move-marker (make-marker) (point)))
  12682. (insert (delete-and-extract-region beg end))
  12683. (goto-char pos)
  12684. (org-move-to-column col)))
  12685. (defun org-move-line-up (arg)
  12686. "Move the current line up. With prefix argument, move it past ARG lines."
  12687. (interactive "p")
  12688. (let ((col (current-column))
  12689. beg end pos)
  12690. (beginning-of-line 1) (setq beg (point))
  12691. (beginning-of-line 2) (setq end (point))
  12692. (beginning-of-line (- arg))
  12693. (setq pos (move-marker (make-marker) (point)))
  12694. (insert (delete-and-extract-region beg end))
  12695. (goto-char pos)
  12696. (org-move-to-column col)))
  12697. (defun org-replace-escapes (string table)
  12698. "Replace %-escapes in STRING with values in TABLE.
  12699. TABLE is an association list with keys like \"%a\" and string values.
  12700. The sequences in STRING may contain normal field width and padding information,
  12701. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12702. so values can contain further %-escapes if they are define later in TABLE."
  12703. (let ((case-fold-search nil)
  12704. e re rpl)
  12705. (while (setq e (pop table))
  12706. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12707. (while (string-match re string)
  12708. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12709. (cdr e)))
  12710. (setq string (replace-match rpl t t string))))
  12711. string))
  12712. (defun org-sublist (list start end)
  12713. "Return a section of LIST, from START to END.
  12714. Counting starts at 1."
  12715. (let (rtn (c start))
  12716. (setq list (nthcdr (1- start) list))
  12717. (while (and list (<= c end))
  12718. (push (pop list) rtn)
  12719. (setq c (1+ c)))
  12720. (nreverse rtn)))
  12721. (defun org-find-base-buffer-visiting (file)
  12722. "Like `find-buffer-visiting' but alway return the base buffer and
  12723. not an indirect buffer."
  12724. (let ((buf (find-buffer-visiting file)))
  12725. (if buf
  12726. (or (buffer-base-buffer buf) buf)
  12727. nil)))
  12728. (defun org-image-file-name-regexp ()
  12729. "Return regexp matching the file names of images."
  12730. (if (fboundp 'image-file-name-regexp)
  12731. (image-file-name-regexp)
  12732. (let ((image-file-name-extensions
  12733. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12734. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12735. (concat "\\."
  12736. (regexp-opt (nconc (mapcar 'upcase
  12737. image-file-name-extensions)
  12738. image-file-name-extensions)
  12739. t)
  12740. "\\'"))))
  12741. (defun org-file-image-p (file)
  12742. "Return non-nil if FILE is an image."
  12743. (save-match-data
  12744. (string-match (org-image-file-name-regexp) file)))
  12745. (defun org-get-cursor-date ()
  12746. "Return the date at cursor in as a time.
  12747. This works in the calendar and in the agenda, anywhere else it just
  12748. returns the current time."
  12749. (let (date day defd)
  12750. (cond
  12751. ((eq major-mode 'calendar-mode)
  12752. (setq date (calendar-cursor-to-date)
  12753. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12754. ((eq major-mode 'org-agenda-mode)
  12755. (setq day (get-text-property (point) 'day))
  12756. (if day
  12757. (setq date (calendar-gregorian-from-absolute day)
  12758. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12759. (nth 2 date))))))
  12760. (or defd (current-time))))
  12761. (defvar org-agenda-action-marker (make-marker)
  12762. "Marker pointing to the entry for the next agenda action.")
  12763. (defun org-mark-entry-for-agenda-action ()
  12764. "Mark the current entry as target of an agenda action.
  12765. Agenda actions are actions executed from the agenda with the key `k',
  12766. which make use of the date at the cursor."
  12767. (interactive)
  12768. (move-marker org-agenda-action-marker
  12769. (save-excursion (org-back-to-heading t) (point))
  12770. (current-buffer))
  12771. (message
  12772. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12773. ;;; Paragraph filling stuff.
  12774. ;; We want this to be just right, so use the full arsenal.
  12775. (defun org-indent-line-function ()
  12776. "Indent line like previous, but further if previous was headline or item."
  12777. (interactive)
  12778. (let* ((pos (point))
  12779. (itemp (org-at-item-p))
  12780. column bpos bcol tpos tcol bullet btype bullet-type)
  12781. ;; Find the previous relevant line
  12782. (beginning-of-line 1)
  12783. (cond
  12784. ((looking-at "#") (setq column 0))
  12785. ((looking-at "\\*+ ") (setq column 0))
  12786. (t
  12787. (beginning-of-line 0)
  12788. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12789. (beginning-of-line 0))
  12790. (cond
  12791. ((looking-at "\\*+[ \t]+")
  12792. (if (not org-adapt-indentation)
  12793. (setq column 0)
  12794. (goto-char (match-end 0))
  12795. (setq column (current-column))))
  12796. ((org-in-item-p)
  12797. (org-beginning-of-item)
  12798. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12799. (setq bpos (match-beginning 1) tpos (match-end 0)
  12800. bcol (progn (goto-char bpos) (current-column))
  12801. tcol (progn (goto-char tpos) (current-column))
  12802. bullet (match-string 1)
  12803. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12804. (if (> tcol (+ bcol org-description-max-indent))
  12805. (setq tcol (+ bcol 5)))
  12806. (if (not itemp)
  12807. (setq column tcol)
  12808. (goto-char pos)
  12809. (beginning-of-line 1)
  12810. (if (looking-at "\\S-")
  12811. (progn
  12812. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12813. (setq bullet (match-string 1)
  12814. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12815. (setq column (if (equal btype bullet-type) bcol tcol)))
  12816. (setq column (org-get-indentation)))))
  12817. (t (setq column (org-get-indentation))))))
  12818. (goto-char pos)
  12819. (if (<= (current-column) (current-indentation))
  12820. (org-indent-line-to column)
  12821. (save-excursion (org-indent-line-to column)))
  12822. (setq column (current-column))
  12823. (beginning-of-line 1)
  12824. (if (looking-at
  12825. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12826. (replace-match (concat "\\1" (format org-property-format
  12827. (match-string 2) (match-string 3)))
  12828. t nil))
  12829. (org-move-to-column column)))
  12830. (defun org-set-autofill-regexps ()
  12831. (interactive)
  12832. ;; In the paragraph separator we include headlines, because filling
  12833. ;; text in a line directly attached to a headline would otherwise
  12834. ;; fill the headline as well.
  12835. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12836. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12837. ;; The paragraph starter includes hand-formatted lists.
  12838. (org-set-local 'paragraph-start
  12839. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12840. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12841. ;; But only if the user has not turned off tables or fixed-width regions
  12842. (org-set-local
  12843. 'auto-fill-inhibit-regexp
  12844. (concat "\\*+ \\|#\\+"
  12845. "\\|[ \t]*" org-keyword-time-regexp
  12846. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12847. (concat
  12848. "\\|[ \t]*["
  12849. (if org-enable-table-editor "|" "")
  12850. (if org-enable-fixed-width-editor ":" "")
  12851. "]"))))
  12852. ;; We use our own fill-paragraph function, to make sure that tables
  12853. ;; and fixed-width regions are not wrapped. That function will pass
  12854. ;; through to `fill-paragraph' when appropriate.
  12855. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12856. ; Adaptive filling: To get full control, first make sure that
  12857. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12858. (org-set-local 'adaptive-fill-regexp "\000")
  12859. (org-set-local 'adaptive-fill-function
  12860. 'org-adaptive-fill-function)
  12861. (org-set-local
  12862. 'align-mode-rules-list
  12863. '((org-in-buffer-settings
  12864. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12865. (modes . '(org-mode))))))
  12866. (defun org-fill-paragraph (&optional justify)
  12867. "Re-align a table, pass through to fill-paragraph if no table."
  12868. (let ((table-p (org-at-table-p))
  12869. (table.el-p (org-at-table.el-p)))
  12870. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12871. (save-excursion (goto-char (point-at-bol))
  12872. (looking-at outline-regexp)))
  12873. t) ; skip headlines
  12874. (table.el-p t) ; skip table.el tables
  12875. (table-p (org-table-align) t) ; align org-mode tables
  12876. (t nil)))) ; call paragraph-fill
  12877. ;; For reference, this is the default value of adaptive-fill-regexp
  12878. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12879. (defun org-adaptive-fill-function ()
  12880. "Return a fill prefix for org-mode files.
  12881. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12882. work correctly."
  12883. (cond ((looking-at "#[ \t]+")
  12884. (match-string 0))
  12885. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12886. (save-excursion
  12887. (if (> (match-end 1) (+ (match-beginning 1)
  12888. org-description-max-indent))
  12889. (goto-char (+ (match-beginning 1) 5))
  12890. (goto-char (match-end 0)))
  12891. (make-string (current-column) ?\ )))
  12892. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  12893. (save-excursion
  12894. (goto-char (match-end 0))
  12895. (make-string (current-column) ?\ )))
  12896. (t nil)))
  12897. ;;; Other stuff.
  12898. (defun org-toggle-fixed-width-section (arg)
  12899. "Toggle the fixed-width export.
  12900. If there is no active region, the QUOTE keyword at the current headline is
  12901. inserted or removed. When present, it causes the text between this headline
  12902. and the next to be exported as fixed-width text, and unmodified.
  12903. If there is an active region, this command adds or removes a colon as the
  12904. first character of this line. If the first character of a line is a colon,
  12905. this line is also exported in fixed-width font."
  12906. (interactive "P")
  12907. (let* ((cc 0)
  12908. (regionp (org-region-active-p))
  12909. (beg (if regionp (region-beginning) (point)))
  12910. (end (if regionp (region-end)))
  12911. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12912. (case-fold-search nil)
  12913. (re "[ \t]*\\(:\\)")
  12914. off)
  12915. (if regionp
  12916. (save-excursion
  12917. (goto-char beg)
  12918. (setq cc (current-column))
  12919. (beginning-of-line 1)
  12920. (setq off (looking-at re))
  12921. (while (> nlines 0)
  12922. (setq nlines (1- nlines))
  12923. (beginning-of-line 1)
  12924. (cond
  12925. (arg
  12926. (org-move-to-column cc t)
  12927. (insert ":\n")
  12928. (forward-line -1))
  12929. ((and off (looking-at re))
  12930. (replace-match "" t t nil 1))
  12931. ((not off) (org-move-to-column cc t) (insert ":")))
  12932. (forward-line 1)))
  12933. (save-excursion
  12934. (org-back-to-heading)
  12935. (if (looking-at (concat outline-regexp
  12936. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12937. (replace-match "" t t nil 1)
  12938. (if (looking-at outline-regexp)
  12939. (progn
  12940. (goto-char (match-end 0))
  12941. (insert org-quote-string " "))))))))
  12942. ;;;; Functions extending outline functionality
  12943. (defun org-beginning-of-line (&optional arg)
  12944. "Go to the beginning of the current line. If that is invisible, continue
  12945. to a visible line beginning. This makes the function of C-a more intuitive.
  12946. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12947. first attempt, and only move to after the tags when the cursor is already
  12948. beyond the end of the headline."
  12949. (interactive "P")
  12950. (let ((pos (point)) refpos)
  12951. (beginning-of-line 1)
  12952. (if (bobp)
  12953. nil
  12954. (backward-char 1)
  12955. (if (org-invisible-p)
  12956. (while (and (not (bobp)) (org-invisible-p))
  12957. (backward-char 1)
  12958. (beginning-of-line 1))
  12959. (forward-char 1)))
  12960. (when org-special-ctrl-a/e
  12961. (cond
  12962. ((and (looking-at org-complex-heading-regexp)
  12963. (= (char-after (match-end 1)) ?\ ))
  12964. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12965. (point-at-eol)))
  12966. (goto-char
  12967. (if (eq org-special-ctrl-a/e t)
  12968. (cond ((> pos refpos) refpos)
  12969. ((= pos (point)) refpos)
  12970. (t (point)))
  12971. (cond ((> pos (point)) (point))
  12972. ((not (eq last-command this-command)) (point))
  12973. (t refpos)))))
  12974. ((org-at-item-p)
  12975. (goto-char
  12976. (if (eq org-special-ctrl-a/e t)
  12977. (cond ((> pos (match-end 4)) (match-end 4))
  12978. ((= pos (point)) (match-end 4))
  12979. (t (point)))
  12980. (cond ((> pos (point)) (point))
  12981. ((not (eq last-command this-command)) (point))
  12982. (t (match-end 4))))))))
  12983. (org-no-warnings
  12984. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12985. (defun org-end-of-line (&optional arg)
  12986. "Go to the end of the line.
  12987. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12988. first attempt, and only move to after the tags when the cursor is already
  12989. beyond the end of the headline."
  12990. (interactive "P")
  12991. (if (or (not org-special-ctrl-a/e)
  12992. (not (org-on-heading-p)))
  12993. (end-of-line arg)
  12994. (let ((pos (point)))
  12995. (beginning-of-line 1)
  12996. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12997. (if (eq org-special-ctrl-a/e t)
  12998. (if (or (< pos (match-beginning 1))
  12999. (= pos (match-end 0)))
  13000. (goto-char (match-beginning 1))
  13001. (goto-char (match-end 0)))
  13002. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13003. (goto-char (match-end 0))
  13004. (goto-char (match-beginning 1))))
  13005. (end-of-line arg))))
  13006. (org-no-warnings
  13007. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13008. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13009. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13010. (defun org-kill-line (&optional arg)
  13011. "Kill line, to tags or end of line."
  13012. (interactive "P")
  13013. (cond
  13014. ((or (not org-special-ctrl-k)
  13015. (bolp)
  13016. (not (org-on-heading-p)))
  13017. (call-interactively 'kill-line))
  13018. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13019. (kill-region (point) (match-beginning 1))
  13020. (org-set-tags nil t))
  13021. (t (kill-region (point) (point-at-eol)))))
  13022. (define-key org-mode-map "\C-k" 'org-kill-line)
  13023. (defun org-yank (&optional arg)
  13024. "Yank. If the kill is a subtree, treat it specially.
  13025. This command will look at the current kill and check if is a single
  13026. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13027. cursor is at the beginning of a line or after the stars of a currently
  13028. empty headline, then the yank is handeled specially. How exactly depends
  13029. on the value of the following variables, both set by default.
  13030. org-yank-folded-subtrees
  13031. When set, the subree(s) will be folded after insertion, but only
  13032. if doing so would now swallow text after the yanked text.
  13033. org-yank-adjusted-subtrees
  13034. When set, the subtree will be promoted or demoted in order to
  13035. fit into the local outline tree structure, which means that the level
  13036. will be adjusted so that it becomes the smaller one of the two
  13037. *visible* surrounding headings.
  13038. Any prefix to this command will cause `yank' to be called directly with
  13039. no special treatment. In particular, a simple `C-u' prefix will just
  13040. plainly yank the text as it is.
  13041. \[1] Basically, the test checks if the first non-white line is a heading
  13042. and if there are no other headings with fewer stars."
  13043. (interactive "P")
  13044. (setq this-command 'yank)
  13045. (if arg
  13046. (call-interactively 'yank)
  13047. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13048. (and (org-kill-is-subtree-p)
  13049. (or (bolp)
  13050. (and (looking-at "[ \t]*$")
  13051. (string-match
  13052. "\\`\\*+\\'"
  13053. (buffer-substring (point-at-bol) (point)))))))
  13054. swallowp)
  13055. (cond
  13056. ((and subtreep org-yank-folded-subtrees)
  13057. (let ((beg (point))
  13058. end)
  13059. (if (and subtreep org-yank-adjusted-subtrees)
  13060. (org-paste-subtree nil nil 'for-yank)
  13061. (call-interactively 'yank))
  13062. (setq end (point))
  13063. (goto-char beg)
  13064. (when (and (bolp) subtreep
  13065. (not (setq swallowp
  13066. (org-yank-folding-would-swallow-text beg end))))
  13067. (or (looking-at outline-regexp)
  13068. (re-search-forward (concat "^" outline-regexp) end t))
  13069. (while (and (< (point) end) (looking-at outline-regexp))
  13070. (hide-subtree)
  13071. (org-cycle-show-empty-lines 'folded)
  13072. (condition-case nil
  13073. (outline-forward-same-level 1)
  13074. (error (goto-char end)))))
  13075. (when swallowp
  13076. (message
  13077. "Yanked text not folded because that would swallow text"))
  13078. (goto-char end)
  13079. (skip-chars-forward " \t\n\r")
  13080. (beginning-of-line 1)
  13081. (push-mark beg 'nomsg)))
  13082. ((and subtreep org-yank-adjusted-subtrees)
  13083. (let ((beg (point-at-bol)))
  13084. (org-paste-subtree nil nil 'for-yank)
  13085. (push-mark beg 'nomsg)))
  13086. (t
  13087. (call-interactively 'yank))))))
  13088. (defun org-yank-folding-would-swallow-text (beg end)
  13089. "Would hide-subtree at BEG swallow any text after END?"
  13090. (let (level)
  13091. (save-excursion
  13092. (goto-char beg)
  13093. (when (or (looking-at outline-regexp)
  13094. (re-search-forward (concat "^" outline-regexp) end t))
  13095. (setq level (org-outline-level)))
  13096. (goto-char end)
  13097. (skip-chars-forward " \t\r\n\v\f")
  13098. (if (or (eobp)
  13099. (and (bolp) (looking-at org-outline-regexp)
  13100. (<= (org-outline-level) level)))
  13101. nil ; Nothing would be swallowed
  13102. t)))) ; something would swallow
  13103. (define-key org-mode-map "\C-y" 'org-yank)
  13104. (defun org-invisible-p ()
  13105. "Check if point is at a character currently not visible."
  13106. ;; Early versions of noutline don't have `outline-invisible-p'.
  13107. (if (fboundp 'outline-invisible-p)
  13108. (outline-invisible-p)
  13109. (get-char-property (point) 'invisible)))
  13110. (defun org-invisible-p2 ()
  13111. "Check if point is at a character currently not visible."
  13112. (save-excursion
  13113. (if (and (eolp) (not (bobp))) (backward-char 1))
  13114. ;; Early versions of noutline don't have `outline-invisible-p'.
  13115. (if (fboundp 'outline-invisible-p)
  13116. (outline-invisible-p)
  13117. (get-char-property (point) 'invisible))))
  13118. (defun org-back-to-heading (&optional invisible-ok)
  13119. "Call `outline-back-to-heading', but provide a better error message."
  13120. (condition-case nil
  13121. (outline-back-to-heading invisible-ok)
  13122. (error (error "Before first headline at position %d in buffer %s"
  13123. (point) (current-buffer)))))
  13124. (defun org-before-first-heading-p ()
  13125. "Before first heading?"
  13126. (save-excursion
  13127. (null (re-search-backward "^\\*+ " nil t))))
  13128. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13129. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13130. (defun org-at-heading-or-item-p ()
  13131. (or (org-on-heading-p) (org-at-item-p)))
  13132. (defun org-on-target-p ()
  13133. (or (org-in-regexp org-radio-target-regexp)
  13134. (org-in-regexp org-target-regexp)))
  13135. (defun org-up-heading-all (arg)
  13136. "Move to the heading line of which the present line is a subheading.
  13137. This function considers both visible and invisible heading lines.
  13138. With argument, move up ARG levels."
  13139. (if (fboundp 'outline-up-heading-all)
  13140. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13141. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13142. (defun org-up-heading-safe ()
  13143. "Move to the heading line of which the present line is a subheading.
  13144. This version will not throw an error. It will return the level of the
  13145. headline found, or nil if no higher level is found."
  13146. (let ((pos (point)) start-level level
  13147. (re (concat "^" outline-regexp)))
  13148. (catch 'exit
  13149. (org-back-to-heading t)
  13150. (setq start-level (funcall outline-level))
  13151. (if (equal start-level 1) (throw 'exit nil))
  13152. (while (re-search-backward re nil t)
  13153. (setq level (funcall outline-level))
  13154. (if (< level start-level) (throw 'exit level)))
  13155. nil)))
  13156. (defun org-first-sibling-p ()
  13157. "Is this heading the first child of its parents?"
  13158. (interactive)
  13159. (let ((re (concat "^" outline-regexp))
  13160. level l)
  13161. (unless (org-at-heading-p t)
  13162. (error "Not at a heading"))
  13163. (setq level (funcall outline-level))
  13164. (save-excursion
  13165. (if (not (re-search-backward re nil t))
  13166. t
  13167. (setq l (funcall outline-level))
  13168. (< l level)))))
  13169. (defun org-goto-sibling (&optional previous)
  13170. "Goto the next sibling, even if it is invisible.
  13171. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13172. when a sibling was found. When none is found, return nil and don't
  13173. move point."
  13174. (let ((fun (if previous 're-search-backward 're-search-forward))
  13175. (pos (point))
  13176. (re (concat "^" outline-regexp))
  13177. level l)
  13178. (when (condition-case nil (org-back-to-heading t) (error nil))
  13179. (setq level (funcall outline-level))
  13180. (catch 'exit
  13181. (or previous (forward-char 1))
  13182. (while (funcall fun re nil t)
  13183. (setq l (funcall outline-level))
  13184. (when (< l level) (goto-char pos) (throw 'exit nil))
  13185. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13186. (goto-char pos)
  13187. nil))))
  13188. (defun org-show-siblings ()
  13189. "Show all siblings of the current headline."
  13190. (save-excursion
  13191. (while (org-goto-sibling) (org-flag-heading nil)))
  13192. (save-excursion
  13193. (while (org-goto-sibling 'previous)
  13194. (org-flag-heading nil))))
  13195. (defun org-show-hidden-entry ()
  13196. "Show an entry where even the heading is hidden."
  13197. (save-excursion
  13198. (org-show-entry)))
  13199. (defun org-flag-heading (flag &optional entry)
  13200. "Flag the current heading. FLAG non-nil means make invisible.
  13201. When ENTRY is non-nil, show the entire entry."
  13202. (save-excursion
  13203. (org-back-to-heading t)
  13204. ;; Check if we should show the entire entry
  13205. (if entry
  13206. (progn
  13207. (org-show-entry)
  13208. (save-excursion
  13209. (and (outline-next-heading)
  13210. (org-flag-heading nil))))
  13211. (outline-flag-region (max (point-min) (1- (point)))
  13212. (save-excursion (outline-end-of-heading) (point))
  13213. flag))))
  13214. (defun org-forward-same-level (arg)
  13215. "Move forward to the ARG'th subheading at same level as this one.
  13216. Stop at the first and last subheadings of a superior heading.
  13217. This is like outline-forward-same-level, but invisible headings are ok."
  13218. (interactive "p")
  13219. (org-back-to-heading t)
  13220. (while (> arg 0)
  13221. (let ((point-to-move-to (save-excursion
  13222. (org-get-next-sibling))))
  13223. (if point-to-move-to
  13224. (progn
  13225. (goto-char point-to-move-to)
  13226. (setq arg (1- arg)))
  13227. (progn
  13228. (setq arg 0)
  13229. (error "No following same-level heading"))))))
  13230. (defun org-get-next-sibling ()
  13231. "Move to next heading of the same level, and return point.
  13232. If there is no such heading, return nil.
  13233. This is like outline-next-sibling, but invisible headings are ok."
  13234. (let ((level (funcall outline-level)))
  13235. (outline-next-heading)
  13236. (while (and (not (eobp)) (> (funcall outline-level) level))
  13237. (outline-next-heading))
  13238. (if (or (eobp) (< (funcall outline-level) level))
  13239. nil
  13240. (point))))
  13241. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13242. ;; This is an exact copy of the original function, but it uses
  13243. ;; `org-back-to-heading', to make it work also in invisible
  13244. ;; trees. And is uses an invisible-OK argument.
  13245. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13246. (org-back-to-heading invisible-OK)
  13247. (let ((first t)
  13248. (level (funcall outline-level)))
  13249. (while (and (not (eobp))
  13250. (or first (> (funcall outline-level) level)))
  13251. (setq first nil)
  13252. (outline-next-heading))
  13253. (unless to-heading
  13254. (if (memq (preceding-char) '(?\n ?\^M))
  13255. (progn
  13256. ;; Go to end of line before heading
  13257. (forward-char -1)
  13258. (if (memq (preceding-char) '(?\n ?\^M))
  13259. ;; leave blank line before heading
  13260. (forward-char -1))))))
  13261. (point))
  13262. (defun org-show-subtree ()
  13263. "Show everything after this heading at deeper levels."
  13264. (outline-flag-region
  13265. (point)
  13266. (save-excursion
  13267. (outline-end-of-subtree) (outline-next-heading) (point))
  13268. nil))
  13269. (defun org-show-entry ()
  13270. "Show the body directly following this heading.
  13271. Show the heading too, if it is currently invisible."
  13272. (interactive)
  13273. (save-excursion
  13274. (condition-case nil
  13275. (progn
  13276. (org-back-to-heading t)
  13277. (outline-flag-region
  13278. (max (point-min) (1- (point)))
  13279. (save-excursion
  13280. (re-search-forward
  13281. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13282. (or (match-beginning 1) (point-max)))
  13283. nil))
  13284. (error nil))))
  13285. (defun org-make-options-regexp (kwds)
  13286. "Make a regular expression for keyword lines."
  13287. (concat
  13288. "^"
  13289. "#?[ \t]*\\+\\("
  13290. (mapconcat 'regexp-quote kwds "\\|")
  13291. "\\):[ \t]*"
  13292. "\\(.+\\)"))
  13293. ;; Make isearch reveal the necessary context
  13294. (defun org-isearch-end ()
  13295. "Reveal context after isearch exits."
  13296. (when isearch-success ; only if search was successful
  13297. (if (featurep 'xemacs)
  13298. ;; Under XEmacs, the hook is run in the correct place,
  13299. ;; we directly show the context.
  13300. (org-show-context 'isearch)
  13301. ;; In Emacs the hook runs *before* restoring the overlays.
  13302. ;; So we have to use a one-time post-command-hook to do this.
  13303. ;; (Emacs 22 has a special variable, see function `org-mode')
  13304. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13305. isearch-mode-end-hook-quit)
  13306. ;; Only when the isearch was not quitted.
  13307. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13308. 'append 'local)))))
  13309. (defun org-isearch-post-command ()
  13310. "Remove self from hook, and show context."
  13311. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13312. (org-show-context 'isearch))
  13313. ;;;; Integration with and fixes for other packages
  13314. ;;; Imenu support
  13315. (defvar org-imenu-markers nil
  13316. "All markers currently used by Imenu.")
  13317. (make-variable-buffer-local 'org-imenu-markers)
  13318. (defun org-imenu-new-marker (&optional pos)
  13319. "Return a new marker for use by Imenu, and remember the marker."
  13320. (let ((m (make-marker)))
  13321. (move-marker m (or pos (point)))
  13322. (push m org-imenu-markers)
  13323. m))
  13324. (defun org-imenu-get-tree ()
  13325. "Produce the index for Imenu."
  13326. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13327. (setq org-imenu-markers nil)
  13328. (let* ((n org-imenu-depth)
  13329. (re (concat "^" outline-regexp))
  13330. (subs (make-vector (1+ n) nil))
  13331. (last-level 0)
  13332. m tree level head)
  13333. (save-excursion
  13334. (save-restriction
  13335. (widen)
  13336. (goto-char (point-max))
  13337. (while (re-search-backward re nil t)
  13338. (setq level (org-reduced-level (funcall outline-level)))
  13339. (when (<= level n)
  13340. (looking-at org-complex-heading-regexp)
  13341. (setq head (org-link-display-format
  13342. (org-match-string-no-properties 4))
  13343. m (org-imenu-new-marker))
  13344. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13345. (if (>= level last-level)
  13346. (push (cons head m) (aref subs level))
  13347. (push (cons head (aref subs (1+ level))) (aref subs level))
  13348. (loop for i from (1+ level) to n do (aset subs i nil)))
  13349. (setq last-level level)))))
  13350. (aref subs 1)))
  13351. (eval-after-load "imenu"
  13352. '(progn
  13353. (add-hook 'imenu-after-jump-hook
  13354. (lambda ()
  13355. (if (eq major-mode 'org-mode)
  13356. (org-show-context 'org-goto))))))
  13357. (defun org-link-display-format (link)
  13358. "Replace a link with either the description, or the link target
  13359. if no description is present"
  13360. (save-match-data
  13361. (if (string-match org-bracket-link-analytic-regexp link)
  13362. (replace-match (or (match-string 5 link)
  13363. (concat (match-string 1 link)
  13364. (match-string 3 link)))
  13365. nil nil link)
  13366. link)))
  13367. ;; Speedbar support
  13368. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13369. "Overlay marking the agenda restriction line in speedbar.")
  13370. (org-overlay-put org-speedbar-restriction-lock-overlay
  13371. 'face 'org-agenda-restriction-lock)
  13372. (org-overlay-put org-speedbar-restriction-lock-overlay
  13373. 'help-echo "Agendas are currently limited to this item.")
  13374. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13375. (defun org-speedbar-set-agenda-restriction ()
  13376. "Restrict future agenda commands to the location at point in speedbar.
  13377. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13378. (interactive)
  13379. (require 'org-agenda)
  13380. (let (p m tp np dir txt w)
  13381. (cond
  13382. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13383. 'org-imenu t))
  13384. (setq m (get-text-property p 'org-imenu-marker))
  13385. (save-excursion
  13386. (save-restriction
  13387. (set-buffer (marker-buffer m))
  13388. (goto-char m)
  13389. (org-agenda-set-restriction-lock 'subtree))))
  13390. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13391. 'speedbar-function 'speedbar-find-file))
  13392. (setq tp (previous-single-property-change
  13393. (1+ p) 'speedbar-function)
  13394. np (next-single-property-change
  13395. tp 'speedbar-function)
  13396. dir (speedbar-line-directory)
  13397. txt (buffer-substring-no-properties (or tp (point-min))
  13398. (or np (point-max))))
  13399. (save-excursion
  13400. (save-restriction
  13401. (set-buffer (find-file-noselect
  13402. (let ((default-directory dir))
  13403. (expand-file-name txt))))
  13404. (unless (org-mode-p)
  13405. (error "Cannot restrict to non-Org-mode file"))
  13406. (org-agenda-set-restriction-lock 'file))))
  13407. (t (error "Don't know how to restrict Org-mode's agenda")))
  13408. (org-move-overlay org-speedbar-restriction-lock-overlay
  13409. (point-at-bol) (point-at-eol))
  13410. (setq current-prefix-arg nil)
  13411. (org-agenda-maybe-redo)))
  13412. (eval-after-load "speedbar"
  13413. '(progn
  13414. (speedbar-add-supported-extension ".org")
  13415. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13416. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13417. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13418. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13419. (add-hook 'speedbar-visiting-tag-hook
  13420. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13421. ;;; Fixes and Hacks for problems with other packages
  13422. ;; Make flyspell not check words in links, to not mess up our keymap
  13423. (defun org-mode-flyspell-verify ()
  13424. "Don't let flyspell put overlays at active buttons."
  13425. (not (get-text-property (point) 'keymap)))
  13426. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13427. (eval-after-load "bookmark"
  13428. '(if (boundp 'bookmark-after-jump-hook)
  13429. ;; We can use the hook
  13430. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13431. ;; Hook not available, use advice
  13432. (defadvice bookmark-jump (after org-make-visible activate)
  13433. "Make the position visible."
  13434. (org-bookmark-jump-unhide))))
  13435. ;; Make sure saveplace show the location if it was hidden
  13436. (eval-after-load "saveplace"
  13437. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13438. "Make the position visible."
  13439. (org-bookmark-jump-unhide)))
  13440. (defun org-bookmark-jump-unhide ()
  13441. "Unhide the current position, to show the bookmark location."
  13442. (and (org-mode-p)
  13443. (or (org-invisible-p)
  13444. (save-excursion (goto-char (max (point-min) (1- (point))))
  13445. (org-invisible-p)))
  13446. (org-show-context 'bookmark-jump)))
  13447. ;; Make session.el ignore our circular variable
  13448. (eval-after-load "session"
  13449. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13450. ;;;; Experimental code
  13451. (defun org-closed-in-range ()
  13452. "Sparse tree of items closed in a certain time range.
  13453. Still experimental, may disappear in the future."
  13454. (interactive)
  13455. ;; Get the time interval from the user.
  13456. (let* ((time1 (time-to-seconds
  13457. (org-read-date nil 'to-time nil "Starting date: ")))
  13458. (time2 (time-to-seconds
  13459. (org-read-date nil 'to-time nil "End date:")))
  13460. ;; callback function
  13461. (callback (lambda ()
  13462. (let ((time
  13463. (time-to-seconds
  13464. (apply 'encode-time
  13465. (org-parse-time-string
  13466. (match-string 1))))))
  13467. ;; check if time in interval
  13468. (and (>= time time1) (<= time time2))))))
  13469. ;; make tree, check each match with the callback
  13470. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13471. ;;;; Finish up
  13472. (provide 'org)
  13473. (run-hooks 'org-load-hook)
  13474. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13475. ;;; org.el ends here