org.el 550 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841
  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.16trans
  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.16trans"
  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 IDs 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 Remember 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 standard 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][description]]. 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 brackets 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 %-escapes 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. Usually \"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 matched 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-link-to-org-use-id 'create-if-interactive
  837. "Non-nil means, storing a link to an Org file will use entry IDs.
  838. Note that before this variable is even considered, org-id must be loaded,
  839. to please customize `org-modules' and turn it on.
  840. The variable can have the following values:
  841. t Create an ID if needed to make a link to the current entry.
  842. create-if-interactive
  843. If `org-store-link' is called directly (interactively, as a user
  844. command), do create an ID to support the link. But when doing the
  845. job for remember, only use the ID if it already exists. The
  846. purpose of this setting is to avoid proliferation of unwanted
  847. IDs, just because you happen to be in an Org file when you
  848. call `org-remember' that automatically and preemptively
  849. creates a link. If you do want to get an ID link in a remember
  850. template to an entry not having an ID, create it first by
  851. explicitly creating a link to it, using `C-c C-l' first.
  852. use-existing
  853. Use existing ID, do not create one.
  854. nil Never use an ID to make a link, instead link using a text search for
  855. the headline text."
  856. :group 'org-link-store
  857. :type '(choice
  858. (const :tag "Create ID to make link" t)
  859. (const :tag "Create if string link interactively"
  860. 'create-if-interactive)
  861. (const :tag "Only use existing" 'use-existing)
  862. (const :tag "Do not use ID to create link" nil)))
  863. (defcustom org-context-in-file-links t
  864. "Non-nil means, file links from `org-store-link' contain context.
  865. A search string will be added to the file name with :: as separator and
  866. used to find the context when the link is activated by the command
  867. `org-open-at-point'.
  868. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  869. negates this setting for the duration of the command."
  870. :group 'org-link-store
  871. :type 'boolean)
  872. (defcustom org-keep-stored-link-after-insertion nil
  873. "Non-nil means, keep link in list for entire session.
  874. The command `org-store-link' adds a link pointing to the current
  875. location to an internal list. These links accumulate during a session.
  876. The command `org-insert-link' can be used to insert links into any
  877. Org-mode file (offering completion for all stored links). When this
  878. option is nil, every link which has been inserted once using \\[org-insert-link]
  879. will be removed from the list, to make completing the unused links
  880. more efficient."
  881. :group 'org-link-store
  882. :type 'boolean)
  883. (defgroup org-link-follow nil
  884. "Options concerning following links in Org-mode."
  885. :tag "Org Follow Link"
  886. :group 'org-link)
  887. (defcustom org-link-translation-function nil
  888. "Function to translate links with different syntax to Org syntax.
  889. This can be used to translate links created for example by the Planner
  890. or emacs-wiki packages to Org syntax.
  891. The function must accept two parameters, a TYPE containing the link
  892. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  893. which is everything after the link protocol. It should return a cons
  894. with possibly modified values of type and path.
  895. Org contains a function for this, so if you set this variable to
  896. `org-translate-link-from-planner', you should be able follow many
  897. links created by planner."
  898. :group 'org-link-follow
  899. :type 'function)
  900. (defcustom org-follow-link-hook nil
  901. "Hook that is run after a link has been followed."
  902. :group 'org-link-follow
  903. :type 'hook)
  904. (defcustom org-tab-follows-link nil
  905. "Non-nil means, on links TAB will follow the link.
  906. Needs to be set before org.el is loaded."
  907. :group 'org-link-follow
  908. :type 'boolean)
  909. (defcustom org-return-follows-link nil
  910. "Non-nil means, on links RET will follow the link.
  911. Needs to be set before org.el is loaded."
  912. :group 'org-link-follow
  913. :type 'boolean)
  914. (defcustom org-mouse-1-follows-link
  915. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  916. "Non-nil means, mouse-1 on a link will follow the link.
  917. A longer mouse click will still set point. Does not work on XEmacs.
  918. Needs to be set before org.el is loaded."
  919. :group 'org-link-follow
  920. :type 'boolean)
  921. (defcustom org-mark-ring-length 4
  922. "Number of different positions to be recorded in the ring
  923. Changing this requires a restart of Emacs to work correctly."
  924. :group 'org-link-follow
  925. :type 'integer)
  926. (defcustom org-link-frame-setup
  927. '((vm . vm-visit-folder-other-frame)
  928. (gnus . gnus-other-frame)
  929. (file . find-file-other-window))
  930. "Setup the frame configuration for following links.
  931. When following a link with Emacs, it may often be useful to display
  932. this link in another window or frame. This variable can be used to
  933. set this up for the different types of links.
  934. For VM, use any of
  935. `vm-visit-folder'
  936. `vm-visit-folder-other-frame'
  937. For Gnus, use any of
  938. `gnus'
  939. `gnus-other-frame'
  940. `org-gnus-no-new-news'
  941. For FILE, use any of
  942. `find-file'
  943. `find-file-other-window'
  944. `find-file-other-frame'
  945. For the calendar, use the variable `calendar-setup'.
  946. For BBDB, it is currently only possible to display the matches in
  947. another window."
  948. :group 'org-link-follow
  949. :type '(list
  950. (cons (const vm)
  951. (choice
  952. (const vm-visit-folder)
  953. (const vm-visit-folder-other-window)
  954. (const vm-visit-folder-other-frame)))
  955. (cons (const gnus)
  956. (choice
  957. (const gnus)
  958. (const gnus-other-frame)
  959. (const org-gnus-no-new-news)))
  960. (cons (const file)
  961. (choice
  962. (const find-file)
  963. (const find-file-other-window)
  964. (const find-file-other-frame)))))
  965. (defcustom org-display-internal-link-with-indirect-buffer nil
  966. "Non-nil means, use indirect buffer to display infile links.
  967. Activating internal links (from one location in a file to another location
  968. in the same file) normally just jumps to the location. When the link is
  969. activated with a C-u prefix (or with mouse-3), the link is displayed in
  970. another window. When this option is set, the other window actually displays
  971. an indirect buffer clone of the current buffer, to avoid any visibility
  972. changes to the current buffer."
  973. :group 'org-link-follow
  974. :type 'boolean)
  975. (defcustom org-open-non-existing-files nil
  976. "Non-nil means, `org-open-file' will open non-existing files.
  977. When nil, an error will be generated."
  978. :group 'org-link-follow
  979. :type 'boolean)
  980. (defcustom org-open-directory-means-index-dot-org nil
  981. "Non-nil means, a link to a directory really means to index.org.
  982. When nil, following a directory link will run dired or open a finder/explorer
  983. window on that directory."
  984. :group 'org-link-follow
  985. :type 'boolean)
  986. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  987. "Function and arguments to call for following mailto links.
  988. This is a list with the first element being a lisp function, and the
  989. remaining elements being arguments to the function. In string arguments,
  990. %a will be replaced by the address, and %s will be replaced by the subject
  991. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  992. :group 'org-link-follow
  993. :type '(choice
  994. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  995. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  996. (const :tag "message-mail" (message-mail "%a" "%s"))
  997. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  998. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  999. "Non-nil means, ask for confirmation before executing shell links.
  1000. Shell links can be dangerous: just think about a link
  1001. [[shell:rm -rf ~/*][Google Search]]
  1002. This link would show up in your Org-mode document as \"Google Search\",
  1003. but really it would remove your entire home directory.
  1004. Therefore we advise against setting this variable to nil.
  1005. Just change it to `y-or-n-p' of you want to confirm with a
  1006. single keystroke rather than having to type \"yes\"."
  1007. :group 'org-link-follow
  1008. :type '(choice
  1009. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1010. (const :tag "with y-or-n (faster)" y-or-n-p)
  1011. (const :tag "no confirmation (dangerous)" nil)))
  1012. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1013. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1014. Elisp links can be dangerous: just think about a link
  1015. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1016. This link would show up in your Org-mode document as \"Google Search\",
  1017. but really it would remove your entire home directory.
  1018. Therefore we advise against setting this variable to nil.
  1019. Just change it to `y-or-n-p' of you want to confirm with a
  1020. single keystroke rather than having to type \"yes\"."
  1021. :group 'org-link-follow
  1022. :type '(choice
  1023. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1024. (const :tag "with y-or-n (faster)" y-or-n-p)
  1025. (const :tag "no confirmation (dangerous)" nil)))
  1026. (defconst org-file-apps-defaults-gnu
  1027. '((remote . emacs)
  1028. (system . mailcap)
  1029. (t . mailcap))
  1030. "Default file applications on a UNIX or GNU/Linux system.
  1031. See `org-file-apps'.")
  1032. (defconst org-file-apps-defaults-macosx
  1033. '((remote . emacs)
  1034. (t . "open %s")
  1035. (system . "open %s")
  1036. ("ps.gz" . "gv %s")
  1037. ("eps.gz" . "gv %s")
  1038. ("dvi" . "xdvi %s")
  1039. ("fig" . "xfig %s"))
  1040. "Default file applications on a MacOS X system.
  1041. The system \"open\" is known as a default, but we use X11 applications
  1042. for some files for which the OS does not have a good default.
  1043. See `org-file-apps'.")
  1044. (defconst org-file-apps-defaults-windowsnt
  1045. (list
  1046. '(remote . emacs)
  1047. (cons t
  1048. (list (if (featurep 'xemacs)
  1049. 'mswindows-shell-execute
  1050. 'w32-shell-execute)
  1051. "open" 'file))
  1052. (cons 'system
  1053. (list (if (featurep 'xemacs)
  1054. 'mswindows-shell-execute
  1055. 'w32-shell-execute)
  1056. "open" 'file)))
  1057. "Default file applications on a Windows NT system.
  1058. The system \"open\" is used for most files.
  1059. See `org-file-apps'.")
  1060. (defcustom org-file-apps
  1061. '(
  1062. (auto-mode . emacs)
  1063. ("\\.x?html?\\'" . default)
  1064. ("\\.pdf\\'" . default)
  1065. )
  1066. "External applications for opening `file:path' items in a document.
  1067. Org-mode uses system defaults for different file types, but
  1068. you can use this variable to set the application for a given file
  1069. extension. The entries in this list are cons cells where the car identifies
  1070. files and the cdr the corresponding command. Possible values for the
  1071. file identifier are
  1072. \"regex\" Regular expression matched against the file name. For backward
  1073. compatibility, this can also be a string with only alphanumeric
  1074. characters, which is then interpreted as an extension.
  1075. `directory' Matches a directory
  1076. `remote' Matches a remote file, accessible through tramp or efs.
  1077. Remote files most likely should be visited through Emacs
  1078. because external applications cannot handle such paths.
  1079. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1080. so all files Emacs knows how to handle. Using this with
  1081. command `emacs' will open most files in Emacs. Beware that this
  1082. will also open html files inside Emacs, unless you add
  1083. (\"html\" . default) to the list as well.
  1084. t Default for files not matched by any of the other options.
  1085. `system' The system command to open files, like `open' on Windows
  1086. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1087. that will be selected if you call `C-c C-o' with a double
  1088. `C-u C-u' prefix.
  1089. Possible values for the command are:
  1090. `emacs' The file will be visited by the current Emacs process.
  1091. `default' Use the default application for this file type, which is the
  1092. association for t in the list, most likely in the system-specific
  1093. part.
  1094. This can be used to overrule an unwanted setting in the
  1095. system-specific variable.
  1096. `system' Use the system command for opening files, like \"open\".
  1097. This command is specified by the entry whose car is `system'.
  1098. Most likely, the system-specific version of this variable
  1099. does define this command, but you can overrule/replace it
  1100. here.
  1101. string A command to be executed by a shell; %s will be replaced
  1102. by the path to the file.
  1103. sexp A Lisp form which will be evaluated. The file path will
  1104. be available in the Lisp variable `file'.
  1105. For more examples, see the system specific constants
  1106. `org-file-apps-defaults-macosx'
  1107. `org-file-apps-defaults-windowsnt'
  1108. `org-file-apps-defaults-gnu'."
  1109. :group 'org-link-follow
  1110. :type '(repeat
  1111. (cons (choice :value ""
  1112. (string :tag "Extension")
  1113. (const :tag "System command to open files" system)
  1114. (const :tag "Default for unrecognized files" t)
  1115. (const :tag "Remote file" remote)
  1116. (const :tag "Links to a directory" directory)
  1117. (const :tag "Any files that have Emacs modes"
  1118. auto-mode))
  1119. (choice :value ""
  1120. (const :tag "Visit with Emacs" emacs)
  1121. (const :tag "Use default" default)
  1122. (const :tag "Use the system command" system)
  1123. (string :tag "Command")
  1124. (sexp :tag "Lisp form")))))
  1125. (defgroup org-refile nil
  1126. "Options concerning refiling entries in Org-mode."
  1127. :tag "Org Refile"
  1128. :group 'org)
  1129. (defcustom org-directory "~/org"
  1130. "Directory with org files.
  1131. This directory will be used as default to prompt for org files.
  1132. Used by the hooks for remember.el."
  1133. :group 'org-refile
  1134. :group 'org-remember
  1135. :type 'directory)
  1136. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1137. "Default target for storing notes.
  1138. Used by the hooks for remember.el. This can be a string, or nil to mean
  1139. the value of `remember-data-file'.
  1140. You can set this on a per-template basis with the variable
  1141. `org-remember-templates'."
  1142. :group 'org-refile
  1143. :group 'org-remember
  1144. :type '(choice
  1145. (const :tag "Default from remember-data-file" nil)
  1146. file))
  1147. (defcustom org-goto-interface 'outline
  1148. "The default interface to be used for `org-goto'.
  1149. Allowed values are:
  1150. outline The interface shows an outline of the relevant file
  1151. and the correct heading is found by moving through
  1152. the outline or by searching with incremental search.
  1153. outline-path-completion Headlines in the current buffer are offered via
  1154. completion. This is the interface also used by
  1155. the refile command."
  1156. :group 'org-refile
  1157. :type '(choice
  1158. (const :tag "Outline" outline)
  1159. (const :tag "Outline-path-completion" outline-path-completion)))
  1160. (defcustom org-goto-max-level 5
  1161. "Maximum level to be considered when running org-goto with refile interface."
  1162. :group 'org-refile
  1163. :type 'number)
  1164. (defcustom org-reverse-note-order nil
  1165. "Non-nil means, store new notes at the beginning of a file or entry.
  1166. When nil, new notes will be filed to the end of a file or entry.
  1167. This can also be a list with cons cells of regular expressions that
  1168. are matched against file names, and values."
  1169. :group 'org-remember
  1170. :group 'org-refile
  1171. :type '(choice
  1172. (const :tag "Reverse always" t)
  1173. (const :tag "Reverse never" nil)
  1174. (repeat :tag "By file name regexp"
  1175. (cons regexp boolean))))
  1176. (defcustom org-refile-targets nil
  1177. "Targets for refiling entries with \\[org-refile].
  1178. This is list of cons cells. Each cell contains:
  1179. - a specification of the files to be considered, either a list of files,
  1180. or a symbol whose function or variable value will be used to retrieve
  1181. a file name or a list of file names. If you use `org-agenda-files' for
  1182. that, all agenda files will be scanned for targets. Nil means, consider
  1183. headings in the current buffer.
  1184. - A specification of how to select find candidate refile targets. This
  1185. may be any of
  1186. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1187. This tag has to be present in all target headlines, inheritance will
  1188. not be considered.
  1189. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1190. todo keyword.
  1191. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1192. headlines that are refiling targets.
  1193. - a cons cell (:level . N). Any headline of level N is considered a target.
  1194. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1195. When this variable is nil, all top-level headlines in the current buffer
  1196. are used, equivalent to the value `((nil . (:level . 1))'."
  1197. :group 'org-refile
  1198. :type '(repeat
  1199. (cons
  1200. (choice :value org-agenda-files
  1201. (const :tag "All agenda files" org-agenda-files)
  1202. (const :tag "Current buffer" nil)
  1203. (function) (variable) (file))
  1204. (choice :tag "Identify target headline by"
  1205. (cons :tag "Specific tag" (const :value :tag) (string))
  1206. (cons :tag "TODO keyword" (const :value :todo) (string))
  1207. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1208. (cons :tag "Level number" (const :value :level) (integer))
  1209. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1210. (defcustom org-refile-use-outline-path nil
  1211. "Non-nil means, provide refile targets as paths.
  1212. So a level 3 headline will be available as level1/level2/level3.
  1213. When the value is `file', also include the file name (without directory)
  1214. into the path. When `full-file-path', include the full file path."
  1215. :group 'org-refile
  1216. :type '(choice
  1217. (const :tag "Not" nil)
  1218. (const :tag "Yes" t)
  1219. (const :tag "Start with file name" file)
  1220. (const :tag "Start with full file path" full-file-path)))
  1221. (defcustom org-outline-path-complete-in-steps t
  1222. "Non-nil means, complete the outline path in hierarchical steps.
  1223. When Org-mode uses the refile interface to select an outline path
  1224. \(see variable `org-refile-use-outline-path'), the completion of
  1225. the path can be done is a single go, or if can be done in steps down
  1226. the headline hierarchy. Going in steps is probably the best if you
  1227. do not use a special completion package like `ido' or `icicles'.
  1228. However, when using these packages, going in one step can be very
  1229. fast, while still showing the whole path to the entry."
  1230. :group 'org-refile
  1231. :type 'boolean)
  1232. (defgroup org-todo nil
  1233. "Options concerning TODO items in Org-mode."
  1234. :tag "Org TODO"
  1235. :group 'org)
  1236. (defgroup org-progress nil
  1237. "Options concerning Progress logging in Org-mode."
  1238. :tag "Org Progress"
  1239. :group 'org-time)
  1240. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1241. "List of TODO entry keyword sequences and their interpretation.
  1242. \\<org-mode-map>This is a list of sequences.
  1243. Each sequence starts with a symbol, either `sequence' or `type',
  1244. indicating if the keywords should be interpreted as a sequence of
  1245. action steps, or as different types of TODO items. The first
  1246. keywords are states requiring action - these states will select a headline
  1247. for inclusion into the global TODO list Org-mode produces. If one of
  1248. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1249. signify that no further action is necessary. If \"|\" is not found,
  1250. the last keyword is treated as the only DONE state of the sequence.
  1251. The command \\[org-todo] cycles an entry through these states, and one
  1252. additional state where no keyword is present. For details about this
  1253. cycling, see the manual.
  1254. TODO keywords and interpretation can also be set on a per-file basis with
  1255. the special #+SEQ_TODO and #+TYP_TODO lines.
  1256. Each keyword can optionally specify a character for fast state selection
  1257. \(in combination with the variable `org-use-fast-todo-selection')
  1258. and specifiers for state change logging, using the same syntax
  1259. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1260. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1261. indicates to record a time stamp each time this state is selected.
  1262. Each keyword may also specify if a timestamp or a note should be
  1263. recorded when entering or leaving the state, by adding additional
  1264. characters in the parenthesis after the keyword. This looks like this:
  1265. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1266. record only the time of the state change. With X and Y being either
  1267. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1268. Y when leaving the state if and only if the *target* state does not
  1269. define X. You may omit any of the fast-selection key or X or /Y,
  1270. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1271. For backward compatibility, this variable may also be just a list
  1272. of keywords - in this case the interpretation (sequence or type) will be
  1273. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1274. :group 'org-todo
  1275. :group 'org-keywords
  1276. :type '(choice
  1277. (repeat :tag "Old syntax, just keywords"
  1278. (string :tag "Keyword"))
  1279. (repeat :tag "New syntax"
  1280. (cons
  1281. (choice
  1282. :tag "Interpretation"
  1283. (const :tag "Sequence (cycling hits every state)" sequence)
  1284. (const :tag "Type (cycling directly to DONE)" type))
  1285. (repeat
  1286. (string :tag "Keyword"))))))
  1287. (defvar org-todo-keywords-1 nil
  1288. "All TODO and DONE keywords active in a buffer.")
  1289. (make-variable-buffer-local 'org-todo-keywords-1)
  1290. (defvar org-todo-keywords-for-agenda nil)
  1291. (defvar org-done-keywords-for-agenda nil)
  1292. (defvar org-todo-keyword-alist-for-agenda nil)
  1293. (defvar org-tag-alist-for-agenda nil)
  1294. (defvar org-agenda-contributing-files nil)
  1295. (defvar org-not-done-keywords nil)
  1296. (make-variable-buffer-local 'org-not-done-keywords)
  1297. (defvar org-done-keywords nil)
  1298. (make-variable-buffer-local 'org-done-keywords)
  1299. (defvar org-todo-heads nil)
  1300. (make-variable-buffer-local 'org-todo-heads)
  1301. (defvar org-todo-sets nil)
  1302. (make-variable-buffer-local 'org-todo-sets)
  1303. (defvar org-todo-log-states nil)
  1304. (make-variable-buffer-local 'org-todo-log-states)
  1305. (defvar org-todo-kwd-alist nil)
  1306. (make-variable-buffer-local 'org-todo-kwd-alist)
  1307. (defvar org-todo-key-alist nil)
  1308. (make-variable-buffer-local 'org-todo-key-alist)
  1309. (defvar org-todo-key-trigger nil)
  1310. (make-variable-buffer-local 'org-todo-key-trigger)
  1311. (defcustom org-todo-interpretation 'sequence
  1312. "Controls how TODO keywords are interpreted.
  1313. This variable is in principle obsolete and is only used for
  1314. backward compatibility, if the interpretation of todo keywords is
  1315. not given already in `org-todo-keywords'. See that variable for
  1316. more information."
  1317. :group 'org-todo
  1318. :group 'org-keywords
  1319. :type '(choice (const sequence)
  1320. (const type)))
  1321. (defcustom org-use-fast-todo-selection 'prefix
  1322. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1323. This variable describes if and under what circumstances the cycling
  1324. mechanism for TODO keywords will be replaced by a single-key, direct
  1325. selection scheme.
  1326. When nil, fast selection is never used.
  1327. When the symbol `prefix', it will be used when `org-todo' is called with
  1328. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1329. in an agenda buffer.
  1330. When t, fast selection is used by default. In this case, the prefix
  1331. argument forces cycling instead.
  1332. In all cases, the special interface is only used if access keys have actually
  1333. been assigned by the user, i.e. if keywords in the configuration are followed
  1334. by a letter in parenthesis, like TODO(t)."
  1335. :group 'org-todo
  1336. :type '(choice
  1337. (const :tag "Never" nil)
  1338. (const :tag "By default" t)
  1339. (const :tag "Only with C-u C-c C-t" prefix)))
  1340. (defcustom org-provide-todo-statistics t
  1341. "Non-nil means, update todo statistics after insert and toggle.
  1342. When this is set, todo statistics is updated in the parent of the current
  1343. entry each time a todo state is changed."
  1344. :group 'org-todo
  1345. :type 'boolean)
  1346. (defcustom org-after-todo-state-change-hook nil
  1347. "Hook which is run after the state of a TODO item was changed.
  1348. The new state (a string with a TODO keyword, or nil) is available in the
  1349. Lisp variable `state'."
  1350. :group 'org-todo
  1351. :type 'hook)
  1352. (defcustom org-todo-state-tags-triggers nil
  1353. "Tag changes that should be triggered by TODO state changes.
  1354. This is a list. Each entry is
  1355. (state-change (tag . flag) .......)
  1356. State-change can be a string with a state, and empty string to indicate the
  1357. state that has no TODO keyword, or it can be one of the symbols `todo'
  1358. or `done', meaning any not-done or done state, respectively."
  1359. :group 'org-todo
  1360. :group 'org-tags
  1361. :type '(repeat
  1362. (cons (choice :tag "When changing to"
  1363. (const :tag "Not-done state" todo)
  1364. (const :tag "Done state" done)
  1365. (string :tag "State"))
  1366. (repeat
  1367. (cons :tag "Tag action"
  1368. (string :tag "Tag")
  1369. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1370. (defcustom org-log-done nil
  1371. "Information to record when a task moves to the DONE state.
  1372. Possible values are:
  1373. nil Don't add anything, just change the keyword
  1374. time Add a time stamp to the task
  1375. note Prompt a closing note and add it with template `org-log-note-headings'
  1376. This option can also be set with on a per-file-basis with
  1377. #+STARTUP: nologdone
  1378. #+STARTUP: logdone
  1379. #+STARTUP: lognotedone
  1380. You can have local logging settings for a subtree by setting the LOGGING
  1381. property to one or more of these keywords."
  1382. :group 'org-todo
  1383. :group 'org-progress
  1384. :type '(choice
  1385. (const :tag "No logging" nil)
  1386. (const :tag "Record CLOSED timestamp" time)
  1387. (const :tag "Record CLOSED timestamp with closing note." note)))
  1388. ;; Normalize old uses of org-log-done.
  1389. (cond
  1390. ((eq org-log-done t) (setq org-log-done 'time))
  1391. ((and (listp org-log-done) (memq 'done org-log-done))
  1392. (setq org-log-done 'note)))
  1393. (defcustom org-log-note-clock-out nil
  1394. "Non-nil means, record a note when clocking out of an item.
  1395. This can also be configured on a per-file basis by adding one of
  1396. the following lines anywhere in the buffer:
  1397. #+STARTUP: lognoteclock-out
  1398. #+STARTUP: nolognoteclock-out"
  1399. :group 'org-todo
  1400. :group 'org-progress
  1401. :type 'boolean)
  1402. (defcustom org-log-done-with-time t
  1403. "Non-nil means, the CLOSED time stamp will contain date and time.
  1404. When nil, only the date will be recorded."
  1405. :group 'org-progress
  1406. :type 'boolean)
  1407. (defcustom org-log-note-headings
  1408. '((done . "CLOSING NOTE %t")
  1409. (state . "State %-12s %t")
  1410. (note . "Note taken on %t")
  1411. (clock-out . ""))
  1412. "Headings for notes added to entries.
  1413. The value is an alist, with the car being a symbol indicating the note
  1414. context, and the cdr is the heading to be used. The heading may also be the
  1415. empty string.
  1416. %t in the heading will be replaced by a time stamp.
  1417. %s will be replaced by the new TODO state, in double quotes.
  1418. %u will be replaced by the user name.
  1419. %U will be replaced by the full user name."
  1420. :group 'org-todo
  1421. :group 'org-progress
  1422. :type '(list :greedy t
  1423. (cons (const :tag "Heading when closing an item" done) string)
  1424. (cons (const :tag
  1425. "Heading when changing todo state (todo sequence only)"
  1426. state) string)
  1427. (cons (const :tag "Heading when just taking a note" note) string)
  1428. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1429. (unless (assq 'note org-log-note-headings)
  1430. (push '(note . "%t") org-log-note-headings))
  1431. (defcustom org-log-state-notes-insert-after-drawers nil
  1432. "Non-nil means, insert state change notes after any drawers in entry.
  1433. Only the drawers that *immediately* follow the headline and the
  1434. deadline/scheduled line are skipped.
  1435. When nil, insert notes right after the heading and perhaps the line
  1436. with deadline/scheduling if present."
  1437. :group 'org-todo
  1438. :group 'org-progress
  1439. :type 'boolean)
  1440. (defcustom org-log-states-order-reversed t
  1441. "Non-nil means, the latest state change note will be directly after heading.
  1442. When nil, the notes will be orderer according to time."
  1443. :group 'org-todo
  1444. :group 'org-progress
  1445. :type 'boolean)
  1446. (defcustom org-log-repeat 'time
  1447. "Non-nil means, record moving through the DONE state when triggering repeat.
  1448. An auto-repeating tasks is immediately switched back to TODO when marked
  1449. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1450. the TODO keyword definition, or recording a closing note by setting
  1451. `org-log-done', there will be no record of the task moving through DONE.
  1452. This variable forces taking a note anyway. Possible values are:
  1453. nil Don't force a record
  1454. time Record a time stamp
  1455. note Record a note
  1456. This option can also be set with on a per-file-basis with
  1457. #+STARTUP: logrepeat
  1458. #+STARTUP: lognoterepeat
  1459. #+STARTUP: nologrepeat
  1460. You can have local logging settings for a subtree by setting the LOGGING
  1461. property to one or more of these keywords."
  1462. :group 'org-todo
  1463. :group 'org-progress
  1464. :type '(choice
  1465. (const :tag "Don't force a record" nil)
  1466. (const :tag "Force recording the DONE state" time)
  1467. (const :tag "Force recording a note with the DONE state" note)))
  1468. (defgroup org-priorities nil
  1469. "Priorities in Org-mode."
  1470. :tag "Org Priorities"
  1471. :group 'org-todo)
  1472. (defcustom org-highest-priority ?A
  1473. "The highest priority of TODO items. A character like ?A, ?B etc.
  1474. Must have a smaller ASCII number than `org-lowest-priority'."
  1475. :group 'org-priorities
  1476. :type 'character)
  1477. (defcustom org-lowest-priority ?C
  1478. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1479. Must have a larger ASCII number than `org-highest-priority'."
  1480. :group 'org-priorities
  1481. :type 'character)
  1482. (defcustom org-default-priority ?B
  1483. "The default priority of TODO items.
  1484. This is the priority an item get if no explicit priority is given."
  1485. :group 'org-priorities
  1486. :type 'character)
  1487. (defcustom org-priority-start-cycle-with-default t
  1488. "Non-nil means, start with default priority when starting to cycle.
  1489. When this is nil, the first step in the cycle will be (depending on the
  1490. command used) one higher or lower that the default priority."
  1491. :group 'org-priorities
  1492. :type 'boolean)
  1493. (defgroup org-time nil
  1494. "Options concerning time stamps and deadlines in Org-mode."
  1495. :tag "Org Time"
  1496. :group 'org)
  1497. (defcustom org-insert-labeled-timestamps-at-point nil
  1498. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1499. When nil, these labeled time stamps are forces into the second line of an
  1500. entry, just after the headline. When scheduling from the global TODO list,
  1501. the time stamp will always be forced into the second line."
  1502. :group 'org-time
  1503. :type 'boolean)
  1504. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1505. "Formats for `format-time-string' which are used for time stamps.
  1506. It is not recommended to change this constant.")
  1507. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1508. "Number of minutes to round time stamps to.
  1509. These are two values, the first applies when first creating a time stamp.
  1510. The second applies when changing it with the commands `S-up' and `S-down'.
  1511. When changing the time stamp, this means that it will change in steps
  1512. of N minutes, as given by the second value.
  1513. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1514. numbers should be factors of 60, so for example 5, 10, 15.
  1515. When this is larger than 1, you can still force an exact time-stamp by using
  1516. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1517. and by using a prefix arg to `S-up/down' to specify the exact number
  1518. of minutes to shift."
  1519. :group 'org-time
  1520. :get '(lambda (var) ; Make sure all entries have 5 elements
  1521. (if (integerp (default-value var))
  1522. (list (default-value var) 5)
  1523. (default-value var)))
  1524. :type '(list
  1525. (integer :tag "when inserting times")
  1526. (integer :tag "when modifying times")))
  1527. ;; Normalize old customizations of this variable.
  1528. (when (integerp org-time-stamp-rounding-minutes)
  1529. (setq org-time-stamp-rounding-minutes
  1530. (list org-time-stamp-rounding-minutes
  1531. org-time-stamp-rounding-minutes)))
  1532. (defcustom org-display-custom-times nil
  1533. "Non-nil means, overlay custom formats over all time stamps.
  1534. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1535. To turn this on on a per-file basis, insert anywhere in the file:
  1536. #+STARTUP: customtime"
  1537. :group 'org-time
  1538. :set 'set-default
  1539. :type 'sexp)
  1540. (make-variable-buffer-local 'org-display-custom-times)
  1541. (defcustom org-time-stamp-custom-formats
  1542. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1543. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1544. These are overlayed over the default ISO format if the variable
  1545. `org-display-custom-times' is set. Time like %H:%M should be at the
  1546. end of the second format."
  1547. :group 'org-time
  1548. :type 'sexp)
  1549. (defun org-time-stamp-format (&optional long inactive)
  1550. "Get the right format for a time string."
  1551. (let ((f (if long (cdr org-time-stamp-formats)
  1552. (car org-time-stamp-formats))))
  1553. (if inactive
  1554. (concat "[" (substring f 1 -1) "]")
  1555. f)))
  1556. (defcustom org-time-clocksum-format "%d:%02d"
  1557. "The format string used when creating CLOCKSUM lines, or when
  1558. org-mode generates a time duration."
  1559. :group 'org-time
  1560. :type 'string)
  1561. (defcustom org-deadline-warning-days 14
  1562. "No. of days before expiration during which a deadline becomes active.
  1563. This variable governs the display in sparse trees and in the agenda.
  1564. When 0 or negative, it means use this number (the absolute value of it)
  1565. even if a deadline has a different individual lead time specified."
  1566. :group 'org-time
  1567. :group 'org-agenda-daily/weekly
  1568. :type 'number)
  1569. (defcustom org-read-date-prefer-future t
  1570. "Non-nil means, assume future for incomplete date input from user.
  1571. This affects the following situations:
  1572. 1. The user gives a day, but no month.
  1573. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1574. read this as the third of *next* month. However, if you enter \"17\",
  1575. it will be considered as *this* month.
  1576. 2. The user gives a month but not a year.
  1577. For example, if it is april and you enter \"feb 2\", this will be read
  1578. as feb 2, *next* year. \"May 5\", however, will be this year.
  1579. Currently this does not work for ISO week specifications.
  1580. When this option is nil, the current month and year will always be used
  1581. as defaults."
  1582. :group 'org-time
  1583. :type 'boolean)
  1584. (defcustom org-read-date-display-live t
  1585. "Non-nil means, display current interpretation of date prompt live.
  1586. This display will be in an overlay, in the minibuffer."
  1587. :group 'org-time
  1588. :type 'boolean)
  1589. (defcustom org-read-date-popup-calendar t
  1590. "Non-nil means, pop up a calendar when prompting for a date.
  1591. In the calendar, the date can be selected with mouse-1. However, the
  1592. minibuffer will also be active, and you can simply enter the date as well.
  1593. When nil, only the minibuffer will be available."
  1594. :group 'org-time
  1595. :type 'boolean)
  1596. (if (fboundp 'defvaralias)
  1597. (defvaralias 'org-popup-calendar-for-date-prompt
  1598. 'org-read-date-popup-calendar))
  1599. (defcustom org-extend-today-until 0
  1600. "The hour when your day really ends. Must be an integer.
  1601. This has influence for the following applications:
  1602. - When switching the agenda to \"today\". It it is still earlier than
  1603. the time given here, the day recognized as TODAY is actually yesterday.
  1604. - When a date is read from the user and it is still before the time given
  1605. here, the current date and time will be assumed to be yesterday, 23:59.
  1606. Also, timestamps inserted in remember templates follow this rule.
  1607. IMPORTANT: This is a feature whose implementation is and likely will
  1608. remain incomplete. Really, it is only here because past midnight seems to
  1609. be the favorite working time of John Wiegley :-)"
  1610. :group 'org-time
  1611. :type 'number)
  1612. (defcustom org-edit-timestamp-down-means-later nil
  1613. "Non-nil means, S-down will increase the time in a time stamp.
  1614. When nil, S-up will increase."
  1615. :group 'org-time
  1616. :type 'boolean)
  1617. (defcustom org-calendar-follow-timestamp-change t
  1618. "Non-nil means, make the calendar window follow timestamp changes.
  1619. When a timestamp is modified and the calendar window is visible, it will be
  1620. moved to the new date."
  1621. :group 'org-time
  1622. :type 'boolean)
  1623. (defgroup org-tags nil
  1624. "Options concerning tags in Org-mode."
  1625. :tag "Org Tags"
  1626. :group 'org)
  1627. (defcustom org-tag-alist nil
  1628. "List of tags allowed in Org-mode files.
  1629. When this list is nil, Org-mode will base TAG input on what is already in the
  1630. buffer.
  1631. The value of this variable is an alist, the car of each entry must be a
  1632. keyword as a string, the cdr may be a character that is used to select
  1633. that tag through the fast-tag-selection interface.
  1634. See the manual for details."
  1635. :group 'org-tags
  1636. :type '(repeat
  1637. (choice
  1638. (cons (string :tag "Tag name")
  1639. (character :tag "Access char"))
  1640. (const :tag "Start radio group" (:startgroup))
  1641. (const :tag "End radio group" (:endgroup)))))
  1642. (defvar org-file-tags nil
  1643. "List of tags that can be inherited by all entries in the file.
  1644. The tags will be inherited if the variable `org-use-tag-inheritance'
  1645. says they should be.
  1646. This variable is populated from #+TAG lines.")
  1647. (defcustom org-use-fast-tag-selection 'auto
  1648. "Non-nil means, use fast tag selection scheme.
  1649. This is a special interface to select and deselect tags with single keys.
  1650. When nil, fast selection is never used.
  1651. When the symbol `auto', fast selection is used if and only if selection
  1652. characters for tags have been configured, either through the variable
  1653. `org-tag-alist' or through a #+TAGS line in the buffer.
  1654. When t, fast selection is always used and selection keys are assigned
  1655. automatically if necessary."
  1656. :group 'org-tags
  1657. :type '(choice
  1658. (const :tag "Always" t)
  1659. (const :tag "Never" nil)
  1660. (const :tag "When selection characters are configured" 'auto)))
  1661. (defcustom org-fast-tag-selection-single-key nil
  1662. "Non-nil means, fast tag selection exits after first change.
  1663. When nil, you have to press RET to exit it.
  1664. During fast tag selection, you can toggle this flag with `C-c'.
  1665. This variable can also have the value `expert'. In this case, the window
  1666. displaying the tags menu is not even shown, until you press C-c again."
  1667. :group 'org-tags
  1668. :type '(choice
  1669. (const :tag "No" nil)
  1670. (const :tag "Yes" t)
  1671. (const :tag "Expert" expert)))
  1672. (defvar org-fast-tag-selection-include-todo nil
  1673. "Non-nil means, fast tags selection interface will also offer TODO states.
  1674. This is an undocumented feature, you should not rely on it.")
  1675. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1676. "The column to which tags should be indented in a headline.
  1677. If this number is positive, it specifies the column. If it is negative,
  1678. it means that the tags should be flushright to that column. For example,
  1679. -80 works well for a normal 80 character screen."
  1680. :group 'org-tags
  1681. :type 'integer)
  1682. (defcustom org-auto-align-tags t
  1683. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1684. These operations change the length of a headline and therefore shift
  1685. the tags around. With this options turned on, after each such operation
  1686. the tags are again aligned to `org-tags-column'."
  1687. :group 'org-tags
  1688. :type 'boolean)
  1689. (defcustom org-use-tag-inheritance t
  1690. "Non-nil means, tags in levels apply also for sublevels.
  1691. When nil, only the tags directly given in a specific line apply there.
  1692. This may also be a list of tags that should be inherited, or a regexp that
  1693. matches tags that should be inherited. Additional control is possible
  1694. with the variable `org-tags-exclude-from-inheritance' which gives an
  1695. explicit list of tags to be excluded from inheritance., even if the value of
  1696. `org-use-tag-inheritance' would select it for inheritance.
  1697. If this option is t, a match early-on in a tree can lead to a large
  1698. number of matches in the subtree when constructing the agenda or creating
  1699. a sparse tree. If you only want to see the first match in a tree during
  1700. a search, check out the variable `org-tags-match-list-sublevels'."
  1701. :group 'org-tags
  1702. :type '(choice
  1703. (const :tag "Not" nil)
  1704. (const :tag "Always" t)
  1705. (repeat :tag "Specific tags" (string :tag "Tag"))
  1706. (regexp :tag "Tags matched by regexp")))
  1707. (defcustom org-tags-exclude-from-inheritance nil
  1708. "List of tags that should never be inherited.
  1709. This is a way to exclude a few tags from inheritance. For way to do
  1710. the opposite, to actively allow inheritance for selected tags,
  1711. see the variable `org-use-tag-inheritance'."
  1712. :group 'org-tags
  1713. :type '(repeat (string :tag "Tag")))
  1714. (defun org-tag-inherit-p (tag)
  1715. "Check if TAG is one that should be inherited."
  1716. (cond
  1717. ((member tag org-tags-exclude-from-inheritance) nil)
  1718. ((eq org-use-tag-inheritance t) t)
  1719. ((not org-use-tag-inheritance) nil)
  1720. ((stringp org-use-tag-inheritance)
  1721. (string-match org-use-tag-inheritance tag))
  1722. ((listp org-use-tag-inheritance)
  1723. (member tag org-use-tag-inheritance))
  1724. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1725. (defcustom org-tags-match-list-sublevels t
  1726. "Non-nil means list also sublevels of headlines matching tag search.
  1727. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1728. the sublevels of a headline matching a tag search often also match
  1729. the same search. Listing all of them can create very long lists.
  1730. Setting this variable to nil causes subtrees of a match to be skipped.
  1731. This option is off by default, because inheritance in on. If you turn
  1732. inheritance off, you very likely want to turn this option on.
  1733. As a special case, if the tag search is restricted to TODO items, the
  1734. value of this variable is ignored and sublevels are always checked, to
  1735. make sure all corresponding TODO items find their way into the list.
  1736. This variable is semi-obsolete and probably should always be true. It
  1737. is better to limit inheritance to certain tags using the variables
  1738. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1739. :group 'org-tags
  1740. :type 'boolean)
  1741. (defvar org-tags-history nil
  1742. "History of minibuffer reads for tags.")
  1743. (defvar org-last-tags-completion-table nil
  1744. "The last used completion table for tags.")
  1745. (defvar org-after-tags-change-hook nil
  1746. "Hook that is run after the tags in a line have changed.")
  1747. (defgroup org-properties nil
  1748. "Options concerning properties in Org-mode."
  1749. :tag "Org Properties"
  1750. :group 'org)
  1751. (defcustom org-property-format "%-10s %s"
  1752. "How property key/value pairs should be formatted by `indent-line'.
  1753. When `indent-line' hits a property definition, it will format the line
  1754. according to this format, mainly to make sure that the values are
  1755. lined-up with respect to each other."
  1756. :group 'org-properties
  1757. :type 'string)
  1758. (defcustom org-use-property-inheritance nil
  1759. "Non-nil means, properties apply also for sublevels.
  1760. This setting is chiefly used during property searches. Turning it on can
  1761. cause significant overhead when doing a search, which is why it is not
  1762. on by default.
  1763. When nil, only the properties directly given in the current entry count.
  1764. When t, every property is inherited. The value may also be a list of
  1765. properties that should have inheritance, or a regular expression matching
  1766. properties that should be inherited.
  1767. However, note that some special properties use inheritance under special
  1768. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1769. and the properties ending in \"_ALL\" when they are used as descriptor
  1770. for valid values of a property.
  1771. Note for programmers:
  1772. When querying an entry with `org-entry-get', you can control if inheritance
  1773. should be used. By default, `org-entry-get' looks only at the local
  1774. properties. You can request inheritance by setting the inherit argument
  1775. to t (to force inheritance) or to `selective' (to respect the setting
  1776. in this variable)."
  1777. :group 'org-properties
  1778. :type '(choice
  1779. (const :tag "Not" nil)
  1780. (const :tag "Always" t)
  1781. (repeat :tag "Specific properties" (string :tag "Property"))
  1782. (regexp :tag "Properties matched by regexp")))
  1783. (defun org-property-inherit-p (property)
  1784. "Check if PROPERTY is one that should be inherited."
  1785. (cond
  1786. ((eq org-use-property-inheritance t) t)
  1787. ((not org-use-property-inheritance) nil)
  1788. ((stringp org-use-property-inheritance)
  1789. (string-match org-use-property-inheritance property))
  1790. ((listp org-use-property-inheritance)
  1791. (member property org-use-property-inheritance))
  1792. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1793. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1794. "The default column format, if no other format has been defined.
  1795. This variable can be set on the per-file basis by inserting a line
  1796. #+COLUMNS: %25ITEM ....."
  1797. :group 'org-properties
  1798. :type 'string)
  1799. (defcustom org-columns-ellipses ".."
  1800. "The ellipses to be used when a field in column view is truncated.
  1801. When this is the empty string, as many characters as possible are shown,
  1802. but then there will be no visual indication that the field has been truncated.
  1803. When this is a string of length N, the last N characters of a truncated
  1804. field are replaced by this string. If the column is narrower than the
  1805. ellipses string, only part of the ellipses string will be shown."
  1806. :group 'org-properties
  1807. :type 'string)
  1808. (defcustom org-columns-modify-value-for-display-function nil
  1809. "Function that modifies values for display in column view.
  1810. For example, it can be used to cut out a certain part from a time stamp.
  1811. The function must take 2 arguments:
  1812. column-title The title of the column (*not* the property name)
  1813. value The value that should be modified.
  1814. The function should return the value that should be displayed,
  1815. or nil if the normal value should be used."
  1816. :group 'org-properties
  1817. :type 'function)
  1818. (defcustom org-effort-property "Effort"
  1819. "The property that is being used to keep track of effort estimates.
  1820. Effort estimates given in this property need to have the format H:MM."
  1821. :group 'org-properties
  1822. :group 'org-progress
  1823. :type '(string :tag "Property"))
  1824. (defconst org-global-properties-fixed
  1825. '(("VISIBILITY_ALL" . "folded children content all"))
  1826. "List of property/value pairs that can be inherited by any entry.
  1827. These are fixed values, for the preset properties.")
  1828. (defcustom org-global-properties nil
  1829. "List of property/value pairs that can be inherited by any entry.
  1830. You can set buffer-local values for the same purpose in the variable
  1831. `org-file-properties' this by adding lines like
  1832. #+PROPERTY: NAME VALUE"
  1833. :group 'org-properties
  1834. :type '(repeat
  1835. (cons (string :tag "Property")
  1836. (string :tag "Value"))))
  1837. (defvar org-file-properties nil
  1838. "List of property/value pairs that can be inherited by any entry.
  1839. Valid for the current buffer.
  1840. This variable is populated from #+PROPERTY lines.")
  1841. (make-variable-buffer-local 'org-file-properties)
  1842. (defgroup org-agenda nil
  1843. "Options concerning agenda views in Org-mode."
  1844. :tag "Org Agenda"
  1845. :group 'org)
  1846. (defvar org-category nil
  1847. "Variable used by org files to set a category for agenda display.
  1848. Such files should use a file variable to set it, for example
  1849. # -*- mode: org; org-category: \"ELisp\"
  1850. or contain a special line
  1851. #+CATEGORY: ELisp
  1852. If the file does not specify a category, then file's base name
  1853. is used instead.")
  1854. (make-variable-buffer-local 'org-category)
  1855. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1856. (defcustom org-agenda-files nil
  1857. "The files to be used for agenda display.
  1858. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1859. \\[org-remove-file]. You can also use customize to edit the list.
  1860. If an entry is a directory, all files in that directory that are matched by
  1861. `org-agenda-file-regexp' will be part of the file list.
  1862. If the value of the variable is not a list but a single file name, then
  1863. the list of agenda files is actually stored and maintained in that file, one
  1864. agenda file per line."
  1865. :group 'org-agenda
  1866. :type '(choice
  1867. (repeat :tag "List of files and directories" file)
  1868. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1869. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1870. "Regular expression to match files for `org-agenda-files'.
  1871. If any element in the list in that variable contains a directory instead
  1872. of a normal file, all files in that directory that are matched by this
  1873. regular expression will be included."
  1874. :group 'org-agenda
  1875. :type 'regexp)
  1876. (defcustom org-agenda-text-search-extra-files nil
  1877. "List of extra files to be searched by text search commands.
  1878. These files will be search in addition to the agenda files by the
  1879. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1880. Note that these files will only be searched for text search commands,
  1881. not for the other agenda views like todo lists, tag searches or the weekly
  1882. agenda. This variable is intended to list notes and possibly archive files
  1883. that should also be searched by these two commands.
  1884. In fact, if the first element in the list is the symbol `agenda-archives',
  1885. than all archive files of all agenda files will be added to the search
  1886. scope."
  1887. :group 'org-agenda
  1888. :type '(set :greedy t
  1889. (const :tag "Agenda Archives" agenda-archives)
  1890. (repeat :inline t (file))))
  1891. (if (fboundp 'defvaralias)
  1892. (defvaralias 'org-agenda-multi-occur-extra-files
  1893. 'org-agenda-text-search-extra-files))
  1894. (defcustom org-agenda-skip-unavailable-files nil
  1895. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1896. A nil value means to remove them, after a query, from the list."
  1897. :group 'org-agenda
  1898. :type 'boolean)
  1899. (defcustom org-calendar-to-agenda-key [?c]
  1900. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1901. The command `org-calendar-goto-agenda' will be bound to this key. The
  1902. default is the character `c' because then `c' can be used to switch back and
  1903. forth between agenda and calendar."
  1904. :group 'org-agenda
  1905. :type 'sexp)
  1906. (defcustom org-calendar-agenda-action-key [?k]
  1907. "The key to be installed in `calendar-mode-map' for agenda-action.
  1908. The command `org-agenda-action' will be bound to this key. The
  1909. default is the character `k' because we use the same key in the agenda."
  1910. :group 'org-agenda
  1911. :type 'sexp)
  1912. (eval-after-load "calendar"
  1913. '(progn
  1914. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1915. 'org-calendar-goto-agenda)
  1916. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1917. 'org-agenda-action)))
  1918. (defgroup org-latex nil
  1919. "Options for embedding LaTeX code into Org-mode."
  1920. :tag "Org LaTeX"
  1921. :group 'org)
  1922. (defcustom org-format-latex-options
  1923. '(:foreground default :background default :scale 1.0
  1924. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1925. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1926. "Options for creating images from LaTeX fragments.
  1927. This is a property list with the following properties:
  1928. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1929. `default' means use the foreground of the default face.
  1930. :background the background color, or \"Transparent\".
  1931. `default' means use the background of the default face.
  1932. :scale a scaling factor for the size of the images.
  1933. :html-foreground, :html-background, :html-scale
  1934. the same numbers for HTML export.
  1935. :matchers a list indicating which matchers should be used to
  1936. find LaTeX fragments. Valid members of this list are:
  1937. \"begin\" find environments
  1938. \"$\" find math expressions surrounded by $...$
  1939. \"$$\" find math expressions surrounded by $$....$$
  1940. \"\\(\" find math expressions surrounded by \\(...\\)
  1941. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1942. :group 'org-latex
  1943. :type 'plist)
  1944. (defcustom org-format-latex-header "\\documentclass{article}
  1945. \\usepackage{fullpage} % do not remove
  1946. \\usepackage{amssymb}
  1947. \\usepackage[usenames]{color}
  1948. \\usepackage{amsmath}
  1949. \\usepackage{latexsym}
  1950. \\usepackage[mathscr]{eucal}
  1951. \\pagestyle{empty} % do not remove"
  1952. "The document header used for processing LaTeX fragments."
  1953. :group 'org-latex
  1954. :type 'string)
  1955. (defgroup org-font-lock nil
  1956. "Font-lock settings for highlighting in Org-mode."
  1957. :tag "Org Font Lock"
  1958. :group 'org)
  1959. (defcustom org-level-color-stars-only nil
  1960. "Non-nil means fontify only the stars in each headline.
  1961. When nil, the entire headline is fontified.
  1962. Changing it requires restart of `font-lock-mode' to become effective
  1963. also in regions already fontified."
  1964. :group 'org-font-lock
  1965. :type 'boolean)
  1966. (defcustom org-hide-leading-stars nil
  1967. "Non-nil means, hide the first N-1 stars in a headline.
  1968. This works by using the face `org-hide' for these stars. This
  1969. face is white for a light background, and black for a dark
  1970. background. You may have to customize the face `org-hide' to
  1971. make this work.
  1972. Changing it requires restart of `font-lock-mode' to become effective
  1973. also in regions already fontified.
  1974. You may also set this on a per-file basis by adding one of the following
  1975. lines to the buffer:
  1976. #+STARTUP: hidestars
  1977. #+STARTUP: showstars"
  1978. :group 'org-font-lock
  1979. :type 'boolean)
  1980. (defcustom org-fontify-done-headline nil
  1981. "Non-nil means, change the face of a headline if it is marked DONE.
  1982. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1983. When this is non-nil, the headline after the keyword is set to the
  1984. `org-headline-done' as an additional indication."
  1985. :group 'org-font-lock
  1986. :type 'boolean)
  1987. (defcustom org-fontify-emphasized-text t
  1988. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1989. Changing this variable requires a restart of Emacs to take effect."
  1990. :group 'org-font-lock
  1991. :type 'boolean)
  1992. (defcustom org-highlight-latex-fragments-and-specials nil
  1993. "Non-nil means, fontify what is treated specially by the exporters."
  1994. :group 'org-font-lock
  1995. :type 'boolean)
  1996. (defcustom org-hide-emphasis-markers nil
  1997. "Non-nil mean font-lock should hide the emphasis marker characters."
  1998. :group 'org-font-lock
  1999. :type 'boolean)
  2000. (defvar org-emph-re nil
  2001. "Regular expression for matching emphasis.")
  2002. (defvar org-verbatim-re nil
  2003. "Regular expression for matching verbatim text.")
  2004. (defvar org-emphasis-regexp-components) ; defined just below
  2005. (defvar org-emphasis-alist) ; defined just below
  2006. (defun org-set-emph-re (var val)
  2007. "Set variable and compute the emphasis regular expression."
  2008. (set var val)
  2009. (when (and (boundp 'org-emphasis-alist)
  2010. (boundp 'org-emphasis-regexp-components)
  2011. org-emphasis-alist org-emphasis-regexp-components)
  2012. (let* ((e org-emphasis-regexp-components)
  2013. (pre (car e))
  2014. (post (nth 1 e))
  2015. (border (nth 2 e))
  2016. (body (nth 3 e))
  2017. (nl (nth 4 e))
  2018. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  2019. (body1 (concat body "*?"))
  2020. (markers (mapconcat 'car org-emphasis-alist ""))
  2021. (vmarkers (mapconcat
  2022. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2023. org-emphasis-alist "")))
  2024. ;; make sure special characters appear at the right position in the class
  2025. (if (string-match "\\^" markers)
  2026. (setq markers (concat (replace-match "" t t markers) "^")))
  2027. (if (string-match "-" markers)
  2028. (setq markers (concat (replace-match "" t t markers) "-")))
  2029. (if (string-match "\\^" vmarkers)
  2030. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2031. (if (string-match "-" vmarkers)
  2032. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2033. (if (> nl 0)
  2034. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2035. (int-to-string nl) "\\}")))
  2036. ;; Make the regexp
  2037. (setq org-emph-re
  2038. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  2039. "\\("
  2040. "\\([" markers "]\\)"
  2041. "\\("
  2042. "[^" border "]\\|"
  2043. "[^" border (if (and nil stacked) markers) "]"
  2044. body1
  2045. "[^" border (if (and nil stacked) markers) "]"
  2046. "\\)"
  2047. "\\3\\)"
  2048. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  2049. (setq org-verbatim-re
  2050. (concat "\\([" pre "]\\|^\\)"
  2051. "\\("
  2052. "\\([" vmarkers "]\\)"
  2053. "\\("
  2054. "[^" border "]\\|"
  2055. "[^" border "]"
  2056. body1
  2057. "[^" border "]"
  2058. "\\)"
  2059. "\\3\\)"
  2060. "\\([" post "]\\|$\\)")))))
  2061. (defcustom org-emphasis-regexp-components
  2062. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2063. "Components used to build the regular expression for emphasis.
  2064. This is a list with 6 entries. Terminology: In an emphasis string
  2065. like \" *strong word* \", we call the initial space PREMATCH, the final
  2066. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2067. and \"trong wor\" is the body. The different components in this variable
  2068. specify what is allowed/forbidden in each part:
  2069. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2070. post Chars allowed as postmatch. End of line will be allowed too.
  2071. border The chars *forbidden* as border characters.
  2072. body-regexp A regexp like \".\" to match a body character. Don't use
  2073. non-shy groups here, and don't allow newline here.
  2074. newline The maximum number of newlines allowed in an emphasis exp.
  2075. Use customize to modify this, or restart Emacs after changing it."
  2076. :group 'org-font-lock
  2077. :set 'org-set-emph-re
  2078. :type '(list
  2079. (sexp :tag "Allowed chars in pre ")
  2080. (sexp :tag "Allowed chars in post ")
  2081. (sexp :tag "Forbidden chars in border ")
  2082. (sexp :tag "Regexp for body ")
  2083. (integer :tag "number of newlines allowed")
  2084. (option (boolean :tag "Please ignore this button"))))
  2085. (defcustom org-emphasis-alist
  2086. `(("*" bold "<b>" "</b>")
  2087. ("/" italic "<i>" "</i>")
  2088. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2089. ("=" org-code "<code>" "</code>" verbatim)
  2090. ("~" org-verbatim "<code>" "</code>" verbatim)
  2091. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2092. "<del>" "</del>")
  2093. )
  2094. "Special syntax for emphasized text.
  2095. Text starting and ending with a special character will be emphasized, for
  2096. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2097. characters, the face to be used by font-lock for highlighting in Org-mode
  2098. Emacs buffers, and the HTML tags to be used for this.
  2099. Use customize to modify this, or restart Emacs after changing it."
  2100. :group 'org-font-lock
  2101. :set 'org-set-emph-re
  2102. :type '(repeat
  2103. (list
  2104. (string :tag "Marker character")
  2105. (choice
  2106. (face :tag "Font-lock-face")
  2107. (plist :tag "Face property list"))
  2108. (string :tag "HTML start tag")
  2109. (string :tag "HTML end tag")
  2110. (option (const verbatim)))))
  2111. ;;; Miscellaneous options
  2112. (defgroup org-completion nil
  2113. "Completion in Org-mode."
  2114. :tag "Org Completion"
  2115. :group 'org)
  2116. (defcustom org-completion-use-ido nil
  2117. "Non-nil means, use ido completion wherever possible.
  2118. Note that `ido-mode' must be active for this variable to be relevant.
  2119. If you decide to turn this variable on, you might well want to turn off
  2120. `org-outline-path-complete-in-steps'."
  2121. :group 'org-completion
  2122. :type 'boolean)
  2123. (defcustom org-completion-fallback-command 'hippie-expand
  2124. "The expansion command called by \\[org-complete] in normal context.
  2125. Normal means, no org-mode-specific context."
  2126. :group 'org-completion
  2127. :type 'function)
  2128. ;;; Functions and variables from ther packages
  2129. ;; Declared here to avoid compiler warnings
  2130. ;; XEmacs only
  2131. (defvar outline-mode-menu-heading)
  2132. (defvar outline-mode-menu-show)
  2133. (defvar outline-mode-menu-hide)
  2134. (defvar zmacs-regions) ; XEmacs regions
  2135. ;; Emacs only
  2136. (defvar mark-active)
  2137. ;; Various packages
  2138. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2139. (declare-function calendar-forward-day "cal-move" (arg))
  2140. (declare-function calendar-goto-date "cal-move" (date))
  2141. (declare-function calendar-goto-today "cal-move" ())
  2142. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2143. (defvar calc-embedded-close-formula)
  2144. (defvar calc-embedded-open-formula)
  2145. (declare-function cdlatex-tab "ext:cdlatex" ())
  2146. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2147. (defvar font-lock-unfontify-region-function)
  2148. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2149. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2150. (defvar iswitchb-temp-buflist)
  2151. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2152. (declare-function org-agenda-skip "org-agenda" ())
  2153. (declare-function org-format-agenda-item "org-agenda"
  2154. (extra txt &optional category tags dotime noprefix remove-re))
  2155. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2156. (declare-function org-agenda-change-all-lines "org-agenda"
  2157. (newhead hdmarker &optional fixface just-this))
  2158. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2159. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2160. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2161. (beg end))
  2162. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2163. (declare-function parse-time-string "parse-time" (string))
  2164. (declare-function remember "remember" (&optional initial))
  2165. (declare-function remember-buffer-desc "remember" ())
  2166. (declare-function remember-finalize "remember" ())
  2167. (defvar remember-save-after-remembering)
  2168. (defvar remember-data-file)
  2169. (defvar remember-register)
  2170. (defvar remember-buffer)
  2171. (defvar remember-handler-functions)
  2172. (defvar remember-annotation-functions)
  2173. (defvar texmathp-why)
  2174. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2175. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2176. (defvar w3m-current-url)
  2177. (defvar w3m-current-title)
  2178. (defvar org-latex-regexps)
  2179. ;;; Autoload and prepare some org modules
  2180. ;; Some table stuff that needs to be defined here, because it is used
  2181. ;; by the functions setting up org-mode or checking for table context.
  2182. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2183. "Detects an org-type or table-type table.")
  2184. (defconst org-table-line-regexp "^[ \t]*|"
  2185. "Detects an org-type table line.")
  2186. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2187. "Detects an org-type table line.")
  2188. (defconst org-table-hline-regexp "^[ \t]*|-"
  2189. "Detects an org-type table hline.")
  2190. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2191. "Detects a table-type table hline.")
  2192. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2193. "Searching from within a table (any type) this finds the first line
  2194. outside the table.")
  2195. ;; Autoload the functions in org-table.el that are needed by functions here.
  2196. (eval-and-compile
  2197. (org-autoload "org-table"
  2198. '(org-table-align org-table-begin org-table-blank-field
  2199. org-table-convert org-table-convert-region org-table-copy-down
  2200. org-table-copy-region org-table-create
  2201. org-table-create-or-convert-from-region
  2202. org-table-create-with-table.el org-table-current-dline
  2203. org-table-cut-region org-table-delete-column org-table-edit-field
  2204. org-table-edit-formulas org-table-end org-table-eval-formula
  2205. org-table-export org-table-field-info
  2206. org-table-get-stored-formulas org-table-goto-column
  2207. org-table-hline-and-move org-table-import org-table-insert-column
  2208. org-table-insert-hline org-table-insert-row org-table-iterate
  2209. org-table-justify-field-maybe org-table-kill-row
  2210. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2211. org-table-move-column org-table-move-column-left
  2212. org-table-move-column-right org-table-move-row
  2213. org-table-move-row-down org-table-move-row-up
  2214. org-table-next-field org-table-next-row org-table-paste-rectangle
  2215. org-table-previous-field org-table-recalculate
  2216. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2217. org-table-toggle-coordinate-overlays
  2218. org-table-toggle-formula-debugger org-table-wrap-region
  2219. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2220. (defun org-at-table-p (&optional table-type)
  2221. "Return t if the cursor is inside an org-type table.
  2222. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2223. (if org-enable-table-editor
  2224. (save-excursion
  2225. (beginning-of-line 1)
  2226. (looking-at (if table-type org-table-any-line-regexp
  2227. org-table-line-regexp)))
  2228. nil))
  2229. (defsubst org-table-p () (org-at-table-p))
  2230. (defun org-at-table.el-p ()
  2231. "Return t if and only if we are at a table.el table."
  2232. (and (org-at-table-p 'any)
  2233. (save-excursion
  2234. (goto-char (org-table-begin 'any))
  2235. (looking-at org-table1-hline-regexp))))
  2236. (defun org-table-recognize-table.el ()
  2237. "If there is a table.el table nearby, recognize it and move into it."
  2238. (if org-table-tab-recognizes-table.el
  2239. (if (org-at-table.el-p)
  2240. (progn
  2241. (beginning-of-line 1)
  2242. (if (looking-at org-table-dataline-regexp)
  2243. nil
  2244. (if (looking-at org-table1-hline-regexp)
  2245. (progn
  2246. (beginning-of-line 2)
  2247. (if (looking-at org-table-any-border-regexp)
  2248. (beginning-of-line -1)))))
  2249. (if (re-search-forward "|" (org-table-end t) t)
  2250. (progn
  2251. (require 'table)
  2252. (if (table--at-cell-p (point))
  2253. t
  2254. (message "recognizing table.el table...")
  2255. (table-recognize-table)
  2256. (message "recognizing table.el table...done")))
  2257. (error "This should not happen..."))
  2258. t)
  2259. nil)
  2260. nil))
  2261. (defun org-at-table-hline-p ()
  2262. "Return t if the cursor is inside a hline in a table."
  2263. (if org-enable-table-editor
  2264. (save-excursion
  2265. (beginning-of-line 1)
  2266. (looking-at org-table-hline-regexp))
  2267. nil))
  2268. (defvar org-table-clean-did-remove-column nil)
  2269. (defun org-table-map-tables (function)
  2270. "Apply FUNCTION to the start of all tables in the buffer."
  2271. (save-excursion
  2272. (save-restriction
  2273. (widen)
  2274. (goto-char (point-min))
  2275. (while (re-search-forward org-table-any-line-regexp nil t)
  2276. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2277. (beginning-of-line 1)
  2278. (if (looking-at org-table-line-regexp)
  2279. (save-excursion (funcall function)))
  2280. (re-search-forward org-table-any-border-regexp nil 1))))
  2281. (message "Mapping tables: done"))
  2282. ;; Declare and autoload functions from org-exp.el
  2283. (declare-function org-default-export-plist "org-exp")
  2284. (declare-function org-infile-export-plist "org-exp")
  2285. (declare-function org-get-current-options "org-exp")
  2286. (eval-and-compile
  2287. (org-autoload "org-exp"
  2288. '(org-export org-export-as-ascii org-export-visible
  2289. org-insert-export-options-template org-export-as-html-and-open
  2290. org-export-as-html-batch org-export-as-html-to-buffer
  2291. org-replace-region-by-html org-export-region-as-html
  2292. org-export-as-html org-export-icalendar-this-file
  2293. org-export-icalendar-all-agenda-files
  2294. org-table-clean-before-export
  2295. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2296. ;; Declare and autoload functions from org-agenda.el
  2297. (eval-and-compile
  2298. (org-autoload "org-agenda"
  2299. '(org-agenda org-agenda-list org-search-view
  2300. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2301. org-diary org-agenda-to-appt)))
  2302. ;; Autoload org-remember
  2303. (eval-and-compile
  2304. (org-autoload "org-remember"
  2305. '(org-remember-insinuate org-remember-annotation
  2306. org-remember-apply-template org-remember org-remember-handler)))
  2307. ;; Autoload org-clock.el
  2308. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2309. (beg end))
  2310. (declare-function org-update-mode-line "org-clock" ())
  2311. (defvar org-clock-start-time)
  2312. (defvar org-clock-marker (make-marker)
  2313. "Marker recording the last clock-in.")
  2314. (eval-and-compile
  2315. (org-autoload
  2316. "org-clock"
  2317. '(org-clock-in org-clock-out org-clock-cancel
  2318. org-clock-goto org-clock-sum org-clock-display
  2319. org-remove-clock-overlays org-clock-report
  2320. org-clocktable-shift org-dblock-write:clocktable
  2321. org-get-clocktable)))
  2322. (defun org-clock-update-time-maybe ()
  2323. "If this is a CLOCK line, update it and return t.
  2324. Otherwise, return nil."
  2325. (interactive)
  2326. (save-excursion
  2327. (beginning-of-line 1)
  2328. (skip-chars-forward " \t")
  2329. (when (looking-at org-clock-string)
  2330. (let ((re (concat "[ \t]*" org-clock-string
  2331. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2332. "\\([ \t]*=>.*\\)?\\)?"))
  2333. ts te h m s neg)
  2334. (cond
  2335. ((not (looking-at re))
  2336. nil)
  2337. ((not (match-end 2))
  2338. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2339. (> org-clock-marker (point))
  2340. (<= org-clock-marker (point-at-eol)))
  2341. ;; The clock is running here
  2342. (setq org-clock-start-time
  2343. (apply 'encode-time
  2344. (org-parse-time-string (match-string 1))))
  2345. (org-update-mode-line)))
  2346. (t
  2347. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2348. (end-of-line 1)
  2349. (setq ts (match-string 1)
  2350. te (match-string 3))
  2351. (setq s (- (time-to-seconds
  2352. (apply 'encode-time (org-parse-time-string te)))
  2353. (time-to-seconds
  2354. (apply 'encode-time (org-parse-time-string ts))))
  2355. neg (< s 0)
  2356. s (abs s)
  2357. h (floor (/ s 3600))
  2358. s (- s (* 3600 h))
  2359. m (floor (/ s 60))
  2360. s (- s (* 60 s)))
  2361. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2362. t))))))
  2363. (defun org-check-running-clock ()
  2364. "Check if the current buffer contains the running clock.
  2365. If yes, offer to stop it and to save the buffer with the changes."
  2366. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2367. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2368. (buffer-name))))
  2369. (org-clock-out)
  2370. (when (y-or-n-p "Save changed buffer?")
  2371. (save-buffer))))
  2372. (defun org-clocktable-try-shift (dir n)
  2373. "Check if this line starts a clock table, if yes, shift the time block."
  2374. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2375. (org-clocktable-shift dir n)))
  2376. ;; Autoload org-timer.el
  2377. ;(declare-function org-timer "org-timer")
  2378. (eval-and-compile
  2379. (org-autoload
  2380. "org-timer"
  2381. '(org-timer-start org-timer org-timer-item
  2382. org-timer-change-times-in-region)))
  2383. ;; Autoload archiving code
  2384. ;; The stuff that is needed for cycling and tags has to be defined here.
  2385. (defgroup org-archive nil
  2386. "Options concerning archiving in Org-mode."
  2387. :tag "Org Archive"
  2388. :group 'org-structure)
  2389. (defcustom org-archive-location "%s_archive::"
  2390. "The location where subtrees should be archived.
  2391. The value of this variable is a string, consisting of two parts,
  2392. separated by a double-colon. The first part is a filename and
  2393. the second part is a headline.
  2394. When the filename is omitted, archiving happens in the same file.
  2395. %s in the filename will be replaced by the current file
  2396. name (without the directory part). Archiving to a different file
  2397. is useful to keep archived entries from contributing to the
  2398. Org-mode Agenda.
  2399. The archived entries will be filed as subtrees of the specified
  2400. headline. When the headline is omitted, the subtrees are simply
  2401. filed away at the end of the file, as top-level entries.
  2402. Here are a few examples:
  2403. \"%s_archive::\"
  2404. If the current file is Projects.org, archive in file
  2405. Projects.org_archive, as top-level trees. This is the default.
  2406. \"::* Archived Tasks\"
  2407. Archive in the current file, under the top-level headline
  2408. \"* Archived Tasks\".
  2409. \"~/org/archive.org::\"
  2410. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2411. \"basement::** Finished Tasks\"
  2412. Archive in file ./basement (relative path), as level 3 trees
  2413. below the level 2 heading \"** Finished Tasks\".
  2414. You may set this option on a per-file basis by adding to the buffer a
  2415. line like
  2416. #+ARCHIVE: basement::** Finished Tasks
  2417. You may also define it locally for a subtree by setting an ARCHIVE property
  2418. in the entry. If such a property is found in an entry, or anywhere up
  2419. the hierarchy, it will be used."
  2420. :group 'org-archive
  2421. :type 'string)
  2422. (defcustom org-archive-tag "ARCHIVE"
  2423. "The tag that marks a subtree as archived.
  2424. An archived subtree does not open during visibility cycling, and does
  2425. not contribute to the agenda listings.
  2426. After changing this, font-lock must be restarted in the relevant buffers to
  2427. get the proper fontification."
  2428. :group 'org-archive
  2429. :group 'org-keywords
  2430. :type 'string)
  2431. (defcustom org-agenda-skip-archived-trees t
  2432. "Non-nil means, the agenda will skip any items located in archived trees.
  2433. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2434. variable is no longer recommended, you should leave it at the value t.
  2435. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2436. :group 'org-archive
  2437. :group 'org-agenda-skip
  2438. :type 'boolean)
  2439. (defcustom org-cycle-open-archived-trees nil
  2440. "Non-nil means, `org-cycle' will open archived trees.
  2441. An archived tree is a tree marked with the tag ARCHIVE.
  2442. When nil, archived trees will stay folded. You can still open them with
  2443. normal outline commands like `show-all', but not with the cycling commands."
  2444. :group 'org-archive
  2445. :group 'org-cycle
  2446. :type 'boolean)
  2447. (defcustom org-sparse-tree-open-archived-trees nil
  2448. "Non-nil means sparse tree construction shows matches in archived trees.
  2449. When nil, matches in these trees are highlighted, but the trees are kept in
  2450. collapsed state."
  2451. :group 'org-archive
  2452. :group 'org-sparse-trees
  2453. :type 'boolean)
  2454. (defun org-cycle-hide-archived-subtrees (state)
  2455. "Re-hide all archived subtrees after a visibility state change."
  2456. (when (and (not org-cycle-open-archived-trees)
  2457. (not (memq state '(overview folded))))
  2458. (save-excursion
  2459. (let* ((globalp (memq state '(contents all)))
  2460. (beg (if globalp (point-min) (point)))
  2461. (end (if globalp (point-max) (org-end-of-subtree t))))
  2462. (org-hide-archived-subtrees beg end)
  2463. (goto-char beg)
  2464. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2465. (message "%s" (substitute-command-keys
  2466. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2467. (defun org-force-cycle-archived ()
  2468. "Cycle subtree even if it is archived."
  2469. (interactive)
  2470. (setq this-command 'org-cycle)
  2471. (let ((org-cycle-open-archived-trees t))
  2472. (call-interactively 'org-cycle)))
  2473. (defun org-hide-archived-subtrees (beg end)
  2474. "Re-hide all archived subtrees after a visibility state change."
  2475. (save-excursion
  2476. (let* ((re (concat ":" org-archive-tag ":")))
  2477. (goto-char beg)
  2478. (while (re-search-forward re end t)
  2479. (and (org-on-heading-p) (hide-subtree))
  2480. (org-end-of-subtree t)))))
  2481. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2482. (eval-and-compile
  2483. (org-autoload "org-archive"
  2484. '(org-add-archive-files org-archive-subtree
  2485. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2486. ;; Autoload Column View Code
  2487. (declare-function org-columns-number-to-string "org-colview")
  2488. (declare-function org-columns-get-format-and-top-level "org-colview")
  2489. (declare-function org-columns-compute "org-colview")
  2490. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2491. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2492. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2493. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2494. ;; Autoload ID code
  2495. (declare-function org-id-store-link "org-id")
  2496. (org-autoload "org-id"
  2497. '(org-id-get-create org-id-new org-id-copy org-id-get
  2498. org-id-get-with-outline-path-completion
  2499. org-id-get-with-outline-drilling
  2500. org-id-goto org-id-find org-id-store-link))
  2501. ;;; Variables for pre-computed regular expressions, all buffer local
  2502. (defvar org-drawer-regexp nil
  2503. "Matches first line of a hidden block.")
  2504. (make-variable-buffer-local 'org-drawer-regexp)
  2505. (defvar org-todo-regexp nil
  2506. "Matches any of the TODO state keywords.")
  2507. (make-variable-buffer-local 'org-todo-regexp)
  2508. (defvar org-not-done-regexp nil
  2509. "Matches any of the TODO state keywords except the last one.")
  2510. (make-variable-buffer-local 'org-not-done-regexp)
  2511. (defvar org-todo-line-regexp nil
  2512. "Matches a headline and puts TODO state into group 2 if present.")
  2513. (make-variable-buffer-local 'org-todo-line-regexp)
  2514. (defvar org-complex-heading-regexp nil
  2515. "Matches a headline and puts everything into groups:
  2516. group 1: the stars
  2517. group 2: The todo keyword, maybe
  2518. group 3: Priority cookie
  2519. group 4: True headline
  2520. group 5: Tags")
  2521. (make-variable-buffer-local 'org-complex-heading-regexp)
  2522. (defvar org-todo-line-tags-regexp nil
  2523. "Matches a headline and puts TODO state into group 2 if present.
  2524. Also put tags into group 4 if tags are present.")
  2525. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2526. (defvar org-nl-done-regexp nil
  2527. "Matches newline followed by a headline with the DONE keyword.")
  2528. (make-variable-buffer-local 'org-nl-done-regexp)
  2529. (defvar org-looking-at-done-regexp nil
  2530. "Matches the DONE keyword a point.")
  2531. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2532. (defvar org-ds-keyword-length 12
  2533. "Maximum length of the Deadline and SCHEDULED keywords.")
  2534. (make-variable-buffer-local 'org-ds-keyword-length)
  2535. (defvar org-deadline-regexp nil
  2536. "Matches the DEADLINE keyword.")
  2537. (make-variable-buffer-local 'org-deadline-regexp)
  2538. (defvar org-deadline-time-regexp nil
  2539. "Matches the DEADLINE keyword together with a time stamp.")
  2540. (make-variable-buffer-local 'org-deadline-time-regexp)
  2541. (defvar org-deadline-line-regexp nil
  2542. "Matches the DEADLINE keyword and the rest of the line.")
  2543. (make-variable-buffer-local 'org-deadline-line-regexp)
  2544. (defvar org-scheduled-regexp nil
  2545. "Matches the SCHEDULED keyword.")
  2546. (make-variable-buffer-local 'org-scheduled-regexp)
  2547. (defvar org-scheduled-time-regexp nil
  2548. "Matches the SCHEDULED keyword together with a time stamp.")
  2549. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2550. (defvar org-closed-time-regexp nil
  2551. "Matches the CLOSED keyword together with a time stamp.")
  2552. (make-variable-buffer-local 'org-closed-time-regexp)
  2553. (defvar org-keyword-time-regexp nil
  2554. "Matches any of the 4 keywords, together with the time stamp.")
  2555. (make-variable-buffer-local 'org-keyword-time-regexp)
  2556. (defvar org-keyword-time-not-clock-regexp nil
  2557. "Matches any of the 3 keywords, together with the time stamp.")
  2558. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2559. (defvar org-maybe-keyword-time-regexp nil
  2560. "Matches a timestamp, possibly preceeded by a keyword.")
  2561. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2562. (defvar org-planning-or-clock-line-re nil
  2563. "Matches a line with planning or clock info.")
  2564. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2565. (defconst org-plain-time-of-day-regexp
  2566. (concat
  2567. "\\(\\<[012]?[0-9]"
  2568. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2569. "\\(--?"
  2570. "\\(\\<[012]?[0-9]"
  2571. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2572. "\\)?")
  2573. "Regular expression to match a plain time or time range.
  2574. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2575. groups carry important information:
  2576. 0 the full match
  2577. 1 the first time, range or not
  2578. 8 the second time, if it is a range.")
  2579. (defconst org-plain-time-extension-regexp
  2580. (concat
  2581. "\\(\\<[012]?[0-9]"
  2582. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2583. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2584. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2585. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2586. groups carry important information:
  2587. 0 the full match
  2588. 7 hours of duration
  2589. 9 minutes of duration")
  2590. (defconst org-stamp-time-of-day-regexp
  2591. (concat
  2592. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2593. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2594. "\\(--?"
  2595. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2596. "Regular expression to match a timestamp time or time range.
  2597. After a match, the following groups carry important information:
  2598. 0 the full match
  2599. 1 date plus weekday, for backreferencing to make sure both times on same day
  2600. 2 the first time, range or not
  2601. 4 the second time, if it is a range.")
  2602. (defconst org-startup-options
  2603. '(("fold" org-startup-folded t)
  2604. ("overview" org-startup-folded t)
  2605. ("nofold" org-startup-folded nil)
  2606. ("showall" org-startup-folded nil)
  2607. ("content" org-startup-folded content)
  2608. ("hidestars" org-hide-leading-stars t)
  2609. ("showstars" org-hide-leading-stars nil)
  2610. ("odd" org-odd-levels-only t)
  2611. ("oddeven" org-odd-levels-only nil)
  2612. ("align" org-startup-align-all-tables t)
  2613. ("noalign" org-startup-align-all-tables nil)
  2614. ("customtime" org-display-custom-times t)
  2615. ("logdone" org-log-done time)
  2616. ("lognotedone" org-log-done note)
  2617. ("nologdone" org-log-done nil)
  2618. ("lognoteclock-out" org-log-note-clock-out t)
  2619. ("nolognoteclock-out" org-log-note-clock-out nil)
  2620. ("logrepeat" org-log-repeat state)
  2621. ("lognoterepeat" org-log-repeat note)
  2622. ("nologrepeat" org-log-repeat nil)
  2623. ("constcgs" constants-unit-system cgs)
  2624. ("constSI" constants-unit-system SI))
  2625. "Variable associated with STARTUP options for org-mode.
  2626. Each element is a list of three items: The startup options as written
  2627. in the #+STARTUP line, the corresponding variable, and the value to
  2628. set this variable to if the option is found. An optional forth element PUSH
  2629. means to push this value onto the list in the variable.")
  2630. (defun org-set-regexps-and-options ()
  2631. "Precompute regular expressions for current buffer."
  2632. (when (org-mode-p)
  2633. (org-set-local 'org-todo-kwd-alist nil)
  2634. (org-set-local 'org-todo-key-alist nil)
  2635. (org-set-local 'org-todo-key-trigger nil)
  2636. (org-set-local 'org-todo-keywords-1 nil)
  2637. (org-set-local 'org-done-keywords nil)
  2638. (org-set-local 'org-todo-heads nil)
  2639. (org-set-local 'org-todo-sets nil)
  2640. (org-set-local 'org-todo-log-states nil)
  2641. (org-set-local 'org-file-properties nil)
  2642. (org-set-local 'org-file-tags nil)
  2643. (let ((re (org-make-options-regexp
  2644. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2645. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2646. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2647. (splitre "[ \t]+")
  2648. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2649. tail sep kws1 prio props ftags drawers
  2650. ext-setup-or-nil setup-contents (start 0))
  2651. (save-excursion
  2652. (save-restriction
  2653. (widen)
  2654. (goto-char (point-min))
  2655. (while (or (and ext-setup-or-nil
  2656. (string-match re ext-setup-or-nil start)
  2657. (setq start (match-end 0)))
  2658. (and (setq ext-setup-or-nil nil start 0)
  2659. (re-search-forward re nil t)))
  2660. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2661. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2662. (cond
  2663. ((equal key "CATEGORY")
  2664. (if (string-match "[ \t]+$" value)
  2665. (setq value (replace-match "" t t value)))
  2666. (setq cat value))
  2667. ((member key '("SEQ_TODO" "TODO"))
  2668. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2669. ((equal key "TYP_TODO")
  2670. (push (cons 'type (org-split-string value splitre)) kwds))
  2671. ((equal key "TAGS")
  2672. (setq tags (append tags (org-split-string value splitre))))
  2673. ((equal key "COLUMNS")
  2674. (org-set-local 'org-columns-default-format value))
  2675. ((equal key "LINK")
  2676. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2677. (push (cons (match-string 1 value)
  2678. (org-trim (match-string 2 value)))
  2679. links)))
  2680. ((equal key "PRIORITIES")
  2681. (setq prio (org-split-string value " +")))
  2682. ((equal key "PROPERTY")
  2683. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2684. (push (cons (match-string 1 value) (match-string 2 value))
  2685. props)))
  2686. ((equal key "FILETAGS")
  2687. (when (string-match "\\S-" value)
  2688. (setq ftags
  2689. (append
  2690. ftags
  2691. (apply 'append
  2692. (mapcar (lambda (x) (org-split-string x ":"))
  2693. (org-split-string value)))))))
  2694. ((equal key "DRAWERS")
  2695. (setq drawers (org-split-string value splitre)))
  2696. ((equal key "CONSTANTS")
  2697. (setq const (append const (org-split-string value splitre))))
  2698. ((equal key "STARTUP")
  2699. (let ((opts (org-split-string value splitre))
  2700. l var val)
  2701. (while (setq l (pop opts))
  2702. (when (setq l (assoc l org-startup-options))
  2703. (setq var (nth 1 l) val (nth 2 l))
  2704. (if (not (nth 3 l))
  2705. (set (make-local-variable var) val)
  2706. (if (not (listp (symbol-value var)))
  2707. (set (make-local-variable var) nil))
  2708. (set (make-local-variable var) (symbol-value var))
  2709. (add-to-list var val))))))
  2710. ((equal key "ARCHIVE")
  2711. (string-match " *$" value)
  2712. (setq arch (replace-match "" t t value))
  2713. (remove-text-properties 0 (length arch)
  2714. '(face t fontified t) arch))
  2715. ((equal key "SETUPFILE")
  2716. (setq setup-contents (org-file-contents
  2717. (expand-file-name
  2718. (org-remove-double-quotes value))
  2719. 'noerror))
  2720. (if (not ext-setup-or-nil)
  2721. (setq ext-setup-or-nil setup-contents start 0)
  2722. (setq ext-setup-or-nil
  2723. (concat (substring ext-setup-or-nil 0 start)
  2724. "\n" setup-contents "\n"
  2725. (substring ext-setup-or-nil start)))))
  2726. ))))
  2727. (when cat
  2728. (org-set-local 'org-category (intern cat))
  2729. (push (cons "CATEGORY" cat) props))
  2730. (when prio
  2731. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2732. (setq prio (mapcar 'string-to-char prio))
  2733. (org-set-local 'org-highest-priority (nth 0 prio))
  2734. (org-set-local 'org-lowest-priority (nth 1 prio))
  2735. (org-set-local 'org-default-priority (nth 2 prio)))
  2736. (and props (org-set-local 'org-file-properties (nreverse props)))
  2737. (and ftags (org-set-local 'org-file-tags ftags))
  2738. (and drawers (org-set-local 'org-drawers drawers))
  2739. (and arch (org-set-local 'org-archive-location arch))
  2740. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2741. ;; Process the TODO keywords
  2742. (unless kwds
  2743. ;; Use the global values as if they had been given locally.
  2744. (setq kwds (default-value 'org-todo-keywords))
  2745. (if (stringp (car kwds))
  2746. (setq kwds (list (cons org-todo-interpretation
  2747. (default-value 'org-todo-keywords)))))
  2748. (setq kwds (reverse kwds)))
  2749. (setq kwds (nreverse kwds))
  2750. (let (inter kws kw)
  2751. (while (setq kws (pop kwds))
  2752. (setq inter (pop kws) sep (member "|" kws)
  2753. kws0 (delete "|" (copy-sequence kws))
  2754. kwsa nil
  2755. kws1 (mapcar
  2756. (lambda (x)
  2757. ;; 1 2
  2758. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2759. (progn
  2760. (setq kw (match-string 1 x)
  2761. key (and (match-end 2) (match-string 2 x))
  2762. log (org-extract-log-state-settings x))
  2763. (push (cons kw (and key (string-to-char key))) kwsa)
  2764. (and log (push log org-todo-log-states))
  2765. kw)
  2766. (error "Invalid TODO keyword %s" x)))
  2767. kws0)
  2768. kwsa (if kwsa (append '((:startgroup))
  2769. (nreverse kwsa)
  2770. '((:endgroup))))
  2771. hw (car kws1)
  2772. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2773. tail (list inter hw (car dws) (org-last dws)))
  2774. (add-to-list 'org-todo-heads hw 'append)
  2775. (push kws1 org-todo-sets)
  2776. (setq org-done-keywords (append org-done-keywords dws nil))
  2777. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2778. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2779. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2780. (setq org-todo-sets (nreverse org-todo-sets)
  2781. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2782. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2783. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2784. ;; Process the constants
  2785. (when const
  2786. (let (e cst)
  2787. (while (setq e (pop const))
  2788. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2789. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2790. (setq org-table-formula-constants-local cst)))
  2791. ;; Process the tags.
  2792. (when tags
  2793. (let (e tgs)
  2794. (while (setq e (pop tags))
  2795. (cond
  2796. ((equal e "{") (push '(:startgroup) tgs))
  2797. ((equal e "}") (push '(:endgroup) tgs))
  2798. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2799. (push (cons (match-string 1 e)
  2800. (string-to-char (match-string 2 e)))
  2801. tgs))
  2802. (t (push (list e) tgs))))
  2803. (org-set-local 'org-tag-alist nil)
  2804. (while (setq e (pop tgs))
  2805. (or (and (stringp (car e))
  2806. (assoc (car e) org-tag-alist))
  2807. (push e org-tag-alist)))))
  2808. ;; Compute the regular expressions and other local variables
  2809. (if (not org-done-keywords)
  2810. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2811. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2812. (length org-scheduled-string)
  2813. (length org-clock-string)
  2814. (length org-closed-string)))
  2815. org-drawer-regexp
  2816. (concat "^[ \t]*:\\("
  2817. (mapconcat 'regexp-quote org-drawers "\\|")
  2818. "\\):[ \t]*$")
  2819. org-not-done-keywords
  2820. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2821. org-todo-regexp
  2822. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2823. "\\|") "\\)\\>")
  2824. org-not-done-regexp
  2825. (concat "\\<\\("
  2826. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2827. "\\)\\>")
  2828. org-todo-line-regexp
  2829. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2830. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2831. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2832. org-complex-heading-regexp
  2833. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2834. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2835. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2836. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2837. org-nl-done-regexp
  2838. (concat "\n\\*+[ \t]+"
  2839. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2840. "\\)" "\\>")
  2841. org-todo-line-tags-regexp
  2842. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2843. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2844. (org-re
  2845. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2846. org-looking-at-done-regexp
  2847. (concat "^" "\\(?:"
  2848. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2849. "\\>")
  2850. org-deadline-regexp (concat "\\<" org-deadline-string)
  2851. org-deadline-time-regexp
  2852. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2853. org-deadline-line-regexp
  2854. (concat "\\<\\(" org-deadline-string "\\).*")
  2855. org-scheduled-regexp
  2856. (concat "\\<" org-scheduled-string)
  2857. org-scheduled-time-regexp
  2858. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2859. org-closed-time-regexp
  2860. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2861. org-keyword-time-regexp
  2862. (concat "\\<\\(" org-scheduled-string
  2863. "\\|" org-deadline-string
  2864. "\\|" org-closed-string
  2865. "\\|" org-clock-string "\\)"
  2866. " *[[<]\\([^]>]+\\)[]>]")
  2867. org-keyword-time-not-clock-regexp
  2868. (concat "\\<\\(" org-scheduled-string
  2869. "\\|" org-deadline-string
  2870. "\\|" org-closed-string
  2871. "\\)"
  2872. " *[[<]\\([^]>]+\\)[]>]")
  2873. org-maybe-keyword-time-regexp
  2874. (concat "\\(\\<\\(" org-scheduled-string
  2875. "\\|" org-deadline-string
  2876. "\\|" org-closed-string
  2877. "\\|" org-clock-string "\\)\\)?"
  2878. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2879. org-planning-or-clock-line-re
  2880. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2881. "\\|" org-deadline-string
  2882. "\\|" org-closed-string "\\|" org-clock-string
  2883. "\\)\\>\\)")
  2884. )
  2885. (org-compute-latex-and-specials-regexp)
  2886. (org-set-font-lock-defaults))))
  2887. (defun org-file-contents (file &optional noerror)
  2888. "Return the contents of FILE, as a string."
  2889. (if (or (not file)
  2890. (not (file-readable-p file)))
  2891. (if noerror
  2892. (progn
  2893. (message "Cannot read file %s" file)
  2894. (ding) (sit-for 2)
  2895. "")
  2896. (error "Cannot read file %s" file))
  2897. (with-temp-buffer
  2898. (insert-file-contents file)
  2899. (buffer-string))))
  2900. (defun org-extract-log-state-settings (x)
  2901. "Extract the log state setting from a TODO keyword string.
  2902. This will extract info from a string like \"WAIT(w@/!)\"."
  2903. (let (kw key log1 log2)
  2904. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2905. (setq kw (match-string 1 x)
  2906. key (and (match-end 2) (match-string 2 x))
  2907. log1 (and (match-end 3) (match-string 3 x))
  2908. log2 (and (match-end 4) (match-string 4 x)))
  2909. (and (or log1 log2)
  2910. (list kw
  2911. (and log1 (if (equal log1 "!") 'time 'note))
  2912. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2913. (defun org-remove-keyword-keys (list)
  2914. "Remove a pair of parenthesis at the end of each string in LIST."
  2915. (mapcar (lambda (x)
  2916. (if (string-match "(.*)$" x)
  2917. (substring x 0 (match-beginning 0))
  2918. x))
  2919. list))
  2920. ;; FIXME: this could be done much better, using second characters etc.
  2921. (defun org-assign-fast-keys (alist)
  2922. "Assign fast keys to a keyword-key alist.
  2923. Respect keys that are already there."
  2924. (let (new e k c c1 c2 (char ?a))
  2925. (while (setq e (pop alist))
  2926. (cond
  2927. ((equal e '(:startgroup)) (push e new))
  2928. ((equal e '(:endgroup)) (push e new))
  2929. (t
  2930. (setq k (car e) c2 nil)
  2931. (if (cdr e)
  2932. (setq c (cdr e))
  2933. ;; automatically assign a character.
  2934. (setq c1 (string-to-char
  2935. (downcase (substring
  2936. k (if (= (string-to-char k) ?@) 1 0)))))
  2937. (if (or (rassoc c1 new) (rassoc c1 alist))
  2938. (while (or (rassoc char new) (rassoc char alist))
  2939. (setq char (1+ char)))
  2940. (setq c2 c1))
  2941. (setq c (or c2 char)))
  2942. (push (cons k c) new))))
  2943. (nreverse new)))
  2944. ;;; Some variables used in various places
  2945. (defvar org-window-configuration nil
  2946. "Used in various places to store a window configuration.")
  2947. (defvar org-finish-function nil
  2948. "Function to be called when `C-c C-c' is used.
  2949. This is for getting out of special buffers like remember.")
  2950. ;; FIXME: Occasionally check by commenting these, to make sure
  2951. ;; no other functions uses these, forgetting to let-bind them.
  2952. (defvar entry)
  2953. (defvar state)
  2954. (defvar last-state)
  2955. (defvar date)
  2956. (defvar description)
  2957. ;; Defined somewhere in this file, but used before definition.
  2958. (defvar org-html-entities)
  2959. (defvar org-struct-menu)
  2960. (defvar org-org-menu)
  2961. (defvar org-tbl-menu)
  2962. (defvar org-agenda-keymap)
  2963. ;;;; Define the Org-mode
  2964. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2965. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  2966. ;; We use a before-change function to check if a table might need
  2967. ;; an update.
  2968. (defvar org-table-may-need-update t
  2969. "Indicates that a table might need an update.
  2970. This variable is set by `org-before-change-function'.
  2971. `org-table-align' sets it back to nil.")
  2972. (defun org-before-change-function (beg end)
  2973. "Every change indicates that a table might need an update."
  2974. (setq org-table-may-need-update t))
  2975. (defvar org-mode-map)
  2976. (defvar org-mode-hook nil)
  2977. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2978. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2979. (defvar org-table-buffer-is-an nil)
  2980. (defconst org-outline-regexp "\\*+ ")
  2981. ;;;###autoload
  2982. (define-derived-mode org-mode outline-mode "Org"
  2983. "Outline-based notes management and organizer, alias
  2984. \"Carsten's outline-mode for keeping track of everything.\"
  2985. Org-mode develops organizational tasks around a NOTES file which
  2986. contains information about projects as plain text. Org-mode is
  2987. implemented on top of outline-mode, which is ideal to keep the content
  2988. of large files well structured. It supports ToDo items, deadlines and
  2989. time stamps, which magically appear in the diary listing of the Emacs
  2990. calendar. Tables are easily created with a built-in table editor.
  2991. Plain text URL-like links connect to websites, emails (VM), Usenet
  2992. messages (Gnus), BBDB entries, and any files related to the project.
  2993. For printing and sharing of notes, an Org-mode file (or a part of it)
  2994. can be exported as a structured ASCII or HTML file.
  2995. The following commands are available:
  2996. \\{org-mode-map}"
  2997. ;; Get rid of Outline menus, they are not needed
  2998. ;; Need to do this here because define-derived-mode sets up
  2999. ;; the keymap so late. Still, it is a waste to call this each time
  3000. ;; we switch another buffer into org-mode.
  3001. (if (featurep 'xemacs)
  3002. (when (boundp 'outline-mode-menu-heading)
  3003. ;; Assume this is Greg's port, it used easymenu
  3004. (easy-menu-remove outline-mode-menu-heading)
  3005. (easy-menu-remove outline-mode-menu-show)
  3006. (easy-menu-remove outline-mode-menu-hide))
  3007. (define-key org-mode-map [menu-bar headings] 'undefined)
  3008. (define-key org-mode-map [menu-bar hide] 'undefined)
  3009. (define-key org-mode-map [menu-bar show] 'undefined))
  3010. (org-load-modules-maybe)
  3011. (easy-menu-add org-org-menu)
  3012. (easy-menu-add org-tbl-menu)
  3013. (org-install-agenda-files-menu)
  3014. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3015. (org-add-to-invisibility-spec '(org-cwidth))
  3016. (when (featurep 'xemacs)
  3017. (org-set-local 'line-move-ignore-invisible t))
  3018. (org-set-local 'outline-regexp org-outline-regexp)
  3019. (org-set-local 'outline-level 'org-outline-level)
  3020. (when (and org-ellipsis
  3021. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3022. (fboundp 'make-glyph-code))
  3023. (unless org-display-table
  3024. (setq org-display-table (make-display-table)))
  3025. (set-display-table-slot
  3026. org-display-table 4
  3027. (vconcat (mapcar
  3028. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3029. org-ellipsis)))
  3030. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3031. (setq buffer-display-table org-display-table))
  3032. (org-set-regexps-and-options)
  3033. (when (and org-tag-faces (not org-tags-special-faces-re))
  3034. ;; tag faces set outside customize.... force initialization.
  3035. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3036. ;; Calc embedded
  3037. (org-set-local 'calc-embedded-open-mode "# ")
  3038. (modify-syntax-entry ?# "<")
  3039. (modify-syntax-entry ?@ "w")
  3040. (if org-startup-truncated (setq truncate-lines t))
  3041. (org-set-local 'font-lock-unfontify-region-function
  3042. 'org-unfontify-region)
  3043. ;; Activate before-change-function
  3044. (org-set-local 'org-table-may-need-update t)
  3045. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3046. 'local)
  3047. ;; Check for running clock before killing a buffer
  3048. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3049. ;; Paragraphs and auto-filling
  3050. (org-set-autofill-regexps)
  3051. (setq indent-line-function 'org-indent-line-function)
  3052. (org-update-radio-target-regexp)
  3053. ;; Comment characters
  3054. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3055. (org-set-local 'comment-padding " ")
  3056. ;; Align options lines
  3057. (org-set-local
  3058. 'align-mode-rules-list
  3059. '((org-in-buffer-settings
  3060. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3061. (modes . '(org-mode)))))
  3062. ;; Imenu
  3063. (org-set-local 'imenu-create-index-function
  3064. 'org-imenu-get-tree)
  3065. ;; Make isearch reveal context
  3066. (if (or (featurep 'xemacs)
  3067. (not (boundp 'outline-isearch-open-invisible-function)))
  3068. ;; Emacs 21 and XEmacs make use of the hook
  3069. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3070. ;; Emacs 22 deals with this through a special variable
  3071. (org-set-local 'outline-isearch-open-invisible-function
  3072. (lambda (&rest ignore) (org-show-context 'isearch))))
  3073. ;; If empty file that did not turn on org-mode automatically, make it to.
  3074. (if (and org-insert-mode-line-in-empty-file
  3075. (interactive-p)
  3076. (= (point-min) (point-max)))
  3077. (insert "# -*- mode: org -*-\n\n"))
  3078. (unless org-inhibit-startup
  3079. (when org-startup-align-all-tables
  3080. (let ((bmp (buffer-modified-p)))
  3081. (org-table-map-tables 'org-table-align)
  3082. (set-buffer-modified-p bmp)))
  3083. (org-set-startup-visibility)))
  3084. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3085. (defun org-current-time ()
  3086. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3087. (if (> (car org-time-stamp-rounding-minutes) 1)
  3088. (let ((r (car org-time-stamp-rounding-minutes))
  3089. (time (decode-time)))
  3090. (apply 'encode-time
  3091. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3092. (nthcdr 2 time))))
  3093. (current-time)))
  3094. ;;;; Font-Lock stuff, including the activators
  3095. (defvar org-mouse-map (make-sparse-keymap))
  3096. (org-defkey org-mouse-map
  3097. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3098. (org-defkey org-mouse-map
  3099. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3100. (when org-mouse-1-follows-link
  3101. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3102. (when org-tab-follows-link
  3103. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3104. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3105. (when org-return-follows-link
  3106. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3107. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3108. (require 'font-lock)
  3109. (defconst org-non-link-chars "]\t\n\r<>")
  3110. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3111. "shell" "elisp"))
  3112. (defvar org-link-types-re nil
  3113. "Matches a link that has a url-like prefix like \"http:\"")
  3114. (defvar org-link-re-with-space nil
  3115. "Matches a link with spaces, optional angular brackets around it.")
  3116. (defvar org-link-re-with-space2 nil
  3117. "Matches a link with spaces, optional angular brackets around it.")
  3118. (defvar org-link-re-with-space3 nil
  3119. "Matches a link with spaces, only for internal part in bracket links.")
  3120. (defvar org-angle-link-re nil
  3121. "Matches link with angular brackets, spaces are allowed.")
  3122. (defvar org-plain-link-re nil
  3123. "Matches plain link, without spaces.")
  3124. (defvar org-bracket-link-regexp nil
  3125. "Matches a link in double brackets.")
  3126. (defvar org-bracket-link-analytic-regexp nil
  3127. "Regular expression used to analyze links.
  3128. Here is what the match groups contain after a match:
  3129. 1: http:
  3130. 2: http
  3131. 3: path
  3132. 4: [desc]
  3133. 5: desc")
  3134. (defvar org-bracket-link-analytic-regexp++ nil
  3135. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3136. (defvar org-any-link-re nil
  3137. "Regular expression matching any link.")
  3138. (defun org-make-link-regexps ()
  3139. "Update the link regular expressions.
  3140. This should be called after the variable `org-link-types' has changed."
  3141. (setq org-link-types-re
  3142. (concat
  3143. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3144. org-link-re-with-space
  3145. (concat
  3146. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3147. "\\([^" org-non-link-chars " ]"
  3148. "[^" org-non-link-chars "]*"
  3149. "[^" org-non-link-chars " ]\\)>?")
  3150. org-link-re-with-space2
  3151. (concat
  3152. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3153. "\\([^" org-non-link-chars " ]"
  3154. "[^\t\n\r]*"
  3155. "[^" org-non-link-chars " ]\\)>?")
  3156. org-link-re-with-space3
  3157. (concat
  3158. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3159. "\\([^" org-non-link-chars " ]"
  3160. "[^\t\n\r]*\\)")
  3161. org-angle-link-re
  3162. (concat
  3163. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3164. "\\([^" org-non-link-chars " ]"
  3165. "[^" org-non-link-chars "]*"
  3166. "\\)>")
  3167. org-plain-link-re
  3168. (concat
  3169. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3170. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3171. org-bracket-link-regexp
  3172. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3173. org-bracket-link-analytic-regexp
  3174. (concat
  3175. "\\[\\["
  3176. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3177. "\\([^]]+\\)"
  3178. "\\]"
  3179. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3180. "\\]")
  3181. org-bracket-link-analytic-regexp++
  3182. (concat
  3183. "\\[\\["
  3184. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3185. "\\([^]]+\\)"
  3186. "\\]"
  3187. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3188. "\\]")
  3189. org-any-link-re
  3190. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3191. org-angle-link-re "\\)\\|\\("
  3192. org-plain-link-re "\\)")))
  3193. (org-make-link-regexps)
  3194. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3195. "Regular expression for fast time stamp matching.")
  3196. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3197. "Regular expression for fast time stamp matching.")
  3198. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3199. "Regular expression matching time strings for analysis.
  3200. This one does not require the space after the date, so it can be used
  3201. on a string that terminates immediately after the date.")
  3202. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3203. "Regular expression matching time strings for analysis.")
  3204. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3205. "Regular expression matching time stamps, with groups.")
  3206. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3207. "Regular expression matching time stamps (also [..]), with groups.")
  3208. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3209. "Regular expression matching a time stamp range.")
  3210. (defconst org-tr-regexp-both
  3211. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3212. "Regular expression matching a time stamp range.")
  3213. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3214. org-ts-regexp "\\)?")
  3215. "Regular expression matching a time stamp or time stamp range.")
  3216. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3217. org-ts-regexp-both "\\)?")
  3218. "Regular expression matching a time stamp or time stamp range.
  3219. The time stamps may be either active or inactive.")
  3220. (defvar org-emph-face nil)
  3221. (defun org-do-emphasis-faces (limit)
  3222. "Run through the buffer and add overlays to links."
  3223. (let (rtn)
  3224. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3225. (if (not (= (char-after (match-beginning 3))
  3226. (char-after (match-beginning 4))))
  3227. (progn
  3228. (setq rtn t)
  3229. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3230. 'face
  3231. (nth 1 (assoc (match-string 3)
  3232. org-emphasis-alist)))
  3233. (add-text-properties (match-beginning 2) (match-end 2)
  3234. '(font-lock-multiline t))
  3235. (when org-hide-emphasis-markers
  3236. (add-text-properties (match-end 4) (match-beginning 5)
  3237. '(invisible org-link))
  3238. (add-text-properties (match-beginning 3) (match-end 3)
  3239. '(invisible org-link)))))
  3240. (backward-char 1))
  3241. rtn))
  3242. (defun org-emphasize (&optional char)
  3243. "Insert or change an emphasis, i.e. a font like bold or italic.
  3244. If there is an active region, change that region to a new emphasis.
  3245. If there is no region, just insert the marker characters and position
  3246. the cursor between them.
  3247. CHAR should be either the marker character, or the first character of the
  3248. HTML tag associated with that emphasis. If CHAR is a space, the means
  3249. to remove the emphasis of the selected region.
  3250. If char is not given (for example in an interactive call) it
  3251. will be prompted for."
  3252. (interactive)
  3253. (let ((eal org-emphasis-alist) e det
  3254. (erc org-emphasis-regexp-components)
  3255. (prompt "")
  3256. (string "") beg end move tag c s)
  3257. (if (org-region-active-p)
  3258. (setq beg (region-beginning) end (region-end)
  3259. string (buffer-substring beg end))
  3260. (setq move t))
  3261. (while (setq e (pop eal))
  3262. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3263. c (aref tag 0))
  3264. (push (cons c (string-to-char (car e))) det)
  3265. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3266. (substring tag 1)))))
  3267. (setq det (nreverse det))
  3268. (unless char
  3269. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3270. (setq char (read-char-exclusive)))
  3271. (setq char (or (cdr (assoc char det)) char))
  3272. (if (equal char ?\ )
  3273. (setq s "" move nil)
  3274. (unless (assoc (char-to-string char) org-emphasis-alist)
  3275. (error "No such emphasis marker: \"%c\"" char))
  3276. (setq s (char-to-string char)))
  3277. (while (and (> (length string) 1)
  3278. (equal (substring string 0 1) (substring string -1))
  3279. (assoc (substring string 0 1) org-emphasis-alist))
  3280. (setq string (substring string 1 -1)))
  3281. (setq string (concat s string s))
  3282. (if beg (delete-region beg end))
  3283. (unless (or (bolp)
  3284. (string-match (concat "[" (nth 0 erc) "\n]")
  3285. (char-to-string (char-before (point)))))
  3286. (insert " "))
  3287. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3288. (char-to-string (char-after (point))))
  3289. (insert " ") (backward-char 1))
  3290. (insert string)
  3291. (and move (backward-char 1))))
  3292. (defconst org-nonsticky-props
  3293. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3294. (defun org-activate-plain-links (limit)
  3295. "Run through the buffer and add overlays to links."
  3296. (catch 'exit
  3297. (let (f)
  3298. (while (re-search-forward org-plain-link-re limit t)
  3299. (setq f (get-text-property (match-beginning 0) 'face))
  3300. (if (or (eq f 'org-tag)
  3301. (and (listp f) (memq 'org-tag f)))
  3302. nil
  3303. (add-text-properties (match-beginning 0) (match-end 0)
  3304. (list 'mouse-face 'highlight
  3305. 'rear-nonsticky org-nonsticky-props
  3306. 'keymap org-mouse-map
  3307. ))
  3308. (throw 'exit t))))))
  3309. (defun org-activate-code (limit)
  3310. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3311. (progn
  3312. (remove-text-properties (match-beginning 0) (match-end 0)
  3313. '(display t invisible t intangible t))
  3314. t)))
  3315. (defun org-activate-angle-links (limit)
  3316. "Run through the buffer and add overlays to links."
  3317. (if (re-search-forward org-angle-link-re limit t)
  3318. (progn
  3319. (add-text-properties (match-beginning 0) (match-end 0)
  3320. (list 'mouse-face 'highlight
  3321. 'rear-nonsticky org-nonsticky-props
  3322. 'keymap org-mouse-map
  3323. ))
  3324. t)))
  3325. (defun org-activate-bracket-links (limit)
  3326. "Run through the buffer and add overlays to bracketed links."
  3327. (if (re-search-forward org-bracket-link-regexp limit t)
  3328. (let* ((help (concat "LINK: "
  3329. (org-match-string-no-properties 1)))
  3330. ;; FIXME: above we should remove the escapes.
  3331. ;; but that requires another match, protecting match data,
  3332. ;; a lot of overhead for font-lock.
  3333. (ip (org-maybe-intangible
  3334. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3335. 'keymap org-mouse-map 'mouse-face 'highlight
  3336. 'font-lock-multiline t 'help-echo help)))
  3337. (vp (list 'rear-nonsticky org-nonsticky-props
  3338. 'keymap org-mouse-map 'mouse-face 'highlight
  3339. ' font-lock-multiline t 'help-echo help)))
  3340. ;; We need to remove the invisible property here. Table narrowing
  3341. ;; may have made some of this invisible.
  3342. (remove-text-properties (match-beginning 0) (match-end 0)
  3343. '(invisible nil))
  3344. (if (match-end 3)
  3345. (progn
  3346. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3347. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3348. (add-text-properties (match-end 3) (match-end 0) ip))
  3349. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3350. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3351. (add-text-properties (match-end 1) (match-end 0) ip))
  3352. t)))
  3353. (defun org-activate-dates (limit)
  3354. "Run through the buffer and add overlays to dates."
  3355. (if (re-search-forward org-tsr-regexp-both limit t)
  3356. (progn
  3357. (add-text-properties (match-beginning 0) (match-end 0)
  3358. (list 'mouse-face 'highlight
  3359. 'rear-nonsticky org-nonsticky-props
  3360. 'keymap org-mouse-map))
  3361. (when org-display-custom-times
  3362. (if (match-end 3)
  3363. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3364. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3365. t)))
  3366. (defvar org-target-link-regexp nil
  3367. "Regular expression matching radio targets in plain text.")
  3368. (make-variable-buffer-local 'org-target-link-regexp)
  3369. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3370. "Regular expression matching a link target.")
  3371. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3372. "Regular expression matching a radio target.")
  3373. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3374. "Regular expression matching any target.")
  3375. (defun org-activate-target-links (limit)
  3376. "Run through the buffer and add overlays to target matches."
  3377. (when org-target-link-regexp
  3378. (let ((case-fold-search t))
  3379. (if (re-search-forward org-target-link-regexp limit t)
  3380. (progn
  3381. (add-text-properties (match-beginning 0) (match-end 0)
  3382. (list 'mouse-face 'highlight
  3383. 'rear-nonsticky org-nonsticky-props
  3384. 'keymap org-mouse-map
  3385. 'help-echo "Radio target link"
  3386. 'org-linked-text t))
  3387. t)))))
  3388. (defun org-update-radio-target-regexp ()
  3389. "Find all radio targets in this file and update the regular expression."
  3390. (interactive)
  3391. (when (memq 'radio org-activate-links)
  3392. (setq org-target-link-regexp
  3393. (org-make-target-link-regexp (org-all-targets 'radio)))
  3394. (org-restart-font-lock)))
  3395. (defun org-hide-wide-columns (limit)
  3396. (let (s e)
  3397. (setq s (text-property-any (point) (or limit (point-max))
  3398. 'org-cwidth t))
  3399. (when s
  3400. (setq e (next-single-property-change s 'org-cwidth))
  3401. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3402. (goto-char e)
  3403. t)))
  3404. (defvar org-latex-and-specials-regexp nil
  3405. "Regular expression for highlighting export special stuff.")
  3406. (defvar org-match-substring-regexp)
  3407. (defvar org-match-substring-with-braces-regexp)
  3408. (defvar org-export-html-special-string-regexps)
  3409. (defun org-compute-latex-and-specials-regexp ()
  3410. "Compute regular expression for stuff treated specially by exporters."
  3411. (if (not org-highlight-latex-fragments-and-specials)
  3412. (org-set-local 'org-latex-and-specials-regexp nil)
  3413. (require 'org-exp)
  3414. (let*
  3415. ((matchers (plist-get org-format-latex-options :matchers))
  3416. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3417. org-latex-regexps)))
  3418. (options (org-combine-plists (org-default-export-plist)
  3419. (org-infile-export-plist)))
  3420. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3421. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3422. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3423. (org-export-html-expand (plist-get options :expand-quoted-html))
  3424. (org-export-with-special-strings (plist-get options :special-strings))
  3425. (re-sub
  3426. (cond
  3427. ((equal org-export-with-sub-superscripts '{})
  3428. (list org-match-substring-with-braces-regexp))
  3429. (org-export-with-sub-superscripts
  3430. (list org-match-substring-regexp))
  3431. (t nil)))
  3432. (re-latex
  3433. (if org-export-with-LaTeX-fragments
  3434. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3435. (re-macros
  3436. (if org-export-with-TeX-macros
  3437. (list (concat "\\\\"
  3438. (regexp-opt
  3439. (append (mapcar 'car org-html-entities)
  3440. (if (boundp 'org-latex-entities)
  3441. org-latex-entities nil))
  3442. 'words))) ; FIXME
  3443. ))
  3444. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3445. (re-special (if org-export-with-special-strings
  3446. (mapcar (lambda (x) (car x))
  3447. org-export-html-special-string-regexps)))
  3448. (re-rest
  3449. (delq nil
  3450. (list
  3451. (if org-export-html-expand "@<[^>\n]+>")
  3452. ))))
  3453. (org-set-local
  3454. 'org-latex-and-specials-regexp
  3455. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3456. re-rest) "\\|")))))
  3457. (defun org-do-latex-and-special-faces (limit)
  3458. "Run through the buffer and add overlays to links."
  3459. (when org-latex-and-specials-regexp
  3460. (let (rtn d)
  3461. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3462. limit t))
  3463. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3464. 'face))
  3465. '(org-code org-verbatim underline)))
  3466. (progn
  3467. (setq rtn t
  3468. d (cond ((member (char-after (1+ (match-beginning 0)))
  3469. '(?_ ?^)) 1)
  3470. (t 0)))
  3471. (font-lock-prepend-text-property
  3472. (+ d (match-beginning 0)) (match-end 0)
  3473. 'face 'org-latex-and-export-specials)
  3474. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3475. '(font-lock-multiline t)))))
  3476. rtn)))
  3477. (defun org-restart-font-lock ()
  3478. "Restart font-lock-mode, to force refontification."
  3479. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3480. (font-lock-mode -1)
  3481. (font-lock-mode 1)))
  3482. (defun org-all-targets (&optional radio)
  3483. "Return a list of all targets in this file.
  3484. With optional argument RADIO, only find radio targets."
  3485. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3486. rtn)
  3487. (save-excursion
  3488. (goto-char (point-min))
  3489. (while (re-search-forward re nil t)
  3490. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3491. rtn)))
  3492. (defun org-make-target-link-regexp (targets)
  3493. "Make regular expression matching all strings in TARGETS.
  3494. The regular expression finds the targets also if there is a line break
  3495. between words."
  3496. (and targets
  3497. (concat
  3498. "\\<\\("
  3499. (mapconcat
  3500. (lambda (x)
  3501. (while (string-match " +" x)
  3502. (setq x (replace-match "\\s-+" t t x)))
  3503. x)
  3504. targets
  3505. "\\|")
  3506. "\\)\\>")))
  3507. (defun org-activate-tags (limit)
  3508. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3509. (progn
  3510. (add-text-properties (match-beginning 1) (match-end 1)
  3511. (list 'mouse-face 'highlight
  3512. 'rear-nonsticky org-nonsticky-props
  3513. 'keymap org-mouse-map))
  3514. t)))
  3515. (defun org-outline-level ()
  3516. (save-excursion
  3517. (looking-at outline-regexp)
  3518. (if (match-beginning 1)
  3519. (+ (org-get-string-indentation (match-string 1)) 1000)
  3520. (1- (- (match-end 0) (match-beginning 0))))))
  3521. (defvar org-font-lock-keywords nil)
  3522. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3523. "Regular expression matching a property line.")
  3524. (defvar org-font-lock-hook nil
  3525. "Functions to be called for special font lock stuff.")
  3526. (defun org-font-lock-hook (limit)
  3527. (run-hook-with-args 'org-font-lock-hook limit))
  3528. (defun org-set-font-lock-defaults ()
  3529. (let* ((em org-fontify-emphasized-text)
  3530. (lk org-activate-links)
  3531. (org-font-lock-extra-keywords
  3532. (list
  3533. ;; Call the hook
  3534. '(org-font-lock-hook)
  3535. ;; Headlines
  3536. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3537. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3538. ;; Table lines
  3539. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3540. (1 'org-table t))
  3541. ;; Table internals
  3542. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3543. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3544. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3545. ;; Drawers
  3546. (list org-drawer-regexp '(0 'org-special-keyword t))
  3547. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3548. ;; Properties
  3549. (list org-property-re
  3550. '(1 'org-special-keyword t)
  3551. '(3 'org-property-value t))
  3552. (if org-format-transports-properties-p
  3553. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3554. ;; Links
  3555. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3556. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3557. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3558. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3559. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3560. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3561. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3562. '(org-hide-wide-columns (0 nil append))
  3563. ;; TODO lines
  3564. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3565. '(1 (org-get-todo-face 1) t))
  3566. ;; DONE
  3567. (if org-fontify-done-headline
  3568. (list (concat "^[*]+ +\\<\\("
  3569. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3570. "\\)\\(.*\\)")
  3571. '(2 'org-headline-done t))
  3572. nil)
  3573. ;; Priorities
  3574. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3575. ;; Tags
  3576. '(org-font-lock-add-tag-faces)
  3577. ;; Special keywords
  3578. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3579. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3580. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3581. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3582. ;; Emphasis
  3583. (if em
  3584. (if (featurep 'xemacs)
  3585. '(org-do-emphasis-faces (0 nil append))
  3586. '(org-do-emphasis-faces)))
  3587. ;; Checkboxes
  3588. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3589. 2 'bold prepend)
  3590. (if org-provide-checkbox-statistics
  3591. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3592. (0 (org-get-checkbox-statistics-face) t)))
  3593. ;; Description list items
  3594. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3595. 2 'bold prepend)
  3596. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3597. '(1 'org-archived prepend))
  3598. ;; Specials
  3599. '(org-do-latex-and-special-faces)
  3600. ;; Code
  3601. '(org-activate-code (1 'org-code t))
  3602. ;; COMMENT
  3603. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3604. "\\|" org-quote-string "\\)\\>")
  3605. '(1 'org-special-keyword t))
  3606. '("^#.*" (0 'font-lock-comment-face t))
  3607. )))
  3608. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3609. ;; Now set the full font-lock-keywords
  3610. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3611. (org-set-local 'font-lock-defaults
  3612. '(org-font-lock-keywords t nil nil backward-paragraph))
  3613. (kill-local-variable 'font-lock-keywords) nil))
  3614. (defvar org-m nil)
  3615. (defvar org-l nil)
  3616. (defvar org-f nil)
  3617. (defun org-get-level-face (n)
  3618. "Get the right face for match N in font-lock matching of headlines."
  3619. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3620. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3621. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3622. (cond
  3623. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3624. ((eq n 2) org-f)
  3625. (t (if org-level-color-stars-only nil org-f))))
  3626. (defun org-get-todo-face (kwd)
  3627. "Get the right face for a TODO keyword KWD.
  3628. If KWD is a number, get the corresponding match group."
  3629. (if (numberp kwd) (setq kwd (match-string kwd)))
  3630. (or (cdr (assoc kwd org-todo-keyword-faces))
  3631. (and (member kwd org-done-keywords) 'org-done)
  3632. 'org-todo))
  3633. (defun org-font-lock-add-tag-faces (limit)
  3634. "Add the special tag faces."
  3635. (when (and org-tag-faces org-tags-special-faces-re)
  3636. (while (re-search-forward org-tags-special-faces-re limit t)
  3637. (add-text-properties (match-beginning 1) (match-end 1)
  3638. (list 'face (org-get-tag-face 1)
  3639. 'font-lock-fontified t))
  3640. (backward-char 1))))
  3641. (defun org-get-tag-face (kwd)
  3642. "Get the right face for a TODO keyword KWD.
  3643. If KWD is a number, get the corresponding match group."
  3644. (if (numberp kwd) (setq kwd (match-string kwd)))
  3645. (or (cdr (assoc kwd org-tag-faces))
  3646. 'org-tag))
  3647. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3648. "Remove fontification and activation overlays from links."
  3649. (font-lock-default-unfontify-region beg end)
  3650. (let* ((buffer-undo-list t)
  3651. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3652. (inhibit-modification-hooks t)
  3653. deactivate-mark buffer-file-name buffer-file-truename)
  3654. (remove-text-properties beg end
  3655. '(mouse-face t keymap t org-linked-text t
  3656. invisible t intangible t))))
  3657. ;;;; Visibility cycling, including org-goto and indirect buffer
  3658. ;;; Cycling
  3659. (defvar org-cycle-global-status nil)
  3660. (make-variable-buffer-local 'org-cycle-global-status)
  3661. (defvar org-cycle-subtree-status nil)
  3662. (make-variable-buffer-local 'org-cycle-subtree-status)
  3663. ;;;###autoload
  3664. (defun org-cycle (&optional arg)
  3665. "Visibility cycling for Org-mode.
  3666. - When this function is called with a prefix argument, rotate the entire
  3667. buffer through 3 states (global cycling)
  3668. 1. OVERVIEW: Show only top-level headlines.
  3669. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3670. 3. SHOW ALL: Show everything.
  3671. When called with two C-u C-u prefixes, switch to the startup visibility,
  3672. determined by the variable `org-startup-folded', and by any VISIBILITY
  3673. properties in the buffer.
  3674. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3675. including drawers.
  3676. - When point is at the beginning of a headline, rotate the subtree started
  3677. by this line through 3 different states (local cycling)
  3678. 1. FOLDED: Only the main headline is shown.
  3679. 2. CHILDREN: The main headline and the direct children are shown.
  3680. From this state, you can move to one of the children
  3681. and zoom in further.
  3682. 3. SUBTREE: Show the entire subtree, including body text.
  3683. - When there is a numeric prefix, go up to a heading with level ARG, do
  3684. a `show-subtree' and return to the previous cursor position. If ARG
  3685. is negative, go up that many levels.
  3686. - When point is not at the beginning of a headline, execute the global
  3687. binding for TAB, which is re-indenting the line. See the option
  3688. `org-cycle-emulate-tab' for details.
  3689. - Special case: if point is at the beginning of the buffer and there is
  3690. no headline in line 1, this function will act as if called with prefix arg.
  3691. But only if also the variable `org-cycle-global-at-bob' is t."
  3692. (interactive "P")
  3693. (org-load-modules-maybe)
  3694. (let* ((outline-regexp
  3695. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3696. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3697. outline-regexp))
  3698. (bob-special (and org-cycle-global-at-bob (bobp)
  3699. (not (looking-at outline-regexp))))
  3700. (org-cycle-hook
  3701. (if bob-special
  3702. (delq 'org-optimize-window-after-visibility-change
  3703. (copy-sequence org-cycle-hook))
  3704. org-cycle-hook))
  3705. (pos (point)))
  3706. (if (or bob-special (equal arg '(4)))
  3707. ;; special case: use global cycling
  3708. (setq arg t))
  3709. (cond
  3710. ((equal arg '(16))
  3711. (org-set-startup-visibility)
  3712. (message "Startup visibility, plus VISIBILITY properties"))
  3713. ((equal arg '(64))
  3714. (show-all)
  3715. (message "Entire buffer visible, including drawers"))
  3716. ((org-at-table-p 'any)
  3717. ;; Enter the table or move to the next field in the table
  3718. (or (org-table-recognize-table.el)
  3719. (progn
  3720. (if arg (org-table-edit-field t)
  3721. (org-table-justify-field-maybe)
  3722. (call-interactively 'org-table-next-field)))))
  3723. ((eq arg t) ;; Global cycling
  3724. (cond
  3725. ((and (eq last-command this-command)
  3726. (eq org-cycle-global-status 'overview))
  3727. ;; We just created the overview - now do table of contents
  3728. ;; This can be slow in very large buffers, so indicate action
  3729. (message "CONTENTS...")
  3730. (org-content)
  3731. (message "CONTENTS...done")
  3732. (setq org-cycle-global-status 'contents)
  3733. (run-hook-with-args 'org-cycle-hook 'contents))
  3734. ((and (eq last-command this-command)
  3735. (eq org-cycle-global-status 'contents))
  3736. ;; We just showed the table of contents - now show everything
  3737. (show-all)
  3738. (message "SHOW ALL")
  3739. (setq org-cycle-global-status 'all)
  3740. (run-hook-with-args 'org-cycle-hook 'all))
  3741. (t
  3742. ;; Default action: go to overview
  3743. (org-overview)
  3744. (message "OVERVIEW")
  3745. (setq org-cycle-global-status 'overview)
  3746. (run-hook-with-args 'org-cycle-hook 'overview))))
  3747. ((and org-drawers org-drawer-regexp
  3748. (save-excursion
  3749. (beginning-of-line 1)
  3750. (looking-at org-drawer-regexp)))
  3751. ;; Toggle block visibility
  3752. (org-flag-drawer
  3753. (not (get-char-property (match-end 0) 'invisible))))
  3754. ((integerp arg)
  3755. ;; Show-subtree, ARG levels up from here.
  3756. (save-excursion
  3757. (org-back-to-heading)
  3758. (outline-up-heading (if (< arg 0) (- arg)
  3759. (- (funcall outline-level) arg)))
  3760. (org-show-subtree)))
  3761. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3762. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3763. ;; At a heading: rotate between three different views
  3764. (org-back-to-heading)
  3765. (let ((goal-column 0) eoh eol eos)
  3766. ;; First, some boundaries
  3767. (save-excursion
  3768. (org-back-to-heading)
  3769. (save-excursion
  3770. (beginning-of-line 2)
  3771. (while (and (not (eobp)) ;; this is like `next-line'
  3772. (get-char-property (1- (point)) 'invisible))
  3773. (beginning-of-line 2)) (setq eol (point)))
  3774. (outline-end-of-heading) (setq eoh (point))
  3775. (org-end-of-subtree t)
  3776. (unless (eobp)
  3777. (skip-chars-forward " \t\n")
  3778. (beginning-of-line 1) ; in case this is an item
  3779. )
  3780. (setq eos (1- (point))))
  3781. ;; Find out what to do next and set `this-command'
  3782. (cond
  3783. ((= eos eoh)
  3784. ;; Nothing is hidden behind this heading
  3785. (message "EMPTY ENTRY")
  3786. (setq org-cycle-subtree-status nil)
  3787. (save-excursion
  3788. (goto-char eos)
  3789. (outline-next-heading)
  3790. (if (org-invisible-p) (org-flag-heading nil))))
  3791. ((or (>= eol eos)
  3792. (not (string-match "\\S-" (buffer-substring eol eos))))
  3793. ;; Entire subtree is hidden in one line: open it
  3794. (org-show-entry)
  3795. (show-children)
  3796. (message "CHILDREN")
  3797. (save-excursion
  3798. (goto-char eos)
  3799. (outline-next-heading)
  3800. (if (org-invisible-p) (org-flag-heading nil)))
  3801. (setq org-cycle-subtree-status 'children)
  3802. (run-hook-with-args 'org-cycle-hook 'children))
  3803. ((and (eq last-command this-command)
  3804. (eq org-cycle-subtree-status 'children))
  3805. ;; We just showed the children, now show everything.
  3806. (org-show-subtree)
  3807. (message "SUBTREE")
  3808. (setq org-cycle-subtree-status 'subtree)
  3809. (run-hook-with-args 'org-cycle-hook 'subtree))
  3810. (t
  3811. ;; Default action: hide the subtree.
  3812. (hide-subtree)
  3813. (message "FOLDED")
  3814. (setq org-cycle-subtree-status 'folded)
  3815. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3816. ;; TAB emulation and template completion
  3817. (buffer-read-only (org-back-to-heading))
  3818. ((org-try-structure-completion))
  3819. ((org-try-cdlatex-tab))
  3820. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3821. (or (not (bolp))
  3822. (not (looking-at outline-regexp))))
  3823. (call-interactively (global-key-binding "\t")))
  3824. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3825. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3826. (or (and (eq org-cycle-emulate-tab 'white)
  3827. (= (match-end 0) (point-at-eol)))
  3828. (and (eq org-cycle-emulate-tab 'whitestart)
  3829. (>= (match-end 0) pos))))
  3830. t
  3831. (eq org-cycle-emulate-tab t))
  3832. (call-interactively (global-key-binding "\t")))
  3833. (t (save-excursion
  3834. (org-back-to-heading)
  3835. (org-cycle))))))
  3836. ;;;###autoload
  3837. (defun org-global-cycle (&optional arg)
  3838. "Cycle the global visibility. For details see `org-cycle'.
  3839. With C-u prefix arg, switch to startup visibility.
  3840. With a numeric prefix, show all headlines up to that level."
  3841. (interactive "P")
  3842. (let ((org-cycle-include-plain-lists
  3843. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3844. (cond
  3845. ((integerp arg)
  3846. (show-all)
  3847. (hide-sublevels arg)
  3848. (setq org-cycle-global-status 'contents))
  3849. ((equal arg '(4))
  3850. (org-set-startup-visibility)
  3851. (message "Startup visibility, plus VISIBILITY properties."))
  3852. (t
  3853. (org-cycle '(4))))))
  3854. (defun org-set-startup-visibility ()
  3855. "Set the visibility required by startup options and properties."
  3856. (cond
  3857. ((eq org-startup-folded t)
  3858. (org-cycle '(4)))
  3859. ((eq org-startup-folded 'content)
  3860. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3861. (org-cycle '(4)) (org-cycle '(4)))))
  3862. (org-set-visibility-according-to-property 'no-cleanup)
  3863. (org-cycle-hide-archived-subtrees 'all)
  3864. (org-cycle-hide-drawers 'all)
  3865. (org-cycle-show-empty-lines 'all))
  3866. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3867. "Switch subtree visibilities according to :VISIBILITY: property."
  3868. (interactive)
  3869. (let (state)
  3870. (save-excursion
  3871. (goto-char (point-min))
  3872. (while (re-search-forward
  3873. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3874. nil t)
  3875. (setq state (match-string 1))
  3876. (save-excursion
  3877. (org-back-to-heading t)
  3878. (hide-subtree)
  3879. (org-reveal)
  3880. (cond
  3881. ((equal state '("fold" "folded"))
  3882. (hide-subtree))
  3883. ((equal state "children")
  3884. (org-show-hidden-entry)
  3885. (show-children))
  3886. ((equal state "content")
  3887. (save-excursion
  3888. (save-restriction
  3889. (org-narrow-to-subtree)
  3890. (org-content))))
  3891. ((member state '("all" "showall"))
  3892. (show-subtree)))))
  3893. (unless no-cleanup
  3894. (org-cycle-hide-archived-subtrees 'all)
  3895. (org-cycle-hide-drawers 'all)
  3896. (org-cycle-show-empty-lines 'all)))))
  3897. (defun org-overview ()
  3898. "Switch to overview mode, showing only top-level headlines.
  3899. Really, this shows all headlines with level equal or greater than the level
  3900. of the first headline in the buffer. This is important, because if the
  3901. first headline is not level one, then (hide-sublevels 1) gives confusing
  3902. results."
  3903. (interactive)
  3904. (let ((level (save-excursion
  3905. (goto-char (point-min))
  3906. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3907. (progn
  3908. (goto-char (match-beginning 0))
  3909. (funcall outline-level))))))
  3910. (and level (hide-sublevels level))))
  3911. (defun org-content (&optional arg)
  3912. "Show all headlines in the buffer, like a table of contents.
  3913. With numerical argument N, show content up to level N."
  3914. (interactive "P")
  3915. (save-excursion
  3916. ;; Visit all headings and show their offspring
  3917. (and (integerp arg) (org-overview))
  3918. (goto-char (point-max))
  3919. (catch 'exit
  3920. (while (and (progn (condition-case nil
  3921. (outline-previous-visible-heading 1)
  3922. (error (goto-char (point-min))))
  3923. t)
  3924. (looking-at outline-regexp))
  3925. (if (integerp arg)
  3926. (show-children (1- arg))
  3927. (show-branches))
  3928. (if (bobp) (throw 'exit nil))))))
  3929. (defun org-optimize-window-after-visibility-change (state)
  3930. "Adjust the window after a change in outline visibility.
  3931. This function is the default value of the hook `org-cycle-hook'."
  3932. (when (get-buffer-window (current-buffer))
  3933. (cond
  3934. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3935. ; ((eq state 'overview) (org-beginning-of-line))
  3936. ((eq state 'content) nil)
  3937. ((eq state 'all) nil)
  3938. ((eq state 'folded) nil)
  3939. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3940. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3941. (defun org-compact-display-after-subtree-move ()
  3942. (let (beg end)
  3943. (save-excursion
  3944. (if (org-up-heading-safe)
  3945. (progn
  3946. (hide-subtree)
  3947. (show-entry)
  3948. (show-children)
  3949. (org-cycle-show-empty-lines 'children)
  3950. (org-cycle-hide-drawers 'children))
  3951. (org-overview)))))
  3952. (defun org-cycle-show-empty-lines (state)
  3953. "Show empty lines above all visible headlines.
  3954. The region to be covered depends on STATE when called through
  3955. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3956. entire buffer covered. Note that an empty line is only shown if there
  3957. are at least `org-cycle-separator-lines' empty lines before the headline."
  3958. (when (> org-cycle-separator-lines 0)
  3959. (save-excursion
  3960. (let* ((n org-cycle-separator-lines)
  3961. (re (cond
  3962. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3963. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3964. (t (let ((ns (number-to-string (- n 2))))
  3965. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3966. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3967. beg end)
  3968. (cond
  3969. ((memq state '(overview contents t))
  3970. (setq beg (point-min) end (point-max)))
  3971. ((memq state '(children folded))
  3972. (setq beg (point) end (progn (org-end-of-subtree t t)
  3973. (beginning-of-line 2)
  3974. (point)))))
  3975. (when beg
  3976. (goto-char beg)
  3977. (while (re-search-forward re end t)
  3978. (if (not (get-char-property (match-end 1) 'invisible))
  3979. (outline-flag-region
  3980. (match-beginning 1) (match-end 1) nil)))))))
  3981. ;; Never hide empty lines at the end of the file.
  3982. (save-excursion
  3983. (goto-char (point-max))
  3984. (outline-previous-heading)
  3985. (outline-end-of-heading)
  3986. (if (and (looking-at "[ \t\n]+")
  3987. (= (match-end 0) (point-max)))
  3988. (outline-flag-region (point) (match-end 0) nil))))
  3989. (defun org-show-empty-lines-in-parent ()
  3990. "Move to the parent and re-show empty lines before visible headlines."
  3991. (save-excursion
  3992. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3993. (org-cycle-show-empty-lines context))))
  3994. (defun org-cycle-hide-drawers (state)
  3995. "Re-hide all drawers after a visibility state change."
  3996. (when (and (org-mode-p)
  3997. (not (memq state '(overview folded))))
  3998. (save-excursion
  3999. (let* ((globalp (memq state '(contents all)))
  4000. (beg (if globalp (point-min) (point)))
  4001. (end (if globalp (point-max) (org-end-of-subtree t))))
  4002. (goto-char beg)
  4003. (while (re-search-forward org-drawer-regexp end t)
  4004. (org-flag-drawer t))))))
  4005. (defun org-flag-drawer (flag)
  4006. (save-excursion
  4007. (beginning-of-line 1)
  4008. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4009. (let ((b (match-end 0))
  4010. (outline-regexp org-outline-regexp))
  4011. (if (re-search-forward
  4012. "^[ \t]*:END:"
  4013. (save-excursion (outline-next-heading) (point)) t)
  4014. (outline-flag-region b (point-at-eol) flag)
  4015. (error ":END: line missing"))))))
  4016. (defun org-subtree-end-visible-p ()
  4017. "Is the end of the current subtree visible?"
  4018. (pos-visible-in-window-p
  4019. (save-excursion (org-end-of-subtree t) (point))))
  4020. (defun org-first-headline-recenter (&optional N)
  4021. "Move cursor to the first headline and recenter the headline.
  4022. Optional argument N means, put the headline into the Nth line of the window."
  4023. (goto-char (point-min))
  4024. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4025. (beginning-of-line)
  4026. (recenter (prefix-numeric-value N))))
  4027. ;;; Org-goto
  4028. (defvar org-goto-window-configuration nil)
  4029. (defvar org-goto-marker nil)
  4030. (defvar org-goto-map
  4031. (let ((map (make-sparse-keymap)))
  4032. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4033. (while (setq cmd (pop cmds))
  4034. (substitute-key-definition cmd cmd map global-map)))
  4035. (suppress-keymap map)
  4036. (org-defkey map "\C-m" 'org-goto-ret)
  4037. (org-defkey map [(return)] 'org-goto-ret)
  4038. (org-defkey map [(left)] 'org-goto-left)
  4039. (org-defkey map [(right)] 'org-goto-right)
  4040. (org-defkey map [(control ?g)] 'org-goto-quit)
  4041. (org-defkey map "\C-i" 'org-cycle)
  4042. (org-defkey map [(tab)] 'org-cycle)
  4043. (org-defkey map [(down)] 'outline-next-visible-heading)
  4044. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4045. (if org-goto-auto-isearch
  4046. (if (fboundp 'define-key-after)
  4047. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4048. nil)
  4049. (org-defkey map "q" 'org-goto-quit)
  4050. (org-defkey map "n" 'outline-next-visible-heading)
  4051. (org-defkey map "p" 'outline-previous-visible-heading)
  4052. (org-defkey map "f" 'outline-forward-same-level)
  4053. (org-defkey map "b" 'outline-backward-same-level)
  4054. (org-defkey map "u" 'outline-up-heading))
  4055. (org-defkey map "/" 'org-occur)
  4056. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4057. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4058. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4059. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4060. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4061. map))
  4062. (defconst org-goto-help
  4063. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4064. RET=jump to location [Q]uit and return to previous location
  4065. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4066. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4067. ;; FIXME: Docstring doe not mention both interfaces
  4068. (defun org-goto (&optional alternative-interface)
  4069. "Look up a different location in the current file, keeping current visibility.
  4070. When you want look-up or go to a different location in a document, the
  4071. fastest way is often to fold the entire buffer and then dive into the tree.
  4072. This method has the disadvantage, that the previous location will be folded,
  4073. which may not be what you want.
  4074. This command works around this by showing a copy of the current buffer
  4075. in an indirect buffer, in overview mode. You can dive into the tree in
  4076. that copy, use org-occur and incremental search to find a location.
  4077. When pressing RET or `Q', the command returns to the original buffer in
  4078. which the visibility is still unchanged. After RET is will also jump to
  4079. the location selected in the indirect buffer and expose the
  4080. the headline hierarchy above."
  4081. (interactive "P")
  4082. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4083. (org-refile-use-outline-path t)
  4084. (interface
  4085. (if (not alternative-interface)
  4086. org-goto-interface
  4087. (if (eq org-goto-interface 'outline)
  4088. 'outline-path-completion
  4089. 'outline)))
  4090. (org-goto-start-pos (point))
  4091. (selected-point
  4092. (if (eq interface 'outline)
  4093. (car (org-get-location (current-buffer) org-goto-help))
  4094. (nth 3 (org-refile-get-location "Goto: ")))))
  4095. (if selected-point
  4096. (progn
  4097. (org-mark-ring-push org-goto-start-pos)
  4098. (goto-char selected-point)
  4099. (if (or (org-invisible-p) (org-invisible-p2))
  4100. (org-show-context 'org-goto)))
  4101. (message "Quit"))))
  4102. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4103. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4104. (defvar org-goto-local-auto-isearch-map) ; defined below
  4105. (defun org-get-location (buf help)
  4106. "Let the user select a location in the Org-mode buffer BUF.
  4107. This function uses a recursive edit. It returns the selected position
  4108. or nil."
  4109. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4110. (isearch-hide-immediately nil)
  4111. (isearch-search-fun-function
  4112. (lambda () 'org-goto-local-search-headings))
  4113. (org-goto-selected-point org-goto-exit-command))
  4114. (save-excursion
  4115. (save-window-excursion
  4116. (delete-other-windows)
  4117. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4118. (switch-to-buffer
  4119. (condition-case nil
  4120. (make-indirect-buffer (current-buffer) "*org-goto*")
  4121. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4122. (with-output-to-temp-buffer "*Help*"
  4123. (princ help))
  4124. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4125. (setq buffer-read-only nil)
  4126. (let ((org-startup-truncated t)
  4127. (org-startup-folded nil)
  4128. (org-startup-align-all-tables nil))
  4129. (org-mode)
  4130. (org-overview))
  4131. (setq buffer-read-only t)
  4132. (if (and (boundp 'org-goto-start-pos)
  4133. (integer-or-marker-p org-goto-start-pos))
  4134. (let ((org-show-hierarchy-above t)
  4135. (org-show-siblings t)
  4136. (org-show-following-heading t))
  4137. (goto-char org-goto-start-pos)
  4138. (and (org-invisible-p) (org-show-context)))
  4139. (goto-char (point-min)))
  4140. (org-beginning-of-line)
  4141. (message "Select location and press RET")
  4142. (use-local-map org-goto-map)
  4143. (recursive-edit)
  4144. ))
  4145. (kill-buffer "*org-goto*")
  4146. (cons org-goto-selected-point org-goto-exit-command)))
  4147. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4148. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4149. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4150. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4151. (defun org-goto-local-search-headings (string bound noerror)
  4152. "Search and make sure that any matches are in headlines."
  4153. (catch 'return
  4154. (while (if isearch-forward
  4155. (search-forward string bound noerror)
  4156. (search-backward string bound noerror))
  4157. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4158. (and (member :headline context)
  4159. (not (member :tags context))))
  4160. (throw 'return (point))))))
  4161. (defun org-goto-local-auto-isearch ()
  4162. "Start isearch."
  4163. (interactive)
  4164. (goto-char (point-min))
  4165. (let ((keys (this-command-keys)))
  4166. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4167. (isearch-mode t)
  4168. (isearch-process-search-char (string-to-char keys)))))
  4169. (defun org-goto-ret (&optional arg)
  4170. "Finish `org-goto' by going to the new location."
  4171. (interactive "P")
  4172. (setq org-goto-selected-point (point)
  4173. org-goto-exit-command 'return)
  4174. (throw 'exit nil))
  4175. (defun org-goto-left ()
  4176. "Finish `org-goto' by going to the new location."
  4177. (interactive)
  4178. (if (org-on-heading-p)
  4179. (progn
  4180. (beginning-of-line 1)
  4181. (setq org-goto-selected-point (point)
  4182. org-goto-exit-command 'left)
  4183. (throw 'exit nil))
  4184. (error "Not on a heading")))
  4185. (defun org-goto-right ()
  4186. "Finish `org-goto' by going to the new location."
  4187. (interactive)
  4188. (if (org-on-heading-p)
  4189. (progn
  4190. (setq org-goto-selected-point (point)
  4191. org-goto-exit-command 'right)
  4192. (throw 'exit nil))
  4193. (error "Not on a heading")))
  4194. (defun org-goto-quit ()
  4195. "Finish `org-goto' without cursor motion."
  4196. (interactive)
  4197. (setq org-goto-selected-point nil)
  4198. (setq org-goto-exit-command 'quit)
  4199. (throw 'exit nil))
  4200. ;;; Indirect buffer display of subtrees
  4201. (defvar org-indirect-dedicated-frame nil
  4202. "This is the frame being used for indirect tree display.")
  4203. (defvar org-last-indirect-buffer nil)
  4204. (defun org-tree-to-indirect-buffer (&optional arg)
  4205. "Create indirect buffer and narrow it to current subtree.
  4206. With numerical prefix ARG, go up to this level and then take that tree.
  4207. If ARG is negative, go up that many levels.
  4208. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4209. indirect buffer previously made with this command, to avoid proliferation of
  4210. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4211. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4212. is kept so that you can work with several indirect buffers at the same time.
  4213. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4214. requests that a new frame be made for the new buffer, so that the dedicated
  4215. frame is not changed."
  4216. (interactive "P")
  4217. (let ((cbuf (current-buffer))
  4218. (cwin (selected-window))
  4219. (pos (point))
  4220. beg end level heading ibuf)
  4221. (save-excursion
  4222. (org-back-to-heading t)
  4223. (when (numberp arg)
  4224. (setq level (org-outline-level))
  4225. (if (< arg 0) (setq arg (+ level arg)))
  4226. (while (> (setq level (org-outline-level)) arg)
  4227. (outline-up-heading 1 t)))
  4228. (setq beg (point)
  4229. heading (org-get-heading))
  4230. (org-end-of-subtree t) (setq end (point)))
  4231. (if (and (buffer-live-p org-last-indirect-buffer)
  4232. (not (eq org-indirect-buffer-display 'new-frame))
  4233. (not arg))
  4234. (kill-buffer org-last-indirect-buffer))
  4235. (setq ibuf (org-get-indirect-buffer cbuf)
  4236. org-last-indirect-buffer ibuf)
  4237. (cond
  4238. ((or (eq org-indirect-buffer-display 'new-frame)
  4239. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4240. (select-frame (make-frame))
  4241. (delete-other-windows)
  4242. (switch-to-buffer ibuf)
  4243. (org-set-frame-title heading))
  4244. ((eq org-indirect-buffer-display 'dedicated-frame)
  4245. (raise-frame
  4246. (select-frame (or (and org-indirect-dedicated-frame
  4247. (frame-live-p org-indirect-dedicated-frame)
  4248. org-indirect-dedicated-frame)
  4249. (setq org-indirect-dedicated-frame (make-frame)))))
  4250. (delete-other-windows)
  4251. (switch-to-buffer ibuf)
  4252. (org-set-frame-title (concat "Indirect: " heading)))
  4253. ((eq org-indirect-buffer-display 'current-window)
  4254. (switch-to-buffer ibuf))
  4255. ((eq org-indirect-buffer-display 'other-window)
  4256. (pop-to-buffer ibuf))
  4257. (t (error "Invalid value.")))
  4258. (if (featurep 'xemacs)
  4259. (save-excursion (org-mode) (turn-on-font-lock)))
  4260. (narrow-to-region beg end)
  4261. (show-all)
  4262. (goto-char pos)
  4263. (and (window-live-p cwin) (select-window cwin))))
  4264. (defun org-get-indirect-buffer (&optional buffer)
  4265. (setq buffer (or buffer (current-buffer)))
  4266. (let ((n 1) (base (buffer-name buffer)) bname)
  4267. (while (buffer-live-p
  4268. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4269. (setq n (1+ n)))
  4270. (condition-case nil
  4271. (make-indirect-buffer buffer bname 'clone)
  4272. (error (make-indirect-buffer buffer bname)))))
  4273. (defun org-set-frame-title (title)
  4274. "Set the title of the current frame to the string TITLE."
  4275. ;; FIXME: how to name a single frame in XEmacs???
  4276. (unless (featurep 'xemacs)
  4277. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4278. ;;;; Structure editing
  4279. ;;; Inserting headlines
  4280. (defun org-insert-heading (&optional force-heading)
  4281. "Insert a new heading or item with same depth at point.
  4282. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4283. If point is at the beginning of a headline, insert a sibling before the
  4284. current headline. If point is not at the beginning, do not split the line,
  4285. but create the new headline after the current line."
  4286. (interactive "P")
  4287. (if (= (buffer-size) 0)
  4288. (insert "\n* ")
  4289. (when (or force-heading (not (org-insert-item)))
  4290. (let* ((head (save-excursion
  4291. (condition-case nil
  4292. (progn
  4293. (org-back-to-heading)
  4294. (match-string 0))
  4295. (error "*"))))
  4296. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4297. pos hide-previous previous-pos)
  4298. (cond
  4299. ((and (org-on-heading-p) (bolp)
  4300. (or (bobp)
  4301. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4302. ;; insert before the current line
  4303. (open-line (if blank 2 1)))
  4304. ((and (bolp)
  4305. (or (bobp)
  4306. (save-excursion
  4307. (backward-char 1) (not (org-invisible-p)))))
  4308. ;; insert right here
  4309. nil)
  4310. (t
  4311. ;; somewhere in the line
  4312. (save-excursion
  4313. (setq previous-pos (point-at-bol))
  4314. (end-of-line)
  4315. (setq hide-previous (org-invisible-p)))
  4316. (and org-insert-heading-respect-content (org-show-subtree))
  4317. (let ((split
  4318. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4319. (save-excursion
  4320. (let ((p (point)))
  4321. (goto-char (point-at-bol))
  4322. (and (looking-at org-complex-heading-regexp)
  4323. (> p (match-beginning 4)))))))
  4324. tags pos)
  4325. (cond
  4326. (org-insert-heading-respect-content
  4327. (org-end-of-subtree nil t)
  4328. (or (bolp) (newline))
  4329. (open-line 1))
  4330. ((org-on-heading-p)
  4331. (when hide-previous
  4332. (show-children)
  4333. (org-show-entry))
  4334. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4335. (setq tags (and (match-end 2) (match-string 2)))
  4336. (and (match-end 1)
  4337. (delete-region (match-beginning 1) (match-end 1)))
  4338. (setq pos (point-at-bol))
  4339. (or split (end-of-line 1))
  4340. (delete-horizontal-space)
  4341. (newline (if blank 2 1))
  4342. (when tags
  4343. (save-excursion
  4344. (goto-char pos)
  4345. (end-of-line 1)
  4346. (insert " " tags)
  4347. (org-set-tags nil 'align))))
  4348. (t
  4349. (or split (end-of-line 1))
  4350. (newline (if blank 2 1)))))))
  4351. (insert head) (just-one-space)
  4352. (setq pos (point))
  4353. (end-of-line 1)
  4354. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4355. (when (and org-insert-heading-respect-content hide-previous)
  4356. (save-excursion
  4357. (goto-char previous-pos)
  4358. (hide-subtree)))
  4359. (run-hooks 'org-insert-heading-hook)))))
  4360. (defun org-get-heading (&optional no-tags)
  4361. "Return the heading of the current entry, without the stars."
  4362. (save-excursion
  4363. (org-back-to-heading t)
  4364. (if (looking-at
  4365. (if no-tags
  4366. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4367. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4368. (match-string 1) "")))
  4369. (defun org-insert-heading-after-current ()
  4370. "Insert a new heading with same level as current, after current subtree."
  4371. (interactive)
  4372. (org-back-to-heading)
  4373. (org-insert-heading)
  4374. (org-move-subtree-down)
  4375. (end-of-line 1))
  4376. (defun org-insert-heading-respect-content ()
  4377. (interactive)
  4378. (let ((org-insert-heading-respect-content t))
  4379. (org-insert-heading t)))
  4380. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4381. (interactive "P")
  4382. (let ((org-insert-heading-respect-content t))
  4383. (org-insert-todo-heading force-state t)))
  4384. (defun org-insert-todo-heading (arg &optional force-heading)
  4385. "Insert a new heading with the same level and TODO state as current heading.
  4386. If the heading has no TODO state, or if the state is DONE, use the first
  4387. state (TODO by default). Also with prefix arg, force first state."
  4388. (interactive "P")
  4389. (when (or force-heading (not (org-insert-item 'checkbox)))
  4390. (org-insert-heading force-heading)
  4391. (save-excursion
  4392. (org-back-to-heading)
  4393. (outline-previous-heading)
  4394. (looking-at org-todo-line-regexp))
  4395. (if (or arg
  4396. (not (match-beginning 2))
  4397. (member (match-string 2) org-done-keywords))
  4398. (insert (car org-todo-keywords-1) " ")
  4399. (insert (match-string 2) " "))
  4400. (when org-provide-todo-statistics
  4401. (org-update-parent-todo-statistics))))
  4402. (defun org-insert-subheading (arg)
  4403. "Insert a new subheading and demote it.
  4404. Works for outline headings and for plain lists alike."
  4405. (interactive "P")
  4406. (org-insert-heading arg)
  4407. (cond
  4408. ((org-on-heading-p) (org-do-demote))
  4409. ((org-at-item-p) (org-indent-item 1))))
  4410. (defun org-insert-todo-subheading (arg)
  4411. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4412. Works for outline headings and for plain lists alike."
  4413. (interactive "P")
  4414. (org-insert-todo-heading arg)
  4415. (cond
  4416. ((org-on-heading-p) (org-do-demote))
  4417. ((org-at-item-p) (org-indent-item 1))))
  4418. ;;; Promotion and Demotion
  4419. (defun org-promote-subtree ()
  4420. "Promote the entire subtree.
  4421. See also `org-promote'."
  4422. (interactive)
  4423. (save-excursion
  4424. (org-map-tree 'org-promote))
  4425. (org-fix-position-after-promote))
  4426. (defun org-demote-subtree ()
  4427. "Demote the entire subtree. See `org-demote'.
  4428. See also `org-promote'."
  4429. (interactive)
  4430. (save-excursion
  4431. (org-map-tree 'org-demote))
  4432. (org-fix-position-after-promote))
  4433. (defun org-do-promote ()
  4434. "Promote the current heading higher up the tree.
  4435. If the region is active in `transient-mark-mode', promote all headings
  4436. in the region."
  4437. (interactive)
  4438. (save-excursion
  4439. (if (org-region-active-p)
  4440. (org-map-region 'org-promote (region-beginning) (region-end))
  4441. (org-promote)))
  4442. (org-fix-position-after-promote))
  4443. (defun org-do-demote ()
  4444. "Demote the current heading lower down the tree.
  4445. If the region is active in `transient-mark-mode', demote all headings
  4446. in the region."
  4447. (interactive)
  4448. (save-excursion
  4449. (if (org-region-active-p)
  4450. (org-map-region 'org-demote (region-beginning) (region-end))
  4451. (org-demote)))
  4452. (org-fix-position-after-promote))
  4453. (defun org-fix-position-after-promote ()
  4454. "Make sure that after pro/demotion cursor position is right."
  4455. (let ((pos (point)))
  4456. (when (save-excursion
  4457. (beginning-of-line 1)
  4458. (looking-at org-todo-line-regexp)
  4459. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4460. (cond ((eobp) (insert " "))
  4461. ((eolp) (insert " "))
  4462. ((equal (char-after) ?\ ) (forward-char 1))))))
  4463. (defun org-reduced-level (l)
  4464. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4465. (defun org-get-valid-level (level &optional change)
  4466. "Rectify a level change under the influence of `org-odd-levels-only'
  4467. LEVEL is a current level, CHANGE is by how much the level should be
  4468. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4469. even level numbers will become the next higher odd number."
  4470. (if org-odd-levels-only
  4471. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4472. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4473. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4474. (max 1 (+ level change))))
  4475. (if (boundp 'define-obsolete-function-alias)
  4476. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4477. (define-obsolete-function-alias 'org-get-legal-level
  4478. 'org-get-valid-level)
  4479. (define-obsolete-function-alias 'org-get-legal-level
  4480. 'org-get-valid-level "23.1")))
  4481. (defun org-promote ()
  4482. "Promote the current heading higher up the tree.
  4483. If the region is active in `transient-mark-mode', promote all headings
  4484. in the region."
  4485. (org-back-to-heading t)
  4486. (let* ((level (save-match-data (funcall outline-level)))
  4487. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4488. (diff (abs (- level (length up-head) -1))))
  4489. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4490. (replace-match up-head nil t)
  4491. ;; Fixup tag positioning
  4492. (and org-auto-align-tags (org-set-tags nil t))
  4493. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4494. (defun org-demote ()
  4495. "Demote the current heading lower down the tree.
  4496. If the region is active in `transient-mark-mode', demote all headings
  4497. in the region."
  4498. (org-back-to-heading t)
  4499. (let* ((level (save-match-data (funcall outline-level)))
  4500. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4501. (diff (abs (- level (length down-head) -1))))
  4502. (replace-match down-head nil t)
  4503. ;; Fixup tag positioning
  4504. (and org-auto-align-tags (org-set-tags nil t))
  4505. (if org-adapt-indentation (org-fixup-indentation diff))))
  4506. (defun org-map-tree (fun)
  4507. "Call FUN for every heading underneath the current one."
  4508. (org-back-to-heading)
  4509. (let ((level (funcall outline-level)))
  4510. (save-excursion
  4511. (funcall fun)
  4512. (while (and (progn
  4513. (outline-next-heading)
  4514. (> (funcall outline-level) level))
  4515. (not (eobp)))
  4516. (funcall fun)))))
  4517. (defun org-map-region (fun beg end)
  4518. "Call FUN for every heading between BEG and END."
  4519. (let ((org-ignore-region t))
  4520. (save-excursion
  4521. (setq end (copy-marker end))
  4522. (goto-char beg)
  4523. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4524. (< (point) end))
  4525. (funcall fun))
  4526. (while (and (progn
  4527. (outline-next-heading)
  4528. (< (point) end))
  4529. (not (eobp)))
  4530. (funcall fun)))))
  4531. (defun org-fixup-indentation (diff)
  4532. "Change the indentation in the current entry by DIFF
  4533. However, if any line in the current entry has no indentation, or if it
  4534. would end up with no indentation after the change, nothing at all is done."
  4535. (save-excursion
  4536. (let ((end (save-excursion (outline-next-heading)
  4537. (point-marker)))
  4538. (prohibit (if (> diff 0)
  4539. "^\\S-"
  4540. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4541. col)
  4542. (unless (save-excursion (end-of-line 1)
  4543. (re-search-forward prohibit end t))
  4544. (while (and (< (point) end)
  4545. (re-search-forward "^[ \t]+" end t))
  4546. (goto-char (match-end 0))
  4547. (setq col (current-column))
  4548. (if (< diff 0) (replace-match ""))
  4549. (org-indent-to-column (+ diff col))))
  4550. (move-marker end nil))))
  4551. (defun org-convert-to-odd-levels ()
  4552. "Convert an org-mode file with all levels allowed to one with odd levels.
  4553. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4554. level 5 etc."
  4555. (interactive)
  4556. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4557. (let ((org-odd-levels-only nil) n)
  4558. (save-excursion
  4559. (goto-char (point-min))
  4560. (while (re-search-forward "^\\*\\*+ " nil t)
  4561. (setq n (- (length (match-string 0)) 2))
  4562. (while (>= (setq n (1- n)) 0)
  4563. (org-demote))
  4564. (end-of-line 1))))))
  4565. (defun org-convert-to-oddeven-levels ()
  4566. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4567. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4568. section with an even level, conversion would destroy the structure of the file. An error
  4569. is signaled in this case."
  4570. (interactive)
  4571. (goto-char (point-min))
  4572. ;; First check if there are no even levels
  4573. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4574. (org-show-context t)
  4575. (error "Not all levels are odd in this file. Conversion not possible."))
  4576. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4577. (let ((org-odd-levels-only nil) n)
  4578. (save-excursion
  4579. (goto-char (point-min))
  4580. (while (re-search-forward "^\\*\\*+ " nil t)
  4581. (setq n (/ (1- (length (match-string 0))) 2))
  4582. (while (>= (setq n (1- n)) 0)
  4583. (org-promote))
  4584. (end-of-line 1))))))
  4585. (defun org-tr-level (n)
  4586. "Make N odd if required."
  4587. (if org-odd-levels-only (1+ (/ n 2)) n))
  4588. ;;; Vertical tree motion, cutting and pasting of subtrees
  4589. (defun org-move-subtree-up (&optional arg)
  4590. "Move the current subtree up past ARG headlines of the same level."
  4591. (interactive "p")
  4592. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4593. (defun org-move-subtree-down (&optional arg)
  4594. "Move the current subtree down past ARG headlines of the same level."
  4595. (interactive "p")
  4596. (setq arg (prefix-numeric-value arg))
  4597. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4598. 'outline-get-last-sibling))
  4599. (ins-point (make-marker))
  4600. (cnt (abs arg))
  4601. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4602. ;; Select the tree
  4603. (org-back-to-heading)
  4604. (setq beg0 (point))
  4605. (save-excursion
  4606. (setq ne-beg (org-back-over-empty-lines))
  4607. (setq beg (point)))
  4608. (save-match-data
  4609. (save-excursion (outline-end-of-heading)
  4610. (setq folded (org-invisible-p)))
  4611. (outline-end-of-subtree))
  4612. (outline-next-heading)
  4613. (setq ne-end (org-back-over-empty-lines))
  4614. (setq end (point))
  4615. (goto-char beg0)
  4616. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4617. ;; include less whitespace
  4618. (save-excursion
  4619. (goto-char beg)
  4620. (forward-line (- ne-beg ne-end))
  4621. (setq beg (point))))
  4622. ;; Find insertion point, with error handling
  4623. (while (> cnt 0)
  4624. (or (and (funcall movfunc) (looking-at outline-regexp))
  4625. (progn (goto-char beg0)
  4626. (error "Cannot move past superior level or buffer limit")))
  4627. (setq cnt (1- cnt)))
  4628. (if (> arg 0)
  4629. ;; Moving forward - still need to move over subtree
  4630. (progn (org-end-of-subtree t t)
  4631. (save-excursion
  4632. (org-back-over-empty-lines)
  4633. (or (bolp) (newline)))))
  4634. (setq ne-ins (org-back-over-empty-lines))
  4635. (move-marker ins-point (point))
  4636. (setq txt (buffer-substring beg end))
  4637. (org-save-markers-in-region beg end)
  4638. (delete-region beg end)
  4639. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4640. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4641. (let ((bbb (point)))
  4642. (insert-before-markers txt)
  4643. (org-reinstall-markers-in-region bbb)
  4644. (move-marker ins-point bbb))
  4645. (or (bolp) (insert "\n"))
  4646. (setq ins-end (point))
  4647. (goto-char ins-point)
  4648. (org-skip-whitespace)
  4649. (when (and (< arg 0)
  4650. (org-first-sibling-p)
  4651. (> ne-ins ne-beg))
  4652. ;; Move whitespace back to beginning
  4653. (save-excursion
  4654. (goto-char ins-end)
  4655. (let ((kill-whole-line t))
  4656. (kill-line (- ne-ins ne-beg)) (point)))
  4657. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4658. (move-marker ins-point nil)
  4659. (org-compact-display-after-subtree-move)
  4660. (org-show-empty-lines-in-parent)
  4661. (unless folded
  4662. (org-show-entry)
  4663. (show-children)
  4664. (org-cycle-hide-drawers 'children))))
  4665. (defvar org-subtree-clip ""
  4666. "Clipboard for cut and paste of subtrees.
  4667. This is actually only a copy of the kill, because we use the normal kill
  4668. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4669. (defvar org-subtree-clip-folded nil
  4670. "Was the last copied subtree folded?
  4671. This is used to fold the tree back after pasting.")
  4672. (defun org-cut-subtree (&optional n)
  4673. "Cut the current subtree into the clipboard.
  4674. With prefix arg N, cut this many sequential subtrees.
  4675. This is a short-hand for marking the subtree and then cutting it."
  4676. (interactive "p")
  4677. (org-copy-subtree n 'cut))
  4678. (defun org-copy-subtree (&optional n cut force-store-markers)
  4679. "Cut the current subtree into the clipboard.
  4680. With prefix arg N, cut this many sequential subtrees.
  4681. This is a short-hand for marking the subtree and then copying it.
  4682. If CUT is non-nil, actually cut the subtree.
  4683. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4684. of some markers in the region, even if CUT is non-nil. This is
  4685. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4686. (interactive "p")
  4687. (let (beg end folded (beg0 (point)))
  4688. (if (interactive-p)
  4689. (org-back-to-heading nil) ; take what looks like a subtree
  4690. (org-back-to-heading t)) ; take what is really there
  4691. (org-back-over-empty-lines)
  4692. (setq beg (point))
  4693. (skip-chars-forward " \t\r\n")
  4694. (save-match-data
  4695. (save-excursion (outline-end-of-heading)
  4696. (setq folded (org-invisible-p)))
  4697. (condition-case nil
  4698. (outline-forward-same-level (1- n))
  4699. (error nil))
  4700. (org-end-of-subtree t t))
  4701. (org-back-over-empty-lines)
  4702. (setq end (point))
  4703. (goto-char beg0)
  4704. (when (> end beg)
  4705. (setq org-subtree-clip-folded folded)
  4706. (when (or cut force-store-markers)
  4707. (org-save-markers-in-region beg end))
  4708. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4709. (setq org-subtree-clip (current-kill 0))
  4710. (message "%s: Subtree(s) with %d characters"
  4711. (if cut "Cut" "Copied")
  4712. (length org-subtree-clip)))))
  4713. (defun org-paste-subtree (&optional level tree for-yank)
  4714. "Paste the clipboard as a subtree, with modification of headline level.
  4715. The entire subtree is promoted or demoted in order to match a new headline
  4716. level.
  4717. If the cursor is at the beginning of a headline, the same level as
  4718. that headline is used to paste the tree
  4719. If not, the new level is derived from the *visible* headings
  4720. before and after the insertion point, and taken to be the inferior headline
  4721. level of the two. So if the previous visible heading is level 3 and the
  4722. next is level 4 (or vice versa), level 4 will be used for insertion.
  4723. This makes sure that the subtree remains an independent subtree and does
  4724. not swallow low level entries.
  4725. You can also force a different level, either by using a numeric prefix
  4726. argument, or by inserting the heading marker by hand. For example, if the
  4727. cursor is after \"*****\", then the tree will be shifted to level 5.
  4728. If optional TREE is given, use this text instead of the kill ring.
  4729. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4730. move back over whitespace before inserting, and move point to the end of
  4731. the inserted text when done."
  4732. (interactive "P")
  4733. (unless (org-kill-is-subtree-p tree)
  4734. (error "%s"
  4735. (substitute-command-keys
  4736. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4737. (let* ((visp (not (org-invisible-p)))
  4738. (txt (or tree (and kill-ring (current-kill 0))))
  4739. (^re (concat "^\\(" outline-regexp "\\)"))
  4740. (re (concat "\\(" outline-regexp "\\)"))
  4741. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4742. (old-level (if (string-match ^re txt)
  4743. (- (match-end 0) (match-beginning 0) 1)
  4744. -1))
  4745. (force-level (cond (level (prefix-numeric-value level))
  4746. ((and (looking-at "[ \t]*$")
  4747. (string-match
  4748. ^re_ (buffer-substring
  4749. (point-at-bol) (point))))
  4750. (- (match-end 1) (match-beginning 1)))
  4751. ((and (bolp)
  4752. (looking-at org-outline-regexp))
  4753. (- (match-end 0) (point) 1))
  4754. (t nil)))
  4755. (previous-level (save-excursion
  4756. (condition-case nil
  4757. (progn
  4758. (outline-previous-visible-heading 1)
  4759. (if (looking-at re)
  4760. (- (match-end 0) (match-beginning 0) 1)
  4761. 1))
  4762. (error 1))))
  4763. (next-level (save-excursion
  4764. (condition-case nil
  4765. (progn
  4766. (or (looking-at outline-regexp)
  4767. (outline-next-visible-heading 1))
  4768. (if (looking-at re)
  4769. (- (match-end 0) (match-beginning 0) 1)
  4770. 1))
  4771. (error 1))))
  4772. (new-level (or force-level (max previous-level next-level)))
  4773. (shift (if (or (= old-level -1)
  4774. (= new-level -1)
  4775. (= old-level new-level))
  4776. 0
  4777. (- new-level old-level)))
  4778. (delta (if (> shift 0) -1 1))
  4779. (func (if (> shift 0) 'org-demote 'org-promote))
  4780. (org-odd-levels-only nil)
  4781. beg end newend)
  4782. ;; Remove the forced level indicator
  4783. (if force-level
  4784. (delete-region (point-at-bol) (point)))
  4785. ;; Paste
  4786. (beginning-of-line 1)
  4787. (unless for-yank (org-back-over-empty-lines))
  4788. (setq beg (point))
  4789. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4790. (insert-before-markers txt)
  4791. (unless (string-match "\n\\'" txt) (insert "\n"))
  4792. (setq newend (point))
  4793. (org-reinstall-markers-in-region beg)
  4794. (setq end (point))
  4795. (goto-char beg)
  4796. (skip-chars-forward " \t\n\r")
  4797. (setq beg (point))
  4798. (if (and (org-invisible-p) visp)
  4799. (save-excursion (outline-show-heading)))
  4800. ;; Shift if necessary
  4801. (unless (= shift 0)
  4802. (save-restriction
  4803. (narrow-to-region beg end)
  4804. (while (not (= shift 0))
  4805. (org-map-region func (point-min) (point-max))
  4806. (setq shift (+ delta shift)))
  4807. (goto-char (point-min))
  4808. (setq newend (point-max))))
  4809. (when (or (interactive-p) for-yank)
  4810. (message "Clipboard pasted as level %d subtree" new-level))
  4811. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4812. kill-ring
  4813. (eq org-subtree-clip (current-kill 0))
  4814. org-subtree-clip-folded)
  4815. ;; The tree was folded before it was killed/copied
  4816. (hide-subtree))
  4817. (and for-yank (goto-char newend))))
  4818. (defun org-kill-is-subtree-p (&optional txt)
  4819. "Check if the current kill is an outline subtree, or a set of trees.
  4820. Returns nil if kill does not start with a headline, or if the first
  4821. headline level is not the largest headline level in the tree.
  4822. So this will actually accept several entries of equal levels as well,
  4823. which is OK for `org-paste-subtree'.
  4824. If optional TXT is given, check this string instead of the current kill."
  4825. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4826. (start-level (and kill
  4827. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4828. org-outline-regexp "\\)")
  4829. kill)
  4830. (- (match-end 2) (match-beginning 2) 1)))
  4831. (re (concat "^" org-outline-regexp))
  4832. (start (1+ (or (match-beginning 2) -1))))
  4833. (if (not start-level)
  4834. (progn
  4835. nil) ;; does not even start with a heading
  4836. (catch 'exit
  4837. (while (setq start (string-match re kill (1+ start)))
  4838. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4839. (throw 'exit nil)))
  4840. t))))
  4841. (defvar org-markers-to-move nil
  4842. "Markers that should be moved with a cut-and-paste operation.
  4843. Those markers are stored together with their positions relative to
  4844. the start of the region.")
  4845. (defun org-save-markers-in-region (beg end)
  4846. "Check markers in region.
  4847. If these markers are between BEG and END, record their position relative
  4848. to BEG, so that after moving the block of text, we can put the markers back
  4849. into place.
  4850. This function gets called just before an entry or tree gets cut from the
  4851. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4852. called immediately, to move the markers with the entries."
  4853. (setq org-markers-to-move nil)
  4854. (when (featurep 'org-clock)
  4855. (org-clock-save-markers-for-cut-and-paste beg end))
  4856. (when (featurep 'org-agenda)
  4857. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4858. (defun org-check-and-save-marker (marker beg end)
  4859. "Check if MARKER is between BEG and END.
  4860. If yes, remember the marker and the distance to BEG."
  4861. (when (and (marker-buffer marker)
  4862. (equal (marker-buffer marker) (current-buffer)))
  4863. (if (and (>= marker beg) (< marker end))
  4864. (push (cons marker (- marker beg)) org-markers-to-move))))
  4865. (defun org-reinstall-markers-in-region (beg)
  4866. "Move all remembered markers to their position relative to BEG."
  4867. (mapc (lambda (x)
  4868. (move-marker (car x) (+ beg (cdr x))))
  4869. org-markers-to-move)
  4870. (setq org-markers-to-move nil))
  4871. (defun org-narrow-to-subtree ()
  4872. "Narrow buffer to the current subtree."
  4873. (interactive)
  4874. (save-excursion
  4875. (save-match-data
  4876. (narrow-to-region
  4877. (progn (org-back-to-heading) (point))
  4878. (progn (org-end-of-subtree t) (point))))))
  4879. ;;; Outline Sorting
  4880. (defun org-sort (with-case)
  4881. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4882. Optional argument WITH-CASE means sort case-sensitively."
  4883. (interactive "P")
  4884. (if (org-at-table-p)
  4885. (org-call-with-arg 'org-table-sort-lines with-case)
  4886. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4887. (defun org-sort-remove-invisible (s)
  4888. (remove-text-properties 0 (length s) org-rm-props s)
  4889. (while (string-match org-bracket-link-regexp s)
  4890. (setq s (replace-match (if (match-end 2)
  4891. (match-string 3 s)
  4892. (match-string 1 s)) t t s)))
  4893. s)
  4894. (defvar org-priority-regexp) ; defined later in the file
  4895. (defun org-sort-entries-or-items
  4896. (&optional with-case sorting-type getkey-func compare-func property)
  4897. "Sort entries on a certain level of an outline tree.
  4898. If there is an active region, the entries in the region are sorted.
  4899. Else, if the cursor is before the first entry, sort the top-level items.
  4900. Else, the children of the entry at point are sorted.
  4901. Sorting can be alphabetically, numerically, and by date/time as given by
  4902. the first time stamp in the entry. The command prompts for the sorting
  4903. type unless it has been given to the function through the SORTING-TYPE
  4904. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4905. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4906. called with point at the beginning of the record. It must return either
  4907. a string or a number that should serve as the sorting key for that record.
  4908. Comparing entries ignores case by default. However, with an optional argument
  4909. WITH-CASE, the sorting considers case as well."
  4910. (interactive "P")
  4911. (let ((case-func (if with-case 'identity 'downcase))
  4912. start beg end stars re re2
  4913. txt what tmp plain-list-p)
  4914. ;; Find beginning and end of region to sort
  4915. (cond
  4916. ((org-region-active-p)
  4917. ;; we will sort the region
  4918. (setq end (region-end)
  4919. what "region")
  4920. (goto-char (region-beginning))
  4921. (if (not (org-on-heading-p)) (outline-next-heading))
  4922. (setq start (point)))
  4923. ((org-at-item-p)
  4924. ;; we will sort this plain list
  4925. (org-beginning-of-item-list) (setq start (point))
  4926. (org-end-of-item-list) (setq end (point))
  4927. (goto-char start)
  4928. (setq plain-list-p t
  4929. what "plain list"))
  4930. ((or (org-on-heading-p)
  4931. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4932. ;; we will sort the children of the current headline
  4933. (org-back-to-heading)
  4934. (setq start (point)
  4935. end (progn (org-end-of-subtree t t)
  4936. (org-back-over-empty-lines)
  4937. (point))
  4938. what "children")
  4939. (goto-char start)
  4940. (show-subtree)
  4941. (outline-next-heading))
  4942. (t
  4943. ;; we will sort the top-level entries in this file
  4944. (goto-char (point-min))
  4945. (or (org-on-heading-p) (outline-next-heading))
  4946. (setq start (point) end (point-max) what "top-level")
  4947. (goto-char start)
  4948. (show-all)))
  4949. (setq beg (point))
  4950. (if (>= beg end) (error "Nothing to sort"))
  4951. (unless plain-list-p
  4952. (looking-at "\\(\\*+\\)")
  4953. (setq stars (match-string 1)
  4954. re (concat "^" (regexp-quote stars) " +")
  4955. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4956. txt (buffer-substring beg end))
  4957. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4958. (if (and (not (equal stars "*")) (string-match re2 txt))
  4959. (error "Region to sort contains a level above the first entry")))
  4960. (unless sorting-type
  4961. (message
  4962. (if plain-list-p
  4963. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4964. "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:")
  4965. what)
  4966. (setq sorting-type (read-char-exclusive))
  4967. (and (= (downcase sorting-type) ?f)
  4968. (setq getkey-func
  4969. (org-ido-completing-read "Sort using function: "
  4970. obarray 'fboundp t nil nil))
  4971. (setq getkey-func (intern getkey-func)))
  4972. (and (= (downcase sorting-type) ?r)
  4973. (setq property
  4974. (org-ido-completing-read "Property: "
  4975. (mapcar 'list (org-buffer-property-keys t))
  4976. nil t))))
  4977. (message "Sorting entries...")
  4978. (save-restriction
  4979. (narrow-to-region start end)
  4980. (let ((dcst (downcase sorting-type))
  4981. (now (current-time)))
  4982. (sort-subr
  4983. (/= dcst sorting-type)
  4984. ;; This function moves to the beginning character of the "record" to
  4985. ;; be sorted.
  4986. (if plain-list-p
  4987. (lambda nil
  4988. (if (org-at-item-p) t (goto-char (point-max))))
  4989. (lambda nil
  4990. (if (re-search-forward re nil t)
  4991. (goto-char (match-beginning 0))
  4992. (goto-char (point-max)))))
  4993. ;; This function moves to the last character of the "record" being
  4994. ;; sorted.
  4995. (if plain-list-p
  4996. 'org-end-of-item
  4997. (lambda nil
  4998. (save-match-data
  4999. (condition-case nil
  5000. (outline-forward-same-level 1)
  5001. (error
  5002. (goto-char (point-max)))))))
  5003. ;; This function returns the value that gets sorted against.
  5004. (if plain-list-p
  5005. (lambda nil
  5006. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5007. (cond
  5008. ((= dcst ?n)
  5009. (string-to-number (buffer-substring (match-end 0)
  5010. (point-at-eol))))
  5011. ((= dcst ?a)
  5012. (buffer-substring (match-end 0) (point-at-eol)))
  5013. ((= dcst ?t)
  5014. (if (re-search-forward org-ts-regexp
  5015. (point-at-eol) t)
  5016. (org-time-string-to-time (match-string 0))
  5017. now))
  5018. ((= dcst ?f)
  5019. (if getkey-func
  5020. (progn
  5021. (setq tmp (funcall getkey-func))
  5022. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5023. tmp)
  5024. (error "Invalid key function `%s'" getkey-func)))
  5025. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5026. (lambda nil
  5027. (cond
  5028. ((= dcst ?n)
  5029. (if (looking-at org-complex-heading-regexp)
  5030. (string-to-number (match-string 4))
  5031. nil))
  5032. ((= dcst ?a)
  5033. (if (looking-at org-complex-heading-regexp)
  5034. (funcall case-func (match-string 4))
  5035. nil))
  5036. ((= dcst ?t)
  5037. (if (re-search-forward org-ts-regexp
  5038. (save-excursion
  5039. (forward-line 2)
  5040. (point)) t)
  5041. (org-time-string-to-time (match-string 0))
  5042. now))
  5043. ((= dcst ?p)
  5044. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5045. (string-to-char (match-string 2))
  5046. org-default-priority))
  5047. ((= dcst ?r)
  5048. (or (org-entry-get nil property) ""))
  5049. ((= dcst ?o)
  5050. (if (looking-at org-complex-heading-regexp)
  5051. (- 9999 (length (member (match-string 2)
  5052. org-todo-keywords-1)))))
  5053. ((= dcst ?f)
  5054. (if getkey-func
  5055. (progn
  5056. (setq tmp (funcall getkey-func))
  5057. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5058. tmp)
  5059. (error "Invalid key function `%s'" getkey-func)))
  5060. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5061. nil
  5062. (cond
  5063. ((= dcst ?a) 'string<)
  5064. ((= dcst ?t) 'time-less-p)
  5065. ((= dcst ?f) compare-func)
  5066. (t nil)))))
  5067. (message "Sorting entries...done")))
  5068. (defun org-do-sort (table what &optional with-case sorting-type)
  5069. "Sort TABLE of WHAT according to SORTING-TYPE.
  5070. The user will be prompted for the SORTING-TYPE if the call to this
  5071. function does not specify it. WHAT is only for the prompt, to indicate
  5072. what is being sorted. The sorting key will be extracted from
  5073. the car of the elements of the table.
  5074. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5075. (unless sorting-type
  5076. (message
  5077. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5078. what)
  5079. (setq sorting-type (read-char-exclusive)))
  5080. (let ((dcst (downcase sorting-type))
  5081. extractfun comparefun)
  5082. ;; Define the appropriate functions
  5083. (cond
  5084. ((= dcst ?n)
  5085. (setq extractfun 'string-to-number
  5086. comparefun (if (= dcst sorting-type) '< '>)))
  5087. ((= dcst ?a)
  5088. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5089. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5090. comparefun (if (= dcst sorting-type)
  5091. 'string<
  5092. (lambda (a b) (and (not (string< a b))
  5093. (not (string= a b)))))))
  5094. ((= dcst ?t)
  5095. (setq extractfun
  5096. (lambda (x)
  5097. (if (string-match org-ts-regexp x)
  5098. (time-to-seconds
  5099. (org-time-string-to-time (match-string 0 x)))
  5100. 0))
  5101. comparefun (if (= dcst sorting-type) '< '>)))
  5102. (t (error "Invalid sorting type `%c'" sorting-type)))
  5103. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5104. table)
  5105. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5106. ;;; Editing source examples
  5107. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5108. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5109. (defvar org-edit-src-force-single-line nil)
  5110. (defvar org-edit-src-from-org-mode nil)
  5111. (defvar org-edit-src-picture nil)
  5112. (define-minor-mode org-exit-edit-mode
  5113. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5114. (defun org-edit-src-code ()
  5115. "Edit the source code example at point.
  5116. An indirect buffer is created, and that buffer is then narrowed to the
  5117. example at point and switched to the correct language mode. When done,
  5118. exit by killing the buffer with \\[org-edit-src-exit]."
  5119. (interactive)
  5120. (let ((line (org-current-line))
  5121. (case-fold-search t)
  5122. (msg (substitute-command-keys
  5123. "Edit, then exit with C-c ' (C-c and single quote)"))
  5124. (info (org-edit-src-find-region-and-lang))
  5125. (org-mode-p (eq major-mode 'org-mode))
  5126. beg end lang lang-f single)
  5127. (if (not info)
  5128. nil
  5129. (setq beg (nth 0 info)
  5130. end (nth 1 info)
  5131. lang (nth 2 info)
  5132. single (nth 3 info)
  5133. lang-f (intern (concat lang "-mode")))
  5134. (unless (functionp lang-f)
  5135. (error "No such language mode: %s" lang-f))
  5136. (goto-line line)
  5137. (if (get-buffer "*Org Edit Src Example*")
  5138. (kill-buffer "*Org Edit Src Example*"))
  5139. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5140. "*Org Edit Src Example*"))
  5141. (narrow-to-region beg end)
  5142. (remove-text-properties beg end '(display nil invisible nil
  5143. intangible nil))
  5144. (let ((org-inhibit-startup t))
  5145. (funcall lang-f))
  5146. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5147. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5148. (when org-mode-p
  5149. (goto-char (point-min))
  5150. (while (re-search-forward "^," nil t)
  5151. (replace-match "")))
  5152. (goto-line line)
  5153. (org-exit-edit-mode)
  5154. (org-set-local 'header-line-format msg)
  5155. (message "%s" msg)
  5156. t)))
  5157. (defun org-edit-fixed-width-region ()
  5158. "Edit the fixed-width ascii drawing at point.
  5159. This must be a region where each line starts with a colon followed by
  5160. a space character.
  5161. An indirect buffer is created, and that buffer is then narrowed to the
  5162. example at point and switched to artist-mode. When done,
  5163. exit by killing the buffer with \\[org-edit-src-exit]."
  5164. (interactive)
  5165. (let ((line (org-current-line))
  5166. (case-fold-search t)
  5167. (msg (substitute-command-keys
  5168. "Edit, then exit with C-c ' (C-c and single quote)"))
  5169. (org-mode-p (eq major-mode 'org-mode))
  5170. beg end lang lang-f)
  5171. (beginning-of-line 1)
  5172. (if (looking-at "[ \t]*[^:\n \t]")
  5173. nil
  5174. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5175. (setq beg (point) end beg)
  5176. (save-excursion
  5177. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5178. (setq beg (point-at-bol 2))
  5179. (setq beg (point))))
  5180. (save-excursion
  5181. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5182. (setq end (1- (match-beginning 0)))
  5183. (setq end (point))))
  5184. (goto-line line))
  5185. (if (get-buffer "*Org Edit Picture*")
  5186. (kill-buffer "*Org Edit Picture*"))
  5187. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5188. "*Org Edit Picture*"))
  5189. (narrow-to-region beg end)
  5190. (remove-text-properties beg end '(display nil invisible nil
  5191. intangible nil))
  5192. (when (fboundp 'font-lock-unfontify-region)
  5193. (font-lock-unfontify-region (point-min) (point-max)))
  5194. (cond
  5195. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5196. (fundamental-mode)
  5197. (artist-mode 1))
  5198. (t (funcall org-edit-fixed-width-region-mode)))
  5199. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5200. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5201. (set (make-local-variable 'org-edit-src-picture) t)
  5202. (goto-char (point-min))
  5203. (while (re-search-forward "^[ \t]*: ?" nil t)
  5204. (replace-match ""))
  5205. (goto-line line)
  5206. (org-exit-edit-mode)
  5207. (org-set-local 'header-line-format msg)
  5208. (message "%s" msg)
  5209. t)))
  5210. (defun org-edit-src-find-region-and-lang ()
  5211. "Find the region and language for a local edit.
  5212. Return a list with beginning and end of the region, a string representing
  5213. the language, a switch telling of the content should be in a single line."
  5214. (let ((re-list
  5215. (append
  5216. org-edit-src-region-extra
  5217. '(
  5218. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5219. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5220. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5221. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5222. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5223. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5224. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5225. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5226. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5227. ("^#\\+html:" "\n" "html" single-line)
  5228. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5229. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5230. ("^#\\+latex:" "\n" "latex" single-line)
  5231. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5232. ("^#\\+ascii:" "\n" "ascii" single-line)
  5233. )))
  5234. (pos (point))
  5235. re re1 re2 single beg end lang)
  5236. (catch 'exit
  5237. (while (setq entry (pop re-list))
  5238. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5239. single (nth 3 entry))
  5240. (save-excursion
  5241. (if (or (looking-at re1)
  5242. (re-search-backward re1 nil t))
  5243. (progn
  5244. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5245. (if (and (re-search-forward re2 nil t)
  5246. (>= (match-end 0) pos))
  5247. (throw 'exit (list beg (match-beginning 0) lang single))))
  5248. (if (or (looking-at re2)
  5249. (re-search-forward re2 nil t))
  5250. (progn
  5251. (setq end (match-beginning 0))
  5252. (if (and (re-search-backward re1 nil t)
  5253. (<= (match-beginning 0) pos))
  5254. (throw 'exit
  5255. (list (match-end 0) end
  5256. (org-edit-src-get-lang lang) single)))))))))))
  5257. (defun org-edit-src-get-lang (lang)
  5258. "Extract the src language."
  5259. (let ((m (match-string 0)))
  5260. (cond
  5261. ((stringp lang) lang)
  5262. ((integerp lang) (match-string lang))
  5263. ((and (eq lang 'lang)
  5264. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5265. (match-string 1 m))
  5266. ((and (eq lang 'style)
  5267. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5268. (match-string 1 m))
  5269. (t "fundamental"))))
  5270. (defun org-edit-src-exit ()
  5271. "Exit special edit and protect problematic lines."
  5272. (interactive)
  5273. (unless (buffer-base-buffer (current-buffer))
  5274. (error "This is not an indirect buffer, something is wrong..."))
  5275. (unless (> (point-min) 1)
  5276. (error "This buffer is not narrowed, something is wrong..."))
  5277. (goto-char (point-min))
  5278. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5279. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5280. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5281. (goto-char (point-min))
  5282. (while (re-search-forward "\n" nil t)
  5283. (replace-match " "))
  5284. (goto-char (point-min))
  5285. (if (looking-at "\\s-*") (replace-match " "))
  5286. (if (re-search-forward "\\s-+\\'" nil t)
  5287. (replace-match "")))
  5288. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5289. (goto-char (point-min))
  5290. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5291. (replace-match ",\\1"))
  5292. (when font-lock-mode
  5293. (font-lock-unfontify-region (point-min) (point-max)))
  5294. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5295. (when (org-bound-and-true-p org-edit-src-picture)
  5296. (untabify (point-min) (point-max))
  5297. (goto-char (point-min))
  5298. (while (re-search-forward "^" nil t)
  5299. (replace-match ": "))
  5300. (when font-lock-mode
  5301. (font-lock-unfontify-region (point-min) (point-max)))
  5302. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5303. (kill-buffer (current-buffer))
  5304. (and (org-mode-p) (org-restart-font-lock)))
  5305. ;;; The orgstruct minor mode
  5306. ;; Define a minor mode which can be used in other modes in order to
  5307. ;; integrate the org-mode structure editing commands.
  5308. ;; This is really a hack, because the org-mode structure commands use
  5309. ;; keys which normally belong to the major mode. Here is how it
  5310. ;; works: The minor mode defines all the keys necessary to operate the
  5311. ;; structure commands, but wraps the commands into a function which
  5312. ;; tests if the cursor is currently at a headline or a plain list
  5313. ;; item. If that is the case, the structure command is used,
  5314. ;; temporarily setting many Org-mode variables like regular
  5315. ;; expressions for filling etc. However, when any of those keys is
  5316. ;; used at a different location, function uses `key-binding' to look
  5317. ;; up if the key has an associated command in another currently active
  5318. ;; keymap (minor modes, major mode, global), and executes that
  5319. ;; command. There might be problems if any of the keys is otherwise
  5320. ;; used as a prefix key.
  5321. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5322. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5323. ;; addresses this by checking explicitly for both bindings.
  5324. (defvar orgstruct-mode-map (make-sparse-keymap)
  5325. "Keymap for the minor `orgstruct-mode'.")
  5326. (defvar org-local-vars nil
  5327. "List of local variables, for use by `orgstruct-mode'")
  5328. ;;;###autoload
  5329. (define-minor-mode orgstruct-mode
  5330. "Toggle the minor more `orgstruct-mode'.
  5331. This mode is for using Org-mode structure commands in other modes.
  5332. The following key behave as if Org-mode was active, if the cursor
  5333. is on a headline, or on a plain list item (both in the definition
  5334. of Org-mode).
  5335. M-up Move entry/item up
  5336. M-down Move entry/item down
  5337. M-left Promote
  5338. M-right Demote
  5339. M-S-up Move entry/item up
  5340. M-S-down Move entry/item down
  5341. M-S-left Promote subtree
  5342. M-S-right Demote subtree
  5343. M-q Fill paragraph and items like in Org-mode
  5344. C-c ^ Sort entries
  5345. C-c - Cycle list bullet
  5346. TAB Cycle item visibility
  5347. M-RET Insert new heading/item
  5348. S-M-RET Insert new TODO heading / Checkbox item
  5349. C-c C-c Set tags / toggle checkbox"
  5350. nil " OrgStruct" nil
  5351. (org-load-modules-maybe)
  5352. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5353. ;;;###autoload
  5354. (defun turn-on-orgstruct ()
  5355. "Unconditionally turn on `orgstruct-mode'."
  5356. (orgstruct-mode 1))
  5357. ;;;###autoload
  5358. (defun turn-on-orgstruct++ ()
  5359. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5360. In addition to setting orgstruct-mode, this also exports all indentation and
  5361. autofilling variables from org-mode into the buffer. Note that turning
  5362. off orgstruct-mode will *not* remove these additional settings."
  5363. (orgstruct-mode 1)
  5364. (let (var val)
  5365. (mapc
  5366. (lambda (x)
  5367. (when (string-match
  5368. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5369. (symbol-name (car x)))
  5370. (setq var (car x) val (nth 1 x))
  5371. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5372. org-local-vars)))
  5373. (defun orgstruct-error ()
  5374. "Error when there is no default binding for a structure key."
  5375. (interactive)
  5376. (error "This key has no function outside structure elements"))
  5377. (defun orgstruct-setup ()
  5378. "Setup orgstruct keymaps."
  5379. (let ((nfunc 0)
  5380. (bindings
  5381. (list
  5382. '([(meta up)] org-metaup)
  5383. '([(meta down)] org-metadown)
  5384. '([(meta left)] org-metaleft)
  5385. '([(meta right)] org-metaright)
  5386. '([(meta shift up)] org-shiftmetaup)
  5387. '([(meta shift down)] org-shiftmetadown)
  5388. '([(meta shift left)] org-shiftmetaleft)
  5389. '([(meta shift right)] org-shiftmetaright)
  5390. '([(shift up)] org-shiftup)
  5391. '([(shift down)] org-shiftdown)
  5392. '([(shift left)] org-shiftleft)
  5393. '([(shift right)] org-shiftright)
  5394. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5395. '("\M-q" fill-paragraph)
  5396. '("\C-c^" org-sort)
  5397. '("\C-c-" org-cycle-list-bullet)))
  5398. elt key fun cmd)
  5399. (while (setq elt (pop bindings))
  5400. (setq nfunc (1+ nfunc))
  5401. (setq key (org-key (car elt))
  5402. fun (nth 1 elt)
  5403. cmd (orgstruct-make-binding fun nfunc key))
  5404. (org-defkey orgstruct-mode-map key cmd))
  5405. ;; Special treatment needed for TAB and RET
  5406. (org-defkey orgstruct-mode-map [(tab)]
  5407. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5408. (org-defkey orgstruct-mode-map "\C-i"
  5409. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5410. (org-defkey orgstruct-mode-map "\M-\C-m"
  5411. (orgstruct-make-binding 'org-insert-heading 105
  5412. "\M-\C-m" [(meta return)]))
  5413. (org-defkey orgstruct-mode-map [(meta return)]
  5414. (orgstruct-make-binding 'org-insert-heading 106
  5415. [(meta return)] "\M-\C-m"))
  5416. (org-defkey orgstruct-mode-map [(shift meta return)]
  5417. (orgstruct-make-binding 'org-insert-todo-heading 107
  5418. [(meta return)] "\M-\C-m"))
  5419. (unless org-local-vars
  5420. (setq org-local-vars (org-get-local-variables)))
  5421. t))
  5422. (defun orgstruct-make-binding (fun n &rest keys)
  5423. "Create a function for binding in the structure minor mode.
  5424. FUN is the command to call inside a table. N is used to create a unique
  5425. command name. KEYS are keys that should be checked in for a command
  5426. to execute outside of tables."
  5427. (eval
  5428. (list 'defun
  5429. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5430. '(arg)
  5431. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5432. "Outside of structure, run the binding of `"
  5433. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5434. "'.")
  5435. '(interactive "p")
  5436. (list 'if
  5437. '(org-context-p 'headline 'item)
  5438. (list 'org-run-like-in-org-mode (list 'quote fun))
  5439. (list 'let '(orgstruct-mode)
  5440. (list 'call-interactively
  5441. (append '(or)
  5442. (mapcar (lambda (k)
  5443. (list 'key-binding k))
  5444. keys)
  5445. '('orgstruct-error))))))))
  5446. (defun org-context-p (&rest contexts)
  5447. "Check if local context is any of CONTEXTS.
  5448. Possible values in the list of contexts are `table', `headline', and `item'."
  5449. (let ((pos (point)))
  5450. (goto-char (point-at-bol))
  5451. (prog1 (or (and (memq 'table contexts)
  5452. (looking-at "[ \t]*|"))
  5453. (and (memq 'headline contexts)
  5454. ;;????????? (looking-at "\\*+"))
  5455. (looking-at outline-regexp))
  5456. (and (memq 'item contexts)
  5457. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5458. (goto-char pos))))
  5459. (defun org-get-local-variables ()
  5460. "Return a list of all local variables in an org-mode buffer."
  5461. (let (varlist)
  5462. (with-current-buffer (get-buffer-create "*Org tmp*")
  5463. (erase-buffer)
  5464. (org-mode)
  5465. (setq varlist (buffer-local-variables)))
  5466. (kill-buffer "*Org tmp*")
  5467. (delq nil
  5468. (mapcar
  5469. (lambda (x)
  5470. (setq x
  5471. (if (symbolp x)
  5472. (list x)
  5473. (list (car x) (list 'quote (cdr x)))))
  5474. (if (string-match
  5475. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5476. (symbol-name (car x)))
  5477. x nil))
  5478. varlist))))
  5479. ;;;###autoload
  5480. (defun org-run-like-in-org-mode (cmd)
  5481. (org-load-modules-maybe)
  5482. (unless org-local-vars
  5483. (setq org-local-vars (org-get-local-variables)))
  5484. (eval (list 'let org-local-vars
  5485. (list 'call-interactively (list 'quote cmd)))))
  5486. ;;;; Archiving
  5487. (defun org-get-category (&optional pos)
  5488. "Get the category applying to position POS."
  5489. (get-text-property (or pos (point)) 'org-category))
  5490. (defun org-refresh-category-properties ()
  5491. "Refresh category text properties in the buffer."
  5492. (let ((def-cat (cond
  5493. ((null org-category)
  5494. (if buffer-file-name
  5495. (file-name-sans-extension
  5496. (file-name-nondirectory buffer-file-name))
  5497. "???"))
  5498. ((symbolp org-category) (symbol-name org-category))
  5499. (t org-category)))
  5500. beg end cat pos optionp)
  5501. (org-unmodified
  5502. (save-excursion
  5503. (save-restriction
  5504. (widen)
  5505. (goto-char (point-min))
  5506. (put-text-property (point) (point-max) 'org-category def-cat)
  5507. (while (re-search-forward
  5508. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5509. (setq pos (match-end 0)
  5510. optionp (equal (char-after (match-beginning 0)) ?#)
  5511. cat (org-trim (match-string 2)))
  5512. (if optionp
  5513. (setq beg (point-at-bol) end (point-max))
  5514. (org-back-to-heading t)
  5515. (setq beg (point) end (org-end-of-subtree t t)))
  5516. (put-text-property beg end 'org-category cat)
  5517. (goto-char pos)))))))
  5518. ;;;; Link Stuff
  5519. ;;; Link abbreviations
  5520. (defun org-link-expand-abbrev (link)
  5521. "Apply replacements as defined in `org-link-abbrev-alist."
  5522. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5523. (let* ((key (match-string 1 link))
  5524. (as (or (assoc key org-link-abbrev-alist-local)
  5525. (assoc key org-link-abbrev-alist)))
  5526. (tag (and (match-end 2) (match-string 3 link)))
  5527. rpl)
  5528. (if (not as)
  5529. link
  5530. (setq rpl (cdr as))
  5531. (cond
  5532. ((symbolp rpl) (funcall rpl tag))
  5533. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5534. ((string-match "%h" rpl)
  5535. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5536. (t (concat rpl tag)))))
  5537. link))
  5538. ;;; Storing and inserting links
  5539. (defvar org-insert-link-history nil
  5540. "Minibuffer history for links inserted with `org-insert-link'.")
  5541. (defvar org-stored-links nil
  5542. "Contains the links stored with `org-store-link'.")
  5543. (defvar org-store-link-plist nil
  5544. "Plist with info about the most recently link created with `org-store-link'.")
  5545. (defvar org-link-protocols nil
  5546. "Link protocols added to Org-mode using `org-add-link-type'.")
  5547. (defvar org-store-link-functions nil
  5548. "List of functions that are called to create and store a link.
  5549. Each function will be called in turn until one returns a non-nil
  5550. value. Each function should check if it is responsible for creating
  5551. this link (for example by looking at the major mode).
  5552. If not, it must exit and return nil.
  5553. If yes, it should return a non-nil value after a calling
  5554. `org-store-link-props' with a list of properties and values.
  5555. Special properties are:
  5556. :type The link prefix. like \"http\". This must be given.
  5557. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5558. This is obligatory as well.
  5559. :description Optional default description for the second pair
  5560. of brackets in an Org-mode link. The user can still change
  5561. this when inserting this link into an Org-mode buffer.
  5562. In addition to these, any additional properties can be specified
  5563. and then used in remember templates.")
  5564. (defun org-add-link-type (type &optional follow export)
  5565. "Add TYPE to the list of `org-link-types'.
  5566. Re-compute all regular expressions depending on `org-link-types'
  5567. FOLLOW and EXPORT are two functions.
  5568. FOLLOW should take the link path as the single argument and do whatever
  5569. is necessary to follow the link, for example find a file or display
  5570. a mail message.
  5571. EXPORT should format the link path for export to one of the export formats.
  5572. It should be a function accepting three arguments:
  5573. path the path of the link, the text after the prefix (like \"http:\")
  5574. desc the description of the link, if any, nil if there was no description
  5575. format the export format, a symbol like `html' or `latex'.
  5576. The function may use the FORMAT information to return different values
  5577. depending on the format. The return value will be put literally into
  5578. the exported file.
  5579. Org-mode has a built-in default for exporting links. If you are happy with
  5580. this default, there is no need to define an export function for the link
  5581. type. For a simple example of an export function, see `org-bbdb.el'."
  5582. (add-to-list 'org-link-types type t)
  5583. (org-make-link-regexps)
  5584. (if (assoc type org-link-protocols)
  5585. (setcdr (assoc type org-link-protocols) (list follow export))
  5586. (push (list type follow export) org-link-protocols)))
  5587. ;;;###autoload
  5588. (defun org-store-link (arg)
  5589. "\\<org-mode-map>Store an org-link to the current location.
  5590. This link is added to `org-stored-links' and can later be inserted
  5591. into an org-buffer with \\[org-insert-link].
  5592. For some link types, a prefix arg is interpreted:
  5593. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5594. For file links, arg negates `org-context-in-file-links'."
  5595. (interactive "P")
  5596. (org-load-modules-maybe)
  5597. (setq org-store-link-plist nil) ; reset
  5598. (let (link cpltxt desc description search txt)
  5599. (cond
  5600. ((run-hook-with-args-until-success 'org-store-link-functions)
  5601. (setq link (plist-get org-store-link-plist :link)
  5602. desc (or (plist-get org-store-link-plist :description) link)))
  5603. ((eq major-mode 'calendar-mode)
  5604. (let ((cd (calendar-cursor-to-date)))
  5605. (setq link
  5606. (format-time-string
  5607. (car org-time-stamp-formats)
  5608. (apply 'encode-time
  5609. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5610. nil nil nil))))
  5611. (org-store-link-props :type "calendar" :date cd)))
  5612. ((eq major-mode 'w3-mode)
  5613. (setq cpltxt (url-view-url t)
  5614. link (org-make-link cpltxt))
  5615. (org-store-link-props :type "w3" :url (url-view-url t)))
  5616. ((eq major-mode 'w3m-mode)
  5617. (setq cpltxt (or w3m-current-title w3m-current-url)
  5618. link (org-make-link w3m-current-url))
  5619. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5620. ((setq search (run-hook-with-args-until-success
  5621. 'org-create-file-search-functions))
  5622. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5623. "::" search))
  5624. (setq cpltxt (or description link)))
  5625. ((eq major-mode 'image-mode)
  5626. (setq cpltxt (concat "file:"
  5627. (abbreviate-file-name buffer-file-name))
  5628. link (org-make-link cpltxt))
  5629. (org-store-link-props :type "image" :file buffer-file-name))
  5630. ((eq major-mode 'dired-mode)
  5631. ;; link to the file in the current line
  5632. (setq cpltxt (concat "file:"
  5633. (abbreviate-file-name
  5634. (expand-file-name
  5635. (dired-get-filename nil t))))
  5636. link (org-make-link cpltxt)))
  5637. ((and buffer-file-name (org-mode-p))
  5638. (cond
  5639. ((org-in-regexp "<<\\(.*?\\)>>")
  5640. (setq cpltxt
  5641. (concat "file:"
  5642. (abbreviate-file-name buffer-file-name)
  5643. "::" (match-string 1))
  5644. link (org-make-link cpltxt)))
  5645. ((and (featurep 'org-id)
  5646. (or (eq org-link-to-org-use-id t)
  5647. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5648. (interactive-p))
  5649. (and org-link-to-org-use-id
  5650. (condition-case nil
  5651. (org-entry-get nil "ID")
  5652. (error nil)))))
  5653. ;; We can make a link using the ID.
  5654. (setq link (condition-case nil
  5655. (prog1 (org-id-store-link)
  5656. (setq desc (plist-get org-store-link-plist
  5657. :description)))
  5658. (error
  5659. ;; probably before first headline, link to file only
  5660. (concat "file:"
  5661. (abbreviate-file-name buffer-file-name))))))
  5662. (t
  5663. ;; Just link to current headline
  5664. (setq cpltxt (concat "file:"
  5665. (abbreviate-file-name buffer-file-name)))
  5666. ;; Add a context search string
  5667. (when (org-xor org-context-in-file-links arg)
  5668. (setq txt (cond
  5669. ((org-on-heading-p) nil)
  5670. ((org-region-active-p)
  5671. (buffer-substring (region-beginning) (region-end)))
  5672. (t nil)))
  5673. (when (or (null txt) (string-match "\\S-" txt))
  5674. (setq cpltxt
  5675. (concat cpltxt "::"
  5676. (condition-case nil
  5677. (org-make-org-heading-search-string txt)
  5678. (error "")))
  5679. desc "NONE")))
  5680. (if (string-match "::\\'" cpltxt)
  5681. (setq cpltxt (substring cpltxt 0 -2)))
  5682. (setq link (org-make-link cpltxt)))))
  5683. ((buffer-file-name (buffer-base-buffer))
  5684. ;; Just link to this file here.
  5685. (setq cpltxt (concat "file:"
  5686. (abbreviate-file-name
  5687. (buffer-file-name (buffer-base-buffer)))))
  5688. ;; Add a context string
  5689. (when (org-xor org-context-in-file-links arg)
  5690. (setq txt (if (org-region-active-p)
  5691. (buffer-substring (region-beginning) (region-end))
  5692. (buffer-substring (point-at-bol) (point-at-eol))))
  5693. ;; Only use search option if there is some text.
  5694. (when (string-match "\\S-" txt)
  5695. (setq cpltxt
  5696. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5697. desc "NONE")))
  5698. (setq link (org-make-link cpltxt)))
  5699. ((interactive-p)
  5700. (error "Cannot link to a buffer which is not visiting a file"))
  5701. (t (setq link nil)))
  5702. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5703. (setq link (or link cpltxt)
  5704. desc (or desc cpltxt))
  5705. (if (equal desc "NONE") (setq desc nil))
  5706. (if (and (interactive-p) link)
  5707. (progn
  5708. (setq org-stored-links
  5709. (cons (list link desc) org-stored-links))
  5710. (message "Stored: %s" (or desc link)))
  5711. (and link (org-make-link-string link desc)))))
  5712. (defun org-store-link-props (&rest plist)
  5713. "Store link properties, extract names and addresses."
  5714. (let (x adr)
  5715. (when (setq x (plist-get plist :from))
  5716. (setq adr (mail-extract-address-components x))
  5717. (setq plist (plist-put plist :fromname (car adr)))
  5718. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5719. (when (setq x (plist-get plist :to))
  5720. (setq adr (mail-extract-address-components x))
  5721. (setq plist (plist-put plist :toname (car adr)))
  5722. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5723. (let ((from (plist-get plist :from))
  5724. (to (plist-get plist :to)))
  5725. (when (and from to org-from-is-user-regexp)
  5726. (setq plist
  5727. (plist-put plist :fromto
  5728. (if (string-match org-from-is-user-regexp from)
  5729. (concat "to %t")
  5730. (concat "from %f"))))))
  5731. (setq org-store-link-plist plist))
  5732. (defun org-add-link-props (&rest plist)
  5733. "Add these properties to the link property list."
  5734. (let (key value)
  5735. (while plist
  5736. (setq key (pop plist) value (pop plist))
  5737. (setq org-store-link-plist
  5738. (plist-put org-store-link-plist key value)))))
  5739. (defun org-email-link-description (&optional fmt)
  5740. "Return the description part of an email link.
  5741. This takes information from `org-store-link-plist' and formats it
  5742. according to FMT (default from `org-email-link-description-format')."
  5743. (setq fmt (or fmt org-email-link-description-format))
  5744. (let* ((p org-store-link-plist)
  5745. (to (plist-get p :toaddress))
  5746. (from (plist-get p :fromaddress))
  5747. (table
  5748. (list
  5749. (cons "%c" (plist-get p :fromto))
  5750. (cons "%F" (plist-get p :from))
  5751. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5752. (cons "%T" (plist-get p :to))
  5753. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5754. (cons "%s" (plist-get p :subject))
  5755. (cons "%m" (plist-get p :message-id)))))
  5756. (when (string-match "%c" fmt)
  5757. ;; Check if the user wrote this message
  5758. (if (and org-from-is-user-regexp from to
  5759. (save-match-data (string-match org-from-is-user-regexp from)))
  5760. (setq fmt (replace-match "to %t" t t fmt))
  5761. (setq fmt (replace-match "from %f" t t fmt))))
  5762. (org-replace-escapes fmt table)))
  5763. (defun org-make-org-heading-search-string (&optional string heading)
  5764. "Make search string for STRING or current headline."
  5765. (interactive)
  5766. (let ((s (or string (org-get-heading))))
  5767. (unless (and string (not heading))
  5768. ;; We are using a headline, clean up garbage in there.
  5769. (if (string-match org-todo-regexp s)
  5770. (setq s (replace-match "" t t s)))
  5771. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5772. (setq s (replace-match "" t t s)))
  5773. (setq s (org-trim s))
  5774. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5775. org-comment-string "\\)") s)
  5776. (setq s (replace-match "" t t s)))
  5777. (while (string-match org-ts-regexp s)
  5778. (setq s (replace-match "" t t s))))
  5779. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5780. (setq s (replace-match " " t t s)))
  5781. (or string (setq s (concat "*" s))) ; Add * for headlines
  5782. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5783. (defun org-make-link (&rest strings)
  5784. "Concatenate STRINGS."
  5785. (apply 'concat strings))
  5786. (defun org-make-link-string (link &optional description)
  5787. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5788. (unless (string-match "\\S-" link)
  5789. (error "Empty link"))
  5790. (when (stringp description)
  5791. ;; Remove brackets from the description, they are fatal.
  5792. (while (string-match "\\[" description)
  5793. (setq description (replace-match "{" t t description)))
  5794. (while (string-match "\\]" description)
  5795. (setq description (replace-match "}" t t description))))
  5796. (when (equal (org-link-escape link) description)
  5797. ;; No description needed, it is identical
  5798. (setq description nil))
  5799. (when (and (not description)
  5800. (not (equal link (org-link-escape link))))
  5801. (setq description (org-extract-attributes link)))
  5802. (concat "[[" (org-link-escape link) "]"
  5803. (if description (concat "[" description "]") "")
  5804. "]"))
  5805. (defconst org-link-escape-chars
  5806. '((?\ . "%20")
  5807. (?\[ . "%5B")
  5808. (?\] . "%5D")
  5809. (?\340 . "%E0") ; `a
  5810. (?\342 . "%E2") ; ^a
  5811. (?\347 . "%E7") ; ,c
  5812. (?\350 . "%E8") ; `e
  5813. (?\351 . "%E9") ; 'e
  5814. (?\352 . "%EA") ; ^e
  5815. (?\356 . "%EE") ; ^i
  5816. (?\364 . "%F4") ; ^o
  5817. (?\371 . "%F9") ; `u
  5818. (?\373 . "%FB") ; ^u
  5819. (?\; . "%3B")
  5820. (?? . "%3F")
  5821. (?= . "%3D")
  5822. (?+ . "%2B")
  5823. )
  5824. "Association list of escapes for some characters problematic in links.
  5825. This is the list that is used for internal purposes.")
  5826. (defconst org-link-escape-chars-browser
  5827. '((?\ . "%20")) ; 32 for the SPC char
  5828. "Association list of escapes for some characters problematic in links.
  5829. This is the list that is used before handing over to the browser.")
  5830. (defun org-link-escape (text &optional table)
  5831. "Escape characters in TEXT that are problematic for links."
  5832. (setq table (or table org-link-escape-chars))
  5833. (when text
  5834. (let ((re (mapconcat (lambda (x) (regexp-quote
  5835. (char-to-string (car x))))
  5836. table "\\|")))
  5837. (while (string-match re text)
  5838. (setq text
  5839. (replace-match
  5840. (cdr (assoc (string-to-char (match-string 0 text))
  5841. table))
  5842. t t text)))
  5843. text)))
  5844. (defun org-link-unescape (text &optional table)
  5845. "Reverse the action of `org-link-escape'."
  5846. (setq table (or table org-link-escape-chars))
  5847. (when text
  5848. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5849. table "\\|")))
  5850. (while (string-match re text)
  5851. (setq text
  5852. (replace-match
  5853. (char-to-string (car (rassoc (match-string 0 text) table)))
  5854. t t text)))
  5855. text)))
  5856. (defun org-xor (a b)
  5857. "Exclusive or."
  5858. (if a (not b) b))
  5859. (defun org-get-header (header)
  5860. "Find a header field in the current buffer."
  5861. (save-excursion
  5862. (goto-char (point-min))
  5863. (let ((case-fold-search t) s)
  5864. (cond
  5865. ((eq header 'from)
  5866. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5867. (setq s (match-string 1)))
  5868. (while (string-match "\"" s)
  5869. (setq s (replace-match "" t t s)))
  5870. (if (string-match "[<(].*" s)
  5871. (setq s (replace-match "" t t s))))
  5872. ((eq header 'message-id)
  5873. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5874. (setq s (match-string 1))))
  5875. ((eq header 'subject)
  5876. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5877. (setq s (match-string 1)))))
  5878. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5879. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5880. s)))
  5881. (defun org-fixup-message-id-for-http (s)
  5882. "Replace special characters in a message id, so it can be used in an http query."
  5883. (while (string-match "<" s)
  5884. (setq s (replace-match "%3C" t t s)))
  5885. (while (string-match ">" s)
  5886. (setq s (replace-match "%3E" t t s)))
  5887. (while (string-match "@" s)
  5888. (setq s (replace-match "%40" t t s)))
  5889. s)
  5890. ;;;###autoload
  5891. (defun org-insert-link-global ()
  5892. "Insert a link like Org-mode does.
  5893. This command can be called in any mode to insert a link in Org-mode syntax."
  5894. (interactive)
  5895. (org-load-modules-maybe)
  5896. (org-run-like-in-org-mode 'org-insert-link))
  5897. (defun org-insert-link (&optional complete-file link-location)
  5898. "Insert a link. At the prompt, enter the link.
  5899. Completion can be used to insert any of the link protocol prefixes like
  5900. http or ftp in use.
  5901. The history can be used to select a link previously stored with
  5902. `org-store-link'. When the empty string is entered (i.e. if you just
  5903. press RET at the prompt), the link defaults to the most recently
  5904. stored link. As SPC triggers completion in the minibuffer, you need to
  5905. use M-SPC or C-q SPC to force the insertion of a space character.
  5906. You will also be prompted for a description, and if one is given, it will
  5907. be displayed in the buffer instead of the link.
  5908. If there is already a link at point, this command will allow you to edit link
  5909. and description parts.
  5910. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5911. be selected using completion. The path to the file will be relative to the
  5912. current directory if the file is in the current directory or a subdirectory.
  5913. Otherwise, the link will be the absolute path as completed in the minibuffer
  5914. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5915. option `org-link-file-path-type'.
  5916. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5917. the current directory or below.
  5918. With three \\[universal-argument] prefixes, negate the meaning of
  5919. `org-keep-stored-link-after-insertion'.
  5920. If `org-make-link-description-function' is non-nil, this function will be
  5921. called with the link target, and the result will be the default
  5922. link description.
  5923. If the LINK-LOCATION parameter is non-nil, this value will be
  5924. used as the link location instead of reading one interactively."
  5925. (interactive "P")
  5926. (let* ((wcf (current-window-configuration))
  5927. (region (if (org-region-active-p)
  5928. (buffer-substring (region-beginning) (region-end))))
  5929. (remove (and region (list (region-beginning) (region-end))))
  5930. (desc region)
  5931. tmphist ; byte-compile incorrectly complains about this
  5932. (link link-location)
  5933. entry file)
  5934. (cond
  5935. (link-location) ; specified by arg, just use it.
  5936. ((org-in-regexp org-bracket-link-regexp 1)
  5937. ;; We do have a link at point, and we are going to edit it.
  5938. (setq remove (list (match-beginning 0) (match-end 0)))
  5939. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5940. (setq link (read-string "Link: "
  5941. (org-link-unescape
  5942. (org-match-string-no-properties 1)))))
  5943. ((or (org-in-regexp org-angle-link-re)
  5944. (org-in-regexp org-plain-link-re))
  5945. ;; Convert to bracket link
  5946. (setq remove (list (match-beginning 0) (match-end 0))
  5947. link (read-string "Link: "
  5948. (org-remove-angle-brackets (match-string 0)))))
  5949. ((member complete-file '((4) (16)))
  5950. ;; Completing read for file names.
  5951. (setq file (read-file-name "File: "))
  5952. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5953. (pwd1 (file-name-as-directory (abbreviate-file-name
  5954. (expand-file-name ".")))))
  5955. (cond
  5956. ((equal complete-file '(16))
  5957. (setq link (org-make-link
  5958. "file:"
  5959. (abbreviate-file-name (expand-file-name file)))))
  5960. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5961. (setq link (org-make-link "file:" (match-string 1 file))))
  5962. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5963. (expand-file-name file))
  5964. (setq link (org-make-link
  5965. "file:" (match-string 1 (expand-file-name file)))))
  5966. (t (setq link (org-make-link "file:" file))))))
  5967. (t
  5968. ;; Read link, with completion for stored links.
  5969. (with-output-to-temp-buffer "*Org Links*"
  5970. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5971. (when org-stored-links
  5972. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5973. (princ (mapconcat
  5974. (lambda (x)
  5975. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5976. (reverse org-stored-links) "\n"))))
  5977. (let ((cw (selected-window)))
  5978. (select-window (get-buffer-window "*Org Links*"))
  5979. (org-fit-window-to-buffer)
  5980. (setq truncate-lines t)
  5981. (select-window cw))
  5982. ;; Fake a link history, containing the stored links.
  5983. (setq tmphist (append (mapcar 'car org-stored-links)
  5984. org-insert-link-history))
  5985. (unwind-protect
  5986. (setq link (org-completing-read
  5987. "Link: "
  5988. (append
  5989. (mapcar (lambda (x) (list (concat (car x) ":")))
  5990. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5991. (mapcar (lambda (x) (list (concat x ":")))
  5992. org-link-types))
  5993. nil nil nil
  5994. 'tmphist
  5995. (or (car (car org-stored-links)))))
  5996. (set-window-configuration wcf)
  5997. (kill-buffer "*Org Links*"))
  5998. (setq entry (assoc link org-stored-links))
  5999. (or entry (push link org-insert-link-history))
  6000. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6001. (not org-keep-stored-link-after-insertion))
  6002. (setq org-stored-links (delq (assoc link org-stored-links)
  6003. org-stored-links)))
  6004. (setq desc (or desc (nth 1 entry)))))
  6005. (if (string-match org-plain-link-re link)
  6006. ;; URL-like link, normalize the use of angular brackets.
  6007. (setq link (org-make-link (org-remove-angle-brackets link))))
  6008. ;; Check if we are linking to the current file with a search option
  6009. ;; If yes, simplify the link by using only the search option.
  6010. (when (and buffer-file-name
  6011. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6012. (let* ((path (match-string 1 link))
  6013. (case-fold-search nil)
  6014. (search (match-string 2 link)))
  6015. (save-match-data
  6016. (if (equal (file-truename buffer-file-name) (file-truename path))
  6017. ;; We are linking to this same file, with a search option
  6018. (setq link search)))))
  6019. ;; Check if we can/should use a relative path. If yes, simplify the link
  6020. (when (string-match "^file:\\(.*\\)" link)
  6021. (let* ((path (match-string 1 link))
  6022. (origpath path)
  6023. (case-fold-search nil))
  6024. (cond
  6025. ((or (eq org-link-file-path-type 'absolute)
  6026. (equal complete-file '(16)))
  6027. (setq path (abbreviate-file-name (expand-file-name path))))
  6028. ((eq org-link-file-path-type 'noabbrev)
  6029. (setq path (expand-file-name path)))
  6030. ((eq org-link-file-path-type 'relative)
  6031. (setq path (file-relative-name path)))
  6032. (t
  6033. (save-match-data
  6034. (if (string-match (concat "^" (regexp-quote
  6035. (file-name-as-directory
  6036. (expand-file-name "."))))
  6037. (expand-file-name path))
  6038. ;; We are linking a file with relative path name.
  6039. (setq path (substring (expand-file-name path)
  6040. (match-end 0)))
  6041. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6042. (setq link (concat "file:" path))
  6043. (if (equal desc origpath)
  6044. (setq desc path))))
  6045. (if org-make-link-description-function
  6046. (setq desc (funcall org-make-link-description-function link desc)))
  6047. (setq desc (read-string "Description: " desc))
  6048. (unless (string-match "\\S-" desc) (setq desc nil))
  6049. (if remove (apply 'delete-region remove))
  6050. (insert (org-make-link-string link desc))))
  6051. (defun org-completing-read (&rest args)
  6052. "Completing-read with SPACE being a normal character."
  6053. (let ((minibuffer-local-completion-map
  6054. (copy-keymap minibuffer-local-completion-map)))
  6055. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6056. (apply 'org-ido-completing-read args)))
  6057. (defun org-ido-completing-read (&rest args)
  6058. "Completing-read using `ido-mode' speedups if available"
  6059. (if (and org-completion-use-ido
  6060. (fboundp 'ido-completing-read)
  6061. (boundp 'ido-mode) ido-mode
  6062. (listp (second args)))
  6063. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6064. (apply 'completing-read args)))
  6065. (defun org-extract-attributes (s)
  6066. "Extract the attributes cookie from a string and set as text property."
  6067. (let (a attr (start 0) key value)
  6068. (save-match-data
  6069. (when (string-match "{{\\([^}]+\\)}}$" s)
  6070. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6071. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6072. (setq key (match-string 1 a) value (match-string 2 a)
  6073. start (match-end 0)
  6074. attr (plist-put attr (intern key) value))))
  6075. (org-add-props s nil 'org-attr attr))
  6076. s))
  6077. (defun org-attributes-to-string (plist)
  6078. "Format a property list into an HTML attribute list."
  6079. (let ((s "") key value)
  6080. (while plist
  6081. (setq key (pop plist) value (pop plist))
  6082. (and value
  6083. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6084. s))
  6085. ;;; Opening/following a link
  6086. (defvar org-link-search-failed nil)
  6087. (defun org-next-link ()
  6088. "Move forward to the next link.
  6089. If the link is in hidden text, expose it."
  6090. (interactive)
  6091. (when (and org-link-search-failed (eq this-command last-command))
  6092. (goto-char (point-min))
  6093. (message "Link search wrapped back to beginning of buffer"))
  6094. (setq org-link-search-failed nil)
  6095. (let* ((pos (point))
  6096. (ct (org-context))
  6097. (a (assoc :link ct)))
  6098. (if a (goto-char (nth 2 a)))
  6099. (if (re-search-forward org-any-link-re nil t)
  6100. (progn
  6101. (goto-char (match-beginning 0))
  6102. (if (org-invisible-p) (org-show-context)))
  6103. (goto-char pos)
  6104. (setq org-link-search-failed t)
  6105. (error "No further link found"))))
  6106. (defun org-previous-link ()
  6107. "Move backward to the previous link.
  6108. If the link is in hidden text, expose it."
  6109. (interactive)
  6110. (when (and org-link-search-failed (eq this-command last-command))
  6111. (goto-char (point-max))
  6112. (message "Link search wrapped back to end of buffer"))
  6113. (setq org-link-search-failed nil)
  6114. (let* ((pos (point))
  6115. (ct (org-context))
  6116. (a (assoc :link ct)))
  6117. (if a (goto-char (nth 1 a)))
  6118. (if (re-search-backward org-any-link-re nil t)
  6119. (progn
  6120. (goto-char (match-beginning 0))
  6121. (if (org-invisible-p) (org-show-context)))
  6122. (goto-char pos)
  6123. (setq org-link-search-failed t)
  6124. (error "No further link found"))))
  6125. (defun org-translate-link (s)
  6126. "Translate a link string if a translation function has been defined."
  6127. (if (and org-link-translation-function
  6128. (fboundp org-link-translation-function)
  6129. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6130. (progn
  6131. (setq s (funcall org-link-translation-function
  6132. (match-string 1) (match-string 2)))
  6133. (concat (car s) ":" (cdr s)))
  6134. s))
  6135. (defun org-translate-link-from-planner (type path)
  6136. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6137. This is still an experimental function, your mileage may vary."
  6138. (cond
  6139. ((member type '("http" "https" "news" "ftp"))
  6140. ;; standard Internet links are the same.
  6141. nil)
  6142. ((and (equal type "irc") (string-match "^//" path))
  6143. ;; Planner has two / at the beginning of an irc link, we have 1.
  6144. ;; We should have zero, actually....
  6145. (setq path (substring path 1)))
  6146. ((and (equal type "lisp") (string-match "^/" path))
  6147. ;; Planner has a slash, we do not.
  6148. (setq type "elisp" path (substring path 1)))
  6149. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6150. ;; A typical message link. Planner has the id after the fina slash,
  6151. ;; we separate it with a hash mark
  6152. (setq path (concat (match-string 1 path) "#"
  6153. (org-remove-angle-brackets (match-string 2 path)))))
  6154. )
  6155. (cons type path))
  6156. (defun org-find-file-at-mouse (ev)
  6157. "Open file link or URL at mouse."
  6158. (interactive "e")
  6159. (mouse-set-point ev)
  6160. (org-open-at-point 'in-emacs))
  6161. (defun org-open-at-mouse (ev)
  6162. "Open file link or URL at mouse."
  6163. (interactive "e")
  6164. (mouse-set-point ev)
  6165. (if (eq major-mode 'org-agenda-mode)
  6166. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6167. (org-open-at-point))
  6168. (defvar org-window-config-before-follow-link nil
  6169. "The window configuration before following a link.
  6170. This is saved in case the need arises to restore it.")
  6171. (defvar org-open-link-marker (make-marker)
  6172. "Marker pointing to the location where `org-open-at-point; was called.")
  6173. ;;;###autoload
  6174. (defun org-open-at-point-global ()
  6175. "Follow a link like Org-mode does.
  6176. This command can be called in any mode to follow a link that has
  6177. Org-mode syntax."
  6178. (interactive)
  6179. (org-run-like-in-org-mode 'org-open-at-point))
  6180. ;;;###autoload
  6181. (defun org-open-link-from-string (s &optional arg)
  6182. "Open a link in the string S, as if it was in Org-mode."
  6183. (interactive "sLink: \nP")
  6184. (with-temp-buffer
  6185. (let ((org-inhibit-startup t))
  6186. (org-mode)
  6187. (insert s)
  6188. (goto-char (point-min))
  6189. (org-open-at-point arg))))
  6190. (defun org-open-at-point (&optional in-emacs)
  6191. "Open link at or after point.
  6192. If there is no link at point, this function will search forward up to
  6193. the end of the current subtree.
  6194. Normally, files will be opened by an appropriate application. If the
  6195. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6196. With a double prefix argument, try to open outside of Emacs, in the
  6197. application the system uses for this file type."
  6198. (interactive "P")
  6199. (org-load-modules-maybe)
  6200. (move-marker org-open-link-marker (point))
  6201. (setq org-window-config-before-follow-link (current-window-configuration))
  6202. (org-remove-occur-highlights nil nil t)
  6203. (if (org-at-timestamp-p t)
  6204. (org-follow-timestamp-link)
  6205. (let (type path link line search (pos (point)))
  6206. (catch 'match
  6207. (save-excursion
  6208. (skip-chars-forward "^]\n\r")
  6209. (when (org-in-regexp org-bracket-link-regexp)
  6210. (setq link (org-extract-attributes
  6211. (org-link-unescape (org-match-string-no-properties 1))))
  6212. (while (string-match " *\n *" link)
  6213. (setq link (replace-match " " t t link)))
  6214. (setq link (org-link-expand-abbrev link))
  6215. (cond
  6216. ((or (file-name-absolute-p link)
  6217. (string-match "^\\.\\.?/" link))
  6218. (setq type "file" path link))
  6219. ((string-match org-link-re-with-space3 link)
  6220. (setq type (match-string 1 link) path (match-string 2 link)))
  6221. (t (setq type "thisfile" path link)))
  6222. (throw 'match t)))
  6223. (when (get-text-property (point) 'org-linked-text)
  6224. (setq type "thisfile"
  6225. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6226. (1+ (point)) (point))
  6227. path (buffer-substring
  6228. (previous-single-property-change pos 'org-linked-text)
  6229. (next-single-property-change pos 'org-linked-text)))
  6230. (throw 'match t))
  6231. (save-excursion
  6232. (when (or (org-in-regexp org-angle-link-re)
  6233. (org-in-regexp org-plain-link-re))
  6234. (setq type (match-string 1) path (match-string 2))
  6235. (throw 'match t)))
  6236. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6237. (setq type "tree-match"
  6238. path (match-string 1))
  6239. (throw 'match t))
  6240. (save-excursion
  6241. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6242. (setq type "tags"
  6243. path (match-string 1))
  6244. (while (string-match ":" path)
  6245. (setq path (replace-match "+" t t path)))
  6246. (throw 'match t))))
  6247. (unless path
  6248. (error "No link found"))
  6249. ;; Remove any trailing spaces in path
  6250. (if (string-match " +\\'" path)
  6251. (setq path (replace-match "" t t path)))
  6252. (if (and org-link-translation-function
  6253. (fboundp org-link-translation-function))
  6254. ;; Check if we need to translate the link
  6255. (let ((tmp (funcall org-link-translation-function type path)))
  6256. (setq type (car tmp) path (cdr tmp))))
  6257. (cond
  6258. ((assoc type org-link-protocols)
  6259. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6260. ((equal type "mailto")
  6261. (let ((cmd (car org-link-mailto-program))
  6262. (args (cdr org-link-mailto-program)) args1
  6263. (address path) (subject "") a)
  6264. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6265. (setq address (match-string 1 path)
  6266. subject (org-link-escape (match-string 2 path))))
  6267. (while args
  6268. (cond
  6269. ((not (stringp (car args))) (push (pop args) args1))
  6270. (t (setq a (pop args))
  6271. (if (string-match "%a" a)
  6272. (setq a (replace-match address t t a)))
  6273. (if (string-match "%s" a)
  6274. (setq a (replace-match subject t t a)))
  6275. (push a args1))))
  6276. (apply cmd (nreverse args1))))
  6277. ((member type '("http" "https" "ftp" "news"))
  6278. (browse-url (concat type ":" (org-link-escape
  6279. path org-link-escape-chars-browser))))
  6280. ((member type '("message"))
  6281. (browse-url (concat type ":" path)))
  6282. ((string= type "tags")
  6283. (org-tags-view in-emacs path))
  6284. ((string= type "thisfile")
  6285. (if in-emacs
  6286. (switch-to-buffer-other-window
  6287. (org-get-buffer-for-internal-link (current-buffer)))
  6288. (org-mark-ring-push))
  6289. (let ((cmd `(org-link-search
  6290. ,path
  6291. ,(cond ((equal in-emacs '(4)) 'occur)
  6292. ((equal in-emacs '(16)) 'org-occur)
  6293. (t nil))
  6294. ,pos)))
  6295. (condition-case nil (eval cmd)
  6296. (error (progn (widen) (eval cmd))))))
  6297. ((string= type "tree-match")
  6298. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6299. ((string= type "file")
  6300. (if (string-match "::\\([0-9]+\\)\\'" path)
  6301. (setq line (string-to-number (match-string 1 path))
  6302. path (substring path 0 (match-beginning 0)))
  6303. (if (string-match "::\\(.+\\)\\'" path)
  6304. (setq search (match-string 1 path)
  6305. path (substring path 0 (match-beginning 0)))))
  6306. (if (string-match "[*?{]" (file-name-nondirectory path))
  6307. (dired path)
  6308. (org-open-file path in-emacs line search)))
  6309. ((string= type "news")
  6310. (require 'org-gnus)
  6311. (org-gnus-follow-link path))
  6312. ((string= type "shell")
  6313. (let ((cmd path))
  6314. (if (or (not org-confirm-shell-link-function)
  6315. (funcall org-confirm-shell-link-function
  6316. (format "Execute \"%s\" in shell? "
  6317. (org-add-props cmd nil
  6318. 'face 'org-warning))))
  6319. (progn
  6320. (message "Executing %s" cmd)
  6321. (shell-command cmd))
  6322. (error "Abort"))))
  6323. ((string= type "elisp")
  6324. (let ((cmd path))
  6325. (if (or (not org-confirm-elisp-link-function)
  6326. (funcall org-confirm-elisp-link-function
  6327. (format "Execute \"%s\" as elisp? "
  6328. (org-add-props cmd nil
  6329. 'face 'org-warning))))
  6330. (message "%s => %s" cmd
  6331. (if (equal (string-to-char cmd) ?\()
  6332. (eval (read cmd))
  6333. (call-interactively (read cmd))))
  6334. (error "Abort"))))
  6335. (t
  6336. (browse-url-at-point)))))
  6337. (move-marker org-open-link-marker nil)
  6338. (run-hook-with-args 'org-follow-link-hook))
  6339. ;;;; Time estimates
  6340. (defun org-get-effort (&optional pom)
  6341. "Get the effort estimate for the current entry."
  6342. (org-entry-get pom org-effort-property))
  6343. ;;; File search
  6344. (defvar org-create-file-search-functions nil
  6345. "List of functions to construct the right search string for a file link.
  6346. These functions are called in turn with point at the location to
  6347. which the link should point.
  6348. A function in the hook should first test if it would like to
  6349. handle this file type, for example by checking the major-mode or
  6350. the file extension. If it decides not to handle this file, it
  6351. should just return nil to give other functions a chance. If it
  6352. does handle the file, it must return the search string to be used
  6353. when following the link. The search string will be part of the
  6354. file link, given after a double colon, and `org-open-at-point'
  6355. will automatically search for it. If special measures must be
  6356. taken to make the search successful, another function should be
  6357. added to the companion hook `org-execute-file-search-functions',
  6358. which see.
  6359. A function in this hook may also use `setq' to set the variable
  6360. `description' to provide a suggestion for the descriptive text to
  6361. be used for this link when it gets inserted into an Org-mode
  6362. buffer with \\[org-insert-link].")
  6363. (defvar org-execute-file-search-functions nil
  6364. "List of functions to execute a file search triggered by a link.
  6365. Functions added to this hook must accept a single argument, the
  6366. search string that was part of the file link, the part after the
  6367. double colon. The function must first check if it would like to
  6368. handle this search, for example by checking the major-mode or the
  6369. file extension. If it decides not to handle this search, it
  6370. should just return nil to give other functions a chance. If it
  6371. does handle the search, it must return a non-nil value to keep
  6372. other functions from trying.
  6373. Each function can access the current prefix argument through the
  6374. variable `current-prefix-argument'. Note that a single prefix is
  6375. used to force opening a link in Emacs, so it may be good to only
  6376. use a numeric or double prefix to guide the search function.
  6377. In case this is needed, a function in this hook can also restore
  6378. the window configuration before `org-open-at-point' was called using:
  6379. (set-window-configuration org-window-config-before-follow-link)")
  6380. (defun org-link-search (s &optional type avoid-pos)
  6381. "Search for a link search option.
  6382. If S is surrounded by forward slashes, it is interpreted as a
  6383. regular expression. In org-mode files, this will create an `org-occur'
  6384. sparse tree. In ordinary files, `occur' will be used to list matches.
  6385. If the current buffer is in `dired-mode', grep will be used to search
  6386. in all files. If AVOID-POS is given, ignore matches near that position."
  6387. (let ((case-fold-search t)
  6388. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6389. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6390. (append '(("") (" ") ("\t") ("\n"))
  6391. org-emphasis-alist)
  6392. "\\|") "\\)"))
  6393. (pos (point))
  6394. (pre nil) (post nil)
  6395. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6396. (cond
  6397. ;; First check if there are any special
  6398. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6399. ;; Now try the builtin stuff
  6400. ((save-excursion
  6401. (goto-char (point-min))
  6402. (and
  6403. (re-search-forward
  6404. (concat "<<" (regexp-quote s0) ">>") nil t)
  6405. (setq type 'dedicated
  6406. pos (match-beginning 0))))
  6407. ;; There is an exact target for this
  6408. (goto-char pos))
  6409. ((string-match "^/\\(.*\\)/$" s)
  6410. ;; A regular expression
  6411. (cond
  6412. ((org-mode-p)
  6413. (org-occur (match-string 1 s)))
  6414. ;;((eq major-mode 'dired-mode)
  6415. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6416. (t (org-do-occur (match-string 1 s)))))
  6417. (t
  6418. ;; A normal search strings
  6419. (when (equal (string-to-char s) ?*)
  6420. ;; Anchor on headlines, post may include tags.
  6421. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6422. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6423. s (substring s 1)))
  6424. (remove-text-properties
  6425. 0 (length s)
  6426. '(face nil mouse-face nil keymap nil fontified nil) s)
  6427. ;; Make a series of regular expressions to find a match
  6428. (setq words (org-split-string s "[ \n\r\t]+")
  6429. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6430. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6431. "\\)" markers)
  6432. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6433. re2a (concat "[ \t\r\n]" re2a_)
  6434. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6435. re4 (concat "[^a-zA-Z_]" re4_)
  6436. re1 (concat pre re2 post)
  6437. re3 (concat pre (if pre re4_ re4) post)
  6438. re5 (concat pre ".*" re4)
  6439. re2 (concat pre re2)
  6440. re2a (concat pre (if pre re2a_ re2a))
  6441. re4 (concat pre (if pre re4_ re4))
  6442. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6443. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6444. re5 "\\)"
  6445. ))
  6446. (cond
  6447. ((eq type 'org-occur) (org-occur reall))
  6448. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6449. (t (goto-char (point-min))
  6450. (setq type 'fuzzy)
  6451. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6452. (org-search-not-self 1 re1 nil t)
  6453. (org-search-not-self 1 re2 nil t)
  6454. (org-search-not-self 1 re2a nil t)
  6455. (org-search-not-self 1 re3 nil t)
  6456. (org-search-not-self 1 re4 nil t)
  6457. (org-search-not-self 1 re5 nil t)
  6458. )
  6459. (goto-char (match-beginning 1))
  6460. (goto-char pos)
  6461. (error "No match")))))
  6462. (t
  6463. ;; Normal string-search
  6464. (goto-char (point-min))
  6465. (if (search-forward s nil t)
  6466. (goto-char (match-beginning 0))
  6467. (error "No match"))))
  6468. (and (org-mode-p) (org-show-context 'link-search))
  6469. type))
  6470. (defun org-search-not-self (group &rest args)
  6471. "Execute `re-search-forward', but only accept matches that do not
  6472. enclose the position of `org-open-link-marker'."
  6473. (let ((m org-open-link-marker))
  6474. (catch 'exit
  6475. (while (apply 're-search-forward args)
  6476. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6477. (goto-char (match-end group))
  6478. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6479. (> (match-beginning 0) (marker-position m))
  6480. (< (match-end 0) (marker-position m)))
  6481. (save-match-data
  6482. (or (not (org-in-regexp
  6483. org-bracket-link-analytic-regexp 1))
  6484. (not (match-end 4)) ; no description
  6485. (and (<= (match-beginning 4) (point))
  6486. (>= (match-end 4) (point))))))
  6487. (throw 'exit (point))))))))
  6488. (defun org-get-buffer-for-internal-link (buffer)
  6489. "Return a buffer to be used for displaying the link target of internal links."
  6490. (cond
  6491. ((not org-display-internal-link-with-indirect-buffer)
  6492. buffer)
  6493. ((string-match "(Clone)$" (buffer-name buffer))
  6494. (message "Buffer is already a clone, not making another one")
  6495. ;; we also do not modify visibility in this case
  6496. buffer)
  6497. (t ; make a new indirect buffer for displaying the link
  6498. (let* ((bn (buffer-name buffer))
  6499. (ibn (concat bn "(Clone)"))
  6500. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6501. (with-current-buffer ib (org-overview))
  6502. ib))))
  6503. (defun org-do-occur (regexp &optional cleanup)
  6504. "Call the Emacs command `occur'.
  6505. If CLEANUP is non-nil, remove the printout of the regular expression
  6506. in the *Occur* buffer. This is useful if the regex is long and not useful
  6507. to read."
  6508. (occur regexp)
  6509. (when cleanup
  6510. (let ((cwin (selected-window)) win beg end)
  6511. (when (setq win (get-buffer-window "*Occur*"))
  6512. (select-window win))
  6513. (goto-char (point-min))
  6514. (when (re-search-forward "match[a-z]+" nil t)
  6515. (setq beg (match-end 0))
  6516. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6517. (setq end (1- (match-beginning 0)))))
  6518. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6519. (goto-char (point-min))
  6520. (select-window cwin))))
  6521. ;;; The mark ring for links jumps
  6522. (defvar org-mark-ring nil
  6523. "Mark ring for positions before jumps in Org-mode.")
  6524. (defvar org-mark-ring-last-goto nil
  6525. "Last position in the mark ring used to go back.")
  6526. ;; Fill and close the ring
  6527. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6528. (loop for i from 1 to org-mark-ring-length do
  6529. (push (make-marker) org-mark-ring))
  6530. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6531. org-mark-ring)
  6532. (defun org-mark-ring-push (&optional pos buffer)
  6533. "Put the current position or POS into the mark ring and rotate it."
  6534. (interactive)
  6535. (setq pos (or pos (point)))
  6536. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6537. (move-marker (car org-mark-ring)
  6538. (or pos (point))
  6539. (or buffer (current-buffer)))
  6540. (message "%s"
  6541. (substitute-command-keys
  6542. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6543. (defun org-mark-ring-goto (&optional n)
  6544. "Jump to the previous position in the mark ring.
  6545. With prefix arg N, jump back that many stored positions. When
  6546. called several times in succession, walk through the entire ring.
  6547. Org-mode commands jumping to a different position in the current file,
  6548. or to another Org-mode file, automatically push the old position
  6549. onto the ring."
  6550. (interactive "p")
  6551. (let (p m)
  6552. (if (eq last-command this-command)
  6553. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6554. (setq p org-mark-ring))
  6555. (setq org-mark-ring-last-goto p)
  6556. (setq m (car p))
  6557. (switch-to-buffer (marker-buffer m))
  6558. (goto-char m)
  6559. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6560. (defun org-remove-angle-brackets (s)
  6561. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6562. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6563. s)
  6564. (defun org-add-angle-brackets (s)
  6565. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6566. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6567. s)
  6568. (defun org-remove-double-quotes (s)
  6569. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6570. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6571. s)
  6572. ;;; Following specific links
  6573. (defun org-follow-timestamp-link ()
  6574. (cond
  6575. ((org-at-date-range-p t)
  6576. (let ((org-agenda-start-on-weekday)
  6577. (t1 (match-string 1))
  6578. (t2 (match-string 2)))
  6579. (setq t1 (time-to-days (org-time-string-to-time t1))
  6580. t2 (time-to-days (org-time-string-to-time t2)))
  6581. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6582. ((org-at-timestamp-p t)
  6583. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6584. (substring (match-string 1) 0 10)))
  6585. 1))
  6586. (t (error "This should not happen"))))
  6587. ;;; Following file links
  6588. (defvar org-wait nil)
  6589. (defun org-open-file (path &optional in-emacs line search)
  6590. "Open the file at PATH.
  6591. First, this expands any special file name abbreviations. Then the
  6592. configuration variable `org-file-apps' is checked if it contains an
  6593. entry for this file type, and if yes, the corresponding command is launched.
  6594. If no application is found, Emacs simply visits the file.
  6595. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6596. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6597. and o use an external application to visit the file.
  6598. Optional LINE specifies a line to go to, optional SEARCH a string to
  6599. search for. If LINE or SEARCH is given, the file will always be
  6600. opened in Emacs.
  6601. If the file does not exist, an error is thrown."
  6602. (setq in-emacs (or in-emacs line search))
  6603. (let* ((file (if (equal path "")
  6604. buffer-file-name
  6605. (substitute-in-file-name (expand-file-name path))))
  6606. (apps (append org-file-apps (org-default-apps)))
  6607. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6608. (dirp (if remp nil (file-directory-p file)))
  6609. (file (if (and dirp org-open-directory-means-index-dot-org)
  6610. (concat (file-name-as-directory file) "index.org")
  6611. file))
  6612. (a-m-a-p (assq 'auto-mode apps))
  6613. (dfile (downcase file))
  6614. (old-buffer (current-buffer))
  6615. (old-pos (point))
  6616. (old-mode major-mode)
  6617. ext cmd)
  6618. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6619. (setq ext (match-string 1 dfile))
  6620. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6621. (setq ext (match-string 1 dfile))))
  6622. (cond
  6623. ((equal in-emacs '(16))
  6624. (setq cmd (cdr (assoc 'system apps))))
  6625. (in-emacs (setq cmd 'emacs))
  6626. (t
  6627. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6628. (and dirp (cdr (assoc 'directory apps)))
  6629. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6630. 'string-match)
  6631. (cdr (assoc ext apps))
  6632. (cdr (assoc t apps))))))
  6633. (when (eq cmd 'system)
  6634. (setq cmd (cdr (assoc 'system apps))))
  6635. (when (eq cmd 'default)
  6636. (setq cmd (cdr (assoc t apps))))
  6637. (when (eq cmd 'mailcap)
  6638. (require 'mailcap)
  6639. (mailcap-parse-mailcaps)
  6640. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6641. (command (mailcap-mime-info mime-type)))
  6642. (if (stringp command)
  6643. (setq cmd command)
  6644. (setq cmd 'emacs))))
  6645. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6646. (not (file-exists-p file))
  6647. (not org-open-non-existing-files))
  6648. (error "No such file: %s" file))
  6649. (cond
  6650. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6651. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6652. (while (string-match "['\"]%s['\"]" cmd)
  6653. (setq cmd (replace-match "%s" t t cmd)))
  6654. (while (string-match "%s" cmd)
  6655. (setq cmd (replace-match
  6656. (save-match-data
  6657. (shell-quote-argument
  6658. (convert-standard-filename file)))
  6659. t t cmd)))
  6660. (save-window-excursion
  6661. (start-process-shell-command cmd nil cmd)
  6662. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6663. ))
  6664. ((or (stringp cmd)
  6665. (eq cmd 'emacs))
  6666. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6667. (widen)
  6668. (if line (goto-line line)
  6669. (if search (org-link-search search))))
  6670. ((consp cmd)
  6671. (let ((file (convert-standard-filename file)))
  6672. (eval cmd)))
  6673. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6674. (and (org-mode-p) (eq old-mode 'org-mode)
  6675. (or (not (equal old-buffer (current-buffer)))
  6676. (not (equal old-pos (point))))
  6677. (org-mark-ring-push old-pos old-buffer))))
  6678. (defun org-default-apps ()
  6679. "Return the default applications for this operating system."
  6680. (cond
  6681. ((eq system-type 'darwin)
  6682. org-file-apps-defaults-macosx)
  6683. ((eq system-type 'windows-nt)
  6684. org-file-apps-defaults-windowsnt)
  6685. (t org-file-apps-defaults-gnu)))
  6686. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6687. "Convert extensions to regular expressions in the cars of LIST.
  6688. Also, weed out any non-string entries, because the return value is used
  6689. only for regexp matching.
  6690. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6691. point to the symbol `emacs', indicating that the file should
  6692. be opened in Emacs."
  6693. (append
  6694. (delq nil
  6695. (mapcar (lambda (x)
  6696. (if (not (stringp (car x)))
  6697. nil
  6698. (if (string-match "\\W" (car x))
  6699. x
  6700. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6701. list))
  6702. (if add-auto-mode
  6703. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6704. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6705. (defun org-file-remote-p (file)
  6706. "Test whether FILE specifies a location on a remote system.
  6707. Return non-nil if the location is indeed remote.
  6708. For example, the filename \"/user@host:/foo\" specifies a location
  6709. on the system \"/user@host:\"."
  6710. (cond ((fboundp 'file-remote-p)
  6711. (file-remote-p file))
  6712. ((fboundp 'tramp-handle-file-remote-p)
  6713. (tramp-handle-file-remote-p file))
  6714. ((and (boundp 'ange-ftp-name-format)
  6715. (string-match (car ange-ftp-name-format) file))
  6716. t)
  6717. (t nil)))
  6718. ;;;; Refiling
  6719. (defun org-get-org-file ()
  6720. "Read a filename, with default directory `org-directory'."
  6721. (let ((default (or org-default-notes-file remember-data-file)))
  6722. (read-file-name (format "File name [%s]: " default)
  6723. (file-name-as-directory org-directory)
  6724. default)))
  6725. (defun org-notes-order-reversed-p ()
  6726. "Check if the current file should receive notes in reversed order."
  6727. (cond
  6728. ((not org-reverse-note-order) nil)
  6729. ((eq t org-reverse-note-order) t)
  6730. ((not (listp org-reverse-note-order)) nil)
  6731. (t (catch 'exit
  6732. (let ((all org-reverse-note-order)
  6733. entry)
  6734. (while (setq entry (pop all))
  6735. (if (string-match (car entry) buffer-file-name)
  6736. (throw 'exit (cdr entry))))
  6737. nil)))))
  6738. (defvar org-refile-target-table nil
  6739. "The list of refile targets, created by `org-refile'.")
  6740. (defvar org-agenda-new-buffers nil
  6741. "Buffers created to visit agenda files.")
  6742. (defun org-get-refile-targets (&optional default-buffer)
  6743. "Produce a table with refile targets."
  6744. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6745. targets txt re files f desc descre fast-path-p level)
  6746. (message "Getting targets...")
  6747. (with-current-buffer (or default-buffer (current-buffer))
  6748. (while (setq entry (pop entries))
  6749. (setq files (car entry) desc (cdr entry))
  6750. (setq fast-path-p nil)
  6751. (cond
  6752. ((null files) (setq files (list (current-buffer))))
  6753. ((eq files 'org-agenda-files)
  6754. (setq files (org-agenda-files 'unrestricted)))
  6755. ((and (symbolp files) (fboundp files))
  6756. (setq files (funcall files)))
  6757. ((and (symbolp files) (boundp files))
  6758. (setq files (symbol-value files))))
  6759. (if (stringp files) (setq files (list files)))
  6760. (cond
  6761. ((eq (car desc) :tag)
  6762. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6763. ((eq (car desc) :todo)
  6764. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6765. ((eq (car desc) :regexp)
  6766. (setq descre (cdr desc)))
  6767. ((eq (car desc) :level)
  6768. (setq descre (concat "^\\*\\{" (number-to-string
  6769. (if org-odd-levels-only
  6770. (1- (* 2 (cdr desc)))
  6771. (cdr desc)))
  6772. "\\}[ \t]")))
  6773. ((eq (car desc) :maxlevel)
  6774. (setq fast-path-p t)
  6775. (setq descre (concat "^\\*\\{1," (number-to-string
  6776. (if org-odd-levels-only
  6777. (1- (* 2 (cdr desc)))
  6778. (cdr desc)))
  6779. "\\}[ \t]")))
  6780. (t (error "Bad refiling target description %s" desc)))
  6781. (while (setq f (pop files))
  6782. (save-excursion
  6783. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6784. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6785. (setq f (expand-file-name f))
  6786. (save-excursion
  6787. (save-restriction
  6788. (widen)
  6789. (goto-char (point-min))
  6790. (while (re-search-forward descre nil t)
  6791. (goto-char (point-at-bol))
  6792. (when (looking-at org-complex-heading-regexp)
  6793. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6794. txt (org-link-display-format (match-string 4))
  6795. re (concat "^" (regexp-quote
  6796. (buffer-substring (match-beginning 1)
  6797. (match-end 4)))))
  6798. (if (match-end 5) (setq re (concat re "[ \t]+"
  6799. (regexp-quote
  6800. (match-string 5)))))
  6801. (setq re (concat re "[ \t]*$"))
  6802. (when org-refile-use-outline-path
  6803. (setq txt (mapconcat 'org-protect-slash
  6804. (append
  6805. (if (eq org-refile-use-outline-path 'file)
  6806. (list (file-name-nondirectory
  6807. (buffer-file-name (buffer-base-buffer))))
  6808. (if (eq org-refile-use-outline-path 'full-file-path)
  6809. (list (buffer-file-name (buffer-base-buffer)))))
  6810. (org-get-outline-path fast-path-p level txt)
  6811. (list txt))
  6812. "/")))
  6813. (push (list txt f re (point)) targets))
  6814. (goto-char (point-at-eol))))))))
  6815. (message "Getting targets...done")
  6816. (nreverse targets))))
  6817. (defun org-protect-slash (s)
  6818. (while (string-match "/" s)
  6819. (setq s (replace-match "\\" t t s)))
  6820. s)
  6821. (defvar org-olpa (make-vector 20 nil))
  6822. (defun org-get-outline-path (&optional fastp level heading)
  6823. "Return the outline path to the current entry, as a list."
  6824. (if fastp
  6825. (progn
  6826. (if (> level 19)
  6827. (error "Outline path failure, more than 19 levels."))
  6828. (loop for i from level upto 19 do
  6829. (aset org-olpa i nil))
  6830. (prog1
  6831. (delq nil (append org-olpa nil))
  6832. (aset org-olpa level heading)))
  6833. (let (rtn)
  6834. (save-excursion
  6835. (while (org-up-heading-safe)
  6836. (when (looking-at org-complex-heading-regexp)
  6837. (push (org-match-string-no-properties 4) rtn)))
  6838. rtn))))
  6839. (defvar org-refile-history nil
  6840. "History for refiling operations.")
  6841. (defun org-refile (&optional goto default-buffer)
  6842. "Move the entry at point to another heading.
  6843. The list of target headings is compiled using the information in
  6844. `org-refile-targets', which see. This list is created before each use
  6845. and will therefore always be up-to-date.
  6846. At the target location, the entry is filed as a subitem of the target heading.
  6847. Depending on `org-reverse-note-order', the new subitem will either be the
  6848. first or the last subitem.
  6849. If there is an active region, all entries in that region will be moved.
  6850. However, the region must fulfil the requirement that the first heading
  6851. is the first one sets the top-level of the moved text - at most siblings
  6852. below it are allowed.
  6853. With prefix arg GOTO, the command will only visit the target location,
  6854. not actually move anything.
  6855. With a double prefix `C-u C-u', go to the location where the last refiling
  6856. operation has put the subtree."
  6857. (interactive "P")
  6858. (let* ((cbuf (current-buffer))
  6859. (regionp (org-region-active-p))
  6860. (region-start (and regionp (region-beginning)))
  6861. (region-end (and regionp (region-end)))
  6862. (region-length (and regionp (- region-end region-start)))
  6863. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6864. pos it nbuf file re level reversed)
  6865. (when regionp (goto-char region-start)
  6866. (unless (org-kill-is-subtree-p
  6867. (buffer-substring region-start region-end))
  6868. (error "The region is not a (sequence of) subtree(s)")))
  6869. (if (equal goto '(16))
  6870. (org-refile-goto-last-stored)
  6871. (when (setq it (org-refile-get-location
  6872. (if goto "Goto: " "Refile to: ") default-buffer))
  6873. (setq file (nth 1 it)
  6874. re (nth 2 it)
  6875. pos (nth 3 it))
  6876. (if (and (equal (buffer-file-name) file)
  6877. (if regionp
  6878. (and (>= pos region-start)
  6879. (<= pos region-end))
  6880. (and (>= pos (point))
  6881. (< pos (save-excursion
  6882. (org-end-of-subtree t t))))))
  6883. (error "Cannot refile to position inside the tree or region"))
  6884. (setq nbuf (or (find-buffer-visiting file)
  6885. (find-file-noselect file)))
  6886. (if goto
  6887. (progn
  6888. (switch-to-buffer nbuf)
  6889. (goto-char pos)
  6890. (org-show-context 'org-goto))
  6891. (if regionp
  6892. (progn
  6893. (kill-new (buffer-substring region-start region-end))
  6894. (org-save-markers-in-region region-start region-end))
  6895. (org-copy-subtree 1 nil t))
  6896. (save-excursion
  6897. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6898. (find-file-noselect file))))
  6899. (setq reversed (org-notes-order-reversed-p))
  6900. (save-excursion
  6901. (save-restriction
  6902. (widen)
  6903. (goto-char pos)
  6904. (looking-at outline-regexp)
  6905. (setq level (org-get-valid-level (funcall outline-level) 1))
  6906. (goto-char
  6907. (if reversed
  6908. (or (outline-next-heading) (point-max))
  6909. (or (save-excursion (outline-get-next-sibling))
  6910. (org-end-of-subtree t t)
  6911. (point-max))))
  6912. (if (not (bolp)) (newline))
  6913. (bookmark-set "org-refile-last-stored")
  6914. (org-paste-subtree level))))
  6915. (if regionp
  6916. (delete-region (point) (+ (point) region-length))
  6917. (org-cut-subtree))
  6918. (setq org-markers-to-move nil)
  6919. (message "Refiled to \"%s\"" (car it)))))))
  6920. (defun org-refile-goto-last-stored ()
  6921. "Go to the location where the last refile was stored."
  6922. (interactive)
  6923. (bookmark-jump "org-refile-last-stored")
  6924. (message "This is the location of the last refile"))
  6925. (defun org-refile-get-location (&optional prompt default-buffer)
  6926. "Prompt the user for a refile location, using PROMPT."
  6927. (let ((org-refile-targets org-refile-targets)
  6928. (org-refile-use-outline-path org-refile-use-outline-path))
  6929. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6930. (unless org-refile-target-table
  6931. (error "No refile targets"))
  6932. (let* ((cbuf (current-buffer))
  6933. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  6934. (cfunc (if (and org-refile-use-outline-path
  6935. org-outline-path-complete-in-steps)
  6936. 'org-olpath-completing-read
  6937. 'org-ido-completing-read))
  6938. (extra (if org-refile-use-outline-path "/" ""))
  6939. (filename (and cfn (expand-file-name cfn)))
  6940. (tbl (mapcar
  6941. (lambda (x)
  6942. (if (not (equal filename (nth 1 x)))
  6943. (cons (concat (car x) extra " ("
  6944. (file-name-nondirectory (nth 1 x)) ")")
  6945. (cdr x))
  6946. (cons (concat (car x) extra) (cdr x))))
  6947. org-refile-target-table))
  6948. (completion-ignore-case t))
  6949. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6950. tbl)))
  6951. (defun org-olpath-completing-read (prompt collection &rest args)
  6952. "Read an outline path like a file name."
  6953. (let ((thetable collection))
  6954. (apply
  6955. 'org-ido-completing-read prompt
  6956. (lambda (string predicate &optional flag)
  6957. (let (rtn r s f (l (length string)))
  6958. (cond
  6959. ((eq flag nil)
  6960. ;; try completion
  6961. (try-completion string thetable))
  6962. ((eq flag t)
  6963. ;; all-completions
  6964. (setq rtn (all-completions string thetable predicate))
  6965. (mapcar
  6966. (lambda (x)
  6967. (setq r (substring x l))
  6968. (if (string-match " ([^)]*)$" x)
  6969. (setq f (match-string 0 x))
  6970. (setq f ""))
  6971. (if (string-match "/" r)
  6972. (concat string (substring r 0 (match-end 0)) f)
  6973. x))
  6974. rtn))
  6975. ((eq flag 'lambda)
  6976. ;; exact match?
  6977. (assoc string thetable)))
  6978. ))
  6979. args)))
  6980. ;;;; Dynamic blocks
  6981. (defun org-find-dblock (name)
  6982. "Find the first dynamic block with name NAME in the buffer.
  6983. If not found, stay at current position and return nil."
  6984. (let (pos)
  6985. (save-excursion
  6986. (goto-char (point-min))
  6987. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6988. nil t)
  6989. (match-beginning 0))))
  6990. (if pos (goto-char pos))
  6991. pos))
  6992. (defconst org-dblock-start-re
  6993. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6994. "Matches the startline of a dynamic block, with parameters.")
  6995. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6996. "Matches the end of a dynamic block.")
  6997. (defun org-create-dblock (plist)
  6998. "Create a dynamic block section, with parameters taken from PLIST.
  6999. PLIST must contain a :name entry which is used as name of the block."
  7000. (unless (bolp) (newline))
  7001. (let ((name (plist-get plist :name)))
  7002. (insert "#+BEGIN: " name)
  7003. (while plist
  7004. (if (eq (car plist) :name)
  7005. (setq plist (cddr plist))
  7006. (insert " " (prin1-to-string (pop plist)))))
  7007. (insert "\n\n#+END:\n")
  7008. (beginning-of-line -2)))
  7009. (defun org-prepare-dblock ()
  7010. "Prepare dynamic block for refresh.
  7011. This empties the block, puts the cursor at the insert position and returns
  7012. the property list including an extra property :name with the block name."
  7013. (unless (looking-at org-dblock-start-re)
  7014. (error "Not at a dynamic block"))
  7015. (let* ((begdel (1+ (match-end 0)))
  7016. (name (org-no-properties (match-string 1)))
  7017. (params (append (list :name name)
  7018. (read (concat "(" (match-string 3) ")")))))
  7019. (unless (re-search-forward org-dblock-end-re nil t)
  7020. (error "Dynamic block not terminated"))
  7021. (setq params
  7022. (append params
  7023. (list :content (buffer-substring
  7024. begdel (match-beginning 0)))))
  7025. (delete-region begdel (match-beginning 0))
  7026. (goto-char begdel)
  7027. (open-line 1)
  7028. params))
  7029. (defun org-map-dblocks (&optional command)
  7030. "Apply COMMAND to all dynamic blocks in the current buffer.
  7031. If COMMAND is not given, use `org-update-dblock'."
  7032. (let ((cmd (or command 'org-update-dblock))
  7033. pos)
  7034. (save-excursion
  7035. (goto-char (point-min))
  7036. (while (re-search-forward org-dblock-start-re nil t)
  7037. (goto-char (setq pos (match-beginning 0)))
  7038. (condition-case nil
  7039. (funcall cmd)
  7040. (error (message "Error during update of dynamic block")))
  7041. (goto-char pos)
  7042. (unless (re-search-forward org-dblock-end-re nil t)
  7043. (error "Dynamic block not terminated"))))))
  7044. (defun org-dblock-update (&optional arg)
  7045. "User command for updating dynamic blocks.
  7046. Update the dynamic block at point. With prefix ARG, update all dynamic
  7047. blocks in the buffer."
  7048. (interactive "P")
  7049. (if arg
  7050. (org-update-all-dblocks)
  7051. (or (looking-at org-dblock-start-re)
  7052. (org-beginning-of-dblock))
  7053. (org-update-dblock)))
  7054. (defun org-update-dblock ()
  7055. "Update the dynamic block at point
  7056. This means to empty the block, parse for parameters and then call
  7057. the correct writing function."
  7058. (save-window-excursion
  7059. (let* ((pos (point))
  7060. (line (org-current-line))
  7061. (params (org-prepare-dblock))
  7062. (name (plist-get params :name))
  7063. (cmd (intern (concat "org-dblock-write:" name))))
  7064. (message "Updating dynamic block `%s' at line %d..." name line)
  7065. (funcall cmd params)
  7066. (message "Updating dynamic block `%s' at line %d...done" name line)
  7067. (goto-char pos))))
  7068. (defun org-beginning-of-dblock ()
  7069. "Find the beginning of the dynamic block at point.
  7070. Error if there is no such block at point."
  7071. (let ((pos (point))
  7072. beg)
  7073. (end-of-line 1)
  7074. (if (and (re-search-backward org-dblock-start-re nil t)
  7075. (setq beg (match-beginning 0))
  7076. (re-search-forward org-dblock-end-re nil t)
  7077. (> (match-end 0) pos))
  7078. (goto-char beg)
  7079. (goto-char pos)
  7080. (error "Not in a dynamic block"))))
  7081. (defun org-update-all-dblocks ()
  7082. "Update all dynamic blocks in the buffer.
  7083. This function can be used in a hook."
  7084. (when (org-mode-p)
  7085. (org-map-dblocks 'org-update-dblock)))
  7086. ;;;; Completion
  7087. (defconst org-additional-option-like-keywords
  7088. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7089. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7090. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7091. "BEGIN_QUOTE" "END_QUOTE"
  7092. "BEGIN_VERSE" "END_VERSE"
  7093. "BEGIN_SRC" "END_SRC"
  7094. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7095. (defcustom org-structure-template-alist
  7096. '(
  7097. ("s" "#+begin_src ?\n\n#+end_src"
  7098. "<src lang=\"?\">\n\n</src>")
  7099. ("e" "#+begin_example\n?\n#+end_example"
  7100. "<example>\n?\n</example>")
  7101. ("q" "#+begin_quote\n?\n#+end_quote"
  7102. "<quote>\n?\n</quote>")
  7103. ("v" "#+begin_verse\n?\n#+end_verse"
  7104. "<verse>\n?\n/verse>")
  7105. ("l" "#+begin_latex\n?\n#+end_latex"
  7106. "<literal style=\"latex\">\n?\n</literal>")
  7107. ("L" "#+latex: "
  7108. "<literal style=\"latex\">?</literal>")
  7109. ("h" "#+begin_html\n?\n#+end_html"
  7110. "<literal style=\"html\">\n?\n</literal>")
  7111. ("H" "#+html: "
  7112. "<literal style=\"html\">?</literal>")
  7113. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7114. ("A" "#+ascii: ")
  7115. ("i" "#+include %file ?"
  7116. "<include file=%file markup=\"?\">")
  7117. )
  7118. "Structure completion elements.
  7119. This is a list of abbreviation keys and values. The value gets inserted
  7120. it you type @samp{.} followed by the key and then the completion key,
  7121. usually `M-TAB'. %file will be replaced by a file name after prompting
  7122. for the file using completion.
  7123. There are two templates for each key, the first uses the original Org syntax,
  7124. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7125. the default when the /org-mtags.el/ module has been loaded. See also the
  7126. variable `org-mtags-prefer-muse-templates'.
  7127. This is an experimental feature, it is undecided if it is going to stay in."
  7128. :group 'org-completion
  7129. :type '(repeat
  7130. (string :tag "Key")
  7131. (string :tag "Template")
  7132. (string :tag "Muse Template")))
  7133. (defun org-try-structure-completion ()
  7134. "Try to complete a structure template before point.
  7135. This looks for strings like \"<e\" on an otherwise empty line and
  7136. expands them."
  7137. (let ((l (buffer-substring (point-at-bol) (point)))
  7138. a)
  7139. (when (and (looking-at "[ \t]*$")
  7140. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7141. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7142. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7143. (match-beginning 1)) a)
  7144. t)))
  7145. (defun org-complete-expand-structure-template (start cell)
  7146. "Expand a structure template."
  7147. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7148. (rpl (nth (if musep 2 1) cell)))
  7149. (delete-region start (point))
  7150. (when (string-match "\\`#\\+" rpl)
  7151. (cond
  7152. ((bolp))
  7153. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7154. (delete-region (point-at-bol) (point)))
  7155. (t (newline))))
  7156. (setq start (point))
  7157. (if (string-match "%file" rpl)
  7158. (setq rpl (replace-match
  7159. (concat
  7160. "\""
  7161. (save-match-data
  7162. (abbreviate-file-name (read-file-name "Include file: ")))
  7163. "\"")
  7164. t t rpl)))
  7165. (insert rpl)
  7166. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7167. (defun org-complete (&optional arg)
  7168. "Perform completion on word at point.
  7169. At the beginning of a headline, this completes TODO keywords as given in
  7170. `org-todo-keywords'.
  7171. If the current word is preceded by a backslash, completes the TeX symbols
  7172. that are supported for HTML support.
  7173. If the current word is preceded by \"#+\", completes special words for
  7174. setting file options.
  7175. In the line after \"#+STARTUP:, complete valid keywords.\"
  7176. At all other locations, this simply calls the value of
  7177. `org-completion-fallback-command'."
  7178. (interactive "P")
  7179. (org-without-partial-completion
  7180. (catch 'exit
  7181. (let* ((a nil)
  7182. (end (point))
  7183. (beg1 (save-excursion
  7184. (skip-chars-backward (org-re "[:alnum:]_@"))
  7185. (point)))
  7186. (beg (save-excursion
  7187. (skip-chars-backward "a-zA-Z0-9_:$")
  7188. (point)))
  7189. (confirm (lambda (x) (stringp (car x))))
  7190. (searchhead (equal (char-before beg) ?*))
  7191. (struct
  7192. (when (and (member (char-before beg1) '(?. ?<))
  7193. (setq a (assoc (buffer-substring beg1 (point))
  7194. org-structure-template-alist)))
  7195. (org-complete-expand-structure-template (1- beg1) a)
  7196. (throw 'exit t)))
  7197. (tag (and (equal (char-before beg1) ?:)
  7198. (equal (char-after (point-at-bol)) ?*)))
  7199. (prop (and (equal (char-before beg1) ?:)
  7200. (not (equal (char-after (point-at-bol)) ?*))))
  7201. (texp (equal (char-before beg) ?\\))
  7202. (link (equal (char-before beg) ?\[))
  7203. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7204. beg)
  7205. "#+"))
  7206. (startup (string-match "^#\\+STARTUP:.*"
  7207. (buffer-substring (point-at-bol) (point))))
  7208. (completion-ignore-case opt)
  7209. (type nil)
  7210. (tbl nil)
  7211. (table (cond
  7212. (opt
  7213. (setq type :opt)
  7214. (require 'org-exp)
  7215. (append
  7216. (mapcar
  7217. (lambda (x)
  7218. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7219. (cons (match-string 2 x) (match-string 1 x)))
  7220. (org-split-string (org-get-current-options) "\n"))
  7221. (mapcar 'list org-additional-option-like-keywords)))
  7222. (startup
  7223. (setq type :startup)
  7224. org-startup-options)
  7225. (link (append org-link-abbrev-alist-local
  7226. org-link-abbrev-alist))
  7227. (texp
  7228. (setq type :tex)
  7229. org-html-entities)
  7230. ((string-match "\\`\\*+[ \t]+\\'"
  7231. (buffer-substring (point-at-bol) beg))
  7232. (setq type :todo)
  7233. (mapcar 'list org-todo-keywords-1))
  7234. (searchhead
  7235. (setq type :searchhead)
  7236. (save-excursion
  7237. (goto-char (point-min))
  7238. (while (re-search-forward org-todo-line-regexp nil t)
  7239. (push (list
  7240. (org-make-org-heading-search-string
  7241. (match-string 3) t))
  7242. tbl)))
  7243. tbl)
  7244. (tag (setq type :tag beg beg1)
  7245. (or org-tag-alist (org-get-buffer-tags)))
  7246. (prop (setq type :prop beg beg1)
  7247. (mapcar 'list (org-buffer-property-keys nil t t)))
  7248. (t (progn
  7249. (call-interactively org-completion-fallback-command)
  7250. (throw 'exit nil)))))
  7251. (pattern (buffer-substring-no-properties beg end))
  7252. (completion (try-completion pattern table confirm)))
  7253. (cond ((eq completion t)
  7254. (if (not (assoc (upcase pattern) table))
  7255. (message "Already complete")
  7256. (if (and (equal type :opt)
  7257. (not (member (car (assoc (upcase pattern) table))
  7258. org-additional-option-like-keywords)))
  7259. (insert (substring (cdr (assoc (upcase pattern) table))
  7260. (length pattern)))
  7261. (if (memq type '(:tag :prop)) (insert ":")))))
  7262. ((null completion)
  7263. (message "Can't find completion for \"%s\"" pattern)
  7264. (ding))
  7265. ((not (string= pattern completion))
  7266. (delete-region beg end)
  7267. (if (string-match " +$" completion)
  7268. (setq completion (replace-match "" t t completion)))
  7269. (insert completion)
  7270. (if (get-buffer-window "*Completions*")
  7271. (delete-window (get-buffer-window "*Completions*")))
  7272. (if (assoc completion table)
  7273. (if (eq type :todo) (insert " ")
  7274. (if (memq type '(:tag :prop)) (insert ":"))))
  7275. (if (and (equal type :opt) (assoc completion table))
  7276. (message "%s" (substitute-command-keys
  7277. "Press \\[org-complete] again to insert example settings"))))
  7278. (t
  7279. (message "Making completion list...")
  7280. (let ((list (sort (all-completions pattern table confirm)
  7281. 'string<)))
  7282. (with-output-to-temp-buffer "*Completions*"
  7283. (condition-case nil
  7284. ;; Protection needed for XEmacs and emacs 21
  7285. (display-completion-list list pattern)
  7286. (error (display-completion-list list)))))
  7287. (message "Making completion list...%s" "done")))))))
  7288. ;;;; TODO, DEADLINE, Comments
  7289. (defun org-toggle-comment ()
  7290. "Change the COMMENT state of an entry."
  7291. (interactive)
  7292. (save-excursion
  7293. (org-back-to-heading)
  7294. (let (case-fold-search)
  7295. (if (looking-at (concat outline-regexp
  7296. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7297. (replace-match "" t t nil 1)
  7298. (if (looking-at outline-regexp)
  7299. (progn
  7300. (goto-char (match-end 0))
  7301. (insert org-comment-string " ")))))))
  7302. (defvar org-last-todo-state-is-todo nil
  7303. "This is non-nil when the last TODO state change led to a TODO state.
  7304. If the last change removed the TODO tag or switched to DONE, then
  7305. this is nil.")
  7306. (defvar org-setting-tags nil) ; dynamically skipped
  7307. (defun org-parse-local-options (string var)
  7308. "Parse STRING for startup setting relevant for variable VAR."
  7309. (let ((rtn (symbol-value var))
  7310. e opts)
  7311. (save-match-data
  7312. (if (or (not string) (not (string-match "\\S-" string)))
  7313. rtn
  7314. (setq opts (delq nil (mapcar (lambda (x)
  7315. (setq e (assoc x org-startup-options))
  7316. (if (eq (nth 1 e) var) e nil))
  7317. (org-split-string string "[ \t]+"))))
  7318. (if (not opts)
  7319. rtn
  7320. (setq rtn nil)
  7321. (while (setq e (pop opts))
  7322. (if (not (nth 3 e))
  7323. (setq rtn (nth 2 e))
  7324. (if (not (listp rtn)) (setq rtn nil))
  7325. (push (nth 2 e) rtn)))
  7326. rtn)))))
  7327. (defvar org-blocker-hook nil
  7328. "Hook for functions that are allowed to block a state change.
  7329. Each function gets as its single argument a property list, see
  7330. `org-trigger-hook' for more information about this list.
  7331. If any of the functions in this hook returns nil, the state change
  7332. is blocked.")
  7333. (defvar org-trigger-hook nil
  7334. "Hook for functions that are triggered by a state change.
  7335. Each function gets as its single argument a property list with at least
  7336. the following elements:
  7337. (:type type-of-change :position pos-at-entry-start
  7338. :from old-state :to new-state)
  7339. Depending on the type, more properties may be present.
  7340. This mechanism is currently implemented for:
  7341. TODO state changes
  7342. ------------------
  7343. :type todo-state-change
  7344. :from previous state (keyword as a string), or nil
  7345. :to new state (keyword as a string), or nil")
  7346. (defvar org-agenda-headline-snapshot-before-repeat)
  7347. (defun org-todo (&optional arg)
  7348. "Change the TODO state of an item.
  7349. The state of an item is given by a keyword at the start of the heading,
  7350. like
  7351. *** TODO Write paper
  7352. *** DONE Call mom
  7353. The different keywords are specified in the variable `org-todo-keywords'.
  7354. By default the available states are \"TODO\" and \"DONE\".
  7355. So for this example: when the item starts with TODO, it is changed to DONE.
  7356. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7357. DONE are present, add TODO at the beginning of the heading.
  7358. With C-u prefix arg, use completion to determine the new state.
  7359. With numeric prefix arg, switch to that state.
  7360. For calling through lisp, arg is also interpreted in the following way:
  7361. 'none -> empty state
  7362. \"\"(empty string) -> switch to empty state
  7363. 'done -> switch to DONE
  7364. 'nextset -> switch to the next set of keywords
  7365. 'previousset -> switch to the previous set of keywords
  7366. \"WAITING\" -> switch to the specified keyword, but only if it
  7367. really is a member of `org-todo-keywords'."
  7368. (interactive "P")
  7369. (save-excursion
  7370. (catch 'exit
  7371. (org-back-to-heading)
  7372. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7373. (or (looking-at (concat " +" org-todo-regexp " *"))
  7374. (looking-at " *"))
  7375. (let* ((match-data (match-data))
  7376. (startpos (point-at-bol))
  7377. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7378. (org-log-done org-log-done)
  7379. (org-log-repeat org-log-repeat)
  7380. (org-todo-log-states org-todo-log-states)
  7381. (this (match-string 1))
  7382. (hl-pos (match-beginning 0))
  7383. (head (org-get-todo-sequence-head this))
  7384. (ass (assoc head org-todo-kwd-alist))
  7385. (interpret (nth 1 ass))
  7386. (done-word (nth 3 ass))
  7387. (final-done-word (nth 4 ass))
  7388. (last-state (or this ""))
  7389. (completion-ignore-case t)
  7390. (member (member this org-todo-keywords-1))
  7391. (tail (cdr member))
  7392. (state (cond
  7393. ((and org-todo-key-trigger
  7394. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7395. (and (not arg) org-use-fast-todo-selection
  7396. (not (eq org-use-fast-todo-selection 'prefix)))))
  7397. ;; Use fast selection
  7398. (org-fast-todo-selection))
  7399. ((and (equal arg '(4))
  7400. (or (not org-use-fast-todo-selection)
  7401. (not org-todo-key-trigger)))
  7402. ;; Read a state with completion
  7403. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7404. org-todo-keywords-1)
  7405. nil t))
  7406. ((eq arg 'right)
  7407. (if this
  7408. (if tail (car tail) nil)
  7409. (car org-todo-keywords-1)))
  7410. ((eq arg 'left)
  7411. (if (equal member org-todo-keywords-1)
  7412. nil
  7413. (if this
  7414. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7415. org-todo-keywords-1)
  7416. (org-last org-todo-keywords-1))))
  7417. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7418. (setq arg nil))) ; hack to fall back to cycling
  7419. (arg
  7420. ;; user or caller requests a specific state
  7421. (cond
  7422. ((equal arg "") nil)
  7423. ((eq arg 'none) nil)
  7424. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7425. ((eq arg 'nextset)
  7426. (or (car (cdr (member head org-todo-heads)))
  7427. (car org-todo-heads)))
  7428. ((eq arg 'previousset)
  7429. (let ((org-todo-heads (reverse org-todo-heads)))
  7430. (or (car (cdr (member head org-todo-heads)))
  7431. (car org-todo-heads))))
  7432. ((car (member arg org-todo-keywords-1)))
  7433. ((nth (1- (prefix-numeric-value arg))
  7434. org-todo-keywords-1))))
  7435. ((null member) (or head (car org-todo-keywords-1)))
  7436. ((equal this final-done-word) nil) ;; -> make empty
  7437. ((null tail) nil) ;; -> first entry
  7438. ((eq interpret 'sequence)
  7439. (car tail))
  7440. ((memq interpret '(type priority))
  7441. (if (eq this-command last-command)
  7442. (car tail)
  7443. (if (> (length tail) 0)
  7444. (or done-word (car org-done-keywords))
  7445. nil)))
  7446. (t nil)))
  7447. (next (if state (concat " " state " ") " "))
  7448. (change-plist (list :type 'todo-state-change :from this :to state
  7449. :position startpos))
  7450. dolog now-done-p)
  7451. (when org-blocker-hook
  7452. (unless (save-excursion
  7453. (save-match-data
  7454. (run-hook-with-args-until-failure
  7455. 'org-blocker-hook change-plist)))
  7456. (if (interactive-p)
  7457. (error "TODO state change from %s to %s blocked" this state)
  7458. ;; fail silently
  7459. (message "TODO state change from %s to %s blocked" this state)
  7460. (throw 'exit nil))))
  7461. (store-match-data match-data)
  7462. (replace-match next t t)
  7463. (unless (pos-visible-in-window-p hl-pos)
  7464. (message "TODO state changed to %s" (org-trim next)))
  7465. (unless head
  7466. (setq head (org-get-todo-sequence-head state)
  7467. ass (assoc head org-todo-kwd-alist)
  7468. interpret (nth 1 ass)
  7469. done-word (nth 3 ass)
  7470. final-done-word (nth 4 ass)))
  7471. (when (memq arg '(nextset previousset))
  7472. (message "Keyword-Set %d/%d: %s"
  7473. (- (length org-todo-sets) -1
  7474. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7475. (length org-todo-sets)
  7476. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7477. (setq org-last-todo-state-is-todo
  7478. (not (member state org-done-keywords)))
  7479. (setq now-done-p (and (member state org-done-keywords)
  7480. (not (member this org-done-keywords))))
  7481. (and logging (org-local-logging logging))
  7482. (when (and (or org-todo-log-states org-log-done)
  7483. (not (memq arg '(nextset previousset))))
  7484. ;; we need to look at recording a time and note
  7485. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7486. (nth 2 (assoc this org-todo-log-states))))
  7487. (when (and state
  7488. (member state org-not-done-keywords)
  7489. (not (member this org-not-done-keywords)))
  7490. ;; This is now a todo state and was not one before
  7491. ;; If there was a CLOSED time stamp, get rid of it.
  7492. (org-add-planning-info nil nil 'closed))
  7493. (when (and now-done-p org-log-done)
  7494. ;; It is now done, and it was not done before
  7495. (org-add-planning-info 'closed (org-current-time))
  7496. (if (and (not dolog) (eq 'note org-log-done))
  7497. (org-add-log-setup 'done state 'findpos 'note)))
  7498. (when (and state dolog)
  7499. ;; This is a non-nil state, and we need to log it
  7500. (org-add-log-setup 'state state 'findpos dolog)))
  7501. ;; Fixup tag positioning
  7502. (org-todo-trigger-tag-changes state)
  7503. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7504. (when org-provide-todo-statistics
  7505. (org-update-parent-todo-statistics))
  7506. (run-hooks 'org-after-todo-state-change-hook)
  7507. (if (and arg (not (member state org-done-keywords)))
  7508. (setq head (org-get-todo-sequence-head state)))
  7509. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7510. ;; Do we need to trigger a repeat?
  7511. (when now-done-p
  7512. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7513. ;; This is for the agenda, take a snapshot of the headline.
  7514. (save-match-data
  7515. (setq org-agenda-headline-snapshot-before-repeat
  7516. (org-get-heading))))
  7517. (org-auto-repeat-maybe state))
  7518. ;; Fixup cursor location if close to the keyword
  7519. (if (and (outline-on-heading-p)
  7520. (not (bolp))
  7521. (save-excursion (beginning-of-line 1)
  7522. (looking-at org-todo-line-regexp))
  7523. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7524. (progn
  7525. (goto-char (or (match-end 2) (match-end 1)))
  7526. (just-one-space)))
  7527. (when org-trigger-hook
  7528. (save-excursion
  7529. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7530. (defun org-update-parent-todo-statistics ()
  7531. "Update any statistics cookie in the parent of the current headline."
  7532. (interactive)
  7533. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7534. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7535. (catch 'exit
  7536. (save-excursion
  7537. (setq level (org-up-heading-safe))
  7538. (unless (and level
  7539. (re-search-forward box-re (point-at-eol) t))
  7540. (throw 'exit nil))
  7541. (setq is-percent (match-end 2))
  7542. (save-match-data
  7543. (unless (outline-next-heading) (throw 'exit nil))
  7544. (while (looking-at org-todo-line-regexp)
  7545. (setq kwd (match-string 2))
  7546. (and kwd (setq cnt-all (1+ cnt-all)))
  7547. (and (member kwd org-done-keywords)
  7548. (setq cnt-done (1+ cnt-done)))
  7549. (condition-case nil
  7550. (org-forward-same-level 1)
  7551. (error (end-of-line 1)))))
  7552. (replace-match
  7553. (if is-percent
  7554. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7555. (format "[%d/%d]" cnt-done cnt-all)))
  7556. (run-hook-with-args 'org-after-todo-statistics-hook
  7557. cnt-done (- cnt-all cnt-done))))))
  7558. (defvar org-after-todo-statistics-hook nil
  7559. "Hook that is called after a TODO statistics cookie has been updated.
  7560. Each function is called with two arguments: the number of not-done entries
  7561. and the number of done entries.
  7562. For example, the following function, when added to this hook, will switch
  7563. an entry to DONE when all children are done, and back to TODO when new
  7564. entries are set to a TODO status. Note that this hook is only called
  7565. when there is a statistics cookie in the headline!
  7566. (defun org-summary-todo (n-done n-not-done)
  7567. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7568. (let (org-log-done org-log-states) ; turn off logging
  7569. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7570. ")
  7571. (defun org-todo-trigger-tag-changes (state)
  7572. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7573. (let ((l org-todo-state-tags-triggers)
  7574. changes)
  7575. (when (or (not state) (equal state ""))
  7576. (setq changes (append changes (cdr (assoc "" l)))))
  7577. (when (and (stringp state) (> (length state) 0))
  7578. (setq changes (append changes (cdr (assoc state l)))))
  7579. (when (member state org-not-done-keywords)
  7580. (setq changes (append changes (cdr (assoc 'todo l)))))
  7581. (when (member state org-done-keywords)
  7582. (setq changes (append changes (cdr (assoc 'done l)))))
  7583. (dolist (c changes)
  7584. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7585. (defun org-local-logging (value)
  7586. "Get logging settings from a property VALUE."
  7587. (let* (words w a)
  7588. ;; directly set the variables, they are already local.
  7589. (setq org-log-done nil
  7590. org-log-repeat nil
  7591. org-todo-log-states nil)
  7592. (setq words (org-split-string value))
  7593. (while (setq w (pop words))
  7594. (cond
  7595. ((setq a (assoc w org-startup-options))
  7596. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7597. (set (nth 1 a) (nth 2 a))))
  7598. ((setq a (org-extract-log-state-settings w))
  7599. (and (member (car a) org-todo-keywords-1)
  7600. (push a org-todo-log-states)))))))
  7601. (defun org-get-todo-sequence-head (kwd)
  7602. "Return the head of the TODO sequence to which KWD belongs.
  7603. If KWD is not set, check if there is a text property remembering the
  7604. right sequence."
  7605. (let (p)
  7606. (cond
  7607. ((not kwd)
  7608. (or (get-text-property (point-at-bol) 'org-todo-head)
  7609. (progn
  7610. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7611. nil (point-at-eol)))
  7612. (get-text-property p 'org-todo-head))))
  7613. ((not (member kwd org-todo-keywords-1))
  7614. (car org-todo-keywords-1))
  7615. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7616. (defun org-fast-todo-selection ()
  7617. "Fast TODO keyword selection with single keys.
  7618. Returns the new TODO keyword, or nil if no state change should occur."
  7619. (let* ((fulltable org-todo-key-alist)
  7620. (done-keywords org-done-keywords) ;; needed for the faces.
  7621. (maxlen (apply 'max (mapcar
  7622. (lambda (x)
  7623. (if (stringp (car x)) (string-width (car x)) 0))
  7624. fulltable)))
  7625. (expert nil)
  7626. (fwidth (+ maxlen 3 1 3))
  7627. (ncol (/ (- (window-width) 4) fwidth))
  7628. tg cnt e c tbl
  7629. groups ingroup)
  7630. (save-window-excursion
  7631. (if expert
  7632. (set-buffer (get-buffer-create " *Org todo*"))
  7633. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7634. (erase-buffer)
  7635. (org-set-local 'org-done-keywords done-keywords)
  7636. (setq tbl fulltable cnt 0)
  7637. (while (setq e (pop tbl))
  7638. (cond
  7639. ((equal e '(:startgroup))
  7640. (push '() groups) (setq ingroup t)
  7641. (when (not (= cnt 0))
  7642. (setq cnt 0)
  7643. (insert "\n"))
  7644. (insert "{ "))
  7645. ((equal e '(:endgroup))
  7646. (setq ingroup nil cnt 0)
  7647. (insert "}\n"))
  7648. (t
  7649. (setq tg (car e) c (cdr e))
  7650. (if ingroup (push tg (car groups)))
  7651. (setq tg (org-add-props tg nil 'face
  7652. (org-get-todo-face tg)))
  7653. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7654. (insert "[" c "] " tg (make-string
  7655. (- fwidth 4 (length tg)) ?\ ))
  7656. (when (= (setq cnt (1+ cnt)) ncol)
  7657. (insert "\n")
  7658. (if ingroup (insert " "))
  7659. (setq cnt 0)))))
  7660. (insert "\n")
  7661. (goto-char (point-min))
  7662. (if (not expert) (org-fit-window-to-buffer))
  7663. (message "[a-z..]:Set [SPC]:clear")
  7664. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7665. (cond
  7666. ((or (= c ?\C-g)
  7667. (and (= c ?q) (not (rassoc c fulltable))))
  7668. (setq quit-flag t))
  7669. ((= c ?\ ) nil)
  7670. ((setq e (rassoc c fulltable) tg (car e))
  7671. tg)
  7672. (t (setq quit-flag t))))))
  7673. (defun org-entry-is-todo-p ()
  7674. (member (org-get-todo-state) org-not-done-keywords))
  7675. (defun org-entry-is-done-p ()
  7676. (member (org-get-todo-state) org-done-keywords))
  7677. (defun org-get-todo-state ()
  7678. (save-excursion
  7679. (org-back-to-heading t)
  7680. (and (looking-at org-todo-line-regexp)
  7681. (match-end 2)
  7682. (match-string 2))))
  7683. (defun org-at-date-range-p (&optional inactive-ok)
  7684. "Is the cursor inside a date range?"
  7685. (interactive)
  7686. (save-excursion
  7687. (catch 'exit
  7688. (let ((pos (point)))
  7689. (skip-chars-backward "^[<\r\n")
  7690. (skip-chars-backward "<[")
  7691. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7692. (>= (match-end 0) pos)
  7693. (throw 'exit t))
  7694. (skip-chars-backward "^<[\r\n")
  7695. (skip-chars-backward "<[")
  7696. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7697. (>= (match-end 0) pos)
  7698. (throw 'exit t)))
  7699. nil)))
  7700. (defun org-get-repeat ()
  7701. "Check if there is a deadline/schedule with repeater in this entry."
  7702. (save-match-data
  7703. (save-excursion
  7704. (org-back-to-heading t)
  7705. (if (re-search-forward
  7706. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7707. (match-string 1)))))
  7708. (defvar org-last-changed-timestamp)
  7709. (defvar org-last-inserted-timestamp)
  7710. (defvar org-log-post-message)
  7711. (defvar org-log-note-purpose)
  7712. (defvar org-log-note-how)
  7713. (defvar org-log-note-extra)
  7714. (defun org-auto-repeat-maybe (done-word)
  7715. "Check if the current headline contains a repeated deadline/schedule.
  7716. If yes, set TODO state back to what it was and change the base date
  7717. of repeating deadline/scheduled time stamps to new date.
  7718. This function is run automatically after each state change to a DONE state."
  7719. ;; last-state is dynamically scoped into this function
  7720. (let* ((repeat (org-get-repeat))
  7721. (aa (assoc last-state org-todo-kwd-alist))
  7722. (interpret (nth 1 aa))
  7723. (head (nth 2 aa))
  7724. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7725. (msg "Entry repeats: ")
  7726. (org-log-done nil)
  7727. (org-todo-log-states nil)
  7728. (nshiftmax 10) (nshift 0)
  7729. re type n what ts mb0 time)
  7730. (when repeat
  7731. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7732. (org-todo (if (eq interpret 'type) last-state head))
  7733. (when org-log-repeat
  7734. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7735. (memq 'org-add-log-note post-command-hook))
  7736. ;; OK, we are already setup for some record
  7737. (if (eq org-log-repeat 'note)
  7738. ;; make sure we take a note, not only a time stamp
  7739. (setq org-log-note-how 'note))
  7740. ;; Set up for taking a record
  7741. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7742. 'findpos org-log-repeat)))
  7743. (org-back-to-heading t)
  7744. (org-add-planning-info nil nil 'closed)
  7745. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7746. org-deadline-time-regexp "\\)\\|\\("
  7747. org-ts-regexp "\\)"))
  7748. (while (re-search-forward
  7749. re (save-excursion (outline-next-heading) (point)) t)
  7750. (setq type (if (match-end 1) org-scheduled-string
  7751. (if (match-end 3) org-deadline-string "Plain:"))
  7752. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7753. mb0 (match-beginning 0))
  7754. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7755. (setq n (string-to-number (match-string 2 ts))
  7756. what (match-string 3 ts))
  7757. (if (equal what "w") (setq n (* n 7) what "d"))
  7758. ;; Preparation, see if we need to modify the start date for the change
  7759. (when (match-end 1)
  7760. (setq time (save-match-data (org-time-string-to-time ts)))
  7761. (cond
  7762. ((equal (match-string 1 ts) ".")
  7763. ;; Shift starting date to today
  7764. (org-timestamp-change
  7765. (- (time-to-days (current-time)) (time-to-days time))
  7766. 'day))
  7767. ((equal (match-string 1 ts) "+")
  7768. (while (or (= nshift 0)
  7769. (<= (time-to-days time) (time-to-days (current-time))))
  7770. (when (= (incf nshift) nshiftmax)
  7771. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7772. (error "Abort")))
  7773. (org-timestamp-change n (cdr (assoc what whata)))
  7774. (org-at-timestamp-p t)
  7775. (setq ts (match-string 1))
  7776. (setq time (save-match-data (org-time-string-to-time ts))))
  7777. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7778. ;; rematch, so that we have everything in place for the real shift
  7779. (org-at-timestamp-p t)
  7780. (setq ts (match-string 1))
  7781. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7782. (org-timestamp-change n (cdr (assoc what whata)))
  7783. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7784. (setq org-log-post-message msg)
  7785. (message "%s" msg))))
  7786. (defun org-show-todo-tree (arg)
  7787. "Make a compact tree which shows all headlines marked with TODO.
  7788. The tree will show the lines where the regexp matches, and all higher
  7789. headlines above the match.
  7790. With a \\[universal-argument] prefix, also show the DONE entries.
  7791. With a numeric prefix N, construct a sparse tree for the Nth element
  7792. of `org-todo-keywords-1'."
  7793. (interactive "P")
  7794. (let ((case-fold-search nil)
  7795. (kwd-re
  7796. (cond ((null arg) org-not-done-regexp)
  7797. ((equal arg '(4))
  7798. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7799. (mapcar 'list org-todo-keywords-1))))
  7800. (concat "\\("
  7801. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7802. "\\)\\>")))
  7803. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7804. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7805. org-todo-keywords-1)))
  7806. (t (error "Invalid prefix argument: %s" arg)))))
  7807. (message "%d TODO entries found"
  7808. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7809. (defun org-deadline (&optional remove time)
  7810. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7811. With argument REMOVE, remove any deadline from the item.
  7812. When TIME is set, it should be an internal time specification, and the
  7813. scheduling will use the corresponding date."
  7814. (interactive "P")
  7815. (if remove
  7816. (progn
  7817. (org-remove-timestamp-with-keyword org-deadline-string)
  7818. (message "Item no longer has a deadline."))
  7819. (if (org-get-repeat)
  7820. (error "Cannot change deadline on task with repeater, please do that by hand")
  7821. (org-add-planning-info 'deadline time 'closed)
  7822. (message "Deadline on %s" org-last-inserted-timestamp))))
  7823. (defun org-schedule (&optional remove time)
  7824. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7825. With argument REMOVE, remove any scheduling date from the item.
  7826. When TIME is set, it should be an internal time specification, and the
  7827. scheduling will use the corresponding date."
  7828. (interactive "P")
  7829. (if remove
  7830. (progn
  7831. (org-remove-timestamp-with-keyword org-scheduled-string)
  7832. (message "Item is no longer scheduled."))
  7833. (if (org-get-repeat)
  7834. (error "Cannot reschedule task with repeater, please do that by hand")
  7835. (org-add-planning-info 'scheduled time 'closed)
  7836. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7837. (defun org-remove-timestamp-with-keyword (keyword)
  7838. "Remove all time stamps with KEYWORD in the current entry."
  7839. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7840. beg)
  7841. (save-excursion
  7842. (org-back-to-heading t)
  7843. (setq beg (point))
  7844. (org-end-of-subtree t t)
  7845. (while (re-search-backward re beg t)
  7846. (replace-match "")
  7847. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7848. (equal (char-before) ?\ ))
  7849. (backward-delete-char 1)
  7850. (if (string-match "^[ \t]*$" (buffer-substring
  7851. (point-at-bol) (point-at-eol)))
  7852. (delete-region (point-at-bol)
  7853. (min (point-max) (1+ (point-at-eol))))))))))
  7854. (defun org-add-planning-info (what &optional time &rest remove)
  7855. "Insert new timestamp with keyword in the line directly after the headline.
  7856. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7857. If non is given, the user is prompted for a date.
  7858. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7859. be removed."
  7860. (interactive)
  7861. (let (org-time-was-given org-end-time-was-given ts
  7862. end default-time default-input)
  7863. (when (and (not time) (memq what '(scheduled deadline)))
  7864. ;; Try to get a default date/time from existing timestamp
  7865. (save-excursion
  7866. (org-back-to-heading t)
  7867. (setq end (save-excursion (outline-next-heading) (point)))
  7868. (when (re-search-forward (if (eq what 'scheduled)
  7869. org-scheduled-time-regexp
  7870. org-deadline-time-regexp)
  7871. end t)
  7872. (setq ts (match-string 1)
  7873. default-time
  7874. (apply 'encode-time (org-parse-time-string ts))
  7875. default-input (and ts (org-get-compact-tod ts))))))
  7876. (when what
  7877. ;; If necessary, get the time from the user
  7878. (setq time (or time (org-read-date nil 'to-time nil nil
  7879. default-time default-input))))
  7880. (when (and org-insert-labeled-timestamps-at-point
  7881. (member what '(scheduled deadline)))
  7882. (insert
  7883. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7884. (org-insert-time-stamp time org-time-was-given
  7885. nil nil nil (list org-end-time-was-given))
  7886. (setq what nil))
  7887. (save-excursion
  7888. (save-restriction
  7889. (let (col list elt ts buffer-invisibility-spec)
  7890. (org-back-to-heading t)
  7891. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7892. (goto-char (match-end 1))
  7893. (setq col (current-column))
  7894. (goto-char (match-end 0))
  7895. (if (eobp) (insert "\n") (forward-char 1))
  7896. (if (and (not (looking-at outline-regexp))
  7897. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7898. "[^\r\n]*"))
  7899. (not (equal (match-string 1) org-clock-string)))
  7900. (narrow-to-region (match-beginning 0) (match-end 0))
  7901. (insert-before-markers "\n")
  7902. (backward-char 1)
  7903. (narrow-to-region (point) (point))
  7904. (and org-adapt-indentation (org-indent-to-column col)))
  7905. ;; Check if we have to remove something.
  7906. (setq list (cons what remove))
  7907. (while list
  7908. (setq elt (pop list))
  7909. (goto-char (point-min))
  7910. (when (or (and (eq elt 'scheduled)
  7911. (re-search-forward org-scheduled-time-regexp nil t))
  7912. (and (eq elt 'deadline)
  7913. (re-search-forward org-deadline-time-regexp nil t))
  7914. (and (eq elt 'closed)
  7915. (re-search-forward org-closed-time-regexp nil t)))
  7916. (replace-match "")
  7917. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7918. (if (looking-at " +") (replace-match ""))))
  7919. (goto-char (point-max))
  7920. (when what
  7921. (insert
  7922. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7923. (cond ((eq what 'scheduled) org-scheduled-string)
  7924. ((eq what 'deadline) org-deadline-string)
  7925. ((eq what 'closed) org-closed-string))
  7926. " ")
  7927. (setq ts (org-insert-time-stamp
  7928. time
  7929. (or org-time-was-given
  7930. (and (eq what 'closed) org-log-done-with-time))
  7931. (eq what 'closed)
  7932. nil nil (list org-end-time-was-given)))
  7933. (end-of-line 1))
  7934. (goto-char (point-min))
  7935. (widen)
  7936. (if (and (looking-at "[ \t]+\n")
  7937. (equal (char-before) ?\n))
  7938. (delete-region (1- (point)) (point-at-eol)))
  7939. ts)))))
  7940. (defvar org-log-note-marker (make-marker))
  7941. (defvar org-log-note-purpose nil)
  7942. (defvar org-log-note-state nil)
  7943. (defvar org-log-note-how nil)
  7944. (defvar org-log-note-extra nil)
  7945. (defvar org-log-note-window-configuration nil)
  7946. (defvar org-log-note-return-to (make-marker))
  7947. (defvar org-log-post-message nil
  7948. "Message to be displayed after a log note has been stored.
  7949. The auto-repeater uses this.")
  7950. (defun org-add-note ()
  7951. "Add a note to the current entry.
  7952. This is done in the same way as adding a state change note."
  7953. (interactive)
  7954. (org-add-log-setup 'note nil 'findpos nil))
  7955. (defvar org-property-end-re)
  7956. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7957. "Set up the post command hook to take a note.
  7958. If this is about to TODO state change, the new state is expected in STATE.
  7959. When FINDPOS is non-nil, find the correct position for the note in
  7960. the current entry. If not, assume that it can be inserted at point.
  7961. HOW is an indicator what kind of note should be created.
  7962. EXTRA is additional text that will be inserted into the notes buffer."
  7963. (save-restriction
  7964. (save-excursion
  7965. (when findpos
  7966. (org-back-to-heading t)
  7967. (narrow-to-region (point) (save-excursion
  7968. (outline-next-heading) (point)))
  7969. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7970. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7971. "[^\r\n]*\\)?"))
  7972. (goto-char (match-end 0))
  7973. (when (and org-log-state-notes-insert-after-drawers
  7974. (save-excursion
  7975. (forward-line) (looking-at org-drawer-regexp)))
  7976. (progn (forward-line)
  7977. (while (looking-at org-drawer-regexp)
  7978. (goto-char (match-end 0))
  7979. (re-search-forward org-property-end-re (point-max) t)
  7980. (forward-line))
  7981. (forward-line -1)))
  7982. (unless org-log-states-order-reversed
  7983. (and (= (char-after) ?\n) (forward-char 1))
  7984. (org-skip-over-state-notes)
  7985. (skip-chars-backward " \t\n\r")))
  7986. (move-marker org-log-note-marker (point))
  7987. (setq org-log-note-purpose purpose
  7988. org-log-note-state state
  7989. org-log-note-how how
  7990. org-log-note-extra extra)
  7991. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7992. (defun org-skip-over-state-notes ()
  7993. "Skip past the list of State notes in an entry."
  7994. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7995. (while (looking-at "[ \t]*- State")
  7996. (condition-case nil
  7997. (org-next-item)
  7998. (error (org-end-of-item)))))
  7999. (defun org-add-log-note (&optional purpose)
  8000. "Pop up a window for taking a note, and add this note later at point."
  8001. (remove-hook 'post-command-hook 'org-add-log-note)
  8002. (setq org-log-note-window-configuration (current-window-configuration))
  8003. (delete-other-windows)
  8004. (move-marker org-log-note-return-to (point))
  8005. (switch-to-buffer (marker-buffer org-log-note-marker))
  8006. (goto-char org-log-note-marker)
  8007. (org-switch-to-buffer-other-window "*Org Note*")
  8008. (erase-buffer)
  8009. (if (memq org-log-note-how '(time state))
  8010. (let (current-prefix-arg) (org-store-log-note))
  8011. (let ((org-inhibit-startup t)) (org-mode))
  8012. (insert (format "# Insert note for %s.
  8013. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8014. (cond
  8015. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8016. ((eq org-log-note-purpose 'done) "closed todo item")
  8017. ((eq org-log-note-purpose 'state)
  8018. (format "state change to \"%s\"" org-log-note-state))
  8019. ((eq org-log-note-purpose 'note)
  8020. "this entry")
  8021. (t (error "This should not happen")))))
  8022. (if org-log-note-extra (insert org-log-note-extra))
  8023. (org-set-local 'org-finish-function 'org-store-log-note)))
  8024. (defvar org-note-abort nil) ; dynamically scoped
  8025. (defun org-store-log-note ()
  8026. "Finish taking a log note, and insert it to where it belongs."
  8027. (let ((txt (buffer-string))
  8028. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8029. lines ind)
  8030. (kill-buffer (current-buffer))
  8031. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8032. (setq txt (replace-match "" t t txt)))
  8033. (if (string-match "\\s-+\\'" txt)
  8034. (setq txt (replace-match "" t t txt)))
  8035. (setq lines (org-split-string txt "\n"))
  8036. (when (and note (string-match "\\S-" note))
  8037. (setq note
  8038. (org-replace-escapes
  8039. note
  8040. (list (cons "%u" (user-login-name))
  8041. (cons "%U" user-full-name)
  8042. (cons "%t" (format-time-string
  8043. (org-time-stamp-format 'long 'inactive)
  8044. (current-time)))
  8045. (cons "%s" (if org-log-note-state
  8046. (concat "\"" org-log-note-state "\"")
  8047. "")))))
  8048. (if lines (setq note (concat note " \\\\")))
  8049. (push note lines))
  8050. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8051. (when lines
  8052. (save-excursion
  8053. (set-buffer (marker-buffer org-log-note-marker))
  8054. (save-excursion
  8055. (goto-char org-log-note-marker)
  8056. (move-marker org-log-note-marker nil)
  8057. (end-of-line 1)
  8058. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8059. (indent-relative nil)
  8060. (insert "- " (pop lines))
  8061. (org-indent-line-function)
  8062. (beginning-of-line 1)
  8063. (looking-at "[ \t]*")
  8064. (setq ind (concat (match-string 0) " "))
  8065. (end-of-line 1)
  8066. (while lines (insert "\n" ind (pop lines)))))))
  8067. (set-window-configuration org-log-note-window-configuration)
  8068. (with-current-buffer (marker-buffer org-log-note-return-to)
  8069. (goto-char org-log-note-return-to))
  8070. (move-marker org-log-note-return-to nil)
  8071. (and org-log-post-message (message "%s" org-log-post-message)))
  8072. (defun org-sparse-tree (&optional arg)
  8073. "Create a sparse tree, prompt for the details.
  8074. This command can create sparse trees. You first need to select the type
  8075. of match used to create the tree:
  8076. t Show entries with a specific TODO keyword.
  8077. T Show entries selected by a tags match.
  8078. p Enter a property name and its value (both with completion on existing
  8079. names/values) and show entries with that property.
  8080. r Show entries matching a regular expression
  8081. d Show deadlines due within `org-deadline-warning-days'."
  8082. (interactive "P")
  8083. (let (ans kwd value)
  8084. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8085. (setq ans (read-char-exclusive))
  8086. (cond
  8087. ((equal ans ?d)
  8088. (call-interactively 'org-check-deadlines))
  8089. ((equal ans ?b)
  8090. (call-interactively 'org-check-before-date))
  8091. ((equal ans ?t)
  8092. (org-show-todo-tree '(4)))
  8093. ((equal ans ?T)
  8094. (call-interactively 'org-tags-sparse-tree))
  8095. ((member ans '(?p ?P))
  8096. (setq kwd (org-ido-completing-read "Property: "
  8097. (mapcar 'list (org-buffer-property-keys))))
  8098. (setq value (org-ido-completing-read "Value: "
  8099. (mapcar 'list (org-property-values kwd))))
  8100. (unless (string-match "\\`{.*}\\'" value)
  8101. (setq value (concat "\"" value "\"")))
  8102. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8103. ((member ans '(?r ?R ?/))
  8104. (call-interactively 'org-occur))
  8105. (t (error "No such sparse tree command \"%c\"" ans)))))
  8106. (defvar org-occur-highlights nil
  8107. "List of overlays used for occur matches.")
  8108. (make-variable-buffer-local 'org-occur-highlights)
  8109. (defvar org-occur-parameters nil
  8110. "Parameters of the active org-occur calls.
  8111. This is a list, each call to org-occur pushes as cons cell,
  8112. containing the regular expression and the callback, onto the list.
  8113. The list can contain several entries if `org-occur' has been called
  8114. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8115. will only contain one set of parameters. When the highlights are
  8116. removed (for example with `C-c C-c', or with the next edit (depending
  8117. on `org-remove-highlights-with-change'), this variable is emptied
  8118. as well.")
  8119. (make-variable-buffer-local 'org-occur-parameters)
  8120. (defun org-occur (regexp &optional keep-previous callback)
  8121. "Make a compact tree which shows all matches of REGEXP.
  8122. The tree will show the lines where the regexp matches, and all higher
  8123. headlines above the match. It will also show the heading after the match,
  8124. to make sure editing the matching entry is easy.
  8125. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8126. call to `org-occur' will be kept, to allow stacking of calls to this
  8127. command.
  8128. If CALLBACK is non-nil, it is a function which is called to confirm
  8129. that the match should indeed be shown."
  8130. (interactive "sRegexp: \nP")
  8131. (unless keep-previous
  8132. (org-remove-occur-highlights nil nil t))
  8133. (push (cons regexp callback) org-occur-parameters)
  8134. (let ((cnt 0))
  8135. (save-excursion
  8136. (goto-char (point-min))
  8137. (if (or (not keep-previous) ; do not want to keep
  8138. (not org-occur-highlights)) ; no previous matches
  8139. ;; hide everything
  8140. (org-overview))
  8141. (while (re-search-forward regexp nil t)
  8142. (when (or (not callback)
  8143. (save-match-data (funcall callback)))
  8144. (setq cnt (1+ cnt))
  8145. (when org-highlight-sparse-tree-matches
  8146. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8147. (org-show-context 'occur-tree))))
  8148. (when org-remove-highlights-with-change
  8149. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8150. nil 'local))
  8151. (unless org-sparse-tree-open-archived-trees
  8152. (org-hide-archived-subtrees (point-min) (point-max)))
  8153. (run-hooks 'org-occur-hook)
  8154. (if (interactive-p)
  8155. (message "%d match(es) for regexp %s" cnt regexp))
  8156. cnt))
  8157. (defun org-show-context (&optional key)
  8158. "Make sure point and context and visible.
  8159. How much context is shown depends upon the variables
  8160. `org-show-hierarchy-above', `org-show-following-heading'. and
  8161. `org-show-siblings'."
  8162. (let ((heading-p (org-on-heading-p t))
  8163. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8164. (following-p (org-get-alist-option org-show-following-heading key))
  8165. (entry-p (org-get-alist-option org-show-entry-below key))
  8166. (siblings-p (org-get-alist-option org-show-siblings key)))
  8167. (catch 'exit
  8168. ;; Show heading or entry text
  8169. (if (and heading-p (not entry-p))
  8170. (org-flag-heading nil) ; only show the heading
  8171. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8172. (org-show-hidden-entry))) ; show entire entry
  8173. (when following-p
  8174. ;; Show next sibling, or heading below text
  8175. (save-excursion
  8176. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8177. (org-flag-heading nil))))
  8178. (when siblings-p (org-show-siblings))
  8179. (when hierarchy-p
  8180. ;; show all higher headings, possibly with siblings
  8181. (save-excursion
  8182. (while (and (condition-case nil
  8183. (progn (org-up-heading-all 1) t)
  8184. (error nil))
  8185. (not (bobp)))
  8186. (org-flag-heading nil)
  8187. (when siblings-p (org-show-siblings))))))))
  8188. (defun org-reveal (&optional siblings)
  8189. "Show current entry, hierarchy above it, and the following headline.
  8190. This can be used to show a consistent set of context around locations
  8191. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8192. not t for the search context.
  8193. With optional argument SIBLINGS, on each level of the hierarchy all
  8194. siblings are shown. This repairs the tree structure to what it would
  8195. look like when opened with hierarchical calls to `org-cycle'."
  8196. (interactive "P")
  8197. (let ((org-show-hierarchy-above t)
  8198. (org-show-following-heading t)
  8199. (org-show-siblings (if siblings t org-show-siblings)))
  8200. (org-show-context nil)))
  8201. (defun org-highlight-new-match (beg end)
  8202. "Highlight from BEG to END and mark the highlight is an occur headline."
  8203. (let ((ov (org-make-overlay beg end)))
  8204. (org-overlay-put ov 'face 'secondary-selection)
  8205. (push ov org-occur-highlights)))
  8206. (defun org-remove-occur-highlights (&optional beg end noremove)
  8207. "Remove the occur highlights from the buffer.
  8208. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8209. from the `before-change-functions' in the current buffer."
  8210. (interactive)
  8211. (unless org-inhibit-highlight-removal
  8212. (mapc 'org-delete-overlay org-occur-highlights)
  8213. (setq org-occur-highlights nil)
  8214. (setq org-occur-parameters nil)
  8215. (unless noremove
  8216. (remove-hook 'before-change-functions
  8217. 'org-remove-occur-highlights 'local))))
  8218. ;;;; Priorities
  8219. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8220. "Regular expression matching the priority indicator.")
  8221. (defvar org-remove-priority-next-time nil)
  8222. (defun org-priority-up ()
  8223. "Increase the priority of the current item."
  8224. (interactive)
  8225. (org-priority 'up))
  8226. (defun org-priority-down ()
  8227. "Decrease the priority of the current item."
  8228. (interactive)
  8229. (org-priority 'down))
  8230. (defun org-priority (&optional action)
  8231. "Change the priority of an item by ARG.
  8232. ACTION can be `set', `up', `down', or a character."
  8233. (interactive)
  8234. (setq action (or action 'set))
  8235. (let (current new news have remove)
  8236. (save-excursion
  8237. (org-back-to-heading)
  8238. (if (looking-at org-priority-regexp)
  8239. (setq current (string-to-char (match-string 2))
  8240. have t)
  8241. (setq current org-default-priority))
  8242. (cond
  8243. ((or (eq action 'set)
  8244. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8245. (if (not (eq action 'set))
  8246. (setq new action)
  8247. (message "Priority %c-%c, SPC to remove: "
  8248. org-highest-priority org-lowest-priority)
  8249. (setq new (read-char-exclusive)))
  8250. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8251. (= (upcase org-lowest-priority) org-lowest-priority))
  8252. (setq new (upcase new)))
  8253. (cond ((equal new ?\ ) (setq remove t))
  8254. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8255. (error "Priority must be between `%c' and `%c'"
  8256. org-highest-priority org-lowest-priority))))
  8257. ((eq action 'up)
  8258. (if (and (not have) (eq last-command this-command))
  8259. (setq new org-lowest-priority)
  8260. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8261. org-default-priority (1- current)))))
  8262. ((eq action 'down)
  8263. (if (and (not have) (eq last-command this-command))
  8264. (setq new org-highest-priority)
  8265. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8266. org-default-priority (1+ current)))))
  8267. (t (error "Invalid action")))
  8268. (if (or (< (upcase new) org-highest-priority)
  8269. (> (upcase new) org-lowest-priority))
  8270. (setq remove t))
  8271. (setq news (format "%c" new))
  8272. (if have
  8273. (if remove
  8274. (replace-match "" t t nil 1)
  8275. (replace-match news t t nil 2))
  8276. (if remove
  8277. (error "No priority cookie found in line")
  8278. (looking-at org-todo-line-regexp)
  8279. (if (match-end 2)
  8280. (progn
  8281. (goto-char (match-end 2))
  8282. (insert " [#" news "]"))
  8283. (goto-char (match-beginning 3))
  8284. (insert "[#" news "] ")))))
  8285. (org-preserve-lc (org-set-tags nil 'align))
  8286. (if remove
  8287. (message "Priority removed")
  8288. (message "Priority of current item set to %s" news))))
  8289. (defun org-get-priority (s)
  8290. "Find priority cookie and return priority."
  8291. (save-match-data
  8292. (if (not (string-match org-priority-regexp s))
  8293. (* 1000 (- org-lowest-priority org-default-priority))
  8294. (* 1000 (- org-lowest-priority
  8295. (string-to-char (match-string 2 s)))))))
  8296. ;;;; Tags
  8297. (defvar org-agenda-archives-mode)
  8298. (defun org-scan-tags (action matcher &optional todo-only)
  8299. "Scan headline tags with inheritance and produce output ACTION.
  8300. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8301. or `agenda' to produce an entry list for an agenda view. It can also be
  8302. a Lisp form or a function that should be called at each matched headline, in
  8303. this case the return value is a list of all return values from these calls.
  8304. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8305. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8306. only lines with a TODO keyword are included in the output."
  8307. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8308. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8309. (org-re
  8310. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8311. (props (list 'face 'default
  8312. 'done-face 'org-done
  8313. 'undone-face 'default
  8314. 'mouse-face 'highlight
  8315. 'org-not-done-regexp org-not-done-regexp
  8316. 'org-todo-regexp org-todo-regexp
  8317. 'keymap org-agenda-keymap
  8318. 'help-echo
  8319. (format "mouse-2 or RET jump to org file %s"
  8320. (abbreviate-file-name
  8321. (or (buffer-file-name (buffer-base-buffer))
  8322. (buffer-name (buffer-base-buffer)))))))
  8323. (case-fold-search nil)
  8324. lspos tags tags-list
  8325. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8326. (llast 0) rtn rtn1 level category i txt
  8327. todo marker entry priority)
  8328. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8329. (setq action (list 'lambda nil action)))
  8330. (save-excursion
  8331. (goto-char (point-min))
  8332. (when (eq action 'sparse-tree)
  8333. (org-overview)
  8334. (org-remove-occur-highlights))
  8335. (while (re-search-forward re nil t)
  8336. (catch :skip
  8337. (setq todo (if (match-end 1) (match-string 2))
  8338. tags (if (match-end 4) (match-string 4)))
  8339. (goto-char (setq lspos (1+ (match-beginning 0))))
  8340. (setq level (org-reduced-level (funcall outline-level))
  8341. category (org-get-category))
  8342. (setq i llast llast level)
  8343. ;; remove tag lists from same and sublevels
  8344. (while (>= i level)
  8345. (when (setq entry (assoc i tags-alist))
  8346. (setq tags-alist (delete entry tags-alist)))
  8347. (setq i (1- i)))
  8348. ;; add the next tags
  8349. (when tags
  8350. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8351. tags-alist
  8352. (cons (cons level tags) tags-alist)))
  8353. ;; compile tags for current headline
  8354. (setq tags-list
  8355. (if org-use-tag-inheritance
  8356. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8357. tags))
  8358. (when org-use-tag-inheritance
  8359. (setcdr (car tags-alist)
  8360. (mapcar (lambda (x)
  8361. (setq x (copy-sequence x))
  8362. (org-add-prop-inherited x))
  8363. (cdar tags-alist))))
  8364. (when (and tags org-use-tag-inheritance
  8365. (not (eq t org-use-tag-inheritance)))
  8366. ;; selective inheritance, remove uninherited ones
  8367. (setcdr (car tags-alist)
  8368. (org-remove-uniherited-tags (cdar tags-alist))))
  8369. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8370. (let ((case-fold-search t)) (eval matcher))
  8371. (or
  8372. (not (member org-archive-tag tags-list))
  8373. ;; we have an archive tag, should we use this anyway?
  8374. (or (not org-agenda-skip-archived-trees)
  8375. (and (eq action 'agenda) org-agenda-archives-mode))))
  8376. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8377. ;; select this headline
  8378. (cond
  8379. ((eq action 'sparse-tree)
  8380. (and org-highlight-sparse-tree-matches
  8381. (org-get-heading) (match-end 0)
  8382. (org-highlight-new-match
  8383. (match-beginning 0) (match-beginning 1)))
  8384. (org-show-context 'tags-tree))
  8385. ((eq action 'agenda)
  8386. (setq txt (org-format-agenda-item
  8387. ""
  8388. (concat
  8389. (if org-tags-match-list-sublevels
  8390. (make-string (1- level) ?.) "")
  8391. (org-get-heading))
  8392. category (org-get-tags-at))
  8393. priority (org-get-priority txt))
  8394. (goto-char lspos)
  8395. (setq marker (org-agenda-new-marker))
  8396. (org-add-props txt props
  8397. 'org-marker marker 'org-hd-marker marker 'org-category category
  8398. 'priority priority 'type "tagsmatch")
  8399. (push txt rtn))
  8400. ((functionp action)
  8401. (save-excursion
  8402. (setq rtn1 (funcall action))
  8403. (push rtn1 rtn))
  8404. (goto-char (point-at-eol)))
  8405. (t (error "Invalid action")))
  8406. ;; if we are to skip sublevels, jump to end of subtree
  8407. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8408. (when (and (eq action 'sparse-tree)
  8409. (not org-sparse-tree-open-archived-trees))
  8410. (org-hide-archived-subtrees (point-min) (point-max)))
  8411. (nreverse rtn)))
  8412. (defun org-remove-uniherited-tags (tags)
  8413. "Remove all tags that are not inherited from the list TAGS."
  8414. (cond
  8415. ((eq org-use-tag-inheritance t)
  8416. (if org-tags-exclude-from-inheritance
  8417. (org-delete-all org-tags-exclude-from-inheritance tags)
  8418. tags))
  8419. ((not org-use-tag-inheritance) nil)
  8420. ((stringp org-use-tag-inheritance)
  8421. (delq nil (mapcar
  8422. (lambda (x)
  8423. (if (and (string-match org-use-tag-inheritance x)
  8424. (not (member x org-tags-exclude-from-inheritance)))
  8425. x nil))
  8426. tags)))
  8427. ((listp org-use-tag-inheritance)
  8428. (delq nil (mapcar
  8429. (lambda (x)
  8430. (if (member x org-use-tag-inheritance) x nil))
  8431. tags)))))
  8432. (defvar todo-only) ;; dynamically scoped
  8433. (defun org-tags-sparse-tree (&optional todo-only match)
  8434. "Create a sparse tree according to tags string MATCH.
  8435. MATCH can contain positive and negative selection of tags, like
  8436. \"+WORK+URGENT-WITHBOSS\".
  8437. If optional argument TODO-ONLY is non-nil, only select lines that are
  8438. also TODO lines."
  8439. (interactive "P")
  8440. (org-prepare-agenda-buffers (list (current-buffer)))
  8441. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8442. (defvar org-cached-props nil)
  8443. (defun org-cached-entry-get (pom property)
  8444. (if (or (eq t org-use-property-inheritance)
  8445. (and (stringp org-use-property-inheritance)
  8446. (string-match org-use-property-inheritance property))
  8447. (and (listp org-use-property-inheritance)
  8448. (member property org-use-property-inheritance)))
  8449. ;; Caching is not possible, check it directly
  8450. (org-entry-get pom property 'inherit)
  8451. ;; Get all properties, so that we can do complicated checks easily
  8452. (cdr (assoc property (or org-cached-props
  8453. (setq org-cached-props
  8454. (org-entry-properties pom)))))))
  8455. (defun org-global-tags-completion-table (&optional files)
  8456. "Return the list of all tags in all agenda buffer/files."
  8457. (save-excursion
  8458. (org-uniquify
  8459. (delq nil
  8460. (apply 'append
  8461. (mapcar
  8462. (lambda (file)
  8463. (set-buffer (find-file-noselect file))
  8464. (append (org-get-buffer-tags)
  8465. (mapcar (lambda (x) (if (stringp (car-safe x))
  8466. (list (car-safe x)) nil))
  8467. org-tag-alist)))
  8468. (if (and files (car files))
  8469. files
  8470. (org-agenda-files))))))))
  8471. (defun org-make-tags-matcher (match)
  8472. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8473. ;; todo-only is scoped dynamically into this function, and the function
  8474. ;; may change it if the matcher asks for it.
  8475. (unless match
  8476. ;; Get a new match request, with completion
  8477. (let ((org-last-tags-completion-table
  8478. (org-global-tags-completion-table)))
  8479. (setq match (org-ido-completing-read
  8480. "Match: " 'org-tags-completion-function nil nil nil
  8481. 'org-tags-history))))
  8482. ;; Parse the string and create a lisp form
  8483. (let ((match0 match)
  8484. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8485. minus tag mm
  8486. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8487. orterms term orlist re-p str-p level-p level-op time-p
  8488. prop-p pn pv po cat-p gv rest)
  8489. (if (string-match "/+" match)
  8490. ;; match contains also a todo-matching request
  8491. (progn
  8492. (setq tagsmatch (substring match 0 (match-beginning 0))
  8493. todomatch (substring match (match-end 0)))
  8494. (if (string-match "^!" todomatch)
  8495. (setq todo-only t todomatch (substring todomatch 1)))
  8496. (if (string-match "^\\s-*$" todomatch)
  8497. (setq todomatch nil)))
  8498. ;; only matching tags
  8499. (setq tagsmatch match todomatch nil))
  8500. ;; Make the tags matcher
  8501. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8502. (setq tagsmatcher t)
  8503. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8504. (while (setq term (pop orterms))
  8505. (while (and (equal (substring term -1) "\\") orterms)
  8506. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8507. (while (string-match re term)
  8508. (setq rest (substring term (match-end 0))
  8509. minus (and (match-end 1)
  8510. (equal (match-string 1 term) "-"))
  8511. tag (match-string 2 term)
  8512. re-p (equal (string-to-char tag) ?{)
  8513. level-p (match-end 4)
  8514. prop-p (match-end 5)
  8515. mm (cond
  8516. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8517. (level-p
  8518. (setq level-op (org-op-to-function (match-string 3 term)))
  8519. `(,level-op level ,(string-to-number
  8520. (match-string 4 term))))
  8521. (prop-p
  8522. (setq pn (match-string 5 term)
  8523. po (match-string 6 term)
  8524. pv (match-string 7 term)
  8525. cat-p (equal pn "CATEGORY")
  8526. re-p (equal (string-to-char pv) ?{)
  8527. str-p (equal (string-to-char pv) ?\")
  8528. time-p (save-match-data
  8529. (string-match "^\"[[<].*[]>]\"$" pv))
  8530. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8531. (if time-p (setq pv (org-matcher-time pv)))
  8532. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8533. (cond
  8534. ((equal pn "CATEGORY")
  8535. (setq gv '(get-text-property (point) 'org-category)))
  8536. ((equal pn "TODO")
  8537. (setq gv 'todo))
  8538. (t
  8539. (setq gv `(org-cached-entry-get nil ,pn))))
  8540. (if re-p
  8541. (if (eq po 'org<>)
  8542. `(not (string-match ,pv (or ,gv "")))
  8543. `(string-match ,pv (or ,gv "")))
  8544. (if str-p
  8545. `(,po (or ,gv "") ,pv)
  8546. `(,po (string-to-number (or ,gv ""))
  8547. ,(string-to-number pv) ))))
  8548. (t `(member ,(downcase tag) tags-list)))
  8549. mm (if minus (list 'not mm) mm)
  8550. term rest)
  8551. (push mm tagsmatcher))
  8552. (push (if (> (length tagsmatcher) 1)
  8553. (cons 'and tagsmatcher)
  8554. (car tagsmatcher))
  8555. orlist)
  8556. (setq tagsmatcher nil))
  8557. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8558. (setq tagsmatcher
  8559. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8560. ;; Make the todo matcher
  8561. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8562. (setq todomatcher t)
  8563. (setq orterms (org-split-string todomatch "|") orlist nil)
  8564. (while (setq term (pop orterms))
  8565. (while (string-match re term)
  8566. (setq minus (and (match-end 1)
  8567. (equal (match-string 1 term) "-"))
  8568. kwd (match-string 2 term)
  8569. re-p (equal (string-to-char kwd) ?{)
  8570. term (substring term (match-end 0))
  8571. mm (if re-p
  8572. `(string-match ,(substring kwd 1 -1) todo)
  8573. (list 'equal 'todo kwd))
  8574. mm (if minus (list 'not mm) mm))
  8575. (push mm todomatcher))
  8576. (push (if (> (length todomatcher) 1)
  8577. (cons 'and todomatcher)
  8578. (car todomatcher))
  8579. orlist)
  8580. (setq todomatcher nil))
  8581. (setq todomatcher (if (> (length orlist) 1)
  8582. (cons 'or orlist) (car orlist))))
  8583. ;; Return the string and lisp forms of the matcher
  8584. (setq matcher (if todomatcher
  8585. (list 'and tagsmatcher todomatcher)
  8586. tagsmatcher))
  8587. (cons match0 matcher)))
  8588. (defun org-op-to-function (op &optional stringp)
  8589. "Turn an operator into the appropriate function."
  8590. (setq op
  8591. (cond
  8592. ((equal op "<" ) '(< string< org-time<))
  8593. ((equal op ">" ) '(> org-string> org-time>))
  8594. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8595. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8596. ((member op '("=" "==")) '(= string= org-time=))
  8597. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8598. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8599. (defun org<> (a b) (not (= a b)))
  8600. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8601. (defun org-string>= (a b) (not (string< a b)))
  8602. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8603. (defun org-string<> (a b) (not (string= a b)))
  8604. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8605. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8606. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8607. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8608. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8609. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8610. (defun org-2ft (s)
  8611. "Convert S to a floating point time.
  8612. If S is already a number, just return it. If it is a string, parse
  8613. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8614. (cond
  8615. ((numberp s) s)
  8616. ((stringp s)
  8617. (condition-case nil
  8618. (float-time (apply 'encode-time (org-parse-time-string s)))
  8619. (error 0.)))
  8620. (t 0.)))
  8621. (defun org-time-today ()
  8622. "Time in seconds today at 0:00.
  8623. Returns the float number of seconds since the beginning of the
  8624. epoch to the beginning of today (00:00)."
  8625. (float-time (apply 'encode-time
  8626. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8627. (defun org-matcher-time (s)
  8628. "Interpret a time comparison value."
  8629. (save-match-data
  8630. (cond
  8631. ((string= s "<now>") (float-time))
  8632. ((string= s "<today>") (org-time-today))
  8633. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8634. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8635. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8636. (+ (org-time-today)
  8637. (* (string-to-number (match-string 1 s))
  8638. (cdr (assoc (match-string 2 s)
  8639. '(("d" . 86400.0) ("w" . 604800.0)
  8640. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8641. (t (org-2ft s)))))
  8642. (defun org-match-any-p (re list)
  8643. "Does re match any element of list?"
  8644. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8645. (delq nil list))
  8646. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8647. (defvar org-tags-overlay (org-make-overlay 1 1))
  8648. (org-detach-overlay org-tags-overlay)
  8649. (defun org-get-local-tags-at (&optional pos)
  8650. "Get a list of tags defined in the current headline."
  8651. (org-get-tags-at pos 'local))
  8652. (defun org-get-local-tags ()
  8653. "Get a list of tags defined in the current headline."
  8654. (org-get-tags-at nil 'local))
  8655. (defun org-get-tags-at (&optional pos local)
  8656. "Get a list of all headline tags applicable at POS.
  8657. POS defaults to point. If tags are inherited, the list contains
  8658. the targets in the same sequence as the headlines appear, i.e.
  8659. the tags of the current headline come last.
  8660. When LOCAL is non-nil, only return tags from the current headline,
  8661. ignore inherited ones."
  8662. (interactive)
  8663. (let (tags ltags lastpos parent)
  8664. (save-excursion
  8665. (save-restriction
  8666. (widen)
  8667. (goto-char (or pos (point)))
  8668. (save-match-data
  8669. (catch 'done
  8670. (condition-case nil
  8671. (progn
  8672. (org-back-to-heading t)
  8673. (while (not (equal lastpos (point)))
  8674. (setq lastpos (point))
  8675. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8676. (setq ltags (org-split-string
  8677. (org-match-string-no-properties 1) ":"))
  8678. (when parent
  8679. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8680. (setq tags (append
  8681. (if parent
  8682. (org-remove-uniherited-tags ltags)
  8683. ltags)
  8684. tags)))
  8685. (or org-use-tag-inheritance (throw 'done t))
  8686. (if local (throw 'done t))
  8687. (org-up-heading-all 1)
  8688. (setq parent t)))
  8689. (error nil)))))
  8690. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8691. (defun org-add-prop-inherited (s)
  8692. (add-text-properties 0 (length s) '(inherited t) s)
  8693. s)
  8694. (defun org-toggle-tag (tag &optional onoff)
  8695. "Toggle the tag TAG for the current line.
  8696. If ONOFF is `on' or `off', don't toggle but set to this state."
  8697. (let (res current)
  8698. (save-excursion
  8699. (org-back-to-heading t)
  8700. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8701. (point-at-eol) t)
  8702. (progn
  8703. (setq current (match-string 1))
  8704. (replace-match ""))
  8705. (setq current ""))
  8706. (setq current (nreverse (org-split-string current ":")))
  8707. (cond
  8708. ((eq onoff 'on)
  8709. (setq res t)
  8710. (or (member tag current) (push tag current)))
  8711. ((eq onoff 'off)
  8712. (or (not (member tag current)) (setq current (delete tag current))))
  8713. (t (if (member tag current)
  8714. (setq current (delete tag current))
  8715. (setq res t)
  8716. (push tag current))))
  8717. (end-of-line 1)
  8718. (if current
  8719. (progn
  8720. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8721. (org-set-tags nil t))
  8722. (delete-horizontal-space))
  8723. (run-hooks 'org-after-tags-change-hook))
  8724. res))
  8725. (defun org-align-tags-here (to-col)
  8726. ;; Assumes that this is a headline
  8727. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8728. (beginning-of-line 1)
  8729. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8730. (< pos (match-beginning 2)))
  8731. (progn
  8732. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8733. (goto-char (match-beginning 1))
  8734. (insert " ")
  8735. (delete-region (point) (1+ (match-beginning 2)))
  8736. (setq ncol (max (1+ (current-column))
  8737. (1+ col)
  8738. (if (> to-col 0)
  8739. to-col
  8740. (- (abs to-col) tags-l))))
  8741. (setq p (point))
  8742. (insert (make-string (- ncol (current-column)) ?\ ))
  8743. (setq ncol (current-column))
  8744. (when indent-tabs-mode (tabify p (point-at-eol)))
  8745. (org-move-to-column (min ncol col) t))
  8746. (goto-char pos))))
  8747. (defun org-set-tags-command (&optional arg just-align)
  8748. "Call the set-tags command for the current entry."
  8749. (interactive "P")
  8750. (if (org-on-heading-p)
  8751. (org-set-tags arg just-align)
  8752. (save-excursion
  8753. (org-back-to-heading t)
  8754. (org-set-tags arg just-align))))
  8755. (defun org-set-tags (&optional arg just-align)
  8756. "Set the tags for the current headline.
  8757. With prefix ARG, realign all tags in headings in the current buffer."
  8758. (interactive "P")
  8759. (let* ((re (concat "^" outline-regexp))
  8760. (current (org-get-tags-string))
  8761. (col (current-column))
  8762. (org-setting-tags t)
  8763. table current-tags inherited-tags ; computed below when needed
  8764. tags p0 c0 c1 rpl)
  8765. (if arg
  8766. (save-excursion
  8767. (goto-char (point-min))
  8768. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8769. (while (re-search-forward re nil t)
  8770. (org-set-tags nil t)
  8771. (end-of-line 1)))
  8772. (message "All tags realigned to column %d" org-tags-column))
  8773. (if just-align
  8774. (setq tags current)
  8775. ;; Get a new set of tags from the user
  8776. (save-excursion
  8777. (setq table (or org-tag-alist (org-get-buffer-tags))
  8778. org-last-tags-completion-table table
  8779. current-tags (org-split-string current ":")
  8780. inherited-tags (nreverse
  8781. (nthcdr (length current-tags)
  8782. (nreverse (org-get-tags-at))))
  8783. tags
  8784. (if (or (eq t org-use-fast-tag-selection)
  8785. (and org-use-fast-tag-selection
  8786. (delq nil (mapcar 'cdr table))))
  8787. (org-fast-tag-selection
  8788. current-tags inherited-tags table
  8789. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8790. (let ((org-add-colon-after-tag-completion t))
  8791. (org-trim
  8792. (org-without-partial-completion
  8793. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8794. nil nil current 'org-tags-history)))))))
  8795. (while (string-match "[-+&]+" tags)
  8796. ;; No boolean logic, just a list
  8797. (setq tags (replace-match ":" t t tags))))
  8798. (if (string-match "\\`[\t ]*\\'" tags)
  8799. (setq tags "")
  8800. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8801. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8802. ;; Insert new tags at the correct column
  8803. (beginning-of-line 1)
  8804. (cond
  8805. ((and (equal current "") (equal tags "")))
  8806. ((re-search-forward
  8807. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8808. (point-at-eol) t)
  8809. (if (equal tags "")
  8810. (setq rpl "")
  8811. (goto-char (match-beginning 0))
  8812. (setq c0 (current-column) p0 (point)
  8813. c1 (max (1+ c0) (if (> org-tags-column 0)
  8814. org-tags-column
  8815. (- (- org-tags-column) (length tags))))
  8816. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8817. (replace-match rpl t t)
  8818. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8819. tags)
  8820. (t (error "Tags alignment failed")))
  8821. (org-move-to-column col)
  8822. (unless just-align
  8823. (run-hooks 'org-after-tags-change-hook)))))
  8824. (defun org-change-tag-in-region (beg end tag off)
  8825. "Add or remove TAG for each entry in the region.
  8826. This works in the agenda, and also in an org-mode buffer."
  8827. (interactive
  8828. (list (region-beginning) (region-end)
  8829. (let ((org-last-tags-completion-table
  8830. (if (org-mode-p)
  8831. (org-get-buffer-tags)
  8832. (org-global-tags-completion-table))))
  8833. (org-ido-completing-read
  8834. "Tag: " 'org-tags-completion-function nil nil nil
  8835. 'org-tags-history))
  8836. (progn
  8837. (message "[s]et or [r]emove? ")
  8838. (equal (read-char-exclusive) ?r))))
  8839. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8840. (let ((agendap (equal major-mode 'org-agenda-mode))
  8841. l1 l2 m buf pos newhead (cnt 0))
  8842. (goto-char end)
  8843. (setq l2 (1- (org-current-line)))
  8844. (goto-char beg)
  8845. (setq l1 (org-current-line))
  8846. (loop for l from l1 to l2 do
  8847. (goto-line l)
  8848. (setq m (get-text-property (point) 'org-hd-marker))
  8849. (when (or (and (org-mode-p) (org-on-heading-p))
  8850. (and agendap m))
  8851. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8852. pos (if agendap m (point)))
  8853. (with-current-buffer buf
  8854. (save-excursion
  8855. (save-restriction
  8856. (goto-char pos)
  8857. (setq cnt (1+ cnt))
  8858. (org-toggle-tag tag (if off 'off 'on))
  8859. (setq newhead (org-get-heading)))))
  8860. (and agendap (org-agenda-change-all-lines newhead m))))
  8861. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8862. (defun org-tags-completion-function (string predicate &optional flag)
  8863. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8864. (confirm (lambda (x) (stringp (car x)))))
  8865. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8866. (setq s1 (match-string 1 string)
  8867. s2 (match-string 2 string))
  8868. (setq s1 "" s2 string))
  8869. (cond
  8870. ((eq flag nil)
  8871. ;; try completion
  8872. (setq rtn (try-completion s2 ctable confirm))
  8873. (if (stringp rtn)
  8874. (setq rtn
  8875. (concat s1 s2 (substring rtn (length s2))
  8876. (if (and org-add-colon-after-tag-completion
  8877. (assoc rtn ctable))
  8878. ":" ""))))
  8879. rtn)
  8880. ((eq flag t)
  8881. ;; all-completions
  8882. (all-completions s2 ctable confirm)
  8883. )
  8884. ((eq flag 'lambda)
  8885. ;; exact match?
  8886. (assoc s2 ctable)))
  8887. ))
  8888. (defun org-fast-tag-insert (kwd tags face &optional end)
  8889. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  8890. (insert (format "%-12s" (concat kwd ":"))
  8891. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8892. (or end "")))
  8893. (defun org-fast-tag-show-exit (flag)
  8894. (save-excursion
  8895. (goto-line 3)
  8896. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8897. (replace-match ""))
  8898. (when flag
  8899. (end-of-line 1)
  8900. (org-move-to-column (- (window-width) 19) t)
  8901. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8902. (defun org-set-current-tags-overlay (current prefix)
  8903. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8904. (if (featurep 'xemacs)
  8905. (org-overlay-display org-tags-overlay (concat prefix s)
  8906. 'secondary-selection)
  8907. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8908. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8909. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8910. "Fast tag selection with single keys.
  8911. CURRENT is the current list of tags in the headline, INHERITED is the
  8912. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8913. possibly with grouping information. TODO-TABLE is a similar table with
  8914. TODO keywords, should these have keys assigned to them.
  8915. If the keys are nil, a-z are automatically assigned.
  8916. Returns the new tags string, or nil to not change the current settings."
  8917. (let* ((fulltable (append table todo-table))
  8918. (maxlen (apply 'max (mapcar
  8919. (lambda (x)
  8920. (if (stringp (car x)) (string-width (car x)) 0))
  8921. fulltable)))
  8922. (buf (current-buffer))
  8923. (expert (eq org-fast-tag-selection-single-key 'expert))
  8924. (buffer-tags nil)
  8925. (fwidth (+ maxlen 3 1 3))
  8926. (ncol (/ (- (window-width) 4) fwidth))
  8927. (i-face 'org-done)
  8928. (c-face 'org-todo)
  8929. tg cnt e c char c1 c2 ntable tbl rtn
  8930. ov-start ov-end ov-prefix
  8931. (exit-after-next org-fast-tag-selection-single-key)
  8932. (done-keywords org-done-keywords)
  8933. groups ingroup)
  8934. (save-excursion
  8935. (beginning-of-line 1)
  8936. (if (looking-at
  8937. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8938. (setq ov-start (match-beginning 1)
  8939. ov-end (match-end 1)
  8940. ov-prefix "")
  8941. (setq ov-start (1- (point-at-eol))
  8942. ov-end (1+ ov-start))
  8943. (skip-chars-forward "^\n\r")
  8944. (setq ov-prefix
  8945. (concat
  8946. (buffer-substring (1- (point)) (point))
  8947. (if (> (current-column) org-tags-column)
  8948. " "
  8949. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8950. (org-move-overlay org-tags-overlay ov-start ov-end)
  8951. (save-window-excursion
  8952. (if expert
  8953. (set-buffer (get-buffer-create " *Org tags*"))
  8954. (delete-other-windows)
  8955. (split-window-vertically)
  8956. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8957. (erase-buffer)
  8958. (org-set-local 'org-done-keywords done-keywords)
  8959. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8960. (org-fast-tag-insert "Current" current c-face "\n\n")
  8961. (org-fast-tag-show-exit exit-after-next)
  8962. (org-set-current-tags-overlay current ov-prefix)
  8963. (setq tbl fulltable char ?a cnt 0)
  8964. (while (setq e (pop tbl))
  8965. (cond
  8966. ((equal e '(:startgroup))
  8967. (push '() groups) (setq ingroup t)
  8968. (when (not (= cnt 0))
  8969. (setq cnt 0)
  8970. (insert "\n"))
  8971. (insert "{ "))
  8972. ((equal e '(:endgroup))
  8973. (setq ingroup nil cnt 0)
  8974. (insert "}\n"))
  8975. (t
  8976. (setq tg (car e) c2 nil)
  8977. (if (cdr e)
  8978. (setq c (cdr e))
  8979. ;; automatically assign a character.
  8980. (setq c1 (string-to-char
  8981. (downcase (substring
  8982. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8983. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8984. (while (or (rassoc char ntable) (rassoc char table))
  8985. (setq char (1+ char)))
  8986. (setq c2 c1))
  8987. (setq c (or c2 char)))
  8988. (if ingroup (push tg (car groups)))
  8989. (setq tg (org-add-props tg nil 'face
  8990. (cond
  8991. ((not (assoc tg table))
  8992. (org-get-todo-face tg))
  8993. ((member tg current) c-face)
  8994. ((member tg inherited) i-face)
  8995. (t nil))))
  8996. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8997. (insert "[" c "] " tg (make-string
  8998. (- fwidth 4 (length tg)) ?\ ))
  8999. (push (cons tg c) ntable)
  9000. (when (= (setq cnt (1+ cnt)) ncol)
  9001. (insert "\n")
  9002. (if ingroup (insert " "))
  9003. (setq cnt 0)))))
  9004. (setq ntable (nreverse ntable))
  9005. (insert "\n")
  9006. (goto-char (point-min))
  9007. (if (not expert) (org-fit-window-to-buffer))
  9008. (setq rtn
  9009. (catch 'exit
  9010. (while t
  9011. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9012. (if groups " [!] no groups" " [!]groups")
  9013. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9014. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9015. (cond
  9016. ((= c ?\r) (throw 'exit t))
  9017. ((= c ?!)
  9018. (setq groups (not groups))
  9019. (goto-char (point-min))
  9020. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9021. ((= c ?\C-c)
  9022. (if (not expert)
  9023. (org-fast-tag-show-exit
  9024. (setq exit-after-next (not exit-after-next)))
  9025. (setq expert nil)
  9026. (delete-other-windows)
  9027. (split-window-vertically)
  9028. (org-switch-to-buffer-other-window " *Org tags*")
  9029. (org-fit-window-to-buffer)))
  9030. ((or (= c ?\C-g)
  9031. (and (= c ?q) (not (rassoc c ntable))))
  9032. (org-detach-overlay org-tags-overlay)
  9033. (setq quit-flag t))
  9034. ((= c ?\ )
  9035. (setq current nil)
  9036. (if exit-after-next (setq exit-after-next 'now)))
  9037. ((= c ?\t)
  9038. (condition-case nil
  9039. (setq tg (org-ido-completing-read
  9040. "Tag: "
  9041. (or buffer-tags
  9042. (with-current-buffer buf
  9043. (org-get-buffer-tags)))))
  9044. (quit (setq tg "")))
  9045. (when (string-match "\\S-" tg)
  9046. (add-to-list 'buffer-tags (list tg))
  9047. (if (member tg current)
  9048. (setq current (delete tg current))
  9049. (push tg current)))
  9050. (if exit-after-next (setq exit-after-next 'now)))
  9051. ((setq e (rassoc c todo-table) tg (car e))
  9052. (with-current-buffer buf
  9053. (save-excursion (org-todo tg)))
  9054. (if exit-after-next (setq exit-after-next 'now)))
  9055. ((setq e (rassoc c ntable) tg (car e))
  9056. (if (member tg current)
  9057. (setq current (delete tg current))
  9058. (loop for g in groups do
  9059. (if (member tg g)
  9060. (mapc (lambda (x)
  9061. (setq current (delete x current)))
  9062. g)))
  9063. (push tg current))
  9064. (if exit-after-next (setq exit-after-next 'now))))
  9065. ;; Create a sorted list
  9066. (setq current
  9067. (sort current
  9068. (lambda (a b)
  9069. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9070. (if (eq exit-after-next 'now) (throw 'exit t))
  9071. (goto-char (point-min))
  9072. (beginning-of-line 2)
  9073. (delete-region (point) (point-at-eol))
  9074. (org-fast-tag-insert "Current" current c-face)
  9075. (org-set-current-tags-overlay current ov-prefix)
  9076. (while (re-search-forward
  9077. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9078. (setq tg (match-string 1))
  9079. (add-text-properties
  9080. (match-beginning 1) (match-end 1)
  9081. (list 'face
  9082. (cond
  9083. ((member tg current) c-face)
  9084. ((member tg inherited) i-face)
  9085. (t (get-text-property (match-beginning 1) 'face))))))
  9086. (goto-char (point-min)))))
  9087. (org-detach-overlay org-tags-overlay)
  9088. (if rtn
  9089. (mapconcat 'identity current ":")
  9090. nil))))
  9091. (defun org-get-tags-string ()
  9092. "Get the TAGS string in the current headline."
  9093. (unless (org-on-heading-p t)
  9094. (error "Not on a heading"))
  9095. (save-excursion
  9096. (beginning-of-line 1)
  9097. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9098. (org-match-string-no-properties 1)
  9099. "")))
  9100. (defun org-get-tags ()
  9101. "Get the list of tags specified in the current headline."
  9102. (org-split-string (org-get-tags-string) ":"))
  9103. (defun org-get-buffer-tags ()
  9104. "Get a table of all tags used in the buffer, for completion."
  9105. (let (tags)
  9106. (save-excursion
  9107. (goto-char (point-min))
  9108. (while (re-search-forward
  9109. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9110. (when (equal (char-after (point-at-bol 0)) ?*)
  9111. (mapc (lambda (x) (add-to-list 'tags x))
  9112. (org-split-string (org-match-string-no-properties 1) ":")))))
  9113. (mapcar 'list tags)))
  9114. ;;;; The mapping API
  9115. ;;;###autoload
  9116. (defun org-map-entries (func &optional match scope &rest skip)
  9117. "Call FUNC at each headline selected by MATCH in SCOPE.
  9118. FUNC is a function or a lisp form. The function will be called without
  9119. arguments, with the cursor positioned at the beginning of the headline.
  9120. The return values of all calls to the function will be collected and
  9121. returned as a list.
  9122. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9123. Only headlines that are matched by this query will be considered during
  9124. the iteration. When MATCH is nil or t, all headlines will be
  9125. visited by the iteration.
  9126. SCOPE determines the scope of this command. It can be any of:
  9127. nil The current buffer, respecting the restriction if any
  9128. tree The subtree started with the entry at point
  9129. file The current buffer, without restriction
  9130. file-with-archives
  9131. The current buffer, and any archives associated with it
  9132. agenda All agenda files
  9133. agenda-with-archives
  9134. All agenda files with any archive files associated with them
  9135. \(file1 file2 ...)
  9136. If this is a list, all files in the list will be scanned
  9137. The remaining args are treated as settings for the skipping facilities of
  9138. the scanner. The following items can be given here:
  9139. archive skip trees with the archive tag.
  9140. comment skip trees with the COMMENT keyword
  9141. function or Emacs Lisp form:
  9142. will be used as value for `org-agenda-skip-function', so whenever
  9143. the the function returns t, FUNC will not be called for that
  9144. entry and search will continue from the point where the
  9145. function leaves it."
  9146. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9147. (org-agenda-skip-archived-trees (memq 'archive skip))
  9148. (org-agenda-skip-comment-trees (memq 'comment skip))
  9149. (org-agenda-skip-function
  9150. (car (org-delete-all '(comment archive) skip)))
  9151. (org-tags-match-list-sublevels t)
  9152. matcher pos file res
  9153. org-todo-keywords-for-agenda
  9154. org-done-keywords-for-agenda
  9155. org-todo-keyword-alist-for-agenda
  9156. org-tag-alist-for-agenda)
  9157. (cond
  9158. ((eq match t) (setq matcher t))
  9159. ((eq match nil) (setq matcher t))
  9160. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9161. (when (eq scope 'tree)
  9162. (org-back-to-heading t)
  9163. (org-narrow-to-subtree)
  9164. (setq scope nil))
  9165. (if (not scope)
  9166. (progn
  9167. (org-prepare-agenda-buffers
  9168. (list (buffer-file-name (current-buffer))))
  9169. (org-scan-tags func matcher))
  9170. ;; Get the right scope
  9171. (setq pos (point))
  9172. (cond
  9173. ((and scope (listp scope) (symbolp (car scope)))
  9174. (setq scope (eval scope)))
  9175. ((eq scope 'agenda)
  9176. (setq scope (org-agenda-files t)))
  9177. ((eq scope 'agenda-with-archives)
  9178. (setq scope (org-agenda-files t))
  9179. (setq scope (org-add-archive-files scope)))
  9180. ((eq scope 'file)
  9181. (setq scope (list (buffer-file-name))))
  9182. ((eq scope 'file-with-archives)
  9183. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9184. (org-prepare-agenda-buffers scope)
  9185. (while (setq file (pop scope))
  9186. (with-current-buffer (org-find-base-buffer-visiting file)
  9187. (save-excursion
  9188. (save-restriction
  9189. (widen)
  9190. (goto-char (point-min))
  9191. (setq res (append res (org-scan-tags func matcher)))))))
  9192. res)))
  9193. ;;;; Properties
  9194. ;;; Setting and retrieving properties
  9195. (defconst org-special-properties
  9196. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9197. "TIMESTAMP" "TIMESTAMP_IA")
  9198. "The special properties valid in Org-mode.
  9199. These are properties that are not defined in the property drawer,
  9200. but in some other way.")
  9201. (defconst org-default-properties
  9202. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9203. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9204. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9205. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9206. "Some properties that are used by Org-mode for various purposes.
  9207. Being in this list makes sure that they are offered for completion.")
  9208. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9209. "Regular expression matching the first line of a property drawer.")
  9210. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9211. "Regular expression matching the first line of a property drawer.")
  9212. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9213. "Regular expression matching the first line of a property drawer.")
  9214. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9215. "Regular expression matching the first line of a property drawer.")
  9216. (defconst org-property-drawer-re
  9217. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9218. org-property-end-re "\\)\n?")
  9219. "Matches an entire property drawer.")
  9220. (defconst org-clock-drawer-re
  9221. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9222. org-property-end-re "\\)\n?")
  9223. "Matches an entire clock drawer.")
  9224. (defun org-property-action ()
  9225. "Do an action on properties."
  9226. (interactive)
  9227. (let (c)
  9228. (org-at-property-p)
  9229. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9230. (setq c (read-char-exclusive))
  9231. (cond
  9232. ((equal c ?s)
  9233. (call-interactively 'org-set-property))
  9234. ((equal c ?d)
  9235. (call-interactively 'org-delete-property))
  9236. ((equal c ?D)
  9237. (call-interactively 'org-delete-property-globally))
  9238. ((equal c ?c)
  9239. (call-interactively 'org-compute-property-at-point))
  9240. (t (error "No such property action %c" c)))))
  9241. (defun org-at-property-p ()
  9242. "Is the cursor in a property line?"
  9243. ;; FIXME: Does not check if we are actually in the drawer.
  9244. ;; FIXME: also returns true on any drawers.....
  9245. ;; This is used by C-c C-c for property action.
  9246. (save-excursion
  9247. (beginning-of-line 1)
  9248. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9249. (defun org-get-property-block (&optional beg end force)
  9250. "Return the (beg . end) range of the body of the property drawer.
  9251. BEG and END can be beginning and end of subtree, if not given
  9252. they will be found.
  9253. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9254. (catch 'exit
  9255. (save-excursion
  9256. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9257. (end (or end (progn (outline-next-heading) (point)))))
  9258. (goto-char beg)
  9259. (if (re-search-forward org-property-start-re end t)
  9260. (setq beg (1+ (match-end 0)))
  9261. (if force
  9262. (save-excursion
  9263. (org-insert-property-drawer)
  9264. (setq end (progn (outline-next-heading) (point))))
  9265. (throw 'exit nil))
  9266. (goto-char beg)
  9267. (if (re-search-forward org-property-start-re end t)
  9268. (setq beg (1+ (match-end 0)))))
  9269. (if (re-search-forward org-property-end-re end t)
  9270. (setq end (match-beginning 0))
  9271. (or force (throw 'exit nil))
  9272. (goto-char beg)
  9273. (setq end beg)
  9274. (org-indent-line-function)
  9275. (insert ":END:\n"))
  9276. (cons beg end)))))
  9277. (defun org-entry-properties (&optional pom which)
  9278. "Get all properties of the entry at point-or-marker POM.
  9279. This includes the TODO keyword, the tags, time strings for deadline,
  9280. scheduled, and clocking, and any additional properties defined in the
  9281. entry. The return value is an alist, keys may occur multiple times
  9282. if the property key was used several times.
  9283. POM may also be nil, in which case the current entry is used.
  9284. If WHICH is nil or `all', get all properties. If WHICH is
  9285. `special' or `standard', only get that subclass."
  9286. (setq which (or which 'all))
  9287. (org-with-point-at pom
  9288. (let ((clockstr (substring org-clock-string 0 -1))
  9289. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9290. beg end range props sum-props key value string clocksum)
  9291. (save-excursion
  9292. (when (condition-case nil (org-back-to-heading t) (error nil))
  9293. (setq beg (point))
  9294. (setq sum-props (get-text-property (point) 'org-summaries))
  9295. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9296. (outline-next-heading)
  9297. (setq end (point))
  9298. (when (memq which '(all special))
  9299. ;; Get the special properties, like TODO and tags
  9300. (goto-char beg)
  9301. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9302. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9303. (when (looking-at org-priority-regexp)
  9304. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9305. (when (and (setq value (org-get-tags-string))
  9306. (string-match "\\S-" value))
  9307. (push (cons "TAGS" value) props))
  9308. (when (setq value (org-get-tags-at))
  9309. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9310. props))
  9311. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9312. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9313. string (if (equal key clockstr)
  9314. (org-no-properties
  9315. (org-trim
  9316. (buffer-substring
  9317. (match-beginning 3) (goto-char (point-at-eol)))))
  9318. (substring (org-match-string-no-properties 3) 1 -1)))
  9319. (unless key
  9320. (if (= (char-after (match-beginning 3)) ?\[)
  9321. (setq key "TIMESTAMP_IA")
  9322. (setq key "TIMESTAMP")))
  9323. (when (or (equal key clockstr) (not (assoc key props)))
  9324. (push (cons key string) props)))
  9325. )
  9326. (when (memq which '(all standard))
  9327. ;; Get the standard properties, like :PORP: ...
  9328. (setq range (org-get-property-block beg end))
  9329. (when range
  9330. (goto-char (car range))
  9331. (while (re-search-forward
  9332. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9333. (cdr range) t)
  9334. (setq key (org-match-string-no-properties 1)
  9335. value (org-trim (or (org-match-string-no-properties 2) "")))
  9336. (unless (member key excluded)
  9337. (push (cons key (or value "")) props)))))
  9338. (if clocksum
  9339. (push (cons "CLOCKSUM"
  9340. (org-columns-number-to-string (/ (float clocksum) 60.)
  9341. 'add_times))
  9342. props))
  9343. (unless (assoc "CATEGORY" props)
  9344. (setq value (or (org-get-category)
  9345. (progn (org-refresh-category-properties)
  9346. (org-get-category))))
  9347. (push (cons "CATEGORY" value) props))
  9348. (append sum-props (nreverse props)))))))
  9349. (defun org-entry-get (pom property &optional inherit)
  9350. "Get value of PROPERTY for entry at point-or-marker POM.
  9351. If INHERIT is non-nil and the entry does not have the property,
  9352. then also check higher levels of the hierarchy.
  9353. If INHERIT is the symbol `selective', use inheritance only if the setting
  9354. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9355. If the property is present but empty, the return value is the empty string.
  9356. If the property is not present at all, nil is returned."
  9357. (org-with-point-at pom
  9358. (if (and inherit (if (eq inherit 'selective)
  9359. (org-property-inherit-p property)
  9360. t))
  9361. (org-entry-get-with-inheritance property)
  9362. (if (member property org-special-properties)
  9363. ;; We need a special property. Use brute force, get all properties.
  9364. (cdr (assoc property (org-entry-properties nil 'special)))
  9365. (let ((range (org-get-property-block)))
  9366. (if (and range
  9367. (goto-char (car range))
  9368. (re-search-forward
  9369. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9370. (cdr range) t))
  9371. ;; Found the property, return it.
  9372. (if (match-end 1)
  9373. (org-match-string-no-properties 1)
  9374. "")))))))
  9375. (defun org-property-or-variable-value (var &optional inherit)
  9376. "Check if there is a property fixing the value of VAR.
  9377. If yes, return this value. If not, return the current value of the variable."
  9378. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9379. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9380. (read prop)
  9381. (symbol-value var))))
  9382. (defun org-entry-delete (pom property)
  9383. "Delete the property PROPERTY from entry at point-or-marker POM."
  9384. (org-with-point-at pom
  9385. (if (member property org-special-properties)
  9386. nil ; cannot delete these properties.
  9387. (let ((range (org-get-property-block)))
  9388. (if (and range
  9389. (goto-char (car range))
  9390. (re-search-forward
  9391. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9392. (cdr range) t))
  9393. (progn
  9394. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9395. t)
  9396. nil)))))
  9397. ;; Multi-values properties are properties that contain multiple values
  9398. ;; These values are assumed to be single words, separated by whitespace.
  9399. (defun org-entry-add-to-multivalued-property (pom property value)
  9400. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9401. (let* ((old (org-entry-get pom property))
  9402. (values (and old (org-split-string old "[ \t]"))))
  9403. (setq value (org-entry-protect-space value))
  9404. (unless (member value values)
  9405. (setq values (cons value values))
  9406. (org-entry-put pom property
  9407. (mapconcat 'identity values " ")))))
  9408. (defun org-entry-remove-from-multivalued-property (pom property value)
  9409. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9410. (let* ((old (org-entry-get pom property))
  9411. (values (and old (org-split-string old "[ \t]"))))
  9412. (setq value (org-entry-protect-space value))
  9413. (when (member value values)
  9414. (setq values (delete value values))
  9415. (org-entry-put pom property
  9416. (mapconcat 'identity values " ")))))
  9417. (defun org-entry-member-in-multivalued-property (pom property value)
  9418. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9419. (let* ((old (org-entry-get pom property))
  9420. (values (and old (org-split-string old "[ \t]"))))
  9421. (setq value (org-entry-protect-space value))
  9422. (member value values)))
  9423. (defun org-entry-get-multivalued-property (pom property)
  9424. "Return a list of values in a multivalued property."
  9425. (let* ((value (org-entry-get pom property))
  9426. (values (and value (org-split-string value "[ \t]"))))
  9427. (mapcar 'org-entry-restore-space values)))
  9428. (defun org-entry-put-multivalued-property (pom property &rest values)
  9429. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9430. VALUES should be a list of strings. Spaces will be protected."
  9431. (org-entry-put pom property
  9432. (mapconcat 'org-entry-protect-space values " "))
  9433. (let* ((value (org-entry-get pom property))
  9434. (values (and value (org-split-string value "[ \t]"))))
  9435. (mapcar 'org-entry-restore-space values)))
  9436. (defun org-entry-protect-space (s)
  9437. "Protect spaces and newline in string S."
  9438. (while (string-match " " s)
  9439. (setq s (replace-match "%20" t t s)))
  9440. (while (string-match "\n" s)
  9441. (setq s (replace-match "%0A" t t s)))
  9442. s)
  9443. (defun org-entry-restore-space (s)
  9444. "Restore spaces and newline in string S."
  9445. (while (string-match "%20" s)
  9446. (setq s (replace-match " " t t s)))
  9447. (while (string-match "%0A" s)
  9448. (setq s (replace-match "\n" t t s)))
  9449. s)
  9450. (defvar org-entry-property-inherited-from (make-marker)
  9451. "Marker pointing to the entry from where a property was inherited.
  9452. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9453. location of the entry where the inheritance search matched. If there was
  9454. no match, the marker will point nowhere.
  9455. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9456. is set.")
  9457. (defun org-entry-get-with-inheritance (property)
  9458. "Get entry property, and search higher levels if not present."
  9459. (move-marker org-entry-property-inherited-from nil)
  9460. (let (tmp)
  9461. (save-excursion
  9462. (save-restriction
  9463. (widen)
  9464. (catch 'ex
  9465. (while t
  9466. (when (setq tmp (org-entry-get nil property))
  9467. (org-back-to-heading t)
  9468. (move-marker org-entry-property-inherited-from (point))
  9469. (throw 'ex tmp))
  9470. (or (org-up-heading-safe) (throw 'ex nil)))))
  9471. (or tmp
  9472. (cdr (assoc property org-file-properties))
  9473. (cdr (assoc property org-global-properties))
  9474. (cdr (assoc property org-global-properties-fixed))))))
  9475. (defun org-entry-put (pom property value)
  9476. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9477. (org-with-point-at pom
  9478. (org-back-to-heading t)
  9479. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9480. range)
  9481. (cond
  9482. ((equal property "TODO")
  9483. (when (and (stringp value) (string-match "\\S-" value)
  9484. (not (member value org-todo-keywords-1)))
  9485. (error "\"%s\" is not a valid TODO state" value))
  9486. (if (or (not value)
  9487. (not (string-match "\\S-" value)))
  9488. (setq value 'none))
  9489. (org-todo value)
  9490. (org-set-tags nil 'align))
  9491. ((equal property "PRIORITY")
  9492. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9493. (string-to-char value) ?\ ))
  9494. (org-set-tags nil 'align))
  9495. ((equal property "SCHEDULED")
  9496. (if (re-search-forward org-scheduled-time-regexp end t)
  9497. (cond
  9498. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9499. ((eq value 'later) (org-timestamp-change 1 'day))
  9500. (t (call-interactively 'org-schedule)))
  9501. (call-interactively 'org-schedule)))
  9502. ((equal property "DEADLINE")
  9503. (if (re-search-forward org-deadline-time-regexp end t)
  9504. (cond
  9505. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9506. ((eq value 'later) (org-timestamp-change 1 'day))
  9507. (t (call-interactively 'org-deadline)))
  9508. (call-interactively 'org-deadline)))
  9509. ((member property org-special-properties)
  9510. (error "The %s property can not yet be set with `org-entry-put'"
  9511. property))
  9512. (t ; a non-special property
  9513. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9514. (setq range (org-get-property-block beg end 'force))
  9515. (goto-char (car range))
  9516. (if (re-search-forward
  9517. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9518. (progn
  9519. (delete-region (match-beginning 1) (match-end 1))
  9520. (goto-char (match-beginning 1)))
  9521. (goto-char (cdr range))
  9522. (insert "\n")
  9523. (backward-char 1)
  9524. (org-indent-line-function)
  9525. (insert ":" property ":"))
  9526. (and value (insert " " value))
  9527. (org-indent-line-function)))))))
  9528. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9529. "Get all property keys in the current buffer.
  9530. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9531. like tags and TODO state.
  9532. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9533. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9534. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9535. formats in the current buffer."
  9536. (let (rtn range cfmt cols s p)
  9537. (save-excursion
  9538. (save-restriction
  9539. (widen)
  9540. (goto-char (point-min))
  9541. (while (re-search-forward org-property-start-re nil t)
  9542. (setq range (org-get-property-block))
  9543. (goto-char (car range))
  9544. (while (re-search-forward
  9545. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9546. (cdr range) t)
  9547. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9548. (outline-next-heading))))
  9549. (when include-specials
  9550. (setq rtn (append org-special-properties rtn)))
  9551. (when include-defaults
  9552. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9553. (when include-columns
  9554. (save-excursion
  9555. (save-restriction
  9556. (widen)
  9557. (goto-char (point-min))
  9558. (while (re-search-forward
  9559. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9560. nil t)
  9561. (setq cfmt (match-string 2) s 0)
  9562. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9563. cfmt s)
  9564. (setq s (match-end 0)
  9565. p (match-string 1 cfmt))
  9566. (unless (or (equal p "ITEM")
  9567. (member p org-special-properties))
  9568. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9569. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9570. (defun org-property-values (key)
  9571. "Return a list of all values of property KEY."
  9572. (save-excursion
  9573. (save-restriction
  9574. (widen)
  9575. (goto-char (point-min))
  9576. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9577. values)
  9578. (while (re-search-forward re nil t)
  9579. (add-to-list 'values (org-trim (match-string 1))))
  9580. (delete "" values)))))
  9581. (defun org-insert-property-drawer ()
  9582. "Insert a property drawer into the current entry."
  9583. (interactive)
  9584. (org-back-to-heading t)
  9585. (looking-at outline-regexp)
  9586. (let ((indent (- (match-end 0)(match-beginning 0)))
  9587. (beg (point))
  9588. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9589. end hiddenp)
  9590. (outline-next-heading)
  9591. (setq end (point))
  9592. (goto-char beg)
  9593. (while (re-search-forward re end t))
  9594. (setq hiddenp (org-invisible-p))
  9595. (end-of-line 1)
  9596. (and (equal (char-after) ?\n) (forward-char 1))
  9597. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9598. (beginning-of-line 2))
  9599. (org-skip-over-state-notes)
  9600. (skip-chars-backward " \t\n\r")
  9601. (if (eq (char-before) ?*) (forward-char 1))
  9602. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9603. (beginning-of-line 0)
  9604. (org-indent-to-column indent)
  9605. (beginning-of-line 2)
  9606. (org-indent-to-column indent)
  9607. (beginning-of-line 0)
  9608. (if hiddenp
  9609. (save-excursion
  9610. (org-back-to-heading t)
  9611. (hide-entry))
  9612. (org-flag-drawer t))))
  9613. (defun org-set-property (property value)
  9614. "In the current entry, set PROPERTY to VALUE.
  9615. When called interactively, this will prompt for a property name, offering
  9616. completion on existing and default properties. And then it will prompt
  9617. for a value, offering completion either on allowed values (via an inherited
  9618. xxx_ALL property) or on existing values in other instances of this property
  9619. in the current file."
  9620. (interactive
  9621. (let* ((completion-ignore-case t)
  9622. (keys (org-buffer-property-keys nil t t))
  9623. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9624. (prop (if (member prop0 keys)
  9625. prop0
  9626. (or (cdr (assoc (downcase prop0)
  9627. (mapcar (lambda (x) (cons (downcase x) x))
  9628. keys)))
  9629. prop0)))
  9630. (cur (org-entry-get nil prop))
  9631. (allowed (org-property-get-allowed-values nil prop 'table))
  9632. (existing (mapcar 'list (org-property-values prop)))
  9633. (val (if allowed
  9634. (org-completing-read "Value: " allowed nil 'req-match)
  9635. (org-completing-read
  9636. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9637. (concat "[" cur "]") "")
  9638. ": ")
  9639. existing nil nil "" nil cur))))
  9640. (list prop (if (equal val "") cur val))))
  9641. (unless (equal (org-entry-get nil property) value)
  9642. (org-entry-put nil property value)))
  9643. (defun org-delete-property (property)
  9644. "In the current entry, delete PROPERTY."
  9645. (interactive
  9646. (let* ((completion-ignore-case t)
  9647. (prop (org-ido-completing-read
  9648. "Property: " (org-entry-properties nil 'standard))))
  9649. (list prop)))
  9650. (message "Property %s %s" property
  9651. (if (org-entry-delete nil property)
  9652. "deleted"
  9653. "was not present in the entry")))
  9654. (defun org-delete-property-globally (property)
  9655. "Remove PROPERTY globally, from all entries."
  9656. (interactive
  9657. (let* ((completion-ignore-case t)
  9658. (prop (org-ido-completing-read
  9659. "Globally remove property: "
  9660. (mapcar 'list (org-buffer-property-keys)))))
  9661. (list prop)))
  9662. (save-excursion
  9663. (save-restriction
  9664. (widen)
  9665. (goto-char (point-min))
  9666. (let ((cnt 0))
  9667. (while (re-search-forward
  9668. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9669. nil t)
  9670. (setq cnt (1+ cnt))
  9671. (replace-match ""))
  9672. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9673. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9674. (defun org-compute-property-at-point ()
  9675. "Compute the property at point.
  9676. This looks for an enclosing column format, extracts the operator and
  9677. then applies it to the property in the column format's scope."
  9678. (interactive)
  9679. (unless (org-at-property-p)
  9680. (error "Not at a property"))
  9681. (let ((prop (org-match-string-no-properties 2)))
  9682. (org-columns-get-format-and-top-level)
  9683. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9684. (error "No operator defined for property %s" prop))
  9685. (org-columns-compute prop)))
  9686. (defun org-property-get-allowed-values (pom property &optional table)
  9687. "Get allowed values for the property PROPERTY.
  9688. When TABLE is non-nil, return an alist that can directly be used for
  9689. completion."
  9690. (let (vals)
  9691. (cond
  9692. ((equal property "TODO")
  9693. (setq vals (org-with-point-at pom
  9694. (append org-todo-keywords-1 '("")))))
  9695. ((equal property "PRIORITY")
  9696. (let ((n org-lowest-priority))
  9697. (while (>= n org-highest-priority)
  9698. (push (char-to-string n) vals)
  9699. (setq n (1- n)))))
  9700. ((member property org-special-properties))
  9701. (t
  9702. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9703. (when (and vals (string-match "\\S-" vals))
  9704. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9705. (setq vals (mapcar (lambda (x)
  9706. (cond ((stringp x) x)
  9707. ((numberp x) (number-to-string x))
  9708. ((symbolp x) (symbol-name x))
  9709. (t "???")))
  9710. vals)))))
  9711. (if table (mapcar 'list vals) vals)))
  9712. (defun org-property-previous-allowed-value (&optional previous)
  9713. "Switch to the next allowed value for this property."
  9714. (interactive)
  9715. (org-property-next-allowed-value t))
  9716. (defun org-property-next-allowed-value (&optional previous)
  9717. "Switch to the next allowed value for this property."
  9718. (interactive)
  9719. (unless (org-at-property-p)
  9720. (error "Not at a property"))
  9721. (let* ((key (match-string 2))
  9722. (value (match-string 3))
  9723. (allowed (or (org-property-get-allowed-values (point) key)
  9724. (and (member value '("[ ]" "[-]" "[X]"))
  9725. '("[ ]" "[X]"))))
  9726. nval)
  9727. (unless allowed
  9728. (error "Allowed values for this property have not been defined"))
  9729. (if previous (setq allowed (reverse allowed)))
  9730. (if (member value allowed)
  9731. (setq nval (car (cdr (member value allowed)))))
  9732. (setq nval (or nval (car allowed)))
  9733. (if (equal nval value)
  9734. (error "Only one allowed value for this property"))
  9735. (org-at-property-p)
  9736. (replace-match (concat " :" key ": " nval) t t)
  9737. (org-indent-line-function)
  9738. (beginning-of-line 1)
  9739. (skip-chars-forward " \t")))
  9740. (defun org-find-entry-with-id (ident)
  9741. "Locate the entry that contains the ID property with exact value IDENT.
  9742. IDENT can be a string, a symbol or a number, this function will search for
  9743. the string representation of it.
  9744. Return the position where this entry starts, or nil if there is no such entry."
  9745. (interactive "sID: ")
  9746. (let ((id (cond
  9747. ((stringp ident) ident)
  9748. ((symbol-name ident) (symbol-name ident))
  9749. ((numberp ident) (number-to-string ident))
  9750. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9751. (case-fold-search nil))
  9752. (save-excursion
  9753. (save-restriction
  9754. (widen)
  9755. (goto-char (point-min))
  9756. (when (re-search-forward
  9757. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9758. nil t)
  9759. (org-back-to-heading)
  9760. (point))))))
  9761. ;;;; Timestamps
  9762. (defvar org-last-changed-timestamp nil)
  9763. (defvar org-last-inserted-timestamp nil
  9764. "The last time stamp inserted with `org-insert-time-stamp'.")
  9765. (defvar org-time-was-given) ; dynamically scoped parameter
  9766. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9767. (defvar org-ts-what) ; dynamically scoped parameter
  9768. (defun org-time-stamp (arg &optional inactive)
  9769. "Prompt for a date/time and insert a time stamp.
  9770. If the user specifies a time like HH:MM, or if this command is called
  9771. with a prefix argument, the time stamp will contain date and time.
  9772. Otherwise, only the date will be included. All parts of a date not
  9773. specified by the user will be filled in from the current date/time.
  9774. So if you press just return without typing anything, the time stamp
  9775. will represent the current date/time. If there is already a timestamp
  9776. at the cursor, it will be modified."
  9777. (interactive "P")
  9778. (let* ((ts nil)
  9779. (default-time
  9780. ;; Default time is either today, or, when entering a range,
  9781. ;; the range start.
  9782. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9783. (save-excursion
  9784. (re-search-backward
  9785. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9786. (- (point) 20) t)))
  9787. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9788. (current-time)))
  9789. (default-input (and ts (org-get-compact-tod ts)))
  9790. org-time-was-given org-end-time-was-given time)
  9791. (cond
  9792. ((and (org-at-timestamp-p t)
  9793. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9794. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9795. (insert "--")
  9796. (setq time (let ((this-command this-command))
  9797. (org-read-date arg 'totime nil nil
  9798. default-time default-input)))
  9799. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9800. ((org-at-timestamp-p t)
  9801. (setq time (let ((this-command this-command))
  9802. (org-read-date arg 'totime nil nil default-time default-input)))
  9803. (when (org-at-timestamp-p t) ; just to get the match data
  9804. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9805. (replace-match "")
  9806. (setq org-last-changed-timestamp
  9807. (org-insert-time-stamp
  9808. time (or org-time-was-given arg)
  9809. inactive nil nil (list org-end-time-was-given))))
  9810. (message "Timestamp updated"))
  9811. (t
  9812. (setq time (let ((this-command this-command))
  9813. (org-read-date arg 'totime nil nil default-time default-input)))
  9814. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9815. nil nil (list org-end-time-was-given))))))
  9816. ;; FIXME: can we use this for something else, like computing time differences?
  9817. (defun org-get-compact-tod (s)
  9818. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9819. (let* ((t1 (match-string 1 s))
  9820. (h1 (string-to-number (match-string 2 s)))
  9821. (m1 (string-to-number (match-string 3 s)))
  9822. (t2 (and (match-end 4) (match-string 5 s)))
  9823. (h2 (and t2 (string-to-number (match-string 6 s))))
  9824. (m2 (and t2 (string-to-number (match-string 7 s))))
  9825. dh dm)
  9826. (if (not t2)
  9827. t1
  9828. (setq dh (- h2 h1) dm (- m2 m1))
  9829. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9830. (concat t1 "+" (number-to-string dh)
  9831. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9832. (defun org-time-stamp-inactive (&optional arg)
  9833. "Insert an inactive time stamp.
  9834. An inactive time stamp is enclosed in square brackets instead of angle
  9835. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9836. does not link to the calendar and cannot be changed with the S-cursor keys.
  9837. So these are more for recording a certain time/date."
  9838. (interactive "P")
  9839. (org-time-stamp arg 'inactive))
  9840. (defvar org-date-ovl (org-make-overlay 1 1))
  9841. (org-overlay-put org-date-ovl 'face 'org-warning)
  9842. (org-detach-overlay org-date-ovl)
  9843. (defvar org-ans1) ; dynamically scoped parameter
  9844. (defvar org-ans2) ; dynamically scoped parameter
  9845. (defvar org-plain-time-of-day-regexp) ; defined below
  9846. (defvar org-overriding-default-time nil) ; dynamically scoped
  9847. (defvar org-read-date-overlay nil)
  9848. (defvar org-dcst nil) ; dynamically scoped
  9849. (defun org-read-date (&optional with-time to-time from-string prompt
  9850. default-time default-input)
  9851. "Read a date, possibly a time, and make things smooth for the user.
  9852. The prompt will suggest to enter an ISO date, but you can also enter anything
  9853. which will at least partially be understood by `parse-time-string'.
  9854. Unrecognized parts of the date will default to the current day, month, year,
  9855. hour and minute. If this command is called to replace a timestamp at point,
  9856. of to enter the second timestamp of a range, the default time is taken from the
  9857. existing stamp. For example,
  9858. 3-2-5 --> 2003-02-05
  9859. feb 15 --> currentyear-02-15
  9860. sep 12 9 --> 2009-09-12
  9861. 12:45 --> today 12:45
  9862. 22 sept 0:34 --> currentyear-09-22 0:34
  9863. 12 --> currentyear-currentmonth-12
  9864. Fri --> nearest Friday (today or later)
  9865. etc.
  9866. Furthermore you can specify a relative date by giving, as the *first* thing
  9867. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9868. change in days weeks, months, years.
  9869. With a single plus or minus, the date is relative to today. With a double
  9870. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9871. +4d --> four days from today
  9872. +4 --> same as above
  9873. +2w --> two weeks from today
  9874. ++5 --> five days from default date
  9875. The function understands only English month and weekday abbreviations,
  9876. but this can be configured with the variables `parse-time-months' and
  9877. `parse-time-weekdays'.
  9878. While prompting, a calendar is popped up - you can also select the
  9879. date with the mouse (button 1). The calendar shows a period of three
  9880. months. To scroll it to other months, use the keys `>' and `<'.
  9881. If you don't like the calendar, turn it off with
  9882. \(setq org-read-date-popup-calendar nil)
  9883. With optional argument TO-TIME, the date will immediately be converted
  9884. to an internal time.
  9885. With an optional argument WITH-TIME, the prompt will suggest to also
  9886. insert a time. Note that when WITH-TIME is not set, you can still
  9887. enter a time, and this function will inform the calling routine about
  9888. this change. The calling routine may then choose to change the format
  9889. used to insert the time stamp into the buffer to include the time.
  9890. With optional argument FROM-STRING, read from this string instead from
  9891. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9892. the time/date that is used for everything that is not specified by the
  9893. user."
  9894. (require 'parse-time)
  9895. (let* ((org-time-stamp-rounding-minutes
  9896. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9897. (org-dcst org-display-custom-times)
  9898. (ct (org-current-time))
  9899. (def (or org-overriding-default-time default-time ct))
  9900. (defdecode (decode-time def))
  9901. (dummy (progn
  9902. (when (< (nth 2 defdecode) org-extend-today-until)
  9903. (setcar (nthcdr 2 defdecode) -1)
  9904. (setcar (nthcdr 1 defdecode) 59)
  9905. (setq def (apply 'encode-time defdecode)
  9906. defdecode (decode-time def)))))
  9907. (calendar-move-hook nil)
  9908. (calendar-view-diary-initially-flag nil)
  9909. (view-diary-entries-initially nil)
  9910. (calendar-view-holidays-initially-flag nil)
  9911. (view-calendar-holidays-initially nil)
  9912. (timestr (format-time-string
  9913. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9914. (prompt (concat (if prompt (concat prompt " ") "")
  9915. (format "Date+time [%s]: " timestr)))
  9916. ans (org-ans0 "") org-ans1 org-ans2 final)
  9917. (cond
  9918. (from-string (setq ans from-string))
  9919. (org-read-date-popup-calendar
  9920. (save-excursion
  9921. (save-window-excursion
  9922. (calendar)
  9923. (calendar-forward-day (- (time-to-days def)
  9924. (calendar-absolute-from-gregorian
  9925. (calendar-current-date))))
  9926. (org-eval-in-calendar nil t)
  9927. (let* ((old-map (current-local-map))
  9928. (map (copy-keymap calendar-mode-map))
  9929. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9930. (org-defkey map (kbd "RET") 'org-calendar-select)
  9931. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9932. 'org-calendar-select-mouse)
  9933. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9934. 'org-calendar-select-mouse)
  9935. (org-defkey minibuffer-local-map [(meta shift left)]
  9936. (lambda () (interactive)
  9937. (org-eval-in-calendar '(calendar-backward-month 1))))
  9938. (org-defkey minibuffer-local-map [(meta shift right)]
  9939. (lambda () (interactive)
  9940. (org-eval-in-calendar '(calendar-forward-month 1))))
  9941. (org-defkey minibuffer-local-map [(meta shift up)]
  9942. (lambda () (interactive)
  9943. (org-eval-in-calendar '(calendar-backward-year 1))))
  9944. (org-defkey minibuffer-local-map [(meta shift down)]
  9945. (lambda () (interactive)
  9946. (org-eval-in-calendar '(calendar-forward-year 1))))
  9947. (org-defkey minibuffer-local-map [(shift up)]
  9948. (lambda () (interactive)
  9949. (org-eval-in-calendar '(calendar-backward-week 1))))
  9950. (org-defkey minibuffer-local-map [(shift down)]
  9951. (lambda () (interactive)
  9952. (org-eval-in-calendar '(calendar-forward-week 1))))
  9953. (org-defkey minibuffer-local-map [(shift left)]
  9954. (lambda () (interactive)
  9955. (org-eval-in-calendar '(calendar-backward-day 1))))
  9956. (org-defkey minibuffer-local-map [(shift right)]
  9957. (lambda () (interactive)
  9958. (org-eval-in-calendar '(calendar-forward-day 1))))
  9959. (org-defkey minibuffer-local-map ">"
  9960. (lambda () (interactive)
  9961. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9962. (org-defkey minibuffer-local-map "<"
  9963. (lambda () (interactive)
  9964. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9965. (unwind-protect
  9966. (progn
  9967. (use-local-map map)
  9968. (add-hook 'post-command-hook 'org-read-date-display)
  9969. (setq org-ans0 (read-string prompt default-input nil nil))
  9970. ;; org-ans0: from prompt
  9971. ;; org-ans1: from mouse click
  9972. ;; org-ans2: from calendar motion
  9973. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9974. (remove-hook 'post-command-hook 'org-read-date-display)
  9975. (use-local-map old-map)
  9976. (when org-read-date-overlay
  9977. (org-delete-overlay org-read-date-overlay)
  9978. (setq org-read-date-overlay nil)))))))
  9979. (t ; Naked prompt only
  9980. (unwind-protect
  9981. (setq ans (read-string prompt default-input nil timestr))
  9982. (when org-read-date-overlay
  9983. (org-delete-overlay org-read-date-overlay)
  9984. (setq org-read-date-overlay nil)))))
  9985. (setq final (org-read-date-analyze ans def defdecode))
  9986. (if to-time
  9987. (apply 'encode-time final)
  9988. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9989. (format "%04d-%02d-%02d %02d:%02d"
  9990. (nth 5 final) (nth 4 final) (nth 3 final)
  9991. (nth 2 final) (nth 1 final))
  9992. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9993. (defvar def)
  9994. (defvar defdecode)
  9995. (defvar with-time)
  9996. (defun org-read-date-display ()
  9997. "Display the current date prompt interpretation in the minibuffer."
  9998. (when org-read-date-display-live
  9999. (when org-read-date-overlay
  10000. (org-delete-overlay org-read-date-overlay))
  10001. (let ((p (point)))
  10002. (end-of-line 1)
  10003. (while (not (equal (buffer-substring
  10004. (max (point-min) (- (point) 4)) (point))
  10005. " "))
  10006. (insert " "))
  10007. (goto-char p))
  10008. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10009. " " (or org-ans1 org-ans2)))
  10010. (org-end-time-was-given nil)
  10011. (f (org-read-date-analyze ans def defdecode))
  10012. (fmts (if org-dcst
  10013. org-time-stamp-custom-formats
  10014. org-time-stamp-formats))
  10015. (fmt (if (or with-time
  10016. (and (boundp 'org-time-was-given) org-time-was-given))
  10017. (cdr fmts)
  10018. (car fmts)))
  10019. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10020. (when (and org-end-time-was-given
  10021. (string-match org-plain-time-of-day-regexp txt))
  10022. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10023. org-end-time-was-given
  10024. (substring txt (match-end 0)))))
  10025. (setq org-read-date-overlay
  10026. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10027. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10028. (defun org-read-date-analyze (ans def defdecode)
  10029. "Analyse the combined answer of the date prompt."
  10030. ;; FIXME: cleanup and comment
  10031. (let (delta deltan deltaw deltadef year month day
  10032. hour minute second wday pm h2 m2 tl wday1
  10033. iso-year iso-weekday iso-week iso-year iso-date)
  10034. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10035. (setq ans "+0"))
  10036. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10037. (setq ans (replace-match "" t t ans)
  10038. deltan (car delta)
  10039. deltaw (nth 1 delta)
  10040. deltadef (nth 2 delta)))
  10041. ;; Check if there is an iso week date in there
  10042. ;; If yes, sore the info and postpone interpreting it until the rest
  10043. ;; of the parsing is done
  10044. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10045. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10046. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10047. iso-week (string-to-number (match-string 2 ans)))
  10048. (setq ans (replace-match "" t t ans)))
  10049. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10050. (when (string-match
  10051. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10052. (setq year (if (match-end 2)
  10053. (string-to-number (match-string 2 ans))
  10054. (string-to-number (format-time-string "%Y")))
  10055. month (string-to-number (match-string 3 ans))
  10056. day (string-to-number (match-string 4 ans)))
  10057. (if (< year 100) (setq year (+ 2000 year)))
  10058. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10059. t nil ans)))
  10060. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10061. ;; If there is a time with am/pm, and *no* time without it, we convert
  10062. ;; so that matching will be successful.
  10063. (loop for i from 1 to 2 do ; twice, for end time as well
  10064. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10065. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10066. (setq hour (string-to-number (match-string 1 ans))
  10067. minute (if (match-end 3)
  10068. (string-to-number (match-string 3 ans))
  10069. 0)
  10070. pm (equal ?p
  10071. (string-to-char (downcase (match-string 4 ans)))))
  10072. (if (and (= hour 12) (not pm))
  10073. (setq hour 0)
  10074. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10075. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10076. t t ans))))
  10077. ;; Check if a time range is given as a duration
  10078. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10079. (setq hour (string-to-number (match-string 1 ans))
  10080. h2 (+ hour (string-to-number (match-string 3 ans)))
  10081. minute (string-to-number (match-string 2 ans))
  10082. m2 (+ minute (if (match-end 5) (string-to-number
  10083. (match-string 5 ans))0)))
  10084. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10085. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10086. t t ans)))
  10087. ;; Check if there is a time range
  10088. (when (boundp 'org-end-time-was-given)
  10089. (setq org-time-was-given nil)
  10090. (when (and (string-match org-plain-time-of-day-regexp ans)
  10091. (match-end 8))
  10092. (setq org-end-time-was-given (match-string 8 ans))
  10093. (setq ans (concat (substring ans 0 (match-beginning 7))
  10094. (substring ans (match-end 7))))))
  10095. (setq tl (parse-time-string ans)
  10096. day (or (nth 3 tl) (nth 3 defdecode))
  10097. month (or (nth 4 tl)
  10098. (if (and org-read-date-prefer-future
  10099. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10100. (1+ (nth 4 defdecode))
  10101. (nth 4 defdecode)))
  10102. year (or (nth 5 tl)
  10103. (if (and org-read-date-prefer-future
  10104. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10105. (1+ (nth 5 defdecode))
  10106. (nth 5 defdecode)))
  10107. hour (or (nth 2 tl) (nth 2 defdecode))
  10108. minute (or (nth 1 tl) (nth 1 defdecode))
  10109. second (or (nth 0 tl) 0)
  10110. wday (nth 6 tl))
  10111. ;; Special date definitions below
  10112. (cond
  10113. (iso-week
  10114. ;; There was an iso week
  10115. (setq year (or iso-year year)
  10116. day (or iso-weekday wday 1)
  10117. wday nil ; to make sure that the trigger below does not match
  10118. iso-date (calendar-gregorian-from-absolute
  10119. (calendar-absolute-from-iso
  10120. (list iso-week day year))))
  10121. ; FIXME: Should we also push ISO weeks into the future?
  10122. ; (when (and org-read-date-prefer-future
  10123. ; (not iso-year)
  10124. ; (< (calendar-absolute-from-gregorian iso-date)
  10125. ; (time-to-days (current-time))))
  10126. ; (setq year (1+ year)
  10127. ; iso-date (calendar-gregorian-from-absolute
  10128. ; (calendar-absolute-from-iso
  10129. ; (list iso-week day year)))))
  10130. (setq month (car iso-date)
  10131. year (nth 2 iso-date)
  10132. day (nth 1 iso-date)))
  10133. (deltan
  10134. (unless deltadef
  10135. (let ((now (decode-time (current-time))))
  10136. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10137. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10138. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10139. ((equal deltaw "m") (setq month (+ month deltan)))
  10140. ((equal deltaw "y") (setq year (+ year deltan)))))
  10141. ((and wday (not (nth 3 tl)))
  10142. ;; Weekday was given, but no day, so pick that day in the week
  10143. ;; on or after the derived date.
  10144. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10145. (unless (equal wday wday1)
  10146. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10147. (if (and (boundp 'org-time-was-given)
  10148. (nth 2 tl))
  10149. (setq org-time-was-given t))
  10150. (if (< year 100) (setq year (+ 2000 year)))
  10151. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10152. (list second minute hour day month year)))
  10153. (defvar parse-time-weekdays)
  10154. (defun org-read-date-get-relative (s today default)
  10155. "Check string S for special relative date string.
  10156. TODAY and DEFAULT are internal times, for today and for a default.
  10157. Return shift list (N what def-flag)
  10158. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10159. N is the number of WHATs to shift.
  10160. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10161. the DEFAULT date rather than TODAY."
  10162. (when (and
  10163. (string-match
  10164. (concat
  10165. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10166. "\\([0-9]+\\)?"
  10167. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10168. "\\([ \t]\\|$\\)") s)
  10169. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10170. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10171. (string-to-char (substring (match-string 1 s) -1))
  10172. ?+))
  10173. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10174. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10175. (what (if (match-end 3) (match-string 3 s) "d"))
  10176. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10177. (date (if rel default today))
  10178. (wday (nth 6 (decode-time date)))
  10179. delta)
  10180. (if wday1
  10181. (progn
  10182. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10183. (if (= dir ?-) (setq delta (- delta 7)))
  10184. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10185. (list delta "d" rel))
  10186. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10187. (defun org-eval-in-calendar (form &optional keepdate)
  10188. "Eval FORM in the calendar window and return to current window.
  10189. Also, store the cursor date in variable org-ans2."
  10190. (let ((sw (selected-window)))
  10191. (select-window (get-buffer-window "*Calendar*"))
  10192. (eval form)
  10193. (when (and (not keepdate) (calendar-cursor-to-date))
  10194. (let* ((date (calendar-cursor-to-date))
  10195. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10196. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10197. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10198. (select-window sw)))
  10199. (defun org-calendar-select ()
  10200. "Return to `org-read-date' with the date currently selected.
  10201. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10202. (interactive)
  10203. (when (calendar-cursor-to-date)
  10204. (let* ((date (calendar-cursor-to-date))
  10205. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10206. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10207. (if (active-minibuffer-window) (exit-minibuffer))))
  10208. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10209. "Insert a date stamp for the date given by the internal TIME.
  10210. WITH-HM means, use the stamp format that includes the time of the day.
  10211. INACTIVE means use square brackets instead of angular ones, so that the
  10212. stamp will not contribute to the agenda.
  10213. PRE and POST are optional strings to be inserted before and after the
  10214. stamp.
  10215. The command returns the inserted time stamp."
  10216. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10217. stamp)
  10218. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10219. (insert-before-markers (or pre ""))
  10220. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10221. (when (listp extra)
  10222. (setq extra (car extra))
  10223. (if (and (stringp extra)
  10224. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10225. (setq extra (format "-%02d:%02d"
  10226. (string-to-number (match-string 1 extra))
  10227. (string-to-number (match-string 2 extra))))
  10228. (setq extra nil)))
  10229. (when extra
  10230. (backward-char 1)
  10231. (insert-before-markers extra)
  10232. (forward-char 1))
  10233. (insert-before-markers (or post ""))
  10234. (setq org-last-inserted-timestamp stamp)))
  10235. (defun org-toggle-time-stamp-overlays ()
  10236. "Toggle the use of custom time stamp formats."
  10237. (interactive)
  10238. (setq org-display-custom-times (not org-display-custom-times))
  10239. (unless org-display-custom-times
  10240. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10241. (while (setq p (next-single-property-change p 'display))
  10242. (if (and (get-text-property p 'display)
  10243. (eq (get-text-property p 'face) 'org-date))
  10244. (remove-text-properties
  10245. p (setq p (next-single-property-change p 'display))
  10246. '(display t))))
  10247. (set-buffer-modified-p bmp)))
  10248. (if (featurep 'xemacs)
  10249. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10250. (org-restart-font-lock)
  10251. (setq org-table-may-need-update t)
  10252. (if org-display-custom-times
  10253. (message "Time stamps are overlayed with custom format")
  10254. (message "Time stamp overlays removed")))
  10255. (defun org-display-custom-time (beg end)
  10256. "Overlay modified time stamp format over timestamp between BEG and END."
  10257. (let* ((ts (buffer-substring beg end))
  10258. t1 w1 with-hm tf time str w2 (off 0))
  10259. (save-match-data
  10260. (setq t1 (org-parse-time-string ts t))
  10261. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10262. (setq off (- (match-end 0) (match-beginning 0)))))
  10263. (setq end (- end off))
  10264. (setq w1 (- end beg)
  10265. with-hm (and (nth 1 t1) (nth 2 t1))
  10266. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10267. time (org-fix-decoded-time t1)
  10268. str (org-add-props
  10269. (format-time-string
  10270. (substring tf 1 -1) (apply 'encode-time time))
  10271. nil 'mouse-face 'highlight)
  10272. w2 (length str))
  10273. (if (not (= w2 w1))
  10274. (add-text-properties (1+ beg) (+ 2 beg)
  10275. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10276. (if (featurep 'xemacs)
  10277. (progn
  10278. (put-text-property beg end 'invisible t)
  10279. (put-text-property beg end 'end-glyph (make-glyph str)))
  10280. (put-text-property beg end 'display str))))
  10281. (defun org-translate-time (string)
  10282. "Translate all timestamps in STRING to custom format.
  10283. But do this only if the variable `org-display-custom-times' is set."
  10284. (when org-display-custom-times
  10285. (save-match-data
  10286. (let* ((start 0)
  10287. (re org-ts-regexp-both)
  10288. t1 with-hm inactive tf time str beg end)
  10289. (while (setq start (string-match re string start))
  10290. (setq beg (match-beginning 0)
  10291. end (match-end 0)
  10292. t1 (save-match-data
  10293. (org-parse-time-string (substring string beg end) t))
  10294. with-hm (and (nth 1 t1) (nth 2 t1))
  10295. inactive (equal (substring string beg (1+ beg)) "[")
  10296. tf (funcall (if with-hm 'cdr 'car)
  10297. org-time-stamp-custom-formats)
  10298. time (org-fix-decoded-time t1)
  10299. str (format-time-string
  10300. (concat
  10301. (if inactive "[" "<") (substring tf 1 -1)
  10302. (if inactive "]" ">"))
  10303. (apply 'encode-time time))
  10304. string (replace-match str t t string)
  10305. start (+ start (length str)))))))
  10306. string)
  10307. (defun org-fix-decoded-time (time)
  10308. "Set 0 instead of nil for the first 6 elements of time.
  10309. Don't touch the rest."
  10310. (let ((n 0))
  10311. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10312. (defun org-days-to-time (timestamp-string)
  10313. "Difference between TIMESTAMP-STRING and now in days."
  10314. (- (time-to-days (org-time-string-to-time timestamp-string))
  10315. (time-to-days (current-time))))
  10316. (defun org-deadline-close (timestamp-string &optional ndays)
  10317. "Is the time in TIMESTAMP-STRING close to the current date?"
  10318. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10319. (and (< (org-days-to-time timestamp-string) ndays)
  10320. (not (org-entry-is-done-p))))
  10321. (defun org-get-wdays (ts)
  10322. "Get the deadline lead time appropriate for timestring TS."
  10323. (cond
  10324. ((<= org-deadline-warning-days 0)
  10325. ;; 0 or negative, enforce this value no matter what
  10326. (- org-deadline-warning-days))
  10327. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10328. ;; lead time is specified.
  10329. (floor (* (string-to-number (match-string 1 ts))
  10330. (cdr (assoc (match-string 2 ts)
  10331. '(("d" . 1) ("w" . 7)
  10332. ("m" . 30.4) ("y" . 365.25)))))))
  10333. ;; go for the default.
  10334. (t org-deadline-warning-days)))
  10335. (defun org-calendar-select-mouse (ev)
  10336. "Return to `org-read-date' with the date currently selected.
  10337. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10338. (interactive "e")
  10339. (mouse-set-point ev)
  10340. (when (calendar-cursor-to-date)
  10341. (let* ((date (calendar-cursor-to-date))
  10342. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10343. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10344. (if (active-minibuffer-window) (exit-minibuffer))))
  10345. (defun org-check-deadlines (ndays)
  10346. "Check if there are any deadlines due or past due.
  10347. A deadline is considered due if it happens within `org-deadline-warning-days'
  10348. days from today's date. If the deadline appears in an entry marked DONE,
  10349. it is not shown. The prefix arg NDAYS can be used to test that many
  10350. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10351. (interactive "P")
  10352. (let* ((org-warn-days
  10353. (cond
  10354. ((equal ndays '(4)) 100000)
  10355. (ndays (prefix-numeric-value ndays))
  10356. (t (abs org-deadline-warning-days))))
  10357. (case-fold-search nil)
  10358. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10359. (callback
  10360. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10361. (message "%d deadlines past-due or due within %d days"
  10362. (org-occur regexp nil callback)
  10363. org-warn-days)))
  10364. (defun org-check-before-date (date)
  10365. "Check if there are deadlines or scheduled entries before DATE."
  10366. (interactive (list (org-read-date)))
  10367. (let ((case-fold-search nil)
  10368. (regexp (concat "\\<\\(" org-deadline-string
  10369. "\\|" org-scheduled-string
  10370. "\\) *<\\([^>]+\\)>"))
  10371. (callback
  10372. (lambda () (time-less-p
  10373. (org-time-string-to-time (match-string 2))
  10374. (org-time-string-to-time date)))))
  10375. (message "%d entries before %s"
  10376. (org-occur regexp nil callback) date)))
  10377. (defun org-evaluate-time-range (&optional to-buffer)
  10378. "Evaluate a time range by computing the difference between start and end.
  10379. Normally the result is just printed in the echo area, but with prefix arg
  10380. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10381. If the time range is actually in a table, the result is inserted into the
  10382. next column.
  10383. For time difference computation, a year is assumed to be exactly 365
  10384. days in order to avoid rounding problems."
  10385. (interactive "P")
  10386. (or
  10387. (org-clock-update-time-maybe)
  10388. (save-excursion
  10389. (unless (org-at-date-range-p t)
  10390. (goto-char (point-at-bol))
  10391. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10392. (if (not (org-at-date-range-p t))
  10393. (error "Not at a time-stamp range, and none found in current line")))
  10394. (let* ((ts1 (match-string 1))
  10395. (ts2 (match-string 2))
  10396. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10397. (match-end (match-end 0))
  10398. (time1 (org-time-string-to-time ts1))
  10399. (time2 (org-time-string-to-time ts2))
  10400. (t1 (time-to-seconds time1))
  10401. (t2 (time-to-seconds time2))
  10402. (diff (abs (- t2 t1)))
  10403. (negative (< (- t2 t1) 0))
  10404. ;; (ys (floor (* 365 24 60 60)))
  10405. (ds (* 24 60 60))
  10406. (hs (* 60 60))
  10407. (fy "%dy %dd %02d:%02d")
  10408. (fy1 "%dy %dd")
  10409. (fd "%dd %02d:%02d")
  10410. (fd1 "%dd")
  10411. (fh "%02d:%02d")
  10412. y d h m align)
  10413. (if havetime
  10414. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10415. y 0
  10416. d (floor (/ diff ds)) diff (mod diff ds)
  10417. h (floor (/ diff hs)) diff (mod diff hs)
  10418. m (floor (/ diff 60)))
  10419. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10420. y 0
  10421. d (floor (+ (/ diff ds) 0.5))
  10422. h 0 m 0))
  10423. (if (not to-buffer)
  10424. (message "%s" (org-make-tdiff-string y d h m))
  10425. (if (org-at-table-p)
  10426. (progn
  10427. (goto-char match-end)
  10428. (setq align t)
  10429. (and (looking-at " *|") (goto-char (match-end 0))))
  10430. (goto-char match-end))
  10431. (if (looking-at
  10432. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10433. (replace-match ""))
  10434. (if negative (insert " -"))
  10435. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10436. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10437. (insert " " (format fh h m))))
  10438. (if align (org-table-align))
  10439. (message "Time difference inserted")))))
  10440. (defun org-make-tdiff-string (y d h m)
  10441. (let ((fmt "")
  10442. (l nil))
  10443. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10444. l (push y l)))
  10445. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10446. l (push d l)))
  10447. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10448. l (push h l)))
  10449. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10450. l (push m l)))
  10451. (apply 'format fmt (nreverse l))))
  10452. (defun org-time-string-to-time (s)
  10453. (apply 'encode-time (org-parse-time-string s)))
  10454. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10455. "Convert a time stamp to an absolute day number.
  10456. If there is a specifyer for a cyclic time stamp, get the closest date to
  10457. DAYNR.
  10458. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10459. (cond
  10460. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10461. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10462. daynr
  10463. (+ daynr 1000)))
  10464. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10465. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10466. (time-to-days (current-time))) (match-string 0 s)
  10467. prefer show-all))
  10468. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10469. (defun org-days-to-iso-week (days)
  10470. "Return the iso week number."
  10471. (require 'cal-iso)
  10472. (car (calendar-iso-from-absolute days)))
  10473. (defun org-small-year-to-year (year)
  10474. "Convert 2-digit years into 4-digit years.
  10475. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10476. The year 2000 cannot be abbreviated. Any year larger than 99
  10477. is returned unchanged."
  10478. (if (< year 38)
  10479. (setq year (+ 2000 year))
  10480. (if (< year 100)
  10481. (setq year (+ 1900 year))))
  10482. year)
  10483. (defun org-time-from-absolute (d)
  10484. "Return the time corresponding to date D.
  10485. D may be an absolute day number, or a calendar-type list (month day year)."
  10486. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10487. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10488. (defun org-calendar-holiday ()
  10489. "List of holidays, for Diary display in Org-mode."
  10490. (require 'holidays)
  10491. (let ((hl (funcall
  10492. (if (fboundp 'calendar-check-holidays)
  10493. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10494. (if hl (mapconcat 'identity hl "; "))))
  10495. (defun org-diary-sexp-entry (sexp entry date)
  10496. "Process a SEXP diary ENTRY for DATE."
  10497. (require 'diary-lib)
  10498. (let ((result (if calendar-debug-sexp
  10499. (let ((stack-trace-on-error t))
  10500. (eval (car (read-from-string sexp))))
  10501. (condition-case nil
  10502. (eval (car (read-from-string sexp)))
  10503. (error
  10504. (beep)
  10505. (message "Bad sexp at line %d in %s: %s"
  10506. (org-current-line)
  10507. (buffer-file-name) sexp)
  10508. (sleep-for 2))))))
  10509. (cond ((stringp result) result)
  10510. ((and (consp result)
  10511. (stringp (cdr result))) (cdr result))
  10512. (result entry)
  10513. (t nil))))
  10514. (defun org-diary-to-ical-string (frombuf)
  10515. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10516. This uses the icalendar.el library."
  10517. (let* ((tmpdir (if (featurep 'xemacs)
  10518. (temp-directory)
  10519. temporary-file-directory))
  10520. (tmpfile (make-temp-name
  10521. (expand-file-name "orgics" tmpdir)))
  10522. buf rtn b e)
  10523. (save-excursion
  10524. (set-buffer frombuf)
  10525. (icalendar-export-region (point-min) (point-max) tmpfile)
  10526. (setq buf (find-buffer-visiting tmpfile))
  10527. (set-buffer buf)
  10528. (goto-char (point-min))
  10529. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10530. (setq b (match-beginning 0)))
  10531. (goto-char (point-max))
  10532. (if (re-search-backward "^END:VEVENT" nil t)
  10533. (setq e (match-end 0)))
  10534. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10535. (kill-buffer buf)
  10536. (delete-file tmpfile)
  10537. rtn))
  10538. (defun org-closest-date (start current change prefer show-all)
  10539. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10540. When PREFER is `past' return a date that is either CURRENT or past.
  10541. When PREFER is `future', return a date that is either CURRENT or future.
  10542. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10543. ;; Make the proper lists from the dates
  10544. (catch 'exit
  10545. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10546. dn dw sday cday n1 n2
  10547. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10548. (setq start (org-date-to-gregorian start)
  10549. current (org-date-to-gregorian
  10550. (if show-all
  10551. current
  10552. (time-to-days (current-time))))
  10553. sday (calendar-absolute-from-gregorian start)
  10554. cday (calendar-absolute-from-gregorian current))
  10555. (if (<= cday sday) (throw 'exit sday))
  10556. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10557. (setq dn (string-to-number (match-string 1 change))
  10558. dw (cdr (assoc (match-string 2 change) a1)))
  10559. (error "Invalid change specifyer: %s" change))
  10560. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10561. (cond
  10562. ((eq dw 'day)
  10563. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10564. n2 (+ n1 dn)))
  10565. ((eq dw 'year)
  10566. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10567. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10568. (setq date1 (list m d y1)
  10569. n1 (calendar-absolute-from-gregorian date1)
  10570. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10571. n2 (calendar-absolute-from-gregorian date2)))
  10572. ((eq dw 'month)
  10573. ;; approx number of month between the two dates
  10574. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10575. ;; How often does dn fit in there?
  10576. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10577. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10578. m (+ m nm)
  10579. ny (floor (/ m 12))
  10580. y (+ y ny)
  10581. m (- m (* ny 12)))
  10582. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10583. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10584. (setq m2 (+ m dn) y2 y)
  10585. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10586. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10587. (while (<= n2 cday)
  10588. (setq n1 n2 m m2 y y2)
  10589. (setq m2 (+ m dn) y2 y)
  10590. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10591. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10592. (if show-all
  10593. (cond
  10594. ((eq prefer 'past) n1)
  10595. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10596. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10597. (cond
  10598. ((eq prefer 'past) n1)
  10599. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10600. (t (if (= cday n1) n1 n2)))))))
  10601. (defun org-date-to-gregorian (date)
  10602. "Turn any specification of DATE into a gregorian date for the calendar."
  10603. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10604. ((and (listp date) (= (length date) 3)) date)
  10605. ((stringp date)
  10606. (setq date (org-parse-time-string date))
  10607. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10608. ((listp date)
  10609. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10610. (defun org-parse-time-string (s &optional nodefault)
  10611. "Parse the standard Org-mode time string.
  10612. This should be a lot faster than the normal `parse-time-string'.
  10613. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10614. hour and minute fields will be nil if not given."
  10615. (if (string-match org-ts-regexp0 s)
  10616. (list 0
  10617. (if (or (match-beginning 8) (not nodefault))
  10618. (string-to-number (or (match-string 8 s) "0")))
  10619. (if (or (match-beginning 7) (not nodefault))
  10620. (string-to-number (or (match-string 7 s) "0")))
  10621. (string-to-number (match-string 4 s))
  10622. (string-to-number (match-string 3 s))
  10623. (string-to-number (match-string 2 s))
  10624. nil nil nil)
  10625. (make-list 9 0)))
  10626. (defun org-timestamp-up (&optional arg)
  10627. "Increase the date item at the cursor by one.
  10628. If the cursor is on the year, change the year. If it is on the month or
  10629. the day, change that.
  10630. With prefix ARG, change by that many units."
  10631. (interactive "p")
  10632. (org-timestamp-change (prefix-numeric-value arg)))
  10633. (defun org-timestamp-down (&optional arg)
  10634. "Decrease the date item at the cursor by one.
  10635. If the cursor is on the year, change the year. If it is on the month or
  10636. the day, change that.
  10637. With prefix ARG, change by that many units."
  10638. (interactive "p")
  10639. (org-timestamp-change (- (prefix-numeric-value arg))))
  10640. (defun org-timestamp-up-day (&optional arg)
  10641. "Increase the date in the time stamp by one day.
  10642. With prefix ARG, change that many days."
  10643. (interactive "p")
  10644. (if (and (not (org-at-timestamp-p t))
  10645. (org-on-heading-p))
  10646. (org-todo 'up)
  10647. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10648. (defun org-timestamp-down-day (&optional arg)
  10649. "Decrease the date in the time stamp by one day.
  10650. With prefix ARG, change that many days."
  10651. (interactive "p")
  10652. (if (and (not (org-at-timestamp-p t))
  10653. (org-on-heading-p))
  10654. (org-todo 'down)
  10655. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10656. (defun org-at-timestamp-p (&optional inactive-ok)
  10657. "Determine if the cursor is in or at a timestamp."
  10658. (interactive)
  10659. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10660. (pos (point))
  10661. (ans (or (looking-at tsr)
  10662. (save-excursion
  10663. (skip-chars-backward "^[<\n\r\t")
  10664. (if (> (point) (point-min)) (backward-char 1))
  10665. (and (looking-at tsr)
  10666. (> (- (match-end 0) pos) -1))))))
  10667. (and ans
  10668. (boundp 'org-ts-what)
  10669. (setq org-ts-what
  10670. (cond
  10671. ((= pos (match-beginning 0)) 'bracket)
  10672. ((= pos (1- (match-end 0))) 'bracket)
  10673. ((org-pos-in-match-range pos 2) 'year)
  10674. ((org-pos-in-match-range pos 3) 'month)
  10675. ((org-pos-in-match-range pos 7) 'hour)
  10676. ((org-pos-in-match-range pos 8) 'minute)
  10677. ((or (org-pos-in-match-range pos 4)
  10678. (org-pos-in-match-range pos 5)) 'day)
  10679. ((and (> pos (or (match-end 8) (match-end 5)))
  10680. (< pos (match-end 0)))
  10681. (- pos (or (match-end 8) (match-end 5))))
  10682. (t 'day))))
  10683. ans))
  10684. (defun org-toggle-timestamp-type ()
  10685. "Toggle the type (<active> or [inactive]) of a time stamp."
  10686. (interactive)
  10687. (when (org-at-timestamp-p t)
  10688. (let ((beg (match-beginning 0)) (end (match-end 0))
  10689. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10690. (save-excursion
  10691. (goto-char beg)
  10692. (while (re-search-forward "[][<>]" end t)
  10693. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10694. t t)))
  10695. (message "Timestamp is now %sactive"
  10696. (if (equal (char-after beg) ?<) "" "in")))))
  10697. (defun org-timestamp-change (n &optional what)
  10698. "Change the date in the time stamp at point.
  10699. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10700. `year', `minute', `second'. If WHAT is not given, the cursor position
  10701. in the timestamp determines what will be changed."
  10702. (let ((pos (point))
  10703. with-hm inactive
  10704. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10705. org-ts-what
  10706. extra rem
  10707. ts time time0)
  10708. (if (not (org-at-timestamp-p t))
  10709. (error "Not at a timestamp"))
  10710. (if (and (not what) (eq org-ts-what 'bracket))
  10711. (org-toggle-timestamp-type)
  10712. (if (and (not what) (not (eq org-ts-what 'day))
  10713. org-display-custom-times
  10714. (get-text-property (point) 'display)
  10715. (not (get-text-property (1- (point)) 'display)))
  10716. (setq org-ts-what 'day))
  10717. (setq org-ts-what (or what org-ts-what)
  10718. inactive (= (char-after (match-beginning 0)) ?\[)
  10719. ts (match-string 0))
  10720. (replace-match "")
  10721. (if (string-match
  10722. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10723. ts)
  10724. (setq extra (match-string 1 ts)))
  10725. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10726. (setq with-hm t))
  10727. (setq time0 (org-parse-time-string ts))
  10728. (when (and (eq org-ts-what 'minute)
  10729. (eq current-prefix-arg nil))
  10730. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10731. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10732. (setcar (cdr time0) (+ (nth 1 time0)
  10733. (if (> n 0) (- rem) (- dm rem))))))
  10734. (setq time
  10735. (encode-time (or (car time0) 0)
  10736. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10737. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10738. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10739. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10740. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10741. (nthcdr 6 time0)))
  10742. (when (integerp org-ts-what)
  10743. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10744. (if (eq what 'calendar)
  10745. (let ((cal-date (org-get-date-from-calendar)))
  10746. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10747. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10748. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10749. (setcar time0 (or (car time0) 0))
  10750. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10751. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10752. (setq time (apply 'encode-time time0))))
  10753. (setq org-last-changed-timestamp
  10754. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10755. (org-clock-update-time-maybe)
  10756. (goto-char pos)
  10757. ;; Try to recenter the calendar window, if any
  10758. (if (and org-calendar-follow-timestamp-change
  10759. (get-buffer-window "*Calendar*" t)
  10760. (memq org-ts-what '(day month year)))
  10761. (org-recenter-calendar (time-to-days time))))))
  10762. (defun org-modify-ts-extra (s pos n dm)
  10763. "Change the different parts of the lead-time and repeat fields in timestamp."
  10764. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10765. ng h m new rem)
  10766. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10767. (cond
  10768. ((or (org-pos-in-match-range pos 2)
  10769. (org-pos-in-match-range pos 3))
  10770. (setq m (string-to-number (match-string 3 s))
  10771. h (string-to-number (match-string 2 s)))
  10772. (if (org-pos-in-match-range pos 2)
  10773. (setq h (+ h n))
  10774. (setq n (* dm (org-no-warnings (signum n))))
  10775. (when (not (= 0 (setq rem (% m dm))))
  10776. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10777. (setq m (+ m n)))
  10778. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10779. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10780. (setq h (min 24 (max 0 h)))
  10781. (setq ng 1 new (format "-%02d:%02d" h m)))
  10782. ((org-pos-in-match-range pos 6)
  10783. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10784. ((org-pos-in-match-range pos 5)
  10785. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10786. ((org-pos-in-match-range pos 9)
  10787. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10788. ((org-pos-in-match-range pos 8)
  10789. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10790. (when ng
  10791. (setq s (concat
  10792. (substring s 0 (match-beginning ng))
  10793. new
  10794. (substring s (match-end ng))))))
  10795. s))
  10796. (defun org-recenter-calendar (date)
  10797. "If the calendar is visible, recenter it to DATE."
  10798. (let* ((win (selected-window))
  10799. (cwin (get-buffer-window "*Calendar*" t))
  10800. (calendar-move-hook nil))
  10801. (when cwin
  10802. (select-window cwin)
  10803. (calendar-goto-date (if (listp date) date
  10804. (calendar-gregorian-from-absolute date)))
  10805. (select-window win))))
  10806. (defun org-goto-calendar (&optional arg)
  10807. "Go to the Emacs calendar at the current date.
  10808. If there is a time stamp in the current line, go to that date.
  10809. A prefix ARG can be used to force the current date."
  10810. (interactive "P")
  10811. (let ((tsr org-ts-regexp) diff
  10812. (calendar-move-hook nil)
  10813. (calendar-view-holidays-initially-flag nil)
  10814. (view-calendar-holidays-initially nil)
  10815. (calendar-view-diary-initially-flag nil)
  10816. (view-diary-entries-initially nil))
  10817. (if (or (org-at-timestamp-p)
  10818. (save-excursion
  10819. (beginning-of-line 1)
  10820. (looking-at (concat ".*" tsr))))
  10821. (let ((d1 (time-to-days (current-time)))
  10822. (d2 (time-to-days
  10823. (org-time-string-to-time (match-string 1)))))
  10824. (setq diff (- d2 d1))))
  10825. (calendar)
  10826. (calendar-goto-today)
  10827. (if (and diff (not arg)) (calendar-forward-day diff))))
  10828. (defun org-get-date-from-calendar ()
  10829. "Return a list (month day year) of date at point in calendar."
  10830. (with-current-buffer "*Calendar*"
  10831. (save-match-data
  10832. (calendar-cursor-to-date))))
  10833. (defun org-date-from-calendar ()
  10834. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10835. If there is already a time stamp at the cursor position, update it."
  10836. (interactive)
  10837. (if (org-at-timestamp-p t)
  10838. (org-timestamp-change 0 'calendar)
  10839. (let ((cal-date (org-get-date-from-calendar)))
  10840. (org-insert-time-stamp
  10841. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10842. (defun org-minutes-to-hh:mm-string (m)
  10843. "Compute H:MM from a number of minutes."
  10844. (let ((h (/ m 60)))
  10845. (setq m (- m (* 60 h)))
  10846. (format org-time-clocksum-format h m)))
  10847. (defun org-hh:mm-string-to-minutes (s)
  10848. "Convert a string H:MM to a number of minutes."
  10849. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10850. (+ (* (string-to-number (match-string 1 s)) 60)
  10851. (string-to-number (match-string 2 s)))
  10852. 0))
  10853. ;;;; Agenda files
  10854. ;;;###autoload
  10855. (defun org-iswitchb (&optional arg)
  10856. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10857. With a prefix argument, restrict available to files.
  10858. With two prefix arguments, restrict available buffers to agenda files.
  10859. Due to some yet unresolved reason, the global function
  10860. `iswitchb-mode' needs to be active for this function to work."
  10861. (interactive "P")
  10862. (require 'iswitchb)
  10863. (let ((enabled iswitchb-mode) blist)
  10864. (or enabled (iswitchb-mode 1))
  10865. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10866. ((equal arg '(16)) (org-buffer-list 'agenda))
  10867. (t (org-buffer-list))))
  10868. (unwind-protect
  10869. (let ((iswitchb-make-buflist-hook
  10870. (lambda ()
  10871. (setq iswitchb-temp-buflist
  10872. (mapcar 'buffer-name blist)))))
  10873. (switch-to-buffer
  10874. (iswitchb-read-buffer
  10875. "Switch-to: " nil t))
  10876. (or enabled (iswitchb-mode -1))))))
  10877. ;;;###autoload
  10878. (defun org-ido-switchb (&optional arg)
  10879. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  10880. With a prefix argument, restrict available to files.
  10881. With two prefix arguments, restrict available buffers to agenda files."
  10882. (interactive "P")
  10883. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10884. ((equal arg '(16)) (org-buffer-list 'agenda))
  10885. (t (org-buffer-list)))))
  10886. (switch-to-buffer
  10887. (org-ido-completing-read "Org buffer: "
  10888. (mapcar 'buffer-name blist)
  10889. nil t))))
  10890. (defun org-buffer-list (&optional predicate exclude-tmp)
  10891. "Return a list of Org buffers.
  10892. PREDICATE can be `export', `files' or `agenda'.
  10893. export restrict the list to Export buffers.
  10894. files restrict the list to buffers visiting Org files.
  10895. agenda restrict the list to buffers visiting agenda files.
  10896. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10897. (let* ((bfn nil)
  10898. (agenda-files (and (eq predicate 'agenda)
  10899. (mapcar 'file-truename (org-agenda-files t))))
  10900. (filter
  10901. (cond
  10902. ((eq predicate 'files)
  10903. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10904. ((eq predicate 'export)
  10905. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10906. ((eq predicate 'agenda)
  10907. (lambda (b)
  10908. (with-current-buffer b
  10909. (and (eq major-mode 'org-mode)
  10910. (setq bfn (buffer-file-name b))
  10911. (member (file-truename bfn) agenda-files)))))
  10912. (t (lambda (b) (with-current-buffer b
  10913. (or (eq major-mode 'org-mode)
  10914. (string-match "\*Org .*Export"
  10915. (buffer-name b)))))))))
  10916. (delq nil
  10917. (mapcar
  10918. (lambda(b)
  10919. (if (and (funcall filter b)
  10920. (or (not exclude-tmp)
  10921. (not (string-match "tmp" (buffer-name b)))))
  10922. b
  10923. nil))
  10924. (buffer-list)))))
  10925. (defun org-agenda-files (&optional unrestricted archives)
  10926. "Get the list of agenda files.
  10927. Optional UNRESTRICTED means return the full list even if a restriction
  10928. is currently in place.
  10929. When ARCHIVES is t, include all archive files hat are really being
  10930. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10931. `org-agenda-archives-mode' is t."
  10932. (let ((files
  10933. (cond
  10934. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10935. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10936. ((listp org-agenda-files) org-agenda-files)
  10937. (t (error "Invalid value of `org-agenda-files'")))))
  10938. (setq files (apply 'append
  10939. (mapcar (lambda (f)
  10940. (if (file-directory-p f)
  10941. (directory-files
  10942. f t org-agenda-file-regexp)
  10943. (list f)))
  10944. files)))
  10945. (when org-agenda-skip-unavailable-files
  10946. (setq files (delq nil
  10947. (mapcar (function
  10948. (lambda (file)
  10949. (and (file-readable-p file) file)))
  10950. files))))
  10951. (when (or (eq archives t)
  10952. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10953. (setq files (org-add-archive-files files)))
  10954. files))
  10955. (defun org-edit-agenda-file-list ()
  10956. "Edit the list of agenda files.
  10957. Depending on setup, this either uses customize to edit the variable
  10958. `org-agenda-files', or it visits the file that is holding the list. In the
  10959. latter case, the buffer is set up in a way that saving it automatically kills
  10960. the buffer and restores the previous window configuration."
  10961. (interactive)
  10962. (if (stringp org-agenda-files)
  10963. (let ((cw (current-window-configuration)))
  10964. (find-file org-agenda-files)
  10965. (org-set-local 'org-window-configuration cw)
  10966. (org-add-hook 'after-save-hook
  10967. (lambda ()
  10968. (set-window-configuration
  10969. (prog1 org-window-configuration
  10970. (kill-buffer (current-buffer))))
  10971. (org-install-agenda-files-menu)
  10972. (message "New agenda file list installed"))
  10973. nil 'local)
  10974. (message "%s" (substitute-command-keys
  10975. "Edit list and finish with \\[save-buffer]")))
  10976. (customize-variable 'org-agenda-files)))
  10977. (defun org-store-new-agenda-file-list (list)
  10978. "Set new value for the agenda file list and save it correctly."
  10979. (if (stringp org-agenda-files)
  10980. (let ((f org-agenda-files) b)
  10981. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10982. (with-temp-file f
  10983. (insert (mapconcat 'identity list "\n") "\n")))
  10984. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10985. (setq org-agenda-files list)
  10986. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10987. (defun org-read-agenda-file-list ()
  10988. "Read the list of agenda files from a file."
  10989. (when (file-directory-p org-agenda-files)
  10990. (error "`org-agenda-files' cannot be a single directory"))
  10991. (when (stringp org-agenda-files)
  10992. (with-temp-buffer
  10993. (insert-file-contents org-agenda-files)
  10994. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10995. ;;;###autoload
  10996. (defun org-cycle-agenda-files ()
  10997. "Cycle through the files in `org-agenda-files'.
  10998. If the current buffer visits an agenda file, find the next one in the list.
  10999. If the current buffer does not, find the first agenda file."
  11000. (interactive)
  11001. (let* ((fs (org-agenda-files t))
  11002. (files (append fs (list (car fs))))
  11003. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11004. file)
  11005. (unless files (error "No agenda files"))
  11006. (catch 'exit
  11007. (while (setq file (pop files))
  11008. (if (equal (file-truename file) tcf)
  11009. (when (car files)
  11010. (find-file (car files))
  11011. (throw 'exit t))))
  11012. (find-file (car fs)))
  11013. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11014. (defun org-agenda-file-to-front (&optional to-end)
  11015. "Move/add the current file to the top of the agenda file list.
  11016. If the file is not present in the list, it is added to the front. If it is
  11017. present, it is moved there. With optional argument TO-END, add/move to the
  11018. end of the list."
  11019. (interactive "P")
  11020. (let ((org-agenda-skip-unavailable-files nil)
  11021. (file-alist (mapcar (lambda (x)
  11022. (cons (file-truename x) x))
  11023. (org-agenda-files t)))
  11024. (ctf (file-truename buffer-file-name))
  11025. x had)
  11026. (setq x (assoc ctf file-alist) had x)
  11027. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11028. (if to-end
  11029. (setq file-alist (append (delq x file-alist) (list x)))
  11030. (setq file-alist (cons x (delq x file-alist))))
  11031. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11032. (org-install-agenda-files-menu)
  11033. (message "File %s to %s of agenda file list"
  11034. (if had "moved" "added") (if to-end "end" "front"))))
  11035. (defun org-remove-file (&optional file)
  11036. "Remove current file from the list of files in variable `org-agenda-files'.
  11037. These are the files which are being checked for agenda entries.
  11038. Optional argument FILE means, use this file instead of the current."
  11039. (interactive)
  11040. (let* ((org-agenda-skip-unavailable-files nil)
  11041. (file (or file buffer-file-name))
  11042. (true-file (file-truename file))
  11043. (afile (abbreviate-file-name file))
  11044. (files (delq nil (mapcar
  11045. (lambda (x)
  11046. (if (equal true-file
  11047. (file-truename x))
  11048. nil x))
  11049. (org-agenda-files t)))))
  11050. (if (not (= (length files) (length (org-agenda-files t))))
  11051. (progn
  11052. (org-store-new-agenda-file-list files)
  11053. (org-install-agenda-files-menu)
  11054. (message "Removed file: %s" afile))
  11055. (message "File was not in list: %s (not removed)" afile))))
  11056. (defun org-file-menu-entry (file)
  11057. (vector file (list 'find-file file) t))
  11058. (defun org-check-agenda-file (file)
  11059. "Make sure FILE exists. If not, ask user what to do."
  11060. (when (not (file-exists-p file))
  11061. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11062. (abbreviate-file-name file))
  11063. (let ((r (downcase (read-char-exclusive))))
  11064. (cond
  11065. ((equal r ?r)
  11066. (org-remove-file file)
  11067. (throw 'nextfile t))
  11068. (t (error "Abort"))))))
  11069. (defun org-get-agenda-file-buffer (file)
  11070. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11071. it to the list of buffers which might be released later."
  11072. (let ((buf (org-find-base-buffer-visiting file)))
  11073. (if buf
  11074. buf ; just return it
  11075. ;; Make a new buffer and remember it
  11076. (setq buf (find-file-noselect file))
  11077. (if buf (push buf org-agenda-new-buffers))
  11078. buf)))
  11079. (defun org-release-buffers (blist)
  11080. "Release all buffers in list, asking the user for confirmation when needed.
  11081. When a buffer is unmodified, it is just killed. When modified, it is saved
  11082. \(if the user agrees) and then killed."
  11083. (let (buf file)
  11084. (while (setq buf (pop blist))
  11085. (setq file (buffer-file-name buf))
  11086. (when (and (buffer-modified-p buf)
  11087. file
  11088. (y-or-n-p (format "Save file %s? " file)))
  11089. (with-current-buffer buf (save-buffer)))
  11090. (kill-buffer buf))))
  11091. (defun org-prepare-agenda-buffers (files)
  11092. "Create buffers for all agenda files, protect archived trees and comments."
  11093. (interactive)
  11094. (let ((pa '(:org-archived t))
  11095. (pc '(:org-comment t))
  11096. (pall '(:org-archived t :org-comment t))
  11097. (inhibit-read-only t)
  11098. (rea (concat ":" org-archive-tag ":"))
  11099. bmp file re)
  11100. (save-excursion
  11101. (save-restriction
  11102. (while (setq file (pop files))
  11103. (if (bufferp file)
  11104. (set-buffer file)
  11105. (org-check-agenda-file file)
  11106. (set-buffer (org-get-agenda-file-buffer file)))
  11107. (widen)
  11108. (setq bmp (buffer-modified-p))
  11109. (org-refresh-category-properties)
  11110. (setq org-todo-keywords-for-agenda
  11111. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11112. (setq org-done-keywords-for-agenda
  11113. (append org-done-keywords-for-agenda org-done-keywords))
  11114. (setq org-todo-keyword-alist-for-agenda
  11115. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11116. (setq org-tag-alist-for-agenda
  11117. (append org-tag-alist-for-agenda org-tag-alist))
  11118. (save-excursion
  11119. (remove-text-properties (point-min) (point-max) pall)
  11120. (when org-agenda-skip-archived-trees
  11121. (goto-char (point-min))
  11122. (while (re-search-forward rea nil t)
  11123. (if (org-on-heading-p t)
  11124. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11125. (goto-char (point-min))
  11126. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11127. (while (re-search-forward re nil t)
  11128. (add-text-properties
  11129. (match-beginning 0) (org-end-of-subtree t) pc)))
  11130. (set-buffer-modified-p bmp))))
  11131. (setq org-todo-keyword-alist-for-agenda
  11132. (org-uniquify org-todo-keyword-alist-for-agenda)
  11133. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11134. ;;;; Embedded LaTeX
  11135. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11136. "Keymap for the minor `org-cdlatex-mode'.")
  11137. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11138. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11139. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11140. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11141. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11142. (defvar org-cdlatex-texmathp-advice-is-done nil
  11143. "Flag remembering if we have applied the advice to texmathp already.")
  11144. (define-minor-mode org-cdlatex-mode
  11145. "Toggle the minor `org-cdlatex-mode'.
  11146. This mode supports entering LaTeX environment and math in LaTeX fragments
  11147. in Org-mode.
  11148. \\{org-cdlatex-mode-map}"
  11149. nil " OCDL" nil
  11150. (when org-cdlatex-mode (require 'cdlatex))
  11151. (unless org-cdlatex-texmathp-advice-is-done
  11152. (setq org-cdlatex-texmathp-advice-is-done t)
  11153. (defadvice texmathp (around org-math-always-on activate)
  11154. "Always return t in org-mode buffers.
  11155. This is because we want to insert math symbols without dollars even outside
  11156. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11157. an embedded LaTeX fragment, let texmathp do its job.
  11158. \\[org-cdlatex-mode-map]"
  11159. (interactive)
  11160. (let (p)
  11161. (cond
  11162. ((not (org-mode-p)) ad-do-it)
  11163. ((eq this-command 'cdlatex-math-symbol)
  11164. (setq ad-return-value t
  11165. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11166. (t
  11167. (let ((p (org-inside-LaTeX-fragment-p)))
  11168. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11169. (setq ad-return-value t
  11170. texmathp-why '("Org-mode embedded math" . 0))
  11171. (if p ad-do-it)))))))))
  11172. (defun turn-on-org-cdlatex ()
  11173. "Unconditionally turn on `org-cdlatex-mode'."
  11174. (org-cdlatex-mode 1))
  11175. (defun org-inside-LaTeX-fragment-p ()
  11176. "Test if point is inside a LaTeX fragment.
  11177. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11178. sequence appearing also before point.
  11179. Even though the matchers for math are configurable, this function assumes
  11180. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11181. delimiters are skipped when they have been removed by customization.
  11182. The return value is nil, or a cons cell with the delimiter and
  11183. and the position of this delimiter.
  11184. This function does a reasonably good job, but can locally be fooled by
  11185. for example currency specifications. For example it will assume being in
  11186. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11187. fragments that are properly closed, but during editing, we have to live
  11188. with the uncertainty caused by missing closing delimiters. This function
  11189. looks only before point, not after."
  11190. (catch 'exit
  11191. (let ((pos (point))
  11192. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11193. (lim (progn
  11194. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11195. (point)))
  11196. dd-on str (start 0) m re)
  11197. (goto-char pos)
  11198. (when dodollar
  11199. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11200. re (nth 1 (assoc "$" org-latex-regexps)))
  11201. (while (string-match re str start)
  11202. (cond
  11203. ((= (match-end 0) (length str))
  11204. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11205. ((= (match-end 0) (- (length str) 5))
  11206. (throw 'exit nil))
  11207. (t (setq start (match-end 0))))))
  11208. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11209. (goto-char pos)
  11210. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11211. (and (match-beginning 2) (throw 'exit nil))
  11212. ;; count $$
  11213. (while (re-search-backward "\\$\\$" lim t)
  11214. (setq dd-on (not dd-on)))
  11215. (goto-char pos)
  11216. (if dd-on (cons "$$" m))))))
  11217. (defun org-try-cdlatex-tab ()
  11218. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11219. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11220. - inside a LaTeX fragment, or
  11221. - after the first word in a line, where an abbreviation expansion could
  11222. insert a LaTeX environment."
  11223. (when org-cdlatex-mode
  11224. (cond
  11225. ((save-excursion
  11226. (skip-chars-backward "a-zA-Z0-9*")
  11227. (skip-chars-backward " \t")
  11228. (bolp))
  11229. (cdlatex-tab) t)
  11230. ((org-inside-LaTeX-fragment-p)
  11231. (cdlatex-tab) t)
  11232. (t nil))))
  11233. (defun org-cdlatex-underscore-caret (&optional arg)
  11234. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11235. Revert to the normal definition outside of these fragments."
  11236. (interactive "P")
  11237. (if (org-inside-LaTeX-fragment-p)
  11238. (call-interactively 'cdlatex-sub-superscript)
  11239. (let (org-cdlatex-mode)
  11240. (call-interactively (key-binding (vector last-input-event))))))
  11241. (defun org-cdlatex-math-modify (&optional arg)
  11242. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11243. Revert to the normal definition outside of these fragments."
  11244. (interactive "P")
  11245. (if (org-inside-LaTeX-fragment-p)
  11246. (call-interactively 'cdlatex-math-modify)
  11247. (let (org-cdlatex-mode)
  11248. (call-interactively (key-binding (vector last-input-event))))))
  11249. (defvar org-latex-fragment-image-overlays nil
  11250. "List of overlays carrying the images of latex fragments.")
  11251. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11252. (defun org-remove-latex-fragment-image-overlays ()
  11253. "Remove all overlays with LaTeX fragment images in current buffer."
  11254. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11255. (setq org-latex-fragment-image-overlays nil))
  11256. (defun org-preview-latex-fragment (&optional subtree)
  11257. "Preview the LaTeX fragment at point, or all locally or globally.
  11258. If the cursor is in a LaTeX fragment, create the image and overlay
  11259. it over the source code. If there is no fragment at point, display
  11260. all fragments in the current text, from one headline to the next. With
  11261. prefix SUBTREE, display all fragments in the current subtree. With a
  11262. double prefix `C-u C-u', or when the cursor is before the first headline,
  11263. display all fragments in the buffer.
  11264. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11265. (interactive "P")
  11266. (org-remove-latex-fragment-image-overlays)
  11267. (save-excursion
  11268. (save-restriction
  11269. (let (beg end at msg)
  11270. (cond
  11271. ((or (equal subtree '(16))
  11272. (not (save-excursion
  11273. (re-search-backward (concat "^" outline-regexp) nil t))))
  11274. (setq beg (point-min) end (point-max)
  11275. msg "Creating images for buffer...%s"))
  11276. ((equal subtree '(4))
  11277. (org-back-to-heading)
  11278. (setq beg (point) end (org-end-of-subtree t)
  11279. msg "Creating images for subtree...%s"))
  11280. (t
  11281. (if (setq at (org-inside-LaTeX-fragment-p))
  11282. (goto-char (max (point-min) (- (cdr at) 2)))
  11283. (org-back-to-heading))
  11284. (setq beg (point) end (progn (outline-next-heading) (point))
  11285. msg (if at "Creating image...%s"
  11286. "Creating images for entry...%s"))))
  11287. (message msg "")
  11288. (narrow-to-region beg end)
  11289. (goto-char beg)
  11290. (org-format-latex
  11291. (concat "ltxpng/" (file-name-sans-extension
  11292. (file-name-nondirectory
  11293. buffer-file-name)))
  11294. default-directory 'overlays msg at 'forbuffer)
  11295. (message msg "done. Use `C-c C-c' to remove images.")))))
  11296. (defvar org-latex-regexps
  11297. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11298. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11299. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11300. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11301. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11302. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11303. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11304. "Regular expressions for matching embedded LaTeX.")
  11305. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11306. "Replace LaTeX fragments with links to an image, and produce images."
  11307. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11308. (let* ((prefixnodir (file-name-nondirectory prefix))
  11309. (absprefix (expand-file-name prefix dir))
  11310. (todir (file-name-directory absprefix))
  11311. (opt org-format-latex-options)
  11312. (matchers (plist-get opt :matchers))
  11313. (re-list org-latex-regexps)
  11314. (cnt 0) txt link beg end re e checkdir
  11315. m n block linkfile movefile ov)
  11316. ;; Check if there are old images files with this prefix, and remove them
  11317. (when (file-directory-p todir)
  11318. (mapc 'delete-file
  11319. (directory-files
  11320. todir 'full
  11321. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11322. ;; Check the different regular expressions
  11323. (while (setq e (pop re-list))
  11324. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11325. block (if (nth 3 e) "\n\n" ""))
  11326. (when (member m matchers)
  11327. (goto-char (point-min))
  11328. (while (re-search-forward re nil t)
  11329. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11330. (setq txt (match-string n)
  11331. beg (match-beginning n) end (match-end n)
  11332. cnt (1+ cnt)
  11333. linkfile (format "%s_%04d.png" prefix cnt)
  11334. movefile (format "%s_%04d.png" absprefix cnt)
  11335. link (concat block "[[file:" linkfile "]]" block))
  11336. (if msg (message msg cnt))
  11337. (goto-char beg)
  11338. (unless checkdir ; make sure the directory exists
  11339. (setq checkdir t)
  11340. (or (file-directory-p todir) (make-directory todir)))
  11341. (org-create-formula-image
  11342. txt movefile opt forbuffer)
  11343. (if overlays
  11344. (progn
  11345. (setq ov (org-make-overlay beg end))
  11346. (if (featurep 'xemacs)
  11347. (progn
  11348. (org-overlay-put ov 'invisible t)
  11349. (org-overlay-put
  11350. ov 'end-glyph
  11351. (make-glyph (vector 'png :file movefile))))
  11352. (org-overlay-put
  11353. ov 'display
  11354. (list 'image :type 'png :file movefile :ascent 'center)))
  11355. (push ov org-latex-fragment-image-overlays)
  11356. (goto-char end))
  11357. (delete-region beg end)
  11358. (insert link))))))))
  11359. ;; This function borrows from Ganesh Swami's latex2png.el
  11360. (defun org-create-formula-image (string tofile options buffer)
  11361. (let* ((tmpdir (if (featurep 'xemacs)
  11362. (temp-directory)
  11363. temporary-file-directory))
  11364. (texfilebase (make-temp-name
  11365. (expand-file-name "orgtex" tmpdir)))
  11366. (texfile (concat texfilebase ".tex"))
  11367. (dvifile (concat texfilebase ".dvi"))
  11368. (pngfile (concat texfilebase ".png"))
  11369. (fnh (if (featurep 'xemacs)
  11370. (font-height (get-face-font 'default))
  11371. (face-attribute 'default :height nil)))
  11372. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11373. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11374. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11375. "Black"))
  11376. (bg (or (plist-get options (if buffer :background :html-background))
  11377. "Transparent")))
  11378. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11379. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11380. (with-temp-file texfile
  11381. (insert org-format-latex-header
  11382. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11383. (let ((dir default-directory))
  11384. (condition-case nil
  11385. (progn
  11386. (cd tmpdir)
  11387. (call-process "latex" nil nil nil texfile))
  11388. (error nil))
  11389. (cd dir))
  11390. (if (not (file-exists-p dvifile))
  11391. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11392. (condition-case nil
  11393. (call-process "dvipng" nil nil nil
  11394. "-E" "-fg" fg "-bg" bg
  11395. "-D" dpi
  11396. ;;"-x" scale "-y" scale
  11397. "-T" "tight"
  11398. "-o" pngfile
  11399. dvifile)
  11400. (error nil))
  11401. (if (not (file-exists-p pngfile))
  11402. (progn (message "Failed to create png file from %s" texfile) nil)
  11403. ;; Use the requested file name and clean up
  11404. (copy-file pngfile tofile 'replace)
  11405. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11406. (delete-file (concat texfilebase e)))
  11407. pngfile))))
  11408. (defun org-dvipng-color (attr)
  11409. "Return an rgb color specification for dvipng."
  11410. (apply 'format "rgb %s %s %s"
  11411. (mapcar 'org-normalize-color
  11412. (color-values (face-attribute 'default attr nil)))))
  11413. (defun org-normalize-color (value)
  11414. "Return string to be used as color value for an RGB component."
  11415. (format "%g" (/ value 65535.0)))
  11416. ;;;; Key bindings
  11417. ;; Make `C-c C-x' a prefix key
  11418. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11419. ;; TAB key with modifiers
  11420. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11421. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11422. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11423. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11424. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11425. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11426. ;; The following line is necessary under Suse GNU/Linux
  11427. (unless (featurep 'xemacs)
  11428. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11429. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11430. (define-key org-mode-map [backtab] 'org-shifttab)
  11431. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11432. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11433. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11434. ;; Cursor keys with modifiers
  11435. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11436. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11437. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11438. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11439. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11440. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11441. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11442. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11443. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11444. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11445. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11446. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11447. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11448. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11449. ;;; Extra keys for tty access.
  11450. ;; We only set them when really needed because otherwise the
  11451. ;; menus don't show the simple keys
  11452. (when (or org-use-extra-keys
  11453. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11454. (not window-system))
  11455. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11456. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11457. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11458. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11459. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11460. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11461. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11462. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11463. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11464. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11465. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11466. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11467. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11468. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11469. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11470. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11471. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11472. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11473. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11474. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11475. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11476. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11477. ;; All the other keys
  11478. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11479. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11480. (if (boundp 'narrow-map)
  11481. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11482. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11483. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11484. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11485. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11486. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11487. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11488. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11489. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11490. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11491. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11492. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11493. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11494. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11495. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11496. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11497. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11498. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11499. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11500. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11501. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11502. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11503. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11504. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11505. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11506. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11507. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11508. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11509. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11510. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11511. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11512. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11513. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11514. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11515. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11516. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11517. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11518. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11519. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11520. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11521. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11522. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11523. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11524. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11525. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11526. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11527. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11528. (org-defkey org-mode-map "\C-m" 'org-return)
  11529. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11530. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11531. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11532. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11533. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11534. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11535. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11536. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11537. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11538. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11539. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11540. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11541. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11542. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11543. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11544. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11545. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11546. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11547. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11548. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11549. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11550. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11551. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11552. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11553. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11554. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11555. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11556. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11557. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11558. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11559. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11560. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11561. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11562. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11563. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11564. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11565. (when (featurep 'xemacs)
  11566. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11567. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11568. (defun org-self-insert-command (N)
  11569. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11570. If the cursor is in a table looking at whitespace, the whitespace is
  11571. overwritten, and the table is not marked as requiring realignment."
  11572. (interactive "p")
  11573. (if (and (org-table-p)
  11574. (progn
  11575. ;; check if we blank the field, and if that triggers align
  11576. (and (featurep 'org-table) org-table-auto-blank-field
  11577. (member last-command
  11578. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11579. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11580. ;; got extra space, this field does not determine column width
  11581. (let (org-table-may-need-update) (org-table-blank-field))
  11582. ;; no extra space, this field may determine column width
  11583. (org-table-blank-field)))
  11584. t)
  11585. (eq N 1)
  11586. (looking-at "[^|\n]* |"))
  11587. (let (org-table-may-need-update)
  11588. (goto-char (1- (match-end 0)))
  11589. (delete-backward-char 1)
  11590. (goto-char (match-beginning 0))
  11591. (self-insert-command N))
  11592. (setq org-table-may-need-update t)
  11593. (self-insert-command N)
  11594. (org-fix-tags-on-the-fly)))
  11595. (defun org-fix-tags-on-the-fly ()
  11596. (when (and (equal (char-after (point-at-bol)) ?*)
  11597. (org-on-heading-p))
  11598. (org-align-tags-here org-tags-column)))
  11599. (defun org-delete-backward-char (N)
  11600. "Like `delete-backward-char', insert whitespace at field end in tables.
  11601. When deleting backwards, in tables this function will insert whitespace in
  11602. front of the next \"|\" separator, to keep the table aligned. The table will
  11603. still be marked for re-alignment if the field did fill the entire column,
  11604. because, in this case the deletion might narrow the column."
  11605. (interactive "p")
  11606. (if (and (org-table-p)
  11607. (eq N 1)
  11608. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11609. (looking-at ".*?|"))
  11610. (let ((pos (point))
  11611. (noalign (looking-at "[^|\n\r]* |"))
  11612. (c org-table-may-need-update))
  11613. (backward-delete-char N)
  11614. (skip-chars-forward "^|")
  11615. (insert " ")
  11616. (goto-char (1- pos))
  11617. ;; noalign: if there were two spaces at the end, this field
  11618. ;; does not determine the width of the column.
  11619. (if noalign (setq org-table-may-need-update c)))
  11620. (backward-delete-char N)
  11621. (org-fix-tags-on-the-fly)))
  11622. (defun org-delete-char (N)
  11623. "Like `delete-char', but insert whitespace at field end in tables.
  11624. When deleting characters, in tables this function will insert whitespace in
  11625. front of the next \"|\" separator, to keep the table aligned. The table will
  11626. still be marked for re-alignment if the field did fill the entire column,
  11627. because, in this case the deletion might narrow the column."
  11628. (interactive "p")
  11629. (if (and (org-table-p)
  11630. (not (bolp))
  11631. (not (= (char-after) ?|))
  11632. (eq N 1))
  11633. (if (looking-at ".*?|")
  11634. (let ((pos (point))
  11635. (noalign (looking-at "[^|\n\r]* |"))
  11636. (c org-table-may-need-update))
  11637. (replace-match (concat
  11638. (substring (match-string 0) 1 -1)
  11639. " |"))
  11640. (goto-char pos)
  11641. ;; noalign: if there were two spaces at the end, this field
  11642. ;; does not determine the width of the column.
  11643. (if noalign (setq org-table-may-need-update c)))
  11644. (delete-char N))
  11645. (delete-char N)
  11646. (org-fix-tags-on-the-fly)))
  11647. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11648. (put 'org-self-insert-command 'delete-selection t)
  11649. (put 'orgtbl-self-insert-command 'delete-selection t)
  11650. (put 'org-delete-char 'delete-selection 'supersede)
  11651. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11652. ;; Make `flyspell-mode' delay after some commands
  11653. (put 'org-self-insert-command 'flyspell-delayed t)
  11654. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11655. (put 'org-delete-char 'flyspell-delayed t)
  11656. (put 'org-delete-backward-char 'flyspell-delayed t)
  11657. ;; Make pabbrev-mode expand after org-mode commands
  11658. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11659. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11660. ;; How to do this: Measure non-white length of current string
  11661. ;; If equal to column width, we should realign.
  11662. (defun org-remap (map &rest commands)
  11663. "In MAP, remap the functions given in COMMANDS.
  11664. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11665. (let (new old)
  11666. (while commands
  11667. (setq old (pop commands) new (pop commands))
  11668. (if (fboundp 'command-remapping)
  11669. (org-defkey map (vector 'remap old) new)
  11670. (substitute-key-definition old new map global-map)))))
  11671. (when (eq org-enable-table-editor 'optimized)
  11672. ;; If the user wants maximum table support, we need to hijack
  11673. ;; some standard editing functions
  11674. (org-remap org-mode-map
  11675. 'self-insert-command 'org-self-insert-command
  11676. 'delete-char 'org-delete-char
  11677. 'delete-backward-char 'org-delete-backward-char)
  11678. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11679. (defun org-shiftcursor-error ()
  11680. "Throw an error because Shift-Cursor command was applied in wrong context."
  11681. (error "This command is active in special context like tables, headlines or timestamps"))
  11682. (defun org-shifttab (&optional arg)
  11683. "Global visibility cycling or move to previous table field.
  11684. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11685. on context.
  11686. See the individual commands for more information."
  11687. (interactive "P")
  11688. (cond
  11689. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11690. ((integerp arg)
  11691. (message "Content view to level: %d" arg)
  11692. (org-content (prefix-numeric-value arg))
  11693. (setq org-cycle-global-status 'overview))
  11694. (t (call-interactively 'org-global-cycle))))
  11695. (defun org-shiftmetaleft ()
  11696. "Promote subtree or delete table column.
  11697. Calls `org-promote-subtree', `org-outdent-item',
  11698. or `org-table-delete-column', depending on context.
  11699. See the individual commands for more information."
  11700. (interactive)
  11701. (cond
  11702. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11703. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11704. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11705. (t (org-shiftcursor-error))))
  11706. (defun org-shiftmetaright ()
  11707. "Demote subtree or insert table column.
  11708. Calls `org-demote-subtree', `org-indent-item',
  11709. or `org-table-insert-column', depending on context.
  11710. See the individual commands for more information."
  11711. (interactive)
  11712. (cond
  11713. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11714. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11715. ((org-at-item-p) (call-interactively 'org-indent-item))
  11716. (t (org-shiftcursor-error))))
  11717. (defun org-shiftmetaup (&optional arg)
  11718. "Move subtree up or kill table row.
  11719. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11720. `org-move-item-up' depending on context. See the individual commands
  11721. for more information."
  11722. (interactive "P")
  11723. (cond
  11724. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11725. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11726. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11727. (t (org-shiftcursor-error))))
  11728. (defun org-shiftmetadown (&optional arg)
  11729. "Move subtree down or insert table row.
  11730. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11731. `org-move-item-down', depending on context. See the individual
  11732. commands for more information."
  11733. (interactive "P")
  11734. (cond
  11735. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11736. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11737. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11738. (t (org-shiftcursor-error))))
  11739. (defun org-metaleft (&optional arg)
  11740. "Promote heading or move table column to left.
  11741. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11742. With no specific context, calls the Emacs default `backward-word'.
  11743. See the individual commands for more information."
  11744. (interactive "P")
  11745. (cond
  11746. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11747. ((or (org-on-heading-p) (org-region-active-p))
  11748. (call-interactively 'org-do-promote))
  11749. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11750. (t (call-interactively 'backward-word))))
  11751. (defun org-metaright (&optional arg)
  11752. "Demote subtree or move table column to right.
  11753. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11754. With no specific context, calls the Emacs default `forward-word'.
  11755. See the individual commands for more information."
  11756. (interactive "P")
  11757. (cond
  11758. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11759. ((or (org-on-heading-p) (org-region-active-p))
  11760. (call-interactively 'org-do-demote))
  11761. ((org-at-item-p) (call-interactively 'org-indent-item))
  11762. (t (call-interactively 'forward-word))))
  11763. (defun org-metaup (&optional arg)
  11764. "Move subtree up or move table row up.
  11765. Calls `org-move-subtree-up' or `org-table-move-row' or
  11766. `org-move-item-up', depending on context. See the individual commands
  11767. for more information."
  11768. (interactive "P")
  11769. (cond
  11770. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11771. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11772. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11773. (t (transpose-lines 1) (beginning-of-line -1))))
  11774. (defun org-metadown (&optional arg)
  11775. "Move subtree down or move table row down.
  11776. Calls `org-move-subtree-down' or `org-table-move-row' or
  11777. `org-move-item-down', depending on context. See the individual
  11778. commands for more information."
  11779. (interactive "P")
  11780. (cond
  11781. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11782. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11783. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11784. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11785. (defun org-shiftup (&optional arg)
  11786. "Increase item in timestamp or increase priority of current headline.
  11787. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11788. depending on context. See the individual commands for more information."
  11789. (interactive "P")
  11790. (cond
  11791. ((org-at-timestamp-p t)
  11792. (call-interactively (if org-edit-timestamp-down-means-later
  11793. 'org-timestamp-down 'org-timestamp-up)))
  11794. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11795. ((org-at-item-p) (call-interactively 'org-previous-item))
  11796. ((org-clocktable-try-shift 'up arg))
  11797. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11798. (defun org-shiftdown (&optional arg)
  11799. "Decrease item in timestamp or decrease priority of current headline.
  11800. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11801. depending on context. See the individual commands for more information."
  11802. (interactive "P")
  11803. (cond
  11804. ((org-at-timestamp-p t)
  11805. (call-interactively (if org-edit-timestamp-down-means-later
  11806. 'org-timestamp-up 'org-timestamp-down)))
  11807. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11808. ((org-clocktable-try-shift 'down arg))
  11809. (t (call-interactively 'org-next-item))))
  11810. (defun org-shiftright (&optional arg)
  11811. "Cycle the thing at point or in the current line, depending on context.
  11812. Depending on context, this does one of the following:
  11813. - switch a timestamp at point one day into the future
  11814. - on a headline, switch to the next TODO keyword.
  11815. - on an item, switch entire list to the next bullet type
  11816. - on a property line, switch to the next allowed value
  11817. - on a clocktable definition line, move time block into the future"
  11818. (interactive "P")
  11819. (cond
  11820. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11821. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11822. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11823. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11824. ((org-clocktable-try-shift 'right arg))
  11825. (t (org-shiftcursor-error))))
  11826. (defun org-shiftleft (&optional arg)
  11827. "Cycle the thing at point or in the current line, depending on context.
  11828. Depending on context, this does one of the following:
  11829. - switch a timestamp at point one day into the past
  11830. - on a headline, switch to the previous TODO keyword.
  11831. - on an item, switch entire list to the previous bullet type
  11832. - on a property line, switch to the previous allowed value
  11833. - on a clocktable definition line, move time block into the past"
  11834. (interactive "P")
  11835. (cond
  11836. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11837. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11838. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11839. ((org-at-property-p)
  11840. (call-interactively 'org-property-previous-allowed-value))
  11841. ((org-clocktable-try-shift 'left arg))
  11842. (t (org-shiftcursor-error))))
  11843. (defun org-shiftcontrolright ()
  11844. "Switch to next TODO set."
  11845. (interactive)
  11846. (cond
  11847. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11848. (t (org-shiftcursor-error))))
  11849. (defun org-shiftcontrolleft ()
  11850. "Switch to previous TODO set."
  11851. (interactive)
  11852. (cond
  11853. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11854. (t (org-shiftcursor-error))))
  11855. (defun org-ctrl-c-ret ()
  11856. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11857. (interactive)
  11858. (cond
  11859. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11860. (t (call-interactively 'org-insert-heading))))
  11861. (defun org-copy-special ()
  11862. "Copy region in table or copy current subtree.
  11863. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11864. See the individual commands for more information."
  11865. (interactive)
  11866. (call-interactively
  11867. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11868. (defun org-cut-special ()
  11869. "Cut region in table or cut current subtree.
  11870. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11871. See the individual commands for more information."
  11872. (interactive)
  11873. (call-interactively
  11874. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11875. (defun org-paste-special (arg)
  11876. "Paste rectangular region into table, or past subtree relative to level.
  11877. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11878. See the individual commands for more information."
  11879. (interactive "P")
  11880. (if (org-at-table-p)
  11881. (org-table-paste-rectangle)
  11882. (org-paste-subtree arg)))
  11883. (defun org-edit-special ()
  11884. "Call a special editor for the stuff at point.
  11885. When at a table, call the formula editor with `org-table-edit-formulas'.
  11886. When at the first line of an src example, call `org-edit-src-code'.
  11887. When in an #+include line, visit the include file. Otherwise call
  11888. `ffap' to visit the file at point."
  11889. (interactive)
  11890. (cond
  11891. ((org-at-table-p)
  11892. (call-interactively 'org-table-edit-formulas))
  11893. ((save-excursion
  11894. (beginning-of-line 1)
  11895. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11896. (find-file (org-trim (match-string 1))))
  11897. ((org-edit-src-code))
  11898. ((org-edit-fixed-width-region))
  11899. (t (call-interactively 'ffap))))
  11900. (defun org-ctrl-c-ctrl-c (&optional arg)
  11901. "Set tags in headline, or update according to changed information at point.
  11902. This command does many different things, depending on context:
  11903. - If the cursor is in a headline, prompt for tags and insert them
  11904. into the current line, aligned to `org-tags-column'. When called
  11905. with prefix arg, realign all tags in the current buffer.
  11906. - If the cursor is in one of the special #+KEYWORD lines, this
  11907. triggers scanning the buffer for these lines and updating the
  11908. information.
  11909. - If the cursor is inside a table, realign the table. This command
  11910. works even if the automatic table editor has been turned off.
  11911. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11912. the entire table.
  11913. - If the cursor is a the beginning of a dynamic block, update it.
  11914. - If the cursor is inside a table created by the table.el package,
  11915. activate that table.
  11916. - If the current buffer is a remember buffer, close note and file
  11917. it. A prefix argument of 1 files to the default location
  11918. without further interaction. A prefix argument of 2 files to
  11919. the currently clocking task.
  11920. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11921. links in this buffer.
  11922. - If the cursor is on a numbered item in a plain list, renumber the
  11923. ordered list.
  11924. - If the cursor is on a checkbox, toggle it."
  11925. (interactive "P")
  11926. (let ((org-enable-table-editor t))
  11927. (cond
  11928. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11929. org-occur-highlights
  11930. org-latex-fragment-image-overlays)
  11931. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11932. (org-remove-occur-highlights)
  11933. (org-remove-latex-fragment-image-overlays)
  11934. (message "Temporary highlights/overlays removed from current buffer"))
  11935. ((and (local-variable-p 'org-finish-function (current-buffer))
  11936. (fboundp org-finish-function))
  11937. (funcall org-finish-function))
  11938. ((org-at-property-p)
  11939. (call-interactively 'org-property-action))
  11940. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11941. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11942. ((org-at-table.el-p)
  11943. (require 'table)
  11944. (beginning-of-line 1)
  11945. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11946. (call-interactively 'table-recognize-table))
  11947. ((org-at-table-p)
  11948. (org-table-maybe-eval-formula)
  11949. (if arg
  11950. (call-interactively 'org-table-recalculate)
  11951. (org-table-maybe-recalculate-line))
  11952. (call-interactively 'org-table-align))
  11953. ((org-at-item-checkbox-p)
  11954. (call-interactively 'org-toggle-checkbox))
  11955. ((org-at-item-p)
  11956. (call-interactively 'org-maybe-renumber-ordered-list))
  11957. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11958. ;; Dynamic block
  11959. (beginning-of-line 1)
  11960. (save-excursion (org-update-dblock)))
  11961. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11962. (cond
  11963. ((equal (match-string 1) "TBLFM")
  11964. ;; Recalculate the table before this line
  11965. (save-excursion
  11966. (beginning-of-line 1)
  11967. (skip-chars-backward " \r\n\t")
  11968. (if (org-at-table-p)
  11969. (org-call-with-arg 'org-table-recalculate t))))
  11970. (t
  11971. ; (org-set-regexps-and-options)
  11972. ; (org-restart-font-lock)
  11973. (let ((org-inhibit-startup t)) (org-mode-restart))
  11974. (message "Local setup has been refreshed"))))
  11975. (t (error "C-c C-c can do nothing useful at this location.")))))
  11976. (defun org-mode-restart ()
  11977. "Restart Org-mode, to scan again for special lines.
  11978. Also updates the keyword regular expressions."
  11979. (interactive)
  11980. (org-mode)
  11981. (message "Org-mode restarted"))
  11982. (defun org-kill-note-or-show-branches ()
  11983. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11984. (interactive)
  11985. (if (not org-finish-function)
  11986. (call-interactively 'show-branches)
  11987. (let ((org-note-abort t))
  11988. (funcall org-finish-function))))
  11989. (defun org-return (&optional indent)
  11990. "Goto next table row or insert a newline.
  11991. Calls `org-table-next-row' or `newline', depending on context.
  11992. See the individual commands for more information."
  11993. (interactive)
  11994. (cond
  11995. ((bobp) (if indent (newline-and-indent) (newline)))
  11996. ((and (org-at-heading-p)
  11997. (looking-at
  11998. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11999. (org-show-entry)
  12000. (end-of-line 1)
  12001. (newline))
  12002. ((org-at-table-p)
  12003. (org-table-justify-field-maybe)
  12004. (call-interactively 'org-table-next-row))
  12005. (t (if indent (newline-and-indent) (newline)))))
  12006. (defun org-return-indent ()
  12007. "Goto next table row or insert a newline and indent.
  12008. Calls `org-table-next-row' or `newline-and-indent', depending on
  12009. context. See the individual commands for more information."
  12010. (interactive)
  12011. (org-return t))
  12012. (defun org-ctrl-c-star ()
  12013. "Compute table, or change heading status of lines.
  12014. Calls `org-table-recalculate' or `org-toggle-region-headings',
  12015. depending on context. This will also turn a plain list item or a normal
  12016. line into a subheading."
  12017. (interactive)
  12018. (cond
  12019. ((org-at-table-p)
  12020. (call-interactively 'org-table-recalculate))
  12021. ((org-region-active-p)
  12022. ;; Convert all lines in region to list items
  12023. (call-interactively 'org-toggle-region-headings))
  12024. ((org-on-heading-p)
  12025. (org-toggle-region-headings (point-at-bol)
  12026. (min (1+ (point-at-eol)) (point-max))))
  12027. ((org-at-item-p)
  12028. ;; Convert to heading
  12029. (let ((level (save-match-data
  12030. (save-excursion
  12031. (condition-case nil
  12032. (progn
  12033. (org-back-to-heading t)
  12034. (funcall outline-level))
  12035. (error 0))))))
  12036. (replace-match
  12037. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12038. (t (org-toggle-region-headings (point-at-bol)
  12039. (min (1+ (point-at-eol)) (point-max))))))
  12040. (defun org-ctrl-c-minus ()
  12041. "Insert separator line in table or modify bullet status of line.
  12042. Also turns a plain line or a region of lines into list items.
  12043. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12044. `org-cycle-list-bullet', depending on context."
  12045. (interactive)
  12046. (cond
  12047. ((org-at-table-p)
  12048. (call-interactively 'org-table-insert-hline))
  12049. ((org-on-heading-p)
  12050. ;; Convert to item
  12051. (save-excursion
  12052. (beginning-of-line 1)
  12053. (if (looking-at "\\*+ ")
  12054. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12055. ((org-region-active-p)
  12056. ;; Convert all lines in region to list items
  12057. (call-interactively 'org-toggle-region-items))
  12058. ((org-in-item-p)
  12059. (call-interactively 'org-cycle-list-bullet))
  12060. (t (org-toggle-region-items (point-at-bol)
  12061. (min (1+ (point-at-eol)) (point-max))))))
  12062. (defun org-toggle-region-items (beg end)
  12063. "Convert all lines in region to list items.
  12064. If the first line is already an item, convert all list items in the region
  12065. to normal lines."
  12066. (interactive "r")
  12067. (let (l2 l)
  12068. (save-excursion
  12069. (goto-char end)
  12070. (setq l2 (org-current-line))
  12071. (goto-char beg)
  12072. (beginning-of-line 1)
  12073. (setq l (1- (org-current-line)))
  12074. (if (org-at-item-p)
  12075. ;; We already have items, de-itemize
  12076. (while (< (setq l (1+ l)) l2)
  12077. (when (org-at-item-p)
  12078. (goto-char (match-beginning 2))
  12079. (delete-region (match-beginning 2) (match-end 2))
  12080. (and (looking-at "[ \t]+") (replace-match "")))
  12081. (beginning-of-line 2))
  12082. (while (< (setq l (1+ l)) l2)
  12083. (unless (org-at-item-p)
  12084. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12085. (replace-match "\\1- \\2")))
  12086. (beginning-of-line 2))))))
  12087. (defun org-toggle-region-headings (beg end)
  12088. "Convert all lines in region to list items.
  12089. If the first line is already an item, convert all list items in the region
  12090. to normal lines."
  12091. (interactive "r")
  12092. (let (l2 l)
  12093. (save-excursion
  12094. (goto-char end)
  12095. (setq l2 (org-current-line))
  12096. (goto-char beg)
  12097. (beginning-of-line 1)
  12098. (setq l (1- (org-current-line)))
  12099. (if (org-on-heading-p)
  12100. ;; We already have headlines, de-star them
  12101. (while (< (setq l (1+ l)) l2)
  12102. (when (org-on-heading-p t)
  12103. (and (looking-at outline-regexp) (replace-match "")))
  12104. (beginning-of-line 2))
  12105. (let* ((stars (save-excursion
  12106. (re-search-backward org-complex-heading-regexp nil t)
  12107. (or (match-string 1) "*")))
  12108. (add-stars (if org-odd-levels-only "**" "*"))
  12109. (rpl (concat stars add-stars " \\2")))
  12110. (while (< (setq l (1+ l)) l2)
  12111. (unless (org-on-heading-p)
  12112. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12113. (replace-match rpl)))
  12114. (beginning-of-line 2)))))))
  12115. (defun org-meta-return (&optional arg)
  12116. "Insert a new heading or wrap a region in a table.
  12117. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12118. See the individual commands for more information."
  12119. (interactive "P")
  12120. (cond
  12121. ((org-at-table-p)
  12122. (call-interactively 'org-table-wrap-region))
  12123. (t (call-interactively 'org-insert-heading))))
  12124. ;;; Menu entries
  12125. ;; Define the Org-mode menus
  12126. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12127. '("Tbl"
  12128. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12129. ["Next Field" org-cycle (org-at-table-p)]
  12130. ["Previous Field" org-shifttab (org-at-table-p)]
  12131. ["Next Row" org-return (org-at-table-p)]
  12132. "--"
  12133. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12134. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12135. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12136. "--"
  12137. ("Column"
  12138. ["Move Column Left" org-metaleft (org-at-table-p)]
  12139. ["Move Column Right" org-metaright (org-at-table-p)]
  12140. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12141. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12142. ("Row"
  12143. ["Move Row Up" org-metaup (org-at-table-p)]
  12144. ["Move Row Down" org-metadown (org-at-table-p)]
  12145. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12146. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12147. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12148. "--"
  12149. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12150. ("Rectangle"
  12151. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12152. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12153. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12154. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12155. "--"
  12156. ("Calculate"
  12157. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12158. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12159. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12160. "--"
  12161. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12162. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12163. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12164. "--"
  12165. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12166. "--"
  12167. ["Sum Column/Rectangle" org-table-sum
  12168. (or (org-at-table-p) (org-region-active-p))]
  12169. ["Which Column?" org-table-current-column (org-at-table-p)])
  12170. ["Debug Formulas"
  12171. org-table-toggle-formula-debugger
  12172. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12173. ["Show Col/Row Numbers"
  12174. org-table-toggle-coordinate-overlays
  12175. :style toggle
  12176. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12177. "--"
  12178. ["Create" org-table-create (and (not (org-at-table-p))
  12179. org-enable-table-editor)]
  12180. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12181. ["Import from File" org-table-import (not (org-at-table-p))]
  12182. ["Export to File" org-table-export (org-at-table-p)]
  12183. "--"
  12184. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12185. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12186. '("Org"
  12187. ("Show/Hide"
  12188. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12189. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12190. ["Sparse Tree..." org-sparse-tree t]
  12191. ["Reveal Context" org-reveal t]
  12192. ["Show All" show-all t]
  12193. "--"
  12194. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12195. "--"
  12196. ["New Heading" org-insert-heading t]
  12197. ("Navigate Headings"
  12198. ["Up" outline-up-heading t]
  12199. ["Next" outline-next-visible-heading t]
  12200. ["Previous" outline-previous-visible-heading t]
  12201. ["Next Same Level" outline-forward-same-level t]
  12202. ["Previous Same Level" outline-backward-same-level t]
  12203. "--"
  12204. ["Jump" org-goto t])
  12205. ("Edit Structure"
  12206. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12207. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12208. "--"
  12209. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12210. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12211. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12212. "--"
  12213. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12214. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12215. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12216. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12217. "--"
  12218. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12219. "--"
  12220. ["Convert to odd levels" org-convert-to-odd-levels t]
  12221. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12222. ("Editing"
  12223. ["Emphasis..." org-emphasize t]
  12224. ["Edit Source Example" org-edit-special t])
  12225. ("Archive"
  12226. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12227. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12228. ; :active t :keys "C-u C-c C-x C-a"]
  12229. ["Sparse trees open ARCHIVE trees"
  12230. (setq org-sparse-tree-open-archived-trees
  12231. (not org-sparse-tree-open-archived-trees))
  12232. :style toggle :selected org-sparse-tree-open-archived-trees]
  12233. ["Cycling opens ARCHIVE trees"
  12234. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12235. :style toggle :selected org-cycle-open-archived-trees]
  12236. "--"
  12237. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12238. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12239. ; ["Check and Move Children" (org-archive-subtree '(4))
  12240. ; :active t :keys "C-u C-c C-x C-s"]
  12241. )
  12242. "--"
  12243. ("TODO Lists"
  12244. ["TODO/DONE/-" org-todo t]
  12245. ("Select keyword"
  12246. ["Next keyword" org-shiftright (org-on-heading-p)]
  12247. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12248. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12249. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12250. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12251. ["Show TODO Tree" org-show-todo-tree t]
  12252. ["Global TODO list" org-todo-list t]
  12253. "--"
  12254. ["Set Priority" org-priority t]
  12255. ["Priority Up" org-shiftup t]
  12256. ["Priority Down" org-shiftdown t])
  12257. ("TAGS and Properties"
  12258. ["Set Tags" org-set-tags-command t]
  12259. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12260. "--"
  12261. ["Set property" org-set-property t]
  12262. ["Column view of properties" org-columns t]
  12263. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12264. ("Dates and Scheduling"
  12265. ["Timestamp" org-time-stamp t]
  12266. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12267. ("Change Date"
  12268. ["1 Day Later" org-shiftright t]
  12269. ["1 Day Earlier" org-shiftleft t]
  12270. ["1 ... Later" org-shiftup t]
  12271. ["1 ... Earlier" org-shiftdown t])
  12272. ["Compute Time Range" org-evaluate-time-range t]
  12273. ["Schedule Item" org-schedule t]
  12274. ["Deadline" org-deadline t]
  12275. "--"
  12276. ["Custom time format" org-toggle-time-stamp-overlays
  12277. :style radio :selected org-display-custom-times]
  12278. "--"
  12279. ["Goto Calendar" org-goto-calendar t]
  12280. ["Date from Calendar" org-date-from-calendar t]
  12281. "--"
  12282. ["Start/restart timer" org-timer-start t]
  12283. ["Insert timer string" org-timer t]
  12284. ["Insert timer item" org-timer-item t])
  12285. ("Logging work"
  12286. ["Clock in" org-clock-in t]
  12287. ["Clock out" org-clock-out t]
  12288. ["Clock cancel" org-clock-cancel t]
  12289. ["Goto running clock" org-clock-goto t]
  12290. ["Display times" org-clock-display t]
  12291. ["Create clock table" org-clock-report t]
  12292. "--"
  12293. ["Record DONE time"
  12294. (progn (setq org-log-done (not org-log-done))
  12295. (message "Switching to %s will %s record a timestamp"
  12296. (car org-done-keywords)
  12297. (if org-log-done "automatically" "not")))
  12298. :style toggle :selected org-log-done])
  12299. "--"
  12300. ["Agenda Command..." org-agenda t]
  12301. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12302. ("File List for Agenda")
  12303. ("Special views current file"
  12304. ["TODO Tree" org-show-todo-tree t]
  12305. ["Check Deadlines" org-check-deadlines t]
  12306. ["Timeline" org-timeline t]
  12307. ["Tags Tree" org-tags-sparse-tree t])
  12308. "--"
  12309. ("Hyperlinks"
  12310. ["Store Link (Global)" org-store-link t]
  12311. ["Insert Link" org-insert-link t]
  12312. ["Follow Link" org-open-at-point t]
  12313. "--"
  12314. ["Next link" org-next-link t]
  12315. ["Previous link" org-previous-link t]
  12316. "--"
  12317. ["Descriptive Links"
  12318. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12319. :style radio
  12320. :selected (member '(org-link) buffer-invisibility-spec)]
  12321. ["Literal Links"
  12322. (progn
  12323. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12324. :style radio
  12325. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12326. "--"
  12327. ["Export/Publish..." org-export t]
  12328. ("LaTeX"
  12329. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12330. :selected org-cdlatex-mode]
  12331. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12332. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12333. ["Modify math symbol" org-cdlatex-math-modify
  12334. (org-inside-LaTeX-fragment-p)]
  12335. ["Export LaTeX fragments as images"
  12336. (if (featurep 'org-exp)
  12337. (setq org-export-with-LaTeX-fragments
  12338. (not org-export-with-LaTeX-fragments))
  12339. (require 'org-exp))
  12340. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12341. org-export-with-LaTeX-fragments)])
  12342. "--"
  12343. ("Documentation"
  12344. ["Show Version" org-version t]
  12345. ["Info Documentation" org-info t])
  12346. ("Customize"
  12347. ["Browse Org Group" org-customize t]
  12348. "--"
  12349. ["Expand This Menu" org-create-customize-menu
  12350. (fboundp 'customize-menu-create)])
  12351. "--"
  12352. ["Refresh setup" org-mode-restart t]
  12353. ))
  12354. (defun org-info (&optional node)
  12355. "Read documentation for Org-mode in the info system.
  12356. With optional NODE, go directly to that node."
  12357. (interactive)
  12358. (info (format "(org)%s" (or node ""))))
  12359. (defun org-install-agenda-files-menu ()
  12360. (let ((bl (buffer-list)))
  12361. (save-excursion
  12362. (while bl
  12363. (set-buffer (pop bl))
  12364. (if (org-mode-p) (setq bl nil)))
  12365. (when (org-mode-p)
  12366. (easy-menu-change
  12367. '("Org") "File List for Agenda"
  12368. (append
  12369. (list
  12370. ["Edit File List" (org-edit-agenda-file-list) t]
  12371. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12372. ["Remove Current File from List" org-remove-file t]
  12373. ["Cycle through agenda files" org-cycle-agenda-files t]
  12374. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12375. "--")
  12376. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12377. ;;;; Documentation
  12378. ;;;###autoload
  12379. (defun org-require-autoloaded-modules ()
  12380. (interactive)
  12381. (mapc 'require
  12382. '(org-agenda org-archive org-clock org-colview
  12383. org-exp org-id org-export-latex org-publish
  12384. org-remember org-table)))
  12385. ;;;###autoload
  12386. (defun org-customize ()
  12387. "Call the customize function with org as argument."
  12388. (interactive)
  12389. (org-load-modules-maybe)
  12390. (org-require-autoloaded-modules)
  12391. (customize-browse 'org))
  12392. (defun org-create-customize-menu ()
  12393. "Create a full customization menu for Org-mode, insert it into the menu."
  12394. (interactive)
  12395. (org-load-modules-maybe)
  12396. (org-require-autoloaded-modules)
  12397. (if (fboundp 'customize-menu-create)
  12398. (progn
  12399. (easy-menu-change
  12400. '("Org") "Customize"
  12401. `(["Browse Org group" org-customize t]
  12402. "--"
  12403. ,(customize-menu-create 'org)
  12404. ["Set" Custom-set t]
  12405. ["Save" Custom-save t]
  12406. ["Reset to Current" Custom-reset-current t]
  12407. ["Reset to Saved" Custom-reset-saved t]
  12408. ["Reset to Standard Settings" Custom-reset-standard t]))
  12409. (message "\"Org\"-menu now contains full customization menu"))
  12410. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12411. ;;;; Miscellaneous stuff
  12412. ;;; Generally useful functions
  12413. (defun org-find-text-property-in-string (prop s)
  12414. "Return the first non-nil value of property PROP in string S."
  12415. (or (get-text-property 0 prop s)
  12416. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12417. prop s)))
  12418. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12419. "Display the given MESSAGE as a warning."
  12420. (if (fboundp 'display-warning)
  12421. (display-warning 'org message
  12422. (if (featurep 'xemacs)
  12423. 'warning
  12424. :warning))
  12425. (let ((buf (get-buffer-create "*Org warnings*")))
  12426. (with-current-buffer buf
  12427. (goto-char (point-max))
  12428. (insert "Warning (Org): " message)
  12429. (unless (bolp)
  12430. (newline)))
  12431. (display-buffer buf)
  12432. (sit-for 0))))
  12433. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12434. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12435. (if (and marker (marker-buffer marker)
  12436. (buffer-live-p (marker-buffer marker)))
  12437. (progn
  12438. (switch-to-buffer (marker-buffer marker))
  12439. (if (or (> marker (point-max)) (< marker (point-min)))
  12440. (widen))
  12441. (goto-char marker))
  12442. (if bookmark
  12443. (bookmark-jump bookmark)
  12444. (error "Cannot find location"))))
  12445. (defun org-quote-csv-field (s)
  12446. "Quote field for inclusion in CSV material."
  12447. (if (string-match "[\",]" s)
  12448. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12449. s))
  12450. (defun org-plist-delete (plist property)
  12451. "Delete PROPERTY from PLIST.
  12452. This is in contrast to merely setting it to 0."
  12453. (let (p)
  12454. (while plist
  12455. (if (not (eq property (car plist)))
  12456. (setq p (plist-put p (car plist) (nth 1 plist))))
  12457. (setq plist (cddr plist)))
  12458. p))
  12459. (defun org-force-self-insert (N)
  12460. "Needed to enforce self-insert under remapping."
  12461. (interactive "p")
  12462. (self-insert-command N))
  12463. (defun org-string-width (s)
  12464. "Compute width of string, ignoring invisible characters.
  12465. This ignores character with invisibility property `org-link', and also
  12466. characters with property `org-cwidth', because these will become invisible
  12467. upon the next fontification round."
  12468. (let (b l)
  12469. (when (or (eq t buffer-invisibility-spec)
  12470. (assq 'org-link buffer-invisibility-spec))
  12471. (while (setq b (text-property-any 0 (length s)
  12472. 'invisible 'org-link s))
  12473. (setq s (concat (substring s 0 b)
  12474. (substring s (or (next-single-property-change
  12475. b 'invisible s) (length s)))))))
  12476. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12477. (setq s (concat (substring s 0 b)
  12478. (substring s (or (next-single-property-change
  12479. b 'org-cwidth s) (length s))))))
  12480. (setq l (string-width s) b -1)
  12481. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12482. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12483. l))
  12484. (defun org-get-indentation (&optional line)
  12485. "Get the indentation of the current line, interpreting tabs.
  12486. When LINE is given, assume it represents a line and compute its indentation."
  12487. (if line
  12488. (if (string-match "^ *" (org-remove-tabs line))
  12489. (match-end 0))
  12490. (save-excursion
  12491. (beginning-of-line 1)
  12492. (skip-chars-forward " \t")
  12493. (current-column))))
  12494. (defun org-remove-tabs (s &optional width)
  12495. "Replace tabulators in S with spaces.
  12496. Assumes that s is a single line, starting in column 0."
  12497. (setq width (or width tab-width))
  12498. (while (string-match "\t" s)
  12499. (setq s (replace-match
  12500. (make-string
  12501. (- (* width (/ (+ (match-beginning 0) width) width))
  12502. (match-beginning 0)) ?\ )
  12503. t t s)))
  12504. s)
  12505. (defun org-fix-indentation (line ind)
  12506. "Fix indentation in LINE.
  12507. IND is a cons cell with target and minimum indentation.
  12508. If the current indentation in LINE is smaller than the minimum,
  12509. leave it alone. If it is larger than ind, set it to the target."
  12510. (let* ((l (org-remove-tabs line))
  12511. (i (org-get-indentation l))
  12512. (i1 (car ind)) (i2 (cdr ind)))
  12513. (if (>= i i2) (setq l (substring line i2)))
  12514. (if (> i1 0)
  12515. (concat (make-string i1 ?\ ) l)
  12516. l)))
  12517. (defun org-base-buffer (buffer)
  12518. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12519. (if (not buffer)
  12520. buffer
  12521. (or (buffer-base-buffer buffer)
  12522. buffer)))
  12523. (defun org-trim (s)
  12524. "Remove whitespace at beginning and end of string."
  12525. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12526. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12527. s)
  12528. (defun org-wrap (string &optional width lines)
  12529. "Wrap string to either a number of lines, or a width in characters.
  12530. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12531. that costs. If there is a word longer than WIDTH, the text is actually
  12532. wrapped to the length of that word.
  12533. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12534. many lines, whatever width that takes.
  12535. The return value is a list of lines, without newlines at the end."
  12536. (let* ((words (org-split-string string "[ \t\n]+"))
  12537. (maxword (apply 'max (mapcar 'org-string-width words)))
  12538. w ll)
  12539. (cond (width
  12540. (org-do-wrap words (max maxword width)))
  12541. (lines
  12542. (setq w maxword)
  12543. (setq ll (org-do-wrap words maxword))
  12544. (if (<= (length ll) lines)
  12545. ll
  12546. (setq ll words)
  12547. (while (> (length ll) lines)
  12548. (setq w (1+ w))
  12549. (setq ll (org-do-wrap words w)))
  12550. ll))
  12551. (t (error "Cannot wrap this")))))
  12552. (defun org-do-wrap (words width)
  12553. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12554. (let (lines line)
  12555. (while words
  12556. (setq line (pop words))
  12557. (while (and words (< (+ (length line) (length (car words))) width))
  12558. (setq line (concat line " " (pop words))))
  12559. (setq lines (push line lines)))
  12560. (nreverse lines)))
  12561. (defun org-split-string (string &optional separators)
  12562. "Splits STRING into substrings at SEPARATORS.
  12563. No empty strings are returned if there are matches at the beginning
  12564. and end of string."
  12565. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12566. (start 0)
  12567. notfirst
  12568. (list nil))
  12569. (while (and (string-match rexp string
  12570. (if (and notfirst
  12571. (= start (match-beginning 0))
  12572. (< start (length string)))
  12573. (1+ start) start))
  12574. (< (match-beginning 0) (length string)))
  12575. (setq notfirst t)
  12576. (or (eq (match-beginning 0) 0)
  12577. (and (eq (match-beginning 0) (match-end 0))
  12578. (eq (match-beginning 0) start))
  12579. (setq list
  12580. (cons (substring string start (match-beginning 0))
  12581. list)))
  12582. (setq start (match-end 0)))
  12583. (or (eq start (length string))
  12584. (setq list
  12585. (cons (substring string start)
  12586. list)))
  12587. (nreverse list)))
  12588. (defun org-context ()
  12589. "Return a list of contexts of the current cursor position.
  12590. If several contexts apply, all are returned.
  12591. Each context entry is a list with a symbol naming the context, and
  12592. two positions indicating start and end of the context. Possible
  12593. contexts are:
  12594. :headline anywhere in a headline
  12595. :headline-stars on the leading stars in a headline
  12596. :todo-keyword on a TODO keyword (including DONE) in a headline
  12597. :tags on the TAGS in a headline
  12598. :priority on the priority cookie in a headline
  12599. :item on the first line of a plain list item
  12600. :item-bullet on the bullet/number of a plain list item
  12601. :checkbox on the checkbox in a plain list item
  12602. :table in an org-mode table
  12603. :table-special on a special filed in a table
  12604. :table-table in a table.el table
  12605. :link on a hyperlink
  12606. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12607. :target on a <<target>>
  12608. :radio-target on a <<<radio-target>>>
  12609. :latex-fragment on a LaTeX fragment
  12610. :latex-preview on a LaTeX fragment with overlayed preview image
  12611. This function expects the position to be visible because it uses font-lock
  12612. faces as a help to recognize the following contexts: :table-special, :link,
  12613. and :keyword."
  12614. (let* ((f (get-text-property (point) 'face))
  12615. (faces (if (listp f) f (list f)))
  12616. (p (point)) clist o)
  12617. ;; First the large context
  12618. (cond
  12619. ((org-on-heading-p t)
  12620. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12621. (when (progn
  12622. (beginning-of-line 1)
  12623. (looking-at org-todo-line-tags-regexp))
  12624. (push (org-point-in-group p 1 :headline-stars) clist)
  12625. (push (org-point-in-group p 2 :todo-keyword) clist)
  12626. (push (org-point-in-group p 4 :tags) clist))
  12627. (goto-char p)
  12628. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12629. (if (looking-at "\\[#[A-Z0-9]\\]")
  12630. (push (org-point-in-group p 0 :priority) clist)))
  12631. ((org-at-item-p)
  12632. (push (org-point-in-group p 2 :item-bullet) clist)
  12633. (push (list :item (point-at-bol)
  12634. (save-excursion (org-end-of-item) (point)))
  12635. clist)
  12636. (and (org-at-item-checkbox-p)
  12637. (push (org-point-in-group p 0 :checkbox) clist)))
  12638. ((org-at-table-p)
  12639. (push (list :table (org-table-begin) (org-table-end)) clist)
  12640. (if (memq 'org-formula faces)
  12641. (push (list :table-special
  12642. (previous-single-property-change p 'face)
  12643. (next-single-property-change p 'face)) clist)))
  12644. ((org-at-table-p 'any)
  12645. (push (list :table-table) clist)))
  12646. (goto-char p)
  12647. ;; Now the small context
  12648. (cond
  12649. ((org-at-timestamp-p)
  12650. (push (org-point-in-group p 0 :timestamp) clist))
  12651. ((memq 'org-link faces)
  12652. (push (list :link
  12653. (previous-single-property-change p 'face)
  12654. (next-single-property-change p 'face)) clist))
  12655. ((memq 'org-special-keyword faces)
  12656. (push (list :keyword
  12657. (previous-single-property-change p 'face)
  12658. (next-single-property-change p 'face)) clist))
  12659. ((org-on-target-p)
  12660. (push (org-point-in-group p 0 :target) clist)
  12661. (goto-char (1- (match-beginning 0)))
  12662. (if (looking-at org-radio-target-regexp)
  12663. (push (org-point-in-group p 0 :radio-target) clist))
  12664. (goto-char p))
  12665. ((setq o (car (delq nil
  12666. (mapcar
  12667. (lambda (x)
  12668. (if (memq x org-latex-fragment-image-overlays) x))
  12669. (org-overlays-at (point))))))
  12670. (push (list :latex-fragment
  12671. (org-overlay-start o) (org-overlay-end o)) clist)
  12672. (push (list :latex-preview
  12673. (org-overlay-start o) (org-overlay-end o)) clist))
  12674. ((org-inside-LaTeX-fragment-p)
  12675. ;; FIXME: positions wrong.
  12676. (push (list :latex-fragment (point) (point)) clist)))
  12677. (setq clist (nreverse (delq nil clist)))
  12678. clist))
  12679. ;; FIXME: Compare with at-regexp-p Do we need both?
  12680. (defun org-in-regexp (re &optional nlines visually)
  12681. "Check if point is inside a match of regexp.
  12682. Normally only the current line is checked, but you can include NLINES extra
  12683. lines both before and after point into the search.
  12684. If VISUALLY is set, require that the cursor is not after the match but
  12685. really on, so that the block visually is on the match."
  12686. (catch 'exit
  12687. (let ((pos (point))
  12688. (eol (point-at-eol (+ 1 (or nlines 0))))
  12689. (inc (if visually 1 0)))
  12690. (save-excursion
  12691. (beginning-of-line (- 1 (or nlines 0)))
  12692. (while (re-search-forward re eol t)
  12693. (if (and (<= (match-beginning 0) pos)
  12694. (>= (+ inc (match-end 0)) pos))
  12695. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12696. (defun org-at-regexp-p (regexp)
  12697. "Is point inside a match of REGEXP in the current line?"
  12698. (catch 'exit
  12699. (save-excursion
  12700. (let ((pos (point)) (end (point-at-eol)))
  12701. (beginning-of-line 1)
  12702. (while (re-search-forward regexp end t)
  12703. (if (and (<= (match-beginning 0) pos)
  12704. (>= (match-end 0) pos))
  12705. (throw 'exit t)))
  12706. nil))))
  12707. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12708. "Call `multi-occur' with buffers for all agenda files."
  12709. (interactive "sOrg-files matching: \np")
  12710. (let* ((files (org-agenda-files))
  12711. (tnames (mapcar 'file-truename files))
  12712. (extra org-agenda-text-search-extra-files)
  12713. f)
  12714. (when (eq (car extra) 'agenda-archives)
  12715. (setq extra (cdr extra))
  12716. (setq files (org-add-archive-files files)))
  12717. (while (setq f (pop extra))
  12718. (unless (member (file-truename f) tnames)
  12719. (add-to-list 'files f 'append)
  12720. (add-to-list 'tnames (file-truename f) 'append)))
  12721. (multi-occur
  12722. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12723. regexp)))
  12724. (if (boundp 'occur-mode-find-occurrence-hook)
  12725. ;; Emacs 23
  12726. (add-hook 'occur-mode-find-occurrence-hook
  12727. (lambda ()
  12728. (when (org-mode-p)
  12729. (org-reveal))))
  12730. ;; Emacs 22
  12731. (defadvice occur-mode-goto-occurrence
  12732. (after org-occur-reveal activate)
  12733. (and (org-mode-p) (org-reveal)))
  12734. (defadvice occur-mode-goto-occurrence-other-window
  12735. (after org-occur-reveal activate)
  12736. (and (org-mode-p) (org-reveal)))
  12737. (defadvice occur-mode-display-occurrence
  12738. (after org-occur-reveal activate)
  12739. (when (org-mode-p)
  12740. (let ((pos (occur-mode-find-occurrence)))
  12741. (with-current-buffer (marker-buffer pos)
  12742. (save-excursion
  12743. (goto-char pos)
  12744. (org-reveal)))))))
  12745. (defun org-uniquify (list)
  12746. "Remove duplicate elements from LIST."
  12747. (let (res)
  12748. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12749. res))
  12750. (defun org-delete-all (elts list)
  12751. "Remove all elements in ELTS from LIST."
  12752. (while elts
  12753. (setq list (delete (pop elts) list)))
  12754. list)
  12755. (defun org-back-over-empty-lines ()
  12756. "Move backwards over whitespace, to the beginning of the first empty line.
  12757. Returns the number of empty lines passed."
  12758. (let ((pos (point)))
  12759. (skip-chars-backward " \t\n\r")
  12760. (beginning-of-line 2)
  12761. (goto-char (min (point) pos))
  12762. (count-lines (point) pos)))
  12763. (defun org-skip-whitespace ()
  12764. (skip-chars-forward " \t\n\r"))
  12765. (defun org-point-in-group (point group &optional context)
  12766. "Check if POINT is in match-group GROUP.
  12767. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12768. match. If the match group does ot exist or point is not inside it,
  12769. return nil."
  12770. (and (match-beginning group)
  12771. (>= point (match-beginning group))
  12772. (<= point (match-end group))
  12773. (if context
  12774. (list context (match-beginning group) (match-end group))
  12775. t)))
  12776. (defun org-switch-to-buffer-other-window (&rest args)
  12777. "Switch to buffer in a second window on the current frame.
  12778. In particular, do not allow pop-up frames."
  12779. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12780. special-display-function)
  12781. (apply 'switch-to-buffer-other-window args)))
  12782. (defun org-combine-plists (&rest plists)
  12783. "Create a single property list from all plists in PLISTS.
  12784. The process starts by copying the first list, and then setting properties
  12785. from the other lists. Settings in the last list are the most significant
  12786. ones and overrule settings in the other lists."
  12787. (let ((rtn (copy-sequence (pop plists)))
  12788. p v ls)
  12789. (while plists
  12790. (setq ls (pop plists))
  12791. (while ls
  12792. (setq p (pop ls) v (pop ls))
  12793. (setq rtn (plist-put rtn p v))))
  12794. rtn))
  12795. (defun org-move-line-down (arg)
  12796. "Move the current line down. With prefix argument, move it past ARG lines."
  12797. (interactive "p")
  12798. (let ((col (current-column))
  12799. beg end pos)
  12800. (beginning-of-line 1) (setq beg (point))
  12801. (beginning-of-line 2) (setq end (point))
  12802. (beginning-of-line (+ 1 arg))
  12803. (setq pos (move-marker (make-marker) (point)))
  12804. (insert (delete-and-extract-region beg end))
  12805. (goto-char pos)
  12806. (org-move-to-column col)))
  12807. (defun org-move-line-up (arg)
  12808. "Move the current line up. With prefix argument, move it past ARG lines."
  12809. (interactive "p")
  12810. (let ((col (current-column))
  12811. beg end pos)
  12812. (beginning-of-line 1) (setq beg (point))
  12813. (beginning-of-line 2) (setq end (point))
  12814. (beginning-of-line (- arg))
  12815. (setq pos (move-marker (make-marker) (point)))
  12816. (insert (delete-and-extract-region beg end))
  12817. (goto-char pos)
  12818. (org-move-to-column col)))
  12819. (defun org-replace-escapes (string table)
  12820. "Replace %-escapes in STRING with values in TABLE.
  12821. TABLE is an association list with keys like \"%a\" and string values.
  12822. The sequences in STRING may contain normal field width and padding information,
  12823. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12824. so values can contain further %-escapes if they are define later in TABLE."
  12825. (let ((case-fold-search nil)
  12826. e re rpl)
  12827. (while (setq e (pop table))
  12828. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12829. (while (string-match re string)
  12830. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12831. (cdr e)))
  12832. (setq string (replace-match rpl t t string))))
  12833. string))
  12834. (defun org-sublist (list start end)
  12835. "Return a section of LIST, from START to END.
  12836. Counting starts at 1."
  12837. (let (rtn (c start))
  12838. (setq list (nthcdr (1- start) list))
  12839. (while (and list (<= c end))
  12840. (push (pop list) rtn)
  12841. (setq c (1+ c)))
  12842. (nreverse rtn)))
  12843. (defun org-find-base-buffer-visiting (file)
  12844. "Like `find-buffer-visiting' but alway return the base buffer and
  12845. not an indirect buffer."
  12846. (let ((buf (find-buffer-visiting file)))
  12847. (if buf
  12848. (or (buffer-base-buffer buf) buf)
  12849. nil)))
  12850. (defun org-image-file-name-regexp ()
  12851. "Return regexp matching the file names of images."
  12852. (if (fboundp 'image-file-name-regexp)
  12853. (image-file-name-regexp)
  12854. (let ((image-file-name-extensions
  12855. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12856. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12857. (concat "\\."
  12858. (regexp-opt (nconc (mapcar 'upcase
  12859. image-file-name-extensions)
  12860. image-file-name-extensions)
  12861. t)
  12862. "\\'"))))
  12863. (defun org-file-image-p (file)
  12864. "Return non-nil if FILE is an image."
  12865. (save-match-data
  12866. (string-match (org-image-file-name-regexp) file)))
  12867. (defun org-get-cursor-date ()
  12868. "Return the date at cursor in as a time.
  12869. This works in the calendar and in the agenda, anywhere else it just
  12870. returns the current time."
  12871. (let (date day defd)
  12872. (cond
  12873. ((eq major-mode 'calendar-mode)
  12874. (setq date (calendar-cursor-to-date)
  12875. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12876. ((eq major-mode 'org-agenda-mode)
  12877. (setq day (get-text-property (point) 'day))
  12878. (if day
  12879. (setq date (calendar-gregorian-from-absolute day)
  12880. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12881. (nth 2 date))))))
  12882. (or defd (current-time))))
  12883. (defvar org-agenda-action-marker (make-marker)
  12884. "Marker pointing to the entry for the next agenda action.")
  12885. (defun org-mark-entry-for-agenda-action ()
  12886. "Mark the current entry as target of an agenda action.
  12887. Agenda actions are actions executed from the agenda with the key `k',
  12888. which make use of the date at the cursor."
  12889. (interactive)
  12890. (move-marker org-agenda-action-marker
  12891. (save-excursion (org-back-to-heading t) (point))
  12892. (current-buffer))
  12893. (message
  12894. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12895. ;;; Paragraph filling stuff.
  12896. ;; We want this to be just right, so use the full arsenal.
  12897. (defun org-indent-line-function ()
  12898. "Indent line like previous, but further if previous was headline or item."
  12899. (interactive)
  12900. (let* ((pos (point))
  12901. (itemp (org-at-item-p))
  12902. column bpos bcol tpos tcol bullet btype bullet-type)
  12903. ;; Find the previous relevant line
  12904. (beginning-of-line 1)
  12905. (cond
  12906. ((looking-at "#") (setq column 0))
  12907. ((looking-at "\\*+ ") (setq column 0))
  12908. (t
  12909. (beginning-of-line 0)
  12910. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12911. (beginning-of-line 0))
  12912. (cond
  12913. ((looking-at "\\*+[ \t]+")
  12914. (if (not org-adapt-indentation)
  12915. (setq column 0)
  12916. (goto-char (match-end 0))
  12917. (setq column (current-column))))
  12918. ((org-in-item-p)
  12919. (org-beginning-of-item)
  12920. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12921. (setq bpos (match-beginning 1) tpos (match-end 0)
  12922. bcol (progn (goto-char bpos) (current-column))
  12923. tcol (progn (goto-char tpos) (current-column))
  12924. bullet (match-string 1)
  12925. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12926. (if (> tcol (+ bcol org-description-max-indent))
  12927. (setq tcol (+ bcol 5)))
  12928. (if (not itemp)
  12929. (setq column tcol)
  12930. (goto-char pos)
  12931. (beginning-of-line 1)
  12932. (if (looking-at "\\S-")
  12933. (progn
  12934. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12935. (setq bullet (match-string 1)
  12936. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12937. (setq column (if (equal btype bullet-type) bcol tcol)))
  12938. (setq column (org-get-indentation)))))
  12939. (t (setq column (org-get-indentation))))))
  12940. (goto-char pos)
  12941. (if (<= (current-column) (current-indentation))
  12942. (org-indent-line-to column)
  12943. (save-excursion (org-indent-line-to column)))
  12944. (setq column (current-column))
  12945. (beginning-of-line 1)
  12946. (if (looking-at
  12947. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12948. (replace-match (concat "\\1" (format org-property-format
  12949. (match-string 2) (match-string 3)))
  12950. t nil))
  12951. (org-move-to-column column)))
  12952. (defun org-set-autofill-regexps ()
  12953. (interactive)
  12954. ;; In the paragraph separator we include headlines, because filling
  12955. ;; text in a line directly attached to a headline would otherwise
  12956. ;; fill the headline as well.
  12957. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12958. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12959. ;; The paragraph starter includes hand-formatted lists.
  12960. (org-set-local 'paragraph-start
  12961. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12962. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12963. ;; But only if the user has not turned off tables or fixed-width regions
  12964. (org-set-local
  12965. 'auto-fill-inhibit-regexp
  12966. (concat "\\*+ \\|#\\+"
  12967. "\\|[ \t]*" org-keyword-time-regexp
  12968. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12969. (concat
  12970. "\\|[ \t]*["
  12971. (if org-enable-table-editor "|" "")
  12972. (if org-enable-fixed-width-editor ":" "")
  12973. "]"))))
  12974. ;; We use our own fill-paragraph function, to make sure that tables
  12975. ;; and fixed-width regions are not wrapped. That function will pass
  12976. ;; through to `fill-paragraph' when appropriate.
  12977. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12978. ; Adaptive filling: To get full control, first make sure that
  12979. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12980. (org-set-local 'adaptive-fill-regexp "\000")
  12981. (org-set-local 'adaptive-fill-function
  12982. 'org-adaptive-fill-function)
  12983. (org-set-local
  12984. 'align-mode-rules-list
  12985. '((org-in-buffer-settings
  12986. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12987. (modes . '(org-mode))))))
  12988. (defun org-fill-paragraph (&optional justify)
  12989. "Re-align a table, pass through to fill-paragraph if no table."
  12990. (let ((table-p (org-at-table-p))
  12991. (table.el-p (org-at-table.el-p)))
  12992. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12993. (save-excursion (goto-char (point-at-bol))
  12994. (looking-at outline-regexp)))
  12995. t) ; skip headlines
  12996. (table.el-p t) ; skip table.el tables
  12997. (table-p (org-table-align) t) ; align org-mode tables
  12998. (t nil)))) ; call paragraph-fill
  12999. ;; For reference, this is the default value of adaptive-fill-regexp
  13000. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13001. (defun org-adaptive-fill-function ()
  13002. "Return a fill prefix for org-mode files.
  13003. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13004. work correctly."
  13005. (cond ((looking-at "#[ \t]+")
  13006. (match-string 0))
  13007. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13008. (save-excursion
  13009. (if (> (match-end 1) (+ (match-beginning 1)
  13010. org-description-max-indent))
  13011. (goto-char (+ (match-beginning 1) 5))
  13012. (goto-char (match-end 0)))
  13013. (make-string (current-column) ?\ )))
  13014. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13015. (save-excursion
  13016. (goto-char (match-end 0))
  13017. (make-string (current-column) ?\ )))
  13018. (t nil)))
  13019. ;;; Other stuff.
  13020. (defun org-toggle-fixed-width-section (arg)
  13021. "Toggle the fixed-width export.
  13022. If there is no active region, the QUOTE keyword at the current headline is
  13023. inserted or removed. When present, it causes the text between this headline
  13024. and the next to be exported as fixed-width text, and unmodified.
  13025. If there is an active region, this command adds or removes a colon as the
  13026. first character of this line. If the first character of a line is a colon,
  13027. this line is also exported in fixed-width font."
  13028. (interactive "P")
  13029. (let* ((cc 0)
  13030. (regionp (org-region-active-p))
  13031. (beg (if regionp (region-beginning) (point)))
  13032. (end (if regionp (region-end)))
  13033. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13034. (case-fold-search nil)
  13035. (re "[ \t]*\\(:\\)")
  13036. off)
  13037. (if regionp
  13038. (save-excursion
  13039. (goto-char beg)
  13040. (setq cc (current-column))
  13041. (beginning-of-line 1)
  13042. (setq off (looking-at re))
  13043. (while (> nlines 0)
  13044. (setq nlines (1- nlines))
  13045. (beginning-of-line 1)
  13046. (cond
  13047. (arg
  13048. (org-move-to-column cc t)
  13049. (insert ":\n")
  13050. (forward-line -1))
  13051. ((and off (looking-at re))
  13052. (replace-match "" t t nil 1))
  13053. ((not off) (org-move-to-column cc t) (insert ":")))
  13054. (forward-line 1)))
  13055. (save-excursion
  13056. (org-back-to-heading)
  13057. (if (looking-at (concat outline-regexp
  13058. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13059. (replace-match "" t t nil 1)
  13060. (if (looking-at outline-regexp)
  13061. (progn
  13062. (goto-char (match-end 0))
  13063. (insert org-quote-string " "))))))))
  13064. ;;;; Functions extending outline functionality
  13065. (defun org-beginning-of-line (&optional arg)
  13066. "Go to the beginning of the current line. If that is invisible, continue
  13067. to a visible line beginning. This makes the function of C-a more intuitive.
  13068. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13069. first attempt, and only move to after the tags when the cursor is already
  13070. beyond the end of the headline."
  13071. (interactive "P")
  13072. (let ((pos (point)) refpos)
  13073. (beginning-of-line 1)
  13074. (if (bobp)
  13075. nil
  13076. (backward-char 1)
  13077. (if (org-invisible-p)
  13078. (while (and (not (bobp)) (org-invisible-p))
  13079. (backward-char 1)
  13080. (beginning-of-line 1))
  13081. (forward-char 1)))
  13082. (when org-special-ctrl-a/e
  13083. (cond
  13084. ((and (looking-at org-complex-heading-regexp)
  13085. (= (char-after (match-end 1)) ?\ ))
  13086. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13087. (point-at-eol)))
  13088. (goto-char
  13089. (if (eq org-special-ctrl-a/e t)
  13090. (cond ((> pos refpos) refpos)
  13091. ((= pos (point)) refpos)
  13092. (t (point)))
  13093. (cond ((> pos (point)) (point))
  13094. ((not (eq last-command this-command)) (point))
  13095. (t refpos)))))
  13096. ((org-at-item-p)
  13097. (goto-char
  13098. (if (eq org-special-ctrl-a/e t)
  13099. (cond ((> pos (match-end 4)) (match-end 4))
  13100. ((= pos (point)) (match-end 4))
  13101. (t (point)))
  13102. (cond ((> pos (point)) (point))
  13103. ((not (eq last-command this-command)) (point))
  13104. (t (match-end 4))))))))
  13105. (org-no-warnings
  13106. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13107. (defun org-end-of-line (&optional arg)
  13108. "Go to the end of the line.
  13109. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13110. first attempt, and only move to after the tags when the cursor is already
  13111. beyond the end of the headline."
  13112. (interactive "P")
  13113. (if (or (not org-special-ctrl-a/e)
  13114. (not (org-on-heading-p)))
  13115. (end-of-line arg)
  13116. (let ((pos (point)))
  13117. (beginning-of-line 1)
  13118. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13119. (if (eq org-special-ctrl-a/e t)
  13120. (if (or (< pos (match-beginning 1))
  13121. (= pos (match-end 0)))
  13122. (goto-char (match-beginning 1))
  13123. (goto-char (match-end 0)))
  13124. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13125. (goto-char (match-end 0))
  13126. (goto-char (match-beginning 1))))
  13127. (end-of-line arg))))
  13128. (org-no-warnings
  13129. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13130. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13131. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13132. (defun org-kill-line (&optional arg)
  13133. "Kill line, to tags or end of line."
  13134. (interactive "P")
  13135. (cond
  13136. ((or (not org-special-ctrl-k)
  13137. (bolp)
  13138. (not (org-on-heading-p)))
  13139. (call-interactively 'kill-line))
  13140. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13141. (kill-region (point) (match-beginning 1))
  13142. (org-set-tags nil t))
  13143. (t (kill-region (point) (point-at-eol)))))
  13144. (define-key org-mode-map "\C-k" 'org-kill-line)
  13145. (defun org-yank (&optional arg)
  13146. "Yank. If the kill is a subtree, treat it specially.
  13147. This command will look at the current kill and check if is a single
  13148. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13149. cursor is at the beginning of a line or after the stars of a currently
  13150. empty headline, then the yank is handled specially. How exactly depends
  13151. on the value of the following variables, both set by default.
  13152. org-yank-folded-subtrees
  13153. When set, the subtree(s) will be folded after insertion, but only
  13154. if doing so would now swallow text after the yanked text.
  13155. org-yank-adjusted-subtrees
  13156. When set, the subtree will be promoted or demoted in order to
  13157. fit into the local outline tree structure, which means that the level
  13158. will be adjusted so that it becomes the smaller one of the two
  13159. *visible* surrounding headings.
  13160. Any prefix to this command will cause `yank' to be called directly with
  13161. no special treatment. In particular, a simple `C-u' prefix will just
  13162. plainly yank the text as it is.
  13163. \[1] Basically, the test checks if the first non-white line is a heading
  13164. and if there are no other headings with fewer stars."
  13165. (interactive "P")
  13166. (setq this-command 'yank)
  13167. (if arg
  13168. (call-interactively 'yank)
  13169. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13170. (and (org-kill-is-subtree-p)
  13171. (or (bolp)
  13172. (and (looking-at "[ \t]*$")
  13173. (string-match
  13174. "\\`\\*+\\'"
  13175. (buffer-substring (point-at-bol) (point)))))))
  13176. swallowp)
  13177. (cond
  13178. ((and subtreep org-yank-folded-subtrees)
  13179. (let ((beg (point))
  13180. end)
  13181. (if (and subtreep org-yank-adjusted-subtrees)
  13182. (org-paste-subtree nil nil 'for-yank)
  13183. (call-interactively 'yank))
  13184. (setq end (point))
  13185. (goto-char beg)
  13186. (when (and (bolp) subtreep
  13187. (not (setq swallowp
  13188. (org-yank-folding-would-swallow-text beg end))))
  13189. (or (looking-at outline-regexp)
  13190. (re-search-forward (concat "^" outline-regexp) end t))
  13191. (while (and (< (point) end) (looking-at outline-regexp))
  13192. (hide-subtree)
  13193. (org-cycle-show-empty-lines 'folded)
  13194. (condition-case nil
  13195. (outline-forward-same-level 1)
  13196. (error (goto-char end)))))
  13197. (when swallowp
  13198. (message
  13199. "Yanked text not folded because that would swallow text"))
  13200. (goto-char end)
  13201. (skip-chars-forward " \t\n\r")
  13202. (beginning-of-line 1)
  13203. (push-mark beg 'nomsg)))
  13204. ((and subtreep org-yank-adjusted-subtrees)
  13205. (let ((beg (point-at-bol)))
  13206. (org-paste-subtree nil nil 'for-yank)
  13207. (push-mark beg 'nomsg)))
  13208. (t
  13209. (call-interactively 'yank))))))
  13210. (defun org-yank-folding-would-swallow-text (beg end)
  13211. "Would hide-subtree at BEG swallow any text after END?"
  13212. (let (level)
  13213. (save-excursion
  13214. (goto-char beg)
  13215. (when (or (looking-at outline-regexp)
  13216. (re-search-forward (concat "^" outline-regexp) end t))
  13217. (setq level (org-outline-level)))
  13218. (goto-char end)
  13219. (skip-chars-forward " \t\r\n\v\f")
  13220. (if (or (eobp)
  13221. (and (bolp) (looking-at org-outline-regexp)
  13222. (<= (org-outline-level) level)))
  13223. nil ; Nothing would be swallowed
  13224. t)))) ; something would swallow
  13225. (define-key org-mode-map "\C-y" 'org-yank)
  13226. (defun org-invisible-p ()
  13227. "Check if point is at a character currently not visible."
  13228. ;; Early versions of noutline don't have `outline-invisible-p'.
  13229. (if (fboundp 'outline-invisible-p)
  13230. (outline-invisible-p)
  13231. (get-char-property (point) 'invisible)))
  13232. (defun org-invisible-p2 ()
  13233. "Check if point is at a character currently not visible."
  13234. (save-excursion
  13235. (if (and (eolp) (not (bobp))) (backward-char 1))
  13236. ;; Early versions of noutline don't have `outline-invisible-p'.
  13237. (if (fboundp 'outline-invisible-p)
  13238. (outline-invisible-p)
  13239. (get-char-property (point) 'invisible))))
  13240. (defun org-back-to-heading (&optional invisible-ok)
  13241. "Call `outline-back-to-heading', but provide a better error message."
  13242. (condition-case nil
  13243. (outline-back-to-heading invisible-ok)
  13244. (error (error "Before first headline at position %d in buffer %s"
  13245. (point) (current-buffer)))))
  13246. (defun org-before-first-heading-p ()
  13247. "Before first heading?"
  13248. (save-excursion
  13249. (null (re-search-backward "^\\*+ " nil t))))
  13250. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13251. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13252. (defun org-at-heading-or-item-p ()
  13253. (or (org-on-heading-p) (org-at-item-p)))
  13254. (defun org-on-target-p ()
  13255. (or (org-in-regexp org-radio-target-regexp)
  13256. (org-in-regexp org-target-regexp)))
  13257. (defun org-up-heading-all (arg)
  13258. "Move to the heading line of which the present line is a subheading.
  13259. This function considers both visible and invisible heading lines.
  13260. With argument, move up ARG levels."
  13261. (if (fboundp 'outline-up-heading-all)
  13262. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13263. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13264. (defun org-up-heading-safe ()
  13265. "Move to the heading line of which the present line is a subheading.
  13266. This version will not throw an error. It will return the level of the
  13267. headline found, or nil if no higher level is found."
  13268. (let (start-level re)
  13269. (org-back-to-heading t)
  13270. (setq start-level (funcall outline-level))
  13271. (if (equal start-level 1)
  13272. nil
  13273. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13274. (if (re-search-backward re nil t)
  13275. (funcall outline-level)))))
  13276. (defun org-first-sibling-p ()
  13277. "Is this heading the first child of its parents?"
  13278. (interactive)
  13279. (let ((re (concat "^" outline-regexp))
  13280. level l)
  13281. (unless (org-at-heading-p t)
  13282. (error "Not at a heading"))
  13283. (setq level (funcall outline-level))
  13284. (save-excursion
  13285. (if (not (re-search-backward re nil t))
  13286. t
  13287. (setq l (funcall outline-level))
  13288. (< l level)))))
  13289. (defun org-goto-sibling (&optional previous)
  13290. "Goto the next sibling, even if it is invisible.
  13291. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13292. when a sibling was found. When none is found, return nil and don't
  13293. move point."
  13294. (let ((fun (if previous 're-search-backward 're-search-forward))
  13295. (pos (point))
  13296. (re (concat "^" outline-regexp))
  13297. level l)
  13298. (when (condition-case nil (org-back-to-heading t) (error nil))
  13299. (setq level (funcall outline-level))
  13300. (catch 'exit
  13301. (or previous (forward-char 1))
  13302. (while (funcall fun re nil t)
  13303. (setq l (funcall outline-level))
  13304. (when (< l level) (goto-char pos) (throw 'exit nil))
  13305. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13306. (goto-char pos)
  13307. nil))))
  13308. (defun org-show-siblings ()
  13309. "Show all siblings of the current headline."
  13310. (save-excursion
  13311. (while (org-goto-sibling) (org-flag-heading nil)))
  13312. (save-excursion
  13313. (while (org-goto-sibling 'previous)
  13314. (org-flag-heading nil))))
  13315. (defun org-show-hidden-entry ()
  13316. "Show an entry where even the heading is hidden."
  13317. (save-excursion
  13318. (org-show-entry)))
  13319. (defun org-flag-heading (flag &optional entry)
  13320. "Flag the current heading. FLAG non-nil means make invisible.
  13321. When ENTRY is non-nil, show the entire entry."
  13322. (save-excursion
  13323. (org-back-to-heading t)
  13324. ;; Check if we should show the entire entry
  13325. (if entry
  13326. (progn
  13327. (org-show-entry)
  13328. (save-excursion
  13329. (and (outline-next-heading)
  13330. (org-flag-heading nil))))
  13331. (outline-flag-region (max (point-min) (1- (point)))
  13332. (save-excursion (outline-end-of-heading) (point))
  13333. flag))))
  13334. (defun org-forward-same-level (arg)
  13335. "Move forward to the ARG'th subheading at same level as this one.
  13336. Stop at the first and last subheadings of a superior heading.
  13337. This is like outline-forward-same-level, but invisible headings are ok."
  13338. (interactive "p")
  13339. (org-back-to-heading t)
  13340. (while (> arg 0)
  13341. (let ((point-to-move-to (save-excursion
  13342. (org-get-next-sibling))))
  13343. (if point-to-move-to
  13344. (progn
  13345. (goto-char point-to-move-to)
  13346. (setq arg (1- arg)))
  13347. (progn
  13348. (setq arg 0)
  13349. (error "No following same-level heading"))))))
  13350. (defun org-get-next-sibling ()
  13351. "Move to next heading of the same level, and return point.
  13352. If there is no such heading, return nil.
  13353. This is like outline-next-sibling, but invisible headings are ok."
  13354. (let ((level (funcall outline-level)))
  13355. (outline-next-heading)
  13356. (while (and (not (eobp)) (> (funcall outline-level) level))
  13357. (outline-next-heading))
  13358. (if (or (eobp) (< (funcall outline-level) level))
  13359. nil
  13360. (point))))
  13361. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13362. ;; This is an exact copy of the original function, but it uses
  13363. ;; `org-back-to-heading', to make it work also in invisible
  13364. ;; trees. And is uses an invisible-OK argument.
  13365. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13366. (org-back-to-heading invisible-OK)
  13367. (let ((first t)
  13368. (level (funcall outline-level)))
  13369. (while (and (not (eobp))
  13370. (or first (> (funcall outline-level) level)))
  13371. (setq first nil)
  13372. (outline-next-heading))
  13373. (unless to-heading
  13374. (if (memq (preceding-char) '(?\n ?\^M))
  13375. (progn
  13376. ;; Go to end of line before heading
  13377. (forward-char -1)
  13378. (if (memq (preceding-char) '(?\n ?\^M))
  13379. ;; leave blank line before heading
  13380. (forward-char -1))))))
  13381. (point))
  13382. (defun org-show-subtree ()
  13383. "Show everything after this heading at deeper levels."
  13384. (outline-flag-region
  13385. (point)
  13386. (save-excursion
  13387. (outline-end-of-subtree) (outline-next-heading) (point))
  13388. nil))
  13389. (defun org-show-entry ()
  13390. "Show the body directly following this heading.
  13391. Show the heading too, if it is currently invisible."
  13392. (interactive)
  13393. (save-excursion
  13394. (condition-case nil
  13395. (progn
  13396. (org-back-to-heading t)
  13397. (outline-flag-region
  13398. (max (point-min) (1- (point)))
  13399. (save-excursion
  13400. (re-search-forward
  13401. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13402. (or (match-beginning 1) (point-max)))
  13403. nil))
  13404. (error nil))))
  13405. (defun org-make-options-regexp (kwds)
  13406. "Make a regular expression for keyword lines."
  13407. (concat
  13408. "^"
  13409. "#?[ \t]*\\+\\("
  13410. (mapconcat 'regexp-quote kwds "\\|")
  13411. "\\):[ \t]*"
  13412. "\\(.+\\)"))
  13413. ;; Make isearch reveal the necessary context
  13414. (defun org-isearch-end ()
  13415. "Reveal context after isearch exits."
  13416. (when isearch-success ; only if search was successful
  13417. (if (featurep 'xemacs)
  13418. ;; Under XEmacs, the hook is run in the correct place,
  13419. ;; we directly show the context.
  13420. (org-show-context 'isearch)
  13421. ;; In Emacs the hook runs *before* restoring the overlays.
  13422. ;; So we have to use a one-time post-command-hook to do this.
  13423. ;; (Emacs 22 has a special variable, see function `org-mode')
  13424. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13425. isearch-mode-end-hook-quit)
  13426. ;; Only when the isearch was not quitted.
  13427. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13428. 'append 'local)))))
  13429. (defun org-isearch-post-command ()
  13430. "Remove self from hook, and show context."
  13431. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13432. (org-show-context 'isearch))
  13433. ;;;; Integration with and fixes for other packages
  13434. ;;; Imenu support
  13435. (defvar org-imenu-markers nil
  13436. "All markers currently used by Imenu.")
  13437. (make-variable-buffer-local 'org-imenu-markers)
  13438. (defun org-imenu-new-marker (&optional pos)
  13439. "Return a new marker for use by Imenu, and remember the marker."
  13440. (let ((m (make-marker)))
  13441. (move-marker m (or pos (point)))
  13442. (push m org-imenu-markers)
  13443. m))
  13444. (defun org-imenu-get-tree ()
  13445. "Produce the index for Imenu."
  13446. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13447. (setq org-imenu-markers nil)
  13448. (let* ((n org-imenu-depth)
  13449. (re (concat "^" outline-regexp))
  13450. (subs (make-vector (1+ n) nil))
  13451. (last-level 0)
  13452. m tree level head)
  13453. (save-excursion
  13454. (save-restriction
  13455. (widen)
  13456. (goto-char (point-max))
  13457. (while (re-search-backward re nil t)
  13458. (setq level (org-reduced-level (funcall outline-level)))
  13459. (when (<= level n)
  13460. (looking-at org-complex-heading-regexp)
  13461. (setq head (org-link-display-format
  13462. (org-match-string-no-properties 4))
  13463. m (org-imenu-new-marker))
  13464. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13465. (if (>= level last-level)
  13466. (push (cons head m) (aref subs level))
  13467. (push (cons head (aref subs (1+ level))) (aref subs level))
  13468. (loop for i from (1+ level) to n do (aset subs i nil)))
  13469. (setq last-level level)))))
  13470. (aref subs 1)))
  13471. (eval-after-load "imenu"
  13472. '(progn
  13473. (add-hook 'imenu-after-jump-hook
  13474. (lambda ()
  13475. (if (eq major-mode 'org-mode)
  13476. (org-show-context 'org-goto))))))
  13477. (defun org-link-display-format (link)
  13478. "Replace a link with either the description, or the link target
  13479. if no description is present"
  13480. (save-match-data
  13481. (if (string-match org-bracket-link-analytic-regexp link)
  13482. (replace-match (or (match-string 5 link)
  13483. (concat (match-string 1 link)
  13484. (match-string 3 link)))
  13485. nil nil link)
  13486. link)))
  13487. ;; Speedbar support
  13488. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13489. "Overlay marking the agenda restriction line in speedbar.")
  13490. (org-overlay-put org-speedbar-restriction-lock-overlay
  13491. 'face 'org-agenda-restriction-lock)
  13492. (org-overlay-put org-speedbar-restriction-lock-overlay
  13493. 'help-echo "Agendas are currently limited to this item.")
  13494. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13495. (defun org-speedbar-set-agenda-restriction ()
  13496. "Restrict future agenda commands to the location at point in speedbar.
  13497. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13498. (interactive)
  13499. (require 'org-agenda)
  13500. (let (p m tp np dir txt w)
  13501. (cond
  13502. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13503. 'org-imenu t))
  13504. (setq m (get-text-property p 'org-imenu-marker))
  13505. (save-excursion
  13506. (save-restriction
  13507. (set-buffer (marker-buffer m))
  13508. (goto-char m)
  13509. (org-agenda-set-restriction-lock 'subtree))))
  13510. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13511. 'speedbar-function 'speedbar-find-file))
  13512. (setq tp (previous-single-property-change
  13513. (1+ p) 'speedbar-function)
  13514. np (next-single-property-change
  13515. tp 'speedbar-function)
  13516. dir (speedbar-line-directory)
  13517. txt (buffer-substring-no-properties (or tp (point-min))
  13518. (or np (point-max))))
  13519. (save-excursion
  13520. (save-restriction
  13521. (set-buffer (find-file-noselect
  13522. (let ((default-directory dir))
  13523. (expand-file-name txt))))
  13524. (unless (org-mode-p)
  13525. (error "Cannot restrict to non-Org-mode file"))
  13526. (org-agenda-set-restriction-lock 'file))))
  13527. (t (error "Don't know how to restrict Org-mode's agenda")))
  13528. (org-move-overlay org-speedbar-restriction-lock-overlay
  13529. (point-at-bol) (point-at-eol))
  13530. (setq current-prefix-arg nil)
  13531. (org-agenda-maybe-redo)))
  13532. (eval-after-load "speedbar"
  13533. '(progn
  13534. (speedbar-add-supported-extension ".org")
  13535. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13536. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13537. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13538. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13539. (add-hook 'speedbar-visiting-tag-hook
  13540. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13541. ;;; Fixes and Hacks for problems with other packages
  13542. ;; Make flyspell not check words in links, to not mess up our keymap
  13543. (defun org-mode-flyspell-verify ()
  13544. "Don't let flyspell put overlays at active buttons."
  13545. (not (get-text-property (point) 'keymap)))
  13546. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13547. (eval-after-load "bookmark"
  13548. '(if (boundp 'bookmark-after-jump-hook)
  13549. ;; We can use the hook
  13550. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13551. ;; Hook not available, use advice
  13552. (defadvice bookmark-jump (after org-make-visible activate)
  13553. "Make the position visible."
  13554. (org-bookmark-jump-unhide))))
  13555. ;; Make sure saveplace show the location if it was hidden
  13556. (eval-after-load "saveplace"
  13557. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13558. "Make the position visible."
  13559. (org-bookmark-jump-unhide)))
  13560. (defun org-bookmark-jump-unhide ()
  13561. "Unhide the current position, to show the bookmark location."
  13562. (and (org-mode-p)
  13563. (or (org-invisible-p)
  13564. (save-excursion (goto-char (max (point-min) (1- (point))))
  13565. (org-invisible-p)))
  13566. (org-show-context 'bookmark-jump)))
  13567. ;; Make session.el ignore our circular variable
  13568. (eval-after-load "session"
  13569. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13570. ;;;; Experimental code
  13571. (defun org-closed-in-range ()
  13572. "Sparse tree of items closed in a certain time range.
  13573. Still experimental, may disappear in the future."
  13574. (interactive)
  13575. ;; Get the time interval from the user.
  13576. (let* ((time1 (time-to-seconds
  13577. (org-read-date nil 'to-time nil "Starting date: ")))
  13578. (time2 (time-to-seconds
  13579. (org-read-date nil 'to-time nil "End date:")))
  13580. ;; callback function
  13581. (callback (lambda ()
  13582. (let ((time
  13583. (time-to-seconds
  13584. (apply 'encode-time
  13585. (org-parse-time-string
  13586. (match-string 1))))))
  13587. ;; check if time in interval
  13588. (and (>= time time1) (<= time time2))))))
  13589. ;; make tree, check each match with the callback
  13590. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13591. ;;;; Finish up
  13592. (provide 'org)
  13593. (run-hooks 'org-load-hook)
  13594. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13595. ;;; org.el ends here