org.el 542 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694
  1. ;;; org.el --- Outline-based notes management and organize
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
  8. ;; Version: 4.33
  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 2, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around a NOTES file that contains
  32. ;; information about projects as plain text. Org-mode is implemented on top
  33. ;; of outline-mode - ideal to keep the content of large files well structured.
  34. ;; It supports ToDo items, deadlines and time stamps, which can be extracted
  35. ;; to create a daily/weekly agenda that also integrates the diary of the Emacs
  36. ;; calendar. Tables are easily created with a built-in table editor. Plain
  37. ;; text URL-like links connect to websites, emails (VM, RMAIL, WANDERLUST),
  38. ;; Usenet messages (Gnus), BBDB entries, and any files related to the
  39. ;; projects. For printing and sharing of notes, an Org-mode file (or a part
  40. ;; of it) can be exported as a structured ASCII file, or as HTML.
  41. ;;
  42. ;; Installation
  43. ;; ------------
  44. ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
  45. ;; only need to copy the following lines to your .emacs file. The last two
  46. ;; lines define *global* keys for the commands `org-store-link' and
  47. ;; `org-agenda' - please choose suitable keys yourself.
  48. ;;
  49. ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  50. ;; (define-key global-map "\C-cl" 'org-store-link)
  51. ;; (define-key global-map "\C-ca" 'org-agenda)
  52. ;;
  53. ;; Furthermore you need to activate font-lock-mode in org-mode buffers.
  54. ;; either of the following two lins will do the trick:
  55. ;;
  56. ;; (global-font-lock-mode 1) ; for all buffers
  57. ;; (add-hook 'org-mode-hook 'turn-on-font-lock) ; org-mode buffers only
  58. ;;
  59. ;; If you have downloaded Org-mode from the Web, you have to take additional
  60. ;; action: Byte-compile org.el and org-publish.el and put them together with
  61. ;; org-install.el on your load path. Then also add to your .emacs file:
  62. ;;
  63. ;; (require 'org-install)
  64. ;;
  65. ;; This setup will put all files with extension ".org" into Org-mode. As
  66. ;; an alternative, make the first line of a file look like this:
  67. ;;
  68. ;; MY PROJECTS -*- mode: org; -*-
  69. ;;
  70. ;; which will select Org-mode for this buffer no matter what the file's
  71. ;; name is.
  72. ;;
  73. ;; Documentation
  74. ;; -------------
  75. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  76. ;; distribution also contains a PDF version of it. At the homepage of
  77. ;; Org-mode, you can read the same text online as HTML. There is also an
  78. ;; excellent reference card made by Philip Rooke. This card can be found
  79. ;; in the etc/ directory of Emacs 22.
  80. ;;
  81. ;; Changes since version 4.10:
  82. ;; ---------------------------
  83. ;; Version 4.33
  84. ;; - New commands to move through plain lists: S-up and S-down.
  85. ;; - Bug fixes and documentation update.
  86. ;;
  87. ;; Version 4.32
  88. ;; - Fast (single-key-per-tag) interface for setting TAGS.
  89. ;; - The list of legal tags can be configured globally and locally.
  90. ;; - Elisp and Info links (thanks to Todd Neal).
  91. ;; - `org-export-publishing-directory' can be an alist, with different
  92. ;; directories for different export types.
  93. ;; - All context-sensitive commands use `call-interactively' to dispatch.
  94. ;; - `org-confirm-shell-links' renamed to `org-confirm-shell-link-function'.
  95. ;; - Bug fixes.
  96. ;;
  97. ;; Version 4.31
  98. ;; - Bug fixes.
  99. ;;
  100. ;; Version 4.30
  101. ;; - Modified installation: Autoloads have been collected in org-install.el.
  102. ;; - Logging (org-log-done) is now a #+STARTUP option.
  103. ;; - Checkboxes in plain list items, following up on Frank Ruell's idea.
  104. ;; - File links inserted with C-c C-l will use relative paths if the linked
  105. ;; file is in the current directory or a subdirectory of it.
  106. ;; - New variable `org-link-file-path-type' to specify preference for
  107. ;; relative and absolute paths.
  108. ;; - New CSS classes for tags, timestamps, timestamp keywords.
  109. ;; - Bug and typo fixes.
  110. ;;
  111. ;; Version 4.29
  112. ;; - Inlining images in HTML export now depends on wheather the link
  113. ;; contains a description or not.
  114. ;; - TODO items can be scheduled from the global TODO list using C-c C-s.
  115. ;; - TODO items already scheduled can be made to disappear from the global
  116. ;; todo list, see `org-agenda-todo-ignore-scheduled'.
  117. ;; - In Tables, formulas may also be Lisp forms.
  118. ;; - Exporting the visible part of an outline with `C-c C-x v' works now
  119. ;; for all available exporters.
  120. ;; - Bug fixes, lots of them :-(
  121. ;;
  122. ;; Version 4.28
  123. ;; - Bug fixes.
  124. ;;
  125. ;; Version 4.27
  126. ;; - HTML exporter generalized to receive external options.
  127. ;; As part of the process, author, email and date have been moved to the
  128. ;; end of the HTML file.
  129. ;; - Support for customizable file search in file links.
  130. ;; - BibTeX database links as first application of the above.
  131. ;; - New option `org-agenda-todo-list-sublevels' to turn off listing TODO
  132. ;; entries that are sublevels of another TODO entry.
  133. ;;
  134. ;; Version 4.26
  135. ;; - Bug fixes.
  136. ;;
  137. ;; Version 4.25
  138. ;; - Revision of the font-lock faces section, with better tty support.
  139. ;; - TODO keywords in Agenda buffer are fontified.
  140. ;; - Export converts links between .org files to links between .html files.
  141. ;; - Better support for bold/italic/underline emphasis.
  142. ;;
  143. ;; Version 4.24
  144. ;; - Bug fixes.
  145. ;;
  146. ;; Version 4.23
  147. ;; - Bug fixes.
  148. ;;
  149. ;; Version 4.22
  150. ;; - Bug fixes.
  151. ;; - In agenda buffer, mouse-1 no longer follows link.
  152. ;; See `org-agenda-mouse-1-follows-link' and `org-mouse-1-follows-link'.
  153. ;;
  154. ;; Version 4.20
  155. ;; - Links use now the [[link][description]] format by default.
  156. ;; When inserting links, the user is prompted for a description.
  157. ;; - If a link has a description, only the description is displayed
  158. ;; the link part is hidden. Use C-c C-l to edit the link part.
  159. ;; - TAGS are now bold, but in the same color as the headline.
  160. ;; - The width of a table column can be limited by using a field "<N>".
  161. ;; - New structure for the customization tree.
  162. ;; - Bug fixes.
  163. ;;
  164. ;; Version 4.13
  165. ;; - The list of agenda files can be maintainted in an external file.
  166. ;; - Bug fixes.
  167. ;;
  168. ;; Version 4.12
  169. ;; - Templates for remember buffer. Note that the remember setup changes.
  170. ;; To set up templates, see `org-remember-templates'.
  171. ;; - The time in new time stamps can be rounded, see new option
  172. ;; `org-time-stamp-rounding-minutes'.
  173. ;; - Bug fixes (there are *always* more bugs).
  174. ;;
  175. ;;; Code:
  176. (eval-when-compile
  177. (require 'cl)
  178. (require 'calendar))
  179. (require 'outline)
  180. (require 'time-date)
  181. (require 'easymenu)
  182. (defvar calc-embedded-close-formula) ; defined by the calc package
  183. (defvar calc-embedded-open-formula) ; defined by the calc package
  184. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  185. ;;; Customization variables
  186. (defvar org-version "4.33"
  187. "The version number of the file org.el.")
  188. (defun org-version ()
  189. (interactive)
  190. (message "Org-mode version %s" org-version))
  191. ;; The following constant is for compatibility with different versions
  192. ;; of outline.el.
  193. (defconst org-noutline-p (featurep 'noutline)
  194. "Are we using the new outline mode?")
  195. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  196. (defconst org-format-transports-properties-p
  197. (let ((x "a"))
  198. (add-text-properties 0 1 '(test t) x)
  199. (get-text-property 0 'test (format "%s" x)))
  200. "Does format transport text properties?")
  201. (defgroup org nil
  202. "Outline-based notes management and organizer."
  203. :tag "Org"
  204. :group 'outlines
  205. :group 'hypermedia
  206. :group 'calendar)
  207. (defgroup org-startup nil
  208. "Options concerning startup of Org-mode."
  209. :tag "Org Startup"
  210. :group 'org)
  211. (defcustom org-startup-folded t
  212. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  213. This can also be configured on a per-file basis by adding one of
  214. the following lines anywhere in the buffer:
  215. #+STARTUP: fold
  216. #+STARTUP: nofold
  217. #+STARTUP: content"
  218. :group 'org-startup
  219. :type '(choice
  220. (const :tag "nofold: show all" nil)
  221. (const :tag "fold: overview" t)
  222. (const :tag "content: all headlines" content)))
  223. (defcustom org-startup-truncated t
  224. "Non-nil means, entering Org-mode will set `truncate-lines'.
  225. This is useful since some lines containing links can be very long and
  226. uninteresting. Also tables look terrible when wrapped."
  227. :group 'org-startup
  228. :type 'boolean)
  229. (defcustom org-startup-align-all-tables nil
  230. "Non-nil means, align all tables when visiting a file.
  231. This is useful when the column width in tables is forced with <N> cookies
  232. in table fields. Such tables will look correct only after the first re-align.
  233. This can also be configured on a per-file basis by adding one of
  234. the following lines anywhere in the buffer:
  235. #+STARTUP: align
  236. #+STARTUP: noalign"
  237. :group 'org-startup
  238. :type 'boolean)
  239. (defcustom org-startup-with-deadline-check nil
  240. "Non-nil means, entering Org-mode will run the deadline check.
  241. This means, if you start editing an org file, you will get an
  242. immediate reminder of any due deadlines.
  243. This can also be configured on a per-file basis by adding one of
  244. the following lines anywhere in the buffer:
  245. #+STARTUP: dlcheck
  246. #+STARTUP: nodlcheck"
  247. :group 'org-startup
  248. :type 'boolean)
  249. (defcustom org-insert-mode-line-in-empty-file nil
  250. "Non-nil means insert the first line setting Org-mode in empty files.
  251. When the function `org-mode' is called interactively in an empty file, this
  252. normally means that the file name does not automatically trigger Org-mode.
  253. To ensure that the file will always be in Org-mode in the future, a
  254. line enforcing Org-mode will be inserted into the buffer, if this option
  255. has been set."
  256. :group 'org-startup
  257. :type 'boolean)
  258. (defcustom org-CUA-compatible nil
  259. "Non-nil means use alternative key bindings for S-<cursor movement>.
  260. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  261. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  262. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  263. alternative bindings. Setting this variable to t will replace the following
  264. keys both in Org-mode and in the Org-agenda buffer.
  265. S-RET -> C-S-RET
  266. S-up -> M-p
  267. S-down -> M-n
  268. S-left -> M--
  269. S-right -> M-+
  270. If you do not like the alternative keys, take a look at the variable
  271. `org-disputed-keys'.
  272. This option is only relevant at load-time of Org-mode. Changing it requires
  273. a restart of Emacs to become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defvar org-disputed-keys
  277. '((S-up [(shift up)] [(meta ?p)])
  278. (S-down [(shift down)] [(meta ?n)])
  279. (S-left [(shift left)] [(meta ?-)])
  280. (S-right [(shift right)] [(meta ?+)])
  281. (S-return [(shift return)] [(control shift return)]))
  282. "Keys for which Org-mode and other modes compete.
  283. This is an alist, cars are symbols for lookup, 1st element is the default key,
  284. second element will be used when `org-CUA-compatible' is t.")
  285. (defun org-key (key)
  286. "Select a key according to `org-CUA-compatible'."
  287. (nth (if org-CUA-compatible 2 1)
  288. (or (assq key org-disputed-keys)
  289. (error "Invalid Key %s in `org-key'" key))))
  290. (defcustom org-ellipsis nil
  291. "The ellipsis to use in the Org-mode outline.
  292. When nil, just use the standard three dots. When a string, use that instead,
  293. and just in Org-mode (which will then use its own display table).
  294. Changing this requires executing `M-x org-mode' in a buffer to become
  295. effective."
  296. :group 'org-startup
  297. :type '(choice (const :tag "Default" nil)
  298. (string :tag "String" :value "...#")))
  299. (defvar org-display-table nil
  300. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  301. (defgroup org-keywords nil
  302. "Keywords in Org-mode."
  303. :tag "Org Keywords"
  304. :group 'org)
  305. (defcustom org-deadline-string "DEADLINE:"
  306. "String to mark deadline entries.
  307. A deadline is this string, followed by a time stamp. Should be a word,
  308. terminated by a colon. You can insert a schedule keyword and
  309. a timestamp with \\[org-deadline].
  310. Changes become only effective after restarting Emacs."
  311. :group 'org-keywords
  312. :type 'string)
  313. (defcustom org-scheduled-string "SCHEDULED:"
  314. "String to mark scheduled TODO entries.
  315. A schedule is this string, followed by a time stamp. Should be a word,
  316. terminated by a colon. You can insert a schedule keyword and
  317. a timestamp with \\[org-schedule].
  318. Changes become only effective after restarting Emacs."
  319. :group 'org-keywords
  320. :type 'string)
  321. (defcustom org-closed-string "CLOSED:"
  322. "String used as the prefix for timestamps logging closing a TODO entry."
  323. :group 'org-keywords
  324. :type 'string)
  325. (defcustom org-comment-string "COMMENT"
  326. "Entries starting with this keyword will never be exported.
  327. An entry can be toggled between COMMENT and normal with
  328. \\[org-toggle-comment].
  329. Changes become only effective after restarting Emacs."
  330. :group 'org-keywords
  331. :type 'string)
  332. (defcustom org-quote-string "QUOTE"
  333. "Entries starting with this keyword will be exported in fixed-width font.
  334. Quoting applies only to the text in the entry following the headline, and does
  335. not extend beyond the next headline, even if that is lower level.
  336. An entry can be toggled between QUOTE and normal with
  337. \\[org-toggle-fixed-width-section]."
  338. :group 'org-keywords
  339. :type 'string)
  340. (defgroup org-structure nil
  341. "Options concerning the general structure of Org-mode files."
  342. :tag "Org Structure"
  343. :group 'org)
  344. (defgroup org-cycle nil
  345. "Options concerning visibility cycling in Org-mode."
  346. :tag "Org Cycle"
  347. :group 'org-structure)
  348. (defcustom org-cycle-global-at-bob t
  349. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  350. This makes it possible to do global cycling without having to use S-TAB or
  351. C-u TAB. For this special case to work, the first line of the buffer
  352. must not be a headline - it may be empty ot some other text. When used in
  353. this way, `org-cycle-hook' is disables temporarily, to make sure the
  354. cursor stays at the beginning of the buffer.
  355. When this option is nil, don't do anything special at the beginning
  356. of the buffer."
  357. :group 'org-cycle
  358. :type 'boolean)
  359. (defcustom org-cycle-emulate-tab t
  360. "Where should `org-cycle' emulate TAB.
  361. nil Never
  362. white Only in completely white lines
  363. t Everywhere except in headlines"
  364. :group 'org-cycle
  365. :type '(choice (const :tag "Never" nil)
  366. (const :tag "Only in completely white lines" white)
  367. (const :tag "Everywhere except in headlines" t)
  368. ))
  369. (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
  370. "Hook that is run after `org-cycle' has changed the buffer visibility.
  371. The function(s) in this hook must accept a single argument which indicates
  372. the new state that was set by the most recent `org-cycle' command. The
  373. argument is a symbol. After a global state change, it can have the values
  374. `overview', `content', or `all'. After a local state change, it can have
  375. the values `folded', `children', or `subtree'."
  376. :group 'org-cycle
  377. :type 'hook)
  378. (defgroup org-edit-structure nil
  379. "Options concerning structure editing in Org-mode."
  380. :tag "Org Edit Structure"
  381. :group 'org-structure)
  382. (defcustom org-odd-levels-only nil
  383. "Non-nil means, skip even levels and only use odd levels for the outline.
  384. This has the effect that two stars are being added/taken away in
  385. promotion/demotion commands. It also influences how levels are
  386. handled by the exporters.
  387. Changing it requires restart of `font-lock-mode' to become effective
  388. for fontification also in regions already fontified.
  389. You may also set this on a per-file basis by adding one of the following
  390. lines to the buffer:
  391. #+STARTUP: odd
  392. #+STARTUP: oddeven"
  393. :group 'org-edit-structure
  394. :group 'org-font-lock
  395. :type 'boolean)
  396. (defcustom org-adapt-indentation t
  397. "Non-nil means, adapt indentation when promoting and demoting.
  398. When this is set and the *entire* text in an entry is indented, the
  399. indentation is increased by one space in a demotion command, and
  400. decreased by one in a promotion command. If any line in the entry
  401. body starts at column 0, indentation is not changed at all."
  402. :group 'org-edit-structure
  403. :type 'boolean)
  404. (defcustom org-insert-heading-hook nil
  405. "Hook being run after inserting a new heading."
  406. :group 'org-edit-structure
  407. :type 'boolean)
  408. (defcustom org-enable-fixed-width-editor t
  409. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  410. This currently only means, they are never auto-wrapped.
  411. When nil, such lines will be treated like ordinary lines.
  412. See also the QUOTE keyword."
  413. :group 'org-edit-structure
  414. :type 'boolean)
  415. (defgroup org-sparse-trees nil
  416. "Options concerning sparse trees in Org-mode."
  417. :tag "Org Sparse Trees"
  418. :group 'org-structure)
  419. (defcustom org-highlight-sparse-tree-matches t
  420. "Non-nil means, highlight all matches that define a sparse tree.
  421. The highlights will automatically disappear the next time the buffer is
  422. changed by an edit command."
  423. :group 'org-sparse-trees
  424. :type 'boolean)
  425. (defcustom org-show-hierarchy-above t
  426. "Non-nil means, show full hierarchy when showing a spot in the tree.
  427. Turning this off makes sparse trees more compact, but also less clear."
  428. :group 'org-sparse-trees
  429. :type 'boolean)
  430. (defcustom org-show-following-heading t
  431. "Non-nil means, show heading following match in `org-occur'.
  432. When doing an `org-occur' it is useful to show the headline which
  433. follows the match, even if they do not match the regexp. This makes it
  434. easier to edit directly inside the sparse tree. However, if you use
  435. `org-occur' mainly as an overview, the following headlines are
  436. unnecessary clutter."
  437. :group 'org-sparse-trees
  438. :type 'boolean)
  439. (defcustom org-occur-hook '(org-first-headline-recenter)
  440. "Hook that is run after `org-occur' has constructed a sparse tree.
  441. This can be used to recenter the window to show as much of the structure
  442. as possible."
  443. :group 'org-sparse-trees
  444. :type 'hook)
  445. (defgroup org-plain-lists nil
  446. "Options concerning plain lists in Org-mode."
  447. :tag "Org Plain lists"
  448. :group 'org-structure)
  449. (defcustom org-cycle-include-plain-lists nil
  450. "Non-nil means, include plain lists into visibility cycling.
  451. This means that during cycling, plain list items will *temporarily* be
  452. interpreted as outline headlines with a level given by 1000+i where i is the
  453. indentation of the bullet. In all other operations, plain list items are
  454. not seen as headlines. For example, you cannot assign a TODO keyword to
  455. such an item."
  456. :group 'org-plain-lists
  457. :type 'boolean)
  458. (defcustom org-plain-list-ordered-item-terminator t
  459. "The character that makes a line with leading number an ordered list item.
  460. Valid values are ?. and ?\). To get both terminators, use t. While
  461. ?. may look nicer, it creates the danger that a line with leading
  462. number may be incorrectly interpreted as an item. ?\) therefore is
  463. the safe choice."
  464. :group 'org-plain-lists
  465. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  466. (const :tag "paren like in \"2)\"" ?\))
  467. (const :tab "both" t)))
  468. (defcustom org-auto-renumber-ordered-lists t
  469. "Non-nil means, automatically renumber ordered plain lists.
  470. Renumbering happens when the sequence have been changed with
  471. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  472. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  473. :group 'org-plain-lists
  474. :type 'boolean)
  475. (defgroup org-archive nil
  476. "Options concerning archiving in Org-mode."
  477. :tag "Org Archive"
  478. :group 'org-structure)
  479. (defcustom org-archive-location "%s_archive::"
  480. "The location where subtrees should be archived.
  481. This string consists of two parts, separated by a double-colon.
  482. The first part is a file name - when omitted, archiving happens in the same
  483. file. %s will be replaced by the current file name (without directory part).
  484. Archiving to a different file is useful to keep archived entries from
  485. contributing to the Org-mode Agenda.
  486. The part after the double colon is a headline. The archived entries will be
  487. filed under that headline. When omitted, the subtrees are simply filed away
  488. at the end of the file, as top-level entries.
  489. Here are a few examples:
  490. \"%s_archive::\"
  491. If the current file is Projects.org, archive in file
  492. Projects.org_archive, as top-level trees. This is the default.
  493. \"::* Archived Tasks\"
  494. Archive in the current file, under the top-level headline
  495. \"* Archived Tasks\".
  496. \"~/org/archive.org::\"
  497. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  498. \"basement::** Finished Tasks\"
  499. Archive in file ./basement (relative path), as level 3 trees
  500. below the level 2 heading \"** Finished Tasks\".
  501. You may set this option on a per-file basis by adding to the buffer a
  502. line like
  503. #+ARCHIVE: basement::** Finished Tasks"
  504. :group 'org-archive
  505. :type 'string)
  506. (defcustom org-archive-mark-done t
  507. "Non-nil means, mark archived entries as DONE."
  508. :group 'org-archive
  509. :type 'boolean)
  510. (defcustom org-archive-stamp-time t
  511. "Non-nil means, add a time stamp to archived entries.
  512. The time stamp will be added directly after the TODO state keyword in the
  513. first line, so it is probably best to use this in combinations with
  514. `org-archive-mark-done'."
  515. :group 'org-archive
  516. :type 'boolean)
  517. (defgroup org-table nil
  518. "Options concerning tables in Org-mode."
  519. :tag "Org Table"
  520. :group 'org)
  521. (defcustom org-enable-table-editor 'optimized
  522. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  523. When nil, such lines will be treated like ordinary lines.
  524. When equal to the symbol `optimized', the table editor will be optimized to
  525. do the following:
  526. - Use automatic overwrite mode in front of whitespace in table fields.
  527. This make the structure of the table stay in tact as long as the edited
  528. field does not exceed the column width.
  529. - Minimize the number of realigns. Normally, the table is aligned each time
  530. TAB or RET are pressed to move to another field. With optimization this
  531. happens only if changes to a field might have changed the column width.
  532. Optimization requires replacing the functions `self-insert-command',
  533. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  534. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  535. very good at guessing when a re-align will be necessary, but you can always
  536. force one with \\[org-ctrl-c-ctrl-c].
  537. If you would like to use the optimized version in Org-mode, but the
  538. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  539. This variable can be used to turn on and off the table editor during a session,
  540. but in order to toggle optimization, a restart is required.
  541. See also the variable `org-table-auto-blank-field'."
  542. :group 'org-table
  543. :type '(choice
  544. (const :tag "off" nil)
  545. (const :tag "on" t)
  546. (const :tag "on, optimized" optimized)))
  547. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  548. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  549. In the optimized version, the table editor takes over all simple keys that
  550. normally just insert a character. In tables, the characters are inserted
  551. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  552. for empty fields). Outside tables, the correct binding of the keys is
  553. restored.
  554. The default for this option is t if the optimized version is also used in
  555. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  556. this variable requires a restart of Emacs to become effective."
  557. :group 'org-table
  558. :type 'boolean)
  559. (defgroup org-table-settings nil
  560. "Settings for tables in Org-mode."
  561. :tag "Org Table Settings"
  562. :group 'org-table)
  563. (defcustom org-table-default-size "5x2"
  564. "The default size for newly created tables, Columns x Rows."
  565. :group 'org-table-settings
  566. :type 'string)
  567. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  568. "Regular expression for recognizing numbers in table columns.
  569. If a table column contains mostly numbers, it will be aligned to the
  570. right. If not, it will be aligned to the left.
  571. The default value of this option is a regular expression which allows
  572. anything which looks remotely like a number as used in scientific
  573. context. For example, all of the following will be considered a
  574. number:
  575. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  576. Other options offered by the customize interface are more restrictive."
  577. :group 'org-table-settings
  578. :type '(choice
  579. (const :tag "Positive Integers"
  580. "^[0-9]+$")
  581. (const :tag "Integers"
  582. "^[-+]?[0-9]+$")
  583. (const :tag "Floating Point Numbers"
  584. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  585. (const :tag "Floating Point Number or Integer"
  586. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  587. (const :tag "Exponential, Floating point, Integer"
  588. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  589. (const :tag "Very General Number-Like"
  590. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  591. (string :tag "Regexp:")))
  592. (defcustom org-table-number-fraction 0.5
  593. "Fraction of numbers in a column required to make the column align right.
  594. In a column all non-white fields are considered. If at least this
  595. fraction of fields is matched by `org-table-number-fraction',
  596. alignment to the right border applies."
  597. :group 'org-table-settings
  598. :type 'number)
  599. (defgroup org-table-editing nil
  600. "Bahavior of tables during editing in Org-mode."
  601. :tag "Org Table Editing"
  602. :group 'org-table)
  603. (defcustom org-table-automatic-realign t
  604. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  605. When nil, aligning is only done with \\[org-table-align], or after column
  606. removal/insertion."
  607. :group 'org-table-editing
  608. :type 'boolean)
  609. (defcustom org-table-limit-column-width t ;kw
  610. "Non-nil means, allow to limit the width of table columns with <N> fields."
  611. :group 'org-table-editing
  612. :type 'boolean)
  613. (defcustom org-table-auto-blank-field t
  614. "Non-nil means, automatically blank table field when starting to type into it.
  615. This only happens when typing immediately after a field motion
  616. command (TAB, S-TAB or RET).
  617. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  618. :group 'org-table-editing
  619. :type 'boolean)
  620. (defcustom org-table-tab-jumps-over-hlines t
  621. "Non-nil means, tab in the last column of a table with jump over a hline.
  622. If a horizontal separator line is following the current line,
  623. `org-table-next-field' can either create a new row before that line, or jump
  624. over the line. When this option is nil, a new line will be created before
  625. this line."
  626. :group 'org-table-editing
  627. :type 'boolean)
  628. (defcustom org-table-tab-recognizes-table.el t
  629. "Non-nil means, TAB will automatically notice a table.el table.
  630. When it sees such a table, it moves point into it and - if necessary -
  631. calls `table-recognize-table'."
  632. :group 'org-table-editing
  633. :type 'boolean)
  634. (defgroup org-table-calculation nil
  635. "Options concerning tables in Org-mode."
  636. :tag "Org Table Calculation"
  637. :group 'org-table)
  638. (defcustom org-table-copy-increment t
  639. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  640. :group 'org-table-calculation
  641. :type 'boolean)
  642. (defcustom org-calc-default-modes
  643. '(calc-internal-prec 12
  644. calc-float-format (float 5)
  645. calc-angle-mode deg
  646. calc-prefer-frac nil
  647. calc-symbolic-mode nil
  648. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  649. calc-display-working-message t
  650. )
  651. "List with Calc mode settings for use in calc-eval for table formulas.
  652. The list must contain alternating symbols (Calc modes variables and values).
  653. Don't remove any of the default settings, just change the values. Org-mode
  654. relies on the variables to be present in the list."
  655. :group 'org-table-calculation
  656. :type 'plist)
  657. (defcustom org-table-formula-evaluate-inline t
  658. "Non-nil means, TAB and RET evaluate a formula in current table field.
  659. If the current field starts with an equal sign, it is assumed to be a formula
  660. which should be evaluated as described in the manual and in the documentation
  661. string of the command `org-table-eval-formula'. This feature requires the
  662. Emacs calc package.
  663. When this variable is nil, formula calculation is only available through
  664. the command \\[org-table-eval-formula]."
  665. :group 'org-table-calculation
  666. :type 'boolean)
  667. (defcustom org-table-formula-use-constants t
  668. "Non-nil means, interpret constants in formulas in tables.
  669. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  670. by the value given in `org-table-formula-constants', or by a value obtained
  671. from the `constants.el' package."
  672. :group 'org-table-calculation
  673. :type 'boolean)
  674. (defcustom org-table-formula-constants nil
  675. "Alist with constant names and values, for use in table formulas.
  676. The car of each element is a name of a constant, without the `$' before it.
  677. The cdr is the value as a string. For example, if you'd like to use the
  678. speed of light in a formula, you would configure
  679. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  680. and then use it in an equation like `$1*$c'."
  681. :group 'org-table-calculation
  682. :type '(repeat
  683. (cons (string :tag "name")
  684. (string :tag "value"))))
  685. (defcustom org-table-formula-numbers-only nil
  686. "Non-nil means, calculate only with numbers in table formulas.
  687. Then all input fields will be converted to a number, and the result
  688. must also be a number. When nil, calc's full potential is available
  689. in table calculations, including symbolics etc."
  690. :group 'org-table-calculation
  691. :type 'boolean)
  692. (defcustom org-table-allow-automatic-line-recalculation t
  693. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  694. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  695. :group 'org-table-calculation
  696. :type 'boolean)
  697. (defgroup org-link nil
  698. "Options concerning links in Org-mode."
  699. :tag "Org Link"
  700. :group 'org)
  701. (defcustom org-descriptive-links t
  702. "Non-nil means, hide link part and only show description of bracket links.
  703. Bracket links are like [[link][descritpion]]. This variable sets the initial
  704. state in new org-mode buffers. The setting can then be toggled on a
  705. per-buffer basis from the Org->Hyperlinks menu."
  706. :group 'org-link
  707. :type 'boolean)
  708. (defcustom org-link-style 'bracket
  709. "The style of links to be inserted with \\[org-insert-link].
  710. Possible values are:
  711. bracket [[link][description]]. This is recommended
  712. plain Description \\n link. The old way, no longer recommended."
  713. :group 'org-link
  714. :type '(choice
  715. (const :tag "Bracket (recommended)" bracket)
  716. (const :tag "Plain (no longer recommended)" plain)))
  717. (defcustom org-link-format "%s"
  718. "Default format for external, URL-like linkes in the buffer.
  719. This is a format string for printf, %s will be replaced by the link text.
  720. The recommended value is just \"%s\", since links will be protected by
  721. enclosing them in double brackets. If you prefer plain links (see variable
  722. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  723. additional URL: prefix, so the format would be \"<URL:%s>\"."
  724. :group 'org-link
  725. :type '(choice
  726. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  727. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  728. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  729. (string :tag "Other" :value "<%s>")))
  730. (defcustom org-link-file-path-type 'adaptive
  731. "How the path name in file links should be stored.
  732. Valid values are:
  733. relative relative to the current directory, i.e. the directory of the file
  734. into which the link is being inserted.
  735. absolute absolute path, if possible with ~ for home directory.
  736. noabbrev absolute path, no abbreviation of home directory.
  737. adaptive Use relative path for files in the current directory and sub-
  738. directories of it. For other files, use an absolute path."
  739. :group 'org-link
  740. :type '(choice
  741. (const relative)
  742. (const absolute)
  743. (const noabbrev)
  744. (const adaptive)))
  745. (defcustom org-activate-links '(bracket angle plain radio tag date)
  746. "Types of links that should be activated in Org-mode files.
  747. This is a list of symbols, each leading to the activation of a certain link
  748. type. In principle, it does not hurt to turn on most link types - there may
  749. be a small gain when turning off unused link types. The types are:
  750. bracket The recommended [[link][description]] or [[link]] links with hiding.
  751. angular Links in angular brackes that may contain whitespace like
  752. <bbdb:Carsten Dominik>.
  753. plain Plain links in normal text, no whitespace, like http://google.com.
  754. radio Text that is matched by a radio target, see manual for details.
  755. tag Tag settings in a headline (link to tag search).
  756. date Time stamps (link to calendar).
  757. camel CamelCase words defining text searches.
  758. Changing this variable requires a restart of Emacs to become effective."
  759. :group 'org-link
  760. :type '(set (const :tag "Double bracket links (new style)" bracket)
  761. (const :tag "Angular bracket links (old style)" angular)
  762. (const :tag "plain text links" plain)
  763. (const :tag "Radio target matches" radio)
  764. (const :tag "Tags" tag)
  765. (const :tag "Timestamps" date)
  766. (const :tag "CamelCase words" camel)))
  767. (defgroup org-link-store nil
  768. "Options concerning storing links in Org-mode"
  769. :tag "Org Store Link"
  770. :group 'org-link)
  771. (defcustom org-context-in-file-links t
  772. "Non-nil means, file links from `org-store-link' contain context.
  773. A search string will be added to the file name with :: as separator and
  774. used to find the context when the link is activated by the command
  775. `org-open-at-point'.
  776. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  777. negates this setting for the duration of the command."
  778. :group 'org-link-store
  779. :type 'boolean)
  780. (defcustom org-file-link-context-use-camel-case nil
  781. "Non-nil means, use CamelCase to store a search context in a file link.
  782. When nil, the search string simply consists of the words of the string.
  783. CamelCase is deprecated, and support for it may be dropped in the future."
  784. :group 'org-link-store
  785. :type 'boolean)
  786. (defcustom org-keep-stored-link-after-insertion nil
  787. "Non-nil means, keep link in list for entire session.
  788. The command `org-store-link' adds a link pointing to the current
  789. location to an internal list. These links accumulate during a session.
  790. The command `org-insert-link' can be used to insert links into any
  791. Org-mode file (offering completion for all stored links). When this
  792. option is nil, every link which has been inserted once using \\[org-insert-link]
  793. will be removed from the list, to make completing the unused links
  794. more efficient."
  795. :group 'org-link-store
  796. :type 'boolean)
  797. (defcustom org-usenet-links-prefer-google nil
  798. "Non-nil means, `org-store-link' will create web links to Google groups.
  799. When nil, Gnus will be used for such links.
  800. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  801. negates this setting for the duration of the command."
  802. :group 'org-link-store
  803. :type 'boolean)
  804. (defgroup org-link-follow nil
  805. "Options concerning following links in Org-mode"
  806. :tag "Org Follow Link"
  807. :group 'org-link)
  808. (defcustom org-tab-follows-link nil
  809. "Non-nil means, on links TAB will follow the link.
  810. Needs to be set before org.el is loaded."
  811. :group 'org-link-follow
  812. :type 'boolean)
  813. (defcustom org-return-follows-link nil
  814. "Non-nil means, on links RET will follow the link.
  815. Needs to be set before org.el is loaded."
  816. :group 'org-link-follow
  817. :type 'boolean)
  818. (defcustom org-mouse-1-follows-link t
  819. "Non-nil means, mouse-1 on a link will follow the link.
  820. A longer mouse click will still set point. Does not wortk on XEmacs.
  821. Needs to be set before org.el is loaded."
  822. :group 'org-link-follow
  823. :type 'boolean)
  824. (defcustom org-mark-ring-length 4
  825. "Number of different positions to be recorded in the ring
  826. Changing this requires a restart of Emacs to work correctly."
  827. :group 'org-link-follow
  828. :type 'interger)
  829. (defcustom org-link-frame-setup
  830. '((vm . vm-visit-folder-other-frame)
  831. (gnus . gnus-other-frame)
  832. (file . find-file-other-window))
  833. "Setup the frame configuration for following links.
  834. When following a link with Emacs, it may often be useful to display
  835. this link in another window or frame. This variable can be used to
  836. set this up for the different types of links.
  837. For VM, use any of
  838. `vm-visit-folder'
  839. `vm-visit-folder-other-frame'
  840. For Gnus, use any of
  841. `gnus'
  842. `gnus-other-frame'
  843. For FILE, use any of
  844. `find-file'
  845. `find-file-other-window'
  846. `find-file-other-frame'
  847. For the calendar, use the variable `calendar-setup'.
  848. For BBDB, it is currently only possible to display the matches in
  849. another window."
  850. :group 'org-link-follow
  851. :type '(list
  852. (cons (const vm)
  853. (choice
  854. (const vm-visit-folder)
  855. (const vm-visit-folder-other-window)
  856. (const vm-visit-folder-other-frame)))
  857. (cons (const gnus)
  858. (choice
  859. (const gnus)
  860. (const gnus-other-frame)))
  861. (cons (const file)
  862. (choice
  863. (const find-file)
  864. (const find-file-other-window)
  865. (const find-file-other-frame)))))
  866. (defcustom org-open-non-existing-files nil
  867. "Non-nil means, `org-open-file' will open non-existing file.
  868. When nil, an error will be generated."
  869. :group 'org-link-follow
  870. :type 'boolean)
  871. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  872. "Non-nil means, ask for confirmation before executing shell links.
  873. Shell links can be dangerous, just thing about a link
  874. [[shell:rm -rf ~/*][Google Search]]
  875. This link would show up in your Org-mode document as \"Google Search\"
  876. but really it would remove your entire home directory.
  877. Therefore I *definitely* advise against setting this variable to nil.
  878. Just change it to `y-or-n-p' of you want to confirm with a single key press
  879. rather than having to type \"yes\"."
  880. :group 'org-link-follow
  881. :type '(choice
  882. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  883. (const :tag "with y-or-n (faster)" y-or-n-p)
  884. (const :tag "no confirmation (dangerous)" nil)))
  885. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  886. "Non-nil means, ask for confirmation before executing elisp links.
  887. Elisp links can be dangerous, just thing about a link
  888. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  889. This link would show up in your Org-mode document as \"Google Search\"
  890. but really it would remove your entire home directory.
  891. Therefore I *definitely* advise against setting this variable to nil.
  892. Just change it to `y-or-n-p' of you want to confirm with a single key press
  893. rather than having to type \"yes\"."
  894. :group 'org-link-follow
  895. :type '(choice
  896. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  897. (const :tag "with y-or-n (faster)" y-or-n-p)
  898. (const :tag "no confirmation (dangerous)" nil)))
  899. (defconst org-file-apps-defaults-gnu
  900. '((t . mailcap))
  901. "Default file applications on a UNIX or GNU/Linux system.
  902. See `org-file-apps'.")
  903. (defconst org-file-apps-defaults-macosx
  904. '((t . "open %s")
  905. ("ps" . "gv %s")
  906. ("ps.gz" . "gv %s")
  907. ("eps" . "gv %s")
  908. ("eps.gz" . "gv %s")
  909. ("dvi" . "xdvi %s")
  910. ("fig" . "xfig %s"))
  911. "Default file applications on a MacOS X system.
  912. The system \"open\" is known as a default, but we use X11 applications
  913. for some files for which the OS does not have a good default.
  914. See `org-file-apps'.")
  915. (defconst org-file-apps-defaults-windowsnt
  916. (list (cons t
  917. (list (if (featurep 'xemacs)
  918. 'mswindows-shell-execute
  919. 'w32-shell-execute)
  920. "open" 'file)))
  921. "Default file applications on a Windows NT system.
  922. The system \"open\" is used for most files.
  923. See `org-file-apps'.")
  924. (defcustom org-file-apps
  925. '(
  926. ("txt" . emacs)
  927. ("tex" . emacs)
  928. ("ltx" . emacs)
  929. ("org" . emacs)
  930. ("el" . emacs)
  931. ("bib" . emacs)
  932. )
  933. "External applications for opening `file:path' items in a document.
  934. Org-mode uses system defaults for different file types, but
  935. you can use this variable to set the application for a given file
  936. extension. The entries in this list are cons cells where the car identifies
  937. files and the cdr the corresponding command. Possible values for the
  938. file identifier are
  939. \"ext\" A string identifying an extension
  940. `directory' Matches a directory
  941. t Default for all remaining files
  942. Possible values for the command are:
  943. `emacs' The file will be visited by the current Emacs process.
  944. `default' Use the default application for this file type.
  945. string A command to be executed by a shell; %s will be replaced
  946. by the path to the file.
  947. sexp A Lisp form which will be evaluated. The file path will
  948. be available in the Lisp variable `file'.
  949. For more examples, see the system specific constants
  950. `org-file-apps-defaults-macosx'
  951. `org-file-apps-defaults-windowsnt'
  952. `org-file-apps-defaults-gnu'."
  953. :group 'org-link-follow
  954. :type '(repeat
  955. (cons (choice :value ""
  956. (string :tag "Extension")
  957. (const :tag "Default for unrecognized files" t)
  958. (const :tag "Links to a directory" directory))
  959. (choice :value ""
  960. (const :tag "Visit with Emacs" emacs)
  961. (const :tag "Use system default" default)
  962. (string :tag "Command")
  963. (sexp :tag "Lisp form")))))
  964. (defcustom org-mhe-search-all-folders nil
  965. "Non-nil means, that the search for the mh-message will be extended to
  966. all folders if the message cannot be found in the folder given in the link.
  967. Searching all folders is very effective with one of the search engines
  968. supported by MH-E, but will be slow with pick."
  969. :group 'org-link-follow
  970. :type 'boolean)
  971. (defgroup org-remember nil
  972. "Options concerning interaction with remember.el."
  973. :tag "Org Remember"
  974. :group 'org)
  975. (defcustom org-directory "~/org"
  976. "Directory with org files.
  977. This directory will be used as default to prompt for org files.
  978. Used by the hooks for remember.el."
  979. :group 'org-remember
  980. :type 'directory)
  981. (defcustom org-default-notes-file "~/.notes"
  982. "Default target for storing notes.
  983. Used by the hooks for remember.el. This can be a string, or nil to mean
  984. the value of `remember-data-file'."
  985. :group 'org-remember
  986. :type '(choice
  987. (const :tag "Default from remember-data-file" nil)
  988. file))
  989. (defcustom org-remember-templates nil
  990. "Templates for the creation of remember buffers.
  991. When nil, just let remember make the buffer.
  992. When not nil, this is a list of 3-element lists. In each entry, the first
  993. element is a character, a unique key to select this template.
  994. The second element is the template. The third element is optional and can
  995. specify a destination file for remember items created with this template.
  996. The default file is given by `org-default-notes-file'.
  997. The template specifies the structure of the remember buffer. It should have
  998. a first line starting with a star, to act as the org-mode headline.
  999. Furthermore, the following %-escapes will be replaced with content:
  1000. %t time stamp, date only
  1001. %T time stamp with date and time
  1002. %u inactive time stamp, date only
  1003. %U inactive time stamp with date and time
  1004. %n user name
  1005. %a annotation, normally the link created with org-store-link
  1006. %i initial content, the region when remember is called with C-u.
  1007. If %i is indented, the entire inserted text will be indented as well.
  1008. %? This will be removed, and the cursor placed at this position."
  1009. :group 'org-remember
  1010. :type '(repeat :tag "enabled"
  1011. (list :value (?a "\n" nil)
  1012. (character :tag "Selection Key")
  1013. (string :tag "Template")
  1014. (file :tag "Destination file (optional)"))))
  1015. (defcustom org-reverse-note-order nil
  1016. "Non-nil means, store new notes at the beginning of a file or entry.
  1017. When nil, new notes will be filed to the end of a file or entry."
  1018. :group 'org-remember
  1019. :type '(choice
  1020. (const :tag "Reverse always" t)
  1021. (const :tag "Reverse never" nil)
  1022. (repeat :tag "By file name regexp"
  1023. (cons regexp boolean))))
  1024. (defgroup org-todo nil
  1025. "Options concerning TODO items in Org-mode."
  1026. :tag "Org TODO"
  1027. :group 'org)
  1028. (defcustom org-todo-keywords '("TODO" "DONE")
  1029. "List of TODO entry keywords.
  1030. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1031. considered to mean that the entry is \"done\". All the other mean that
  1032. action is required, and will make the entry show up in todo lists, diaries
  1033. etc.
  1034. The command \\[org-todo] cycles an entry through these states, and an
  1035. additional state where no keyword is present. For details about this
  1036. cycling, see also the variable `org-todo-interpretation'
  1037. Changes become only effective after restarting Emacs."
  1038. :group 'org-todo
  1039. :group 'org-keywords
  1040. :type '(repeat (string :tag "Keyword")))
  1041. (defcustom org-todo-interpretation 'sequence
  1042. "Controls how TODO keywords are interpreted.
  1043. This variable is only relevant if `org-todo-keywords' contains more than two
  1044. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1045. When `sequence', \\[org-todo] will always switch to the next state in the
  1046. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1047. to state when executed several times in direct succession. Otherwise, it
  1048. switches directly to DONE from any state.
  1049. See the manual for more information."
  1050. :group 'org-todo
  1051. :group 'org-keywords
  1052. :type '(choice (const sequence)
  1053. (const type)))
  1054. (defcustom org-after-todo-state-change-hook nil
  1055. "Hook which is run after the state of a TODO item was changed.
  1056. The new state (a string with a TODO keyword, or nil) is available in the
  1057. Lisp variable `state'."
  1058. :group 'org-todo
  1059. :type 'hook)
  1060. (defcustom org-log-done nil
  1061. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1062. When the state of an entry is changed from nothing to TODO, remove a previous
  1063. closing date.
  1064. This can also be configured on a per-file basis by adding one of
  1065. the following lines anywhere in the buffer:
  1066. #+STARTUP: logging
  1067. #+STARTUP: nologging"
  1068. :group 'org-todo
  1069. :type 'boolean)
  1070. (defgroup org-priorities nil
  1071. "Priorities in Org-mode."
  1072. :tag "Org Priorities"
  1073. :group 'org-todo)
  1074. (defcustom org-default-priority ?B
  1075. "The default priority of TODO items.
  1076. This is the priority an item get if no explicit priority is given."
  1077. :group 'org-priorities
  1078. :type 'character)
  1079. (defcustom org-lowest-priority ?C
  1080. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1081. :group 'org-priorities
  1082. :type 'character)
  1083. (defgroup org-time nil
  1084. "Options concerning time stamps and deadlines in Org-mode."
  1085. :tag "Org Time"
  1086. :group 'org)
  1087. (defcustom org-insert-labeled-timestamps-at-point nil
  1088. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1089. When nil, these labeled time stamps are forces into the second line of an
  1090. entry, just after the headline. When scheduling from the global TODO list,
  1091. the time stamp will always be forced into the second line."
  1092. :group 'org-time
  1093. :type 'boolean)
  1094. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1095. "Formats for `format-time-string' which are used for time stamps.
  1096. It is not recommended to change this constant.")
  1097. (defcustom org-time-stamp-rounding-minutes 0
  1098. "Number of minutes to round time stamps to upon insertion.
  1099. When zero, insert the time unmodified. Useful rounding numbers
  1100. should be factors of 60, so for example 5, 10, 15.
  1101. When this is not zero, you can still force an exact time-stamp by using
  1102. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1103. :group 'org-time
  1104. :type 'integer)
  1105. (defcustom org-deadline-warning-days 30
  1106. "No. of days before expiration during which a deadline becomes active.
  1107. This variable governs the display in the org file."
  1108. :group 'org-time
  1109. :type 'number)
  1110. (defcustom org-popup-calendar-for-date-prompt t
  1111. "Non-nil means, pop up a calendar when prompting for a date.
  1112. In the calendar, the date can be selected with mouse-1. However, the
  1113. minibuffer will also be active, and you can simply enter the date as well.
  1114. When nil, only the minibuffer will be available."
  1115. :group 'org-time
  1116. :type 'boolean)
  1117. (defcustom org-calendar-follow-timestamp-change t
  1118. "Non-nil means, make the calendar window follow timestamp changes.
  1119. When a timestamp is modified and the calendar window is visible, it will be
  1120. moved to the new date."
  1121. :group 'org-time
  1122. :type 'boolean)
  1123. (defgroup org-tags nil
  1124. "Options concerning tags in Org-mode."
  1125. :tag "Org Tags"
  1126. :group 'org)
  1127. (defcustom org-tag-alist nil
  1128. "List of tags allowed in Org-mode files.
  1129. When this list is nil, Org-mode will base TAG input on what is already in the
  1130. buffer.
  1131. The value of this variable is an alist, the car may be (and should) be a
  1132. character that is used to select that tag through the fast-tag-selection
  1133. interface. See the manual for details."
  1134. :group 'org-tags
  1135. :type '(repeat
  1136. (cons (character) (string :tag "Tag"))))
  1137. (defcustom org-use-fast-tag-selection 'auto
  1138. "Non-nil means, use fast tag selection scheme.
  1139. This is a special interface to select and deselect tags with single keys.
  1140. When nil, fast selection is never used.
  1141. When the symbol `auto', fast selection is used if and only if selection
  1142. characters for tags have been configured, either through the variable
  1143. `org-tag-alist' or through a #+TAGS line in the buffer.
  1144. When t, fast selection is always used and selection keys are assigned
  1145. automatically if necessary."
  1146. :group 'org-tags
  1147. :type '(choice
  1148. (const :tag "Always" t)
  1149. (const :tag "Never" nil)
  1150. (const :tag "When selection characters are configured" 'auto)))
  1151. (defcustom org-tags-column 48
  1152. "The column to which tags should be indented in a headline.
  1153. If this number is positive, it specifies the column. If it is negative,
  1154. it means that the tags should be flushright to that column. For example,
  1155. -79 works well for a normal 80 character screen."
  1156. :group 'org-tags
  1157. :type 'integer)
  1158. (defcustom org-auto-align-tags t
  1159. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1160. These operations change the length of a headline and therefore shift
  1161. the tags around. With this options turned on, after each such operation
  1162. the tags are again aligned to `org-tags-column'."
  1163. :group 'org-tags
  1164. :type 'boolean)
  1165. (defcustom org-use-tag-inheritance t
  1166. "Non-nil means, tags in levels apply also for sublevels.
  1167. When nil, only the tags directly given in a specific line apply there.
  1168. If you turn off this option, you very likely want to turn on the
  1169. companion option `org-tags-match-list-sublevels'."
  1170. :group 'org-tags
  1171. :type 'boolean)
  1172. (defcustom org-tags-match-list-sublevels nil
  1173. "Non-nil means list also sublevels of headlines matching tag search.
  1174. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1175. the sublevels of a headline matching a tag search often also match
  1176. the same search. Listing all of them can create very long lists.
  1177. Setting this variable to nil causes subtrees of a match to be skipped.
  1178. This option is off by default, because inheritance in on. If you turn
  1179. inheritance off, you very likely want to turn this option on.
  1180. As a special case, if the tag search is restricted to TODO items, the
  1181. value of this variable is ignored and sublevels are always checked, to
  1182. make sure all corresponding TODO items find their way into the list."
  1183. :group 'org-tags
  1184. :type 'boolean)
  1185. (defvar org-tags-history nil
  1186. "History of minibuffer reads for tags.")
  1187. (defvar org-last-tags-completion-table nil
  1188. "The last used completion table for tags.")
  1189. (defgroup org-agenda nil
  1190. "Options concerning agenda display Org-mode."
  1191. :tag "Org Agenda"
  1192. :group 'org)
  1193. (defvar org-category nil
  1194. "Variable used by org files to set a category for agenda display.
  1195. Such files should use a file variable to set it, for example
  1196. -*- mode: org; org-category: \"ELisp\"
  1197. or contain a special line
  1198. #+CATEGORY: ELisp
  1199. If the file does not specify a category, then file's base name
  1200. is used instead.")
  1201. (make-variable-buffer-local 'org-category)
  1202. (defcustom org-agenda-files nil
  1203. "The files to be used for agenda display.
  1204. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1205. \\[org-remove-file]. You can also use customize to edit the list.
  1206. If the value of the variable is not a list but a single file name, then
  1207. the list of agenda files is actually stored and maintained in that file, one
  1208. agenda file per line."
  1209. :group 'org-agenda
  1210. :type '(choice
  1211. (repeat :tag "List of files" file)
  1212. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1213. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  1214. "Custom commands for the agenda.
  1215. These commands will be offered on the splash screen displayed by the
  1216. agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
  1217. key The key (a single char as a string) to be associated with the command.
  1218. type The command type, any of the following symbols:
  1219. todo Entries with a specific TODO keyword, in all agenda files.
  1220. tags Tags match in all agenda files.
  1221. tags-todo Tags match in all agenda files, TODO entries only.
  1222. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1223. tags-tree Sparse tree with all tags matches in *current* file.
  1224. occur-tree Occur sparse tree for current file.
  1225. match What to search for:
  1226. - a single keyword for TODO keyword searches
  1227. - a tags match expression for tags searches
  1228. - a regular expression for occur searches"
  1229. :group 'org-agenda
  1230. :type '(repeat
  1231. (list (string :tag "Key")
  1232. (choice :tag "Type"
  1233. (const :tag "Tags search in all agenda files" tags)
  1234. (const :tag "Tags search of TODO entries, all agenda files" tags-todo)
  1235. (const :tag "TODO keyword search in all agenda files" todo)
  1236. (const :tag "Tags sparse tree in current buffer" tags-tree)
  1237. (const :tag "TODO keyword tree in current buffer" todo-tree)
  1238. (const :tag "Occur tree in current buffer" occur-tree))
  1239. (string :tag "Match"))))
  1240. ;; FIXME: Need a toggle for this variable, maybe a mode in the agenda buffer?
  1241. (defcustom org-agenda-todo-list-sublevels t
  1242. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1243. When nil, the sublevels of a TODO entry are not checked, resulting in
  1244. potentially much shorter TODO lists."
  1245. :group 'org-agenda
  1246. :group 'org-todo
  1247. :type 'boolean)
  1248. (defcustom org-agenda-todo-ignore-scheduled nil
  1249. "Non-nil means, don't show scheduled entries in the global todo list.
  1250. The idea behind this is that by scheduling it, you have already taken care
  1251. of this item."
  1252. :group 'org-agenda
  1253. :group 'org-todo
  1254. :type 'boolean)
  1255. (defcustom org-agenda-include-all-todo nil
  1256. "Non-nil means, the agenda will always contain all TODO entries.
  1257. When nil, date-less entries will only be shown if `org-agenda' is called
  1258. with a prefix argument.
  1259. When non-nil, the TODO entries will be listed at the top of the agenda, before
  1260. the entries for specific days."
  1261. :group 'org-agenda
  1262. :type 'boolean)
  1263. (defcustom org-agenda-include-diary nil
  1264. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1265. :group 'org-agenda
  1266. :type 'boolean)
  1267. (defcustom org-calendar-to-agenda-key [?c]
  1268. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1269. The command `org-calendar-goto-agenda' will be bound to this key. The
  1270. default is the character `c' because then `c' can be used to switch back and
  1271. forth between agenda and calendar."
  1272. :group 'org-agenda
  1273. :type 'sexp)
  1274. (defgroup org-agenda-setup nil
  1275. "Options concerning setting up the Agenda window in Org Mode."
  1276. :tag "Org Agenda Window Setup"
  1277. :group 'org-agenda)
  1278. (defcustom org-agenda-mouse-1-follows-link nil
  1279. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1280. A longer mouse click will still set point. Does not wortk on XEmacs.
  1281. Needs to be set before org.el is loaded."
  1282. :group 'org-agenda-setup
  1283. :type 'boolean)
  1284. (defcustom org-agenda-start-with-follow-mode nil
  1285. "The initial value of follwo-mode in a newly created agenda window."
  1286. :group 'org-agenda-setup
  1287. :type 'boolean)
  1288. (defcustom org-select-timeline-window t
  1289. "Non-nil means, after creating a timeline, move cursor into Timeline window.
  1290. When nil, cursor will remain in the current window."
  1291. :group 'org-agenda-setup
  1292. :type 'boolean)
  1293. (defcustom org-select-agenda-window t
  1294. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1295. When nil, cursor will remain in the current window."
  1296. :group 'org-agenda-setup
  1297. :type 'boolean)
  1298. (defcustom org-fit-agenda-window t
  1299. "Non-nil means, change window size of agenda to fit content."
  1300. :group 'org-agenda-setup
  1301. :type 'boolean)
  1302. (defgroup org-agenda-display nil
  1303. "Options concerning what to display initially in Agenda."
  1304. :tag "Org Agenda Display"
  1305. :group 'org-agenda)
  1306. (defcustom org-agenda-show-all-dates t
  1307. "Non-nil means, `org-agenda' shows every day in the selected range.
  1308. When nil, only the days which actually have entries are shown."
  1309. :group 'org-agenda-display
  1310. :type 'boolean)
  1311. (defcustom org-agenda-start-on-weekday 1
  1312. "Non-nil means, start the overview always on the specified weekday.
  1313. 0 denotes Sunday, 1 denotes Monday etc.
  1314. When nil, always start on the current day."
  1315. :group 'org-agenda-display
  1316. :type '(choice (const :tag "Today" nil)
  1317. (number :tag "Weekday No.")))
  1318. (defcustom org-agenda-ndays 7
  1319. "Number of days to include in overview display.
  1320. Should be 1 or 7."
  1321. :group 'org-agenda-display
  1322. :type 'number)
  1323. (defcustom org-agenda-use-time-grid t
  1324. "Non-nil means, show a time grid in the agenda schedule.
  1325. A time grid is a set of lines for specific times (like every two hours between
  1326. 8:00 and 20:00). The items scheduled for a day at specific times are
  1327. sorted in between these lines.
  1328. For details about when the grid will be shown, and what it will look like, see
  1329. the variable `org-agenda-time-grid'."
  1330. :group 'org-agenda-display
  1331. :type 'boolean)
  1332. (defcustom org-agenda-time-grid
  1333. '((daily today require-timed)
  1334. "----------------"
  1335. (800 1000 1200 1400 1600 1800 2000))
  1336. "The settings for time grid for agenda display.
  1337. This is a list of three items. The first item is again a list. It contains
  1338. symbols specifying conditions when the grid should be displayed:
  1339. daily if the agenda shows a single day
  1340. weekly if the agenda shows an entire week
  1341. today show grid on current date, independent of daily/weekly display
  1342. require-timed show grid only if at least on item has a time specification
  1343. The second item is a string which will be places behing the grid time.
  1344. The third item is a list of integers, indicating the times that should have
  1345. a grid line."
  1346. :group 'org-agenda-display
  1347. :type
  1348. '(list
  1349. (set :greedy t :tag "Grid Display Options"
  1350. (const :tag "Show grid in single day agenda display" daily)
  1351. (const :tag "Show grid in weekly agenda display" weekly)
  1352. (const :tag "Always show grid for today" today)
  1353. (const :tag "Show grid only if any timed entries are present"
  1354. require-timed)
  1355. (const :tag "Skip grid times already present in an entry"
  1356. remove-match))
  1357. (string :tag "Grid String")
  1358. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1359. (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
  1360. "Sorting structure for the agenda items of a single day.
  1361. This is a list of symbols which will be used in sequence to determine
  1362. if an entry should be listed before another entry. The following
  1363. symbols are recognized:
  1364. time-up Put entries with time-of-day indications first, early first
  1365. time-down Put entries with time-of-day indications first, late first
  1366. category-keep Keep the default order of categories, corresponding to the
  1367. sequence in `org-agenda-files'.
  1368. category-up Sort alphabetically by category, A-Z.
  1369. category-down Sort alphabetically by category, Z-A.
  1370. priority-up Sort numerically by priority, high priority last.
  1371. priority-down Sort numerically by priority, high priority first.
  1372. The different possibilities will be tried in sequence, and testing stops
  1373. if one comparison returns a \"not-equal\". For example, the default
  1374. '(time-up category-keep priority-down)
  1375. means: Pull out all entries having a specified time of day and sort them,
  1376. in order to make a time schedule for the current day the first thing in the
  1377. agenda listing for the day. Of the entries without a time indication, keep
  1378. the grouped in categories, don't sort the categories, but keep them in
  1379. the sequence given in `org-agenda-files'. Within each category sort by
  1380. priority.
  1381. Leaving out `category-keep' would mean that items will be sorted across
  1382. categories by priority."
  1383. :group 'org-agenda-display
  1384. :type '(repeat
  1385. (choice
  1386. (const time-up)
  1387. (const time-down)
  1388. (const category-keep)
  1389. (const category-up)
  1390. (const category-down)
  1391. (const priority-up)
  1392. (const priority-down))))
  1393. (defcustom org-sort-agenda-notime-is-late t
  1394. "Non-nil means, items without time are considered late.
  1395. This is only relevant for sorting. When t, items which have no explicit
  1396. time like 15:30 will be considered as 24:01, i.e. later than any items which
  1397. do have a time. When nil, the default time is before 0:00. You can use this
  1398. option to decide if the schedule for today should come before or after timeless
  1399. agenda entries."
  1400. :group 'org-agenda-display
  1401. :type 'boolean)
  1402. (defgroup org-agenda-prefix nil
  1403. "Options concerning the entry prefix in the Org-mode agenda display."
  1404. :tag "Org Agenda Prefix"
  1405. :group 'org-agenda)
  1406. (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
  1407. "Format specification for the prefix of items in the agenda buffer.
  1408. This format works similar to a printf format, with the following meaning:
  1409. %c the category of the item, \"Diary\" for entries from the diary, or
  1410. as given by the CATEGORY keyword or derived from the file name.
  1411. %T the first tag of the item.
  1412. %t the time-of-day specification if one applies to the entry, in the
  1413. format HH:MM
  1414. %s Scheduling/Deadline information, a short string
  1415. All specifiers work basically like the standard `%s' of printf, but may
  1416. contain two additional characters: A question mark just after the `%' and
  1417. a whitespace/punctuation character just before the final letter.
  1418. If the first character after `%' is a question mark, the entire field
  1419. will only be included if the corresponding value applies to the
  1420. current entry. This is useful for fields which should have fixed
  1421. width when present, but zero width when absent. For example,
  1422. \"%?-12t\" will result in a 12 character time field if a time of the
  1423. day is specified, but will completely disappear in entries which do
  1424. not contain a time.
  1425. If there is punctuation or whitespace character just before the final
  1426. format letter, this character will be appended to the field value if
  1427. the value is not empty. For example, the format \"%-12:c\" leads to
  1428. \"Diary: \" if the category is \"Diary\". If the category were be
  1429. empty, no additional colon would be interted.
  1430. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1431. - Indent the line with two space characters
  1432. - Give the category in a 12 chars wide field, padded with whitespace on
  1433. the right (because of `-'). Append a colon if there is a category
  1434. (because of `:').
  1435. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1436. time, don't put in an empty field, just skip it (because of '?').
  1437. - Finally, put the scheduling information and append a whitespace.
  1438. As another example, if you don't want the time-of-day of entries in
  1439. the prefix, you could use:
  1440. (setq org-agenda-prefix-format \" %-11:c% s\")
  1441. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1442. `org-agenda-remove-tags-when-in-prefix'."
  1443. :type 'string
  1444. :group 'org-agenda-prefix)
  1445. (defcustom org-timeline-prefix-format " % s"
  1446. "Like `org-agenda-prefix-format', but for the timeline of a single file."
  1447. :type 'string
  1448. :group 'org-agenda-prefix)
  1449. (defvar org-prefix-format-compiled nil
  1450. "The compiled version of the most recently used prefix format.
  1451. Depending on which command was used last, this may be the compiled version
  1452. of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
  1453. ;; FIXME: There seem to be situations where this does not work.
  1454. (defcustom org-agenda-remove-times-when-in-prefix t
  1455. "Non-nil means, remove duplicate time specifications in agenda items.
  1456. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1457. time-of-day specification in a headline or diary entry is extracted and
  1458. placed into the prefix. If this option is non-nil, the original specification
  1459. \(a timestamp or -range, or just a plain time(range) specification like
  1460. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1461. cluttered.
  1462. The option can be t or nil. It may also be the symbol `beg', indicating
  1463. that the time should only be removed what it is located at the beginning of
  1464. the headline/diary entry."
  1465. :group 'org-agenda-prefix
  1466. :type '(choice
  1467. (const :tag "Always" t)
  1468. (const :tag "Never" nil)
  1469. (const :tag "When at beginning of entry" beg)))
  1470. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1471. "Non-nil means, remove the tags from the headline copy in the agenda.
  1472. When this is the symbol `prefix', only remove tags when
  1473. `org-agenda-prefix-format' contains a `%T' specifier."
  1474. :group 'org-agenda-prefix
  1475. :type '(choice
  1476. (const :tag "Always" t)
  1477. (const :tag "Never" nil)
  1478. (const :tag "When prefix format contains %T" prefix)))
  1479. (defgroup org-export nil
  1480. "Options for exporting org-listings."
  1481. :tag "Org Export"
  1482. :group 'org)
  1483. (defgroup org-export-general nil
  1484. "General options for exporting Org-mode files."
  1485. :tag "Org Export General"
  1486. :group 'org-export)
  1487. (defcustom org-export-publishing-directory "."
  1488. "Path to the location where exported files should be located.
  1489. This path may be relative to the directory where the Org-mode file lives.
  1490. The default is to put them into the same directory as the Org-mode file.
  1491. The variable may also be an alist with export types `:html', `:ascii',
  1492. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1493. is relative, it is interpreted relative to the directory where the exported
  1494. Org-mode files lives."
  1495. :group 'org-export-general
  1496. :type '(choice
  1497. (directory)
  1498. (repeat
  1499. (cons
  1500. (choice :tag "Type"
  1501. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1502. (directory)))))
  1503. (defcustom org-export-language-setup
  1504. '(("en" "Author" "Date" "Table of Contents")
  1505. ("da" "Ophavsmand" "Dato" "Indhold")
  1506. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1507. ("es" "Autor" "Fecha" "\xccndice")
  1508. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1509. ("it" "Autore" "Data" "Indice")
  1510. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1511. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1512. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1513. "Terms used in export text, translated to different languages.
  1514. Use the variable `org-export-default-language' to set the language,
  1515. or use the +OPTION lines for a per-file setting."
  1516. :group 'org-export-general
  1517. :type '(repeat
  1518. (list
  1519. (string :tag "HTML language tag")
  1520. (string :tag "Author")
  1521. (string :tag "Date")
  1522. (string :tag "Table of Contents"))))
  1523. (defcustom org-export-default-language "en"
  1524. "The default language of HTML export, as a string.
  1525. This should have an association in `org-export-language-setup'."
  1526. :group 'org-export-general
  1527. :type 'string)
  1528. (defcustom org-export-headline-levels 3
  1529. "The last level which is still exported as a headline.
  1530. Inferior levels will produce itemize lists when exported.
  1531. Note that a numeric prefix argument to an exporter function overrides
  1532. this setting.
  1533. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1534. :group 'org-export-general
  1535. :type 'number)
  1536. (defcustom org-export-with-section-numbers t
  1537. "Non-nil means, add section numbers to headlines when exporting.
  1538. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1539. :group 'org-export-general
  1540. :type 'boolean)
  1541. (defcustom org-export-with-toc t
  1542. "Non-nil means, create a table of contents in exported files.
  1543. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1544. Headlines which contain any TODO items will be marked with \"(*)\" in
  1545. ASCII export, and with red color in HTML output.
  1546. In HTML output, the TOC will be clickable.
  1547. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1548. :group 'org-export-general
  1549. :type 'boolean)
  1550. (defcustom org-export-mark-todo-in-toc nil
  1551. "Non-nil means, mark TOC lines that contain any open TODO items."
  1552. :group 'org-export-general
  1553. :type 'boolean)
  1554. (defcustom org-export-preserve-breaks nil
  1555. "Non-nil means, preserve all line breaks when exporting.
  1556. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1557. export, line breaks will always be preserved, regardless of this variable.
  1558. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1559. :group 'org-export-general
  1560. :type 'boolean)
  1561. (defcustom org-export-with-timestamps t
  1562. "Nil means, do not export time stamps and associated keywords."
  1563. :group 'org-export
  1564. :type 'boolean)
  1565. (defcustom org-export-with-tags t
  1566. "Nil means, do not export tags, just remove them from headlines."
  1567. :group 'org-export-general
  1568. :type 'boolean)
  1569. (defcustom org-export-with-timestamps t
  1570. "Nil means, do not export timestamps and associated keywords."
  1571. :group 'org-export-general
  1572. :type 'boolean)
  1573. (defgroup org-export-translation nil
  1574. "Options for translating special ascii sequences for the export backends."
  1575. :tag "Org Export Translation"
  1576. :group 'org-export)
  1577. (defcustom org-export-with-emphasize t
  1578. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1579. If the export target supports emphasizing text, the word will be
  1580. typeset in bold, italic, or underlined, respectively. Works only for
  1581. single words, but you can say: I *really* *mean* *this*.
  1582. Not all export backends support this.
  1583. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1584. :group 'org-export-translation
  1585. :type 'boolean)
  1586. (defcustom org-export-with-sub-superscripts t
  1587. "Non-nil means, interpret \"_\" and \"^\" for export.
  1588. When this option is turned on, you can use TeX-like syntax for sub- and
  1589. superscripts. Several characters after \"_\" or \"^\" will be
  1590. considered as a single item - so grouping with {} is normally not
  1591. needed. For example, the following things will be parsed as single
  1592. sub- or superscripts.
  1593. 10^24 or 10^tau several digits will be considered 1 item.
  1594. 10^-12 or 10^-tau a leading sign with digits or a word
  1595. x^2-y^3 will be read as x^2 - y^3, because items are
  1596. terminated by almost any nonword/nondigit char.
  1597. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1598. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1599. sub/superscript.
  1600. Not all export backends support this, but HTML does.
  1601. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1602. :group 'org-export-translation
  1603. :type 'boolean)
  1604. (defcustom org-export-with-TeX-macros t
  1605. "Non-nil means, interpret simple TeX-like macros when exporting.
  1606. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1607. No only real TeX macros will work here, but the standard HTML entities
  1608. for math can be used as macro names as well. For a list of supported
  1609. names in HTML export, see the constant `org-html-entities'.
  1610. Not all export backends support this.
  1611. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1612. :group 'org-export-translation
  1613. :type 'boolean)
  1614. (defcustom org-export-with-fixed-width t
  1615. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1616. This can be used to have pre-formatted text, fragments of code etc. For
  1617. example:
  1618. : ;; Some Lisp examples
  1619. : (while (defc cnt)
  1620. : (ding))
  1621. will be looking just like this in also HTML. See also the QUOTE keyword.
  1622. Not all export backends support this.
  1623. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1624. :group 'org-export-translation
  1625. :type 'boolean)
  1626. (defcustom org-match-sexp-depth 3
  1627. "Number of stacked braces for sub/superscript matching.
  1628. This has to be set before loading org.el to be effective."
  1629. :group 'org-export-translation
  1630. :type 'integer)
  1631. (defgroup org-export-tables nil
  1632. "Options for exporting tables in Org-mode."
  1633. :tag "Org Export Tables"
  1634. :group 'org-export)
  1635. (defcustom org-export-with-tables t
  1636. "If non-nil, lines starting with \"|\" define a table.
  1637. For example:
  1638. | Name | Address | Birthday |
  1639. |-------------+----------+-----------|
  1640. | Arthur Dent | England | 29.2.2100 |
  1641. Not all export backends support this.
  1642. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1643. :group 'org-export-tables
  1644. :type 'boolean)
  1645. (defcustom org-export-highlight-first-table-line t
  1646. "Non-nil means, highlight the first table line.
  1647. In HTML export, this means use <th> instead of <td>.
  1648. In tables created with table.el, this applies to the first table line.
  1649. In Org-mode tables, all lines before the first horizontal separator
  1650. line will be formatted with <th> tags."
  1651. :group 'org-export-tables
  1652. :type 'boolean)
  1653. (defcustom org-export-table-remove-special-lines t
  1654. "Remove special lines and marking characters in calculating tables.
  1655. This removes the special marking character column from tables that are set
  1656. up for spreadsheet calculations. It also removes the entire lines
  1657. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1658. the values of constants may be useful to have."
  1659. :group 'org-export-tables
  1660. :type 'boolean)
  1661. (defcustom org-export-prefer-native-exporter-for-tables nil
  1662. "Non-nil means, always export tables created with table.el natively.
  1663. Natively means, use the HTML code generator in table.el.
  1664. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1665. the table does not use row- or column-spanning). This has the
  1666. advantage, that the automatic HTML conversions for math symbols and
  1667. sub/superscripts can be applied. Org-mode's HTML generator is also
  1668. much faster."
  1669. :group 'org-export-tables
  1670. :type 'boolean)
  1671. (defgroup org-export-ascii nil
  1672. "Options specific for ASCII export of Org-mode files."
  1673. :tag "Org Export ASCII"
  1674. :group 'org-export)
  1675. (defcustom org-export-ascii-show-new-buffer t
  1676. "Non-nil means, popup buffer containing the exported ASCII text.
  1677. Otherwise the buffer will just be saved to a file and stay hidden."
  1678. :group 'org-export-ascii
  1679. :type 'boolean)
  1680. (defgroup org-export-xml nil
  1681. "Options specific for XML export of Org-mode files."
  1682. :tag "Org Export XML"
  1683. :group 'org-export)
  1684. ;; FIXME: I am told XOXO is not XML, it is semantic-only HTML.
  1685. (defcustom org-export-xml-type 'xoxo
  1686. "The kind of XML to be produced by the XML exporter.
  1687. Allowed values are:
  1688. xoxo The XOXO exporter."
  1689. :group 'org-export-xml
  1690. :type '(choice
  1691. (const :tag "XOXO" xoxo)))
  1692. (defgroup org-export-html nil
  1693. "Options specific for HTML export of Org-mode files."
  1694. :tag "Org Export HTML"
  1695. :group 'org-export)
  1696. (defcustom org-export-html-style
  1697. "<style type=\"text/css\">
  1698. html {
  1699. font-family: Times, serif;
  1700. font-size: 12pt;
  1701. }
  1702. .title { text-align: center; }
  1703. .todo { color: red; }
  1704. .done { color: green; }
  1705. .timestamp { color: grey }
  1706. .timestamp-kwd { color: CadetBlue }
  1707. .tag { background-color:lightblue; font-weight:normal }
  1708. .target { background-color: lavender; }
  1709. pre {
  1710. border: 1pt solid #AEBDCC;
  1711. background-color: #F3F5F7;
  1712. padding: 5pt;
  1713. font-family: courier, monospace;
  1714. }
  1715. table { border-collapse: collapse; }
  1716. td, th {
  1717. vertical-align: top;
  1718. border: 1pt solid #ADB9CC;
  1719. }
  1720. </style>"
  1721. "The default style specification for exported HTML files.
  1722. Since there are different ways of setting style information, this variable
  1723. needs to contain the full HTML structure to provide a style, including the
  1724. surrounding HTML tags. The style specifications should include definitions
  1725. for new classes todo, done, title, and deadline. For example, legal values
  1726. would be:
  1727. <style type=\"text/css\">
  1728. p { font-weight: normal; color: gray; }
  1729. h1 { color: black; }
  1730. .title { text-align: center; }
  1731. .todo, .deadline { color: red; }
  1732. .done { color: green; }
  1733. </style>
  1734. or, if you want to keep the style in a file,
  1735. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1736. As the value of this option simply gets inserted into the HTML <head> header,
  1737. you can \"misuse\" it to add arbitrary text to the header."
  1738. :group 'org-export-html
  1739. :type 'string)
  1740. (defcustom org-export-html-link-org-files-as-html t
  1741. "Non-nil means, make file links to `file.org' point to `file.html'.
  1742. When org-mode is exporting an org-mode file to HTML, links to
  1743. non-html files are directly put into a href tag in HTML.
  1744. However, links to other Org-mode files (recognized by the
  1745. extension `.org.) should become links to the corresponding html
  1746. file, assuming that the linked org-mode file will also be
  1747. converted to HTML.
  1748. When nil, the links still point to the plain `.org' file."
  1749. :group 'org-export-html
  1750. :type 'boolean)
  1751. (defcustom org-export-html-inline-images 'maybe
  1752. "Non-nil means, inline images into exported HTML pages.
  1753. This is done using an <img> tag. When nil, an anchor with href is used to
  1754. link to the image. If this option is `maybe', then images in links with
  1755. an empty description will be inlined, while images with a description will
  1756. be linked only."
  1757. :group 'org-export-html
  1758. :type '(choice (const :tag "Never" nil)
  1759. (const :tag "Always" t)
  1760. (const :tag "When there is no description" maybe)))
  1761. (defcustom org-export-html-expand t
  1762. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1763. When nil, these tags will be exported as plain text and therefore
  1764. not be interpreted by a browser.
  1765. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1766. :group 'org-export-html
  1767. :type 'boolean)
  1768. (defcustom org-export-html-table-tag
  1769. "<table border=1 cellspacing=0 cellpadding=6>"
  1770. "The HTML tag used to start a table.
  1771. This must be a <table> tag, but you may change the options like
  1772. borders and spacing."
  1773. :group 'org-export-html
  1774. :type 'string)
  1775. (defcustom org-export-html-with-timestamp nil
  1776. "If non-nil, write `org-export-html-html-helper-timestamp'
  1777. into the exported HTML text. Otherwise, the buffer will just be saved
  1778. to a file."
  1779. :group 'org-export-html
  1780. :type 'boolean)
  1781. (defcustom org-export-html-html-helper-timestamp
  1782. "<br><br><hr><p><!-- hhmts start --> <!-- hhmts end -->\n"
  1783. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1784. :group 'org-export-html
  1785. :type 'string)
  1786. (defcustom org-export-html-show-new-buffer nil
  1787. "Non-nil means, popup buffer containing the exported html text.
  1788. Otherwise, the buffer will just be saved to a file and stay hidden."
  1789. :group 'org-export-html
  1790. :type 'boolean)
  1791. (defgroup org-export-icalendar nil
  1792. "Options specific for iCalendar export of Org-mode files."
  1793. :tag "Org Export iCalendar"
  1794. :group 'org-export)
  1795. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1796. "The file name for the iCalendar file covering all agenda files.
  1797. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  1798. The file name should be absolute."
  1799. :group 'org-export-icalendar
  1800. :type 'file)
  1801. (defcustom org-icalendar-include-todo nil
  1802. "Non-nil means, export to iCalendar files should also cover TODO items."
  1803. :group 'org-export-icalendar
  1804. :type 'boolean)
  1805. (defcustom org-icalendar-combined-name "OrgMode"
  1806. "Calendar name for the combined iCalendar representing all agenda files."
  1807. :group 'org-export-icalendar
  1808. :type 'string)
  1809. (defgroup org-font-lock nil
  1810. "Font-lock settings for highlighting in Org-mode."
  1811. :tag "Org Font Lock"
  1812. :group 'org)
  1813. (defcustom org-level-color-stars-only nil
  1814. "Non-nil means fontify only the stars in each headline.
  1815. When nil, the entire headline is fontified.
  1816. Changing it requires restart of `font-lock-mode' to become effective
  1817. also in regions already fontified."
  1818. :group 'org-font-lock
  1819. :type 'boolean)
  1820. (defcustom org-hide-leading-stars nil
  1821. "Non-nil means, hide the first N-1 stars in a headline.
  1822. This works by using the face `org-hide' for these stars. This
  1823. face is white for a light background, and black for a dark
  1824. background. You may have to customize the face `org-hide' to
  1825. make this work.
  1826. Changing it requires restart of `font-lock-mode' to become effective
  1827. also in regions already fontified.
  1828. You may also set this on a per-file basis by adding one of the following
  1829. lines to the buffer:
  1830. #+STARTUP: hidestars
  1831. #+STARTUP: showstars"
  1832. :group 'org-font-lock
  1833. :type 'boolean)
  1834. (defcustom org-fontify-done-headline nil
  1835. "Non-nil means, change the face of a headline if it is marked DONE.
  1836. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1837. When this is non-nil, the headline after the keyword is set to the
  1838. `org-headline-done' as an additional indication."
  1839. :group 'org-font-lock
  1840. :type 'boolean)
  1841. (defcustom org-fontify-emphasized-text t
  1842. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1843. Changing this variable requires a restart of Emacs to take effect."
  1844. :group 'org-font-lock
  1845. :type 'boolean)
  1846. (defgroup org-faces nil
  1847. "Faces in Org-mode."
  1848. :tag "Org Faces"
  1849. :group 'org-font-lock)
  1850. (defun org-compatible-face (specs)
  1851. "Make a compatible face specification.
  1852. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  1853. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  1854. to the top of the list. The `min-colors' attribute will be removed from
  1855. any other entries, and any resulting duplicates will be removed entirely."
  1856. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  1857. (let (r e a)
  1858. (while (setq e (pop specs))
  1859. (cond
  1860. ((memq (car e) '(t default)) (push e r))
  1861. ((setq a (member '(min-colors 8) (car e)))
  1862. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  1863. (cdr e)))))
  1864. ((setq a (assq 'min-colors (car e)))
  1865. (setq e (cons (delq a (car e)) (cdr e)))
  1866. (or (assoc (car e) r) (push e r)))
  1867. (t (or (assoc (car e) r) (push e r)))))
  1868. (nreverse r))
  1869. specs))
  1870. (defface org-hide
  1871. '((((background light)) (:foreground "white"))
  1872. (((background dark)) (:foreground "black")))
  1873. "Face used to hide leading stars in headlines.
  1874. The forground color of this face should be equal to the background
  1875. color of the frame."
  1876. :group 'org-faces)
  1877. (defface org-level-1 ;; font-lock-function-name-face
  1878. (org-compatible-face
  1879. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1880. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1881. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1882. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1883. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  1884. (t (:bold t))))
  1885. "Face used for level 1 headlines."
  1886. :group 'org-faces)
  1887. (defface org-level-2 ;; font-lock-variable-name-face
  1888. (org-compatible-face
  1889. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  1890. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  1891. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  1892. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  1893. (t (:bold t))))
  1894. "Face used for level 2 headlines."
  1895. :group 'org-faces)
  1896. (defface org-level-3 ;; font-lock-keyword-face
  1897. (org-compatible-face
  1898. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  1899. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  1900. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  1901. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  1902. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  1903. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  1904. (t (:bold t))))
  1905. "Face used for level 3 headlines."
  1906. :group 'org-faces)
  1907. (defface org-level-4 ;; font-lock-comment-face
  1908. (org-compatible-face
  1909. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  1910. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  1911. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  1912. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  1913. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  1914. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  1915. (t (:bold t))))
  1916. "Face used for level 4 headlines."
  1917. :group 'org-faces)
  1918. (defface org-level-5 ;; font-lock-type-face
  1919. (org-compatible-face
  1920. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  1921. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  1922. (((class color) (min-colors 8)) (:foreground "green"))))
  1923. "Face used for level 5 headlines."
  1924. :group 'org-faces)
  1925. (defface org-level-6 ;; font-lock-constant-face
  1926. (org-compatible-face
  1927. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  1928. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  1929. (((class color) (min-colors 8)) (:foreground "magenta"))))
  1930. "Face used for level 6 headlines."
  1931. :group 'org-faces)
  1932. (defface org-level-7 ;; font-lock-builtin-face
  1933. (org-compatible-face
  1934. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  1935. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  1936. (((class color) (min-colors 8)) (:foreground "blue"))))
  1937. "Face used for level 7 headlines."
  1938. :group 'org-faces)
  1939. (defface org-level-8 ;; font-lock-string-face
  1940. (org-compatible-face
  1941. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1942. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1943. (((class color) (min-colors 8)) (:foreground "green"))))
  1944. "Face used for level 8 headlines."
  1945. :group 'org-faces)
  1946. (defface org-special-keyword ;; font-lock-string-face
  1947. (org-compatible-face
  1948. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1949. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1950. (t (:italic t))))
  1951. "Face used for special keywords."
  1952. :group 'org-faces)
  1953. (defface org-warning ;; font-lock-warning-face
  1954. (org-compatible-face
  1955. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  1956. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  1957. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  1958. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  1959. (t (:bold t))))
  1960. "Face for deadlines and TODO keywords."
  1961. :group 'org-faces)
  1962. (defface org-headline-done ;; font-lock-string-face
  1963. (org-compatible-face
  1964. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1965. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1966. (((class color) (min-colors 8) (background light)) (:bold nil))))
  1967. "Face used to indicate that a headline is DONE.
  1968. This face is only used if `org-fontify-done-headline' is set."
  1969. :group 'org-faces)
  1970. (defface org-link
  1971. '((((class color) (background light)) (:foreground "Purple" :underline t))
  1972. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  1973. (t (:underline t)))
  1974. "Face for links."
  1975. :group 'org-faces)
  1976. (defface org-date
  1977. '((((class color) (background light)) (:foreground "Purple" :underline t))
  1978. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  1979. (t (:underline t)))
  1980. "Face for links."
  1981. :group 'org-faces)
  1982. (defface org-tag
  1983. '((t (:bold t)))
  1984. "Face for tags."
  1985. :group 'org-faces)
  1986. (defface org-todo ;; font-lock-warning-face
  1987. (org-compatible-face
  1988. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  1989. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  1990. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  1991. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  1992. (t (:inverse-video t :bold t))))
  1993. "Face for TODO keywords."
  1994. :group 'org-faces)
  1995. (defface org-done ;; font-lock-type-face
  1996. (org-compatible-face
  1997. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  1998. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  1999. (((class color) (min-colors 8)) (:foreground "green"))
  2000. (t (:bold t))))
  2001. "Face used for DONE."
  2002. :group 'org-faces)
  2003. (defface org-table ;; font-lock-function-name-face
  2004. (org-compatible-face
  2005. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2006. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2007. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2008. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2009. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2010. (((class color) (min-colors 8) (background dark)))))
  2011. "Face used for tables."
  2012. :group 'org-faces)
  2013. (defface org-formula
  2014. (org-compatible-face
  2015. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2016. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2017. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2018. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2019. (t (:bold t :italic t))))
  2020. "Face for formulas."
  2021. :group 'org-faces)
  2022. (defface org-scheduled-today
  2023. (org-compatible-face
  2024. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2025. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2026. (((class color) (min-colors 8)) (:foreground "green"))
  2027. (t (:bold t :italic t))))
  2028. "Face for items scheduled for a certain day."
  2029. :group 'org-faces)
  2030. (defface org-scheduled-previously
  2031. (org-compatible-face
  2032. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2033. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2034. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2035. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2036. (t (:bold t))))
  2037. "Face for items scheduled previously, and not yet done."
  2038. :group 'org-faces)
  2039. (defface org-time-grid ;; font-lock-variable-name-face
  2040. (org-compatible-face
  2041. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2042. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2043. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2044. "Face used for time grids."
  2045. :group 'org-faces)
  2046. (defconst org-level-faces
  2047. '(org-level-1 org-level-2 org-level-3 org-level-4
  2048. org-level-5 org-level-6 org-level-7 org-level-8
  2049. ))
  2050. (defconst org-n-levels (length org-level-faces))
  2051. (defconst org-bold-re
  2052. (if (featurep 'xemacs)
  2053. "\\([ ]\\|^\\)\\(\\*\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)\\*\\)\\([ ,.]\\|$\\)"
  2054. "\\([ ]\\|^\\)\\(\\*\\(\\w[[:word:] -_]*?\\w\\)\\*\\)\\([ ,.]\\|$\\)")
  2055. "Regular expression for bold emphasis.")
  2056. (defconst org-italic-re
  2057. (if (featurep 'xemacs)
  2058. "\\([ ]\\|^\\)\\(/\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)/\\)\\([ ,.]\\|$\\)"
  2059. "\\([ ]\\|^\\)\\(/\\(\\w[[:word:] -_]*?\\w\\)/\\)\\([ ,.]\\|$\\)")
  2060. "Regular expression for italic emphasis.")
  2061. (defconst org-underline-re
  2062. (if (featurep 'xemacs)
  2063. "\\([ ]\\|^\\)\\(_\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)_\\)\\([ ,.]\\|$\\)"
  2064. "\\([ ]\\|^\\)\\(_\\(\\w[[:word:] -_]*?\\w\\)_\\)\\([ ,.]\\|$\\)")
  2065. "Regular expression for underline emphasis.")
  2066. ;; Variables for pre-computed regular expressions, all buffer local
  2067. (defvar org-done-string nil
  2068. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2069. (make-variable-buffer-local 'org-done-string)
  2070. (defvar org-todo-regexp nil
  2071. "Matches any of the TODO state keywords.")
  2072. (make-variable-buffer-local 'org-todo-regexp)
  2073. (defvar org-not-done-regexp nil
  2074. "Matches any of the TODO state keywords except the last one.")
  2075. (make-variable-buffer-local 'org-not-done-regexp)
  2076. (defvar org-todo-line-regexp nil
  2077. "Matches a headline and puts TODO state into group 2 if present.")
  2078. (make-variable-buffer-local 'org-todo-line-regexp)
  2079. (defvar org-nl-done-regexp nil
  2080. "Matches newline followed by a headline with the DONE keyword.")
  2081. (make-variable-buffer-local 'org-nl-done-regexp)
  2082. (defvar org-looking-at-done-regexp nil
  2083. "Matches the DONE keyword a point.")
  2084. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2085. (defvar org-todo-kwd-priority-p nil
  2086. "Do TODO items have priorities?")
  2087. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2088. (defvar org-todo-kwd-max-priority nil
  2089. "Maximum priority of TODO items.")
  2090. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2091. (defvar org-ds-keyword-length 12
  2092. "Maximum length of the Deadline and SCHEDULED keywords.")
  2093. (make-variable-buffer-local 'org-ds-keyword-length)
  2094. (defvar org-deadline-regexp nil
  2095. "Matches the DEADLINE keyword.")
  2096. (make-variable-buffer-local 'org-deadline-regexp)
  2097. (defvar org-deadline-time-regexp nil
  2098. "Matches the DEADLINE keyword together with a time stamp.")
  2099. (make-variable-buffer-local 'org-deadline-time-regexp)
  2100. (defvar org-deadline-line-regexp nil
  2101. "Matches the DEADLINE keyword and the rest of the line.")
  2102. (make-variable-buffer-local 'org-deadline-line-regexp)
  2103. (defvar org-scheduled-regexp nil
  2104. "Matches the SCHEDULED keyword.")
  2105. (make-variable-buffer-local 'org-scheduled-regexp)
  2106. (defvar org-scheduled-time-regexp nil
  2107. "Matches the SCHEDULED keyword together with a time stamp.")
  2108. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2109. (defvar org-closed-time-regexp nil
  2110. "Matches the CLOSED keyword together with a time stamp.")
  2111. (make-variable-buffer-local 'org-closed-time-regexp)
  2112. (defvar org-keyword-time-regexp nil
  2113. "Matches any of the 3 keywords, together with the time stamp.")
  2114. (make-variable-buffer-local 'org-keyword-time-regexp)
  2115. (defvar org-maybe-keyword-time-regexp nil
  2116. "Matches a timestamp, possibly preceeded by a keyword.")
  2117. (make-variable-buffer-local 'org-keyword-time-regexp)
  2118. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2119. mouse-map t)
  2120. "Properties to remove when a string without properties is wanted.")
  2121. (defsubst org-match-string-no-properties (num &optional string)
  2122. (if (featurep 'xemacs)
  2123. (let ((s (match-string num string)))
  2124. (remove-text-properties 0 (length s) org-rm-props s)
  2125. s)
  2126. (match-string-no-properties num string)))
  2127. (defsubst org-no-properties (s)
  2128. (remove-text-properties 0 (length s) org-rm-props s)
  2129. s)
  2130. (defun org-set-regexps-and-options ()
  2131. "Precompute regular expressions for current buffer."
  2132. (when (eq major-mode 'org-mode)
  2133. (let ((re (org-make-options-regexp
  2134. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2135. "STARTUP" "ARCHIVE" "TAGS")))
  2136. (splitre "[ \t]+")
  2137. kwds int key value cat arch tags)
  2138. (save-excursion
  2139. (save-restriction
  2140. (widen)
  2141. (goto-char (point-min))
  2142. (while (re-search-forward re nil t)
  2143. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2144. (cond
  2145. ((equal key "CATEGORY")
  2146. (if (string-match "[ \t]+$" value)
  2147. (setq value (replace-match "" t t value)))
  2148. (setq cat (intern value)))
  2149. ((equal key "SEQ_TODO")
  2150. (setq int 'sequence
  2151. kwds (append kwds (org-split-string value splitre))))
  2152. ((equal key "PRI_TODO")
  2153. (setq int 'priority
  2154. kwds (append kwds (org-split-string value splitre))))
  2155. ((equal key "TYP_TODO")
  2156. (setq int 'type
  2157. kwds (append kwds (org-split-string value splitre))))
  2158. ((equal key "TAGS")
  2159. (setq tags (append tags (org-split-string value splitre))))
  2160. ((equal key "STARTUP")
  2161. (let ((opts (org-split-string value splitre))
  2162. (set '(("fold" org-startup-folded t)
  2163. ("overview" org-startup-folded t)
  2164. ("nofold" org-startup-folded nil)
  2165. ("showall" org-startup-folded nil)
  2166. ("content" org-startup-folded content)
  2167. ("hidestars" org-hide-leading-stars t)
  2168. ("showstars" org-hide-leading-stars nil)
  2169. ("odd" org-odd-levels-only t)
  2170. ("oddeven" org-odd-levels-only nil)
  2171. ("align" org-startup-align-all-tables t)
  2172. ("noalign" org-startup-align-all-tables nil)
  2173. ("logging" org-log-done t)
  2174. ("nologging" org-log-done nil)
  2175. ("dlcheck" org-startup-with-deadline-check t)
  2176. ("nodlcheck" org-startup-with-deadline-check nil)))
  2177. l var val)
  2178. (while (setq l (assoc (pop opts) set))
  2179. (setq var (nth 1 l) val (nth 2 l))
  2180. (set (make-local-variable var) val))))
  2181. ((equal key "ARCHIVE")
  2182. (string-match " *$" value)
  2183. (setq arch (replace-match "" t t value))
  2184. (remove-text-properties 0 (length arch)
  2185. '(face t fontified t) arch)))
  2186. )))
  2187. (and cat (set (make-local-variable 'org-category) cat))
  2188. (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
  2189. (and arch (set (make-local-variable 'org-archive-location) arch))
  2190. (and int (set (make-local-variable 'org-todo-interpretation) int))
  2191. (when tags
  2192. (let (e tg c tgs)
  2193. (while (setq e (pop tags))
  2194. (if (string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2195. (push (cons (match-string 1 e)
  2196. (string-to-char (match-string 2 e)))
  2197. tgs)
  2198. (push (list e) tgs)))
  2199. (set (make-local-variable 'org-tag-alist) nil)
  2200. (while (setq e (pop tgs))
  2201. (or (assoc (car e) org-tag-alist)
  2202. (push e org-tag-alist))))))
  2203. ;; Compute the regular expressions and other local variables
  2204. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2205. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2206. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2207. (length org-scheduled-string)))
  2208. org-done-string
  2209. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2210. org-todo-regexp
  2211. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2212. "\\|") "\\)\\>")
  2213. org-not-done-regexp
  2214. (concat "\\<\\("
  2215. (mapconcat 'regexp-quote
  2216. (nreverse (cdr (reverse org-todo-keywords)))
  2217. "\\|")
  2218. "\\)\\>")
  2219. org-todo-line-regexp
  2220. (concat "^\\(\\*+\\)[ \t]*\\("
  2221. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2222. "\\)? *\\(.*\\)")
  2223. org-nl-done-regexp
  2224. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2225. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2226. org-deadline-regexp (concat "\\<" org-deadline-string)
  2227. org-deadline-time-regexp
  2228. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2229. org-deadline-line-regexp
  2230. (concat "\\<\\(" org-deadline-string "\\).*")
  2231. org-scheduled-regexp
  2232. (concat "\\<" org-scheduled-string)
  2233. org-scheduled-time-regexp
  2234. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2235. org-closed-time-regexp
  2236. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2237. org-keyword-time-regexp
  2238. (concat "\\<\\(" org-scheduled-string
  2239. "\\|" org-deadline-string
  2240. "\\|" org-closed-string "\\)"
  2241. " *[[<]\\([^]>]+\\)[]>]") ;; FIXME: is this correct?
  2242. org-maybe-keyword-time-regexp
  2243. (concat "\\(\\<\\(" org-scheduled-string
  2244. "\\|" org-deadline-string
  2245. "\\|" org-closed-string "\\)\\)?"
  2246. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)")) ;; FIXME: is this correct?
  2247. (org-set-font-lock-defaults)))
  2248. ;; Tell the compiler about dynamically scoped variables,
  2249. ;; and variables from other packages
  2250. (defvar zmacs-regions) ; XEmacs regions
  2251. (defvar original-date) ; dynamically scoped in calendar
  2252. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2253. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2254. (defvar org-html-entities) ; defined later in this file
  2255. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2256. (defvar org-time-was-given) ; dynamically scoped parameter
  2257. (defvar org-ts-what) ; dynamically scoped parameter
  2258. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2259. (defvar timecnt) ; dynamically scoped parameter
  2260. (defvar levels-open) ; dynamically scoped parameter
  2261. (defvar entry) ; dynamically scoped parameter
  2262. (defvar date) ; dynamically scoped parameter
  2263. (defvar description) ; dynamically scoped parameter
  2264. (defvar ans1) ; dynamically scoped parameter
  2265. (defvar ans2) ; dynamically scoped parameter
  2266. (defvar starting-day) ; local variable
  2267. (defvar include-all-loc) ; local variable
  2268. (defvar vm-message-pointer) ; from vm
  2269. (defvar vm-folder-directory) ; from vm
  2270. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2271. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2272. (defvar gnus-group-name) ; from gnus
  2273. (defvar gnus-article-current) ; from gnus
  2274. (defvar w3m-current-url) ; from w3m
  2275. (defvar mh-progs) ; from MH-E
  2276. (defvar mh-current-folder) ; from MH-E
  2277. (defvar mh-show-folder-buffer) ; from MH-E
  2278. (defvar mh-index-folder) ; from MH-E
  2279. (defvar mh-searcher) ; from MH-E
  2280. (defvar org-selected-point) ; dynamically scoped parameter
  2281. (defvar calendar-mode-map) ; from calendar.el
  2282. (defvar last-arg) ; local variable
  2283. (defvar remember-save-after-remembering) ; from remember.el
  2284. (defvar remember-data-file) ; from remember.el
  2285. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2286. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2287. (defvar orgtbl-mode) ; defined later in this file
  2288. (defvar Info-current-file) ; from info.el
  2289. (defvar Info-current-node) ; from info.el
  2290. ;;; Define the mode
  2291. (defvar org-mode-map
  2292. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2293. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22.")
  2294. (copy-keymap outline-mode-map))
  2295. "Keymap for Org-mode.")
  2296. (defvar org-struct-menu) ; defined later in this file
  2297. (defvar org-org-menu) ; defined later in this file
  2298. (defvar org-tbl-menu) ; defined later in this file
  2299. ;; We use a before-change function to check if a table might need
  2300. ;; an update.
  2301. (defvar org-table-may-need-update t
  2302. "Indicates that a table might need an update.
  2303. This variable is set by `org-before-change-function'.
  2304. `org-table-align' sets it back to nil.")
  2305. (defvar org-mode-hook nil)
  2306. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2307. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2308. ;;;###autoload
  2309. (define-derived-mode org-mode outline-mode "Org"
  2310. "Outline-based notes management and organizer, alias
  2311. \"Carsten's outline-mode for keeping track of everything.\"
  2312. Org-mode develops organizational tasks around a NOTES file which
  2313. contains information about projects as plain text. Org-mode is
  2314. implemented on top of outline-mode, which is ideal to keep the content
  2315. of large files well structured. It supports ToDo items, deadlines and
  2316. time stamps, which magically appear in the diary listing of the Emacs
  2317. calendar. Tables are easily created with a built-in table editor.
  2318. Plain text URL-like links connect to websites, emails (VM), Usenet
  2319. messages (Gnus), BBDB entries, and any files related to the project.
  2320. For printing and sharing of notes, an Org-mode file (or a part of it)
  2321. can be exported as a structured ASCII or HTML file.
  2322. The following commands are available:
  2323. \\{org-mode-map}"
  2324. ;; Get rid of Outline menus, they are not needed
  2325. ;; Need to do this here because define-derived-mode sets up
  2326. ;; the keymap so late.
  2327. (if (featurep 'xemacs)
  2328. (if org-noutline-p
  2329. (progn
  2330. (easy-menu-remove outline-mode-menu-heading)
  2331. (easy-menu-remove outline-mode-menu-show)
  2332. (easy-menu-remove outline-mode-menu-hide))
  2333. (delete-menu-item '("Headings"))
  2334. (delete-menu-item '("Show"))
  2335. (delete-menu-item '("Hide"))
  2336. (set-menubar-dirty-flag))
  2337. (define-key org-mode-map [menu-bar headings] 'undefined)
  2338. (define-key org-mode-map [menu-bar hide] 'undefined)
  2339. (define-key org-mode-map [menu-bar show] 'undefined))
  2340. (easy-menu-add org-org-menu)
  2341. (easy-menu-add org-tbl-menu)
  2342. (org-install-agenda-files-menu)
  2343. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2344. (org-add-to-invisibility-spec '(org-cwidth))
  2345. (when (featurep 'xemacs)
  2346. (set (make-local-variable 'line-move-ignore-invisible) t))
  2347. (setq outline-regexp "\\*+")
  2348. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2349. (setq outline-level 'org-outline-level)
  2350. (when (and org-ellipsis (stringp org-ellipsis))
  2351. (unless org-display-table
  2352. (setq org-display-table (make-display-table)))
  2353. (set-display-table-slot org-display-table
  2354. 4 (string-to-vector org-ellipsis))
  2355. (setq buffer-display-table org-display-table))
  2356. (org-set-regexps-and-options)
  2357. (if org-startup-truncated (setq truncate-lines t))
  2358. (set (make-local-variable 'font-lock-unfontify-region-function)
  2359. 'org-unfontify-region)
  2360. ;; Activate before-change-function
  2361. (set (make-local-variable 'org-table-may-need-update) t)
  2362. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2363. 'local)
  2364. ;; Paragraphs and auto-filling
  2365. (org-set-autofill-regexps)
  2366. (org-update-radio-target-regexp)
  2367. ;; Settings for Calc embedded mode
  2368. (set (make-local-variable 'calc-embedded-open-formula) "|\\|\n")
  2369. (set (make-local-variable 'calc-embedded-close-formula) "|\\|\n")
  2370. (if (and org-insert-mode-line-in-empty-file
  2371. (interactive-p)
  2372. (= (point-min) (point-max)))
  2373. (insert " -*- mode: org -*-\n\n"))
  2374. (unless org-inhibit-startup
  2375. (if org-startup-align-all-tables
  2376. (org-table-map-tables 'org-table-align))
  2377. (if org-startup-with-deadline-check
  2378. (call-interactively 'org-check-deadlines)
  2379. (cond
  2380. ((eq org-startup-folded t)
  2381. (org-cycle '(4)))
  2382. ((eq org-startup-folded 'content)
  2383. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2384. (org-cycle '(4)) (org-cycle '(4))))))))
  2385. (defsubst org-call-with-arg (command arg)
  2386. "Call COMMAND interactively, but pretend prefix are was ARG."
  2387. (let ((current-prefix-arg arg)) (call-interactively command)))
  2388. (defsubst org-current-line (&optional pos)
  2389. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2390. (defun org-current-time ()
  2391. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2392. (if (> org-time-stamp-rounding-minutes 0)
  2393. (let ((r org-time-stamp-rounding-minutes)
  2394. (time (decode-time)))
  2395. (apply 'encode-time
  2396. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2397. (nthcdr 2 time))))
  2398. (current-time)))
  2399. (defun org-add-props (string plist &rest props)
  2400. "Add text properties to entire string, from beginning to end.
  2401. PLIST may be a list of properties, PROPS are individual properties and values
  2402. that will be added to PLIST. Returns the string that was modified."
  2403. (add-text-properties
  2404. 0 (length string) (if props (append plist props) plist) string)
  2405. string)
  2406. (put 'org-add-props 'lisp-indent-function 2)
  2407. ;;; Font-Lock stuff
  2408. (defvar org-mouse-map (make-sparse-keymap))
  2409. (define-key org-mouse-map
  2410. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2411. (define-key org-mouse-map
  2412. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2413. (when org-mouse-1-follows-link
  2414. (define-key org-mouse-map [follow-link] 'mouse-face))
  2415. (when org-tab-follows-link
  2416. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2417. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2418. (when org-return-follows-link
  2419. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2420. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2421. (require 'font-lock)
  2422. (defconst org-non-link-chars "]\t\n\r<>")
  2423. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2424. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2425. (defconst org-link-re-with-space
  2426. (concat
  2427. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2428. "\\([^" org-non-link-chars " ]"
  2429. "[^" org-non-link-chars "]*"
  2430. "[^" org-non-link-chars " ]\\)>?")
  2431. "Matches a link with spaces, optional angular brackets around it.")
  2432. (defconst org-link-re-with-space2
  2433. (concat
  2434. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2435. "\\([^" org-non-link-chars " ]"
  2436. "[^]\t\n\r]*"
  2437. "[^" org-non-link-chars " ]\\)>?")
  2438. "Matches a link with spaces, optional angular brackets around it.")
  2439. (defconst org-angle-link-re
  2440. (concat
  2441. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2442. "\\([^" org-non-link-chars " ]"
  2443. "[^" org-non-link-chars "]*"
  2444. "\\)>")
  2445. "Matches link with angular brackets, spaces are allowed.")
  2446. (defconst org-plain-link-re
  2447. (concat
  2448. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2449. "\\([^]\t\n\r<>,;() ]+\\)")
  2450. "Matches plain link, without spaces.")
  2451. (defconst org-bracket-link-regexp
  2452. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2453. "Matches a link in double brackets.")
  2454. (defconst org-bracket-link-analytic-regexp
  2455. (concat
  2456. "\\[\\["
  2457. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2458. "\\([^]]+\\)"
  2459. "\\]"
  2460. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2461. "\\]"))
  2462. ; 1: http:
  2463. ; 2: http
  2464. ; 3: path
  2465. ; 4: [desc]
  2466. ; 5: desc
  2467. (defconst org-ts-lengths
  2468. (cons (length (format-time-string (car org-time-stamp-formats)))
  2469. (length (format-time-string (cdr org-time-stamp-formats))))
  2470. "This holds the lengths of the two different time formats.")
  2471. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2472. "Regular expression for fast time stamp matching.")
  2473. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2474. "Regular expression for fast time stamp matching.")
  2475. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2476. "Regular expression matching time strings for analysis.")
  2477. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2478. "Regular expression matching time stamps, with groups.")
  2479. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2480. "Regular expression matching a time stamp range.")
  2481. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2482. org-ts-regexp "\\)?")
  2483. "Regular expression matching a time stamp or time stamp range.")
  2484. (defun org-activate-plain-links (limit)
  2485. "Run through the buffer and add overlays to links."
  2486. (if (re-search-forward org-plain-link-re limit t)
  2487. (progn
  2488. (add-text-properties (match-beginning 0) (match-end 0)
  2489. (list 'mouse-face 'highlight
  2490. 'keymap org-mouse-map
  2491. ))
  2492. t)))
  2493. (defun org-activate-angle-links (limit)
  2494. "Run through the buffer and add overlays to links."
  2495. (if (re-search-forward org-angle-link-re limit t)
  2496. (progn
  2497. (add-text-properties (match-beginning 0) (match-end 0)
  2498. (list 'mouse-face 'highlight
  2499. 'keymap org-mouse-map
  2500. ))
  2501. t)))
  2502. (defun org-activate-bracket-links (limit)
  2503. "Run through the buffer and add overlays to bracketed links."
  2504. (if (re-search-forward org-bracket-link-regexp limit t)
  2505. (let* ((help (concat "LINK: "
  2506. (org-match-string-no-properties 1)))
  2507. ;; FIXME: above we should remove the escapes.
  2508. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2509. 'keymap org-mouse-map 'mouse-face 'highlight
  2510. 'help-echo help))
  2511. (vp (list 'rear-nonsticky t
  2512. 'keymap org-mouse-map 'mouse-face 'highlight
  2513. 'help-echo help)))
  2514. ;; We need to remove the invisible property here. Table narrowing
  2515. ;; may have made some of this invisible.
  2516. (remove-text-properties (match-beginning 0) (match-end 0)
  2517. '(invisible nil))
  2518. (if (match-end 3)
  2519. (progn
  2520. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2521. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2522. (add-text-properties (match-end 3) (match-end 0) ip))
  2523. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2524. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2525. (add-text-properties (match-end 1) (match-end 0) ip))
  2526. t)))
  2527. (defun org-activate-dates (limit)
  2528. "Run through the buffer and add overlays to dates."
  2529. (if (re-search-forward org-tsr-regexp limit t)
  2530. (progn
  2531. (add-text-properties (match-beginning 0) (match-end 0)
  2532. (list 'mouse-face 'highlight
  2533. 'keymap org-mouse-map))
  2534. t)))
  2535. (defvar org-target-link-regexp nil
  2536. "Regular expression matching radio targets in plain text.")
  2537. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2538. "Regular expression matching a link target.")
  2539. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2540. "Regular expression matching a link target.")
  2541. (defun org-activate-target-links (limit)
  2542. "Run through the buffer and add overlays to target matches."
  2543. (when org-target-link-regexp
  2544. (let ((case-fold-search t))
  2545. (if (re-search-forward org-target-link-regexp limit t)
  2546. (progn
  2547. (add-text-properties (match-beginning 0) (match-end 0)
  2548. (list 'mouse-face 'highlight
  2549. 'keymap org-mouse-map
  2550. 'help-echo "Radio target link"
  2551. 'org-linked-text t))
  2552. t)))))
  2553. (defun org-update-radio-target-regexp ()
  2554. "Find all radio targets in this file and update the regular expression."
  2555. (interactive)
  2556. (when (memq 'radio org-activate-links)
  2557. (setq org-target-link-regexp
  2558. (org-make-target-link-regexp (org-all-targets 'radio)))
  2559. (org-restart-font-lock)))
  2560. (defun org-hide-wide-columns (limit)
  2561. (let (s e)
  2562. (setq s (text-property-any (point) (or limit (point-max))
  2563. 'org-cwidth t))
  2564. (when s
  2565. (setq e (next-single-property-change s 'org-cwidth))
  2566. (add-text-properties s e '(invisible org-cwidth intangible t))
  2567. (goto-char e)
  2568. t)))
  2569. (defun org-restart-font-lock ()
  2570. "Restart font-lock-mode, to force refontification."
  2571. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2572. (font-lock-mode -1)
  2573. (font-lock-mode 1)))
  2574. (defun org-all-targets (&optional radio)
  2575. "Return a list of all targets in this file.
  2576. With optional argument RADIO, only find radio targets."
  2577. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2578. rtn)
  2579. (save-excursion
  2580. (goto-char (point-min))
  2581. (while (re-search-forward re nil t)
  2582. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2583. rtn)))
  2584. (defun org-make-target-link-regexp (targets)
  2585. "Make regular expression matching all strings in TARGETS.
  2586. The regular expression finds the targets also if there is a line break
  2587. between words."
  2588. (and targets
  2589. (concat
  2590. "\\<\\("
  2591. (mapconcat
  2592. (lambda (x)
  2593. (while (string-match " +" x)
  2594. (setq x (replace-match "\\s-+" t t x)))
  2595. x)
  2596. targets
  2597. "\\|")
  2598. "\\)\\>")))
  2599. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2600. "Matches CamelCase words, possibly with a star before it.")
  2601. (defun org-activate-camels (limit)
  2602. "Run through the buffer and add overlays to dates."
  2603. (if (re-search-forward org-camel-regexp limit t)
  2604. (progn
  2605. (add-text-properties (match-beginning 0) (match-end 0)
  2606. (list 'mouse-face 'highlight
  2607. 'keymap org-mouse-map))
  2608. t)))
  2609. (defun org-activate-tags (limit)
  2610. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2611. (progn
  2612. (add-text-properties (match-beginning 1) (match-end 1)
  2613. (list 'mouse-face 'highlight
  2614. 'keymap org-mouse-map))
  2615. t)))
  2616. (defun org-font-lock-level ()
  2617. (save-excursion
  2618. (org-back-to-heading t)
  2619. (- (match-end 0) (match-beginning 0))))
  2620. (defun org-outline-level ()
  2621. (save-excursion
  2622. (looking-at outline-regexp)
  2623. (if (match-beginning 1)
  2624. (+ (org-get-string-indentation (match-string 1)) 1000)
  2625. (- (match-end 0) (match-beginning 0)))))
  2626. (defvar org-font-lock-keywords nil)
  2627. (defun org-set-font-lock-defaults ()
  2628. (let* ((em org-fontify-emphasized-text)
  2629. (lk org-activate-links)
  2630. (org-font-lock-extra-keywords
  2631. ;; Headlines
  2632. (list
  2633. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2634. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2635. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2636. (1 'org-table))
  2637. ;; Links
  2638. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  2639. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  2640. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  2641. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  2642. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  2643. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  2644. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  2645. (if org-table-limit-column-width
  2646. '(org-hide-wide-columns (0 nil append)))
  2647. ;; TODO lines
  2648. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2649. '(1 'org-todo t))
  2650. ;; Priorities
  2651. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2652. ;; Special keywords
  2653. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2654. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2655. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2656. ;; Emphasis
  2657. (if em (list org-bold-re 2 ''bold 'prepend))
  2658. (if em (list org-italic-re 2 ''italic 'prepend))
  2659. (if em (list org-underline-re 2 ''underline 'prepend))
  2660. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  2661. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  2662. 2 'bold prepend)
  2663. ;; COMMENT
  2664. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2665. "\\|" org-quote-string "\\)\\>")
  2666. '(1 'org-special-keyword t))
  2667. '("^#.*" (0 'font-lock-comment-face t))
  2668. ;; DONE
  2669. (if org-fontify-done-headline
  2670. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2671. '(1 'org-done t) '(2 'org-headline-done t))
  2672. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2673. '(1 'org-done t)))
  2674. ;; Table stuff
  2675. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2676. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2677. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2678. (if org-format-transports-properties-p
  2679. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  2680. )))
  2681. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2682. ;; Now set the full font-lock-keywords
  2683. (set (make-local-variable 'org-font-lock-keywords)
  2684. org-font-lock-extra-keywords)
  2685. (set (make-local-variable 'font-lock-defaults)
  2686. '(org-font-lock-keywords t nil nil backward-paragraph))
  2687. (kill-local-variable 'font-lock-keywords) nil))
  2688. (defvar org-m nil)
  2689. (defvar org-l nil)
  2690. (defvar org-f nil)
  2691. (defun org-get-level-face (n)
  2692. "Get the right face for match N in font-lock matching of healdines."
  2693. (setq org-l (- (match-end 2) (match-beginning 1)))
  2694. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2695. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2696. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2697. (cond
  2698. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2699. ((eq n 2) org-f)
  2700. (t (if org-level-color-stars-only nil org-f))))
  2701. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2702. "Remove fontification and activation overlays from links."
  2703. (font-lock-default-unfontify-region beg end)
  2704. (let* ((buffer-undo-list t)
  2705. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2706. (inhibit-modification-hooks t)
  2707. deactivate-mark buffer-file-name buffer-file-truename)
  2708. (remove-text-properties beg end
  2709. '(mouse-face nil keymap nil org-linked-text nil
  2710. invisible nil intangible nil))))
  2711. ;;; Visibility cycling
  2712. (defvar org-cycle-global-status nil)
  2713. (make-variable-buffer-local 'org-cycle-global-status)
  2714. (defvar org-cycle-subtree-status nil)
  2715. (make-variable-buffer-local 'org-cycle-subtree-status)
  2716. ;;;###autoload
  2717. (defun org-cycle (&optional arg)
  2718. "Visibility cycling for Org-mode.
  2719. - When this function is called with a prefix argument, rotate the entire
  2720. buffer through 3 states (global cycling)
  2721. 1. OVERVIEW: Show only top-level headlines.
  2722. 2. CONTENTS: Show all headlines of all levels, but no body text.
  2723. 3. SHOW ALL: Show everything.
  2724. - When point is at the beginning of a headline, rotate the subtree started
  2725. by this line through 3 different states (local cycling)
  2726. 1. FOLDED: Only the main headline is shown.
  2727. 2. CHILDREN: The main headline and the direct children are shown.
  2728. From this state, you can move to one of the children
  2729. and zoom in further.
  2730. 3. SUBTREE: Show the entire subtree, including body text.
  2731. - When there is a numeric prefix, go up to a heading with level ARG, do
  2732. a `show-subtree' and return to the previous cursor position. If ARG
  2733. is negative, go up that many levels.
  2734. - When point is not at the beginning of a headline, execute
  2735. `indent-relative', like TAB normally does. See the option
  2736. `org-cycle-emulate-tab' for details.
  2737. - Special case: if point is the the beginning of the buffer and there is
  2738. no headline in line 1, this function will act as if called with prefix arg."
  2739. (interactive "P")
  2740. (let* ((outline-regexp
  2741. (if org-cycle-include-plain-lists
  2742. "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
  2743. outline-regexp))
  2744. (bob-special (and org-cycle-global-at-bob (bobp)
  2745. (not (looking-at outline-regexp))))
  2746. (org-cycle-hook (if bob-special nil org-cycle-hook)))
  2747. (if (or bob-special (equal arg '(4)))
  2748. ;; special case: use global cycling
  2749. (setq arg t))
  2750. (cond
  2751. ((org-at-table-p 'any)
  2752. ;; Enter the table or move to the next field in the table
  2753. (or (org-table-recognize-table.el)
  2754. (progn
  2755. (if arg (org-table-edit-field t)
  2756. (org-table-justify-field-maybe)
  2757. (call-interactively 'org-table-next-field)))))
  2758. ((eq arg t) ;; Global cycling
  2759. (cond
  2760. ((and (eq last-command this-command)
  2761. (eq org-cycle-global-status 'overview))
  2762. ;; We just created the overview - now do table of contents
  2763. ;; This can be slow in very large buffers, so indicate action
  2764. (message "CONTENTS...")
  2765. (org-content)
  2766. (message "CONTENTS...done")
  2767. (setq org-cycle-global-status 'contents)
  2768. (run-hook-with-args 'org-cycle-hook 'contents))
  2769. ((and (eq last-command this-command)
  2770. (eq org-cycle-global-status 'contents))
  2771. ;; We just showed the table of contents - now show everything
  2772. (show-all)
  2773. (message "SHOW ALL")
  2774. (setq org-cycle-global-status 'all)
  2775. (run-hook-with-args 'org-cycle-hook 'all))
  2776. (t
  2777. ;; Default action: go to overview
  2778. (org-overview)
  2779. (message "OVERVIEW")
  2780. (setq org-cycle-global-status 'overview)
  2781. (run-hook-with-args 'org-cycle-hook 'overview))))
  2782. ((integerp arg)
  2783. ;; Show-subtree, ARG levels up from here.
  2784. (save-excursion
  2785. (org-back-to-heading)
  2786. (outline-up-heading (if (< arg 0) (- arg)
  2787. (- (funcall outline-level) arg)))
  2788. (org-show-subtree)))
  2789. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  2790. ;; At a heading: rotate between three different views
  2791. (org-back-to-heading)
  2792. (let ((goal-column 0) eoh eol eos)
  2793. ;; First, some boundaries
  2794. (save-excursion
  2795. (org-back-to-heading)
  2796. (save-excursion
  2797. (beginning-of-line 2)
  2798. (while (and (not (eobp)) ;; this is like `next-line'
  2799. (get-char-property (1- (point)) 'invisible))
  2800. (beginning-of-line 2)) (setq eol (point)))
  2801. (outline-end-of-heading) (setq eoh (point))
  2802. (org-end-of-subtree t) (setq eos (point))
  2803. (outline-next-heading))
  2804. ;; Find out what to do next and set `this-command'
  2805. (cond
  2806. ((and (= eos eoh)
  2807. ;; Nothing is hidden behind this heading
  2808. (message "EMPTY ENTRY")
  2809. (setq org-cycle-subtree-status nil)))
  2810. ((>= eol eos)
  2811. ;; Entire subtree is hidden in one line: open it
  2812. (org-show-entry)
  2813. (show-children)
  2814. (message "CHILDREN")
  2815. (setq org-cycle-subtree-status 'children)
  2816. (run-hook-with-args 'org-cycle-hook 'children))
  2817. ((and (eq last-command this-command)
  2818. (eq org-cycle-subtree-status 'children))
  2819. ;; We just showed the children, now show everything.
  2820. (org-show-subtree)
  2821. (message "SUBTREE")
  2822. (setq org-cycle-subtree-status 'subtree)
  2823. (run-hook-with-args 'org-cycle-hook 'subtree))
  2824. (t
  2825. ;; Default action: hide the subtree.
  2826. (hide-subtree)
  2827. (message "FOLDED")
  2828. (setq org-cycle-subtree-status 'folded)
  2829. (run-hook-with-args 'org-cycle-hook 'folded)))))
  2830. ;; TAB emulation
  2831. (buffer-read-only (org-back-to-heading))
  2832. ((if (and (eq org-cycle-emulate-tab 'white)
  2833. (save-excursion (beginning-of-line 1) (looking-at "[ \t]+$")))
  2834. t
  2835. (eq org-cycle-emulate-tab t))
  2836. (if (and (looking-at "[ \n\r\t]")
  2837. (string-match "^[ \t]*$" (buffer-substring
  2838. (point-at-bol) (point))))
  2839. (progn
  2840. (beginning-of-line 1)
  2841. (and (looking-at "[ \t]+") (replace-match ""))))
  2842. (indent-relative))
  2843. (t (save-excursion
  2844. (org-back-to-heading)
  2845. (org-cycle))))))
  2846. ;;;###autoload
  2847. (defun org-global-cycle ()
  2848. "Cycle the global visibility. For details see `org-cycle'."
  2849. (interactive)
  2850. (org-cycle '(4)))
  2851. (defun org-overview ()
  2852. "Switch to overview mode, shoing only top-level headlines.
  2853. Really, this shows all headlines with level equal or greater than the level
  2854. of the first headline in the buffer. This is important, because if the
  2855. first headline is not level one, then (hide-sublevels 1) gives confusing
  2856. results."
  2857. (interactive)
  2858. (hide-sublevels (save-excursion
  2859. (goto-char (point-min))
  2860. (if (re-search-forward (concat "^" outline-regexp) nil t)
  2861. (progn
  2862. (goto-char (match-beginning 0))
  2863. (funcall outline-level))
  2864. 1))))
  2865. ;; FIXME: allow an argument to give a limiting level for this.
  2866. (defun org-content ()
  2867. "Show all headlines in the buffer, like a table of contents"
  2868. (interactive)
  2869. (save-excursion
  2870. ;; Visit all headings and show their offspring
  2871. (goto-char (point-max))
  2872. (catch 'exit
  2873. (while (and (progn (condition-case nil
  2874. (outline-previous-visible-heading 1)
  2875. (error (goto-char (point-min))))
  2876. t)
  2877. (looking-at outline-regexp))
  2878. (show-branches)
  2879. (if (bobp) (throw 'exit nil))))))
  2880. (defun org-optimize-window-after-visibility-change (state)
  2881. "Adjust the window after a change in outline visibility.
  2882. This function is the default value of the hook `org-cycle-hook'."
  2883. (when (get-buffer-window (current-buffer))
  2884. (cond
  2885. ((eq state 'overview) (org-first-headline-recenter 1))
  2886. ((eq state 'content) nil)
  2887. ((eq state 'all) nil)
  2888. ((eq state 'folded) nil)
  2889. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  2890. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  2891. (defun org-subtree-end-visible-p ()
  2892. "Is the end of the current subtree visible?"
  2893. (pos-visible-in-window-p
  2894. (save-excursion (org-end-of-subtree t) (point))))
  2895. (defun org-first-headline-recenter (&optional N)
  2896. "Move cursor to the first headline and recenter the headline.
  2897. Optional argument N means, put the headline into the Nth line of the window."
  2898. (goto-char (point-min))
  2899. (when (re-search-forward (concat "^" outline-regexp) nil t)
  2900. (beginning-of-line)
  2901. (recenter (prefix-numeric-value N))))
  2902. (defvar org-goto-window-configuration nil)
  2903. (defvar org-goto-marker nil)
  2904. (defvar org-goto-map (make-sparse-keymap))
  2905. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  2906. (while (setq cmd (pop cmds))
  2907. (substitute-key-definition cmd cmd org-goto-map global-map)))
  2908. (define-key org-goto-map "\C-m" 'org-goto-ret)
  2909. (define-key org-goto-map [(left)] 'org-goto-left)
  2910. (define-key org-goto-map [(right)] 'org-goto-right)
  2911. (define-key org-goto-map [(?q)] 'org-goto-quit)
  2912. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  2913. (define-key org-goto-map "\C-i" 'org-cycle)
  2914. (define-key org-goto-map [(tab)] 'org-cycle)
  2915. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  2916. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  2917. (define-key org-goto-map "n" 'outline-next-visible-heading)
  2918. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  2919. (define-key org-goto-map "f" 'outline-forward-same-level)
  2920. (define-key org-goto-map "b" 'outline-backward-same-level)
  2921. (define-key org-goto-map "u" 'outline-up-heading)
  2922. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  2923. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  2924. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  2925. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  2926. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  2927. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  2928. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  2929. (defconst org-goto-help
  2930. "Select a location to jump to, press RET
  2931. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  2932. (defun org-goto ()
  2933. "Go to a different location of the document, keeping current visibility.
  2934. When you want to go to a different location in a document, the fastest way
  2935. is often to fold the entire buffer and then dive into the tree. This
  2936. method has the disadvantage, that the previous location will be folded,
  2937. which may not be what you want.
  2938. This command works around this by showing a copy of the current buffer in
  2939. overview mode. You can dive into the tree in that copy, to find the
  2940. location you want to reach. When pressing RET, the command returns to the
  2941. original buffer in which the visibility is still unchanged. It then jumps
  2942. to the new location, making it and the headline hierarchy above it visible."
  2943. (interactive)
  2944. (let* ((org-goto-start-pos (point))
  2945. (selected-point
  2946. (org-get-location (current-buffer) org-goto-help)))
  2947. (if selected-point
  2948. (progn
  2949. (org-mark-ring-push org-goto-start-pos)
  2950. (goto-char selected-point)
  2951. (if (or (org-invisible-p) (org-invisible-p2))
  2952. (org-show-hierarchy-above)))
  2953. (error "Quit"))))
  2954. (defun org-get-location (buf help)
  2955. "Let the user select a location in the Org-mode buffer BUF.
  2956. This function uses a recursive edit. It returns the selected position
  2957. or nil."
  2958. (let (org-selected-point)
  2959. (save-excursion
  2960. (save-window-excursion
  2961. (delete-other-windows)
  2962. (switch-to-buffer (get-buffer-create "*org-goto*"))
  2963. (with-output-to-temp-buffer "*Help*"
  2964. (princ help))
  2965. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  2966. (setq buffer-read-only nil)
  2967. (erase-buffer)
  2968. (insert-buffer-substring buf)
  2969. (let ((org-startup-truncated t)
  2970. (org-startup-folded t)
  2971. (org-startup-align-all-tables nil)
  2972. (org-startup-with-deadline-check nil))
  2973. (org-mode))
  2974. (setq buffer-read-only t)
  2975. (if (boundp 'org-goto-start-pos)
  2976. (goto-char org-goto-start-pos)
  2977. (goto-char (point-min)))
  2978. (org-beginning-of-line)
  2979. (message "Select location and press RET")
  2980. ;; now we make sure that during selection, ony very few keys work
  2981. ;; and that it is impossible to switch to another window.
  2982. (let ((gm (current-global-map))
  2983. (overriding-local-map org-goto-map))
  2984. (unwind-protect
  2985. (progn
  2986. (use-global-map org-goto-map)
  2987. (recursive-edit))
  2988. (use-global-map gm)))))
  2989. (kill-buffer "*org-goto*")
  2990. org-selected-point))
  2991. (defun org-goto-ret (&optional arg)
  2992. "Finish `org-goto' by going to the new location."
  2993. (interactive "P")
  2994. (setq org-selected-point (point)
  2995. current-prefix-arg arg)
  2996. (throw 'exit nil))
  2997. (defun org-goto-left ()
  2998. "Finish `org-goto' by going to the new location."
  2999. (interactive)
  3000. (if (org-on-heading-p)
  3001. (progn
  3002. (beginning-of-line 1)
  3003. (setq org-selected-point (point)
  3004. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3005. (throw 'exit nil))
  3006. (error "Not on a heading")))
  3007. (defun org-goto-right ()
  3008. "Finish `org-goto' by going to the new location."
  3009. (interactive)
  3010. (if (org-on-heading-p)
  3011. (progn
  3012. (outline-end-of-subtree)
  3013. (or (eobp) (forward-char 1))
  3014. (setq org-selected-point (point)
  3015. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3016. (throw 'exit nil))
  3017. (error "Not on a heading")))
  3018. (defun org-goto-quit ()
  3019. "Finish `org-goto' without cursor motion."
  3020. (interactive)
  3021. (setq org-selected-point nil)
  3022. (throw 'exit nil))
  3023. ;;; Promotion, Demotion, Inserting new headlines
  3024. (defvar org-ignore-region nil
  3025. "To temporarily disable the active region.")
  3026. (defun org-insert-heading (&optional force-heading)
  3027. "Insert a new heading or item with same depth at point.
  3028. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3029. If point is at the beginning of a headline, insert a sibling before the
  3030. current headline. If point is in the middle of a headline, split the headline
  3031. at that position and make the rest of the headline part of the sibling below
  3032. the current headline."
  3033. (interactive "P")
  3034. (if (= (buffer-size) 0)
  3035. (insert "\n* ")
  3036. (when (or force-heading (not (org-insert-item)))
  3037. (let* ((head (save-excursion
  3038. (condition-case nil
  3039. (progn
  3040. (org-back-to-heading)
  3041. (match-string 0))
  3042. (error "*"))))
  3043. pos)
  3044. (cond
  3045. ((and (org-on-heading-p) (bolp)
  3046. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3047. (open-line 1))
  3048. ((bolp) nil)
  3049. (t (newline)))
  3050. (insert head) (just-one-space)
  3051. (setq pos (point))
  3052. (end-of-line 1)
  3053. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3054. (run-hooks 'org-insert-heading-hook)))))
  3055. (defun org-insert-item (&optional checkbox)
  3056. "Insert a new item at the current level.
  3057. Return t when things worked, nil when we are not in an item."
  3058. (when (save-excursion
  3059. (condition-case nil
  3060. (progn
  3061. (org-beginning-of-item)
  3062. (org-at-item-p)
  3063. t)
  3064. (error nil)))
  3065. (let* ((bul (match-string 0))
  3066. (end (match-end 0))
  3067. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3068. (match-end 0)))
  3069. (eowcol (save-excursion (goto-char eow) (current-column)))
  3070. pos)
  3071. (cond
  3072. ((and (org-at-item-p) (<= (point) eow))
  3073. ;; before the bullet
  3074. (beginning-of-line 1)
  3075. (open-line 1))
  3076. ((<= (point) eow)
  3077. (beginning-of-line 1))
  3078. (t (newline)))
  3079. (insert bul (if checkbox "[ ]" ""))
  3080. (just-one-space)
  3081. (setq pos (point))
  3082. (end-of-line 1)
  3083. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3084. (org-maybe-renumber-ordered-list)
  3085. t))
  3086. (defun org-insert-todo-heading (arg)
  3087. "Insert a new heading with the same level and TODO state as current heading.
  3088. If the heading has no TODO state, or if the state is DONE, use the first
  3089. state (TODO by default). Also with prefix arg, force first state."
  3090. (interactive "P")
  3091. (when (not (org-insert-item 'checkbox))
  3092. (org-insert-heading)
  3093. (save-excursion
  3094. (org-back-to-heading)
  3095. (if org-noutline-p
  3096. (outline-previous-heading)
  3097. (outline-previous-visible-heading t))
  3098. (looking-at org-todo-line-regexp))
  3099. (if (or arg
  3100. (not (match-beginning 2))
  3101. (equal (match-string 2) org-done-string))
  3102. (insert (car org-todo-keywords) " ")
  3103. (insert (match-string 2) " "))))
  3104. (defun org-promote-subtree ()
  3105. "Promote the entire subtree.
  3106. See also `org-promote'."
  3107. (interactive)
  3108. (save-excursion
  3109. (org-map-tree 'org-promote)))
  3110. (defun org-demote-subtree ()
  3111. "Demote the entire subtree. See `org-demote'.
  3112. See also `org-promote'."
  3113. (interactive)
  3114. (save-excursion
  3115. (org-map-tree 'org-demote)))
  3116. (defun org-do-promote ()
  3117. "Promote the current heading higher up the tree.
  3118. If the region is active in `transient-mark-mode', promote all headings
  3119. in the region."
  3120. (interactive)
  3121. (save-excursion
  3122. (if (org-region-active-p)
  3123. (org-map-region 'org-promote (region-beginning) (region-end))
  3124. (org-promote)))
  3125. (org-fix-position-after-promote))
  3126. (defun org-do-demote ()
  3127. "Demote the current heading lower down the tree.
  3128. If the region is active in `transient-mark-mode', demote all headings
  3129. in the region."
  3130. (interactive)
  3131. (save-excursion
  3132. (if (org-region-active-p)
  3133. (org-map-region 'org-demote (region-beginning) (region-end))
  3134. (org-demote)))
  3135. (org-fix-position-after-promote))
  3136. (defun org-fix-position-after-promote ()
  3137. "Make sure that after pro/demotion cursor position is right."
  3138. (and (equal (char-after) ?\ )
  3139. (equal (char-before) ?*)
  3140. (forward-char 1)))
  3141. (defun org-get-legal-level (level change)
  3142. "Rectify a level change under the influence of `org-odd-levels-only'
  3143. LEVEL is a current level, CHANGE is by how much the level should be
  3144. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3145. even level numbers will become the next higher odd number."
  3146. (if org-odd-levels-only
  3147. (cond ((not change) (1+ (* 2 (/ level 2))))
  3148. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3149. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3150. (max 1 (+ level change))))
  3151. (defun org-promote ()
  3152. "Promote the current heading higher up the tree.
  3153. If the region is active in `transient-mark-mode', promote all headings
  3154. in the region."
  3155. (org-back-to-heading t)
  3156. (let* ((level (save-match-data (funcall outline-level)))
  3157. (up-head (make-string (org-get-legal-level level -1) ?*))
  3158. (diff (abs (- level (length up-head)))))
  3159. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3160. (replace-match up-head nil t)
  3161. ;; Fixup tag positioning
  3162. (and org-auto-align-tags (org-set-tags nil t))
  3163. (if org-adapt-indentation
  3164. (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
  3165. (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
  3166. (defun org-demote ()
  3167. "Demote the current heading lower down the tree.
  3168. If the region is active in `transient-mark-mode', demote all headings
  3169. in the region."
  3170. (org-back-to-heading t)
  3171. (let* ((level (save-match-data (funcall outline-level)))
  3172. (down-head (make-string (org-get-legal-level level 1) ?*))
  3173. (diff (abs (- level (length down-head)))))
  3174. (replace-match down-head nil t)
  3175. ;; Fixup tag positioning
  3176. (and org-auto-align-tags (org-set-tags nil t))
  3177. (if org-adapt-indentation
  3178. (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
  3179. (defun org-map-tree (fun)
  3180. "Call FUN for every heading underneath the current one."
  3181. (org-back-to-heading)
  3182. (let ((level (funcall outline-level)))
  3183. (save-excursion
  3184. (funcall fun)
  3185. (while (and (progn
  3186. (outline-next-heading)
  3187. (> (funcall outline-level) level))
  3188. (not (eobp)))
  3189. (funcall fun)))))
  3190. (defun org-map-region (fun beg end)
  3191. "Call FUN for every heading between BEG and END."
  3192. (let ((org-ignore-region t))
  3193. (save-excursion
  3194. (setq end (copy-marker end))
  3195. (goto-char beg)
  3196. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3197. (< (point) end))
  3198. (funcall fun))
  3199. (while (and (progn
  3200. (outline-next-heading)
  3201. (< (point) end))
  3202. (not (eobp)))
  3203. (funcall fun)))))
  3204. (defun org-fixup-indentation (from to prohibit)
  3205. "Change the indentation in the current entry by re-replacing FROM with TO.
  3206. However, if the regexp PROHIBIT matches at all, don't do anything.
  3207. This is being used to change indentation along with the length of the
  3208. heading marker. But if there are any lines which are not indented, nothing
  3209. is changed at all."
  3210. (save-excursion
  3211. (let ((end (save-excursion (outline-next-heading)
  3212. (point-marker))))
  3213. (unless (save-excursion (re-search-forward prohibit end t))
  3214. (while (re-search-forward from end t)
  3215. (replace-match to)
  3216. (beginning-of-line 2)))
  3217. (move-marker end nil))))
  3218. ;;; Vertical tree motion, cutting and pasting of subtrees
  3219. (defun org-move-subtree-up (&optional arg)
  3220. "Move the current subtree up past ARG headlines of the same level."
  3221. (interactive "p")
  3222. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3223. (defun org-move-subtree-down (&optional arg)
  3224. "Move the current subtree down past ARG headlines of the same level."
  3225. (interactive "p")
  3226. (setq arg (prefix-numeric-value arg))
  3227. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3228. 'outline-get-last-sibling))
  3229. (ins-point (make-marker))
  3230. (cnt (abs arg))
  3231. beg end txt folded)
  3232. ;; Select the tree
  3233. (org-back-to-heading)
  3234. (setq beg (point))
  3235. (save-match-data
  3236. (save-excursion (outline-end-of-heading)
  3237. (setq folded (org-invisible-p)))
  3238. (outline-end-of-subtree))
  3239. (outline-next-heading)
  3240. (setq end (point))
  3241. ;; Find insertion point, with error handling
  3242. (goto-char beg)
  3243. (while (> cnt 0)
  3244. (or (and (funcall movfunc) (looking-at outline-regexp))
  3245. (progn (goto-char beg)
  3246. (error "Cannot move past superior level or buffer limit")))
  3247. (setq cnt (1- cnt)))
  3248. (if (> arg 0)
  3249. ;; Moving forward - still need to move over subtree
  3250. (progn (outline-end-of-subtree)
  3251. (outline-next-heading)
  3252. (if (not (or (looking-at (concat "^" outline-regexp))
  3253. (bolp)))
  3254. (newline))))
  3255. (move-marker ins-point (point))
  3256. (setq txt (buffer-substring beg end))
  3257. (delete-region beg end)
  3258. (insert txt)
  3259. (goto-char ins-point)
  3260. (if folded (hide-subtree))
  3261. (move-marker ins-point nil)))
  3262. (defvar org-subtree-clip ""
  3263. "Clipboard for cut and paste of subtrees.
  3264. This is actually only a copy of the kill, because we use the normal kill
  3265. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3266. (defvar org-subtree-clip-folded nil
  3267. "Was the last copied subtree folded?
  3268. This is used to fold the tree back after pasting.")
  3269. (defun org-cut-subtree ()
  3270. "Cut the current subtree into the clipboard.
  3271. This is a short-hand for marking the subtree and then cutting it."
  3272. (interactive)
  3273. (org-copy-subtree 'cut))
  3274. (defun org-copy-subtree (&optional cut)
  3275. "Cut the current subtree into the clipboard.
  3276. This is a short-hand for marking the subtree and then copying it.
  3277. If CUT is non nil, actually cut the subtree."
  3278. (interactive)
  3279. (let (beg end folded)
  3280. (org-back-to-heading)
  3281. (setq beg (point))
  3282. (save-match-data
  3283. (save-excursion (outline-end-of-heading)
  3284. (setq folded (org-invisible-p)))
  3285. (outline-end-of-subtree))
  3286. (if (equal (char-after) ?\n) (forward-char 1))
  3287. (setq end (point))
  3288. (goto-char beg)
  3289. (when (> end beg)
  3290. (setq org-subtree-clip-folded folded)
  3291. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3292. (setq org-subtree-clip (current-kill 0))
  3293. (message "%s: Subtree with %d characters"
  3294. (if cut "Cut" "Copied")
  3295. (length org-subtree-clip)))))
  3296. (defun org-paste-subtree (&optional level tree)
  3297. "Paste the clipboard as a subtree, with modification of headline level.
  3298. The entire subtree is promoted or demoted in order to match a new headline
  3299. level. By default, the new level is derived from the visible headings
  3300. before and after the insertion point, and taken to be the inferior headline
  3301. level of the two. So if the previous visible heading is level 3 and the
  3302. next is level 4 (or vice versa), level 4 will be used for insertion.
  3303. This makes sure that the subtree remains an independent subtree and does
  3304. not swallow low level entries.
  3305. You can also force a different level, either by using a numeric prefix
  3306. argument, or by inserting the heading marker by hand. For example, if the
  3307. cursor is after \"*****\", then the tree will be shifted to level 5.
  3308. If you want to insert the tree as is, just use \\[yank].
  3309. If optional TREE is given, use this text instead of the kill ring."
  3310. (interactive "P")
  3311. (unless (org-kill-is-subtree-p tree)
  3312. (error
  3313. (substitute-command-keys
  3314. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3315. (let* ((txt (or tree (current-kill 0)))
  3316. (^re (concat "^\\(" outline-regexp "\\)"))
  3317. (re (concat "\\(" outline-regexp "\\)"))
  3318. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3319. (old-level (if (string-match ^re txt)
  3320. (- (match-end 0) (match-beginning 0))
  3321. -1))
  3322. (force-level (cond (level (prefix-numeric-value level))
  3323. ((string-match
  3324. ^re_ (buffer-substring (point-at-bol) (point)))
  3325. (- (match-end 0) (match-beginning 0)))
  3326. (t nil)))
  3327. (previous-level (save-excursion
  3328. (condition-case nil
  3329. (progn
  3330. (outline-previous-visible-heading 1)
  3331. (if (looking-at re)
  3332. (- (match-end 0) (match-beginning 0))
  3333. 1))
  3334. (error 1))))
  3335. (next-level (save-excursion
  3336. (condition-case nil
  3337. (progn
  3338. (outline-next-visible-heading 1)
  3339. (if (looking-at re)
  3340. (- (match-end 0) (match-beginning 0))
  3341. 1))
  3342. (error 1))))
  3343. (new-level (or force-level (max previous-level next-level)))
  3344. (shift (if (or (= old-level -1)
  3345. (= new-level -1)
  3346. (= old-level new-level))
  3347. 0
  3348. (- new-level old-level)))
  3349. (shift1 shift)
  3350. (delta (if (> shift 0) -1 1))
  3351. (func (if (> shift 0) 'org-demote 'org-promote))
  3352. (org-odd-levels-only nil)
  3353. beg end)
  3354. ;; Remove the forces level indicator
  3355. (if force-level
  3356. (delete-region (point-at-bol) (point)))
  3357. ;; Make sure we start at the beginning of an empty line
  3358. (if (not (bolp)) (insert "\n"))
  3359. (if (not (looking-at "[ \t]*$"))
  3360. (progn (insert "\n") (backward-char 1)))
  3361. ;; Paste
  3362. (setq beg (point))
  3363. (insert txt)
  3364. (setq end (point))
  3365. (goto-char beg)
  3366. ;; Shift if necessary
  3367. (if (= shift 0)
  3368. (message "Pasted at level %d, without shift" new-level)
  3369. (save-restriction
  3370. (narrow-to-region beg end)
  3371. (while (not (= shift 0))
  3372. (org-map-region func (point-min) (point-max))
  3373. (setq shift (+ delta shift)))
  3374. (goto-char (point-min))
  3375. (message "Pasted at level %d, with shift by %d levels"
  3376. new-level shift1)))
  3377. (if (and (eq org-subtree-clip (current-kill 0))
  3378. org-subtree-clip-folded)
  3379. ;; The tree was folded before it was killed/copied
  3380. (hide-subtree))))
  3381. (defun org-kill-is-subtree-p (&optional txt)
  3382. "Check if the current kill is an outline subtree, or a set of trees.
  3383. Returns nil if kill does not start with a headline, or if the first
  3384. headline level is not the largest headline level in the tree.
  3385. So this will actually accept several entries of equal levels as well,
  3386. which is OK for `org-paste-subtree'.
  3387. If optional TXT is given, check this string instead of the current kill."
  3388. (let* ((kill (or txt (current-kill 0) ""))
  3389. (start-level (and (string-match (concat "\\`" outline-regexp) kill)
  3390. (- (match-end 0) (match-beginning 0))))
  3391. (re (concat "^" outline-regexp))
  3392. (start 1))
  3393. (if (not start-level)
  3394. nil ;; does not even start with a heading
  3395. (catch 'exit
  3396. (while (setq start (string-match re kill (1+ start)))
  3397. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3398. (throw 'exit nil)))
  3399. t))))
  3400. ;;; Plain list items
  3401. (defun org-at-item-p ()
  3402. "Is point in a line starting a hand-formatted item?"
  3403. (let ((llt org-plain-list-ordered-item-terminator))
  3404. (save-excursion
  3405. (goto-char (point-at-bol))
  3406. (looking-at
  3407. (cond
  3408. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3409. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3410. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3411. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3412. (defun org-at-item-checkbox-p ()
  3413. "Is point at a line starting a plain-list item with a checklet?"
  3414. (and (org-at-item-p)
  3415. (save-excursion
  3416. (goto-char (match-end 0))
  3417. (skip-chars-forward " \t")
  3418. (looking-at "\\[[ X]\\]"))))
  3419. (defun org-toggle-checkbox ()
  3420. "Toggle the checkbox in the current line."
  3421. (interactive)
  3422. (save-excursion
  3423. (if (org-at-item-checkbox-p)
  3424. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))))
  3425. (defun org-get-indentation ()
  3426. "Get the indentation of the current line, interpreting tabs."
  3427. (save-excursion
  3428. (beginning-of-line 1)
  3429. (skip-chars-forward " \t")
  3430. (current-column)))
  3431. (defun org-beginning-of-item ()
  3432. "Go to the beginning of the current hand-formatted item.
  3433. If the cursor is not in an item, throw an error."
  3434. (interactive)
  3435. (let ((pos (point))
  3436. (limit (save-excursion (org-back-to-heading)
  3437. (beginning-of-line 2) (point)))
  3438. ind ind1)
  3439. (if (org-at-item-p)
  3440. (beginning-of-line 1)
  3441. (beginning-of-line 1)
  3442. (skip-chars-forward " \t")
  3443. (setq ind (current-column))
  3444. (if (catch 'exit
  3445. (while t
  3446. (beginning-of-line 0)
  3447. (if (< (point) limit) (throw 'exit nil))
  3448. (unless (looking-at " \t]*$")
  3449. (skip-chars-forward " \t")
  3450. (setq ind1 (current-column))
  3451. (if (< ind1 ind)
  3452. (throw 'exit (org-at-item-p))))))
  3453. nil
  3454. (goto-char pos)
  3455. (error "Not in an item")))))
  3456. (defun org-end-of-item ()
  3457. "Go to the end of the current hand-formatted item.
  3458. If the cursor is not in an item, throw an error."
  3459. (interactive)
  3460. (let ((pos (point))
  3461. (limit (save-excursion (outline-next-heading) (point)))
  3462. (ind (save-excursion
  3463. (org-beginning-of-item)
  3464. (skip-chars-forward " \t")
  3465. (current-column)))
  3466. ind1)
  3467. (if (catch 'exit
  3468. (while t
  3469. (beginning-of-line 2)
  3470. (if (>= (point) limit) (throw 'exit t))
  3471. (unless (looking-at "[ \t]*$")
  3472. (skip-chars-forward " \t")
  3473. (setq ind1 (current-column))
  3474. (if (<= ind1 ind) (throw 'exit t)))))
  3475. (beginning-of-line 1)
  3476. (goto-char pos)
  3477. (error "Not in an item"))))
  3478. (defun org-next-item ()
  3479. "Move to the beginning of the next item in the current plain list.
  3480. Error if not at a plain list, or if this is the last item in the list."
  3481. (interactive)
  3482. (let (beg end ind ind1 (pos (point)) txt)
  3483. (org-beginning-of-item)
  3484. (setq beg (point))
  3485. (setq ind (org-get-indentation))
  3486. (org-end-of-item)
  3487. (setq end (point))
  3488. (setq ind1 (org-get-indentation))
  3489. (unless (and (org-at-item-p) (= ind ind1))
  3490. (goto-char pos)
  3491. (error "This is already the last item in the list"))))
  3492. (defun org-previous-item ()
  3493. "Move to the beginning of the previous item in the current plain list.
  3494. Error if not at a plain list, or if this is the last item in the list."
  3495. (interactive)
  3496. (let (beg end ind ind1 (pos (point)) txt)
  3497. (org-beginning-of-item)
  3498. (setq beg (point))
  3499. (setq ind (org-get-indentation))
  3500. (goto-char beg)
  3501. (catch 'exit
  3502. (while t
  3503. (beginning-of-line 0)
  3504. (if (looking-at "[ \t]*$")
  3505. nil
  3506. (if (<= (setq ind1 (org-get-indentation)) ind)
  3507. (throw 'exit t)))))
  3508. (condition-case nil
  3509. (org-beginning-of-item)
  3510. (error (goto-char pos)
  3511. (error "This is already the first item in the list")))))
  3512. (defun org-move-item-down ()
  3513. "Move the plain list item at point down, i.e. swap with following item.
  3514. Subitems (items with larger indentation) are considered part of the item,
  3515. so this really moves item trees."
  3516. (interactive)
  3517. (let (beg end ind ind1 (pos (point)) txt)
  3518. (org-beginning-of-item)
  3519. (setq beg (point))
  3520. (setq ind (org-get-indentation))
  3521. (org-end-of-item)
  3522. (setq end (point))
  3523. (setq ind1 (org-get-indentation))
  3524. (if (and (org-at-item-p) (= ind ind1))
  3525. (progn
  3526. (org-end-of-item)
  3527. (setq txt (buffer-substring beg end))
  3528. (save-excursion
  3529. (delete-region beg end))
  3530. (setq pos (point))
  3531. (insert txt)
  3532. (goto-char pos)
  3533. (org-maybe-renumber-ordered-list))
  3534. (goto-char pos)
  3535. (error "Cannot move this item further down"))))
  3536. (defun org-move-item-up (arg)
  3537. "Move the plain list item at point up, i.e. swap with previous item.
  3538. Subitems (items with larger indentation) are considered part of the item,
  3539. so this really moves item trees."
  3540. (interactive "p")
  3541. (let (beg end ind ind1 (pos (point)) txt)
  3542. (org-beginning-of-item)
  3543. (setq beg (point))
  3544. (setq ind (org-get-indentation))
  3545. (org-end-of-item)
  3546. (setq end (point))
  3547. (goto-char beg)
  3548. (catch 'exit
  3549. (while t
  3550. (beginning-of-line 0)
  3551. (if (looking-at "[ \t]*$")
  3552. nil
  3553. (if (<= (setq ind1 (org-get-indentation)) ind)
  3554. (throw 'exit t)))))
  3555. (condition-case nil
  3556. (org-beginning-of-item)
  3557. (error (goto-char beg)
  3558. (error "Cannot move this item further up")))
  3559. (setq ind1 (org-get-indentation))
  3560. (if (and (org-at-item-p) (= ind ind1))
  3561. (progn
  3562. (setq txt (buffer-substring beg end))
  3563. (save-excursion
  3564. (delete-region beg end))
  3565. (setq pos (point))
  3566. (insert txt)
  3567. (goto-char pos)
  3568. (org-maybe-renumber-ordered-list))
  3569. (goto-char pos)
  3570. (error "Cannot move this item further up"))))
  3571. (defun org-maybe-renumber-ordered-list ()
  3572. "Renumber the ordered list at point if setup allows it.
  3573. This tests the user option `org-auto-renumber-ordered-lists' before
  3574. doing the renumbering."
  3575. (and org-auto-renumber-ordered-lists
  3576. (org-at-item-p)
  3577. (match-beginning 3)
  3578. (org-renumber-ordered-list 1)))
  3579. (defun org-get-string-indentation (s)
  3580. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  3581. (let ((n -1) (i 0) (w tab-width) c)
  3582. (catch 'exit
  3583. (while (< (setq n (1+ n)) (length s))
  3584. (setq c (aref s n))
  3585. (cond ((= c ?\ ) (setq i (1+ i)))
  3586. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  3587. (t (throw 'exit t)))))
  3588. i))
  3589. (defun org-renumber-ordered-list (arg)
  3590. "Renumber an ordered plain list.
  3591. Cursor needs to be in the first line of an item, the line that starts
  3592. with something like \"1.\" or \"2)\"."
  3593. (interactive "p")
  3594. (unless (and (org-at-item-p)
  3595. (match-beginning 3))
  3596. (error "This is not an ordered list"))
  3597. (let ((line (org-current-line))
  3598. (col (current-column))
  3599. (ind (org-get-string-indentation
  3600. (buffer-substring (point-at-bol) (match-beginning 3))))
  3601. ;; (term (substring (match-string 3) -1))
  3602. ind1 (n (1- arg)))
  3603. ;; find where this list begins
  3604. (catch 'exit
  3605. (while t
  3606. (catch 'next
  3607. (beginning-of-line 0)
  3608. (if (looking-at "[ \t]*$") (throw 'next t))
  3609. (skip-chars-forward " \t") (setq ind1 (current-column))
  3610. (if (or (< ind1 ind)
  3611. (and (= ind1 ind)
  3612. (not (org-at-item-p))))
  3613. (throw 'exit t)))))
  3614. ;; Walk forward and replace these numbers
  3615. (catch 'exit
  3616. (while t
  3617. (catch 'next
  3618. (beginning-of-line 2)
  3619. (if (eobp) (throw 'exit nil))
  3620. (if (looking-at "[ \t]*$") (throw 'next nil))
  3621. (skip-chars-forward " \t") (setq ind1 (current-column))
  3622. (if (> ind1 ind) (throw 'next t))
  3623. (if (< ind1 ind) (throw 'exit t))
  3624. (if (not (org-at-item-p)) (throw 'exit nil))
  3625. (if (not (match-beginning 3))
  3626. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  3627. (delete-region (match-beginning 3) (1- (match-end 3)))
  3628. (goto-char (match-beginning 3))
  3629. (insert (format "%d" (setq n (1+ n)))))))
  3630. (goto-line line)
  3631. (move-to-column col)))
  3632. (defvar org-last-indent-begin-marker (make-marker))
  3633. (defvar org-last-indent-end-marker (make-marker))
  3634. (defun org-outdent-item (arg)
  3635. "Outdent a local list item."
  3636. (interactive "p")
  3637. (org-indent-item (- arg)))
  3638. (defun org-indent-item (arg)
  3639. "Indent a local list item."
  3640. (interactive "p")
  3641. (unless (org-at-item-p)
  3642. (error "Not on an item"))
  3643. (save-excursion
  3644. (let (beg end ind ind1)
  3645. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  3646. (setq beg org-last-indent-begin-marker
  3647. end org-last-indent-end-marker)
  3648. (org-beginning-of-item)
  3649. (setq beg (move-marker org-last-indent-begin-marker (point)))
  3650. (org-end-of-item)
  3651. (setq end (move-marker org-last-indent-end-marker (point))))
  3652. (goto-char beg)
  3653. (skip-chars-forward " \t") (setq ind (current-column))
  3654. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  3655. (while (< (point) end)
  3656. (beginning-of-line 1)
  3657. (skip-chars-forward " \t") (setq ind1 (current-column))
  3658. (delete-region (point-at-bol) (point))
  3659. (indent-to-column (+ ind1 arg))
  3660. (beginning-of-line 2)))))
  3661. ;;; Archiving
  3662. (defun org-archive-subtree ()
  3663. "Move the current subtree to the archive.
  3664. The archive can be a certain top-level heading in the current file, or in
  3665. a different file. The tree will be moved to that location, the subtree
  3666. heading be marked DONE, and the current time will be added."
  3667. (interactive)
  3668. ;; Save all relevant TODO keyword-relatex variables
  3669. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  3670. (tr-org-todo-keywords org-todo-keywords)
  3671. (tr-org-todo-interpretation org-todo-interpretation)
  3672. (tr-org-done-string org-done-string)
  3673. (tr-org-todo-regexp org-todo-regexp)
  3674. (tr-org-todo-line-regexp org-todo-line-regexp)
  3675. (this-buffer (current-buffer))
  3676. file heading buffer level newfile-p)
  3677. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  3678. (progn
  3679. (setq file (format (match-string 1 org-archive-location)
  3680. (file-name-nondirectory buffer-file-name))
  3681. heading (match-string 2 org-archive-location)))
  3682. (error "Invalid `org-archive-location'"))
  3683. (if (> (length file) 0)
  3684. (setq newfile-p (not (file-exists-p file))
  3685. buffer (find-file-noselect file))
  3686. (setq buffer (current-buffer)))
  3687. (unless buffer
  3688. (error "Cannot access file \"%s\"" file))
  3689. (if (and (> (length heading) 0)
  3690. (string-match "^\\*+" heading))
  3691. (setq level (match-end 0))
  3692. (setq heading nil level 0))
  3693. (save-excursion
  3694. ;; We first only copy, in case something goes wrong
  3695. ;; we need to protect this-command, to avoid kill-region sets it,
  3696. ;; which would lead to duplication of subtrees
  3697. (let (this-command) (org-copy-subtree))
  3698. (set-buffer buffer)
  3699. ;; Enforce org-mode for the archive buffer
  3700. (if (not (eq major-mode 'org-mode))
  3701. ;; Force the mode for future visits.
  3702. (let ((org-insert-mode-line-in-empty-file t))
  3703. (call-interactively 'org-mode)))
  3704. (when newfile-p
  3705. (goto-char (point-max))
  3706. (insert (format "\nArchived entries from file %s\n\n"
  3707. (buffer-file-name this-buffer))))
  3708. ;; Force the TODO keywords of the original buffer
  3709. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  3710. (org-todo-keywords tr-org-todo-keywords)
  3711. (org-todo-interpretation tr-org-todo-interpretation)
  3712. (org-done-string tr-org-done-string)
  3713. (org-todo-regexp tr-org-todo-regexp)
  3714. (org-todo-line-regexp tr-org-todo-line-regexp))
  3715. (goto-char (point-min))
  3716. (if heading
  3717. (progn
  3718. (if (re-search-forward
  3719. (concat "\\(^\\|\r\\)"
  3720. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  3721. nil t)
  3722. (goto-char (match-end 0))
  3723. ;; Heading not found, just insert it at the end
  3724. (goto-char (point-max))
  3725. (or (bolp) (insert "\n"))
  3726. (insert "\n" heading "\n")
  3727. (end-of-line 0))
  3728. ;; Make the heading visible, and the following as well
  3729. (let ((org-show-following-heading t)) (org-show-hierarchy-above))
  3730. (if (re-search-forward
  3731. (concat "^" (regexp-quote (make-string level ?*)) "[ \t]")
  3732. nil t)
  3733. (progn (goto-char (match-beginning 0)) (insert "\n")
  3734. (beginning-of-line 0))
  3735. (goto-char (point-max)) (insert "\n")))
  3736. (goto-char (point-max)) (insert "\n"))
  3737. ;; Paste
  3738. (org-paste-subtree (1+ level))
  3739. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  3740. (if org-archive-mark-done
  3741. (org-todo (length org-todo-keywords)))
  3742. ;; Move cursor to right after the TODO keyword
  3743. (when org-archive-stamp-time
  3744. (beginning-of-line 1)
  3745. (looking-at org-todo-line-regexp)
  3746. (goto-char (or (match-end 2) (match-beginning 3)))
  3747. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  3748. (org-current-time))
  3749. ")"))
  3750. ;; Save the buffer, if it is not the same buffer.
  3751. (if (not (eq this-buffer buffer)) (save-buffer))))
  3752. ;; Here we are back in the original buffer. Everything seems to have
  3753. ;; worked. So now cut the tree and finish up.
  3754. (let (this-command) (org-cut-subtree))
  3755. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  3756. (message "Subtree archived %s"
  3757. (if (eq this-buffer buffer)
  3758. (concat "under heading: " heading)
  3759. (concat "in file: " (abbreviate-file-name file))))))
  3760. ;;; Completion
  3761. (defun org-complete (&optional arg)
  3762. "Perform completion on word at point.
  3763. At the beginning of a headline, this completes TODO keywords as given in
  3764. `org-todo-keywords'.
  3765. If the current word is preceded by a backslash, completes the TeX symbols
  3766. that are supported for HTML support.
  3767. If the current word is preceded by \"#+\", completes special words for
  3768. setting file options.
  3769. At all other locations, this simply calls `ispell-complete-word'."
  3770. (interactive "P")
  3771. (catch 'exit
  3772. (let* ((end (point))
  3773. (beg1 (save-excursion
  3774. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3775. (skip-chars-backward "a-zA-Z_@0-9")
  3776. (point)))
  3777. (beg (save-excursion
  3778. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3779. (skip-chars-backward "a-zA-Z0-9_:$")
  3780. (point)))
  3781. (camel (equal (char-before beg) ?*))
  3782. (tag (equal (char-before beg1) ?:))
  3783. (texp (equal (char-before beg) ?\\))
  3784. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  3785. beg)
  3786. "#+"))
  3787. (completion-ignore-case opt)
  3788. (type nil)
  3789. (tbl nil)
  3790. (table (cond
  3791. (opt
  3792. (setq type :opt)
  3793. (mapcar (lambda (x)
  3794. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  3795. (cons (match-string 2 x) (match-string 1 x)))
  3796. (org-split-string (org-get-current-options) "\n")))
  3797. (texp
  3798. (setq type :tex)
  3799. org-html-entities)
  3800. ((string-match "\\`\\*+[ \t]*\\'"
  3801. (buffer-substring (point-at-bol) beg))
  3802. (setq type :todo)
  3803. (mapcar 'list org-todo-keywords))
  3804. (camel
  3805. (setq type :camel)
  3806. (save-excursion
  3807. (goto-char (point-min))
  3808. (while (re-search-forward org-todo-line-regexp nil t)
  3809. (push (list
  3810. (if org-file-link-context-use-camel-case
  3811. (org-make-org-heading-camel (match-string 3) t)
  3812. (org-make-org-heading-search-string
  3813. (match-string 3) t)))
  3814. tbl)))
  3815. tbl)
  3816. (tag (setq type :tag beg beg1)
  3817. (or org-tag-alist (org-get-buffer-tags)))
  3818. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  3819. (pattern (buffer-substring-no-properties beg end))
  3820. (completion (try-completion pattern table)))
  3821. (cond ((eq completion t)
  3822. (if (equal type :opt)
  3823. (insert (substring (cdr (assoc (upcase pattern) table))
  3824. (length pattern)))))
  3825. ((null completion)
  3826. (message "Can't find completion for \"%s\"" pattern)
  3827. (ding))
  3828. ((not (string= pattern completion))
  3829. (delete-region beg end)
  3830. (if (string-match " +$" completion)
  3831. (setq completion (replace-match "" t t completion)))
  3832. (insert completion)
  3833. (if (get-buffer-window "*Completions*")
  3834. (delete-window (get-buffer-window "*Completions*")))
  3835. (if (assoc completion table)
  3836. (if (eq type :todo) (insert " ")
  3837. (if (eq type :tag) (insert ":"))))
  3838. (if (and (equal type :opt) (assoc completion table))
  3839. (message "%s" (substitute-command-keys
  3840. "Press \\[org-complete] again to insert example settings"))))
  3841. (t
  3842. (message "Making completion list...")
  3843. (let ((list (sort (all-completions pattern table) 'string<)))
  3844. (with-output-to-temp-buffer "*Completions*"
  3845. (condition-case nil
  3846. ;; Protection needed for XEmacs and emacs 21
  3847. (display-completion-list list pattern)
  3848. (error (display-completion-list list)))))
  3849. (message "Making completion list...%s" "done"))))))
  3850. ;;; Comments, TODO and DEADLINE
  3851. (defun org-toggle-comment ()
  3852. "Change the COMMENT state of an entry."
  3853. (interactive)
  3854. (save-excursion
  3855. (org-back-to-heading)
  3856. (if (looking-at (concat outline-regexp
  3857. "\\( +\\<" org-comment-string "\\>\\)"))
  3858. (replace-match "" t t nil 1)
  3859. (if (looking-at outline-regexp)
  3860. (progn
  3861. (goto-char (match-end 0))
  3862. (insert " " org-comment-string))))))
  3863. (defvar org-last-todo-state-is-todo nil
  3864. "This is non-nil when the last TODO state change led to a TODO state.
  3865. If the last change removed the TODO tag or switched to DONE, then
  3866. this is nil.")
  3867. (defun org-todo (&optional arg)
  3868. "Change the TODO state of an item.
  3869. The state of an item is given by a keyword at the start of the heading,
  3870. like
  3871. *** TODO Write paper
  3872. *** DONE Call mom
  3873. The different keywords are specified in the variable `org-todo-keywords'.
  3874. By default the available states are \"TODO\" and \"DONE\".
  3875. So for this example: when the item starts with TODO, it is changed to DONE.
  3876. When it starts with DONE, the DONE is removed. And when neither TODO nor
  3877. DONE are present, add TODO at the beginning of the heading.
  3878. With prefix arg, use completion to determine the new state. With numeric
  3879. prefix arg, switch to that state."
  3880. (interactive "P")
  3881. (save-excursion
  3882. (org-back-to-heading)
  3883. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  3884. (or (looking-at (concat " +" org-todo-regexp " *"))
  3885. (looking-at " *"))
  3886. (let* ((this (match-string 1))
  3887. (completion-ignore-case t)
  3888. (member (member this org-todo-keywords))
  3889. (tail (cdr member))
  3890. (state (cond
  3891. ((equal arg '(4))
  3892. ;; Read a state with completion
  3893. (completing-read "State: " (mapcar (lambda(x) (list x))
  3894. org-todo-keywords)
  3895. nil t))
  3896. ((eq arg 'right)
  3897. (if this
  3898. (if tail (car tail) nil)
  3899. (car org-todo-keywords)))
  3900. ((eq arg 'left)
  3901. (if (equal member org-todo-keywords)
  3902. nil
  3903. (if this
  3904. (nth (- (length org-todo-keywords) (length tail) 2)
  3905. org-todo-keywords)
  3906. org-done-string)))
  3907. (arg
  3908. ;; user requests a specific state
  3909. (nth (1- (prefix-numeric-value arg))
  3910. org-todo-keywords))
  3911. ((null member) (car org-todo-keywords))
  3912. ((null tail) nil) ;; -> first entry
  3913. ((eq org-todo-interpretation 'sequence)
  3914. (car tail))
  3915. ((memq org-todo-interpretation '(type priority))
  3916. (if (eq this-command last-command)
  3917. (car tail)
  3918. (if (> (length tail) 0) org-done-string nil)))
  3919. (t nil)))
  3920. (next (if state (concat " " state " ") " ")))
  3921. (replace-match next t t)
  3922. (setq org-last-todo-state-is-todo
  3923. (not (equal state org-done-string)))
  3924. (when org-log-done
  3925. (if (equal state org-done-string)
  3926. (org-add-planning-info 'closed (current-time) 'scheduled)
  3927. (if (not this)
  3928. (org-add-planning-info nil nil 'closed))))
  3929. ;; Fixup tag positioning
  3930. (and org-auto-align-tags (org-set-tags nil t))
  3931. (run-hooks 'org-after-todo-state-change-hook)))
  3932. ;; Fixup cursor location if close to the keyword
  3933. (if (and (outline-on-heading-p)
  3934. (not (bolp))
  3935. (save-excursion (beginning-of-line 1)
  3936. (looking-at org-todo-line-regexp))
  3937. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  3938. (progn
  3939. (goto-char (or (match-end 2) (match-end 1)))
  3940. (just-one-space))))
  3941. (defun org-log-done (&optional undone)
  3942. "Add a time stamp logging that a TODO entry has been closed.
  3943. When UNDONE is non-nil, remove such a time stamp again."
  3944. (interactive)
  3945. (let (beg end col)
  3946. (save-excursion
  3947. (org-back-to-heading t)
  3948. (setq beg (point))
  3949. (looking-at (concat outline-regexp " *"))
  3950. (goto-char (match-end 0))
  3951. (setq col (current-column))
  3952. (outline-next-heading)
  3953. (setq end (point))
  3954. (goto-char beg)
  3955. (when (re-search-forward (concat
  3956. "[\r\n]\\([ \t]*"
  3957. (regexp-quote org-closed-string)
  3958. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  3959. (delete-region (match-beginning 1) (match-end 1)))
  3960. (unless undone
  3961. (org-back-to-heading t)
  3962. (skip-chars-forward "^\n\r")
  3963. (goto-char (min (1+ (point)) (point-max)))
  3964. (when (not (member (char-before) '(?\r ?\n)))
  3965. (insert "\n"))
  3966. (indent-to col)
  3967. (insert org-closed-string " "
  3968. (format-time-string
  3969. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  3970. (org-current-time))
  3971. "\n")))))
  3972. (defun org-show-todo-tree (arg)
  3973. "Make a compact tree which shows all headlines marked with TODO.
  3974. The tree will show the lines where the regexp matches, and all higher
  3975. headlines above the match.
  3976. With \\[universal-argument] prefix, also show the DONE entries.
  3977. With a numeric prefix N, construct a sparse tree for the Nth element
  3978. of `org-todo-keywords'."
  3979. (interactive "P")
  3980. (let ((case-fold-search nil)
  3981. (kwd-re
  3982. (cond ((null arg) org-not-done-regexp)
  3983. ((equal arg '(4)) org-todo-regexp)
  3984. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  3985. (regexp-quote (nth (1- (prefix-numeric-value arg))
  3986. org-todo-keywords)))
  3987. (t (error "Invalid prefix argument: %s" arg)))))
  3988. (message "%d TODO entries found"
  3989. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  3990. (defun org-deadline ()
  3991. "Insert the DEADLINE: string to make a deadline.
  3992. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3993. to modify it to the correct date."
  3994. (interactive)
  3995. (org-add-planning-info 'deadline nil 'closed))
  3996. (defun org-schedule ()
  3997. "Insert the SCHEDULED: string to schedule a TODO item.
  3998. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3999. to modify it to the correct date."
  4000. (interactive)
  4001. (org-add-planning-info 'scheduled nil 'closed))
  4002. (defun org-add-planning-info (what &optional time &rest remove)
  4003. "Insert new timestamp with keyword in the line directly after the headline.
  4004. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  4005. If non is given, the user is prompted for a date.
  4006. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  4007. be removed."
  4008. (interactive)
  4009. (when what (setq time (or time (org-read-date nil 'to-time))))
  4010. (when (and org-insert-labeled-timestamps-at-point
  4011. (member what '(scheduled deadline)))
  4012. (insert
  4013. (if (eq what 'scheduled) org-scheduled-string org-deadline-string)
  4014. " "
  4015. (format-time-string (car org-time-stamp-formats) time))
  4016. (setq what nil))
  4017. (save-excursion
  4018. (let (beg end col list elt (buffer-invisibility-spec nil) ts)
  4019. (org-back-to-heading t)
  4020. (setq beg (point))
  4021. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  4022. (goto-char (match-end 1))
  4023. (setq col (current-column))
  4024. (goto-char (1+ (match-end 0)))
  4025. (if (and (not (looking-at outline-regexp))
  4026. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  4027. "[^\r\n]*")))
  4028. (narrow-to-region (match-beginning 0) (match-end 0))
  4029. (insert "\n")
  4030. (backward-char 1)
  4031. (narrow-to-region (point) (point))
  4032. (indent-to-column col))
  4033. ;; Check if we have to remove something.
  4034. (setq list (cons what remove))
  4035. (while list
  4036. (setq elt (pop list))
  4037. (goto-char (point-min))
  4038. (when (or (and (eq elt 'scheduled)
  4039. (re-search-forward org-scheduled-time-regexp nil t))
  4040. (and (eq elt 'deadline)
  4041. (re-search-forward org-deadline-time-regexp nil t))
  4042. (and (eq elt 'closed)
  4043. (re-search-forward org-closed-time-regexp nil t)))
  4044. (replace-match "")
  4045. (if (looking-at " +") (replace-match ""))))
  4046. (goto-char (point-max))
  4047. (when what
  4048. (insert
  4049. (if (not (equal (char-before) ?\ )) " " "")
  4050. (cond ((eq what 'scheduled) org-scheduled-string)
  4051. ((eq what 'deadline) org-deadline-string)
  4052. ((eq what 'closed) org-closed-string))
  4053. " ")
  4054. (insert
  4055. (setq ts
  4056. (format-time-string
  4057. (if (eq what 'closed)
  4058. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4059. (car org-time-stamp-formats))
  4060. time))))
  4061. (goto-char (point-min))
  4062. (widen)
  4063. (if (looking-at "[ \t]+\r?\n")
  4064. (replace-match ""))
  4065. ts)))
  4066. (defun org-occur (regexp &optional callback)
  4067. "Make a compact tree which shows all matches of REGEXP.
  4068. The tree will show the lines where the regexp matches, and all higher
  4069. headlines above the match. It will also show the heading after the match,
  4070. to make sure editing the matching entry is easy.
  4071. If CALLBACK is non-nil, it is a function which is called to confirm
  4072. that the match should indeed be shown."
  4073. (interactive "sRegexp: ")
  4074. (org-remove-occur-highlights nil nil t)
  4075. (setq regexp (org-check-occur-regexp regexp))
  4076. (let ((cnt 0))
  4077. (save-excursion
  4078. (goto-char (point-min))
  4079. (org-overview)
  4080. (while (re-search-forward regexp nil t)
  4081. (when (or (not callback)
  4082. (save-match-data (funcall callback)))
  4083. (setq cnt (1+ cnt))
  4084. (org-highlight-new-match (match-beginning 0) (match-end 0))
  4085. (org-show-hierarchy-above))))
  4086. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  4087. nil 'local)
  4088. (run-hooks 'org-occur-hook)
  4089. (if (interactive-p)
  4090. (message "%d match(es) for regexp %s" cnt regexp))
  4091. cnt))
  4092. (defun org-show-hierarchy-above ()
  4093. "Make sure point and the headings hierarchy above is visible."
  4094. (catch 'exit
  4095. (if (org-on-heading-p t)
  4096. (org-flag-heading nil) ; only show the heading
  4097. (and (or (org-invisible-p) (org-invisible-p2))
  4098. (org-show-hidden-entry))) ; show entire entry
  4099. (save-excursion
  4100. (and org-show-following-heading
  4101. (outline-next-heading)
  4102. (org-flag-heading nil))) ; show the next heading
  4103. (when org-show-hierarchy-above
  4104. (save-excursion ; show all higher headings
  4105. (while (and (condition-case nil
  4106. (progn (org-up-heading-all 1) t)
  4107. (error nil))
  4108. (not (bobp)))
  4109. (org-flag-heading nil))))))
  4110. ;; Overlay compatibility functions
  4111. (defun org-make-overlay (beg end &optional buffer)
  4112. (if (featurep 'xemacs)
  4113. (make-extent beg end buffer)
  4114. (make-overlay beg end buffer)))
  4115. (defun org-delete-overlay (ovl)
  4116. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4117. (defun org-detatch-overlay (ovl)
  4118. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4119. (defun org-move-overlay (ovl beg end &optional buffer)
  4120. (if (featurep 'xemacs)
  4121. (set-extent-endpoints ovl beg end buffer)
  4122. (move-overlay ovl beg end buffer)))
  4123. (defun org-overlay-put (ovl prop value)
  4124. (if (featurep 'xemacs)
  4125. (set-extent-property ovl prop value)
  4126. (overlay-put ovl prop value)))
  4127. (defvar org-occur-highlights nil)
  4128. (defun org-highlight-new-match (beg end)
  4129. "Highlight from BEG to END and mark the highlight is an occur headline."
  4130. (let ((ov (org-make-overlay beg end)))
  4131. (org-overlay-put ov 'face 'secondary-selection)
  4132. (push ov org-occur-highlights)))
  4133. (defun org-remove-occur-highlights (&optional beg end noremove)
  4134. "Remove the occur highlights from the buffer.
  4135. BEG and END are ignored. If NOREMOVE is nil, remove this function
  4136. from the `before-change-functions' in the current buffer."
  4137. (interactive)
  4138. (mapc 'org-delete-overlay org-occur-highlights)
  4139. (setq org-occur-highlights nil)
  4140. (unless noremove
  4141. (remove-hook 'before-change-functions
  4142. 'org-remove-occur-highlights 'local)))
  4143. ;;; Priorities
  4144. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  4145. "Regular expression matching the priority indicator.")
  4146. (defvar org-remove-priority-next-time nil)
  4147. (defun org-priority-up ()
  4148. "Increase the priority of the current item."
  4149. (interactive)
  4150. (org-priority 'up))
  4151. (defun org-priority-down ()
  4152. "Decrease the priority of the current item."
  4153. (interactive)
  4154. (org-priority 'down))
  4155. (defun org-priority (&optional action)
  4156. "Change the priority of an item by ARG.
  4157. ACTION can be set, up, or down."
  4158. (interactive)
  4159. (setq action (or action 'set))
  4160. (let (current new news have remove)
  4161. (save-excursion
  4162. (org-back-to-heading)
  4163. (if (looking-at org-priority-regexp)
  4164. (setq current (string-to-char (match-string 2))
  4165. have t)
  4166. (setq current org-default-priority))
  4167. (cond
  4168. ((eq action 'set)
  4169. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  4170. (setq new (read-char-exclusive))
  4171. (cond ((equal new ?\ ) (setq remove t))
  4172. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  4173. (error "Priority must be between `%c' and `%c'"
  4174. ?A org-lowest-priority))))
  4175. ((eq action 'up)
  4176. (setq new (1- current)))
  4177. ((eq action 'down)
  4178. (setq new (1+ current)))
  4179. (t (error "Invalid action")))
  4180. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  4181. (setq news (format "%c" new))
  4182. (if have
  4183. (if remove
  4184. (replace-match "" t t nil 1)
  4185. (replace-match news t t nil 2))
  4186. (if remove
  4187. (error "No priority cookie found in line")
  4188. (looking-at org-todo-line-regexp)
  4189. (if (match-end 2)
  4190. (progn
  4191. (goto-char (match-end 2))
  4192. (insert " [#" news "]"))
  4193. (goto-char (match-beginning 3))
  4194. (insert "[#" news "] ")))))
  4195. (if remove
  4196. (message "Priority removed")
  4197. (message "Priority of current item set to %s" news))))
  4198. (defun org-get-priority (s)
  4199. "Find priority cookie and return priority."
  4200. (save-match-data
  4201. (if (not (string-match org-priority-regexp s))
  4202. (* 1000 (- org-lowest-priority org-default-priority))
  4203. (* 1000 (- org-lowest-priority
  4204. (string-to-char (match-string 2 s)))))))
  4205. ;;; Timestamps
  4206. (defvar org-last-changed-timestamp nil)
  4207. (defun org-time-stamp (arg)
  4208. "Prompt for a date/time and insert a time stamp.
  4209. If the user specifies a time like HH:MM, or if this command is called
  4210. with a prefix argument, the time stamp will contain date and time.
  4211. Otherwise, only the date will be included. All parts of a date not
  4212. specified by the user will be filled in from the current date/time.
  4213. So if you press just return without typing anything, the time stamp
  4214. will represent the current date/time. If there is already a timestamp
  4215. at the cursor, it will be modified."
  4216. (interactive "P")
  4217. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4218. (car org-time-stamp-formats)))
  4219. (org-time-was-given nil)
  4220. time)
  4221. (cond
  4222. ((and (org-at-timestamp-p)
  4223. (eq last-command 'org-time-stamp)
  4224. (eq this-command 'org-time-stamp))
  4225. (insert "--")
  4226. (setq time (let ((this-command this-command))
  4227. (org-read-date arg 'totime)))
  4228. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4229. (insert (format-time-string fmt time)))
  4230. ((org-at-timestamp-p)
  4231. (setq time (let ((this-command this-command))
  4232. (org-read-date arg 'totime)))
  4233. (and (org-at-timestamp-p) (replace-match
  4234. (setq org-last-changed-timestamp
  4235. (format-time-string fmt time))
  4236. t t))
  4237. (message "Timestamp updated"))
  4238. (t
  4239. (setq time (let ((this-command this-command))
  4240. (org-read-date arg 'totime)))
  4241. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4242. (insert (format-time-string fmt time))))))
  4243. (defun org-time-stamp-inactive (&optional arg)
  4244. "Insert an inactive time stamp.
  4245. An inactive time stamp is enclosed in square brackets instead of angle
  4246. brackets. It is inactive in the sense that it does not trigger agenda entries,
  4247. does not link to the calendar and cannot be changed with the S-cursor keys.
  4248. So these are more for recording a certain time/date."
  4249. (interactive "P")
  4250. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4251. (car org-time-stamp-formats)))
  4252. (org-time-was-given nil)
  4253. time)
  4254. (setq time (org-read-date arg 'totime))
  4255. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4256. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  4257. (insert (format-time-string fmt time))))
  4258. (defvar org-date-ovl (org-make-overlay 1 1))
  4259. (org-overlay-put org-date-ovl 'face 'org-warning)
  4260. (org-detatch-overlay org-date-ovl)
  4261. (defun org-read-date (&optional with-time to-time)
  4262. "Read a date and make things smooth for the user.
  4263. The prompt will suggest to enter an ISO date, but you can also enter anything
  4264. which will at least partially be understood by `parse-time-string'.
  4265. Unrecognized parts of the date will default to the current day, month, year,
  4266. hour and minute. For example,
  4267. 3-2-5 --> 2003-02-05
  4268. feb 15 --> currentyear-02-15
  4269. sep 12 9 --> 2009-09-12
  4270. 12:45 --> today 12:45
  4271. 22 sept 0:34 --> currentyear-09-22 0:34
  4272. 12 --> currentyear-currentmonth-12
  4273. Fri --> nearest Friday (today or later)
  4274. etc.
  4275. The function understands only English month and weekday abbreviations,
  4276. but this can be configured with the variables `parse-time-months' and
  4277. `parse-time-weekdays'.
  4278. While prompting, a calendar is popped up - you can also select the
  4279. date with the mouse (button 1). The calendar shows a period of three
  4280. months. To scroll it to other months, use the keys `>' and `<'.
  4281. If you don't like the calendar, turn it off with
  4282. \(setq org-popup-calendar-for-date-prompt nil)
  4283. With optional argument TO-TIME, the date will immediately be converted
  4284. to an internal time.
  4285. With an optional argument WITH-TIME, the prompt will suggest to also
  4286. insert a time. Note that when WITH-TIME is not set, you can still
  4287. enter a time, and this function will inform the calling routine about
  4288. this change. The calling routine may then choose to change the format
  4289. used to insert the time stamp into the buffer to include the time."
  4290. (require 'parse-time)
  4291. (let* ((org-time-stamp-rounding-minutes
  4292. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  4293. (ct (org-current-time))
  4294. (default-time
  4295. ;; Default time is either today, or, when entering a range,
  4296. ;; the range start.
  4297. (if (save-excursion
  4298. (re-search-backward
  4299. (concat org-ts-regexp "--\\=") ; FIXME: exactly two minuses?
  4300. (- (point) 20) t))
  4301. (apply
  4302. 'encode-time
  4303. (mapcar (lambda(x) (or x 0))
  4304. (parse-time-string (match-string 1))))
  4305. ct))
  4306. (calendar-move-hook nil)
  4307. (view-diary-entries-initially nil)
  4308. (view-calendar-holidays-initially nil)
  4309. (timestr (format-time-string
  4310. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  4311. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  4312. ans ans1 ans2
  4313. second minute hour day month year tl wday wday1)
  4314. (if org-popup-calendar-for-date-prompt
  4315. (save-excursion
  4316. (save-window-excursion
  4317. (calendar)
  4318. (calendar-forward-day (- (time-to-days default-time)
  4319. (calendar-absolute-from-gregorian
  4320. (calendar-current-date))))
  4321. (org-eval-in-calendar nil)
  4322. (let* ((old-map (current-local-map))
  4323. (map (copy-keymap calendar-mode-map))
  4324. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  4325. (define-key map (kbd "RET") 'org-calendar-select)
  4326. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  4327. 'org-calendar-select-mouse)
  4328. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  4329. 'org-calendar-select-mouse)
  4330. (define-key minibuffer-local-map [(meta shift left)]
  4331. (lambda () (interactive)
  4332. (org-eval-in-calendar '(calendar-backward-month 1))))
  4333. (define-key minibuffer-local-map [(meta shift right)]
  4334. (lambda () (interactive)
  4335. (org-eval-in-calendar '(calendar-forward-month 1))))
  4336. (define-key minibuffer-local-map [(shift up)]
  4337. (lambda () (interactive)
  4338. (org-eval-in-calendar '(calendar-backward-week 1))))
  4339. (define-key minibuffer-local-map [(shift down)]
  4340. (lambda () (interactive)
  4341. (org-eval-in-calendar '(calendar-forward-week 1))))
  4342. (define-key minibuffer-local-map [(shift left)]
  4343. (lambda () (interactive)
  4344. (org-eval-in-calendar '(calendar-backward-day 1))))
  4345. (define-key minibuffer-local-map [(shift right)]
  4346. (lambda () (interactive)
  4347. (org-eval-in-calendar '(calendar-forward-day 1))))
  4348. (define-key minibuffer-local-map ">"
  4349. (lambda () (interactive)
  4350. (org-eval-in-calendar '(scroll-calendar-left 1))))
  4351. (define-key minibuffer-local-map "<"
  4352. (lambda () (interactive)
  4353. (org-eval-in-calendar '(scroll-calendar-right 1))))
  4354. (unwind-protect
  4355. (progn
  4356. (use-local-map map)
  4357. (setq ans (read-string prompt "" nil nil))
  4358. (if (not (string-match "\\S-" ans)) (setq ans nil))
  4359. (setq ans (or ans1 ans ans2)))
  4360. (use-local-map old-map)))))
  4361. ;; Naked prompt only
  4362. (setq ans (read-string prompt "" nil timestr)))
  4363. (org-detatch-overlay org-date-ovl)
  4364. (if (string-match
  4365. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  4366. (progn
  4367. (setq year (if (match-end 2)
  4368. (string-to-number (match-string 2 ans))
  4369. (string-to-number (format-time-string "%Y")))
  4370. month (string-to-number (match-string 3 ans))
  4371. day (string-to-number (match-string 4 ans)))
  4372. (if (< year 100) (setq year (+ 2000 year)))
  4373. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  4374. t nil ans))))
  4375. (setq tl (parse-time-string ans)
  4376. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  4377. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  4378. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  4379. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  4380. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  4381. second (or (nth 0 tl) 0)
  4382. wday (nth 6 tl))
  4383. (when (and wday (not (nth 3 tl)))
  4384. ;; Weekday was given, but no day, so pick that day in the week
  4385. ;; on or after the derived date.
  4386. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  4387. (unless (equal wday wday1)
  4388. (setq day (+ day (% (- wday wday1 -7) 7)))))
  4389. (if (and (boundp 'org-time-was-given)
  4390. (nth 2 tl))
  4391. (setq org-time-was-given t))
  4392. (if (< year 100) (setq year (+ 2000 year)))
  4393. (if to-time
  4394. (encode-time second minute hour day month year)
  4395. (if (or (nth 1 tl) (nth 2 tl))
  4396. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  4397. (format "%04d-%02d-%02d" year month day)))))
  4398. (defun org-eval-in-calendar (form)
  4399. "Eval FORM in the calendar window and return to current window.
  4400. Also, store the cursor date in variable ans2."
  4401. (let ((sw (selected-window)))
  4402. (select-window (get-buffer-window "*Calendar*"))
  4403. (eval form)
  4404. (when (calendar-cursor-to-date)
  4405. (let* ((date (calendar-cursor-to-date))
  4406. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4407. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  4408. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  4409. (select-window sw)))
  4410. (defun org-calendar-select ()
  4411. "Return to `org-read-date' with the date currently selected.
  4412. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  4413. (interactive)
  4414. (when (calendar-cursor-to-date)
  4415. (let* ((date (calendar-cursor-to-date))
  4416. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4417. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  4418. (if (active-minibuffer-window) (exit-minibuffer))))
  4419. (defun org-calendar-select-mouse (ev)
  4420. "Return to `org-read-date' with the date currently selected.
  4421. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  4422. (interactive "e")
  4423. (mouse-set-point ev)
  4424. (when (calendar-cursor-to-date)
  4425. (let* ((date (calendar-cursor-to-date))
  4426. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4427. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  4428. (if (active-minibuffer-window) (exit-minibuffer))))
  4429. (defun org-check-deadlines (ndays)
  4430. "Check if there are any deadlines due or past due.
  4431. A deadline is considered due if it happens within `org-deadline-warning-days'
  4432. days from today's date. If the deadline appears in an entry marked DONE,
  4433. it is not shown. The prefix arg NDAYS can be used to test that many
  4434. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  4435. (interactive "P")
  4436. (let* ((org-warn-days
  4437. (cond
  4438. ((equal ndays '(4)) 100000)
  4439. (ndays (prefix-numeric-value ndays))
  4440. (t org-deadline-warning-days)))
  4441. (case-fold-search nil)
  4442. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  4443. (callback
  4444. (lambda ()
  4445. (and (let ((d1 (time-to-days (current-time)))
  4446. (d2 (time-to-days
  4447. (org-time-string-to-time (match-string 1)))))
  4448. (< (- d2 d1) org-warn-days))
  4449. (not (org-entry-is-done-p))))))
  4450. (message "%d deadlines past-due or due within %d days"
  4451. (org-occur regexp callback)
  4452. org-warn-days)))
  4453. (defun org-evaluate-time-range (&optional to-buffer)
  4454. "Evaluate a time range by computing the difference between start and end.
  4455. Normally the result is just printed in the echo area, but with prefix arg
  4456. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  4457. If the time range is actually in a table, the result is inserted into the
  4458. next column.
  4459. For time difference computation, a year is assumed to be exactly 365
  4460. days in order to avoid rounding problems."
  4461. (interactive "P")
  4462. (save-excursion
  4463. (unless (org-at-date-range-p)
  4464. (goto-char (point-at-bol))
  4465. (re-search-forward org-tr-regexp (point-at-eol) t))
  4466. (if (not (org-at-date-range-p))
  4467. (error "Not at a time-stamp range, and none found in current line")))
  4468. (let* ((ts1 (match-string 1))
  4469. (ts2 (match-string 2))
  4470. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  4471. (match-end (match-end 0))
  4472. (time1 (org-time-string-to-time ts1))
  4473. (time2 (org-time-string-to-time ts2))
  4474. (t1 (time-to-seconds time1))
  4475. (t2 (time-to-seconds time2))
  4476. (diff (abs (- t2 t1)))
  4477. (negative (< (- t2 t1) 0))
  4478. ;; (ys (floor (* 365 24 60 60)))
  4479. (ds (* 24 60 60))
  4480. (hs (* 60 60))
  4481. (fy "%dy %dd %02d:%02d")
  4482. (fy1 "%dy %dd")
  4483. (fd "%dd %02d:%02d")
  4484. (fd1 "%dd")
  4485. (fh "%02d:%02d")
  4486. y d h m align)
  4487. (if havetime
  4488. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4489. y 0
  4490. d (floor (/ diff ds)) diff (mod diff ds)
  4491. h (floor (/ diff hs)) diff (mod diff hs)
  4492. m (floor (/ diff 60)))
  4493. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4494. y 0
  4495. d (floor (+ (/ diff ds) 0.5))
  4496. h 0 m 0))
  4497. (if (not to-buffer)
  4498. (message (org-make-tdiff-string y d h m))
  4499. (when (org-at-table-p)
  4500. (goto-char match-end)
  4501. (setq align t)
  4502. (and (looking-at " *|") (goto-char (match-end 0))))
  4503. (if (looking-at
  4504. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  4505. (replace-match ""))
  4506. (if negative (insert " -"))
  4507. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  4508. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  4509. (insert " " (format fh h m))))
  4510. (if align (org-table-align))
  4511. (message "Time difference inserted"))))
  4512. (defun org-make-tdiff-string (y d h m)
  4513. (let ((fmt "")
  4514. (l nil))
  4515. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  4516. l (push y l)))
  4517. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  4518. l (push d l)))
  4519. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  4520. l (push h l)))
  4521. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  4522. l (push m l)))
  4523. (apply 'format fmt (nreverse l))))
  4524. (defun org-time-string-to-time (s)
  4525. (apply 'encode-time (org-parse-time-string s)))
  4526. (defun org-parse-time-string (s &optional nodefault)
  4527. "Parse the standard Org-mode time string.
  4528. This should be a lot faster than the normal `parse-time-string'.
  4529. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  4530. hour and minute fields will be nil if not given."
  4531. (if (string-match org-ts-regexp1 s)
  4532. (list 0
  4533. (if (or (match-beginning 8) (not nodefault))
  4534. (string-to-number (or (match-string 8 s) "0")))
  4535. (if (or (match-beginning 7) (not nodefault))
  4536. (string-to-number (or (match-string 7 s) "0")))
  4537. (string-to-number (match-string 4 s))
  4538. (string-to-number (match-string 3 s))
  4539. (string-to-number (match-string 2 s))
  4540. nil nil nil)
  4541. (make-list 9 0)))
  4542. (defun org-timestamp-up (&optional arg)
  4543. "Increase the date item at the cursor by one.
  4544. If the cursor is on the year, change the year. If it is on the month or
  4545. the day, change that.
  4546. With prefix ARG, change by that many units."
  4547. (interactive "p")
  4548. (org-timestamp-change (prefix-numeric-value arg)))
  4549. (defun org-timestamp-down (&optional arg)
  4550. "Decrease the date item at the cursor by one.
  4551. If the cursor is on the year, change the year. If it is on the month or
  4552. the day, change that.
  4553. With prefix ARG, change by that many units."
  4554. (interactive "p")
  4555. (org-timestamp-change (- (prefix-numeric-value arg))))
  4556. (defun org-timestamp-up-day (&optional arg)
  4557. "Increase the date in the time stamp by one day.
  4558. With prefix ARG, change that many days."
  4559. (interactive "p")
  4560. (if (and (not (org-at-timestamp-p))
  4561. (org-on-heading-p))
  4562. (org-todo 'up)
  4563. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  4564. (defun org-timestamp-down-day (&optional arg)
  4565. "Decrease the date in the time stamp by one day.
  4566. With prefix ARG, change that many days."
  4567. (interactive "p")
  4568. (if (and (not (org-at-timestamp-p))
  4569. (org-on-heading-p))
  4570. (org-todo 'down)
  4571. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  4572. (defsubst org-pos-in-match-range (pos n)
  4573. (and (match-beginning n)
  4574. (<= (match-beginning n) pos)
  4575. (>= (match-end n) pos)))
  4576. (defun org-at-timestamp-p ()
  4577. "Determine if the cursor is in or at a timestamp."
  4578. (interactive)
  4579. (let* ((tsr org-ts-regexp2)
  4580. (pos (point))
  4581. (ans (or (looking-at tsr)
  4582. (save-excursion
  4583. (skip-chars-backward "^<\n\r\t")
  4584. (if (> (point) 1) (backward-char 1))
  4585. (and (looking-at tsr)
  4586. (> (- (match-end 0) pos) -1))))))
  4587. (and (boundp 'org-ts-what)
  4588. (setq org-ts-what
  4589. (cond
  4590. ((org-pos-in-match-range pos 2) 'year)
  4591. ((org-pos-in-match-range pos 3) 'month)
  4592. ((org-pos-in-match-range pos 7) 'hour)
  4593. ((org-pos-in-match-range pos 8) 'minute)
  4594. ((or (org-pos-in-match-range pos 4)
  4595. (org-pos-in-match-range pos 5)) 'day)
  4596. (t 'day))))
  4597. ans))
  4598. (defun org-timestamp-change (n &optional what)
  4599. "Change the date in the time stamp at point.
  4600. The date will be changed by N times WHAT. WHAT can be `day', `month',
  4601. `year', `minute', `second'. If WHAT is not given, the cursor position
  4602. in the timestamp determines what will be changed."
  4603. (let ((fmt (car org-time-stamp-formats))
  4604. org-ts-what
  4605. (pos (point))
  4606. ts time time0)
  4607. (if (not (org-at-timestamp-p))
  4608. (error "Not at a timestamp"))
  4609. (setq org-ts-what (or what org-ts-what))
  4610. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  4611. (- (match-end 0) (match-beginning 0))))
  4612. 1)
  4613. (cdr org-time-stamp-formats)
  4614. (car org-time-stamp-formats)))
  4615. (setq ts (match-string 0))
  4616. (replace-match "")
  4617. (setq time0 (org-parse-time-string ts))
  4618. (setq time
  4619. (apply 'encode-time
  4620. (append
  4621. (list (or (car time0) 0))
  4622. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  4623. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  4624. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  4625. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  4626. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  4627. (nthcdr 6 time0))))
  4628. (if (eq what 'calendar)
  4629. (let ((cal-date
  4630. (save-excursion
  4631. (save-match-data
  4632. (set-buffer "*Calendar*")
  4633. (calendar-cursor-to-date)))))
  4634. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  4635. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  4636. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  4637. (setcar time0 (or (car time0) 0))
  4638. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  4639. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  4640. (setq time (apply 'encode-time time0))))
  4641. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  4642. (goto-char pos)
  4643. ;; Try to recenter the calendar window, if any
  4644. (if (and org-calendar-follow-timestamp-change
  4645. (get-buffer-window "*Calendar*" t)
  4646. (memq org-ts-what '(day month year)))
  4647. (org-recenter-calendar (time-to-days time)))))
  4648. (defun org-recenter-calendar (date)
  4649. "If the calendar is visible, recenter it to DATE."
  4650. (let* ((win (selected-window))
  4651. (cwin (get-buffer-window "*Calendar*" t))
  4652. (calendar-move-hook nil))
  4653. (when cwin
  4654. (select-window cwin)
  4655. (calendar-goto-date (if (listp date) date
  4656. (calendar-gregorian-from-absolute date)))
  4657. (select-window win))))
  4658. (defun org-goto-calendar (&optional arg)
  4659. "Go to the Emacs calendar at the current date.
  4660. If there is a time stamp in the current line, go to that date.
  4661. A prefix ARG can be used to force the current date."
  4662. (interactive "P")
  4663. (let ((tsr org-ts-regexp) diff
  4664. (calendar-move-hook nil)
  4665. (view-calendar-holidays-initially nil)
  4666. (view-diary-entries-initially nil))
  4667. (if (or (org-at-timestamp-p)
  4668. (save-excursion
  4669. (beginning-of-line 1)
  4670. (looking-at (concat ".*" tsr))))
  4671. (let ((d1 (time-to-days (current-time)))
  4672. (d2 (time-to-days
  4673. (org-time-string-to-time (match-string 1)))))
  4674. (setq diff (- d2 d1))))
  4675. (calendar)
  4676. (calendar-goto-today)
  4677. (if (and diff (not arg)) (calendar-forward-day diff))))
  4678. (defun org-date-from-calendar ()
  4679. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  4680. If there is already a time stamp at the cursor position, update it."
  4681. (interactive)
  4682. (org-timestamp-change 0 'calendar))
  4683. ;;; Agenda, and Diary Integration
  4684. ;;; Define the mode
  4685. (defvar org-agenda-mode-map (make-sparse-keymap)
  4686. "Keymap for `org-agenda-mode'.")
  4687. (defvar org-agenda-menu) ; defined later in this file.
  4688. (defvar org-agenda-follow-mode nil)
  4689. (defvar org-agenda-show-log nil)
  4690. (defvar org-agenda-buffer-name "*Org Agenda*")
  4691. (defvar org-agenda-redo-command nil)
  4692. (defvar org-agenda-mode-hook nil)
  4693. (defvar org-agenda-type nil)
  4694. (defvar org-agenda-force-single-file nil)
  4695. (defun org-agenda-mode ()
  4696. "Mode for time-sorted view on action items in Org-mode files.
  4697. The following commands are available:
  4698. \\{org-agenda-mode-map}"
  4699. (interactive)
  4700. (kill-all-local-variables)
  4701. (setq major-mode 'org-agenda-mode)
  4702. (setq mode-name "Org-Agenda")
  4703. (use-local-map org-agenda-mode-map)
  4704. (easy-menu-add org-agenda-menu)
  4705. (if org-startup-truncated (setq truncate-lines t))
  4706. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  4707. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  4708. (unless org-agenda-keep-modes
  4709. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  4710. org-agenda-show-log nil))
  4711. (easy-menu-change
  4712. '("Agenda") "Agenda Files"
  4713. (append
  4714. (list
  4715. (vector
  4716. (if (get 'org-agenda-files 'org-restrict)
  4717. "Restricted to single file"
  4718. "Edit File List")
  4719. '(org-edit-agenda-file-list)
  4720. (not (get 'org-agenda-files 'org-restrict)))
  4721. "--")
  4722. (mapcar 'org-file-menu-entry (org-agenda-files))))
  4723. (org-agenda-set-mode-name)
  4724. (apply
  4725. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  4726. (list 'org-agenda-mode-hook)))
  4727. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  4728. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  4729. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  4730. (define-key org-agenda-mode-map " " 'org-agenda-show)
  4731. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  4732. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  4733. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  4734. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  4735. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  4736. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  4737. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  4738. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  4739. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  4740. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  4741. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  4742. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  4743. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  4744. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  4745. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  4746. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4747. (while l (define-key org-agenda-mode-map
  4748. (int-to-string (pop l)) 'digit-argument)))
  4749. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  4750. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  4751. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  4752. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  4753. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  4754. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  4755. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  4756. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  4757. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  4758. (define-key org-agenda-mode-map "n" 'next-line)
  4759. (define-key org-agenda-mode-map "p" 'previous-line)
  4760. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  4761. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  4762. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  4763. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  4764. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  4765. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  4766. (eval-after-load "calendar"
  4767. '(define-key calendar-mode-map org-calendar-to-agenda-key
  4768. 'org-calendar-goto-agenda))
  4769. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  4770. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  4771. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  4772. (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
  4773. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  4774. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  4775. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  4776. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  4777. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  4778. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  4779. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  4780. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  4781. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  4782. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  4783. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  4784. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  4785. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  4786. "Local keymap for agenda entries from Org-mode.")
  4787. (define-key org-agenda-keymap
  4788. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  4789. (define-key org-agenda-keymap
  4790. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  4791. (when org-agenda-mouse-1-follows-link
  4792. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  4793. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  4794. '("Agenda"
  4795. ("Agenda Files")
  4796. "--"
  4797. ["Show" org-agenda-show t]
  4798. ["Go To (other window)" org-agenda-goto t]
  4799. ["Go To (one window)" org-agenda-switch-to t]
  4800. ["Follow Mode" org-agenda-follow-mode
  4801. :style toggle :selected org-agenda-follow-mode :active t]
  4802. "--"
  4803. ["Cycle TODO" org-agenda-todo t]
  4804. ("Tags"
  4805. ["Show all Tags" org-agenda-show-tags t]
  4806. ["Set Tags" org-agenda-set-tags t])
  4807. ("Schedule"
  4808. ["Schedule" org-agenda-schedule t]
  4809. ["Set Deadline" org-agenda-deadline t]
  4810. "--"
  4811. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  4812. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  4813. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  4814. ("Priority"
  4815. ["Set Priority" org-agenda-priority t]
  4816. ["Increase Priority" org-agenda-priority-up t]
  4817. ["Decrease Priority" org-agenda-priority-down t]
  4818. ["Show Priority" org-agenda-show-priority t])
  4819. "--"
  4820. ;; ["New agenda command" org-agenda t]
  4821. ["Rebuild buffer" org-agenda-redo t]
  4822. "--"
  4823. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  4824. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  4825. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  4826. "--"
  4827. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  4828. :style radio :selected (equal org-agenda-ndays 1)]
  4829. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  4830. :style radio :selected (equal org-agenda-ndays 7)]
  4831. "--"
  4832. ["Show Logbook entries" org-agenda-log-mode
  4833. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  4834. ["Include Diary" org-agenda-toggle-diary
  4835. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  4836. ["Use Time Grid" org-agenda-toggle-time-grid
  4837. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  4838. "--"
  4839. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  4840. ("Calendar Commands"
  4841. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  4842. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  4843. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  4844. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  4845. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  4846. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  4847. "--"
  4848. ["Quit" org-agenda-quit t]
  4849. ["Exit and Release Buffers" org-agenda-exit t]
  4850. ))
  4851. ;;;###autoload
  4852. (defun org-agenda (arg)
  4853. "Dispatch agenda commands to collect entries to the agenda buffer.
  4854. Prompts for a character to select a command. Any prefix arg will be passed
  4855. on to the selected command. The default selections are:
  4856. a Call `org-agenda' to display the agenda for the current day or week.
  4857. t Call `org-todo-list' to display the global todo list.
  4858. T Call `org-todo-list' to display the global todo list, select only
  4859. entries with a specific TODO keyword (the user gets a prompt).
  4860. m Call `org-tags-view' to display headlines with tags matching
  4861. a condition (the user is prompted for the condition).
  4862. M Like `m', but select only TODO entries, no ordinary headlines.
  4863. More commands can be added by configuring the variable
  4864. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  4865. searches can be pre-defined in this way.
  4866. If the current buffer is in Org-mode and visiting a file, you can also
  4867. first press `1' to indicate that the agenda should be temporarily (until the
  4868. next use of \\[org-agenda]) restricted to the current file."
  4869. (interactive "P")
  4870. (catch 'exit
  4871. (let ((restrict-ok (and buffer-file-name (eq major-mode 'org-mode)))
  4872. (custom org-agenda-custom-commands)
  4873. c entry key type string)
  4874. (put 'org-agenda-files 'org-restrict nil)
  4875. (save-window-excursion
  4876. (delete-other-windows)
  4877. (switch-to-buffer-other-window " *Agenda Commands*")
  4878. (erase-buffer)
  4879. (insert
  4880. "Press key for an agenda command:
  4881. --------------------------------
  4882. a Agenda for current week or day
  4883. t List of all TODO entries T Entries with special TODO kwd
  4884. m Match a TAGS query M Like m, but only TODO entries
  4885. C Configure your own agenda commands")
  4886. (while (setq entry (pop custom))
  4887. (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
  4888. (insert (format "\n%-4s%-14s: %s"
  4889. key
  4890. (cond
  4891. ((eq type 'tags) "Tags query")
  4892. ((eq type 'todo) "TODO keyword")
  4893. ((eq type 'tags-tree) "Tags tree")
  4894. ((eq type 'todo-tree) "TODO kwd tree")
  4895. ((eq type 'occur-tree) "Occur tree")
  4896. (t "???"))
  4897. (org-add-props string nil 'face 'org-warning))))
  4898. (goto-char (point-min))
  4899. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  4900. (message "Press key for agenda command%s"
  4901. (if restrict-ok ", or [1] to restrict to current file" ""))
  4902. (setq c (read-char-exclusive))
  4903. (message "")
  4904. (when (equal c ?1)
  4905. (if restrict-ok
  4906. (put 'org-agenda-files 'org-restrict (list buffer-file-name))
  4907. (error "Cannot restrict agenda to current buffer"))
  4908. (message "Press key for agenda command%s"
  4909. (if restrict-ok " (restricted to current file)" ""))
  4910. (setq c (read-char-exclusive))
  4911. (message "")))
  4912. (require 'calendar) ; FIXME: can we avoid this for some commands?
  4913. ;; For example the todo list should not need it (but does...)
  4914. (cond
  4915. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  4916. ((equal c ?a) (call-interactively 'org-agenda-list))
  4917. ((equal c ?t) (call-interactively 'org-todo-list))
  4918. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  4919. ((equal c ?m) (call-interactively 'org-tags-view))
  4920. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  4921. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  4922. (setq type (nth 1 entry) string (nth 2 entry))
  4923. (cond
  4924. ((eq type 'tags)
  4925. (org-tags-view current-prefix-arg string))
  4926. ((eq type 'tags-todo)
  4927. (org-tags-view '(4) string))
  4928. ((eq type 'todo)
  4929. (org-todo-list string))
  4930. ((eq type 'tags-tree)
  4931. (org-check-for-org-mode)
  4932. (org-tags-sparse-tree current-prefix-arg string))
  4933. ((eq type 'todo-tree)
  4934. (org-check-for-org-mode)
  4935. (org-occur (concat "^" outline-regexp "[ \t]*"
  4936. (regexp-quote string) "\\>")))
  4937. ((eq type 'occur-tree)
  4938. (org-check-for-org-mode)
  4939. (org-occur string))
  4940. (t (error "Invalid custom agenda command type %s" type))))
  4941. (t (error "Invalid key"))))))
  4942. (defun org-check-for-org-mode ()
  4943. "Make sure current buffer is in org-mode. Error if not."
  4944. (or (eq major-mode 'org-mode)
  4945. (error "Cannot execute org-mode agenda command on buffer in %s."
  4946. major-mode)))
  4947. (defun org-fit-agenda-window ()
  4948. "Fit the window to the buffer size."
  4949. (and org-fit-agenda-window
  4950. (fboundp 'fit-window-to-buffer)
  4951. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  4952. (/ (frame-height) 2))))
  4953. (defun org-agenda-files (&optional unrestricted)
  4954. "Get the list of agenda files.
  4955. Optional UNRESTRICTED means return the full list even if a restriction
  4956. is currently in place."
  4957. (cond
  4958. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  4959. ((stringp org-agenda-files) (org-read-agenda-file-list))
  4960. ((listp org-agenda-files) org-agenda-files)
  4961. (t (error "Invalid value of `org-agenda-files'"))))
  4962. (defvar org-window-configuration)
  4963. (defun org-edit-agenda-file-list ()
  4964. "Edit the list of agenda files.
  4965. Depending on setup, this either uses customize to edit the variable
  4966. `org-agenda-files', or it visits the file that is holding the list. In the
  4967. latter case, the buffer is set up in a way that saving it automatically kills
  4968. the buffer and restores the previous window configuration."
  4969. (interactive)
  4970. (if (stringp org-agenda-files)
  4971. (let ((cw (current-window-configuration)))
  4972. (find-file org-agenda-files)
  4973. (set (make-local-variable 'org-window-configuration) cw)
  4974. (org-add-hook 'after-save-hook
  4975. (lambda ()
  4976. (set-window-configuration
  4977. (prog1 org-window-configuration
  4978. (kill-buffer (current-buffer))))
  4979. (org-install-agenda-files-menu)
  4980. (message "New agenda file list installed"))
  4981. nil 'local)
  4982. (message (substitute-command-keys
  4983. "Edit list and finish with \\[save-buffer]")))
  4984. (customize-variable 'org-agenda-files)))
  4985. (defun org-store-new-agenda-file-list (list)
  4986. "Set new value for the agenda file list and save it correcly."
  4987. (if (stringp org-agenda-files)
  4988. (let ((f org-agenda-files) b)
  4989. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  4990. (with-temp-file f
  4991. (insert (mapconcat 'identity list "\n") "\n")))
  4992. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  4993. (setq org-agenda-files list)
  4994. (customize-save-variable 'org-agenda-files org-agenda-files))))
  4995. (defun org-read-agenda-file-list ()
  4996. "Read the list of agenda files from a file."
  4997. (when (stringp org-agenda-files)
  4998. (with-temp-buffer
  4999. (insert-file-contents org-agenda-files)
  5000. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  5001. (defvar org-agenda-markers nil
  5002. "List of all currently active markers created by `org-agenda'.")
  5003. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  5004. "Creation time of the last agenda marker.")
  5005. (defun org-agenda-new-marker (&optional pos)
  5006. "Return a new agenda marker.
  5007. Org-mode keeps a list of these markers and resets them when they are
  5008. no longer in use."
  5009. (let ((m (copy-marker (or pos (point)))))
  5010. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  5011. (push m org-agenda-markers)
  5012. m))
  5013. (defun org-agenda-maybe-reset-markers (&optional force)
  5014. "Reset markers created by `org-agenda'. But only if they are old enough."
  5015. (if (or force
  5016. (> (- (time-to-seconds (current-time))
  5017. org-agenda-last-marker-time)
  5018. 5))
  5019. (while org-agenda-markers
  5020. (move-marker (pop org-agenda-markers) nil))))
  5021. (defvar org-agenda-new-buffers nil
  5022. "Buffers created to visit agenda files.")
  5023. (defun org-get-agenda-file-buffer (file)
  5024. "Get a buffer visiting FILE. If the buffer needs to be created, add
  5025. it to the list of buffers which might be released later."
  5026. (let ((buf (find-buffer-visiting file)))
  5027. (if buf
  5028. buf ; just return it
  5029. ;; Make a new buffer and remember it
  5030. (setq buf (find-file-noselect file))
  5031. (if buf (push buf org-agenda-new-buffers))
  5032. buf)))
  5033. (defun org-release-buffers (blist)
  5034. "Release all buffers in list, asking the user for confirmation when needed.
  5035. When a buffer is unmodified, it is just killed. When modified, it is saved
  5036. \(if the user agrees) and then killed."
  5037. (let (buf file)
  5038. (while (setq buf (pop blist))
  5039. (setq file (buffer-file-name buf))
  5040. (when (and (buffer-modified-p buf)
  5041. file
  5042. (y-or-n-p (format "Save file %s? " file)))
  5043. (with-current-buffer buf (save-buffer)))
  5044. (kill-buffer buf))))
  5045. (defvar org-respect-restriction nil) ; Dynamically-scoped param.
  5046. (defun org-timeline (&optional include-all keep-modes)
  5047. "Show a time-sorted view of the entries in the current org file.
  5048. Only entries with a time stamp of today or later will be listed. With
  5049. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  5050. under the current date.
  5051. If the buffer contains an active region, only check the region for
  5052. dates."
  5053. (interactive "P")
  5054. (require 'calendar)
  5055. (org-agenda-maybe-reset-markers 'force)
  5056. (org-compile-prefix-format org-timeline-prefix-format)
  5057. (let* ((dopast t)
  5058. (dotodo include-all)
  5059. (doclosed org-agenda-show-log)
  5060. (org-agenda-keep-modes keep-modes)
  5061. (entry buffer-file-name)
  5062. (org-agenda-files (list buffer-file-name))
  5063. (date (calendar-current-date))
  5064. (win (selected-window))
  5065. (pos1 (point))
  5066. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  5067. (end (if (org-region-active-p) (region-end) (point-max)))
  5068. (day-numbers (org-get-all-dates beg end 'no-ranges
  5069. t doclosed)) ; always include today
  5070. (today (time-to-days (current-time)))
  5071. (org-respect-restriction t)
  5072. (past t)
  5073. args
  5074. s e rtn d)
  5075. (setq org-agenda-redo-command
  5076. (list 'progn
  5077. (list 'switch-to-buffer-other-window (current-buffer))
  5078. (list 'org-timeline (list 'quote include-all) t)))
  5079. (if (not dopast)
  5080. ;; Remove past dates from the list of dates.
  5081. (setq day-numbers (delq nil (mapcar (lambda(x)
  5082. (if (>= x today) x nil))
  5083. day-numbers))))
  5084. (switch-to-buffer-other-window
  5085. (get-buffer-create org-agenda-buffer-name))
  5086. (setq buffer-read-only nil)
  5087. (erase-buffer)
  5088. (org-agenda-mode) (setq buffer-read-only nil)
  5089. (set (make-local-variable 'org-agenda-type) 'timeline)
  5090. (if doclosed (push :closed args))
  5091. (push :timestamp args)
  5092. (if dotodo (push :todo args))
  5093. (while (setq d (pop day-numbers))
  5094. (if (and (>= d today)
  5095. dopast
  5096. past)
  5097. (progn
  5098. (setq past nil)
  5099. (insert (make-string 79 ?-) "\n")))
  5100. (setq date (calendar-gregorian-from-absolute d))
  5101. (setq s (point))
  5102. (setq rtn (apply 'org-agenda-get-day-entries
  5103. entry date args))
  5104. (if (or rtn (equal d today))
  5105. (progn
  5106. (insert (calendar-day-name date) " "
  5107. (number-to-string (extract-calendar-day date)) " "
  5108. (calendar-month-name (extract-calendar-month date)) " "
  5109. (number-to-string (extract-calendar-year date)) "\n")
  5110. (put-text-property s (1- (point)) 'face
  5111. 'org-level-3)
  5112. (if (equal d today)
  5113. (put-text-property s (1- (point)) 'org-today t))
  5114. (insert (org-finalize-agenda-entries rtn) "\n")
  5115. (put-text-property s (1- (point)) 'day d))))
  5116. (goto-char (point-min))
  5117. (setq buffer-read-only t)
  5118. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  5119. (point-min)))
  5120. (when (not org-select-timeline-window)
  5121. (select-window win)
  5122. (goto-char pos1))))
  5123. ;;;###autoload
  5124. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  5125. "Produce a weekly view from all files in variable `org-agenda-files'.
  5126. The view will be for the current week, but from the overview buffer you
  5127. will be able to go to other weeks.
  5128. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  5129. also be shown, under the current date.
  5130. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  5131. on the days are also shown. See the variable `org-log-done' for how
  5132. to turn on logging.
  5133. START-DAY defaults to TODAY, or to the most recent match for the weekday
  5134. given in `org-agenda-start-on-weekday'.
  5135. NDAYS defaults to `org-agenda-ndays'."
  5136. (interactive "P")
  5137. (org-agenda-maybe-reset-markers 'force)
  5138. (org-compile-prefix-format org-agenda-prefix-format)
  5139. (require 'calendar)
  5140. (let* ((org-agenda-start-on-weekday
  5141. (if (or (equal ndays 1)
  5142. (and (null ndays) (equal 1 org-agenda-ndays)))
  5143. nil org-agenda-start-on-weekday))
  5144. (org-agenda-keep-modes keep-modes)
  5145. (thefiles (org-agenda-files))
  5146. (files thefiles)
  5147. (win (selected-window))
  5148. (today (time-to-days (current-time)))
  5149. (sd (or start-day today))
  5150. (start (if (or (null org-agenda-start-on-weekday)
  5151. (< org-agenda-ndays 7))
  5152. sd
  5153. (let* ((nt (calendar-day-of-week
  5154. (calendar-gregorian-from-absolute sd)))
  5155. (n1 org-agenda-start-on-weekday)
  5156. (d (- nt n1)))
  5157. (- sd (+ (if (< d 0) 7 0) d)))))
  5158. (day-numbers (list start))
  5159. (inhibit-redisplay t)
  5160. s e rtn rtnall file date d start-pos end-pos todayp nd)
  5161. (setq org-agenda-redo-command
  5162. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  5163. ;; Make the list of days
  5164. (setq ndays (or ndays org-agenda-ndays)
  5165. nd ndays)
  5166. (while (> ndays 1)
  5167. (push (1+ (car day-numbers)) day-numbers)
  5168. (setq ndays (1- ndays)))
  5169. (setq day-numbers (nreverse day-numbers))
  5170. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  5171. (progn
  5172. (delete-other-windows)
  5173. (switch-to-buffer-other-window
  5174. (get-buffer-create org-agenda-buffer-name))))
  5175. (setq buffer-read-only nil)
  5176. (erase-buffer)
  5177. (org-agenda-mode) (setq buffer-read-only nil)
  5178. (set (make-local-variable 'org-agenda-type) 'agenda)
  5179. (set (make-local-variable 'starting-day) (car day-numbers))
  5180. (set (make-local-variable 'include-all-loc) include-all)
  5181. (when (and (or include-all org-agenda-include-all-todo)
  5182. (member today day-numbers))
  5183. (setq files thefiles
  5184. rtnall nil)
  5185. (while (setq file (pop files))
  5186. (catch 'nextfile
  5187. (org-check-agenda-file file)
  5188. (setq date (calendar-gregorian-from-absolute today)
  5189. rtn (org-agenda-get-day-entries
  5190. file date :todo))
  5191. (setq rtnall (append rtnall rtn))))
  5192. (when rtnall
  5193. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  5194. (add-text-properties (point-min) (1- (point))
  5195. (list 'face 'org-level-3))
  5196. (insert (org-finalize-agenda-entries rtnall) "\n")))
  5197. (while (setq d (pop day-numbers))
  5198. (setq date (calendar-gregorian-from-absolute d)
  5199. s (point))
  5200. (if (or (setq todayp (= d today))
  5201. (and (not start-pos) (= d sd)))
  5202. (setq start-pos (point))
  5203. (if (and start-pos (not end-pos))
  5204. (setq end-pos (point))))
  5205. (setq files thefiles
  5206. rtnall nil)
  5207. (while (setq file (pop files))
  5208. (catch 'nextfile
  5209. (org-check-agenda-file file)
  5210. (if org-agenda-show-log
  5211. (setq rtn (org-agenda-get-day-entries
  5212. file date
  5213. :deadline :scheduled :timestamp :closed))
  5214. (setq rtn (org-agenda-get-day-entries
  5215. file date
  5216. :deadline :scheduled :timestamp)))
  5217. (setq rtnall (append rtnall rtn))))
  5218. (if org-agenda-include-diary
  5219. (progn
  5220. (require 'diary-lib)
  5221. (setq rtn (org-get-entries-from-diary date))
  5222. (setq rtnall (append rtnall rtn))))
  5223. (if (or rtnall org-agenda-show-all-dates)
  5224. (progn
  5225. (insert (format "%-9s %2d %s %4d\n"
  5226. (calendar-day-name date)
  5227. (extract-calendar-day date)
  5228. (calendar-month-name (extract-calendar-month date))
  5229. (extract-calendar-year date)))
  5230. (put-text-property s (1- (point)) 'face
  5231. 'org-level-3)
  5232. (if rtnall (insert
  5233. (org-finalize-agenda-entries
  5234. (org-agenda-add-time-grid-maybe
  5235. rtnall nd todayp))
  5236. "\n"))
  5237. (put-text-property s (1- (point)) 'day d))))
  5238. (goto-char (point-min))
  5239. (setq buffer-read-only t)
  5240. (org-fit-agenda-window)
  5241. (unless (and (pos-visible-in-window-p (point-min))
  5242. (pos-visible-in-window-p (point-max)))
  5243. (goto-char (1- (point-max)))
  5244. (recenter -1)
  5245. (if (not (pos-visible-in-window-p (or start-pos 1)))
  5246. (progn
  5247. (goto-char (or start-pos 1))
  5248. (recenter 1))))
  5249. (goto-char (or start-pos 1))
  5250. (if (not org-select-agenda-window) (select-window win))
  5251. (message "")))
  5252. (defvar org-select-this-todo-keyword nil)
  5253. ;;;###autoload
  5254. (defun org-todo-list (arg &optional keep-modes)
  5255. "Show all TODO entries from all agenda file in a single list.
  5256. The prefix arg can be used to select a specific TODO keyword and limit
  5257. the list to these. When using \\[universal-argument], you will be prompted
  5258. for a keyword. A numeric prefix directly selects the Nth keyword in
  5259. `org-todo-keywords'."
  5260. (interactive "P")
  5261. (org-agenda-maybe-reset-markers 'force)
  5262. (org-compile-prefix-format org-agenda-prefix-format)
  5263. (let* ((org-agenda-keep-modes keep-modes)
  5264. (today (time-to-days (current-time)))
  5265. (date (calendar-gregorian-from-absolute today))
  5266. (win (selected-window))
  5267. (kwds org-todo-keywords)
  5268. (completion-ignore-case t)
  5269. (org-select-this-todo-keyword
  5270. (if (stringp arg) arg
  5271. (and arg (integerp arg) (> arg 0)
  5272. (nth (1- arg) org-todo-keywords))))
  5273. rtn rtnall files file pos)
  5274. (when (equal arg '(4))
  5275. (setq org-select-this-todo-keyword
  5276. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  5277. nil t)))
  5278. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  5279. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  5280. (progn
  5281. (delete-other-windows)
  5282. (switch-to-buffer-other-window
  5283. (get-buffer-create org-agenda-buffer-name))))
  5284. (setq buffer-read-only nil)
  5285. (erase-buffer)
  5286. (org-agenda-mode) (setq buffer-read-only nil)
  5287. (set (make-local-variable 'org-agenda-type) 'todo)
  5288. (set (make-local-variable 'last-arg) arg)
  5289. (set (make-local-variable 'org-todo-keywords) kwds)
  5290. (set (make-local-variable 'org-agenda-redo-command)
  5291. '(org-todo-list (or current-prefix-arg last-arg) t))
  5292. (setq files (org-agenda-files)
  5293. rtnall nil)
  5294. (while (setq file (pop files))
  5295. (catch 'nextfile
  5296. (org-check-agenda-file file)
  5297. (setq rtn (org-agenda-get-day-entries file date :todo))
  5298. (setq rtnall (append rtnall rtn))))
  5299. (insert "Global list of TODO items of type: ")
  5300. (add-text-properties (point-min) (1- (point))
  5301. (list 'face 'org-level-3))
  5302. (setq pos (point))
  5303. (insert (or org-select-this-todo-keyword "ALL") "\n")
  5304. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  5305. (setq pos (point))
  5306. (insert
  5307. "Available with `N r': (0)ALL "
  5308. (let ((n 0))
  5309. (mapconcat (lambda (x)
  5310. (format "(%d)%s" (setq n (1+ n)) x))
  5311. org-todo-keywords " "))
  5312. "\n")
  5313. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  5314. (when rtnall
  5315. (insert (org-finalize-agenda-entries rtnall) "\n"))
  5316. (goto-char (point-min))
  5317. (setq buffer-read-only t)
  5318. (org-fit-agenda-window)
  5319. (if (not org-select-agenda-window) (select-window win))))
  5320. (defun org-check-agenda-file (file)
  5321. "Make sure FILE exists. If not, ask user what to do."
  5322. (when (not (file-exists-p file))
  5323. (message "non-existent file %s. [R]emove from list or [A]bort?"
  5324. (abbreviate-file-name file))
  5325. (let ((r (downcase (read-char-exclusive))))
  5326. (cond
  5327. ((equal r ?r)
  5328. (org-remove-file file)
  5329. (throw 'nextfile t))
  5330. (t (error "Abort"))))))
  5331. (defun org-agenda-check-type (error &rest types)
  5332. "Check if agenda buffer is of allowed type.
  5333. If ERROR is non-nil, throw an error, otherwise just return nil."
  5334. (if (memq org-agenda-type types)
  5335. t
  5336. (if error
  5337. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  5338. nil)))
  5339. (defun org-agenda-quit ()
  5340. "Exit agenda by removing the window or the buffer."
  5341. (interactive)
  5342. (let ((buf (current-buffer)))
  5343. (if (not (one-window-p)) (delete-window))
  5344. (kill-buffer buf)
  5345. (org-agenda-maybe-reset-markers 'force)))
  5346. (defun org-agenda-exit ()
  5347. "Exit agenda by removing the window or the buffer.
  5348. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  5349. Org-mode buffers visited directly by the user will not be touched."
  5350. (interactive)
  5351. (org-release-buffers org-agenda-new-buffers)
  5352. (setq org-agenda-new-buffers nil)
  5353. (org-agenda-quit))
  5354. (defun org-agenda-redo ()
  5355. "Rebuild Agenda.
  5356. When this is the global TODO list, a prefix argument will be interpreted."
  5357. (interactive)
  5358. (message "Rebuilding agenda buffer...")
  5359. (eval org-agenda-redo-command)
  5360. (message "Rebuilding agenda buffer...done"))
  5361. (defun org-agenda-goto-today ()
  5362. "Go to today."
  5363. (interactive)
  5364. (org-agenda-check-type t 'timeline 'agenda)
  5365. (if (boundp 'starting-day)
  5366. (let ((cmd (car org-agenda-redo-command))
  5367. (iall (nth 1 org-agenda-redo-command))
  5368. (nday (nth 3 org-agenda-redo-command))
  5369. (keep (nth 4 org-agenda-redo-command)))
  5370. (eval (list cmd iall nil nday keep)))
  5371. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  5372. (point-min)))))
  5373. (defun org-agenda-later (arg)
  5374. "Go forward in time by `org-agenda-ndays' days.
  5375. With prefix ARG, go forward that many times `org-agenda-ndays'."
  5376. (interactive "p")
  5377. (org-agenda-check-type t 'agenda)
  5378. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  5379. (+ starting-day (* arg org-agenda-ndays)) nil t))
  5380. (defun org-agenda-earlier (arg)
  5381. "Go back in time by `org-agenda-ndays' days.
  5382. With prefix ARG, go back that many times `org-agenda-ndays'."
  5383. (interactive "p")
  5384. (org-agenda-check-type t 'agenda)
  5385. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  5386. (- starting-day (* arg org-agenda-ndays)) nil t))
  5387. (defun org-agenda-week-view ()
  5388. "Switch to weekly view for agenda."
  5389. (interactive)
  5390. (org-agenda-check-type t 'agenda)
  5391. (setq org-agenda-ndays 7)
  5392. (org-agenda-list include-all-loc
  5393. (or (get-text-property (point) 'day)
  5394. starting-day)
  5395. nil t)
  5396. (org-agenda-set-mode-name)
  5397. (message "Switched to week view"))
  5398. (defun org-agenda-day-view ()
  5399. "Switch to daily view for agenda."
  5400. (interactive)
  5401. (org-agenda-check-type t 'agenda)
  5402. (setq org-agenda-ndays 1)
  5403. (org-agenda-list include-all-loc
  5404. (or (get-text-property (point) 'day)
  5405. starting-day)
  5406. nil t)
  5407. (org-agenda-set-mode-name)
  5408. (message "Switched to day view"))
  5409. (defun org-agenda-next-date-line (&optional arg)
  5410. "Jump to the next line indicating a date in agenda buffer."
  5411. (interactive "p")
  5412. (org-agenda-check-type t 'agenda 'timeline)
  5413. (beginning-of-line 1)
  5414. (if (looking-at "^\\S-") (forward-char 1))
  5415. (if (not (re-search-forward "^\\S-" nil t arg))
  5416. (progn
  5417. (backward-char 1)
  5418. (error "No next date after this line in this buffer")))
  5419. (goto-char (match-beginning 0)))
  5420. (defun org-agenda-previous-date-line (&optional arg)
  5421. "Jump to the previous line indicating a date in agenda buffer."
  5422. (interactive "p")
  5423. (org-agenda-check-type t 'agenda 'timeline)
  5424. (beginning-of-line 1)
  5425. (if (not (re-search-backward "^\\S-" nil t arg))
  5426. (error "No previous date before this line in this buffer")))
  5427. ;; Initialize the highlight
  5428. (defvar org-hl (org-make-overlay 1 1))
  5429. (org-overlay-put org-hl 'face 'highlight)
  5430. (defun org-highlight (begin end &optional buffer)
  5431. "Highlight a region with overlay."
  5432. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  5433. org-hl begin end (or buffer (current-buffer))))
  5434. (defun org-unhighlight ()
  5435. "Detach overlay INDEX."
  5436. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  5437. (defun org-agenda-follow-mode ()
  5438. "Toggle follow mode in an agenda buffer."
  5439. (interactive)
  5440. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  5441. (org-agenda-set-mode-name)
  5442. (message "Follow mode is %s"
  5443. (if org-agenda-follow-mode "on" "off")))
  5444. (defun org-agenda-log-mode ()
  5445. "Toggle log mode in an agenda buffer."
  5446. (interactive)
  5447. (org-agenda-check-type t 'agenda 'timeline)
  5448. (setq org-agenda-show-log (not org-agenda-show-log))
  5449. (org-agenda-set-mode-name)
  5450. (org-agenda-redo)
  5451. (message "Log mode is %s"
  5452. (if org-agenda-show-log "on" "off")))
  5453. (defun org-agenda-toggle-diary ()
  5454. "Toggle diary inclusion in an agenda buffer."
  5455. (interactive)
  5456. (org-agenda-check-type t 'agenda)
  5457. (setq org-agenda-include-diary (not org-agenda-include-diary))
  5458. (org-agenda-redo)
  5459. (org-agenda-set-mode-name)
  5460. (message "Diary inclusion turned %s"
  5461. (if org-agenda-include-diary "on" "off")))
  5462. (defun org-agenda-toggle-time-grid ()
  5463. "Toggle time grid in an agenda buffer."
  5464. (interactive)
  5465. (org-agenda-check-type t 'agenda)
  5466. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  5467. (org-agenda-redo)
  5468. (org-agenda-set-mode-name)
  5469. (message "Time-grid turned %s"
  5470. (if org-agenda-use-time-grid "on" "off")))
  5471. (defun org-agenda-set-mode-name ()
  5472. "Set the mode name to indicate all the small mode settings."
  5473. (setq mode-name
  5474. (concat "Org-Agenda"
  5475. (if (equal org-agenda-ndays 1) " Day" "")
  5476. (if (equal org-agenda-ndays 7) " Week" "")
  5477. (if org-agenda-follow-mode " Follow" "")
  5478. (if org-agenda-include-diary " Diary" "")
  5479. (if org-agenda-use-time-grid " Grid" "")
  5480. (if org-agenda-show-log " Log" "")))
  5481. (force-mode-line-update))
  5482. (defun org-agenda-post-command-hook ()
  5483. (and (eolp) (not (bolp)) (backward-char 1))
  5484. (if (and org-agenda-follow-mode
  5485. (get-text-property (point) 'org-marker))
  5486. (org-agenda-show)))
  5487. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  5488. (defun org-get-entries-from-diary (date)
  5489. "Get the (Emacs Calendar) diary entries for DATE."
  5490. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  5491. (diary-display-hook '(fancy-diary-display))
  5492. (list-diary-entries-hook
  5493. (cons 'org-diary-default-entry list-diary-entries-hook))
  5494. (diary-file-name-prefix-function nil) ; turn this feature off
  5495. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  5496. entries
  5497. (org-disable-agenda-to-diary t))
  5498. (save-excursion
  5499. (save-window-excursion
  5500. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  5501. (if (not (get-buffer fancy-diary-buffer))
  5502. (setq entries nil)
  5503. (with-current-buffer fancy-diary-buffer
  5504. (setq buffer-read-only nil)
  5505. (if (= (point-max) 1)
  5506. ;; No entries
  5507. (setq entries nil)
  5508. ;; Omit the date and other unnecessary stuff
  5509. (org-agenda-cleanup-fancy-diary)
  5510. ;; Add prefix to each line and extend the text properties
  5511. (if (= (point-max) 1)
  5512. (setq entries nil)
  5513. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  5514. (set-buffer-modified-p nil)
  5515. (kill-buffer fancy-diary-buffer)))
  5516. (when entries
  5517. (setq entries (org-split-string entries "\n"))
  5518. (setq entries
  5519. (mapcar
  5520. (lambda (x)
  5521. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  5522. ;; Extend the text properties to the beginning of the line
  5523. (org-add-props x (text-properties-at (1- (length x)) x)))
  5524. entries)))))
  5525. (defun org-agenda-cleanup-fancy-diary ()
  5526. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  5527. This gets rid of the date, the underline under the date, and
  5528. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  5529. date. It also removes lines that contain only whitespace."
  5530. (goto-char (point-min))
  5531. (if (looking-at ".*?:[ \t]*")
  5532. (progn
  5533. (replace-match "")
  5534. (re-search-forward "\n=+$" nil t)
  5535. (replace-match "")
  5536. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  5537. (re-search-forward "\n=+$" nil t)
  5538. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  5539. (goto-char (point-min))
  5540. (while (re-search-forward "^ +\n" nil t)
  5541. (replace-match ""))
  5542. (goto-char (point-min))
  5543. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  5544. (replace-match "")))
  5545. ;; Make sure entries from the diary have the right text properties.
  5546. (eval-after-load "diary-lib"
  5547. '(if (boundp 'diary-modify-entry-list-string-function)
  5548. ;; We can rely on the hook, nothing to do
  5549. nil
  5550. ;; Hook not avaiable, must use advice to make this work
  5551. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  5552. "Make the position visible."
  5553. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  5554. (stringp string)
  5555. buffer-file-name)
  5556. (setq string (org-modify-diary-entry-string string))))))
  5557. (defun org-modify-diary-entry-string (string)
  5558. "Add text properties to string, allowing org-mode to act on it."
  5559. (org-add-props string nil
  5560. 'mouse-face 'highlight
  5561. 'keymap org-agenda-keymap
  5562. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  5563. (abbreviate-file-name buffer-file-name))
  5564. 'org-agenda-diary-link t
  5565. 'org-marker (org-agenda-new-marker (point-at-bol))))
  5566. (defun org-diary-default-entry ()
  5567. "Add a dummy entry to the diary.
  5568. Needed to avoid empty dates which mess up holiday display."
  5569. ;; Catch the error if dealing with the new add-to-diary-alist
  5570. (when org-disable-agenda-to-diary
  5571. (condition-case nil
  5572. (add-to-diary-list original-date "Org-mode dummy" "")
  5573. (error
  5574. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  5575. (defun org-cycle-agenda-files ()
  5576. "Cycle through the files in `org-agenda-files'.
  5577. If the current buffer visits an agenda file, find the next one in the list.
  5578. If the current buffer does not, find the first agenda file."
  5579. (interactive)
  5580. (let* ((fs (org-agenda-files t))
  5581. (files (append fs (list (car fs))))
  5582. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  5583. file)
  5584. (unless files (error "No agenda files"))
  5585. (catch 'exit
  5586. (while (setq file (pop files))
  5587. (if (equal (file-truename file) tcf)
  5588. (when (car files)
  5589. (find-file (car files))
  5590. (throw 'exit t))))
  5591. (find-file (car fs)))))
  5592. (defun org-agenda-file-to-end ()
  5593. "Move/add the current file to the end of the agenda file list.
  5594. If the file is not present in the list, it is appended to the list. If it is
  5595. present, it is moved there."
  5596. (interactive)
  5597. (org-agenda-file-to-front 'to-end))
  5598. (defun org-agenda-file-to-front (&optional to-end)
  5599. "Move/add the current file to the top of the agenda file list.
  5600. If the file is not present in the list, it is added to the front. If it is
  5601. present, it is moved there. With optional argument TO-END, add/move to the
  5602. end of the list."
  5603. (interactive "P")
  5604. (let ((file-alist (mapcar (lambda (x)
  5605. (cons (file-truename x) x))
  5606. (org-agenda-files t)))
  5607. (ctf (file-truename buffer-file-name))
  5608. x had)
  5609. (setq x (assoc ctf file-alist) had x)
  5610. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  5611. (if to-end
  5612. (setq file-alist (append (delq x file-alist) (list x)))
  5613. (setq file-alist (cons x (delq x file-alist))))
  5614. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  5615. (org-install-agenda-files-menu)
  5616. (message "File %s to %s of agenda file list"
  5617. (if had "moved" "added") (if to-end "end" "front"))))
  5618. (defun org-remove-file (&optional file)
  5619. "Remove current file from the list of files in variable `org-agenda-files'.
  5620. These are the files which are being checked for agenda entries.
  5621. Optional argument FILE means, use this file instead of the current."
  5622. (interactive)
  5623. (let* ((file (or file buffer-file-name))
  5624. (true-file (file-truename file))
  5625. (afile (abbreviate-file-name file))
  5626. (files (delq nil (mapcar
  5627. (lambda (x)
  5628. (if (equal true-file
  5629. (file-truename x))
  5630. nil x))
  5631. (org-agenda-files t)))))
  5632. (if (not (= (length files) (length (org-agenda-files t))))
  5633. (progn
  5634. (org-store-new-agenda-file-list files)
  5635. (org-install-agenda-files-menu)
  5636. (message "Removed file: %s" afile))
  5637. (message "File was not in list: %s" afile))))
  5638. (defun org-file-menu-entry (file)
  5639. (vector file (list 'find-file file) t))
  5640. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive)
  5641. "Return a list of all relevant day numbers from BEG to END buffer positions.
  5642. If NO-RANGES is non-nil, include only the start and end dates of a range,
  5643. not every single day in the range. If FORCE-TODAY is non-nil, make
  5644. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  5645. inactive time stamps (those in square brackets) are included."
  5646. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  5647. dates date day day1 day2 ts1 ts2)
  5648. (if force-today
  5649. (setq dates (list (time-to-days (current-time)))))
  5650. (save-excursion
  5651. (goto-char beg)
  5652. (while (re-search-forward re end t)
  5653. (setq day (time-to-days (org-time-string-to-time
  5654. (substring (match-string 1) 0 10))))
  5655. (or (memq day dates) (push day dates)))
  5656. (unless no-ranges
  5657. (goto-char beg)
  5658. (while (re-search-forward org-tr-regexp end t)
  5659. (setq ts1 (substring (match-string 1) 0 10)
  5660. ts2 (substring (match-string 2) 0 10)
  5661. day1 (time-to-days (org-time-string-to-time ts1))
  5662. day2 (time-to-days (org-time-string-to-time ts2)))
  5663. (while (< (setq day1 (1+ day1)) day2)
  5664. (or (memq day1 dates) (push day1 dates)))))
  5665. (sort dates '<))))
  5666. ;;;###autoload
  5667. (defun org-diary (&rest args)
  5668. "Return diary information from org-files.
  5669. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  5670. It accesses org files and extracts information from those files to be
  5671. listed in the diary. The function accepts arguments specifying what
  5672. items should be listed. The following arguments are allowed:
  5673. :timestamp List the headlines of items containing a date stamp or
  5674. date range matching the selected date. Deadlines will
  5675. also be listed, on the expiration day.
  5676. :deadline List any deadlines past due, or due within
  5677. `org-deadline-warning-days'. The listing occurs only
  5678. in the diary for *today*, not at any other date. If
  5679. an entry is marked DONE, it is no longer listed.
  5680. :scheduled List all items which are scheduled for the given date.
  5681. The diary for *today* also contains items which were
  5682. scheduled earlier and are not yet marked DONE.
  5683. :todo List all TODO items from the org-file. This may be a
  5684. long list - so this is not turned on by default.
  5685. Like deadlines, these entries only show up in the
  5686. diary for *today*, not at any other date.
  5687. The call in the diary file should look like this:
  5688. &%%(org-diary) ~/path/to/some/orgfile.org
  5689. Use a separate line for each org file to check. Or, if you omit the file name,
  5690. all files listed in `org-agenda-files' will be checked automatically:
  5691. &%%(org-diary)
  5692. If you don't give any arguments (as in the example above), the default
  5693. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  5694. also be written as
  5695. &%%(org-diary :deadline :timestamp :scheduled)
  5696. The function expects the lisp variables `entry' and `date' to be provided
  5697. by the caller, because this is how the calendar works. Don't use this
  5698. function from a program - use `org-agenda-get-day-entries' instead."
  5699. (org-agenda-maybe-reset-markers)
  5700. (org-compile-prefix-format org-agenda-prefix-format)
  5701. (setq args (or args '(:deadline :scheduled :timestamp)))
  5702. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  5703. (list entry)
  5704. (org-agenda-files t)))
  5705. file rtn results)
  5706. ;; If this is called during org-agenda, don't return any entries to
  5707. ;; the calendar. Org Agenda will list these entries itself.
  5708. (if org-disable-agenda-to-diary (setq files nil))
  5709. (while (setq file (pop files))
  5710. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  5711. (setq results (append results rtn)))
  5712. (if results
  5713. (concat (org-finalize-agenda-entries results) "\n"))))
  5714. (defvar org-category-table nil)
  5715. (defun org-get-category-table ()
  5716. "Get the table of categories and positions in current buffer."
  5717. (let (tbl)
  5718. (save-excursion
  5719. (goto-char (point-min))
  5720. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  5721. (push (cons (point) (org-trim (match-string 2))) tbl)))
  5722. tbl))
  5723. (defun org-get-category (&optional pos)
  5724. "Get the category applying to position POS."
  5725. (if (not org-category-table)
  5726. (cond
  5727. ((null org-category)
  5728. (setq org-category
  5729. (if buffer-file-name
  5730. (file-name-sans-extension
  5731. (file-name-nondirectory buffer-file-name))
  5732. "???")))
  5733. ((symbolp org-category) (symbol-name org-category))
  5734. (t org-category))
  5735. (let ((tbl org-category-table)
  5736. (pos (or pos (point))))
  5737. (while (and tbl (> (caar tbl) pos))
  5738. (pop tbl))
  5739. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  5740. org-category-table))))))
  5741. (defun org-agenda-get-day-entries (file date &rest args)
  5742. "Does the work for `org-diary' and `org-agenda'.
  5743. FILE is the path to a file to be checked for entries. DATE is date like
  5744. the one returned by `calendar-current-date'. ARGS are symbols indicating
  5745. which kind of entries should be extracted. For details about these, see
  5746. the documentation of `org-diary'."
  5747. (setq args (or args '(:deadline :scheduled :timestamp)))
  5748. (let* ((org-startup-with-deadline-check nil)
  5749. (org-startup-folded nil)
  5750. (org-startup-align-all-tables nil)
  5751. (buffer (if (file-exists-p file)
  5752. (org-get-agenda-file-buffer file)
  5753. (error "No such file %s" file)))
  5754. arg results rtn)
  5755. (if (not buffer)
  5756. ;; If file does not exist, make sure an error message ends up in diary
  5757. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  5758. (with-current-buffer buffer
  5759. (unless (eq major-mode 'org-mode)
  5760. (error "Agenda file %s is not in `org-mode'" file))
  5761. (setq org-category-table (org-get-category-table))
  5762. (let ((case-fold-search nil))
  5763. (save-excursion
  5764. (save-restriction
  5765. (if org-respect-restriction
  5766. (if (org-region-active-p)
  5767. ;; Respect a region to restrict search
  5768. (narrow-to-region (region-beginning) (region-end)))
  5769. ;; If we work for the calendar or many files,
  5770. ;; get rid of any restriction
  5771. (widen))
  5772. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  5773. (while (setq arg (pop args))
  5774. (cond
  5775. ((and (eq arg :todo)
  5776. (equal date (calendar-current-date)))
  5777. (setq rtn (org-agenda-get-todos))
  5778. (setq results (append results rtn)))
  5779. ((eq arg :timestamp)
  5780. (setq rtn (org-agenda-get-blocks))
  5781. (setq results (append results rtn))
  5782. (setq rtn (org-agenda-get-timestamps))
  5783. (setq results (append results rtn)))
  5784. ((eq arg :scheduled)
  5785. (setq rtn (org-agenda-get-scheduled))
  5786. (setq results (append results rtn)))
  5787. ((eq arg :closed)
  5788. (setq rtn (org-agenda-get-closed))
  5789. (setq results (append results rtn)))
  5790. ((and (eq arg :deadline)
  5791. (equal date (calendar-current-date)))
  5792. (setq rtn (org-agenda-get-deadlines))
  5793. (setq results (append results rtn))))))))
  5794. results))))
  5795. (defun org-entry-is-done-p ()
  5796. "Is the current entry marked DONE?"
  5797. (save-excursion
  5798. (and (re-search-backward "[\r\n]\\*" nil t)
  5799. (looking-at org-nl-done-regexp))))
  5800. (defun org-at-date-range-p ()
  5801. "Is the cursor inside a date range?"
  5802. (interactive)
  5803. (save-excursion
  5804. (catch 'exit
  5805. (let ((pos (point)))
  5806. (skip-chars-backward "^<\r\n")
  5807. (skip-chars-backward "<")
  5808. (and (looking-at org-tr-regexp)
  5809. (>= (match-end 0) pos)
  5810. (throw 'exit t))
  5811. (skip-chars-backward "^<\r\n")
  5812. (skip-chars-backward "<")
  5813. (and (looking-at org-tr-regexp)
  5814. (>= (match-end 0) pos)
  5815. (throw 'exit t)))
  5816. nil)))
  5817. (defun org-agenda-get-todos ()
  5818. "Return the TODO information for agenda display."
  5819. (let* ((props (list 'face nil
  5820. 'done-face 'org-done
  5821. 'org-not-done-regexp org-not-done-regexp
  5822. 'mouse-face 'highlight
  5823. 'keymap org-agenda-keymap
  5824. 'help-echo
  5825. (format "mouse-2 or RET jump to org file %s"
  5826. (abbreviate-file-name buffer-file-name))))
  5827. (regexp (concat "[\n\r]\\*+ *\\("
  5828. (if org-select-this-todo-keyword
  5829. (concat "\\<\\(" org-select-this-todo-keyword
  5830. "\\)\\>")
  5831. org-not-done-regexp)
  5832. "[^\n\r]*\\)"))
  5833. (sched-re (concat ".*\n.*?" org-scheduled-time-regexp))
  5834. marker priority category tags
  5835. ee txt)
  5836. (goto-char (point-min))
  5837. (while (re-search-forward regexp nil t)
  5838. (when (not (and org-agenda-todo-ignore-scheduled
  5839. (save-match-data (looking-at sched-re))))
  5840. (goto-char (match-beginning 1))
  5841. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  5842. category (org-get-category)
  5843. tags (org-get-tags-at (point))
  5844. txt (org-format-agenda-item "" (match-string 1) category tags)
  5845. priority
  5846. (+ (org-get-priority txt)
  5847. (if org-todo-kwd-priority-p
  5848. (- org-todo-kwd-max-priority -2
  5849. (length
  5850. (member (match-string 2) org-todo-keywords)))
  5851. 1)))
  5852. (org-add-props txt props
  5853. 'org-marker marker 'org-hd-marker marker
  5854. 'priority priority 'category category)
  5855. (push txt ee)
  5856. (if org-agenda-todo-list-sublevels ; FIXME???? Change needed?
  5857. (goto-char (match-end 1))
  5858. (org-end-of-subtree 'invisible))))
  5859. (nreverse ee)))
  5860. (defconst org-agenda-no-heading-message
  5861. "No heading for this item in buffer or region.")
  5862. (defun org-agenda-get-timestamps ()
  5863. "Return the date stamp information for agenda display."
  5864. (let* ((props (list 'face nil
  5865. 'org-not-done-regexp org-not-done-regexp
  5866. 'mouse-face 'highlight
  5867. 'keymap org-agenda-keymap
  5868. 'help-echo
  5869. (format "mouse-2 or RET jump to org file %s"
  5870. (abbreviate-file-name buffer-file-name))))
  5871. (regexp (regexp-quote
  5872. (substring
  5873. (format-time-string
  5874. (car org-time-stamp-formats)
  5875. (apply 'encode-time ; DATE bound by calendar
  5876. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5877. 0 11)))
  5878. marker hdmarker deadlinep scheduledp donep tmp priority category
  5879. ee txt timestr tags)
  5880. (goto-char (point-min))
  5881. (while (re-search-forward regexp nil t)
  5882. (if (not (save-match-data (org-at-date-range-p)))
  5883. (progn
  5884. (setq marker (org-agenda-new-marker (match-beginning 0))
  5885. category (org-get-category (match-beginning 0))
  5886. tmp (buffer-substring (max (point-min)
  5887. (- (match-beginning 0)
  5888. org-ds-keyword-length))
  5889. (match-beginning 0))
  5890. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5891. deadlinep (string-match org-deadline-regexp tmp)
  5892. scheduledp (string-match org-scheduled-regexp tmp)
  5893. donep (org-entry-is-done-p))
  5894. (if (string-match ">" timestr)
  5895. ;; substring should only run to end of time stamp
  5896. (setq timestr (substring timestr 0 (match-end 0))))
  5897. (save-excursion
  5898. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5899. (progn
  5900. (goto-char (match-end 1))
  5901. (setq hdmarker (org-agenda-new-marker)
  5902. tags (org-get-tags-at))
  5903. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5904. (setq txt (org-format-agenda-item
  5905. (format "%s%s"
  5906. (if deadlinep "Deadline: " "")
  5907. (if scheduledp "Scheduled: " ""))
  5908. (match-string 1) category tags timestr)))
  5909. (setq txt org-agenda-no-heading-message))
  5910. (setq priority (org-get-priority txt))
  5911. (org-add-props txt props
  5912. 'org-marker marker 'org-hd-marker hdmarker)
  5913. (if deadlinep
  5914. (org-add-props txt nil
  5915. 'face (if donep 'org-done 'org-warning)
  5916. 'undone-face 'org-warning 'done-face 'org-done
  5917. 'category category 'priority (+ 100 priority))
  5918. (if scheduledp
  5919. (org-add-props txt nil
  5920. 'face 'org-scheduled-today
  5921. 'undone-face 'org-scheduled-today 'done-face 'org-done
  5922. 'category category 'priority (+ 99 priority))
  5923. (org-add-props txt nil 'priority priority 'category category)))
  5924. (push txt ee))
  5925. (outline-next-heading))))
  5926. (nreverse ee)))
  5927. (defun org-agenda-get-closed ()
  5928. "Return the logged TODO entries for agenda display."
  5929. (let* ((props (list 'mouse-face 'highlight
  5930. 'org-not-done-regexp org-not-done-regexp
  5931. 'keymap org-agenda-keymap
  5932. 'help-echo
  5933. (format "mouse-2 or RET jump to org file %s"
  5934. (abbreviate-file-name buffer-file-name))))
  5935. (regexp (concat
  5936. "\\<" org-closed-string " *\\["
  5937. (regexp-quote
  5938. (substring
  5939. (format-time-string
  5940. (car org-time-stamp-formats)
  5941. (apply 'encode-time ; DATE bound by calendar
  5942. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5943. 1 11))))
  5944. marker hdmarker priority category tags
  5945. ee txt timestr)
  5946. (goto-char (point-min))
  5947. (while (re-search-forward regexp nil t)
  5948. (if (not (save-match-data (org-at-date-range-p)))
  5949. (progn
  5950. (setq marker (org-agenda-new-marker (match-beginning 0))
  5951. category (org-get-category (match-beginning 0))
  5952. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5953. ;; donep (org-entry-is-done-p)
  5954. )
  5955. (if (string-match "\\]" timestr)
  5956. ;; substring should only run to end of time stamp
  5957. (setq timestr (substring timestr 0 (match-end 0))))
  5958. (save-excursion
  5959. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5960. (progn
  5961. (goto-char (match-end 1))
  5962. (setq hdmarker (org-agenda-new-marker)
  5963. tags (org-get-tags-at))
  5964. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5965. (setq txt (org-format-agenda-item
  5966. "Closed: "
  5967. (match-string 1) category tags timestr)))
  5968. (setq txt org-agenda-no-heading-message))
  5969. (setq priority 100000)
  5970. (org-add-props txt props
  5971. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  5972. 'priority priority 'category category
  5973. 'undone-face 'org-warning 'done-face 'org-done)
  5974. (push txt ee))
  5975. (outline-next-heading))))
  5976. (nreverse ee)))
  5977. (defun org-agenda-get-deadlines ()
  5978. "Return the deadline information for agenda display."
  5979. (let* ((wdays org-deadline-warning-days)
  5980. (props (list 'mouse-face 'highlight
  5981. 'org-not-done-regexp org-not-done-regexp
  5982. 'keymap org-agenda-keymap
  5983. 'help-echo
  5984. (format "mouse-2 or RET jump to org file %s"
  5985. (abbreviate-file-name buffer-file-name))))
  5986. (regexp org-deadline-time-regexp)
  5987. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  5988. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  5989. d2 diff pos pos1 category tags
  5990. ee txt head)
  5991. (goto-char (point-min))
  5992. (while (re-search-forward regexp nil t)
  5993. (setq pos (1- (match-beginning 1))
  5994. d2 (time-to-days
  5995. (org-time-string-to-time (match-string 1)))
  5996. diff (- d2 d1))
  5997. ;; When to show a deadline in the calendar:
  5998. ;; If the expiration is within wdays warning time.
  5999. ;; Past-due deadlines are only shown on the current date
  6000. (if (and (< diff wdays) todayp (not (= diff 0)))
  6001. (save-excursion
  6002. (setq category (org-get-category))
  6003. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  6004. (progn
  6005. (goto-char (match-end 0))
  6006. (setq pos1 (match-end 1))
  6007. (setq tags (org-get-tags-at pos1))
  6008. (setq head (buffer-substring-no-properties
  6009. (point)
  6010. (progn (skip-chars-forward "^\r\n")
  6011. (point))))
  6012. (if (string-match org-looking-at-done-regexp head)
  6013. (setq txt nil)
  6014. (setq txt (org-format-agenda-item
  6015. (format "In %3d d.: " diff) head category tags))))
  6016. (setq txt org-agenda-no-heading-message))
  6017. (when txt
  6018. (org-add-props txt props
  6019. 'org-marker (org-agenda-new-marker pos)
  6020. 'org-hd-marker (org-agenda-new-marker pos1)
  6021. 'priority (+ (- 10 diff) (org-get-priority txt))
  6022. 'category category
  6023. 'face (cond ((<= diff 0) 'org-warning)
  6024. ((<= diff 5) 'org-scheduled-previously)
  6025. (t nil))
  6026. 'undone-face (cond
  6027. ((<= diff 0) 'org-warning)
  6028. ((<= diff 5) 'org-scheduled-previously)
  6029. (t nil))
  6030. 'done-face 'org-done)
  6031. (push txt ee)))))
  6032. ee))
  6033. (defun org-agenda-get-scheduled ()
  6034. "Return the scheduled information for agenda display."
  6035. (let* ((props (list 'face 'org-scheduled-previously
  6036. 'org-not-done-regexp org-not-done-regexp
  6037. 'undone-face 'org-scheduled-previously
  6038. 'done-face 'org-done
  6039. 'mouse-face 'highlight
  6040. 'keymap org-agenda-keymap
  6041. 'help-echo
  6042. (format "mouse-2 or RET jump to org file %s"
  6043. (abbreviate-file-name buffer-file-name))))
  6044. (regexp org-scheduled-time-regexp)
  6045. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  6046. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  6047. d2 diff pos pos1 category tags
  6048. ee txt head)
  6049. (goto-char (point-min))
  6050. (while (re-search-forward regexp nil t)
  6051. (setq pos (1- (match-beginning 1))
  6052. d2 (time-to-days
  6053. (org-time-string-to-time (match-string 1)))
  6054. diff (- d2 d1))
  6055. ;; When to show a scheduled item in the calendar:
  6056. ;; If it is on or past the date.
  6057. (if (and (< diff 0) todayp)
  6058. (save-excursion
  6059. (setq category (org-get-category))
  6060. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  6061. (progn
  6062. (goto-char (match-end 0))
  6063. (setq pos1 (match-end 1))
  6064. (setq tags (org-get-tags-at))
  6065. (setq head (buffer-substring-no-properties
  6066. (point)
  6067. (progn (skip-chars-forward "^\r\n") (point))))
  6068. (if (string-match org-looking-at-done-regexp head)
  6069. (setq txt nil)
  6070. (setq txt (org-format-agenda-item
  6071. (format "Sched.%2dx: " (- 1 diff)) head
  6072. category tags))))
  6073. (setq txt org-agenda-no-heading-message))
  6074. (when txt
  6075. (org-add-props txt props
  6076. 'org-marker (org-agenda-new-marker pos)
  6077. 'org-hd-marker (org-agenda-new-marker pos1)
  6078. 'priority (+ (- 5 diff) (org-get-priority txt))
  6079. 'category category)
  6080. (push txt ee)))))
  6081. ee))
  6082. (defun org-agenda-get-blocks ()
  6083. "Return the date-range information for agenda display."
  6084. (let* ((props (list 'face nil
  6085. 'org-not-done-regexp org-not-done-regexp
  6086. 'mouse-face 'highlight
  6087. 'keymap org-agenda-keymap
  6088. 'help-echo
  6089. (format "mouse-2 or RET jump to org file %s"
  6090. (abbreviate-file-name buffer-file-name))))
  6091. (regexp org-tr-regexp)
  6092. (d0 (calendar-absolute-from-gregorian date))
  6093. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags)
  6094. (goto-char (point-min))
  6095. (while (re-search-forward regexp nil t)
  6096. (setq timestr (match-string 0)
  6097. s1 (match-string 1)
  6098. s2 (match-string 2)
  6099. d1 (time-to-days (org-time-string-to-time s1))
  6100. d2 (time-to-days (org-time-string-to-time s2)))
  6101. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  6102. ;; Only allow days between the limits, because the normal
  6103. ;; date stamps will catch the limits.
  6104. (save-excursion
  6105. (setq marker (org-agenda-new-marker (point)))
  6106. (setq category (org-get-category))
  6107. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  6108. (progn
  6109. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  6110. (goto-char (match-end 1))
  6111. (setq tags (org-get-tags-at))
  6112. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6113. (setq txt (org-format-agenda-item
  6114. (format (if (= d1 d2) "" "(%d/%d): ")
  6115. (1+ (- d0 d1)) (1+ (- d2 d1)))
  6116. (match-string 1) category tags
  6117. (if (= d0 d1) timestr))))
  6118. (setq txt org-agenda-no-heading-message))
  6119. (org-add-props txt props
  6120. 'org-marker marker 'org-hd-marker hdmarker
  6121. 'priority (org-get-priority txt) 'category category)
  6122. (push txt ee)))
  6123. (outline-next-heading))
  6124. ;; Sort the entries by expiration date.
  6125. (nreverse ee)))
  6126. (defconst org-plain-time-of-day-regexp
  6127. (concat
  6128. "\\(\\<[012]?[0-9]"
  6129. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  6130. "\\(--?"
  6131. "\\(\\<[012]?[0-9]"
  6132. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  6133. "\\)?")
  6134. "Regular expression to match a plain time or time range.
  6135. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  6136. groups carry important information:
  6137. 0 the full match
  6138. 1 the first time, range or not
  6139. 8 the second time, if it is a range.")
  6140. (defconst org-stamp-time-of-day-regexp
  6141. (concat
  6142. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
  6143. "\\([012][0-9]:[0-5][0-9]\\)>"
  6144. "\\(--?"
  6145. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  6146. "Regular expression to match a timestamp time or time range.
  6147. After a match, the following groups carry important information:
  6148. 0 the full match
  6149. 1 date plus weekday, for backreferencing to make sure both times on same day
  6150. 2 the first time, range or not
  6151. 4 the second time, if it is a range.")
  6152. (defvar org-prefix-has-time nil
  6153. "A flag, set by `org-compile-prefix-format'.
  6154. The flag is set if the currently compiled format contains a `%t'.")
  6155. (defvar org-prefix-has-tag nil
  6156. "A flag, set by `org-compile-prefix-format'.
  6157. The flag is set if the currently compiled format contains a `%T'.")
  6158. (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
  6159. "Format TXT to be inserted into the agenda buffer.
  6160. In particular, it adds the prefix and corresponding text properties. EXTRA
  6161. must be a string and replaces the `%s' specifier in the prefix format.
  6162. CATEGORY (string, symbol or nil) may be used to overrule the default
  6163. category taken from local variable or file name. It will replace the `%c'
  6164. specifier in the format. DOTIME, when non-nil, indicates that a
  6165. time-of-day should be extracted from TXT for sorting of this entry, and for
  6166. the `%t' specifier in the format. When DOTIME is a string, this string is
  6167. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  6168. only the correctly processes TXT should be returned - this is used by
  6169. `org-agenda-change-all-lines'. TAG can be the tag of the headline."
  6170. (save-match-data
  6171. ;; Diary entries sometimes have extra whitespace at the beginning
  6172. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  6173. (let* ((category (or category
  6174. org-category
  6175. (if buffer-file-name
  6176. (file-name-sans-extension
  6177. (file-name-nondirectory buffer-file-name))
  6178. "")))
  6179. (tag (if tags (nth (1- (length tags)) tags) ""))
  6180. time ;; needed for the eval of the prefix format
  6181. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  6182. (time-of-day (and dotime (org-get-time-of-day ts)))
  6183. stamp plain s0 s1 s2 rtn)
  6184. (when (and dotime time-of-day org-prefix-has-time)
  6185. ;; Extract starting and ending time and move them to prefix
  6186. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  6187. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  6188. (setq s0 (match-string 0 ts)
  6189. s1 (match-string (if plain 1 2) ts)
  6190. s2 (match-string (if plain 8 4) ts))
  6191. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  6192. ;; them, we might want to remove them there to avoid duplication.
  6193. ;; The user can turn this off with a variable.
  6194. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  6195. (string-match (concat (regexp-quote s0) " *") txt)
  6196. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  6197. (= (match-beginning 0) 0)
  6198. t))
  6199. (setq txt (replace-match "" nil nil txt))))
  6200. ;; Normalize the time(s) to 24 hour
  6201. (if s1 (setq s1 (org-get-time-of-day s1 'string)))
  6202. (if s2 (setq s2 (org-get-time-of-day s2 'string))))
  6203. ; (when (and (or (eq org-agenda-remove-tags-when-in-prefix t)
  6204. ; (and org-agenda-remove-tags-when-in-prefix
  6205. ; org-prefix-has-tag))
  6206. ; (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" txt))
  6207. ; (setq txt (replace-match "" t t txt)))
  6208. ;; FIXME!!!
  6209. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  6210. ;; Tags are in the string
  6211. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  6212. (and org-agenda-remove-tags-when-in-prefix
  6213. org-prefix-has-tag))
  6214. (setq txt (replace-match "" t t txt))
  6215. (setq txt (replace-match
  6216. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  6217. (match-string 2 txt))
  6218. t t txt))))
  6219. ;; Create the final string
  6220. (if noprefix
  6221. (setq rtn txt)
  6222. ;; Prepare the variables needed in the eval of the compiled format
  6223. (setq time (cond (s2 (concat s1 "-" s2))
  6224. (s1 (concat s1 "......"))
  6225. (t ""))
  6226. extra (or extra "")
  6227. category (if (symbolp category) (symbol-name category) category))
  6228. ;; Evaluate the compiled format
  6229. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  6230. ;; And finally add the text properties
  6231. (org-add-props rtn nil
  6232. 'category (downcase category) 'tags tags
  6233. 'prefix-length (- (length rtn) (length txt))
  6234. 'time-of-day time-of-day
  6235. 'dotime dotime))))
  6236. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  6237. (catch 'exit
  6238. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  6239. ((and todayp (member 'today (car org-agenda-time-grid))))
  6240. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  6241. ((member 'weekly (car org-agenda-time-grid)))
  6242. (t (throw 'exit list)))
  6243. (let* ((have (delq nil (mapcar
  6244. (lambda (x) (get-text-property 1 'time-of-day x))
  6245. list)))
  6246. (string (nth 1 org-agenda-time-grid))
  6247. (gridtimes (nth 2 org-agenda-time-grid))
  6248. (req (car org-agenda-time-grid))
  6249. (remove (member 'remove-match req))
  6250. new time)
  6251. (if (and (member 'require-timed req) (not have))
  6252. ;; don't show empty grid
  6253. (throw 'exit list))
  6254. (while (setq time (pop gridtimes))
  6255. (unless (and remove (member time have))
  6256. (setq time (int-to-string time))
  6257. (push (org-format-agenda-item
  6258. nil string "" nil
  6259. (concat (substring time 0 -2) ":" (substring time -2)))
  6260. new)
  6261. (put-text-property
  6262. 1 (length (car new)) 'face 'org-time-grid (car new))))
  6263. (if (member 'time-up org-agenda-sorting-strategy)
  6264. (append new list)
  6265. (append list new)))))
  6266. (defun org-compile-prefix-format (format)
  6267. "Compile the prefix format into a Lisp form that can be evaluated.
  6268. The resulting form is returned and stored in the variable
  6269. `org-prefix-format-compiled'."
  6270. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  6271. (let ((start 0) varform vars var (s format)e c f opt)
  6272. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  6273. s start)
  6274. (setq var (cdr (assoc (match-string 4 s)
  6275. '(("c" . category) ("t" . time) ("s" . extra)
  6276. ("T" . tag))))
  6277. c (or (match-string 3 s) "")
  6278. opt (match-beginning 1)
  6279. start (1+ (match-beginning 0)))
  6280. (if (equal var 'time) (setq org-prefix-has-time t))
  6281. (if (equal var 'tag) (setq org-prefix-has-tag t))
  6282. (setq f (concat "%" (match-string 2 s) "s"))
  6283. (if opt
  6284. (setq varform
  6285. `(if (equal "" ,var)
  6286. ""
  6287. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  6288. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  6289. (setq s (replace-match "%s" t nil s))
  6290. (push varform vars))
  6291. (setq vars (nreverse vars))
  6292. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  6293. (defun org-get-time-of-day (s &optional string)
  6294. "Check string S for a time of day.
  6295. If found, return it as a military time number between 0 and 2400.
  6296. If not found, return nil.
  6297. The optional STRING argument forces conversion into a 5 character wide string
  6298. HH:MM."
  6299. (save-match-data
  6300. (when
  6301. (or
  6302. (string-match
  6303. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  6304. (string-match
  6305. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  6306. (let* ((t0 (+ (* 100
  6307. (+ (string-to-number (match-string 1 s))
  6308. (if (and (match-beginning 4)
  6309. (equal (downcase (match-string 4 s)) "pm"))
  6310. 12 0)))
  6311. (if (match-beginning 3)
  6312. (string-to-number (match-string 3 s))
  6313. 0)))
  6314. (t1 (concat " "
  6315. (if (< t0 100) "0" "") (if (< t0 10) "0" "")
  6316. (int-to-string t0))))
  6317. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  6318. (defun org-finalize-agenda-entries (list)
  6319. "Sort and concatenate the agenda items."
  6320. (setq list (mapcar 'org-agenda-highlight-todo list))
  6321. (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
  6322. (defun org-agenda-highlight-todo (x)
  6323. (let (re)
  6324. (if (eq x 'line)
  6325. (save-excursion
  6326. (beginning-of-line 1)
  6327. (setq re (get-text-property (point) 'org-not-done-regexp))
  6328. (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
  6329. (and (looking-at (concat "[ \t]*" re))
  6330. (add-text-properties (match-beginning 0) (match-end 0)
  6331. '(face org-todo))))
  6332. (setq re (get-text-property 0 'org-not-done-regexp x))
  6333. (and re (string-match re x)
  6334. (add-text-properties (match-beginning 0) (match-end 0)
  6335. '(face org-todo) x))
  6336. x)))
  6337. (defsubst org-cmp-priority (a b)
  6338. "Compare the priorities of string A and B."
  6339. (let ((pa (or (get-text-property 1 'priority a) 0))
  6340. (pb (or (get-text-property 1 'priority b) 0)))
  6341. (cond ((> pa pb) +1)
  6342. ((< pa pb) -1)
  6343. (t nil))))
  6344. (defsubst org-cmp-category (a b)
  6345. "Compare the string values of categories of strings A and B."
  6346. (let ((ca (or (get-text-property 1 'category a) ""))
  6347. (cb (or (get-text-property 1 'category b) "")))
  6348. (cond ((string-lessp ca cb) -1)
  6349. ((string-lessp cb ca) +1)
  6350. (t nil))))
  6351. (defsubst org-cmp-time (a b)
  6352. "Compare the time-of-day values of strings A and B."
  6353. (let* ((def (if org-sort-agenda-notime-is-late 2401 -1))
  6354. (ta (or (get-text-property 1 'time-of-day a) def))
  6355. (tb (or (get-text-property 1 'time-of-day b) def)))
  6356. (cond ((< ta tb) -1)
  6357. ((< tb ta) +1)
  6358. (t nil))))
  6359. (defun org-entries-lessp (a b)
  6360. "Predicate for sorting agenda entries."
  6361. ;; The following variables will be used when the form is evaluated.
  6362. (let* ((time-up (org-cmp-time a b))
  6363. (time-down (if time-up (- time-up) nil))
  6364. (priority-up (org-cmp-priority a b))
  6365. (priority-down (if priority-up (- priority-up) nil))
  6366. (category-up (org-cmp-category a b))
  6367. (category-down (if category-up (- category-up) nil))
  6368. (category-keep (if category-up +1 nil)))
  6369. (cdr (assoc
  6370. (eval (cons 'or org-agenda-sorting-strategy))
  6371. '((-1 . t) (1 . nil) (nil . nil))))))
  6372. (defun org-agenda-show-priority ()
  6373. "Show the priority of the current item.
  6374. This priority is composed of the main priority given with the [#A] cookies,
  6375. and by additional input from the age of a schedules or deadline entry."
  6376. (interactive)
  6377. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  6378. (message "Priority is %d" (if pri pri -1000))))
  6379. (defun org-agenda-show-tags ()
  6380. "Show the tags applicable to the current item."
  6381. (interactive)
  6382. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  6383. (if tags
  6384. (message "Tags are :%s:"
  6385. (org-no-properties (mapconcat 'identity tags ":")))
  6386. (message "No tags associated with this line"))))
  6387. (defun org-agenda-goto (&optional highlight)
  6388. "Go to the Org-mode file which contains the item at point."
  6389. (interactive)
  6390. (let* ((marker (or (get-text-property (point) 'org-marker)
  6391. (org-agenda-error)))
  6392. (buffer (marker-buffer marker))
  6393. (pos (marker-position marker)))
  6394. (switch-to-buffer-other-window buffer)
  6395. (widen)
  6396. (goto-char pos)
  6397. (when (eq major-mode 'org-mode)
  6398. (org-show-hidden-entry)
  6399. (save-excursion
  6400. (and (outline-next-heading)
  6401. (org-flag-heading nil)))) ; show the next heading
  6402. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  6403. (defun org-agenda-switch-to ()
  6404. "Go to the Org-mode file which contains the item at point."
  6405. (interactive)
  6406. (let* ((marker (or (get-text-property (point) 'org-marker)
  6407. (org-agenda-error)))
  6408. (buffer (marker-buffer marker))
  6409. (pos (marker-position marker)))
  6410. (switch-to-buffer buffer)
  6411. (delete-other-windows)
  6412. (widen)
  6413. (goto-char pos)
  6414. (when (eq major-mode 'org-mode)
  6415. (org-show-hidden-entry)
  6416. (save-excursion
  6417. (and (outline-next-heading)
  6418. (org-flag-heading nil)))))) ; show the next heading
  6419. (defun org-agenda-goto-mouse (ev)
  6420. "Go to the Org-mode file which contains the item at the mouse click."
  6421. (interactive "e")
  6422. (mouse-set-point ev)
  6423. (org-agenda-goto))
  6424. (defun org-agenda-show ()
  6425. "Display the Org-mode file which contains the item at point."
  6426. (interactive)
  6427. (let ((win (selected-window)))
  6428. (org-agenda-goto t)
  6429. (select-window win)))
  6430. (defun org-agenda-recenter (arg)
  6431. "Display the Org-mode file which contains the item at point and recenter."
  6432. (interactive "P")
  6433. (let ((win (selected-window)))
  6434. (org-agenda-goto t)
  6435. (recenter arg)
  6436. (select-window win)))
  6437. (defun org-agenda-show-mouse (ev)
  6438. "Display the Org-mode file which contains the item at the mouse click."
  6439. (interactive "e")
  6440. (mouse-set-point ev)
  6441. (org-agenda-show))
  6442. (defun org-agenda-check-no-diary ()
  6443. "Check if the entry is a diary link and abort if yes."
  6444. (if (get-text-property (point) 'org-agenda-diary-link)
  6445. (org-agenda-error)))
  6446. (defun org-agenda-error ()
  6447. (error "Command not allowed in this line"))
  6448. (defvar org-last-heading-marker (make-marker)
  6449. "Marker pointing to the headline that last changed its TODO state
  6450. by a remote command from the agenda.")
  6451. (defun org-agenda-todo (&optional arg)
  6452. "Cycle TODO state of line at point, also in Org-mode file.
  6453. This changes the line at point, all other lines in the agenda referring to
  6454. the same tree node, and the headline of the tree node in the Org-mode file."
  6455. (interactive "P")
  6456. (org-agenda-check-no-diary)
  6457. (let* ((col (current-column))
  6458. (marker (or (get-text-property (point) 'org-marker)
  6459. (org-agenda-error)))
  6460. (buffer (marker-buffer marker))
  6461. (pos (marker-position marker))
  6462. (hdmarker (get-text-property (point) 'org-hd-marker))
  6463. (buffer-read-only nil)
  6464. newhead)
  6465. (with-current-buffer buffer
  6466. (widen)
  6467. (goto-char pos)
  6468. (org-show-hidden-entry)
  6469. (save-excursion
  6470. (and (outline-next-heading)
  6471. (org-flag-heading nil))) ; show the next heading
  6472. (org-todo arg)
  6473. (and (bolp) (forward-char 1))
  6474. (setq newhead (org-get-heading))
  6475. (save-excursion
  6476. (org-back-to-heading)
  6477. (move-marker org-last-heading-marker (point))))
  6478. (beginning-of-line 1)
  6479. (save-excursion
  6480. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  6481. (move-to-column col)))
  6482. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  6483. "Change all lines in the agenda buffer which match HDMARKER.
  6484. The new content of the line will be NEWHEAD (as modified by
  6485. `org-format-agenda-item'). HDMARKER is checked with
  6486. `equal' against all `org-hd-marker' text properties in the file.
  6487. If FIXFACE is non-nil, the face of each item is modified acording to
  6488. the new TODO state."
  6489. (let* (props m pl undone-face done-face finish new dotime cat tags)
  6490. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  6491. (save-excursion
  6492. (goto-char (point-max))
  6493. (beginning-of-line 1)
  6494. (while (not finish)
  6495. (setq finish (bobp))
  6496. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  6497. (equal m hdmarker))
  6498. (setq props (text-properties-at (point))
  6499. dotime (get-text-property (point) 'dotime)
  6500. cat (get-text-property (point) 'category)
  6501. tags (get-text-property (point) 'tags)
  6502. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  6503. pl (get-text-property (point) 'prefix-length)
  6504. undone-face (get-text-property (point) 'undone-face)
  6505. done-face (get-text-property (point) 'done-face))
  6506. (move-to-column pl)
  6507. (if (looking-at ".*")
  6508. (progn
  6509. (replace-match new t t)
  6510. (beginning-of-line 1)
  6511. (add-text-properties (point-at-bol) (point-at-eol) props)
  6512. (when fixface
  6513. (add-text-properties
  6514. (point-at-bol) (point-at-eol)
  6515. (list 'face
  6516. (if org-last-todo-state-is-todo
  6517. undone-face done-face)))
  6518. (org-agenda-highlight-todo 'line))
  6519. (beginning-of-line 1))
  6520. (error "Line update did not work")))
  6521. (beginning-of-line 0)))))
  6522. (defun org-agenda-priority-up ()
  6523. "Increase the priority of line at point, also in Org-mode file."
  6524. (interactive)
  6525. (org-agenda-priority 'up))
  6526. (defun org-agenda-priority-down ()
  6527. "Decrease the priority of line at point, also in Org-mode file."
  6528. (interactive)
  6529. (org-agenda-priority 'down))
  6530. (defun org-agenda-priority (&optional force-direction)
  6531. "Set the priority of line at point, also in Org-mode file.
  6532. This changes the line at point, all other lines in the agenda referring to
  6533. the same tree node, and the headline of the tree node in the Org-mode file."
  6534. (interactive)
  6535. (org-agenda-check-no-diary)
  6536. (let* ((marker (or (get-text-property (point) 'org-marker)
  6537. (org-agenda-error)))
  6538. (buffer (marker-buffer marker))
  6539. (pos (marker-position marker))
  6540. (hdmarker (get-text-property (point) 'org-hd-marker))
  6541. (buffer-read-only nil)
  6542. newhead)
  6543. (with-current-buffer buffer
  6544. (widen)
  6545. (goto-char pos)
  6546. (org-show-hidden-entry)
  6547. (save-excursion
  6548. (and (outline-next-heading)
  6549. (org-flag-heading nil))) ; show the next heading
  6550. (funcall 'org-priority force-direction)
  6551. (end-of-line 1)
  6552. (setq newhead (org-get-heading)))
  6553. (org-agenda-change-all-lines newhead hdmarker)
  6554. (beginning-of-line 1)))
  6555. (defun org-get-tags-at (&optional pos)
  6556. "Get a list of all headline tags applicable at POS.
  6557. POS defaults to point. If tags are inherited, the list contains
  6558. the targets in the same sequence as the headlines appear, i.e.
  6559. the tags of the current headline come last."
  6560. (interactive)
  6561. (let (tags)
  6562. (save-excursion
  6563. (goto-char (or pos (point)))
  6564. (save-match-data
  6565. (org-back-to-heading t)
  6566. (condition-case nil
  6567. (while t
  6568. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  6569. (setq tags (append (org-split-string
  6570. (org-match-string-no-properties 1) ":")
  6571. tags)))
  6572. (or org-use-tag-inheritance (error ""))
  6573. (org-up-heading-all 1))
  6574. (error nil))))
  6575. (message "%s" tags)
  6576. tags))
  6577. (defun org-agenda-set-tags ()
  6578. "Set tags for the current headline."
  6579. (interactive)
  6580. (org-agenda-check-no-diary)
  6581. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  6582. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  6583. (org-agenda-error)))
  6584. (buffer (marker-buffer hdmarker))
  6585. (pos (marker-position hdmarker))
  6586. (buffer-read-only nil)
  6587. newhead)
  6588. (with-current-buffer buffer
  6589. (widen)
  6590. (goto-char pos)
  6591. (org-show-hidden-entry)
  6592. (save-excursion
  6593. (and (outline-next-heading)
  6594. (org-flag-heading nil))) ; show the next heading
  6595. (call-interactively 'org-set-tags)
  6596. (end-of-line 1)
  6597. (setq newhead (org-get-heading)))
  6598. (org-agenda-change-all-lines newhead hdmarker)
  6599. (beginning-of-line 1)))
  6600. (defun org-agenda-date-later (arg &optional what)
  6601. "Change the date of this item to one day later."
  6602. (interactive "p")
  6603. (org-agenda-check-type t 'agenda 'timeline)
  6604. (org-agenda-check-no-diary)
  6605. (let* ((marker (or (get-text-property (point) 'org-marker)
  6606. (org-agenda-error)))
  6607. (buffer (marker-buffer marker))
  6608. (pos (marker-position marker)))
  6609. (with-current-buffer buffer
  6610. (widen)
  6611. (goto-char pos)
  6612. (if (not (org-at-timestamp-p))
  6613. (error "Cannot find time stamp"))
  6614. (org-timestamp-change arg (or what 'day))
  6615. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6616. (defun org-agenda-date-earlier (arg &optional what)
  6617. "Change the date of this item to one day earlier."
  6618. (interactive "p")
  6619. (org-agenda-date-later (- arg) what))
  6620. (defun org-agenda-date-prompt (arg)
  6621. "Change the date of this item. Date is prompted for, with default today.
  6622. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  6623. be used to request time specification in the time stamp."
  6624. (interactive "P")
  6625. (org-agenda-check-type t 'agenda 'timeline)
  6626. (org-agenda-check-no-diary)
  6627. (let* ((marker (or (get-text-property (point) 'org-marker)
  6628. (org-agenda-error)))
  6629. (buffer (marker-buffer marker))
  6630. (pos (marker-position marker)))
  6631. (with-current-buffer buffer
  6632. (widen)
  6633. (goto-char pos)
  6634. (if (not (org-at-timestamp-p))
  6635. (error "Cannot find time stamp"))
  6636. (org-time-stamp arg)
  6637. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6638. (defun org-agenda-schedule (arg)
  6639. "Schedule the item at point."
  6640. (interactive "P")
  6641. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  6642. (org-agenda-check-no-diary)
  6643. (let* ((marker (or (get-text-property (point) 'org-marker)
  6644. (org-agenda-error)))
  6645. (buffer (marker-buffer marker))
  6646. (pos (marker-position marker))
  6647. (org-insert-labeled-timestamps-at-point nil)
  6648. ts)
  6649. (with-current-buffer buffer
  6650. (widen)
  6651. (goto-char pos)
  6652. (setq ts (org-schedule))
  6653. (message "Item scheduled for %s" ts))))
  6654. (defun org-agenda-deadline (arg)
  6655. "Schedule the item at point."
  6656. (interactive "P")
  6657. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  6658. (org-agenda-check-no-diary)
  6659. (let* ((marker (or (get-text-property (point) 'org-marker)
  6660. (org-agenda-error)))
  6661. (buffer (marker-buffer marker))
  6662. (pos (marker-position marker))
  6663. (org-insert-labeled-timestamps-at-point nil)
  6664. ts)
  6665. (with-current-buffer buffer
  6666. (widen)
  6667. (goto-char pos)
  6668. (setq ts (org-deadline))
  6669. (message "Deadline for this item set to %s" ts))))
  6670. (defun org-get-heading ()
  6671. "Return the heading of the current entry, without the stars."
  6672. (save-excursion
  6673. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  6674. (if (and (re-search-backward "[\r\n]\\*" nil t)
  6675. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  6676. (match-string 1)
  6677. "")))
  6678. (defun org-agenda-diary-entry ()
  6679. "Make a diary entry, like the `i' command from the calendar.
  6680. All the standard commands work: block, weekly etc."
  6681. (interactive)
  6682. (org-agenda-check-type t 'agenda 'timeline)
  6683. (require 'diary-lib)
  6684. (let* ((char (progn
  6685. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  6686. (read-char-exclusive)))
  6687. (cmd (cdr (assoc char
  6688. '((?d . insert-diary-entry)
  6689. (?w . insert-weekly-diary-entry)
  6690. (?m . insert-monthly-diary-entry)
  6691. (?y . insert-yearly-diary-entry)
  6692. (?a . insert-anniversary-diary-entry)
  6693. (?b . insert-block-diary-entry)
  6694. (?c . insert-cyclic-diary-entry)))))
  6695. (oldf (symbol-function 'calendar-cursor-to-date))
  6696. (point (point))
  6697. (mark (or (mark t) (point))))
  6698. (unless cmd
  6699. (error "No command associated with <%c>" char))
  6700. (unless (and (get-text-property point 'day)
  6701. (or (not (equal ?b char))
  6702. (get-text-property mark 'day)))
  6703. (error "Don't know which date to use for diary entry"))
  6704. ;; We implement this by hacking the `calendar-cursor-to-date' function
  6705. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  6706. (let ((calendar-mark-ring
  6707. (list (calendar-gregorian-from-absolute
  6708. (or (get-text-property mark 'day)
  6709. (get-text-property point 'day))))))
  6710. (unwind-protect
  6711. (progn
  6712. (fset 'calendar-cursor-to-date
  6713. (lambda (&optional error)
  6714. (calendar-gregorian-from-absolute
  6715. (get-text-property point 'day))))
  6716. (call-interactively cmd))
  6717. (fset 'calendar-cursor-to-date oldf)))))
  6718. (defun org-agenda-execute-calendar-command (cmd)
  6719. "Execute a calendar command from the agenda, with the date associated to
  6720. the cursor position."
  6721. (org-agenda-check-type t 'agenda 'timeline)
  6722. (require 'diary-lib)
  6723. (unless (get-text-property (point) 'day)
  6724. (error "Don't know which date to use for calendar command"))
  6725. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  6726. (point (point))
  6727. (date (calendar-gregorian-from-absolute
  6728. (get-text-property point 'day)))
  6729. (displayed-day (extract-calendar-day date))
  6730. (displayed-month (extract-calendar-month date))
  6731. (displayed-year (extract-calendar-year date)))
  6732. (unwind-protect
  6733. (progn
  6734. (fset 'calendar-cursor-to-date
  6735. (lambda (&optional error)
  6736. (calendar-gregorian-from-absolute
  6737. (get-text-property point 'day))))
  6738. (call-interactively cmd))
  6739. (fset 'calendar-cursor-to-date oldf))))
  6740. (defun org-agenda-phases-of-moon ()
  6741. "Display the phases of the moon for the 3 months around the cursor date."
  6742. (interactive)
  6743. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  6744. (defun org-agenda-holidays ()
  6745. "Display the holidays for the 3 months around the cursor date."
  6746. (interactive)
  6747. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  6748. (defun org-agenda-sunrise-sunset (arg)
  6749. "Display sunrise and sunset for the cursor date.
  6750. Latitude and longitude can be specified with the variables
  6751. `calendar-latitude' and `calendar-longitude'. When called with prefix
  6752. argument, latitude and longitude will be prompted for."
  6753. (interactive "P")
  6754. (let ((calendar-longitude (if arg nil calendar-longitude))
  6755. (calendar-latitude (if arg nil calendar-latitude))
  6756. (calendar-location-name
  6757. (if arg "the given coordinates" calendar-location-name)))
  6758. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  6759. (defun org-agenda-goto-calendar ()
  6760. "Open the Emacs calendar with the date at the cursor."
  6761. (interactive)
  6762. (org-agenda-check-type t 'agenda 'timeline)
  6763. (let* ((day (or (get-text-property (point) 'day)
  6764. (error "Don't know which date to open in calendar")))
  6765. (date (calendar-gregorian-from-absolute day))
  6766. (calendar-move-hook nil)
  6767. (view-calendar-holidays-initially nil)
  6768. (view-diary-entries-initially nil))
  6769. (calendar)
  6770. (calendar-goto-date date)))
  6771. (defun org-calendar-goto-agenda ()
  6772. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  6773. This is a command that has to be installed in `calendar-mode-map'."
  6774. (interactive)
  6775. (org-agenda-list nil (calendar-absolute-from-gregorian
  6776. (calendar-cursor-to-date))
  6777. nil t))
  6778. (defun org-agenda-convert-date ()
  6779. (interactive)
  6780. (org-agenda-check-type t 'agenda 'timeline)
  6781. (let ((day (get-text-property (point) 'day))
  6782. date s)
  6783. (unless day
  6784. (error "Don't know which date to convert"))
  6785. (setq date (calendar-gregorian-from-absolute day))
  6786. (setq s (concat
  6787. "Gregorian: " (calendar-date-string date) "\n"
  6788. "ISO: " (calendar-iso-date-string date) "\n"
  6789. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  6790. "Julian: " (calendar-julian-date-string date) "\n"
  6791. "Astron. JD: " (calendar-astro-date-string date)
  6792. " (Julian date number at noon UTC)\n"
  6793. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  6794. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  6795. "French: " (calendar-french-date-string date) "\n"
  6796. "Mayan: " (calendar-mayan-date-string date) "\n"
  6797. "Coptic: " (calendar-coptic-date-string date) "\n"
  6798. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  6799. "Persian: " (calendar-persian-date-string date) "\n"
  6800. "Chinese: " (calendar-chinese-date-string date) "\n"))
  6801. (with-output-to-temp-buffer "*Dates*"
  6802. (princ s))
  6803. (if (fboundp 'fit-window-to-buffer)
  6804. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  6805. ;;; Tags
  6806. (defun org-scan-tags (action matcher &optional todo-only)
  6807. "Scan headline tags with inheritance and produce output ACTION.
  6808. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  6809. evaluated, testing if a given set of tags qualifies a headline for
  6810. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  6811. are included in the output."
  6812. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  6813. (mapconcat 'regexp-quote
  6814. (nreverse (cdr (reverse org-todo-keywords)))
  6815. "\\|")
  6816. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  6817. (props (list 'face nil
  6818. 'done-face 'org-done
  6819. 'undone-face nil
  6820. 'mouse-face 'highlight
  6821. 'keymap org-agenda-keymap
  6822. 'help-echo
  6823. (format "mouse-2 or RET jump to org file %s"
  6824. (abbreviate-file-name buffer-file-name))))
  6825. lspos
  6826. tags tags-list tags-alist (llast 0) rtn level category i txt
  6827. todo marker)
  6828. (save-excursion
  6829. (goto-char (point-min))
  6830. (when (eq action 'sparse-tree) (org-overview))
  6831. (while (re-search-forward re nil t)
  6832. (setq todo (if (match-end 1) (match-string 2))
  6833. tags (if (match-end 4) (match-string 4)))
  6834. (goto-char (setq lspos (1+ (match-beginning 0))))
  6835. (setq level (funcall outline-level)
  6836. category (org-get-category))
  6837. (setq i llast llast level)
  6838. ;; remove tag lists from same and sublevels
  6839. (while (>= i level)
  6840. (when (setq entry (assoc i tags-alist))
  6841. (setq tags-alist (delete entry tags-alist)))
  6842. (setq i (1- i)))
  6843. ;; add the nex tags
  6844. (when tags
  6845. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  6846. tags-alist
  6847. (cons (cons level tags) tags-alist)))
  6848. ;; compile tags for current headline
  6849. (setq tags-list
  6850. (if org-use-tag-inheritance
  6851. (apply 'append (mapcar 'cdr tags-alist))
  6852. tags))
  6853. (when (and (or (not todo-only) todo)
  6854. (eval matcher))
  6855. ;; list this headline
  6856. (if (eq action 'sparse-tree)
  6857. (progn
  6858. (org-show-hierarchy-above))
  6859. (setq txt (org-format-agenda-item
  6860. ""
  6861. (concat
  6862. (if org-tags-match-list-sublevels
  6863. (make-string (1- level) ?.) "")
  6864. (org-get-heading))
  6865. category tags-list))
  6866. (goto-char lspos)
  6867. (setq marker (org-agenda-new-marker))
  6868. (org-add-props txt props
  6869. 'org-marker marker 'org-hd-marker marker 'category category)
  6870. (push txt rtn))
  6871. ;; if we are to skip sublevels, jump to end of subtree
  6872. (point)
  6873. (or org-tags-match-list-sublevels (org-end-of-subtree)))))
  6874. (nreverse rtn)))
  6875. (defun org-tags-sparse-tree (&optional arg match)
  6876. "Create a sparse tree according to tags search string MATCH.
  6877. MATCH can contain positive and negative selection of tags, like
  6878. \"+WORK+URGENT-WITHBOSS\"."
  6879. (interactive "P")
  6880. (let ((org-show-following-heading nil)
  6881. (org-show-hierarchy-above nil))
  6882. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  6883. (defun org-make-tags-matcher (match)
  6884. "Create the TAGS matcher form for the tags-selecting string MATCH."
  6885. (unless match
  6886. ;; Get a new match request, with completion
  6887. (setq org-last-tags-completion-table
  6888. (or (org-get-buffer-tags)
  6889. org-last-tags-completion-table))
  6890. (setq match (completing-read
  6891. "Tags: " 'org-tags-completion-function nil nil nil
  6892. 'org-tags-history)))
  6893. ;; parse the string and create a lisp form
  6894. (let ((match0 match) minus tag mm matcher orterms term orlist)
  6895. (setq orterms (org-split-string match "|"))
  6896. (while (setq term (pop orterms))
  6897. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  6898. (setq minus (and (match-end 1)
  6899. (equal (match-string 1 term) "-"))
  6900. tag (match-string 2 term)
  6901. term (substring term (match-end 0))
  6902. mm (list 'member (downcase tag) 'tags-list)
  6903. mm (if minus (list 'not mm) mm))
  6904. (push mm matcher))
  6905. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  6906. orlist)
  6907. (setq matcher nil))
  6908. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  6909. ;; Return the string and lisp forms of the matcher
  6910. (cons match0 matcher)))
  6911. ;;;###autoload
  6912. (defun org-tags-view (&optional todo-only match keep-modes)
  6913. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  6914. The prefix arg TODO-ONLY limits the search to TODO entries."
  6915. (interactive "P")
  6916. (org-agenda-maybe-reset-markers 'force)
  6917. (org-compile-prefix-format org-agenda-prefix-format)
  6918. (let* ((org-agenda-keep-modes keep-modes)
  6919. (org-tags-match-list-sublevels
  6920. (if todo-only t org-tags-match-list-sublevels))
  6921. (win (selected-window))
  6922. (completion-ignore-case t)
  6923. rtn rtnall files file pos matcher
  6924. buffer)
  6925. (setq matcher (org-make-tags-matcher match)
  6926. match (car matcher) matcher (cdr matcher))
  6927. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  6928. (progn
  6929. (delete-other-windows)
  6930. (switch-to-buffer-other-window
  6931. (get-buffer-create org-agenda-buffer-name))))
  6932. (setq buffer-read-only nil)
  6933. (erase-buffer)
  6934. (org-agenda-mode) (setq buffer-read-only nil)
  6935. (set (make-local-variable 'org-agenda-type) 'tags)
  6936. (set (make-local-variable 'org-agenda-redo-command)
  6937. (list 'org-tags-view (list 'quote todo-only)
  6938. (list 'if 'current-prefix-arg nil match) t))
  6939. (setq files (org-agenda-files)
  6940. rtnall nil)
  6941. (while (setq file (pop files))
  6942. (catch 'nextfile
  6943. (org-check-agenda-file file)
  6944. (setq buffer (if (file-exists-p file)
  6945. (org-get-agenda-file-buffer file)
  6946. (error "No such file %s" file)))
  6947. (if (not buffer)
  6948. ;; If file does not exist, merror message to agenda
  6949. (setq rtn (list
  6950. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  6951. rtnall (append rtnall rtn))
  6952. (with-current-buffer buffer
  6953. (unless (eq major-mode 'org-mode)
  6954. (error "Agenda file %s is not in `org-mode'" file))
  6955. (save-excursion
  6956. (save-restriction
  6957. (if org-respect-restriction
  6958. (if (org-region-active-p)
  6959. ;; Respect a region to restrict search
  6960. (narrow-to-region (region-beginning) (region-end)))
  6961. ;; If we work for the calendar or many files,
  6962. ;; get rid of any restriction
  6963. (widen))
  6964. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  6965. (setq rtnall (append rtnall rtn))))))))
  6966. (insert "Headlines with TAGS match: ")
  6967. (add-text-properties (point-min) (1- (point))
  6968. (list 'face 'org-level-3))
  6969. (setq pos (point))
  6970. (insert match "\n")
  6971. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6972. (setq pos (point))
  6973. (insert "Press `C-u r' to search again with new search string\n")
  6974. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  6975. (when rtnall
  6976. (insert (mapconcat 'identity rtnall "\n")))
  6977. (goto-char (point-min))
  6978. (setq buffer-read-only t)
  6979. (org-fit-agenda-window)
  6980. (if (not org-select-agenda-window) (select-window win))))
  6981. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  6982. (defun org-set-tags (&optional arg just-align)
  6983. "Set the tags for the current headline.
  6984. With prefix ARG, realign all tags in headings in the current buffer."
  6985. (interactive "P")
  6986. (let* ((re (concat "^" outline-regexp))
  6987. (col (current-column))
  6988. (current (org-get-tags))
  6989. table current-tags inherited-tags ; computed below when needed
  6990. tags hd empty invis)
  6991. (if arg
  6992. (save-excursion
  6993. (goto-char (point-min))
  6994. (while (re-search-forward re nil t)
  6995. (org-set-tags nil t))
  6996. (message "All tags realigned to column %d" org-tags-column))
  6997. (if just-align
  6998. (setq tags current)
  6999. (setq table (or org-tag-alist (org-get-buffer-tags))
  7000. org-last-tags-completion-table table
  7001. current-tags (org-split-string current ":")
  7002. inherited-tags (nreverse
  7003. (nthcdr (length current-tags)
  7004. (nreverse (org-get-tags-at))))
  7005. tags
  7006. (if (or (eq t org-use-fast-tag-selection)
  7007. (and org-use-fast-tag-selection (cdar table)))
  7008. (org-fast-tag-selection current-tags inherited-tags table)
  7009. (let ((org-add-colon-after-tag-completion t))
  7010. (completing-read "Tags: " 'org-tags-completion-function
  7011. nil nil current 'org-tags-history))))
  7012. (while (string-match "[-+&]+" tags)
  7013. (setq tags (replace-match ":" t t tags))))
  7014. ;; FIXME: still optimize this by not checking when JUST-ALIGN?
  7015. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  7016. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  7017. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  7018. (if (equal current "")
  7019. (progn
  7020. (end-of-line 1)
  7021. (or empty (insert-before-markers " ")))
  7022. (beginning-of-line 1)
  7023. (setq invis (org-invisible-p))
  7024. (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
  7025. (setq hd (match-string 1))
  7026. (delete-region (match-beginning 0) (match-end 0))
  7027. (insert-before-markers (org-trim hd) (if empty "" " ")))
  7028. (unless (equal tags "")
  7029. (move-to-column (max (current-column)
  7030. (if (> org-tags-column 0)
  7031. org-tags-column
  7032. (- (- org-tags-column) (length tags))))
  7033. t)
  7034. (insert-before-markers tags)
  7035. (if (and (not invis) (org-invisible-p))
  7036. (outline-flag-region (point-at-bol) (point) nil)))
  7037. (move-to-column col))))
  7038. (defun org-tags-completion-function (string predicate &optional flag)
  7039. (let (s1 s2 rtn (ctable org-last-tags-completion-table))
  7040. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  7041. (setq s1 (match-string 1 string)
  7042. s2 (match-string 2 string))
  7043. (setq s1 "" s2 string))
  7044. (cond
  7045. ((eq flag nil)
  7046. ;; try completion
  7047. (setq rtn (try-completion s2 ctable))
  7048. (if (stringp rtn)
  7049. (concat s1 s2 (substring rtn (length s2))
  7050. (if (and org-add-colon-after-tag-completion
  7051. (assoc rtn ctable))
  7052. ":" "")))
  7053. )
  7054. ((eq flag t)
  7055. ;; all-completions
  7056. (all-completions s2 ctable)
  7057. )
  7058. ((eq flag 'lambda)
  7059. ;; exact match?
  7060. (assoc s2 ctable)))
  7061. ))
  7062. (defun org-fast-tag-insert (kwd tags face &optional end)
  7063. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  7064. (insert (format "%-12s" (concat kwd ":"))
  7065. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  7066. (or end "")))
  7067. (defun org-fast-tag-selection (current inherited table)
  7068. "Fast tag selection with single keys.
  7069. CURRENT is the current list of tags in the headline, INHERITED is the
  7070. list of inherited tags, and TABLE is an alist of tags and corresponding keys.
  7071. If the keys are nil, a-z are automatically assigned.
  7072. Returns the new tags string, or nil to not change the current settings."
  7073. (let* ((maxlen (apply 'max (mapcar (lambda (x)
  7074. (string-width (car x))) table)))
  7075. (fwidth (+ maxlen 3 1 3))
  7076. (ncol (/ (window-width) fwidth))
  7077. (i-face 'org-done)
  7078. (c-face 'org-tag)
  7079. tg cnt e c char ntable tbl rtn)
  7080. (save-window-excursion
  7081. (delete-other-windows)
  7082. (split-window-vertically)
  7083. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  7084. (erase-buffer)
  7085. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  7086. (org-fast-tag-insert "Current" current c-face "\n\n")
  7087. (setq tbl table char (1- ?a) cnt 0)
  7088. (while (setq e (pop tbl))
  7089. (setq tg (car e) c (or (cdr e) (setq char (1+ char))))
  7090. (setq tg (org-add-props tg nil 'face
  7091. (cond
  7092. ((member tg current) c-face)
  7093. ((member tg inherited) i-face)
  7094. (t nil))))
  7095. (insert "[" c "] " tg (make-string
  7096. (- fwidth 4 (length tg)) ?\ ))
  7097. (push (cons tg c) ntable)
  7098. (when (= (setq cnt (1+ cnt)) ncol)
  7099. (insert "\n")
  7100. (setq cnt 0)))
  7101. (insert "\n")
  7102. (goto-char (point-min))
  7103. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  7104. (setq rtn
  7105. (catch 'exit
  7106. (while t
  7107. (message "[key]:Toggle SPC: clear current RET accept")
  7108. (setq c (read-char-exclusive))
  7109. (cond
  7110. ((= c ?\r) (throw 'exit t))
  7111. ((= c ?\C-g) (throw 'exit nil))
  7112. ((= c ?\ ) (setq current nil))
  7113. (t (setq e (rassoc c ntable) tg (car e))
  7114. (if (member tg current)
  7115. (setq current (delete tg current))
  7116. (setq current (append current (list tg))))))
  7117. (goto-char (point-min))
  7118. (beginning-of-line 2)
  7119. (delete-region (point) (point-at-eol))
  7120. (org-fast-tag-insert "Current" current c-face)
  7121. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  7122. (setq tg (match-string 1))
  7123. (add-text-properties (match-beginning 1) (match-end 1)
  7124. (list 'face
  7125. (cond
  7126. ((member tg current) c-face)
  7127. ((member tg inherited) i-face)
  7128. (t nil)))))
  7129. (goto-char (point-min)))))
  7130. (if rtn
  7131. (mapconcat 'identity current ":")
  7132. nil))))
  7133. (defun org-get-tags ()
  7134. "Get the TAGS string in the current headline."
  7135. (unless (org-on-heading-p)
  7136. (error "Not on a heading"))
  7137. (save-excursion
  7138. (beginning-of-line 1)
  7139. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  7140. (org-match-string-no-properties 1)
  7141. "")))
  7142. (defun org-get-buffer-tags ()
  7143. "Get a table of all tags used in the buffer, for completion."
  7144. (let (tags)
  7145. (save-excursion
  7146. (goto-char (point-min))
  7147. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  7148. (mapc (lambda (x) (add-to-list 'tags x))
  7149. (org-split-string (org-match-string-no-properties 1) ":"))))
  7150. (mapcar 'list tags)))
  7151. ;;; Link Stuff
  7152. (defvar org-create-file-search-functions nil
  7153. "List of functions to construct the right search string for a file link.
  7154. These functions are called in turn with point at the location to
  7155. which the link should point.
  7156. A function in the hook should first test if it would like to
  7157. handle this file type, for example by checking the major-mode or
  7158. the file extension. If it decides not to handle this file, it
  7159. should just return nil to give other functions a chance. If it
  7160. does handle the file, it must return the search string to be used
  7161. when following the link. The search string will be part of the
  7162. file link, given after a double colon, and `org-open-at-point'
  7163. will automatically search for it. If special measures must be
  7164. taken to make the search successful, another function should be
  7165. added to the companion hook `org-execute-file-search-functions',
  7166. which see.
  7167. A function in this hook may also use `setq' to set the variable
  7168. `description' to provide a suggestion for the descriptive text to
  7169. be used for this link when it gets inserted into an Org-mode
  7170. buffer with \\[org-insert-link].")
  7171. (defvar org-execute-file-search-functions nil
  7172. "List of functions to execute a file search triggered by a link.
  7173. Functions added to this hook must accept a single argument, the
  7174. search string that was part of the file link, the part after the
  7175. double colon. The function must first check if it would like to
  7176. handle this search, for example by checking the major-mode or the
  7177. file extension. If it decides not to handle this search, it
  7178. should just return nil to give other functions a chance. If it
  7179. does handle the search, it must return a non-nil value to keep
  7180. other functions from trying.
  7181. Each function can access the current prefix argument through the
  7182. variable `current-prefix-argument'. Note that a single prefix is
  7183. used to force opening a link in Emacs, so it may be good to only
  7184. use a numeric or double prefix to guide the search function.
  7185. In case this is needed, a function in this hook can also restore
  7186. the window configuration before `org-open-at-point' was called using:
  7187. (set-window-configuration org-window-config-before-follow-link)")
  7188. (defun org-find-file-at-mouse (ev)
  7189. "Open file link or URL at mouse."
  7190. (interactive "e")
  7191. (mouse-set-point ev)
  7192. (org-open-at-point 'in-emacs))
  7193. (defun org-open-at-mouse (ev)
  7194. "Open file link or URL at mouse."
  7195. (interactive "e")
  7196. (mouse-set-point ev)
  7197. (org-open-at-point))
  7198. (defvar org-window-config-before-follow-link nil
  7199. "The window configuration before following a link.
  7200. This is saved in case the need arises to restore it.")
  7201. (defun org-open-at-point (&optional in-emacs)
  7202. "Open link at or after point.
  7203. If there is no link at point, this function will search forward up to
  7204. the end of the current subtree.
  7205. Normally, files will be opened by an appropriate application. If the
  7206. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  7207. (interactive "P")
  7208. (setq org-window-config-before-follow-link (current-window-configuration))
  7209. (org-remove-occur-highlights nil nil t)
  7210. (if (org-at-timestamp-p)
  7211. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7212. (substring (match-string 1) 0 10)))
  7213. 1)
  7214. (let (type path link line search (pos (point)))
  7215. (catch 'match
  7216. (save-excursion
  7217. (skip-chars-forward "^]\n\r")
  7218. (when (and (re-search-backward "\\[\\[" nil t)
  7219. (looking-at org-bracket-link-regexp)
  7220. (<= (match-beginning 0) pos)
  7221. (>= (match-end 0) pos))
  7222. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  7223. (while (string-match " *\n *" link)
  7224. (setq link (replace-match " " t t link)))
  7225. (if (string-match org-link-re-with-space2 link)
  7226. (setq type (match-string 1 link)
  7227. path (match-string 2 link))
  7228. (setq type "thisfile"
  7229. path link))
  7230. (throw 'match t)))
  7231. (when (get-text-property (point) 'org-linked-text)
  7232. (setq type "thisfile"
  7233. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7234. (1+ (point)) (point))
  7235. path (buffer-substring
  7236. (previous-single-property-change pos 'org-linked-text)
  7237. (next-single-property-change pos 'org-linked-text)))
  7238. (throw 'match t))
  7239. (save-excursion
  7240. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  7241. (if (equal (char-before) ?<) (backward-char 1))
  7242. (when (or (looking-at org-angle-link-re)
  7243. (looking-at org-plain-link-re)
  7244. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  7245. (re-search-forward org-plain-link-re (point-at-eol) t))
  7246. (<= (match-beginning 0) pos)
  7247. (>= (match-end 0) pos)))
  7248. (setq type (match-string 1)
  7249. path (match-string 2))
  7250. (throw 'match t)))
  7251. (save-excursion
  7252. (skip-chars-backward "^ \t\n\r")
  7253. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  7254. (setq type "tags"
  7255. path (match-string 1))
  7256. (while (string-match ":" path)
  7257. (setq path (replace-match "+" t t path)))
  7258. (throw 'match t)))
  7259. (save-excursion
  7260. (skip-chars-backward "a-zA-Z_")
  7261. (when (and (memq 'camel org-activate-links)
  7262. (looking-at org-camel-regexp))
  7263. (setq type "camel" path (match-string 0))
  7264. (if (equal (char-before) ?*)
  7265. (setq path (concat "*" path))))
  7266. (throw 'match t)))
  7267. (unless path
  7268. (error "No link found"))
  7269. ;; Remove any trailing spaces in path
  7270. (if (string-match " +\\'" path)
  7271. (setq path (replace-match "" t t path)))
  7272. (cond
  7273. ((member type '("http" "https" "ftp" "mailto" "news"))
  7274. (browse-url (concat type ":" path)))
  7275. ((string= type "tags")
  7276. (org-tags-view in-emacs path))
  7277. ((or (string= type "camel")
  7278. (string= type "thisfile"))
  7279. (org-mark-ring-push)
  7280. (org-link-search
  7281. path
  7282. (cond ((equal in-emacs '(4)) 'occur)
  7283. ((equal in-emacs '(16)) 'org-occur)
  7284. (t nil))))
  7285. ((string= type "file")
  7286. (if (string-match "::?\\([0-9]+\\)\\'" path) ;; second : optional
  7287. ;; FIXME: It is unsafe to allow a single colon.
  7288. (setq line (string-to-number (match-string 1 path))
  7289. path (substring path 0 (match-beginning 0)))
  7290. (if (string-match "::\\(.+\\)\\'" path)
  7291. (setq search (match-string 1 path)
  7292. path (substring path 0 (match-beginning 0)))))
  7293. (org-open-file path in-emacs line search))
  7294. ((string= type "news")
  7295. (org-follow-gnus-link path))
  7296. ((string= type "bbdb")
  7297. (org-follow-bbdb-link path))
  7298. ((string= type "info")
  7299. (org-follow-info-link path))
  7300. ((string= type "gnus")
  7301. (let (group article)
  7302. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7303. (error "Error in Gnus link"))
  7304. (setq group (match-string 1 path)
  7305. article (match-string 3 path))
  7306. (org-follow-gnus-link group article)))
  7307. ((string= type "vm")
  7308. (let (folder article)
  7309. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7310. (error "Error in VM link"))
  7311. (setq folder (match-string 1 path)
  7312. article (match-string 3 path))
  7313. ;; in-emacs is the prefix arg, will be interpreted as read-only
  7314. (org-follow-vm-link folder article in-emacs)))
  7315. ((string= type "wl")
  7316. (let (folder article)
  7317. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7318. (error "Error in Wanderlust link"))
  7319. (setq folder (match-string 1 path)
  7320. article (match-string 3 path))
  7321. (org-follow-wl-link folder article)))
  7322. ((string= type "mhe")
  7323. (let (folder article)
  7324. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7325. (error "Error in MHE link"))
  7326. (setq folder (match-string 1 path)
  7327. article (match-string 3 path))
  7328. (org-follow-mhe-link folder article)))
  7329. ((string= type "rmail")
  7330. (let (folder article)
  7331. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7332. (error "Error in RMAIL link"))
  7333. (setq folder (match-string 1 path)
  7334. article (match-string 3 path))
  7335. (org-follow-rmail-link folder article)))
  7336. ((string= type "shell")
  7337. (let ((cmd path))
  7338. (while (string-match "@{" cmd)
  7339. (setq cmd (replace-match "<" t t cmd)))
  7340. (while (string-match "@}" cmd)
  7341. (setq cmd (replace-match ">" t t cmd)))
  7342. (if (or (not org-confirm-shell-link-function)
  7343. (funcall org-confirm-shell-link-function
  7344. (format "Execute \"%s\" in shell? "
  7345. (org-add-props cmd nil
  7346. 'face 'org-warning))))
  7347. (progn
  7348. (message "Executing %s" cmd)
  7349. (shell-command cmd))
  7350. (error "Abort"))))
  7351. ((string= type "elisp")
  7352. (let ((cmd path))
  7353. (if (or (not org-confirm-elisp-link-function)
  7354. (funcall org-confirm-elisp-link-function
  7355. (format "Execute \"%s\" as elisp? "
  7356. (org-add-props cmd nil
  7357. 'face 'org-warning))))
  7358. (message "%s => %s" cmd (eval (read cmd)))
  7359. (error "Abort"))))
  7360. (t
  7361. (browse-url-at-point))))))
  7362. (defun org-link-search (s &optional type)
  7363. "Search for a link search option.
  7364. When S is a CamelCaseWord, search for a target, or for a sentence containing
  7365. the words. If S is surrounded by forward slashes, it is interpreted as a
  7366. regular expression. In org-mode files, this will create an `org-occur'
  7367. sparse tree. In ordinary files, `occur' will be used to list matches.
  7368. If the current buffer is in `dired-mode', grep will be used to search
  7369. in all files."
  7370. (let ((case-fold-search t)
  7371. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7372. (pos (point))
  7373. (pre "") (post "")
  7374. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  7375. (cond
  7376. ;; First check if there are any special
  7377. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7378. ;; Now try the builtin stuff
  7379. ((save-excursion
  7380. (goto-char (point-min))
  7381. (and
  7382. (re-search-forward
  7383. (concat "<<" (regexp-quote s0) ">>") nil t)
  7384. (setq pos (match-beginning 0))))
  7385. ;; There is an exact target for this
  7386. (goto-char pos))
  7387. ((string-match "^/\\(.*\\)/$" s)
  7388. ;; A regular expression
  7389. (cond
  7390. ((eq major-mode 'org-mode)
  7391. (org-occur (match-string 1 s)))
  7392. ;;((eq major-mode 'dired-mode)
  7393. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7394. (t (org-do-occur (match-string 1 s)))))
  7395. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  7396. t)
  7397. ;; A camel or a normal search string
  7398. (when (equal (string-to-char s) ?*)
  7399. ;; Anchor on headlines, post may include tags.
  7400. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  7401. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  7402. s (substring s 1)))
  7403. (remove-text-properties
  7404. 0 (length s)
  7405. '(face nil mouse-face nil keymap nil fontified nil) s)
  7406. ;; Make a series of regular expressions to find a match
  7407. (setq words
  7408. (if camel
  7409. (org-camel-to-words s)
  7410. (org-split-string s "[ \n\r\t]+"))
  7411. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7412. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  7413. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7414. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7415. re1 (concat pre re2 post)
  7416. re3 (concat pre re4 post)
  7417. re5 (concat pre ".*" re4)
  7418. re2 (concat pre re2)
  7419. re2a (concat pre re2a)
  7420. re4 (concat pre re4)
  7421. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7422. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7423. re5 "\\)"
  7424. ))
  7425. (cond
  7426. ((eq type 'org-occur) (org-occur reall))
  7427. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7428. (t (goto-char (point-min))
  7429. (if (or (org-search-not-link re0 nil t)
  7430. (org-search-not-link re1 nil t)
  7431. (org-search-not-link re2 nil t)
  7432. (org-search-not-link re2a nil t)
  7433. (org-search-not-link re3 nil t)
  7434. (org-search-not-link re4 nil t)
  7435. (org-search-not-link re5 nil t)
  7436. )
  7437. (goto-char (match-beginning 1)) ;; Fixme: does every re have group 1?
  7438. (goto-char pos)
  7439. (error "No match")))))
  7440. (t
  7441. ;; Normal string-search
  7442. (goto-char (point-min))
  7443. (if (search-forward s nil t)
  7444. (goto-char (match-beginning 0))
  7445. (error "No match"))))
  7446. (and (eq major-mode 'org-mode) (org-show-hierarchy-above))))
  7447. (defun org-search-not-link (&rest args)
  7448. "Execute `re-search-forward', but only accept matches that are not a link."
  7449. (catch 'exit
  7450. (let ((pos (point)) p1)
  7451. (while (apply 're-search-forward args)
  7452. (setq p1 (point))
  7453. (if (not (save-match-data
  7454. (and (re-search-backward "\\[\\[" nil t)
  7455. (looking-at org-bracket-link-regexp)
  7456. (<= (match-beginning 0) p1)
  7457. (>= (match-end 0) p1))))
  7458. (progn (goto-char (match-end 0))
  7459. (throw 'exit (point)))
  7460. (goto-char (match-end 0)))))))
  7461. (defun org-do-occur (regexp &optional cleanup)
  7462. "Call the Emacs command `occur'.
  7463. If CLEANUP is non-nil, remove the printout of the regular expression
  7464. in the *Occur* buffer. This is useful if the regex is long and not useful
  7465. to read."
  7466. (occur regexp)
  7467. (when cleanup
  7468. (let ((cwin (selected-window)) win beg end)
  7469. (when (setq win (get-buffer-window "*Occur*"))
  7470. (select-window win))
  7471. (goto-char (point-min))
  7472. (when (re-search-forward "match[a-z]+" nil t)
  7473. (setq beg (match-end 0))
  7474. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7475. (setq end (1- (match-beginning 0)))))
  7476. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  7477. (goto-char (point-min))
  7478. (select-window cwin))))
  7479. (defvar org-mark-ring nil
  7480. "Mark ring for positions before jumps in Org-mode.")
  7481. (defvar org-mark-ring-last-goto nil
  7482. "Last position in the mark ring used to go back.")
  7483. ;; Fill and close the ring
  7484. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7485. (loop for i from 1 to org-mark-ring-length do
  7486. (push (make-marker) org-mark-ring))
  7487. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7488. org-mark-ring)
  7489. (defun org-mark-ring-push (&optional pos buffer)
  7490. "Put the current position or POS into the mark ring and rotate it."
  7491. (interactive)
  7492. (setq pos (or pos (point)))
  7493. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7494. (move-marker (car org-mark-ring)
  7495. (or pos (point))
  7496. (or buffer (current-buffer)))
  7497. (message
  7498. (substitute-command-keys
  7499. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7500. (defun org-mark-ring-goto (&optional n)
  7501. "Jump to the previous position in the mark ring.
  7502. With prefix arg N, jump back that many stored positions. When
  7503. called several times in succession, walk through the entire ring.
  7504. Org-mode commands jumping to a different position in the current file,
  7505. or to another Org-mode file, automatically push the old position
  7506. onto the ring."
  7507. (interactive "p")
  7508. (let (p m)
  7509. (if (eq last-command this-command)
  7510. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7511. (setq p org-mark-ring))
  7512. (setq org-mark-ring-last-goto p)
  7513. (setq m (car p))
  7514. (switch-to-buffer (marker-buffer m))
  7515. (goto-char m)
  7516. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  7517. (defun org-camel-to-words (s)
  7518. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  7519. (let ((case-fold-search nil)
  7520. words)
  7521. (while (string-match "[a-z][A-Z]" s)
  7522. (push (substring s 0 (1+ (match-beginning 0))) words)
  7523. (setq s (substring s (1+ (match-beginning 0)))))
  7524. (nreverse (cons s words))))
  7525. (defun org-remove-angle-brackets (s)
  7526. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7527. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7528. s)
  7529. (defun org-add-angle-brackets (s)
  7530. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7531. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7532. s)
  7533. (defun org-follow-bbdb-link (name)
  7534. "Follow a BBDB link to NAME."
  7535. (require 'bbdb)
  7536. (let ((inhibit-redisplay t)
  7537. (bbdb-electric-p nil))
  7538. (catch 'exit
  7539. ;; Exact match on name
  7540. (bbdb-name (concat "\\`" name "\\'") nil)
  7541. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7542. ;; Exact match on name
  7543. (bbdb-company (concat "\\`" name "\\'") nil)
  7544. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7545. ;; Partial match on name
  7546. (bbdb-name name nil)
  7547. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7548. ;; Partial match on company
  7549. (bbdb-company name nil)
  7550. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7551. ;; General match including network address and notes
  7552. (bbdb name nil)
  7553. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  7554. (delete-window (get-buffer-window "*BBDB*"))
  7555. (error "No matching BBDB record")))))
  7556. (defun org-follow-info-link (name)
  7557. "Follow an info file & node link to NAME."
  7558. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  7559. (string-match "\\(.*\\)" name))
  7560. (progn
  7561. (require 'info)
  7562. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  7563. (Info-find-node (match-string 1 name) (match-string 2 name))
  7564. (Info-find-node (match-string 1 name) "Top")))
  7565. (message (concat "Could not open: " name))))
  7566. (defun org-follow-gnus-link (&optional group article)
  7567. "Follow a Gnus link to GROUP and ARTICLE."
  7568. (require 'gnus)
  7569. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  7570. (if group (gnus-fetch-group group))
  7571. (if article
  7572. (or (gnus-summary-goto-article article nil 'force)
  7573. (if (fboundp 'gnus-summary-insert-cached-articles)
  7574. (progn
  7575. (gnus-summary-insert-cached-articles)
  7576. (gnus-summary-goto-article article nil 'force))
  7577. (message "Message could not be found.")))))
  7578. (defun org-follow-vm-link (&optional folder article readonly)
  7579. "Follow a VM link to FOLDER and ARTICLE."
  7580. (require 'vm)
  7581. (setq article (org-add-angle-brackets article))
  7582. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  7583. ;; ange-ftp or efs or tramp access
  7584. (let ((user (or (match-string 1 folder) (user-login-name)))
  7585. (host (match-string 2 folder))
  7586. (file (match-string 3 folder)))
  7587. (cond
  7588. ((featurep 'tramp)
  7589. ;; use tramp to access the file
  7590. (if (featurep 'xemacs)
  7591. (setq folder (format "[%s@%s]%s" user host file))
  7592. (setq folder (format "/%s@%s:%s" user host file))))
  7593. (t
  7594. ;; use ange-ftp or efs
  7595. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  7596. (setq folder (format "/%s@%s:%s" user host file))))))
  7597. (when folder
  7598. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  7599. (sit-for 0.1)
  7600. (when article
  7601. (vm-select-folder-buffer)
  7602. (widen)
  7603. (let ((case-fold-search t))
  7604. (goto-char (point-min))
  7605. (if (not (re-search-forward
  7606. (concat "^" "message-id: *" (regexp-quote article))))
  7607. (error "Could not find the specified message in this folder"))
  7608. (vm-isearch-update)
  7609. (vm-isearch-narrow)
  7610. (vm-beginning-of-message)
  7611. (vm-summarize)))))
  7612. (defun org-follow-wl-link (folder article)
  7613. "Follow a Wanderlust link to FOLDER and ARTICLE."
  7614. (setq article (org-add-angle-brackets article))
  7615. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  7616. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  7617. (wl-summary-redisplay))
  7618. (defun org-follow-rmail-link (folder article)
  7619. "Follow an RMAIL link to FOLDER and ARTICLE."
  7620. (setq article (org-add-angle-brackets article))
  7621. (let (message-number)
  7622. (save-excursion
  7623. (save-window-excursion
  7624. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  7625. (setq message-number
  7626. (save-restriction
  7627. (widen)
  7628. (goto-char (point-max))
  7629. (if (re-search-backward
  7630. (concat "^Message-ID:\\s-+" (regexp-quote
  7631. (or article "")))
  7632. nil t)
  7633. (rmail-what-message))))))
  7634. (if message-number
  7635. (progn
  7636. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  7637. (rmail-show-message message-number)
  7638. message-number)
  7639. (error "Message not found"))))
  7640. ;; mh-e integration based on planner-mode
  7641. (defun org-mhe-get-message-real-folder ()
  7642. "Return the name of the current message real folder, so if you use
  7643. sequences, it will now work."
  7644. (save-excursion
  7645. (let* ((folder
  7646. (if (equal major-mode 'mh-folder-mode)
  7647. mh-current-folder
  7648. ;; Refer to the show buffer
  7649. mh-show-folder-buffer))
  7650. (end-index
  7651. (if (boundp 'mh-index-folder)
  7652. (min (length mh-index-folder) (length folder))))
  7653. )
  7654. ;; a simple test on mh-index-data does not work, because
  7655. ;; mh-index-data is always nil in a show buffer.
  7656. (if (and (boundp 'mh-index-folder)
  7657. (string= mh-index-folder (substring folder 0 end-index)))
  7658. (if (equal major-mode 'mh-show-mode)
  7659. (save-window-excursion
  7660. (when (buffer-live-p (get-buffer folder))
  7661. (progn
  7662. (pop-to-buffer folder)
  7663. (org-mhe-get-message-folder-from-index)
  7664. )
  7665. ))
  7666. (org-mhe-get-message-folder-from-index)
  7667. )
  7668. folder
  7669. )
  7670. )))
  7671. (defun org-mhe-get-message-folder-from-index ()
  7672. "Returns the name of the message folder in a index folder buffer."
  7673. (save-excursion
  7674. (mh-index-previous-folder)
  7675. (re-search-forward "^\\(+.*\\)$" nil t)
  7676. (message (match-string 1))))
  7677. (defun org-mhe-get-message-folder ()
  7678. "Return the name of the current message folder. Be careful if you
  7679. use sequences."
  7680. (save-excursion
  7681. (if (equal major-mode 'mh-folder-mode)
  7682. mh-current-folder
  7683. ;; Refer to the show buffer
  7684. mh-show-folder-buffer)))
  7685. (defun org-mhe-get-message-num ()
  7686. "Return the number of the current message. Be careful if you
  7687. use sequences."
  7688. (save-excursion
  7689. (if (equal major-mode 'mh-folder-mode)
  7690. (mh-get-msg-num nil)
  7691. ;; Refer to the show buffer
  7692. (mh-show-buffer-message-number))))
  7693. (defun org-mhe-get-header (header)
  7694. "Return a header of the message in folder mode. This will create a
  7695. show buffer for the corresponding message. If you have a more clever
  7696. idea..."
  7697. (let* ((folder (org-mhe-get-message-folder))
  7698. (num (org-mhe-get-message-num))
  7699. (buffer (get-buffer-create (concat "show-" folder)))
  7700. (header-field))
  7701. (with-current-buffer buffer
  7702. (mh-display-msg num folder)
  7703. (if (equal major-mode 'mh-folder-mode)
  7704. (mh-header-display)
  7705. (mh-show-header-display))
  7706. (set-buffer buffer)
  7707. (setq header-field (mh-get-header-field header))
  7708. (if (equal major-mode 'mh-folder-mode)
  7709. (mh-show)
  7710. (mh-show-show))
  7711. header-field)))
  7712. (defun org-follow-mhe-link (folder article)
  7713. "Follow an MHE link to FOLDER and ARTICLE.
  7714. If ARTICLE is nil FOLDER is shown. If the configuration variable
  7715. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  7716. ARTICLE is searched in all folders. Indexed searches (swish++,
  7717. namazu, and others supported by MH-E) will always search in all
  7718. folders."
  7719. (require 'mh-e)
  7720. (require 'mh-search)
  7721. (require 'mh-utils)
  7722. (mh-find-path)
  7723. (if (not article)
  7724. (mh-visit-folder (mh-normalize-folder-name folder))
  7725. (setq article (org-add-angle-brackets article))
  7726. (mh-search-choose)
  7727. (if (equal mh-searcher 'pick)
  7728. (progn
  7729. (mh-search folder (list "--message-id" article))
  7730. (when (and org-mhe-search-all-folders
  7731. (not (org-mhe-get-message-real-folder)))
  7732. (kill-this-buffer)
  7733. (mh-search "+" (list "--message-id" article))))
  7734. (mh-search "+" article))
  7735. (if (org-mhe-get-message-real-folder)
  7736. (mh-show-msg 1)
  7737. (kill-this-buffer)
  7738. (error "Message not found"))))
  7739. ;; BibTeX links
  7740. ;; Use the custom search meachnism to construct and use search strings for
  7741. ;; file links to BibTeX database entries.
  7742. (defun org-create-file-search-in-bibtex ()
  7743. "Create the search string and description for a BibTeX database entry."
  7744. (when (eq major-mode 'bibtex-mode)
  7745. ;; yes, we want to construct this search string.
  7746. ;; Make a good description for this entry, using names, year and the title
  7747. ;; Put it into the `description' variable which is dynamically scoped.
  7748. (let ((bibtex-autokey-names 1)
  7749. (bibtex-autokey-names-stretch 1)
  7750. (bibtex-autokey-name-case-convert-function 'identity)
  7751. (bibtex-autokey-name-separator " & ")
  7752. (bibtex-autokey-additional-names " et al.")
  7753. (bibtex-autokey-year-length 4)
  7754. (bibtex-autokey-name-year-separator " ")
  7755. (bibtex-autokey-titlewords 3)
  7756. (bibtex-autokey-titleword-separator " ")
  7757. (bibtex-autokey-titleword-case-convert-function 'identity)
  7758. (bibtex-autokey-titleword-length 'infty)
  7759. (bibtex-autokey-year-title-separator ": "))
  7760. (setq description (bibtex-generate-autokey)))
  7761. ;; Now parse the entry, get the key and return it.
  7762. (save-excursion
  7763. (bibtex-beginning-of-entry)
  7764. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  7765. (defun org-execute-file-search-in-bibtex (s)
  7766. "Find the link search string S as a key for a database entry."
  7767. (when (eq major-mode 'bibtex-mode)
  7768. ;; Yes, we want to do the search in this file.
  7769. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  7770. (goto-char (point-min))
  7771. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  7772. (regexp-quote s) "[ \t\n]*,") nil t)
  7773. (goto-char (match-beginning 0)))
  7774. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  7775. ;; Use double prefix to indicate that any web link should be browsed
  7776. (let ((b (current-buffer)) (p (point)))
  7777. ;; Restore the window configuration because we just use the web link
  7778. (set-window-configuration org-window-config-before-follow-link)
  7779. (save-excursion (set-buffer b) (goto-char p)
  7780. (bibtex-url)))
  7781. (recenter 0)) ; Move entry start to beginning of window
  7782. ;; return t to indicate that the search is done.
  7783. t))
  7784. ;; Finally add the functions to the right hooks.
  7785. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  7786. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  7787. ;; end of Bibtex link setup
  7788. (defun org-upgrade-old-links (&optional query-description)
  7789. "Transfer old <...> style links to new [[...]] style links.
  7790. With arg query-description, ask at each match for a description text to use
  7791. for this link."
  7792. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  7793. (save-excursion
  7794. (goto-char (point-min))
  7795. (let ((re (concat "\\([^[]\\)<\\("
  7796. "\\(" (mapconcat 'identity org-link-types "\\|")
  7797. "\\):"
  7798. "[^" org-non-link-chars "]+\\)>"))
  7799. l1 l2 (cnt 0))
  7800. (while (re-search-forward re nil t)
  7801. (setq cnt (1+ cnt)
  7802. l1 (org-match-string-no-properties 2)
  7803. l2 (save-match-data (org-link-escape l1)))
  7804. (when query-description (setq l1 (read-string "Desc: " l1)))
  7805. (if (equal l1 l2)
  7806. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  7807. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  7808. (message "%d matches have beed treated" cnt))))
  7809. (defun org-open-file (path &optional in-emacs line search)
  7810. "Open the file at PATH.
  7811. First, this expands any special file name abbreviations. Then the
  7812. configuration variable `org-file-apps' is checked if it contains an
  7813. entry for this file type, and if yes, the corresponding command is launched.
  7814. If no application is found, Emacs simply visits the file.
  7815. With optional argument IN-EMACS, Emacs will visit the file.
  7816. Optional LINE specifies a line to go to, optional SEARCH a string to
  7817. search for. If LINE or SEARCH is given, the file will always be
  7818. opened in Emacs.
  7819. If the file does not exist, an error is thrown."
  7820. (setq in-emacs (or in-emacs line search))
  7821. (let* ((file (if (equal path "")
  7822. buffer-file-name
  7823. (convert-standard-filename (org-expand-file-name path))))
  7824. (dirp (file-directory-p file))
  7825. (dfile (downcase file))
  7826. (old-buffer (current-buffer))
  7827. (old-pos (point))
  7828. (old-mode major-mode)
  7829. ext cmd apps)
  7830. (if (and (not (file-exists-p file))
  7831. (not org-open-non-existing-files))
  7832. (error "No such file: %s" file))
  7833. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7834. (setq ext (match-string 1 dfile))
  7835. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7836. (setq ext (match-string 1 dfile))))
  7837. (setq apps (append org-file-apps (org-default-apps)))
  7838. (if in-emacs
  7839. (setq cmd 'emacs)
  7840. (setq cmd (or (and dirp (cdr (assoc 'directory apps)))
  7841. (cdr (assoc ext apps))
  7842. (cdr (assoc t apps)))))
  7843. (when (eq cmd 'mailcap)
  7844. (require 'mailcap)
  7845. (mailcap-parse-mailcaps)
  7846. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7847. (command (mailcap-mime-info mime-type)))
  7848. (if (stringp command)
  7849. (setq cmd command)
  7850. (setq cmd 'emacs))))
  7851. (cond
  7852. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7853. ;; Normalize use of quote, this can vary.
  7854. (if (string-match "['\"]%s['\"]" cmd)
  7855. (setq cmd (replace-match "'%s'" t t cmd)))
  7856. (setq cmd (format cmd file))
  7857. (save-window-excursion
  7858. (shell-command (concat cmd " &"))))
  7859. ((or (stringp cmd)
  7860. (eq cmd 'emacs))
  7861. (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  7862. (funcall (cdr (assq 'file org-link-frame-setup)) file))
  7863. (if line (goto-line line)
  7864. (if search (org-link-search search))))
  7865. ((consp cmd)
  7866. (eval cmd))
  7867. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7868. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  7869. (or (not (equal old-buffer (current-buffer)))
  7870. (not (equal old-pos (point))))
  7871. (org-mark-ring-push old-pos old-buffer))))
  7872. (defun org-default-apps ()
  7873. "Return the default applications for this operating system."
  7874. (cond
  7875. ((eq system-type 'darwin)
  7876. org-file-apps-defaults-macosx)
  7877. ((eq system-type 'windows-nt)
  7878. org-file-apps-defaults-windowsnt)
  7879. (t org-file-apps-defaults-gnu)))
  7880. (defun org-expand-file-name (path)
  7881. "Replace special path abbreviations and expand the file name."
  7882. (expand-file-name path))
  7883. (defvar org-insert-link-history nil
  7884. "Minibuffer history for links inserted with `org-insert-link'.")
  7885. (defvar org-stored-links nil
  7886. "Contains the links stored with `org-store-link'.")
  7887. ;;;###autoload
  7888. (defun org-store-link (arg)
  7889. "\\<org-mode-map>Store an org-link to the current location.
  7890. This link can later be inserted into an org-buffer with
  7891. \\[org-insert-link].
  7892. For some link types, a prefix arg is interpreted:
  7893. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  7894. For file links, arg negates `org-context-in-file-links'."
  7895. (interactive "P")
  7896. (let (link cpltxt desc description search txt (pos (point)))
  7897. (cond
  7898. ((eq major-mode 'bbdb-mode)
  7899. (setq cpltxt (concat
  7900. "bbdb:"
  7901. (or (bbdb-record-name (bbdb-current-record))
  7902. (bbdb-record-company (bbdb-current-record))))
  7903. link (org-make-link cpltxt)))
  7904. ((eq major-mode 'Info-mode)
  7905. (setq link (org-make-link "info:"
  7906. (file-name-nondirectory Info-current-file)
  7907. ":" Info-current-node))
  7908. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  7909. ":" Info-current-node)))
  7910. ((eq major-mode 'calendar-mode)
  7911. (let ((cd (calendar-cursor-to-date)))
  7912. (setq link
  7913. (format-time-string
  7914. (car org-time-stamp-formats)
  7915. (apply 'encode-time
  7916. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7917. nil nil nil))))))
  7918. ((or (eq major-mode 'vm-summary-mode)
  7919. (eq major-mode 'vm-presentation-mode))
  7920. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  7921. (vm-follow-summary-cursor)
  7922. (save-excursion
  7923. (vm-select-folder-buffer)
  7924. (let* ((message (car vm-message-pointer))
  7925. (folder buffer-file-name)
  7926. (subject (vm-su-subject message))
  7927. (author (vm-su-full-name message))
  7928. (message-id (vm-su-message-id message)))
  7929. (setq message-id (org-remove-angle-brackets message-id))
  7930. (setq folder (abbreviate-file-name folder))
  7931. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  7932. folder)
  7933. (setq folder (replace-match "" t t folder)))
  7934. (setq cpltxt (concat author " on: " subject))
  7935. (setq link (org-make-link "vm:" folder "#" message-id)))))
  7936. ((eq major-mode 'wl-summary-mode)
  7937. (let* ((msgnum (wl-summary-message-number))
  7938. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  7939. msgnum 'message-id))
  7940. (wl-message-entity (elmo-msgdb-overview-get-entity
  7941. msgnum (wl-summary-buffer-msgdb)))
  7942. (author (wl-summary-line-from)) ; FIXME: correct?
  7943. (subject "???")) ; FIXME:
  7944. (setq message-id (org-remove-angle-brackets message-id))
  7945. (setq cpltxt (concat author " on: " subject))
  7946. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  7947. "#" message-id))))
  7948. ((or (equal major-mode 'mh-folder-mode)
  7949. (equal major-mode 'mh-show-mode))
  7950. (let ((from-header (org-mhe-get-header "From:"))
  7951. (to-header (org-mhe-get-header "To:"))
  7952. (subject (org-mhe-get-header "Subject:")))
  7953. (setq cpltxt (concat from-header " on: " subject))
  7954. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  7955. (org-remove-angle-brackets
  7956. (org-mhe-get-header "Message-Id:"))))))
  7957. ((eq major-mode 'rmail-mode)
  7958. (save-excursion
  7959. (save-restriction
  7960. (rmail-narrow-to-non-pruned-header)
  7961. (let ((folder buffer-file-name)
  7962. (message-id (mail-fetch-field "message-id"))
  7963. (author (mail-fetch-field "from"))
  7964. (subject (mail-fetch-field "subject")))
  7965. (setq message-id (org-remove-angle-brackets message-id))
  7966. (setq cpltxt (concat author " on: " subject))
  7967. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  7968. ((eq major-mode 'gnus-group-mode)
  7969. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  7970. (gnus-group-group-name)) ; version
  7971. ((fboundp 'gnus-group-name)
  7972. (gnus-group-name))
  7973. (t "???"))))
  7974. (setq cpltxt (concat
  7975. (if (org-xor arg org-usenet-links-prefer-google)
  7976. "http://groups.google.com/groups?group="
  7977. "gnus:")
  7978. group)
  7979. link (org-make-link cpltxt))))
  7980. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  7981. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  7982. (gnus-summary-beginning-of-article)
  7983. (let* ((group (car gnus-article-current))
  7984. (article (cdr gnus-article-current))
  7985. (header (gnus-summary-article-header article))
  7986. (author (mail-header-from header))
  7987. (message-id (mail-header-id header))
  7988. (date (mail-header-date header))
  7989. (subject (gnus-summary-subject-string)))
  7990. (setq cpltxt (concat author " on: " subject))
  7991. (if (org-xor arg org-usenet-links-prefer-google)
  7992. (setq link
  7993. (concat
  7994. cpltxt "\n "
  7995. (format "http://groups.google.com/groups?as_umsgid=%s"
  7996. (org-fixup-message-id-for-http message-id))))
  7997. (setq link (org-make-link "gnus:" group
  7998. "#" (number-to-string article))))))
  7999. ((eq major-mode 'w3-mode)
  8000. (setq cpltxt (url-view-url t)
  8001. link (org-make-link cpltxt)))
  8002. ((eq major-mode 'w3m-mode)
  8003. (setq cpltxt w3m-current-url
  8004. link (org-make-link cpltxt)))
  8005. ((setq search (run-hook-with-args-until-success
  8006. 'org-create-file-search-functions))
  8007. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8008. "::" search))
  8009. (setq cpltxt (or description link))) ;; FIXME: is this the best way?
  8010. ((eq major-mode 'org-mode)
  8011. ;; Just link to current headline
  8012. (setq cpltxt (concat "file:"
  8013. (abbreviate-file-name buffer-file-name)))
  8014. ;; Add a context search string
  8015. (when (org-xor org-context-in-file-links arg)
  8016. ;; Check if we are on a target
  8017. (if (save-excursion
  8018. (skip-chars-forward "^>\n\r")
  8019. (and (re-search-backward "<<" nil t)
  8020. (looking-at "<<\\(.*?\\)>>")
  8021. (<= (match-beginning 0) pos)
  8022. (>= (match-end 0) pos)))
  8023. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  8024. (setq txt (cond
  8025. ((org-on-heading-p) nil)
  8026. ((org-region-active-p)
  8027. (buffer-substring (region-beginning) (region-end)))
  8028. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  8029. (when (string-match "\\S-" txt)
  8030. (setq cpltxt
  8031. (concat cpltxt "::"
  8032. (if org-file-link-context-use-camel-case
  8033. (org-make-org-heading-camel txt)
  8034. (org-make-org-heading-search-string txt)))
  8035. desc "NONE"))))
  8036. (if (string-match "::\\'" cpltxt)
  8037. (setq cpltxt (substring cpltxt 0 -2)))
  8038. (setq link (org-make-link cpltxt)))
  8039. (buffer-file-name
  8040. ;; Just link to this file here.
  8041. (setq cpltxt (concat "file:"
  8042. (abbreviate-file-name buffer-file-name)))
  8043. ;; Add a context string
  8044. (when (org-xor org-context-in-file-links arg)
  8045. (setq txt (if (org-region-active-p)
  8046. (buffer-substring (region-beginning) (region-end))
  8047. (buffer-substring (point-at-bol) (point-at-eol))))
  8048. ;; Only use search option if there is some text.
  8049. (when (string-match "\\S-" txt)
  8050. (setq cpltxt
  8051. (concat cpltxt "::"
  8052. (if org-file-link-context-use-camel-case
  8053. (org-make-org-heading-camel txt)
  8054. (org-make-org-heading-search-string txt)))
  8055. desc "NONE")))
  8056. (setq link (org-make-link cpltxt)))
  8057. ((interactive-p)
  8058. (error "Cannot link to a buffer which is not visiting a file"))
  8059. (t (setq link nil)))
  8060. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8061. (setq link (or link cpltxt)
  8062. desc (or desc cpltxt))
  8063. (if (equal desc "NONE") (setq desc nil))
  8064. (if (and (interactive-p) link)
  8065. (progn
  8066. (setq org-stored-links
  8067. (cons (list cpltxt link desc) org-stored-links))
  8068. (message "Stored: %s" (or cpltxt link)))
  8069. (org-make-link-string link desc))))
  8070. (defun org-make-org-heading-search-string (&optional string heading)
  8071. "Make search string for STRING or current headline."
  8072. (interactive)
  8073. (let ((s (or string (org-get-heading))))
  8074. (unless (and string (not heading))
  8075. ;; We are using a headline, clean up garbage in there.
  8076. (if (string-match org-todo-regexp s)
  8077. (setq s (replace-match "" t t s)))
  8078. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8079. (setq s (replace-match "" t t s)))
  8080. (setq s (org-trim s))
  8081. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8082. org-comment-string "\\)") s)
  8083. (setq s (replace-match "" t t s)))
  8084. (while (string-match org-ts-regexp s)
  8085. (setq s (replace-match "" t t s))))
  8086. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  8087. (setq s (replace-match " " t t s)))
  8088. (or string (setq s (concat "*" s))) ; Add * for headlines
  8089. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8090. (defun org-make-org-heading-camel (&optional string heading)
  8091. "Make a CamelCase string for STRING or the current headline."
  8092. (interactive)
  8093. (let ((s (or string (org-get-heading))))
  8094. (unless (and string (not heading))
  8095. ;; We are using a headline, clean up garbage in there.
  8096. (if (string-match org-todo-regexp s)
  8097. (setq s (replace-match "" t t s)))
  8098. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8099. (setq s (replace-match "" t t s)))
  8100. (setq s (org-trim s))
  8101. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8102. org-comment-string "\\)") s)
  8103. (setq s (replace-match "" t t s)))
  8104. (while (string-match org-ts-regexp s)
  8105. (setq s (replace-match "" t t s))))
  8106. (while (string-match "[^a-zA-Z_ \t]+" s)
  8107. (setq s (replace-match " " t t s)))
  8108. (or string (setq s (concat "*" s))) ; Add * for headlines
  8109. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  8110. (defun org-make-link (&rest strings)
  8111. "Concatenate STRINGS, format resulting string with `org-link-format'."
  8112. (format org-link-format (apply 'concat strings)))
  8113. (defun org-make-link-string (link &optional description)
  8114. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8115. (if (eq org-link-style 'plain)
  8116. (if (equal description link)
  8117. link
  8118. (concat description "\n" link))
  8119. (when (stringp description)
  8120. ;; Remove brackets from the description, they are fatal.
  8121. (while (string-match "\\[\\|\\]" description)
  8122. (setq description (replace-match "" t t description))))
  8123. (when (equal (org-link-escape link) description)
  8124. ;; No description needed, it is identical
  8125. (setq description nil))
  8126. (when (and (not description)
  8127. (not (equal link (org-link-escape link))))
  8128. (setq description link))
  8129. (concat "[[" (org-link-escape link) "]"
  8130. (if description (concat "[" description "]") "")
  8131. "]")))
  8132. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  8133. "Association list of escapes for some characters problematic in links.")
  8134. (defun org-link-escape (text)
  8135. "Escape charaters in TEXT that are problematic for links."
  8136. (when text
  8137. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  8138. org-link-escape-chars "\\|")))
  8139. (while (string-match re text)
  8140. (setq text
  8141. (replace-match
  8142. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  8143. t t text)))
  8144. text)))
  8145. (defun org-link-unescape (text)
  8146. "Reverse the action of `org-link-escape'."
  8147. (when text
  8148. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  8149. org-link-escape-chars "\\|")))
  8150. (while (string-match re text)
  8151. (setq text
  8152. (replace-match
  8153. (car (rassoc (match-string 0 text) org-link-escape-chars))
  8154. t t text)))
  8155. text)))
  8156. (defun org-xor (a b)
  8157. "Exclusive or."
  8158. (if a (not b) b))
  8159. (defun org-get-header (header)
  8160. "Find a header field in the current buffer."
  8161. (save-excursion
  8162. (goto-char (point-min))
  8163. (let ((case-fold-search t) s)
  8164. (cond
  8165. ((eq header 'from)
  8166. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  8167. (setq s (match-string 1)))
  8168. (while (string-match "\"" s)
  8169. (setq s (replace-match "" t t s)))
  8170. (if (string-match "[<(].*" s)
  8171. (setq s (replace-match "" t t s))))
  8172. ((eq header 'message-id)
  8173. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  8174. (setq s (match-string 1))))
  8175. ((eq header 'subject)
  8176. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  8177. (setq s (match-string 1)))))
  8178. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  8179. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  8180. s)))
  8181. (defun org-fixup-message-id-for-http (s)
  8182. "Replace special characters in a message id, so it can be used in an http query."
  8183. (while (string-match "<" s)
  8184. (setq s (replace-match "%3C" t t s)))
  8185. (while (string-match ">" s)
  8186. (setq s (replace-match "%3E" t t s)))
  8187. (while (string-match "@" s)
  8188. (setq s (replace-match "%40" t t s)))
  8189. s)
  8190. (defun org-insert-link (&optional complete-file)
  8191. "Insert a link. At the prompt, enter the link.
  8192. Completion can be used to select a link previously stored with
  8193. `org-store-link'. When the empty string is entered (i.e. if you just
  8194. press RET at the prompt), the link defaults to the most recently
  8195. stored link. As SPC triggers completion in the minibuffer, you need to
  8196. use M-SPC or C-q SPC to force the insertion of a space character.
  8197. You will also be prompted for a description, and if one is given, it will
  8198. be displayed in the buffer instead of the link.
  8199. If there is already a link at point, this command will allow you to edit link
  8200. and description parts.
  8201. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  8202. selected using completion. The path to the file will be relative to
  8203. the current directory if the file is in the current directory or a
  8204. subdirectory. Otherwise, the link will be the absolute path as
  8205. completed in the minibuffer (i.e. normally ~/path/to/file).
  8206. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  8207. is in the current directory or below."
  8208. (interactive "P")
  8209. (let (link desc entry remove file (pos (point)))
  8210. (cond
  8211. ((save-excursion
  8212. (skip-chars-forward "^]\n\r")
  8213. (and (re-search-backward "\\[\\[" nil t)
  8214. (looking-at org-bracket-link-regexp)
  8215. (<= (match-beginning 0) pos)
  8216. (>= (match-end 0) pos)))
  8217. ;; We do have a link at point, and we are going to edit it.
  8218. (setq remove (list (match-beginning 0) (match-end 0)))
  8219. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8220. (setq link (read-string "Link: "
  8221. (org-link-unescape
  8222. (org-match-string-no-properties 1)))))
  8223. (complete-file
  8224. ;; Completing read for file names.
  8225. (setq file (read-file-name "File: "))
  8226. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8227. (pwd1 (file-name-as-directory (abbreviate-file-name
  8228. (expand-file-name ".")))))
  8229. (cond
  8230. ((equal complete-file '(16))
  8231. (setq link (org-make-link
  8232. "file:"
  8233. (abbreviate-file-name (expand-file-name file)))))
  8234. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8235. (setq link (org-make-link "file:" (match-string 1 file))))
  8236. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8237. (expand-file-name file))
  8238. (setq link (org-make-link
  8239. "file:" (match-string 1 (expand-file-name file)))))
  8240. (t (setq link (org-make-link "file:" file))))))
  8241. (t
  8242. ;; Read link, with completion for stored links.
  8243. (setq link (org-completing-read
  8244. "Link: " org-stored-links nil nil nil
  8245. org-insert-link-history
  8246. (or (car (car org-stored-links)))))
  8247. (setq entry (assoc link org-stored-links))
  8248. (if (not org-keep-stored-link-after-insertion)
  8249. (setq org-stored-links (delq (assoc link org-stored-links)
  8250. org-stored-links)))
  8251. (setq link (if entry (nth 1 entry) link)
  8252. desc (or desc (nth 2 entry)))))
  8253. (if (string-match org-plain-link-re link)
  8254. ;; URL-like link, normalize the use of angular brackets.
  8255. (setq link (org-make-link (org-remove-angle-brackets link))))
  8256. ;; Check if we are linking to the current file with a search option
  8257. ;; If yes, simplify the link by using only the search option.
  8258. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  8259. (let* ((path (match-string 1 link))
  8260. (case-fold-search nil)
  8261. (search (match-string 2 link)))
  8262. (save-match-data
  8263. (if (equal (file-truename buffer-file-name) (file-truename path))
  8264. ;; We are linking to this same file, with a search option
  8265. (setq link search)))))
  8266. ;; Check if we can/should use a relative path. If yes, simplify the link
  8267. (when (string-match "\\<file:\\(.*\\)" link)
  8268. (let* ((path (match-string 1 link))
  8269. (case-fold-search nil))
  8270. (cond
  8271. ((eq org-link-file-path-type 'absolute)
  8272. (setq path (abbreviate-file-name (expand-file-name path))))
  8273. ((eq org-link-file-path-type 'noabbrev)
  8274. (setq path (expand-file-name path)))
  8275. ((eq org-link-file-path-type 'relative)
  8276. (setq path (file-relative-name path)))
  8277. (t
  8278. (save-match-data
  8279. (if (string-match (concat "^" (regexp-quote
  8280. (file-name-as-directory
  8281. (expand-file-name "."))))
  8282. (expand-file-name path))
  8283. ;; We are linking a file with relative path name.
  8284. (setq path (substring (expand-file-name path)
  8285. (match-end 0)))))))
  8286. (setq link (concat "file:" path))))
  8287. (setq desc (read-string "Description: " desc))
  8288. (unless (string-match "\\S-" desc) (setq desc nil))
  8289. (if remove (apply 'delete-region remove))
  8290. (insert (org-make-link-string link desc))))
  8291. (defun org-completing-read (&rest args)
  8292. (let ((minibuffer-local-completion-map
  8293. (copy-keymap minibuffer-local-completion-map)))
  8294. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  8295. (apply 'completing-read args)))
  8296. ;;; Hooks for remember.el
  8297. (defvar org-finish-function nil)
  8298. ;;;###autoload
  8299. (defun org-remember-annotation ()
  8300. "Return a link to the current location as an annotation for remember.el.
  8301. If you are using Org-mode files as target for data storage with
  8302. remember.el, then the annotations should include a link compatible with the
  8303. conventions in Org-mode. This function returns such a link."
  8304. (org-store-link nil))
  8305. (defconst org-remember-help
  8306. "Select a destination location for the note.
  8307. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  8308. RET at beg-of-buf -> Append to file as level 2 headline
  8309. RET on headline -> Store as sublevel entry to current headline
  8310. <left>/<right> -> before/after current headline, same headings level")
  8311. ;;;###autoload
  8312. (defun org-remember-apply-template ()
  8313. "Initialize *remember* buffer with template, invoke `org-mode'.
  8314. This function should be placed into `remember-mode-hook' and in fact requires
  8315. to be run from that hook to fucntion properly."
  8316. (if org-remember-templates
  8317. (let* ((entry (if (= (length org-remember-templates) 1)
  8318. (cdar org-remember-templates)
  8319. (message "Select template: %s"
  8320. (mapconcat
  8321. (lambda (x) (char-to-string (car x)))
  8322. org-remember-templates " "))
  8323. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  8324. (tpl (car entry))
  8325. (file (if (consp (cdr entry)) (nth 1 entry)))
  8326. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  8327. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  8328. (v-u (concat "[" (substring v-t 1 -1) "]"))
  8329. (v-U (concat "[" (substring v-T 1 -1) "]"))
  8330. (v-a annotation) ; defined in `remember-mode'
  8331. (v-i initial) ; defined in `remember-mode'
  8332. (v-n user-full-name)
  8333. )
  8334. (unless tpl (setq tpl "") (message "No template") (ding))
  8335. (insert tpl) (goto-char (point-min))
  8336. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  8337. (when (and initial (equal (match-string 0) "%i"))
  8338. (save-match-data
  8339. (let* ((lead (buffer-substring
  8340. (point-at-bol) (match-beginning 0))))
  8341. (setq v-i (mapconcat 'identity
  8342. (org-split-string initial "\n")
  8343. (concat "\n" lead))))))
  8344. (replace-match
  8345. (or (eval (intern (concat "v-" (match-string 1)))) "")
  8346. t t))
  8347. (let ((org-startup-folded nil)
  8348. (org-startup-with-deadline-check nil))
  8349. (org-mode))
  8350. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  8351. (set (make-local-variable 'org-default-notes-file) file))
  8352. (goto-char (point-min))
  8353. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  8354. (let ((org-startup-folded nil)
  8355. (org-startup-with-deadline-check nil))
  8356. (org-mode)))
  8357. (set (make-local-variable 'org-finish-function) 'remember-buffer))
  8358. ;;;###autoload
  8359. (defun org-remember-handler ()
  8360. "Store stuff from remember.el into an org file.
  8361. First prompts for an org file. If the user just presses return, the value
  8362. of `org-default-notes-file' is used.
  8363. Then the command offers the headings tree of the selected file in order to
  8364. file the text at a specific location.
  8365. You can either immediately press RET to get the note appended to the
  8366. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  8367. find a better place. Then press RET or <left> or <right> in insert the note.
  8368. Key Cursor position Note gets inserted
  8369. -----------------------------------------------------------------------------
  8370. RET buffer-start as level 2 heading at end of file
  8371. RET on headline as sublevel of the heading at cursor
  8372. RET no heading at cursor position, level taken from context.
  8373. Or use prefix arg to specify level manually.
  8374. <left> on headline as same level, before current heading
  8375. <right> on headline as same level, after current heading
  8376. So the fastest way to store the note is to press RET RET to append it to
  8377. the default file. This way your current train of thought is not
  8378. interrupted, in accordance with the principles of remember.el. But with
  8379. little extra effort, you can push it directly to the correct location.
  8380. Before being stored away, the function ensures that the text has a
  8381. headline, i.e. a first line that starts with a \"*\". If not, a headline
  8382. is constructed from the current date and some additional data.
  8383. If the variable `org-adapt-indentation' is non-nil, the entire text is
  8384. also indented so that it starts in the same column as the headline
  8385. \(i.e. after the stars).
  8386. See also the variable `org-reverse-note-order'."
  8387. (catch 'quit
  8388. (let* ((txt (buffer-substring (point-min) (point-max)))
  8389. (fastp current-prefix-arg)
  8390. (file (if fastp org-default-notes-file (org-get-org-file)))
  8391. (visiting (find-buffer-visiting file))
  8392. (org-startup-with-deadline-check nil)
  8393. (org-startup-folded nil)
  8394. (org-startup-align-all-tables nil)
  8395. spos level indent reversed)
  8396. ;; Modify text so that it becomes a nice subtree which can be inserted
  8397. ;; into an org tree.
  8398. (let* ((lines (split-string txt "\n"))
  8399. first)
  8400. ;; remove empty lines at the beginning
  8401. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  8402. (setq lines (cdr lines)))
  8403. (setq first (car lines) lines (cdr lines))
  8404. (if (string-match "^\\*+" first)
  8405. ;; Is already a headline
  8406. (setq indent nil)
  8407. ;; We need to add a headline: Use time and first buffer line
  8408. (setq lines (cons first lines)
  8409. first (concat "* " (current-time-string)
  8410. " (" (remember-buffer-desc) ")")
  8411. indent " "))
  8412. (if (and org-adapt-indentation indent)
  8413. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  8414. (setq txt (concat first "\n"
  8415. (mapconcat 'identity lines "\n"))))
  8416. ;; Find the file
  8417. (if (not visiting)
  8418. (find-file-noselect file))
  8419. (with-current-buffer (get-file-buffer file)
  8420. (save-excursion (and (goto-char (point-min))
  8421. (not (re-search-forward "^\\* " nil t))
  8422. (insert "\n* Notes\n")))
  8423. (setq reversed (org-notes-order-reversed-p))
  8424. (save-excursion
  8425. (save-restriction
  8426. (widen)
  8427. ;; Ask the User for a location
  8428. (setq spos (if fastp 1 (org-get-location
  8429. (current-buffer)
  8430. org-remember-help)))
  8431. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  8432. ; not handle this note
  8433. (goto-char spos)
  8434. (cond ((bobp)
  8435. ;; Put it at the start or end, as level 2
  8436. (save-restriction
  8437. (widen)
  8438. (goto-char (if reversed (point-min) (point-max)))
  8439. (if (not (bolp)) (newline))
  8440. (org-paste-subtree 2 txt)))
  8441. ((and (org-on-heading-p nil) (not current-prefix-arg))
  8442. ;; Put it below this entry, at the beg/end of the subtree
  8443. (org-back-to-heading)
  8444. (setq level (funcall outline-level))
  8445. (if reversed
  8446. (outline-end-of-heading)
  8447. (outline-end-of-subtree))
  8448. (if (not (bolp)) (newline))
  8449. (beginning-of-line 1)
  8450. (org-paste-subtree (1+ level) txt))
  8451. (t
  8452. ;; Put it right there, with automatic level determined by
  8453. ;; org-paste-subtree or from prefix arg
  8454. (org-paste-subtree current-prefix-arg txt)))
  8455. (when remember-save-after-remembering
  8456. (save-buffer)
  8457. (if (not visiting) (kill-buffer (current-buffer)))))))))
  8458. t) ;; return t to indicate that we took care of this note.
  8459. (defun org-get-org-file ()
  8460. "Read a filename, with default directory `org-directory'."
  8461. (let ((default (or org-default-notes-file remember-data-file)))
  8462. (read-file-name (format "File name [%s]: " default)
  8463. (file-name-as-directory org-directory)
  8464. default)))
  8465. (defun org-notes-order-reversed-p ()
  8466. "Check if the current file should receive notes in reversed order."
  8467. (cond
  8468. ((not org-reverse-note-order) nil)
  8469. ((eq t org-reverse-note-order) t)
  8470. ((not (listp org-reverse-note-order)) nil)
  8471. (t (catch 'exit
  8472. (let ((all org-reverse-note-order)
  8473. entry)
  8474. (while (setq entry (pop all))
  8475. (if (string-match (car entry) buffer-file-name)
  8476. (throw 'exit (cdr entry))))
  8477. nil)))))
  8478. ;;; Tables
  8479. ;; Watch out: Here we are talking about two different kind of tables.
  8480. ;; Most of the code is for the tables created with the Org-mode table editor.
  8481. ;; Sometimes, we talk about tables created and edited with the table.el
  8482. ;; Emacs package. We call the former org-type tables, and the latter
  8483. ;; table.el-type tables.
  8484. (defun org-before-change-function (beg end)
  8485. "Every change indicates that a table might need an update."
  8486. (setq org-table-may-need-update t))
  8487. (defconst org-table-line-regexp "^[ \t]*|"
  8488. "Detects an org-type table line.")
  8489. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  8490. "Detects an org-type table line.")
  8491. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  8492. "Detects a table line marked for automatic recalculation.")
  8493. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  8494. "Detects a table line marked for automatic recalculation.")
  8495. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  8496. "Detects a table line marked for automatic recalculation.")
  8497. (defconst org-table-hline-regexp "^[ \t]*|-"
  8498. "Detects an org-type table hline.")
  8499. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  8500. "Detects a table-type table hline.")
  8501. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  8502. "Detects an org-type or table-type table.")
  8503. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  8504. "Searching from within a table (any type) this finds the first line
  8505. outside the table.")
  8506. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  8507. "Searching from within a table (any type) this finds the first line
  8508. outside the table.")
  8509. (defun org-table-create-with-table.el ()
  8510. "Use the table.el package to insert a new table.
  8511. If there is already a table at point, convert between Org-mode tables
  8512. and table.el tables."
  8513. (interactive)
  8514. (require 'table)
  8515. (cond
  8516. ((org-at-table.el-p)
  8517. (if (y-or-n-p "Convert table to Org-mode table? ")
  8518. (org-table-convert)))
  8519. ((org-at-table-p)
  8520. (if (y-or-n-p "Convert table to table.el table? ")
  8521. (org-table-convert)))
  8522. (t (call-interactively 'table-insert))))
  8523. (defun org-table-create-or-convert-from-region (arg)
  8524. "Convert region to table, or create an empty table.
  8525. If there is an active region, convert it to a table. If there is no such
  8526. region, create an empty table."
  8527. (interactive "P")
  8528. (if (org-region-active-p)
  8529. (org-table-convert-region (region-beginning) (region-end) arg)
  8530. (org-table-create arg)))
  8531. (defun org-table-create (&optional size)
  8532. "Query for a size and insert a table skeleton.
  8533. SIZE is a string Columns x Rows like for example \"3x2\"."
  8534. (interactive "P")
  8535. (unless size
  8536. (setq size (read-string
  8537. (concat "Table size Columns x Rows [e.g. "
  8538. org-table-default-size "]: ")
  8539. "" nil org-table-default-size)))
  8540. (let* ((pos (point))
  8541. (indent (make-string (current-column) ?\ ))
  8542. (split (org-split-string size " *x *"))
  8543. (rows (string-to-number (nth 1 split)))
  8544. (columns (string-to-number (car split)))
  8545. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  8546. "\n")))
  8547. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  8548. (point-at-bol) (point)))
  8549. (beginning-of-line 1)
  8550. (newline))
  8551. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  8552. (dotimes (i rows) (insert line))
  8553. (goto-char pos)
  8554. (if (> rows 1)
  8555. ;; Insert a hline after the first row.
  8556. (progn
  8557. (end-of-line 1)
  8558. (insert "\n|-")
  8559. (goto-char pos)))
  8560. (org-table-align)))
  8561. (defun org-table-convert-region (beg0 end0 &optional nspace)
  8562. "Convert region to a table.
  8563. The region goes from BEG0 to END0, but these borders will be moved
  8564. slightly, to make sure a beginning of line in the first line is included.
  8565. When NSPACE is non-nil, it indicates the minimum number of spaces that
  8566. separate columns (default: just one space)."
  8567. (interactive "rP")
  8568. (let* ((beg (min beg0 end0))
  8569. (end (max beg0 end0))
  8570. (tabsep t)
  8571. re)
  8572. (goto-char beg)
  8573. (beginning-of-line 1)
  8574. (setq beg (move-marker (make-marker) (point)))
  8575. (goto-char end)
  8576. (if (bolp) (backward-char 1) (end-of-line 1))
  8577. (setq end (move-marker (make-marker) (point)))
  8578. ;; Lets see if this is tab-separated material. If every nonempty line
  8579. ;; contains a tab, we will assume that it is tab-separated material
  8580. (if nspace
  8581. (setq tabsep nil)
  8582. (goto-char beg)
  8583. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  8584. (if nspace (setq tabsep nil))
  8585. (if tabsep
  8586. (setq re "^\\|\t")
  8587. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  8588. (max 1 (prefix-numeric-value nspace)))))
  8589. (goto-char beg)
  8590. (while (re-search-forward re end t)
  8591. (replace-match "|" t t))
  8592. (goto-char beg)
  8593. (insert " ")
  8594. (org-table-align)))
  8595. (defun org-table-import (file arg)
  8596. "Import FILE as a table.
  8597. The file is assumed to be tab-separated. Such files can be produced by most
  8598. spreadsheet and database applications. If no tabs (at least one per line)
  8599. are found, lines will be split on whitespace into fields."
  8600. (interactive "f\nP")
  8601. (or (bolp) (newline))
  8602. (let ((beg (point))
  8603. (pm (point-max)))
  8604. (insert-file-contents file)
  8605. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  8606. (defun org-table-export ()
  8607. "Export table as a tab-separated file.
  8608. Such a file can be imported into a spreadsheet program like Excel."
  8609. (interactive)
  8610. (let* ((beg (org-table-begin))
  8611. (end (org-table-end))
  8612. (table (buffer-substring beg end))
  8613. (file (read-file-name "Export table to: "))
  8614. buf)
  8615. (unless (or (not (file-exists-p file))
  8616. (y-or-n-p (format "Overwrite file %s? " file)))
  8617. (error "Abort"))
  8618. (with-current-buffer (find-file-noselect file)
  8619. (setq buf (current-buffer))
  8620. (erase-buffer)
  8621. (fundamental-mode)
  8622. (insert table)
  8623. (goto-char (point-min))
  8624. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  8625. (replace-match "" t t)
  8626. (end-of-line 1))
  8627. (goto-char (point-min))
  8628. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  8629. (replace-match "" t t)
  8630. (goto-char (min (1+ (point)) (point-max))))
  8631. (goto-char (point-min))
  8632. (while (re-search-forward "^-[-+]*$" nil t)
  8633. (replace-match "")
  8634. (if (looking-at "\n")
  8635. (delete-char 1)))
  8636. (goto-char (point-min))
  8637. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  8638. (replace-match "\t" t t))
  8639. (save-buffer))
  8640. (kill-buffer buf)))
  8641. (defvar org-table-aligned-begin-marker (make-marker)
  8642. "Marker at the beginning of the table last aligned.
  8643. Used to check if cursor still is in that table, to minimize realignment.")
  8644. (defvar org-table-aligned-end-marker (make-marker)
  8645. "Marker at the end of the table last aligned.
  8646. Used to check if cursor still is in that table, to minimize realignment.")
  8647. (defvar org-table-last-alignment nil
  8648. "List of flags for flushright alignment, from the last re-alignment.
  8649. This is being used to correctly align a single field after TAB or RET.")
  8650. (defvar org-table-last-column-widths nil
  8651. "List of max width of fields in each column.
  8652. This is being used to correctly align a single field after TAB or RET.")
  8653. (defvar org-last-recalc-line nil)
  8654. (defconst org-narrow-column-arrow "=>"
  8655. "Used as display property in narrowed table columns.")
  8656. (defun org-table-align ()
  8657. "Align the table at point by aligning all vertical bars."
  8658. (interactive)
  8659. (let* (
  8660. ;; Limits of table
  8661. (beg (org-table-begin))
  8662. (end (org-table-end))
  8663. ;; Current cursor position
  8664. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8665. (colpos (org-table-current-column))
  8666. (winstart (window-start))
  8667. text lines (new "") lengths l typenums ty fields maxfields i
  8668. column
  8669. (indent "") cnt frac
  8670. rfmt hfmt
  8671. (spaces '(1 . 1))
  8672. (sp1 (car spaces))
  8673. (sp2 (cdr spaces))
  8674. (rfmt1 (concat
  8675. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  8676. (hfmt1 (concat
  8677. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  8678. emptystrings xx links narrow fmax fmin f1 len c e)
  8679. (untabify beg end)
  8680. (remove-text-properties beg end '(org-cwidth t display t))
  8681. ;; Check if we have links
  8682. (goto-char beg)
  8683. (setq links (re-search-forward org-bracket-link-regexp end t))
  8684. ;; Make sure the link properties are right FIXME: Can this be optimized????
  8685. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  8686. ;; Check if we are narrowing any columns
  8687. (goto-char beg)
  8688. (setq narrow (and org-format-transports-properties-p
  8689. (re-search-forward "<[0-9]+>" end t)))
  8690. ;; Get the rows
  8691. (setq lines (org-split-string
  8692. (buffer-substring beg end) "\n"))
  8693. ;; Store the indentation of the first line
  8694. (if (string-match "^ *" (car lines))
  8695. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  8696. ;; Mark the hlines by setting the corresponding element to nil
  8697. ;; At the same time, we remove trailing space.
  8698. (setq lines (mapcar (lambda (l)
  8699. (if (string-match "^ *|-" l)
  8700. nil
  8701. (if (string-match "[ \t]+$" l)
  8702. (substring l 0 (match-beginning 0))
  8703. l)))
  8704. lines))
  8705. ;; Get the data fields by splitting the lines.
  8706. (setq fields (mapcar
  8707. (lambda (l)
  8708. (org-split-string l " *| *"))
  8709. (delq nil (copy-sequence lines))))
  8710. ;; How many fields in the longest line?
  8711. (condition-case nil
  8712. (setq maxfields (apply 'max (mapcar 'length fields)))
  8713. (error
  8714. (kill-region beg end)
  8715. (org-table-create org-table-default-size)
  8716. (error "Empty table - created default table")))
  8717. ;; A list of empty string to fill any short rows on output
  8718. (setq emptystrings (make-list maxfields ""))
  8719. ;; Check for special formatting.
  8720. (setq i -1)
  8721. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  8722. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  8723. ;; Check if there is an explicit width specified
  8724. (when (and org-table-limit-column-width narrow)
  8725. (setq c column fmax nil)
  8726. (while c
  8727. (setq e (pop c))
  8728. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  8729. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  8730. ;; Find fields that are wider than fmax, and shorten them
  8731. (when fmax
  8732. (loop for xx in column do
  8733. (when (and (stringp xx)
  8734. (> (org-string-width xx) fmax))
  8735. (org-add-props xx nil
  8736. 'help-echo
  8737. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  8738. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  8739. (unless (> f1 1)
  8740. (error "Cannot narrow field starting with wide link \"%s\""
  8741. (match-string 0 xx)))
  8742. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  8743. (add-text-properties (- f1 2) f1
  8744. (list 'display org-narrow-column-arrow)
  8745. xx)))))
  8746. ;; Get the maximum width for each column
  8747. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  8748. ;; Get the fraction of numbers, to decide about alignment of the column
  8749. (setq cnt 0 frac 0.0)
  8750. (loop for x in column do
  8751. (if (equal x "")
  8752. nil
  8753. (setq frac ( / (+ (* frac cnt)
  8754. (if (string-match org-table-number-regexp x) 1 0))
  8755. (setq cnt (1+ cnt))))))
  8756. (push (>= frac org-table-number-fraction) typenums))
  8757. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  8758. ;; Store the alignment of this table, for later editing of single fields
  8759. (setq org-table-last-alignment typenums
  8760. org-table-last-column-widths lengths)
  8761. ;; With invisible characters, `format' does not get the field width right
  8762. ;; So we need to make these fields wide by hand.
  8763. (when links
  8764. (loop for i from 0 upto (1- maxfields) do
  8765. (setq len (nth i lengths))
  8766. (loop for j from 0 upto (1- (length fields)) do
  8767. (setq c (nthcdr i (car (nthcdr j fields))))
  8768. (if (and (stringp (car c))
  8769. (string-match org-bracket-link-regexp (car c))
  8770. (< (org-string-width (car c)) len))
  8771. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  8772. ;; Compute the formats needed for output of the table
  8773. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  8774. (while (setq l (pop lengths))
  8775. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  8776. (setq rfmt (concat rfmt (format rfmt1 ty l))
  8777. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  8778. (setq rfmt (concat rfmt "\n")
  8779. hfmt (concat (substring hfmt 0 -1) "|\n"))
  8780. (setq new (mapconcat
  8781. (lambda (l)
  8782. (if l (apply 'format rfmt
  8783. (append (pop fields) emptystrings))
  8784. hfmt))
  8785. lines ""))
  8786. ;; Replace the old one
  8787. (delete-region beg end)
  8788. (move-marker end nil)
  8789. (move-marker org-table-aligned-begin-marker (point))
  8790. (insert new)
  8791. (move-marker org-table-aligned-end-marker (point))
  8792. (when (and orgtbl-mode (not (eq major-mode 'org-mode)))
  8793. (goto-char org-table-aligned-begin-marker)
  8794. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  8795. ;; Try to move to the old location (approximately)
  8796. (goto-line linepos)
  8797. (set-window-start (selected-window) winstart 'noforce)
  8798. (org-table-goto-column colpos)
  8799. (setq org-table-may-need-update nil)
  8800. ))
  8801. (defun org-string-width (s)
  8802. "Compute width of string, ignoring invisible characters.
  8803. This ignores character with invisibility property `org-link', and also
  8804. characters with property `org-cwidth', because these will become invisible
  8805. upon the next fontification round."
  8806. (let (b)
  8807. (when (or (eq t buffer-invisibility-spec)
  8808. (assq 'org-link buffer-invisibility-spec))
  8809. (while (setq b (text-property-any 0 (length s)
  8810. 'invisible 'org-link s))
  8811. (setq s (concat (substring s 0 b)
  8812. (substring s (or (next-single-property-change
  8813. b 'invisible s) (length s)))))))
  8814. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  8815. (setq s (concat (substring s 0 b)
  8816. (substring s (or (next-single-property-change
  8817. b 'org-cwidth s) (length s))))))
  8818. (string-width s)))
  8819. (defun org-table-begin (&optional table-type)
  8820. "Find the beginning of the table and return its position.
  8821. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  8822. (save-excursion
  8823. (if (not (re-search-backward
  8824. (if table-type org-table-any-border-regexp
  8825. org-table-border-regexp)
  8826. nil t))
  8827. (progn (goto-char (point-min)) (point))
  8828. (goto-char (match-beginning 0))
  8829. (beginning-of-line 2)
  8830. (point))))
  8831. (defun org-table-end (&optional table-type)
  8832. "Find the end of the table and return its position.
  8833. With argument TABLE-TYPE, go to the end of a table.el-type table."
  8834. (save-excursion
  8835. (if (not (re-search-forward
  8836. (if table-type org-table-any-border-regexp
  8837. org-table-border-regexp)
  8838. nil t))
  8839. (goto-char (point-max))
  8840. (goto-char (match-beginning 0)))
  8841. (point-marker)))
  8842. (defun org-table-justify-field-maybe (&optional new)
  8843. "Justify the current field, text to left, number to right.
  8844. Optional argument NEW may specify text to replace the current field content."
  8845. (cond
  8846. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  8847. ((org-at-table-hline-p))
  8848. ((and (not new)
  8849. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  8850. (current-buffer)))
  8851. (< (point) org-table-aligned-begin-marker)
  8852. (>= (point) org-table-aligned-end-marker)))
  8853. ;; This is not the same table, force a full re-align
  8854. (setq org-table-may-need-update t))
  8855. (t ;; realign the current field, based on previous full realign
  8856. (let* ((pos (point)) s
  8857. (col (org-table-current-column))
  8858. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  8859. l f n o e)
  8860. (when (> col 0)
  8861. (skip-chars-backward "^|\n")
  8862. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  8863. (progn
  8864. (setq s (match-string 1)
  8865. o (match-string 0)
  8866. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  8867. e (not (= (match-beginning 2) (match-end 2))))
  8868. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  8869. l (if e "|" (setq org-table-may-need-update t) ""))
  8870. n (format f s))
  8871. (if new
  8872. (if (<= (length new) l) ;; FIXME: length -> str-width?
  8873. (setq n (format f new t t)) ;; FIXME: t t?
  8874. (setq n (concat new "|") org-table-may-need-update t)))
  8875. (or (equal n o)
  8876. (let (org-table-may-need-update)
  8877. (replace-match n))))
  8878. (setq org-table-may-need-update t))
  8879. (goto-char pos))))))
  8880. (defun org-table-next-field ()
  8881. "Go to the next field in the current table, creating new lines as needed.
  8882. Before doing so, re-align the table if necessary."
  8883. (interactive)
  8884. (org-table-maybe-eval-formula)
  8885. (org-table-maybe-recalculate-line)
  8886. (if (and org-table-automatic-realign
  8887. org-table-may-need-update)
  8888. (org-table-align))
  8889. (let ((end (org-table-end)))
  8890. (if (org-at-table-hline-p)
  8891. (end-of-line 1))
  8892. (condition-case nil
  8893. (progn
  8894. (re-search-forward "|" end)
  8895. (if (looking-at "[ \t]*$")
  8896. (re-search-forward "|" end))
  8897. (if (and (looking-at "-")
  8898. org-table-tab-jumps-over-hlines
  8899. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  8900. (goto-char (match-beginning 1)))
  8901. (if (looking-at "-")
  8902. (progn
  8903. (beginning-of-line 0)
  8904. (org-table-insert-row 'below))
  8905. (if (looking-at " ") (forward-char 1))))
  8906. (error
  8907. (org-table-insert-row 'below)))))
  8908. (defun org-table-previous-field ()
  8909. "Go to the previous field in the table.
  8910. Before doing so, re-align the table if necessary."
  8911. (interactive)
  8912. (org-table-justify-field-maybe)
  8913. (org-table-maybe-recalculate-line)
  8914. (if (and org-table-automatic-realign
  8915. org-table-may-need-update)
  8916. (org-table-align))
  8917. (if (org-at-table-hline-p)
  8918. (end-of-line 1))
  8919. (re-search-backward "|" (org-table-begin))
  8920. (re-search-backward "|" (org-table-begin))
  8921. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  8922. (re-search-backward "|" (org-table-begin)))
  8923. (if (looking-at "| ?")
  8924. (goto-char (match-end 0))))
  8925. (defun org-table-next-row ()
  8926. "Go to the next row (same column) in the current table.
  8927. Before doing so, re-align the table if necessary."
  8928. (interactive)
  8929. (org-table-maybe-eval-formula)
  8930. (org-table-maybe-recalculate-line)
  8931. (if (or (looking-at "[ \t]*$")
  8932. (save-excursion (skip-chars-backward " \t") (bolp)))
  8933. (newline)
  8934. (if (and org-table-automatic-realign
  8935. org-table-may-need-update)
  8936. (org-table-align))
  8937. (let ((col (org-table-current-column)))
  8938. (beginning-of-line 2)
  8939. (if (or (not (org-at-table-p))
  8940. (org-at-table-hline-p))
  8941. (progn
  8942. (beginning-of-line 0)
  8943. (org-table-insert-row 'below)))
  8944. (org-table-goto-column col)
  8945. (skip-chars-backward "^|\n\r")
  8946. (if (looking-at " ") (forward-char 1)))))
  8947. (defun org-table-copy-down (n)
  8948. "Copy a field down in the current column.
  8949. If the field at the cursor is empty, copy into it the content of the nearest
  8950. non-empty field above. With argument N, use the Nth non-empty field.
  8951. If the current field is not empty, it is copied down to the next row, and
  8952. the cursor is moved with it. Therefore, repeating this command causes the
  8953. column to be filled row-by-row.
  8954. If the variable `org-table-copy-increment' is non-nil and the field is an
  8955. integer, it will be incremented while copying."
  8956. (interactive "p")
  8957. (let* ((colpos (org-table-current-column))
  8958. (field (org-table-get-field))
  8959. (non-empty (string-match "[^ \t]" field))
  8960. (beg (org-table-begin))
  8961. txt)
  8962. (org-table-check-inside-data-field)
  8963. (if non-empty
  8964. (progn
  8965. (setq txt (org-trim field))
  8966. (org-table-next-row)
  8967. (org-table-blank-field))
  8968. (save-excursion
  8969. (setq txt
  8970. (catch 'exit
  8971. (while (progn (beginning-of-line 1)
  8972. (re-search-backward org-table-dataline-regexp
  8973. beg t))
  8974. (org-table-goto-column colpos t)
  8975. (if (and (looking-at
  8976. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8977. (= (setq n (1- n)) 0))
  8978. (throw 'exit (match-string 1))))))))
  8979. (if txt
  8980. (progn
  8981. (if (and org-table-copy-increment
  8982. (string-match "^[0-9]+$" txt))
  8983. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8984. (insert txt)
  8985. (org-table-maybe-recalculate-line)
  8986. (org-table-align))
  8987. (error "No non-empty field found"))))
  8988. (defun org-table-check-inside-data-field ()
  8989. "Is point inside a table data field?
  8990. I.e. not on a hline or before the first or after the last column?
  8991. This actually throws an error, so it aborts the current command."
  8992. (if (or (not (org-at-table-p))
  8993. (= (org-table-current-column) 0)
  8994. (org-at-table-hline-p)
  8995. (looking-at "[ \t]*$"))
  8996. (error "Not in table data field")))
  8997. (defvar org-table-clip nil
  8998. "Clipboard for table regions.")
  8999. (defun org-table-blank-field ()
  9000. "Blank the current table field or active region."
  9001. (interactive)
  9002. (org-table-check-inside-data-field)
  9003. (if (and (interactive-p) (org-region-active-p))
  9004. (let (org-table-clip)
  9005. (org-table-cut-region (region-beginning) (region-end)))
  9006. (skip-chars-backward "^|")
  9007. (backward-char 1)
  9008. (if (looking-at "|[^|\n]+")
  9009. (let* ((pos (match-beginning 0))
  9010. (match (match-string 0))
  9011. (len (org-string-width match)))
  9012. (replace-match (concat "|" (make-string (1- len) ?\ )))
  9013. (goto-char (+ 2 pos))
  9014. (substring match 1)))))
  9015. (defun org-table-get-field (&optional n replace)
  9016. "Return the value of the field in column N of current row.
  9017. N defaults to current field.
  9018. If REPLACE is a string, replace field with this value. The return value
  9019. is always the old value."
  9020. (and n (org-table-goto-column n))
  9021. (skip-chars-backward "^|\n")
  9022. (backward-char 1)
  9023. (if (looking-at "|[^|\r\n]*")
  9024. (let* ((pos (match-beginning 0))
  9025. (val (buffer-substring (1+ pos) (match-end 0))))
  9026. (if replace
  9027. (replace-match (concat "|" replace)))
  9028. (goto-char (min (point-at-eol) (+ 2 pos)))
  9029. val)
  9030. (forward-char 1) ""))
  9031. (defun org-table-current-column ()
  9032. "Find out which column we are in.
  9033. When called interactively, column is also displayed in echo area."
  9034. (interactive)
  9035. (if (interactive-p) (org-table-check-inside-data-field))
  9036. (save-excursion
  9037. (let ((cnt 0) (pos (point)))
  9038. (beginning-of-line 1)
  9039. (while (search-forward "|" pos t)
  9040. (setq cnt (1+ cnt)))
  9041. (if (interactive-p) (message "This is table column %d" cnt))
  9042. cnt)))
  9043. (defun org-table-goto-column (n &optional on-delim force)
  9044. "Move the cursor to the Nth column in the current table line.
  9045. With optional argument ON-DELIM, stop with point before the left delimiter
  9046. of the field.
  9047. If there are less than N fields, just go to after the last delimiter.
  9048. However, when FORCE is non-nil, create new columns if necessary."
  9049. (interactive "p")
  9050. (let ((pos (point-at-eol)))
  9051. (beginning-of-line 1)
  9052. (when (> n 0)
  9053. (while (and (> (setq n (1- n)) -1)
  9054. (or (search-forward "|" pos t)
  9055. (and force
  9056. (progn (end-of-line 1)
  9057. (skip-chars-backward "^|")
  9058. (insert " | "))))))
  9059. ; (backward-char 2) t)))))
  9060. (when (and force (not (looking-at ".*|")))
  9061. (save-excursion (end-of-line 1) (insert " | ")))
  9062. (if on-delim
  9063. (backward-char 1)
  9064. (if (looking-at " ") (forward-char 1))))))
  9065. (defun org-at-table-p (&optional table-type)
  9066. "Return t if the cursor is inside an org-type table.
  9067. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  9068. (if org-enable-table-editor
  9069. (save-excursion
  9070. (beginning-of-line 1)
  9071. (looking-at (if table-type org-table-any-line-regexp
  9072. org-table-line-regexp)))
  9073. nil))
  9074. (defun org-at-table.el-p ()
  9075. "Return t if and only if we are at a table.el table."
  9076. (and (org-at-table-p 'any)
  9077. (save-excursion
  9078. (goto-char (org-table-begin 'any))
  9079. (looking-at org-table1-hline-regexp))))
  9080. (defun org-table-recognize-table.el ()
  9081. "If there is a table.el table nearby, recognize it and move into it."
  9082. (if org-table-tab-recognizes-table.el
  9083. (if (org-at-table.el-p)
  9084. (progn
  9085. (beginning-of-line 1)
  9086. (if (looking-at org-table-dataline-regexp)
  9087. nil
  9088. (if (looking-at org-table1-hline-regexp)
  9089. (progn
  9090. (beginning-of-line 2)
  9091. (if (looking-at org-table-any-border-regexp)
  9092. (beginning-of-line -1)))))
  9093. (if (re-search-forward "|" (org-table-end t) t)
  9094. (progn
  9095. (require 'table)
  9096. (if (table--at-cell-p (point))
  9097. t
  9098. (message "recognizing table.el table...")
  9099. (table-recognize-table)
  9100. (message "recognizing table.el table...done")))
  9101. (error "This should not happen..."))
  9102. t)
  9103. nil)
  9104. nil))
  9105. (defun org-at-table-hline-p ()
  9106. "Return t if the cursor is inside a hline in a table."
  9107. (if org-enable-table-editor
  9108. (save-excursion
  9109. (beginning-of-line 1)
  9110. (looking-at org-table-hline-regexp))
  9111. nil))
  9112. (defun org-table-insert-column ()
  9113. "Insert a new column into the table."
  9114. (interactive)
  9115. (if (not (org-at-table-p))
  9116. (error "Not at a table"))
  9117. (org-table-find-dataline)
  9118. (let* ((col (max 1 (org-table-current-column)))
  9119. (beg (org-table-begin))
  9120. (end (org-table-end))
  9121. ;; Current cursor position
  9122. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9123. (colpos col))
  9124. (goto-char beg)
  9125. (while (< (point) end)
  9126. (if (org-at-table-hline-p)
  9127. nil
  9128. (org-table-goto-column col t)
  9129. (insert "| "))
  9130. (beginning-of-line 2))
  9131. (move-marker end nil)
  9132. (goto-line linepos)
  9133. (org-table-goto-column colpos)
  9134. (org-table-align)
  9135. (org-table-modify-formulas 'insert col)))
  9136. (defun org-table-find-dataline ()
  9137. "Find a dataline in the current table, which is needed for column commands."
  9138. (if (and (org-at-table-p)
  9139. (not (org-at-table-hline-p)))
  9140. t
  9141. (let ((col (current-column))
  9142. (end (org-table-end)))
  9143. (move-to-column col)
  9144. (while (and (< (point) end)
  9145. (or (not (= (current-column) col))
  9146. (org-at-table-hline-p)))
  9147. (beginning-of-line 2)
  9148. (move-to-column col))
  9149. (if (and (org-at-table-p)
  9150. (not (org-at-table-hline-p)))
  9151. t
  9152. (error
  9153. "Please position cursor in a data line for column operations")))))
  9154. (defun org-table-delete-column ()
  9155. "Delete a column from the table."
  9156. (interactive)
  9157. (if (not (org-at-table-p))
  9158. (error "Not at a table"))
  9159. (org-table-find-dataline)
  9160. (org-table-check-inside-data-field)
  9161. (let* ((col (org-table-current-column))
  9162. (beg (org-table-begin))
  9163. (end (org-table-end))
  9164. ;; Current cursor position
  9165. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9166. (colpos col))
  9167. (goto-char beg)
  9168. (while (< (point) end)
  9169. (if (org-at-table-hline-p)
  9170. nil
  9171. (org-table-goto-column col t)
  9172. (and (looking-at "|[^|\n]+|")
  9173. (replace-match "|")))
  9174. (beginning-of-line 2))
  9175. (move-marker end nil)
  9176. (goto-line linepos)
  9177. (org-table-goto-column colpos)
  9178. (org-table-align)
  9179. (org-table-modify-formulas 'remove col)))
  9180. (defun org-table-move-column-right ()
  9181. "Move column to the right."
  9182. (interactive)
  9183. (org-table-move-column nil))
  9184. (defun org-table-move-column-left ()
  9185. "Move column to the left."
  9186. (interactive)
  9187. (org-table-move-column 'left))
  9188. (defun org-table-move-column (&optional left)
  9189. "Move the current column to the right. With arg LEFT, move to the left."
  9190. (interactive "P")
  9191. (if (not (org-at-table-p))
  9192. (error "Not at a table"))
  9193. (org-table-find-dataline)
  9194. (org-table-check-inside-data-field)
  9195. (let* ((col (org-table-current-column))
  9196. (col1 (if left (1- col) col))
  9197. (beg (org-table-begin))
  9198. (end (org-table-end))
  9199. ;; Current cursor position
  9200. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9201. (colpos (if left (1- col) (1+ col))))
  9202. (if (and left (= col 1))
  9203. (error "Cannot move column further left"))
  9204. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  9205. (error "Cannot move column further right"))
  9206. (goto-char beg)
  9207. (while (< (point) end)
  9208. (if (org-at-table-hline-p)
  9209. nil
  9210. (org-table-goto-column col1 t)
  9211. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  9212. (replace-match "|\\2|\\1|")))
  9213. (beginning-of-line 2))
  9214. (move-marker end nil)
  9215. (goto-line linepos)
  9216. (org-table-goto-column colpos)
  9217. (org-table-align)
  9218. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  9219. (defun org-table-move-row-down ()
  9220. "Move table row down."
  9221. (interactive)
  9222. (org-table-move-row nil))
  9223. (defun org-table-move-row-up ()
  9224. "Move table row up."
  9225. (interactive)
  9226. (org-table-move-row 'up))
  9227. (defun org-table-move-row (&optional up)
  9228. "Move the current table line down. With arg UP, move it up."
  9229. (interactive "P")
  9230. (let ((col (current-column))
  9231. (pos (point))
  9232. (tonew (if up 0 2))
  9233. txt)
  9234. (beginning-of-line tonew)
  9235. (if (not (org-at-table-p))
  9236. (progn
  9237. (goto-char pos)
  9238. (error "Cannot move row further")))
  9239. (goto-char pos)
  9240. (beginning-of-line 1)
  9241. (setq pos (point))
  9242. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  9243. (delete-region (point) (1+ (point-at-eol)))
  9244. (beginning-of-line tonew)
  9245. (insert txt)
  9246. (beginning-of-line 0)
  9247. (move-to-column col)))
  9248. (defun org-table-insert-row (&optional arg)
  9249. "Insert a new row above the current line into the table.
  9250. With prefix ARG, insert below the current line."
  9251. (interactive "P")
  9252. (if (not (org-at-table-p))
  9253. (error "Not at a table"))
  9254. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  9255. (new (org-table-clean-line line)))
  9256. ;; Fix the first field if necessary
  9257. (if (string-match "^[ \t]*| *[#$] *|" line)
  9258. (setq new (replace-match (match-string 0 line) t t new)))
  9259. (beginning-of-line (if arg 2 1))
  9260. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  9261. (beginning-of-line 0)
  9262. (re-search-forward "| ?" (point-at-eol) t)
  9263. (and org-table-may-need-update (org-table-align))))
  9264. (defun org-table-insert-hline (&optional arg)
  9265. "Insert a horizontal-line below the current line into the table.
  9266. With prefix ARG, insert above the current line."
  9267. (interactive "P")
  9268. (if (not (org-at-table-p))
  9269. (error "Not at a table"))
  9270. (let ((line (org-table-clean-line
  9271. (buffer-substring (point-at-bol) (point-at-eol))))
  9272. (col (current-column)))
  9273. (while (string-match "|\\( +\\)|" line)
  9274. (setq line (replace-match
  9275. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  9276. ?-) "|") t t line)))
  9277. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  9278. (beginning-of-line (if arg 1 2))
  9279. (insert line "\n")
  9280. (beginning-of-line (if arg 1 -1))
  9281. (move-to-column col)))
  9282. (defun org-table-clean-line (s)
  9283. "Convert a table line S into a string with only \"|\" and space.
  9284. In particular, this does handle wide and invisible characters."
  9285. (if (string-match "^[ \t]*|-" s)
  9286. ;; It's a hline, just map the characters
  9287. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  9288. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  9289. (setq s (replace-match
  9290. (concat "|" (make-string (org-string-width (match-string 1 s))
  9291. ?\ ) "|")
  9292. t t s)))
  9293. s))
  9294. (defun org-table-kill-row ()
  9295. "Delete the current row or horizontal line from the table."
  9296. (interactive)
  9297. (if (not (org-at-table-p))
  9298. (error "Not at a table"))
  9299. (let ((col (current-column)))
  9300. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  9301. (if (not (org-at-table-p)) (beginning-of-line 0))
  9302. (move-to-column col)))
  9303. (defun org-table-sort-lines (beg end numericp)
  9304. "Sort table lines in region.
  9305. Point and mark define the first and last line to include. Both point and
  9306. mark should be in the column that is used for sorting. For example, to
  9307. sort according to column 3, put the mark in the first line to sort, in
  9308. table column 3. Put point into the last line to be included in the sorting,
  9309. also in table column 3. The command will prompt for the sorting method
  9310. \(n for numerical, a for alphanumeric)."
  9311. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  9312. (setq numericp (string-match "[nN]" numericp))
  9313. (org-table-align) ;; Just to be safe
  9314. (let* (bcol ecol cmp column lns)
  9315. (goto-char beg)
  9316. (org-table-check-inside-data-field)
  9317. (setq column (org-table-current-column)
  9318. beg (move-marker (make-marker) (point-at-bol)))
  9319. (goto-char end)
  9320. (org-table-check-inside-data-field)
  9321. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  9322. (untabify beg end)
  9323. (goto-char beg)
  9324. (org-table-goto-column column)
  9325. (skip-chars-backward "^|")
  9326. (setq bcol (current-column))
  9327. (org-table-goto-column (1+ column))
  9328. (skip-chars-backward "^|")
  9329. (setq ecol (1- (current-column)))
  9330. (setq cmp (if numericp
  9331. (lambda (a b) (< (car a) (car b)))
  9332. (lambda (a b) (string< (car a) (car b)))))
  9333. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  9334. (org-split-string (buffer-substring beg end) "\n")))
  9335. (if numericp
  9336. (setq lns (mapcar (lambda(x)
  9337. (cons (string-to-number (car x)) (cdr x)))
  9338. lns)))
  9339. (delete-region beg end)
  9340. (move-marker beg nil)
  9341. (move-marker end nil)
  9342. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  9343. (message "%d lines sorted %s based on column %d"
  9344. (length lns)
  9345. (if numericp "numerically" "alphabetically") column)))
  9346. (defun org-table-cut-region (beg end)
  9347. "Copy region in table to the clipboard and blank all relevant fields."
  9348. (interactive "r")
  9349. (org-table-copy-region beg end 'cut))
  9350. (defun org-table-copy-region (beg end &optional cut)
  9351. "Copy rectangular region in table to clipboard.
  9352. A special clipboard is used which can only be accessed
  9353. with `org-table-paste-rectangle'."
  9354. (interactive "rP")
  9355. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  9356. region cols
  9357. (rpl (if cut " " nil)))
  9358. (goto-char beg)
  9359. (org-table-check-inside-data-field)
  9360. (setq l01 (count-lines (point-min) (point))
  9361. c01 (org-table-current-column))
  9362. (goto-char end)
  9363. (org-table-check-inside-data-field)
  9364. (setq l02 (count-lines (point-min) (point))
  9365. c02 (org-table-current-column))
  9366. (setq l1 (min l01 l02) l2 (max l01 l02)
  9367. c1 (min c01 c02) c2 (max c01 c02))
  9368. (catch 'exit
  9369. (while t
  9370. (catch 'nextline
  9371. (if (> l1 l2) (throw 'exit t))
  9372. (goto-line l1)
  9373. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  9374. (setq cols nil ic1 c1 ic2 c2)
  9375. (while (< ic1 (1+ ic2))
  9376. (push (org-table-get-field ic1 rpl) cols)
  9377. (setq ic1 (1+ ic1)))
  9378. (push (nreverse cols) region)
  9379. (setq l1 (1+ l1)))))
  9380. (setq org-table-clip (nreverse region))
  9381. (if cut (org-table-align))
  9382. org-table-clip))
  9383. (defun org-table-paste-rectangle ()
  9384. "Paste a rectangular region into a table.
  9385. The upper right corner ends up in the current field. All involved fields
  9386. will be overwritten. If the rectangle does not fit into the present table,
  9387. the table is enlarged as needed. The process ignores horizontal separator
  9388. lines."
  9389. (interactive)
  9390. (unless (and org-table-clip (listp org-table-clip))
  9391. (error "First cut/copy a region to paste!"))
  9392. (org-table-check-inside-data-field)
  9393. (let* ((clip org-table-clip)
  9394. (line (count-lines (point-min) (point)))
  9395. (col (org-table-current-column))
  9396. (org-enable-table-editor t)
  9397. (org-table-automatic-realign nil)
  9398. c cols field)
  9399. (while (setq cols (pop clip))
  9400. (while (org-at-table-hline-p) (beginning-of-line 2))
  9401. (if (not (org-at-table-p))
  9402. (progn (end-of-line 0) (org-table-next-field)))
  9403. (setq c col)
  9404. (while (setq field (pop cols))
  9405. (org-table-goto-column c nil 'force)
  9406. (org-table-get-field nil field)
  9407. (setq c (1+ c)))
  9408. (beginning-of-line 2))
  9409. (goto-line line)
  9410. (org-table-goto-column col)
  9411. (org-table-align)))
  9412. (defun org-table-convert ()
  9413. "Convert from `org-mode' table to table.el and back.
  9414. Obviously, this only works within limits. When an Org-mode table is
  9415. converted to table.el, all horizontal separator lines get lost, because
  9416. table.el uses these as cell boundaries and has no notion of horizontal lines.
  9417. A table.el table can be converted to an Org-mode table only if it does not
  9418. do row or column spanning. Multiline cells will become multiple cells.
  9419. Beware, Org-mode does not test if the table can be successfully converted - it
  9420. blindly applies a recipe that works for simple tables."
  9421. (interactive)
  9422. (require 'table)
  9423. (if (org-at-table.el-p)
  9424. ;; convert to Org-mode table
  9425. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  9426. (end (move-marker (make-marker) (org-table-end t))))
  9427. (table-unrecognize-region beg end)
  9428. (goto-char beg)
  9429. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  9430. (replace-match ""))
  9431. (goto-char beg))
  9432. (if (org-at-table-p)
  9433. ;; convert to table.el table
  9434. (let ((beg (move-marker (make-marker) (org-table-begin)))
  9435. (end (move-marker (make-marker) (org-table-end))))
  9436. ;; first, get rid of all horizontal lines
  9437. (goto-char beg)
  9438. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  9439. (replace-match ""))
  9440. ;; insert a hline before first
  9441. (goto-char beg)
  9442. (org-table-insert-hline 'above)
  9443. (beginning-of-line -1)
  9444. ;; insert a hline after each line
  9445. (while (progn (beginning-of-line 3) (< (point) end))
  9446. (org-table-insert-hline))
  9447. (goto-char beg)
  9448. (setq end (move-marker end (org-table-end)))
  9449. ;; replace "+" at beginning and ending of hlines
  9450. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  9451. (replace-match "\\1+-"))
  9452. (goto-char beg)
  9453. (while (re-search-forward "-|[ \t]*$" end t)
  9454. (replace-match "-+"))
  9455. (goto-char beg)))))
  9456. (defun org-table-wrap-region (arg)
  9457. "Wrap several fields in a column like a paragraph.
  9458. This is useful if you'd like to spread the contents of a field over several
  9459. lines, in order to keep the table compact.
  9460. If there is an active region, and both point and mark are in the same column,
  9461. the text in the column is wrapped to minimum width for the given number of
  9462. lines. Generally, this makes the table more compact. A prefix ARG may be
  9463. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  9464. formats the selected text to two lines. If the region was longer than two
  9465. lines, the remaining lines remain empty. A negative prefix argument reduces
  9466. the current number of lines by that amount. The wrapped text is pasted back
  9467. into the table. If you formatted it to more lines than it was before, fields
  9468. further down in the table get overwritten - so you might need to make space in
  9469. the table first.
  9470. If there is no region, the current field is split at the cursor position and
  9471. the text fragment to the right of the cursor is prepended to the field one
  9472. line down.
  9473. If there is no region, but you specify a prefix ARG, the current field gets
  9474. blank, and the content is appended to the field above."
  9475. (interactive "P")
  9476. (org-table-check-inside-data-field)
  9477. (if (org-region-active-p)
  9478. ;; There is a region: fill as a paragraph
  9479. (let ((beg (region-beginning))
  9480. nlines)
  9481. (org-table-cut-region (region-beginning) (region-end))
  9482. (if (> (length (car org-table-clip)) 1)
  9483. (error "Region must be limited to single column"))
  9484. (setq nlines (if arg
  9485. (if (< arg 1)
  9486. (+ (length org-table-clip) arg)
  9487. arg)
  9488. (length org-table-clip)))
  9489. (setq org-table-clip
  9490. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  9491. nil nlines)))
  9492. (goto-char beg)
  9493. (org-table-paste-rectangle))
  9494. ;; No region, split the current field at point
  9495. (if arg
  9496. ;; combine with field above
  9497. (let ((s (org-table-blank-field))
  9498. (col (org-table-current-column)))
  9499. (beginning-of-line 0)
  9500. (while (org-at-table-hline-p) (beginning-of-line 0))
  9501. (org-table-goto-column col)
  9502. (skip-chars-forward "^|")
  9503. (skip-chars-backward " ")
  9504. (insert " " (org-trim s))
  9505. (org-table-align))
  9506. ;; split field
  9507. (when (looking-at "\\([^|]+\\)+|")
  9508. (let ((s (match-string 1)))
  9509. (replace-match " |")
  9510. (goto-char (match-beginning 0))
  9511. (org-table-next-row)
  9512. (insert (org-trim s) " ")
  9513. (org-table-align))))))
  9514. (defvar org-field-marker nil)
  9515. (defun org-table-edit-field (arg)
  9516. "Edit table field in a different window.
  9517. This is mainly useful for fields that contain hidden parts.
  9518. When called with a \\[universal-argument] prefix, just make the full field visible so that
  9519. it can be edited in place."
  9520. (interactive "P")
  9521. (if arg
  9522. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  9523. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  9524. (remove-text-properties b e '(org-cwidth t invisible t
  9525. display t intangible t))
  9526. (if (and (boundp 'font-lock-mode) font-lock-mode)
  9527. (font-lock-fontify-block)))
  9528. (let ((pos (move-marker (make-marker) (point)))
  9529. (field (org-table-get-field))
  9530. (cw (current-window-configuration))
  9531. p)
  9532. (switch-to-buffer-other-window "*Org tmp*")
  9533. (erase-buffer)
  9534. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  9535. (org-mode)
  9536. (goto-char (setq p (point-max)))
  9537. (insert (org-trim field))
  9538. (remove-text-properties p (point-max)
  9539. '(invisible t org-cwidth t display t
  9540. intangible t))
  9541. (goto-char p)
  9542. (set (make-local-variable 'org-finish-function)
  9543. 'org-table-finish-edit-field)
  9544. (set (make-local-variable 'org-window-configuration) cw)
  9545. (set (make-local-variable 'org-field-marker) pos)
  9546. (message "Edit and finish with C-c C-c"))))
  9547. (defun org-table-finish-edit-field ()
  9548. "Finish editing a table data field.
  9549. Remove all newline characters, insert the result into the table, realign
  9550. the table and kill the editing buffer."
  9551. (let ((pos org-field-marker)
  9552. (cw org-window-configuration)
  9553. (cb (current-buffer))
  9554. text)
  9555. (goto-char (point-min))
  9556. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  9557. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  9558. (replace-match " "))
  9559. (setq text (org-trim (buffer-string)))
  9560. (set-window-configuration cw)
  9561. (kill-buffer cb)
  9562. (select-window (get-buffer-window (marker-buffer pos)))
  9563. (goto-char pos)
  9564. (move-marker pos nil)
  9565. (org-table-check-inside-data-field)
  9566. (org-table-get-field nil text)
  9567. (org-table-align)
  9568. (message "New field value inserted")))
  9569. (defun org-trim (s)
  9570. "Remove whitespace at beginning and end of string."
  9571. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  9572. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  9573. s)
  9574. (defun org-wrap (string &optional width lines)
  9575. "Wrap string to either a number of lines, or a width in characters.
  9576. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  9577. that costs. If there is a word longer than WIDTH, the text is actually
  9578. wrapped to the length of that word.
  9579. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  9580. many lines, whatever width that takes.
  9581. The return value is a list of lines, without newlines at the end."
  9582. (let* ((words (org-split-string string "[ \t\n]+"))
  9583. (maxword (apply 'max (mapcar 'org-string-width words)))
  9584. w ll)
  9585. (cond (width
  9586. (org-do-wrap words (max maxword width)))
  9587. (lines
  9588. (setq w maxword)
  9589. (setq ll (org-do-wrap words maxword))
  9590. (if (<= (length ll) lines)
  9591. ll
  9592. (setq ll words)
  9593. (while (> (length ll) lines)
  9594. (setq w (1+ w))
  9595. (setq ll (org-do-wrap words w)))
  9596. ll))
  9597. (t (error "Cannot wrap this")))))
  9598. (defun org-do-wrap (words width)
  9599. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  9600. (let (lines line)
  9601. (while words
  9602. (setq line (pop words))
  9603. (while (and words (< (+ (length line) (length (car words))) width))
  9604. (setq line (concat line " " (pop words))))
  9605. (setq lines (push line lines)))
  9606. (nreverse lines)))
  9607. (defun org-split-string (string &optional separators)
  9608. "Splits STRING into substrings at SEPARATORS.
  9609. No empty strings are returned if there are matches at the beginning
  9610. and end of string."
  9611. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  9612. (start 0)
  9613. notfirst
  9614. (list nil))
  9615. (while (and (string-match rexp string
  9616. (if (and notfirst
  9617. (= start (match-beginning 0))
  9618. (< start (length string)))
  9619. (1+ start) start))
  9620. (< (match-beginning 0) (length string)))
  9621. (setq notfirst t)
  9622. (or (eq (match-beginning 0) 0)
  9623. (and (eq (match-beginning 0) (match-end 0))
  9624. (eq (match-beginning 0) start))
  9625. (setq list
  9626. (cons (substring string start (match-beginning 0))
  9627. list)))
  9628. (setq start (match-end 0)))
  9629. (or (eq start (length string))
  9630. (setq list
  9631. (cons (substring string start)
  9632. list)))
  9633. (nreverse list)))
  9634. (defun org-table-map-tables (function)
  9635. "Apply FUNCTION to the start of all tables in the buffer."
  9636. (save-excursion
  9637. (save-restriction
  9638. (widen)
  9639. (goto-char (point-min))
  9640. (while (re-search-forward org-table-any-line-regexp nil t)
  9641. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  9642. (beginning-of-line 1)
  9643. (if (looking-at org-table-line-regexp)
  9644. (save-excursion (funcall function)))
  9645. (re-search-forward org-table-any-border-regexp nil 1))))
  9646. (message "Mapping tables: done"))
  9647. (defun org-table-sum (&optional beg end nlast)
  9648. "Sum numbers in region of current table column.
  9649. The result will be displayed in the echo area, and will be available
  9650. as kill to be inserted with \\[yank].
  9651. If there is an active region, it is interpreted as a rectangle and all
  9652. numbers in that rectangle will be summed. If there is no active
  9653. region and point is located in a table column, sum all numbers in that
  9654. column.
  9655. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  9656. numbers are assumed to be times as well (in decimal hours) and the
  9657. numbers are added as such.
  9658. If NLAST is a number, only the NLAST fields will actually be summed."
  9659. (interactive)
  9660. (save-excursion
  9661. (let (col (timecnt 0) diff h m s org-table-clip)
  9662. (cond
  9663. ((and beg end)) ; beg and end given explicitly
  9664. ((org-region-active-p)
  9665. (setq beg (region-beginning) end (region-end)))
  9666. (t
  9667. (setq col (org-table-current-column))
  9668. (goto-char (org-table-begin))
  9669. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  9670. (error "No table data"))
  9671. (org-table-goto-column col)
  9672. ;not needed? (skip-chars-backward "^|")
  9673. (setq beg (point))
  9674. (goto-char (org-table-end))
  9675. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  9676. (error "No table data"))
  9677. (org-table-goto-column col)
  9678. ;not needed? (skip-chars-forward "^|")
  9679. (setq end (point))))
  9680. (let* ((items (apply 'append (org-table-copy-region beg end)))
  9681. (items1 (cond ((not nlast) items)
  9682. ((>= nlast (length items)) items)
  9683. (t (setq items (reverse items))
  9684. (setcdr (nthcdr (1- nlast) items) nil)
  9685. (nreverse items))))
  9686. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  9687. items1)))
  9688. (res (apply '+ numbers))
  9689. (sres (if (= timecnt 0)
  9690. (format "%g" res)
  9691. (setq diff (* 3600 res)
  9692. h (floor (/ diff 3600)) diff (mod diff 3600)
  9693. m (floor (/ diff 60)) diff (mod diff 60)
  9694. s diff)
  9695. (format "%d:%02d:%02d" h m s))))
  9696. (kill-new sres)
  9697. (if (interactive-p)
  9698. (message "%s"
  9699. (substitute-command-keys
  9700. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  9701. (length numbers) sres))))
  9702. sres))))
  9703. (defun org-table-get-number-for-summing (s)
  9704. (let (n)
  9705. (if (string-match "^ *|? *" s)
  9706. (setq s (replace-match "" nil nil s)))
  9707. (if (string-match " *|? *$" s)
  9708. (setq s (replace-match "" nil nil s)))
  9709. (setq n (string-to-number s))
  9710. (cond
  9711. ((and (string-match "0" s)
  9712. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  9713. ((string-match "\\`[ \t]+\\'" s) nil)
  9714. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  9715. (let ((h (string-to-number (or (match-string 1 s) "0")))
  9716. (m (string-to-number (or (match-string 2 s) "0")))
  9717. (s (string-to-number (or (match-string 4 s) "0"))))
  9718. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  9719. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  9720. ((equal n 0) nil)
  9721. (t n))))
  9722. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  9723. "Get a calc vector from a column, accorting to descriptor DESC.
  9724. Optional arguments TBEG and COL can give the beginning of the table and
  9725. the current column, to avoid unnecessary parsing."
  9726. (save-excursion
  9727. (or tbeg (setq tbeg (org-table-begin)))
  9728. (or col (setq col (org-table-current-column)))
  9729. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  9730. (cond
  9731. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  9732. (setq n1 (- (match-end 1) (match-beginning 1)))
  9733. (if (match-beginning 3)
  9734. (setq n2 (- (match-end 2) (match-beginning 3))))
  9735. (setq n (if n2 (max n1 n2) n1))
  9736. (setq n1 (if n2 (min n1 n2)))
  9737. (setq nn n)
  9738. (while (and (> nn 0)
  9739. (re-search-backward org-table-hline-regexp tbeg t))
  9740. (push (org-current-line) hline-list)
  9741. (setq nn (1- nn)))
  9742. (setq hline-list (nreverse hline-list))
  9743. (goto-line (nth (1- n) hline-list))
  9744. (when (re-search-forward org-table-dataline-regexp)
  9745. (org-table-goto-column col)
  9746. (setq beg (point)))
  9747. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  9748. (when (re-search-backward org-table-dataline-regexp)
  9749. (org-table-goto-column col)
  9750. (setq end (point)))
  9751. (setq l (apply 'append (org-table-copy-region beg end)))
  9752. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  9753. (if (equal x "") "0" x))
  9754. l ",") "]"))
  9755. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  9756. (setq n1 (string-to-number (match-string 1 desc))
  9757. n2 (string-to-number (match-string 2 desc)))
  9758. (beginning-of-line 1)
  9759. (save-excursion
  9760. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  9761. (org-table-goto-column col)
  9762. (setq beg (point))))
  9763. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  9764. (org-table-goto-column col)
  9765. (setq end (point)))
  9766. (setq l (apply 'append (org-table-copy-region beg end)))
  9767. (concat "[" (mapconcat
  9768. (lambda (x) (setq x (org-trim x))
  9769. (if (equal x "") "0" x))
  9770. l ",") "]"))
  9771. ((string-match "\\([0-9]+\\)" desc)
  9772. (beginning-of-line 1)
  9773. (when (re-search-backward org-table-dataline-regexp tbeg t
  9774. (string-to-number (match-string 0 desc)))
  9775. (org-table-goto-column col)
  9776. (org-trim (org-table-get-field))))))))
  9777. (defvar org-table-formula-history nil)
  9778. (defvar org-table-column-names nil
  9779. "Alist with column names, derived from the `!' line.")
  9780. (defvar org-table-column-name-regexp nil
  9781. "Regular expression matching the current column names.")
  9782. (defvar org-table-local-parameters nil
  9783. "Alist with parameter names, derived from the `$' line.")
  9784. (defvar org-table-named-field-locations nil
  9785. "Alist with locations of named fields.")
  9786. (defun org-table-get-formula (&optional equation named)
  9787. "Read a formula from the minibuffer, offer stored formula as default."
  9788. (let* ((name (car (rassoc (list (org-current-line)
  9789. (org-table-current-column))
  9790. org-table-named-field-locations)))
  9791. (scol (if named
  9792. (if name name
  9793. (error "Not in a named field"))
  9794. (int-to-string (org-table-current-column))))
  9795. (dummy (and name (not named)
  9796. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  9797. (error "Abort")))
  9798. (org-table-may-need-update nil)
  9799. (stored-list (org-table-get-stored-formulas))
  9800. (stored (cdr (assoc scol stored-list)))
  9801. (eq (cond
  9802. ((and stored equation (string-match "^ *=? *$" equation))
  9803. stored)
  9804. ((stringp equation)
  9805. equation)
  9806. (t (read-string
  9807. (format "%s formula $%s=" (if named "Field" "Column") scol)
  9808. (or stored "") 'org-table-formula-history
  9809. ;stored
  9810. ))))
  9811. mustsave)
  9812. (when (not (string-match "\\S-" eq))
  9813. ;; remove formula
  9814. (setq stored-list (delq (assoc scol stored-list) stored-list))
  9815. (org-table-store-formulas stored-list)
  9816. (error "Formula removed"))
  9817. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  9818. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  9819. (if (and name (not named))
  9820. ;; We set the column equation, delete the named one.
  9821. (setq stored-list (delq (assoc name stored-list) stored-list)
  9822. mustsave t))
  9823. (if stored
  9824. (setcdr (assoc scol stored-list) eq)
  9825. (setq stored-list (cons (cons scol eq) stored-list)))
  9826. (if (or mustsave (not (equal stored eq)))
  9827. (org-table-store-formulas stored-list))
  9828. eq))
  9829. (defun org-table-store-formulas (alist)
  9830. "Store the list of formulas below the current table."
  9831. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  9832. (save-excursion
  9833. (goto-char (org-table-end))
  9834. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  9835. (delete-region (point) (match-end 0)))
  9836. (insert "#+TBLFM: "
  9837. (mapconcat (lambda (x)
  9838. (concat "$" (car x) "=" (cdr x)))
  9839. alist "::")
  9840. "\n")))
  9841. (defun org-table-get-stored-formulas ()
  9842. "Return an alist with the stored formulas directly after current table."
  9843. (interactive)
  9844. (let (scol eq eq-alist strings string seen)
  9845. (save-excursion
  9846. (goto-char (org-table-end))
  9847. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9848. (setq strings (org-split-string (match-string 2) " *:: *"))
  9849. (while (setq string (pop strings))
  9850. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  9851. (setq scol (match-string 1 string)
  9852. eq (match-string 2 string)
  9853. eq-alist (cons (cons scol eq) eq-alist))
  9854. (if (member scol seen)
  9855. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9856. (push scol seen))))))
  9857. (nreverse eq-alist)))
  9858. (defun org-table-modify-formulas (action &rest columns)
  9859. "Modify the formulas stored below the current table.
  9860. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  9861. expected, for the other actions only a single column number is needed."
  9862. (let ((list (org-table-get-stored-formulas))
  9863. (nmax (length (org-split-string
  9864. (buffer-substring (point-at-bol) (point-at-eol))
  9865. "|")))
  9866. col col1 col2 scol si sc1 sc2)
  9867. (cond
  9868. ((null list)) ; No action needed if there are no stored formulas
  9869. ((eq action 'remove)
  9870. (setq col (car columns)
  9871. scol (int-to-string col))
  9872. (org-table-replace-in-formulas list scol "INVALID")
  9873. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  9874. (loop for i from (1+ col) upto nmax by 1 do
  9875. (setq si (int-to-string i))
  9876. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  9877. (if (assoc si list) (setcar (assoc si list)
  9878. (int-to-string (1- i))))))
  9879. ((eq action 'insert)
  9880. (setq col (car columns))
  9881. (loop for i from nmax downto col by 1 do
  9882. (setq si (int-to-string i))
  9883. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  9884. (if (assoc si list) (setcar (assoc si list)
  9885. (int-to-string (1+ i))))))
  9886. ((eq action 'swap)
  9887. (setq col1 (car columns) col2 (nth 1 columns)
  9888. sc1 (int-to-string col1) sc2 (int-to-string col2))
  9889. ;; Hopefully, ZqZtZ will never be a name in a table
  9890. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  9891. (org-table-replace-in-formulas list sc2 sc1)
  9892. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  9893. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  9894. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  9895. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  9896. (t (error "Invalid action in `org-table-modify-formulas'")))
  9897. (if list (org-table-store-formulas list))))
  9898. (defun org-table-replace-in-formulas (list s1 s2)
  9899. (let (elt re s)
  9900. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  9901. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  9902. re (concat (regexp-quote s1) "\\>"))
  9903. (while (setq elt (pop list))
  9904. (setq s (cdr elt))
  9905. (while (string-match re s)
  9906. (setq s (replace-match s2 t t s)))
  9907. (setcdr elt s))))
  9908. (defun org-table-get-specials ()
  9909. "Get the column names and local parameters for this table."
  9910. (save-excursion
  9911. (let ((beg (org-table-begin)) (end (org-table-end))
  9912. names name fields fields1 field cnt c v line col)
  9913. (setq org-table-column-names nil
  9914. org-table-local-parameters nil
  9915. org-table-named-field-locations nil)
  9916. (goto-char beg)
  9917. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9918. (setq names (org-split-string (match-string 1) " *| *")
  9919. cnt 1)
  9920. (while (setq name (pop names))
  9921. (setq cnt (1+ cnt))
  9922. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9923. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9924. (setq org-table-column-names (nreverse org-table-column-names))
  9925. (setq org-table-column-name-regexp
  9926. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9927. (goto-char beg)
  9928. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9929. (setq fields (org-split-string (match-string 1) " *| *"))
  9930. (while (setq field (pop fields))
  9931. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9932. (push (cons (match-string 1 field) (match-string 2 field))
  9933. org-table-local-parameters))))
  9934. (goto-char beg)
  9935. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9936. (setq c (match-string 1)
  9937. fields (org-split-string (match-string 2) " *| *"))
  9938. (save-excursion
  9939. (beginning-of-line (if (equal c "_") 2 0))
  9940. (setq line (org-current-line) col 1)
  9941. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9942. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9943. (while (and fields1 (setq field (pop fields)))
  9944. (setq v (pop fields1) col (1+ col))
  9945. (when (and (stringp field) (stringp v)
  9946. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9947. (push (cons field v) org-table-local-parameters)
  9948. (push (list field line col) org-table-named-field-locations)))))))
  9949. (defun org-this-word ()
  9950. ;; Get the current word
  9951. (save-excursion
  9952. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  9953. (end (progn (skip-chars-forward "^ \t\n") (point))))
  9954. (buffer-substring-no-properties beg end))))
  9955. (defun org-table-maybe-eval-formula ()
  9956. "Check if the current field starts with \"=\" or \":=\".
  9957. If yes, store the formula and apply it."
  9958. ;; We already know we are in a table. Get field will only return a formula
  9959. ;; when appropriate. It might return a separator line, but no problem.
  9960. (when org-table-formula-evaluate-inline
  9961. (let* ((field (org-trim (or (org-table-get-field) "")))
  9962. named eq)
  9963. (when (string-match "^:?=\\(.*\\)" field)
  9964. (setq named (equal (string-to-char field) ?:)
  9965. eq (match-string 1 field))
  9966. (if (fboundp 'calc-eval)
  9967. (org-table-eval-formula (if named '(4) nil) eq))))))
  9968. (defvar org-recalc-commands nil
  9969. "List of commands triggering the recalculation of a line.
  9970. Will be filled automatically during use.")
  9971. (defvar org-recalc-marks
  9972. '((" " . "Unmarked: no special line, no automatic recalculation")
  9973. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9974. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9975. ("!" . "Column name definition line. Reference in formula as $name.")
  9976. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9977. ("_" . "Names for values in row below this one.")
  9978. ("^" . "Names for values in row above this one.")))
  9979. (defun org-table-rotate-recalc-marks (&optional newchar)
  9980. "Rotate the recalculation mark in the first column.
  9981. If in any row, the first field is not consistent with a mark,
  9982. insert a new column for the markers.
  9983. When there is an active region, change all the lines in the region,
  9984. after prompting for the marking character.
  9985. After each change, a message will be displayed indicating the meaning
  9986. of the new mark."
  9987. (interactive)
  9988. (unless (org-at-table-p) (error "Not at a table"))
  9989. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9990. (beg (org-table-begin))
  9991. (end (org-table-end))
  9992. (l (org-current-line))
  9993. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9994. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9995. (have-col
  9996. (save-excursion
  9997. (goto-char beg)
  9998. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9999. (col (org-table-current-column))
  10000. (forcenew (car (assoc newchar org-recalc-marks)))
  10001. epos new)
  10002. (when l1
  10003. (message "Change region to what mark? Type # * ! $ or SPC: ")
  10004. (setq newchar (char-to-string (read-char-exclusive))
  10005. forcenew (car (assoc newchar org-recalc-marks))))
  10006. (if (and newchar (not forcenew))
  10007. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  10008. newchar))
  10009. (if l1 (goto-line l1))
  10010. (save-excursion
  10011. (beginning-of-line 1)
  10012. (unless (looking-at org-table-dataline-regexp)
  10013. (error "Not at a table data line")))
  10014. (unless have-col
  10015. (org-table-goto-column 1)
  10016. (org-table-insert-column)
  10017. (org-table-goto-column (1+ col)))
  10018. (setq epos (point-at-eol))
  10019. (save-excursion
  10020. (beginning-of-line 1)
  10021. (org-table-get-field
  10022. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  10023. (concat " "
  10024. (setq new (or forcenew
  10025. (cadr (member (match-string 1) marks))))
  10026. " ")
  10027. " # ")))
  10028. (if (and l1 l2)
  10029. (progn
  10030. (goto-line l1)
  10031. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  10032. (and (looking-at org-table-dataline-regexp)
  10033. (org-table-get-field 1 (concat " " new " "))))
  10034. (goto-line l1)))
  10035. (if (not (= epos (point-at-eol))) (org-table-align))
  10036. (goto-line l)
  10037. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  10038. (defun org-table-maybe-recalculate-line ()
  10039. "Recompute the current line if marked for it, and if we haven't just done it."
  10040. (interactive)
  10041. (and org-table-allow-automatic-line-recalculation
  10042. (not (and (memq last-command org-recalc-commands)
  10043. (equal org-last-recalc-line (org-current-line))))
  10044. (save-excursion (beginning-of-line 1)
  10045. (looking-at org-table-auto-recalculate-regexp))
  10046. (fboundp 'calc-eval)
  10047. (org-table-recalculate) t))
  10048. (defvar org-table-formula-debug nil
  10049. "Non-nil means, debug table formulas.
  10050. When nil, simply write \"#ERROR\" in corrupted fields.")
  10051. (defvar modes)
  10052. (defsubst org-set-calc-mode (var &optional value)
  10053. (if (stringp var)
  10054. (setq var (assoc var '(("D" calc-angle-mode deg)
  10055. ("R" calc-angle-mode rad)
  10056. ("F" calc-prefer-frac t)
  10057. ("S" calc-symbolic-mode t)))
  10058. value (nth 2 var) var (nth 1 var)))
  10059. (if (memq var modes)
  10060. (setcar (cdr (memq var modes)) value)
  10061. (cons var (cons value modes)))
  10062. modes)
  10063. (defun org-table-eval-formula (&optional arg equation
  10064. suppress-align suppress-const
  10065. suppress-store)
  10066. "Replace the table field value at the cursor by the result of a calculation.
  10067. This function makes use of Dave Gillespie's Calc package, in my view the
  10068. most exciting program ever written for GNU Emacs. So you need to have Calc
  10069. installed in order to use this function.
  10070. In a table, this command replaces the value in the current field with the
  10071. result of a formula. It also installs the formula as the \"current\" column
  10072. formula, by storing it in a special line below the table. When called
  10073. with a `C-u' prefix, the current field must ba a named field, and the
  10074. formula is installed as valid in only this specific field.
  10075. When called, the command first prompts for a formula, which is read in
  10076. the minibuffer. Previously entered formulas are available through the
  10077. history list, and the last used formula is offered as a default.
  10078. These stored formulas are adapted correctly when moving, inserting, or
  10079. deleting columns with the corresponding commands.
  10080. The formula can be any algebraic expression understood by the Calc package.
  10081. For details, see the Org-mode manual.
  10082. This function can also be called from Lisp programs and offers
  10083. additional arguments: EQUATION can be the formula to apply. If this
  10084. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  10085. used to speed-up recursive calls by by-passing unnecessary aligns.
  10086. SUPPRESS-CONST suppresses the interpretation of constants in the
  10087. formula, assuming that this has been done already outside the function.
  10088. SUPPRESS-STORE means the formula should not be stored, either because
  10089. it is already stored, or because it is a modified equation that should
  10090. not overwrite the stored one."
  10091. (interactive "P")
  10092. (require 'calc)
  10093. (org-table-check-inside-data-field)
  10094. (org-table-get-specials)
  10095. (let* (fields
  10096. (ndown (if (integerp arg) arg 1))
  10097. (org-table-automatic-realign nil)
  10098. (case-fold-search nil)
  10099. (down (> ndown 1))
  10100. (formula (if (and equation suppress-store)
  10101. equation
  10102. (org-table-get-formula equation (equal arg '(4)))))
  10103. (n0 (org-table-current-column))
  10104. (modes (copy-sequence org-calc-default-modes))
  10105. n form fmt x ev orig c lispp)
  10106. ;; Parse the format string. Since we have a lot of modes, this is
  10107. ;; a lot of work. However, I think calc still uses most of the time.
  10108. (if (string-match ";" formula)
  10109. (let ((tmp (org-split-string formula ";")))
  10110. (setq formula (car tmp)
  10111. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  10112. (nth 1 tmp)))
  10113. (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt)
  10114. (setq c (string-to-char (match-string 1 fmt))
  10115. n (string-to-number (or (match-string 1 fmt) "")))
  10116. (if (= c ?p) (setq modes (org-set-calc-mode 'calc-internal-prec n))
  10117. (setq modes (org-set-calc-mode
  10118. 'calc-float-format
  10119. (list (cdr (assoc c '((?n . float) (?f . fix)
  10120. (?s . sci) (?e . eng))))
  10121. n))))
  10122. (setq fmt (replace-match "" t t fmt)))
  10123. (while (string-match "[DRFS]" fmt)
  10124. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  10125. (setq fmt (replace-match "" t t fmt)))
  10126. (unless (string-match "\\S-" fmt)
  10127. (setq fmt nil))))
  10128. (if (and (not suppress-const) org-table-formula-use-constants)
  10129. (setq formula (org-table-formula-substitute-names formula)))
  10130. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  10131. (while (> ndown 0)
  10132. (setq fields (org-split-string
  10133. (buffer-substring
  10134. (point-at-bol) (point-at-eol)) " *| *"))
  10135. (if org-table-formula-numbers-only
  10136. (setq fields (mapcar
  10137. (lambda (x) (number-to-string (string-to-number x)))
  10138. fields)))
  10139. (setq ndown (1- ndown))
  10140. (setq form (copy-sequence formula)
  10141. lispp (equal (substring form 0 2) "'("))
  10142. ;; Insert the references to fields in same row
  10143. (while (string-match "\\$\\([0-9]+\\)?" form)
  10144. (setq n (if (match-beginning 1)
  10145. (string-to-number (match-string 1 form))
  10146. n0)
  10147. x (nth (1- n) fields))
  10148. (unless x (error "Invalid field specifier \"%s\""
  10149. (match-string 0 form)))
  10150. (if (equal x "") (setq x "0"))
  10151. (setq form (replace-match
  10152. (if lispp x (concat "(" x ")"))
  10153. t t form)))
  10154. ;; Insert ranges in current column
  10155. (while (string-match "\\&[-I0-9]+" form)
  10156. (setq form (replace-match
  10157. (save-match-data
  10158. (org-table-get-vertical-vector (match-string 0 form)
  10159. nil n0))
  10160. t t form)))
  10161. ;; (setq ev (calc-eval (cons form modes)
  10162. ;; FIXME (if org-table-formula-numbers-only 'num)))
  10163. (if lispp
  10164. (setq ev (eval (eval (read form)))
  10165. ev (if (numberp ev) (number-to-string ev) ev))
  10166. (setq ev (calc-eval (cons form modes)
  10167. (if org-table-formula-numbers-only 'num))))
  10168. (when org-table-formula-debug
  10169. (with-output-to-temp-buffer "*Help*"
  10170. (princ (format "Substitution history of formula
  10171. Orig: %s
  10172. $xyz-> %s
  10173. $1-> %s\n" orig formula form))
  10174. (if (listp ev)
  10175. (princ (format " %s^\nError: %s"
  10176. (make-string (car ev) ?\-) (nth 1 ev)))
  10177. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  10178. ev (or fmt "NONE")
  10179. (if fmt (format fmt (string-to-number ev)) ev)))))
  10180. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  10181. (unless (and (interactive-p) (not ndown))
  10182. (unless (let (inhibit-redisplay)
  10183. (y-or-n-p "Debugging Formula. Continue to next? "))
  10184. (org-table-align)
  10185. (error "Abort"))
  10186. (delete-window (get-buffer-window "*Help*"))
  10187. (message "")))
  10188. (if (listp ev) (setq fmt nil ev "#ERROR"))
  10189. (org-table-justify-field-maybe
  10190. (if fmt (format fmt (string-to-number ev)) ev))
  10191. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  10192. (call-interactively 'org-return)
  10193. (setq ndown 0)))
  10194. (and down (org-table-maybe-recalculate-line))
  10195. (or suppress-align (and org-table-may-need-update
  10196. (org-table-align)))))
  10197. (defun org-table-recalculate (&optional all noalign)
  10198. "Recalculate the current table line by applying all stored formulas.
  10199. With prefix arg ALL, do this for all lines in the table."
  10200. (interactive "P")
  10201. (or (memq this-command org-recalc-commands)
  10202. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  10203. (unless (org-at-table-p) (error "Not at a table"))
  10204. (org-table-get-specials)
  10205. (let* ((eqlist (sort (org-table-get-stored-formulas)
  10206. (lambda (a b) (string< (car a) (car b)))))
  10207. (inhibit-redisplay t)
  10208. (line-re org-table-dataline-regexp)
  10209. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10210. (thiscol (org-table-current-column))
  10211. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  10212. ;; Insert constants in all formulas
  10213. (setq eqlist
  10214. (mapcar (lambda (x)
  10215. (setcdr x (org-table-formula-substitute-names (cdr x)))
  10216. x)
  10217. eqlist))
  10218. ;; Split the equation list
  10219. (while (setq eq (pop eqlist))
  10220. (if (<= (string-to-char (car eq)) ?9)
  10221. (push eq eqlnum)
  10222. (push eq eqlname)))
  10223. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  10224. (if all
  10225. (progn
  10226. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  10227. (goto-char (setq beg (org-table-begin)))
  10228. (if (re-search-forward org-table-calculate-mark-regexp end t)
  10229. ;; This is a table with marked lines, only compute selected lines
  10230. (setq line-re org-table-recalculate-regexp)
  10231. ;; Move forward to the first non-header line
  10232. (if (and (re-search-forward org-table-dataline-regexp end t)
  10233. (re-search-forward org-table-hline-regexp end t)
  10234. (re-search-forward org-table-dataline-regexp end t))
  10235. (setq beg (match-beginning 0))
  10236. nil))) ;; just leave beg where it is
  10237. (setq beg (point-at-bol)
  10238. end (move-marker (make-marker) (1+ (point-at-eol)))))
  10239. (goto-char beg)
  10240. (and all (message "Re-applying formulas to full table..."))
  10241. (while (re-search-forward line-re end t)
  10242. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  10243. ;; Unprotected line, recalculate
  10244. (and all (message "Re-applying formulas to full table...(line %d)"
  10245. (setq cnt (1+ cnt))))
  10246. (setq org-last-recalc-line (org-current-line))
  10247. (setq eql eqlnum)
  10248. (while (setq entry (pop eql))
  10249. (goto-line org-last-recalc-line)
  10250. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  10251. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  10252. (goto-line thisline)
  10253. (org-table-goto-column thiscol)
  10254. (or noalign (and org-table-may-need-update (org-table-align))
  10255. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  10256. ;; Now do the names fields
  10257. (while (setq eq (pop eqlname))
  10258. (setq name (car eq)
  10259. a (assoc name org-table-named-field-locations))
  10260. (when a
  10261. (message "Re-applying formula to named field: %s" name)
  10262. (goto-line (nth 1 a))
  10263. (org-table-goto-column (nth 2 a))
  10264. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  10265. ;; back to initial position
  10266. (goto-line thisline)
  10267. (org-table-goto-column thiscol)
  10268. (or noalign (and org-table-may-need-update (org-table-align))
  10269. (and all (message "Re-applying formulas...done")))))
  10270. (defun org-table-formula-substitute-names (f)
  10271. "Replace $const with values in string F."
  10272. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  10273. ;; First, check for column names
  10274. (while (setq start (string-match org-table-column-name-regexp f start))
  10275. (setq start (1+ start))
  10276. (setq a (assoc (match-string 1 f) org-table-column-names))
  10277. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  10278. ;; Expand ranges to vectors
  10279. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  10280. (setq n1 (string-to-number (match-string 1 f))
  10281. n2 (string-to-number (match-string 2 f))
  10282. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  10283. s (concat "[($" (number-to-string (1- nn1)) ")"))
  10284. (loop for i from nn1 upto nn2 do
  10285. (setq s (concat s ",($" (int-to-string i) ")")))
  10286. (setq s (concat s "]"))
  10287. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  10288. (setq f (replace-match s t t f)))
  10289. ;; Parameters and constants
  10290. (setq start 0)
  10291. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  10292. (setq start (1+ start))
  10293. (if (setq a (save-match-data
  10294. (org-table-get-constant (match-string 1 f))))
  10295. (setq f (replace-match (concat "(" a ")") t t f))))
  10296. (if org-table-formula-debug
  10297. (put-text-property 0 (length f) :orig-formula f1 f))
  10298. f))
  10299. (defun org-table-get-constant (const)
  10300. "Find the value for a parameter or constant in a formula.
  10301. Parameters get priority."
  10302. (or (cdr (assoc const org-table-local-parameters))
  10303. (cdr (assoc const org-table-formula-constants))
  10304. (and (fboundp 'constants-get) (constants-get const))
  10305. "#UNDEFINED_NAME"))
  10306. (defvar org-edit-formulas-map (make-sparse-keymap))
  10307. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  10308. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  10309. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  10310. (defvar org-pos)
  10311. (defun org-table-edit-formulas ()
  10312. "Edit the formulas of the current table in a separate buffer."
  10313. (interactive)
  10314. (unless (org-at-table-p)
  10315. (error "Not at a table"))
  10316. (org-table-get-specials)
  10317. (let ((eql (org-table-get-stored-formulas))
  10318. (pos (move-marker (make-marker) (point)))
  10319. (wc (current-window-configuration))
  10320. entry loc s)
  10321. (switch-to-buffer-other-window "*Edit Formulas*")
  10322. (erase-buffer)
  10323. (fundamental-mode)
  10324. (set (make-local-variable 'org-pos) pos)
  10325. (set (make-local-variable 'org-window-configuration) wc)
  10326. (use-local-map org-edit-formulas-map)
  10327. (setq s "# Edit formulas and finish with `C-c C-c'.
  10328. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  10329. # Use `C-c ?' to get information about $name at point.
  10330. # To cancel editing, press `C-c C-q'.\n")
  10331. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  10332. (insert s)
  10333. (while (setq entry (pop eql))
  10334. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  10335. (setq s (format "# Named formula, referring to column %d in line %d\n"
  10336. (nth 2 loc) (nth 1 loc)))
  10337. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  10338. (insert s))
  10339. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  10340. (remove-text-properties 0 (length s) '(face nil) s)
  10341. (insert s))
  10342. (goto-char (point-min))
  10343. (message "Edit formulas and finish with `C-c C-c'.")))
  10344. (defun org-show-variable ()
  10345. "Show the location/value of the $ expression at point."
  10346. (interactive)
  10347. (let (var (pos org-pos) (win (selected-window)) e)
  10348. (save-excursion
  10349. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  10350. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  10351. (setq var (match-string 1))
  10352. (error "No variable at point")))
  10353. (cond
  10354. ((setq e (assoc var org-table-named-field-locations))
  10355. (switch-to-buffer-other-window (marker-buffer pos))
  10356. (goto-line (nth 1 e))
  10357. (org-table-goto-column (nth 2 e))
  10358. (select-window win)
  10359. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10360. ((setq e (assoc var org-table-column-names))
  10361. (switch-to-buffer-other-window (marker-buffer pos))
  10362. (goto-char pos)
  10363. (goto-char (org-table-begin))
  10364. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10365. (org-table-end) t)
  10366. (progn
  10367. (goto-char (match-beginning 1))
  10368. (message "Named column (column %s)" (cdr e)))
  10369. (error "Column name not found"))
  10370. (select-window win))
  10371. ((string-match "^[0-9]$" var)
  10372. ;; column number
  10373. (switch-to-buffer-other-window (marker-buffer pos))
  10374. (goto-char pos)
  10375. (goto-char (org-table-begin))
  10376. (recenter 1)
  10377. (if (re-search-forward org-table-dataline-regexp
  10378. (org-table-end) t)
  10379. (progn
  10380. (goto-char (match-beginning 0))
  10381. (org-table-goto-column (string-to-number var))
  10382. (message "Column %s" var))
  10383. (error "Column name not found"))
  10384. (select-window win))
  10385. ((setq e (assoc var org-table-local-parameters))
  10386. (switch-to-buffer-other-window (marker-buffer pos))
  10387. (goto-char pos)
  10388. (goto-char (org-table-begin))
  10389. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10390. (progn
  10391. (goto-char (match-beginning 1))
  10392. (message "Local parameter."))
  10393. (error "Parameter not found"))
  10394. (select-window win))
  10395. (t
  10396. (cond
  10397. ((setq e (assoc var org-table-formula-constants))
  10398. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  10399. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10400. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  10401. (t (error "Undefined name $%s" var)))))))
  10402. (defun org-finish-edit-formulas (&optional arg)
  10403. "Parse the buffer for formula definitions and install them.
  10404. With prefix ARG, apply the new formulas to the table."
  10405. (interactive "P")
  10406. (let ((pos org-pos) eql)
  10407. (goto-char (point-min))
  10408. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  10409. (push (cons (match-string 1) (match-string 2)) eql))
  10410. (set-window-configuration org-window-configuration)
  10411. (select-window (get-buffer-window (marker-buffer pos)))
  10412. (goto-char pos)
  10413. (unless (org-at-table-p)
  10414. (error "Lost table position - cannot install formulae"))
  10415. (org-table-store-formulas eql)
  10416. (move-marker pos nil)
  10417. (kill-buffer "*Edit Formulas*")
  10418. (if arg
  10419. (org-table-recalculate 'all)
  10420. (message "New formulas installed - press C-u C-c C-c to apply."))))
  10421. (defun org-abort-edit-formulas ()
  10422. "Abort editing formulas, without installing the changes."
  10423. (interactive)
  10424. (let ((pos org-pos))
  10425. (set-window-configuration org-window-configuration)
  10426. (select-window (get-buffer-window (marker-buffer pos)))
  10427. (goto-char pos)
  10428. (message "Formula editing aborted without installing changes")))
  10429. ;;; The orgtbl minor mode
  10430. ;; Define a minor mode which can be used in other modes in order to
  10431. ;; integrate the org-mode table editor.
  10432. ;; This is really a hack, because the org-mode table editor uses several
  10433. ;; keys which normally belong to the major mode, for example the TAB and
  10434. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10435. ;; necessary to operate the table editor, but wraps the commands into a
  10436. ;; function which tests if the cursor is currently inside a table. If that
  10437. ;; is the case, the table editor command is executed. However, when any of
  10438. ;; those keys is used outside a table, the function uses `key-binding' to
  10439. ;; look up if the key has an associated command in another currently active
  10440. ;; keymap (minor modes, major mode, global), and executes that command.
  10441. ;; There might be problems if any of the keys used by the table editor is
  10442. ;; otherwise used as a prefix key.
  10443. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10444. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10445. ;; addresses this by checking explicitly for both bindings.
  10446. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10447. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10448. ;; Some modes bind other commands to simple characters, for example
  10449. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10450. ;; active, this binding is ignored inside tables and replaced with a
  10451. ;; modified self-insert.
  10452. (defvar orgtbl-mode nil
  10453. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10454. table editor in arbitrary modes.")
  10455. (make-variable-buffer-local 'orgtbl-mode)
  10456. (defvar orgtbl-mode-map (make-keymap)
  10457. "Keymap for `orgtbl-mode'.")
  10458. ;;;###autoload
  10459. (defun turn-on-orgtbl ()
  10460. "Unconditionally turn on `orgtbl-mode'."
  10461. (orgtbl-mode 1))
  10462. ;;;###autoload
  10463. (defun orgtbl-mode (&optional arg)
  10464. "The `org-mode' table editor as a minor mode for use in other modes."
  10465. (interactive)
  10466. (if (eq major-mode 'org-mode)
  10467. ;; Exit without error, in case some hook functions calls this
  10468. ;; by accident in org-mode.
  10469. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10470. (setq orgtbl-mode
  10471. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10472. (if orgtbl-mode
  10473. (progn
  10474. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10475. ;; Make sure we are first in minor-mode-map-alist
  10476. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10477. (and c (setq minor-mode-map-alist
  10478. (cons c (delq c minor-mode-map-alist)))))
  10479. (set (make-local-variable (quote org-table-may-need-update)) t)
  10480. (org-add-hook 'before-change-functions 'org-before-change-function
  10481. nil 'local)
  10482. (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
  10483. auto-fill-inhibit-regexp)
  10484. (set (make-local-variable 'auto-fill-inhibit-regexp)
  10485. (if auto-fill-inhibit-regexp
  10486. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  10487. "[ \t]*|"))
  10488. (org-add-to-invisibility-spec '(org-cwidth))
  10489. (easy-menu-add orgtbl-mode-menu)
  10490. (run-hooks 'orgtbl-mode-hook))
  10491. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10492. (org-cleanup-narrow-column-properties)
  10493. (org-remove-from-invisibility-spec '(org-cwidth))
  10494. (remove-hook 'before-change-functions 'org-before-change-function t)
  10495. (easy-menu-remove orgtbl-mode-menu)
  10496. (force-mode-line-update 'all))))
  10497. (defun org-cleanup-narrow-column-properties ()
  10498. "Remove all properties related to narrow-column invisibility."
  10499. (let ((s 1))
  10500. (while (setq s (text-property-any s (point-max)
  10501. 'display org-narrow-column-arrow))
  10502. (remove-text-properties s (1+ s) '(display t)))
  10503. (setq s 1)
  10504. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10505. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10506. (setq s 1)
  10507. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10508. (remove-text-properties s (1+ s) '(invisible t)))))
  10509. ;; Install it as a minor mode.
  10510. (put 'orgtbl-mode :included t)
  10511. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10512. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10513. (defun orgtbl-make-binding (fun n &rest keys)
  10514. "Create a function for binding in the table minor mode.
  10515. FUN is the command to call inside a table. N is used to create a unique
  10516. command name. KEYS are keys that should be checked in for a command
  10517. to execute outside of tables."
  10518. (eval
  10519. (list 'defun
  10520. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10521. '(arg)
  10522. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10523. "Outside of tables, run the binding of `"
  10524. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10525. "'.")
  10526. '(interactive "p")
  10527. (list 'if
  10528. '(org-at-table-p)
  10529. (list 'call-interactively (list 'quote fun))
  10530. (list 'let '(orgtbl-mode)
  10531. (list 'call-interactively
  10532. (append '(or)
  10533. (mapcar (lambda (k)
  10534. (list 'key-binding k))
  10535. keys)
  10536. '('orgtbl-error))))))))
  10537. (defun orgtbl-error ()
  10538. "Error when there is no default binding for a table key."
  10539. (interactive)
  10540. (error "This key is has no function outside tables"))
  10541. (defun orgtbl-setup ()
  10542. "Setup orgtbl keymaps."
  10543. (let ((nfunc 0)
  10544. (bindings
  10545. (list
  10546. '([(meta shift left)] org-table-delete-column)
  10547. '([(meta left)] org-table-move-column-left)
  10548. '([(meta right)] org-table-move-column-right)
  10549. '([(meta shift right)] org-table-insert-column)
  10550. '([(meta shift up)] org-table-kill-row)
  10551. '([(meta shift down)] org-table-insert-row)
  10552. '([(meta up)] org-table-move-row-up)
  10553. '([(meta down)] org-table-move-row-down)
  10554. '("\C-c\C-w" org-table-cut-region)
  10555. '("\C-c\M-w" org-table-copy-region)
  10556. '("\C-c\C-y" org-table-paste-rectangle)
  10557. '("\C-c-" org-table-insert-hline)
  10558. ; '([(shift tab)] org-table-previous-field)
  10559. '("\C-m" org-table-next-row)
  10560. (list (org-key 'S-return) 'org-table-copy-down)
  10561. '([(meta return)] org-table-wrap-region)
  10562. '("\C-c\C-q" org-table-wrap-region)
  10563. '("\C-c?" org-table-current-column)
  10564. '("\C-c " org-table-blank-field)
  10565. '("\C-c+" org-table-sum)
  10566. '("\C-c=" org-table-eval-formula)
  10567. '("\C-c'" org-table-edit-formulas)
  10568. '("\C-c`" org-table-edit-field)
  10569. '("\C-c*" org-table-recalculate)
  10570. '("\C-c|" org-table-create-or-convert-from-region)
  10571. '("\C-c^" org-table-sort-lines)
  10572. '([(control ?#)] org-table-rotate-recalc-marks)))
  10573. elt key fun cmd)
  10574. (while (setq elt (pop bindings))
  10575. (setq nfunc (1+ nfunc))
  10576. (setq key (car elt)
  10577. fun (nth 1 elt)
  10578. cmd (orgtbl-make-binding fun nfunc key))
  10579. (define-key orgtbl-mode-map key cmd))
  10580. ;; Special treatment needed for TAB and RET
  10581. (define-key orgtbl-mode-map [(return)]
  10582. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10583. (define-key orgtbl-mode-map "\C-m"
  10584. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10585. (define-key orgtbl-mode-map [(tab)]
  10586. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10587. (define-key orgtbl-mode-map "\C-i"
  10588. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  10589. (define-key orgtbl-mode-map "\C-i"
  10590. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  10591. (define-key orgtbl-mode-map "\C-c\C-c"
  10592. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  10593. (when orgtbl-optimized
  10594. ;; If the user wants maximum table support, we need to hijack
  10595. ;; some standard editing functions
  10596. (org-remap orgtbl-mode-map
  10597. 'self-insert-command 'orgtbl-self-insert-command
  10598. 'delete-char 'org-delete-char
  10599. 'delete-backward-char 'org-delete-backward-char)
  10600. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  10601. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10602. '("OrgTbl"
  10603. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10604. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10605. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10606. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10607. "--"
  10608. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10609. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10610. ["Copy Field from Above"
  10611. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10612. "--"
  10613. ("Column"
  10614. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10615. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10616. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10617. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  10618. "--"
  10619. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  10620. ("Row"
  10621. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10622. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10623. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10624. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10625. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10626. "--"
  10627. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10628. ("Rectangle"
  10629. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10630. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10631. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10632. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10633. "--"
  10634. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10635. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10636. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10637. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10638. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10639. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10640. ["Sum Column/Rectangle" org-table-sum
  10641. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10642. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10643. ["Debug Formulas"
  10644. (setq org-table-formula-debug (not org-table-formula-debug))
  10645. :style toggle :selected org-table-formula-debug]
  10646. ))
  10647. t)
  10648. (defun orgtbl-tab (arg)
  10649. "Justification and field motion for `orgtbl-mode'."
  10650. (interactive "P")
  10651. (if arg (org-table-edit-field t)
  10652. (org-table-justify-field-maybe)
  10653. (org-table-next-field)))
  10654. (defun orgtbl-ret ()
  10655. "Justification and field motion for `orgtbl-mode'."
  10656. (interactive)
  10657. (org-table-justify-field-maybe)
  10658. (org-table-next-row))
  10659. (defun orgtbl-self-insert-command (N)
  10660. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10661. If the cursor is in a table looking at whitespace, the whitespace is
  10662. overwritten, and the table is not marked as requiring realignment."
  10663. (interactive "p")
  10664. (if (and (org-at-table-p)
  10665. (or
  10666. (and org-table-auto-blank-field
  10667. (member last-command
  10668. '(orgtbl-hijacker-command-100
  10669. orgtbl-hijacker-command-101
  10670. orgtbl-hijacker-command-102
  10671. orgtbl-hijacker-command-103
  10672. orgtbl-hijacker-command-104
  10673. orgtbl-hijacker-command-105))
  10674. (org-table-blank-field))
  10675. t)
  10676. (eq N 1)
  10677. (looking-at "[^|\n]* +|"))
  10678. (let (org-table-may-need-update)
  10679. (goto-char (1- (match-end 0)))
  10680. (delete-backward-char 1)
  10681. (goto-char (match-beginning 0))
  10682. (self-insert-command N))
  10683. (setq org-table-may-need-update t)
  10684. (let (orgtbl-mode)
  10685. (call-interactively (key-binding (vector last-input-event))))))
  10686. (defun org-force-self-insert (N)
  10687. "Needed to enforce self-insert under remapping."
  10688. (interactive "p")
  10689. (self-insert-command N))
  10690. ;;; Exporting
  10691. (defconst org-level-max 20)
  10692. (defvar org-export-html-preamble nil
  10693. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  10694. (defvar org-export-html-postamble nil
  10695. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  10696. (defvar org-export-html-auto-preamble t
  10697. "Should default preamble be inserted? Set by publishing functions.")
  10698. (defvar org-export-html-auto-postamble t
  10699. "Should default postamble be inserted? Set by publishing functions.")
  10700. (defconst org-export-plist-vars
  10701. '((:language . org-export-default-language)
  10702. (:headline-levels . org-export-headline-levels)
  10703. (:section-numbers . org-export-with-section-numbers)
  10704. (:table-of-contents . org-export-with-toc)
  10705. (:emphasize . org-export-with-emphasize)
  10706. (:sub-superscript . org-export-with-sub-superscripts)
  10707. (:TeX-macros . org-export-with-TeX-macros)
  10708. (:fixed-width . org-export-with-fixed-width)
  10709. (:timestamps . org-export-with-timestamps)
  10710. (:tables . org-export-with-tables)
  10711. (:table-auto-headline . org-export-highlight-first-table-line)
  10712. (:style . org-export-html-style)
  10713. (:convert-org-links . org-export-html-link-org-files-as-html)
  10714. (:inline-images . org-export-html-inline-images)
  10715. (:expand-quoted-html . org-export-html-expand)
  10716. (:timestamp . org-export-html-with-timestamp)
  10717. (:publishing-directory . org-export-publishing-directory)
  10718. (:preamble . org-export-html-preamble)
  10719. (:postamble . org-export-html-postamble)
  10720. (:auto-preamble . org-export-html-auto-preamble)
  10721. (:auto-postamble . org-export-html-auto-postamble)
  10722. (:author . user-full-name)
  10723. (:email . user-mail-address)))
  10724. (defun org-default-export-plist ()
  10725. "Return the property list with default settings for the export variables."
  10726. (let ((l org-export-plist-vars) rtn e)
  10727. (while (setq e (pop l))
  10728. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  10729. rtn))
  10730. (defun org-infile-export-plist ()
  10731. "Return the property list with file-local settings for export."
  10732. (save-excursion
  10733. (goto-char 0)
  10734. (let ((re (org-make-options-regexp
  10735. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  10736. (text nil)
  10737. p key val text options)
  10738. (while (re-search-forward re nil t)
  10739. (setq key (org-match-string-no-properties 1)
  10740. val (org-match-string-no-properties 2))
  10741. (cond
  10742. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  10743. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  10744. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  10745. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  10746. ((string-equal key "TEXT")
  10747. (setq text (if text (concat text "\n" val) val)))
  10748. ((string-equal key "OPTIONS") (setq options val))))
  10749. (setq p (plist-put p :text text))
  10750. (when options
  10751. (let ((op '(("H" . :headline-levels)
  10752. ("num" . :section-numbers)
  10753. ("toc" . :table-of-contents)
  10754. ("\\n" . :preserve-breaks)
  10755. ("@" . :expand-quoted-html)
  10756. (":" . :fixed-width)
  10757. ("|" . :tables)
  10758. ("^" . :sub-superscript)
  10759. ("*" . :emphasize)
  10760. ("TeX" . :TeX-macros)))
  10761. o)
  10762. (while (setq o (pop op))
  10763. (if (string-match (concat (regexp-quote (car o))
  10764. ":\\([^ \t\n\r;,.]*\\)")
  10765. options)
  10766. (setq p (plist-put p (cdr o)
  10767. (car (read-from-string
  10768. (match-string 1 options)))))))))
  10769. p)))
  10770. (defun org-combine-plists (&rest plists)
  10771. "Create a single property list from all plists in PLISTS.
  10772. The process starts by copying the last list, and then setting properties
  10773. from the other lists. Settings in the first list are the most significant
  10774. ones and overrule settings in the other lists."
  10775. (let ((rtn (copy-sequence (pop plists)))
  10776. p v ls)
  10777. (while plists
  10778. (setq ls (pop plists))
  10779. (while ls
  10780. (setq p (pop ls) v (pop ls))
  10781. (setq rtn (plist-put rtn p v))))
  10782. rtn))
  10783. (defun org-export-directory (type plist)
  10784. (let* ((val (plist-get plist :publishing-directory))
  10785. (dir (if (listp val)
  10786. (or (cdr (assoc type val)) ".")
  10787. val)))
  10788. dir))
  10789. (defun org-export-find-first-heading-line (list)
  10790. "Remove all lines from LIST which are before the first headline."
  10791. (let ((orig-list list)
  10792. (re (concat "^" outline-regexp)))
  10793. (while (and list
  10794. (not (string-match re (car list))))
  10795. (pop list))
  10796. (or list orig-list)))
  10797. (defun org-skip-comments (lines)
  10798. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  10799. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  10800. (re2 "^\\(\\*+\\)[ \t\n\r]")
  10801. rtn line level)
  10802. (while (setq line (pop lines))
  10803. (cond
  10804. ((and (string-match re1 line)
  10805. (setq level (- (match-end 1) (match-beginning 1))))
  10806. ;; Beginning of a COMMENT subtree. Skip it.
  10807. (while (and (setq line (pop lines))
  10808. (or (not (string-match re2 line))
  10809. (> (- (match-end 1) (match-beginning 1)) level))))
  10810. (setq lines (cons line lines)))
  10811. ((string-match "^#" line)
  10812. ;; an ordinary comment line
  10813. )
  10814. ((and org-export-table-remove-special-lines
  10815. (string-match "^[ \t]*| *[!_^] *|" line))
  10816. ;; a special table line that should be removed
  10817. )
  10818. (t (setq rtn (cons line rtn)))))
  10819. (nreverse rtn)))
  10820. ;; ASCII
  10821. (defconst org-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  10822. "Characters for underlining headings in ASCII export.")
  10823. (defconst org-html-entities
  10824. '(("nbsp")
  10825. ("iexcl")
  10826. ("cent")
  10827. ("pound")
  10828. ("curren")
  10829. ("yen")
  10830. ("brvbar")
  10831. ("sect")
  10832. ("uml")
  10833. ("copy")
  10834. ("ordf")
  10835. ("laquo")
  10836. ("not")
  10837. ("shy")
  10838. ("reg")
  10839. ("macr")
  10840. ("deg")
  10841. ("plusmn")
  10842. ("sup2")
  10843. ("sup3")
  10844. ("acute")
  10845. ("micro")
  10846. ("para")
  10847. ("middot")
  10848. ("odot"."o")
  10849. ("star"."*")
  10850. ("cedil")
  10851. ("sup1")
  10852. ("ordm")
  10853. ("raquo")
  10854. ("frac14")
  10855. ("frac12")
  10856. ("frac34")
  10857. ("iquest")
  10858. ("Agrave")
  10859. ("Aacute")
  10860. ("Acirc")
  10861. ("Atilde")
  10862. ("Auml")
  10863. ("Aring") ("AA"."&Aring;")
  10864. ("AElig")
  10865. ("Ccedil")
  10866. ("Egrave")
  10867. ("Eacute")
  10868. ("Ecirc")
  10869. ("Euml")
  10870. ("Igrave")
  10871. ("Iacute")
  10872. ("Icirc")
  10873. ("Iuml")
  10874. ("ETH")
  10875. ("Ntilde")
  10876. ("Ograve")
  10877. ("Oacute")
  10878. ("Ocirc")
  10879. ("Otilde")
  10880. ("Ouml")
  10881. ("times")
  10882. ("Oslash")
  10883. ("Ugrave")
  10884. ("Uacute")
  10885. ("Ucirc")
  10886. ("Uuml")
  10887. ("Yacute")
  10888. ("THORN")
  10889. ("szlig")
  10890. ("agrave")
  10891. ("aacute")
  10892. ("acirc")
  10893. ("atilde")
  10894. ("auml")
  10895. ("aring")
  10896. ("aelig")
  10897. ("ccedil")
  10898. ("egrave")
  10899. ("eacute")
  10900. ("ecirc")
  10901. ("euml")
  10902. ("igrave")
  10903. ("iacute")
  10904. ("icirc")
  10905. ("iuml")
  10906. ("eth")
  10907. ("ntilde")
  10908. ("ograve")
  10909. ("oacute")
  10910. ("ocirc")
  10911. ("otilde")
  10912. ("ouml")
  10913. ("divide")
  10914. ("oslash")
  10915. ("ugrave")
  10916. ("uacute")
  10917. ("ucirc")
  10918. ("uuml")
  10919. ("yacute")
  10920. ("thorn")
  10921. ("yuml")
  10922. ("fnof")
  10923. ("Alpha")
  10924. ("Beta")
  10925. ("Gamma")
  10926. ("Delta")
  10927. ("Epsilon")
  10928. ("Zeta")
  10929. ("Eta")
  10930. ("Theta")
  10931. ("Iota")
  10932. ("Kappa")
  10933. ("Lambda")
  10934. ("Mu")
  10935. ("Nu")
  10936. ("Xi")
  10937. ("Omicron")
  10938. ("Pi")
  10939. ("Rho")
  10940. ("Sigma")
  10941. ("Tau")
  10942. ("Upsilon")
  10943. ("Phi")
  10944. ("Chi")
  10945. ("Psi")
  10946. ("Omega")
  10947. ("alpha")
  10948. ("beta")
  10949. ("gamma")
  10950. ("delta")
  10951. ("epsilon")
  10952. ("varepsilon"."&epsilon;")
  10953. ("zeta")
  10954. ("eta")
  10955. ("theta")
  10956. ("iota")
  10957. ("kappa")
  10958. ("lambda")
  10959. ("mu")
  10960. ("nu")
  10961. ("xi")
  10962. ("omicron")
  10963. ("pi")
  10964. ("rho")
  10965. ("sigmaf") ("varsigma"."&sigmaf;")
  10966. ("sigma")
  10967. ("tau")
  10968. ("upsilon")
  10969. ("phi")
  10970. ("chi")
  10971. ("psi")
  10972. ("omega")
  10973. ("thetasym") ("vartheta"."&thetasym;")
  10974. ("upsih")
  10975. ("piv")
  10976. ("bull") ("bullet"."&bull;")
  10977. ("hellip") ("dots"."&hellip;")
  10978. ("prime")
  10979. ("Prime")
  10980. ("oline")
  10981. ("frasl")
  10982. ("weierp")
  10983. ("image")
  10984. ("real")
  10985. ("trade")
  10986. ("alefsym")
  10987. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  10988. ("uarr") ("uparrow"."&uarr;")
  10989. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  10990. ("darr")("downarrow"."&darr;")
  10991. ("harr") ("leftrightarrow"."&harr;")
  10992. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  10993. ("lArr") ("Leftarrow"."&lArr;")
  10994. ("uArr") ("Uparrow"."&uArr;")
  10995. ("rArr") ("Rightarrow"."&rArr;")
  10996. ("dArr") ("Downarrow"."&dArr;")
  10997. ("hArr") ("Leftrightarrow"."&hArr;")
  10998. ("forall")
  10999. ("part") ("partial"."&part;")
  11000. ("exist") ("exists"."&exist;")
  11001. ("empty") ("emptyset"."&empty;")
  11002. ("nabla")
  11003. ("isin") ("in"."&isin;")
  11004. ("notin")
  11005. ("ni")
  11006. ("prod")
  11007. ("sum")
  11008. ("minus")
  11009. ("lowast") ("ast"."&lowast;")
  11010. ("radic")
  11011. ("prop") ("proptp"."&prop;")
  11012. ("infin") ("infty"."&infin;")
  11013. ("ang") ("angle"."&ang;")
  11014. ("and") ("vee"."&and;")
  11015. ("or") ("wedge"."&or;")
  11016. ("cap")
  11017. ("cup")
  11018. ("int")
  11019. ("there4")
  11020. ("sim")
  11021. ("cong") ("simeq"."&cong;")
  11022. ("asymp")("approx"."&asymp;")
  11023. ("ne") ("neq"."&ne;")
  11024. ("equiv")
  11025. ("le")
  11026. ("ge")
  11027. ("sub") ("subset"."&sub;")
  11028. ("sup") ("supset"."&sup;")
  11029. ("nsub")
  11030. ("sube")
  11031. ("supe")
  11032. ("oplus")
  11033. ("otimes")
  11034. ("perp")
  11035. ("sdot") ("cdot"."&sdot;")
  11036. ("lceil")
  11037. ("rceil")
  11038. ("lfloor")
  11039. ("rfloor")
  11040. ("lang")
  11041. ("rang")
  11042. ("loz") ("Diamond"."&loz;")
  11043. ("spades") ("spadesuit"."&spades;")
  11044. ("clubs") ("clubsuit"."&clubs;")
  11045. ("hearts") ("diamondsuit"."&hearts;")
  11046. ("diams") ("diamondsuit"."&diams;")
  11047. ("quot")
  11048. ("amp")
  11049. ("lt")
  11050. ("gt")
  11051. ("OElig")
  11052. ("oelig")
  11053. ("Scaron")
  11054. ("scaron")
  11055. ("Yuml")
  11056. ("circ")
  11057. ("tilde")
  11058. ("ensp")
  11059. ("emsp")
  11060. ("thinsp")
  11061. ("zwnj")
  11062. ("zwj")
  11063. ("lrm")
  11064. ("rlm")
  11065. ("ndash")
  11066. ("mdash")
  11067. ("lsquo")
  11068. ("rsquo")
  11069. ("sbquo")
  11070. ("ldquo")
  11071. ("rdquo")
  11072. ("bdquo")
  11073. ("dagger")
  11074. ("Dagger")
  11075. ("permil")
  11076. ("lsaquo")
  11077. ("rsaquo")
  11078. ("euro")
  11079. ("arccos"."arccos")
  11080. ("arcsin"."arcsin")
  11081. ("arctan"."arctan")
  11082. ("arg"."arg")
  11083. ("cos"."cos")
  11084. ("cosh"."cosh")
  11085. ("cot"."cot")
  11086. ("coth"."coth")
  11087. ("csc"."csc")
  11088. ("deg"."deg")
  11089. ("det"."det")
  11090. ("dim"."dim")
  11091. ("exp"."exp")
  11092. ("gcd"."gcd")
  11093. ("hom"."hom")
  11094. ("inf"."inf")
  11095. ("ker"."ker")
  11096. ("lg"."lg")
  11097. ("lim"."lim")
  11098. ("liminf"."liminf")
  11099. ("limsup"."limsup")
  11100. ("ln"."ln")
  11101. ("log"."log")
  11102. ("max"."max")
  11103. ("min"."min")
  11104. ("Pr"."Pr")
  11105. ("sec"."sec")
  11106. ("sin"."sin")
  11107. ("sinh"."sinh")
  11108. ("sup"."sup")
  11109. ("tan"."tan")
  11110. ("tanh"."tanh")
  11111. )
  11112. "Entities for TeX->HTML translation.
  11113. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  11114. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  11115. In that case, \"\\ent\" will be translated to \"&other;\".
  11116. The list contains HTML entities for Latin-1, Greek and other symbols.
  11117. It is supplemented by a number of commonly used TeX macros with appropriate
  11118. translations. There is currently no way for users to extend this.")
  11119. (defun org-cleaned-string-for-export (string)
  11120. "Cleanup a buffer substring so that links can be created safely."
  11121. (interactive)
  11122. (let* ((cb (current-buffer))
  11123. (re-radio (and org-target-link-regexp
  11124. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  11125. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  11126. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  11127. rtn)
  11128. (save-excursion
  11129. (set-buffer (get-buffer-create " org-mode-tmp"))
  11130. (erase-buffer)
  11131. (insert string)
  11132. (org-mode)
  11133. ;; Find targets in comments and move them out of comments,
  11134. ;; but mark them as targets that should be invisible
  11135. (goto-char (point-min))
  11136. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  11137. (replace-match "\\1(INVISIBLE)"))
  11138. ;; Find matches for radio targets and turn them into internal links
  11139. (goto-char (point-min))
  11140. (when re-radio
  11141. (while (re-search-forward re-radio nil t)
  11142. (replace-match "\\1[[\\2]]")))
  11143. ;; Find all links that contain a newline and put them into a single line
  11144. (goto-char (point-min))
  11145. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  11146. (replace-match "\\1 \\3")
  11147. (goto-char (match-beginning 0)))
  11148. ;; Normalize links: Convert angle and plain links into bracket links
  11149. (goto-char (point-min))
  11150. (while (re-search-forward re-plain-link nil t)
  11151. (replace-match
  11152. (concat
  11153. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  11154. t t))
  11155. (goto-char (point-min))
  11156. (while (re-search-forward re-angle-link nil t)
  11157. (replace-match
  11158. (concat
  11159. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  11160. t t))
  11161. ;; Remove comments
  11162. (goto-char (point-min))
  11163. (while (re-search-forward "^#.*\n?" nil t)
  11164. (replace-match ""))
  11165. (setq rtn (buffer-string)))
  11166. (kill-buffer " org-mode-tmp")
  11167. rtn))
  11168. (defun org-solidify-link-text (s &optional alist)
  11169. "Take link text and make a safe target out of it."
  11170. (save-match-data
  11171. (let* ((rtn
  11172. (mapconcat
  11173. 'identity
  11174. (org-split-string s "[ \t\r\n]+") "--"))
  11175. (a (assoc rtn alist)))
  11176. (or (cdr a) rtn))))
  11177. (defun org-convert-to-odd-levels ()
  11178. "Convert an org-mode file with all levels allowed to one with odd levels.
  11179. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  11180. level 5 etc."
  11181. (interactive)
  11182. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  11183. (let ((org-odd-levels-only nil) n)
  11184. (save-excursion
  11185. (goto-char (point-min))
  11186. (while (re-search-forward "^\\*\\*+" nil t)
  11187. (setq n (1- (length (match-string 0))))
  11188. (while (>= (setq n (1- n)) 0)
  11189. (org-demote))
  11190. (end-of-line 1))))))
  11191. (defun org-convert-to-oddeven-levels ()
  11192. "Convert an org-mode file with only odd levels to one with odd and even levels.
  11193. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  11194. section with an even level, conversion would destroy the structure of the file. An error
  11195. is signaled in this case."
  11196. (interactive)
  11197. (goto-char (point-min))
  11198. ;; First check if there are no even levels
  11199. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  11200. (org-show-hierarchy-above)
  11201. (error "Not all levels are odd in this file. Conversion not possible."))
  11202. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  11203. (let ((org-odd-levels-only nil) n)
  11204. (save-excursion
  11205. (goto-char (point-min))
  11206. (while (re-search-forward "^\\*\\*+" nil t)
  11207. (setq n (/ (length (match-string 0)) 2))
  11208. (while (>= (setq n (1- n)) 0)
  11209. (org-promote))
  11210. (end-of-line 1))))))
  11211. (defun org-tr-level (n)
  11212. "Make N odd if required."
  11213. (if org-odd-levels-only (1+ (/ n 2)) n))
  11214. (defvar org-last-level nil) ; dynamically scoped variable
  11215. (defun org-export-as-ascii (arg)
  11216. "Export the outline as a pretty ASCII file.
  11217. If there is an active region, export only the region.
  11218. The prefix ARG specifies how many levels of the outline should become
  11219. underlined headlines. The default is 3."
  11220. (interactive "P")
  11221. (setq-default org-todo-line-regexp org-todo-line-regexp)
  11222. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  11223. (org-infile-export-plist)))
  11224. (region
  11225. (buffer-substring
  11226. (if (org-region-active-p) (region-beginning) (point-min))
  11227. (if (org-region-active-p) (region-end) (point-max))))
  11228. (lines (org-export-find-first-heading-line
  11229. (org-skip-comments
  11230. (org-split-string
  11231. (org-cleaned-string-for-export region)
  11232. "[\r\n]"))))
  11233. (org-startup-with-deadline-check nil)
  11234. (level 0) line txt
  11235. (umax nil)
  11236. (case-fold-search nil)
  11237. (filename (concat (file-name-as-directory
  11238. (org-export-directory :ascii opt-plist))
  11239. (file-name-sans-extension
  11240. (file-name-nondirectory buffer-file-name))
  11241. ".txt"))
  11242. (buffer (find-file-noselect filename))
  11243. (levels-open (make-vector org-level-max nil))
  11244. (odd org-odd-levels-only)
  11245. (date (format-time-string "%Y/%m/%d" (current-time)))
  11246. (time (format-time-string "%X" (org-current-time)))
  11247. (author (plist-get opt-plist :author))
  11248. (title (or (plist-get opt-plist :title)
  11249. (file-name-sans-extension
  11250. (file-name-nondirectory buffer-file-name))))
  11251. (options nil)
  11252. (email (plist-get opt-plist :email))
  11253. (language (plist-get opt-plist :language))
  11254. (text nil)
  11255. (todo nil)
  11256. (lang-words nil))
  11257. (setq org-last-level 1)
  11258. (org-init-section-numbers)
  11259. (find-file-noselect filename)
  11260. (setq lang-words (or (assoc language org-export-language-setup)
  11261. (assoc "en" org-export-language-setup)))
  11262. (if org-export-ascii-show-new-buffer
  11263. (switch-to-buffer-other-window buffer)
  11264. (set-buffer buffer))
  11265. (erase-buffer)
  11266. (fundamental-mode)
  11267. ;; create local variables for all options, to make sure all called
  11268. ;; functions get the correct information
  11269. (mapcar (lambda (x)
  11270. (set (make-local-variable (cdr x))
  11271. (plist-get opt-plist (car x))))
  11272. org-export-plist-vars)
  11273. (set (make-local-variable 'org-odd-levels-only) odd)
  11274. (setq umax (if arg (prefix-numeric-value arg)
  11275. org-export-headline-levels))
  11276. ;; File header
  11277. (if title (org-insert-centered title ?=))
  11278. (insert "\n")
  11279. (if (or author email)
  11280. (insert (concat (nth 1 lang-words) ": " (or author "")
  11281. (if email (concat " <" email ">") "")
  11282. "\n")))
  11283. (if (and date time)
  11284. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  11285. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  11286. (insert "\n\n")
  11287. (if org-export-with-toc
  11288. (progn
  11289. (insert (nth 3 lang-words) "\n"
  11290. (make-string (length (nth 3 lang-words)) ?=) "\n")
  11291. (mapcar '(lambda (line)
  11292. (if (string-match org-todo-line-regexp
  11293. line)
  11294. ;; This is a headline
  11295. (progn
  11296. (setq level (- (match-end 1) (match-beginning 1))
  11297. level (org-tr-level level)
  11298. txt (match-string 3 line)
  11299. todo
  11300. (or (and org-export-mark-todo-in-toc
  11301. (match-beginning 2)
  11302. (not (equal (match-string 2 line)
  11303. org-done-string)))
  11304. ; TODO, not DONE
  11305. (and org-export-mark-todo-in-toc
  11306. (= level umax)
  11307. (org-search-todo-below
  11308. line lines level))))
  11309. (setq txt (org-html-expand-for-ascii txt))
  11310. (if org-export-with-section-numbers
  11311. (setq txt (concat (org-section-number level)
  11312. " " txt)))
  11313. (if (<= level umax)
  11314. (progn
  11315. (insert
  11316. (make-string (* (1- level) 4) ?\ )
  11317. (format (if todo "%s (*)\n" "%s\n") txt))
  11318. (setq org-last-level level))
  11319. ))))
  11320. lines)))
  11321. (org-init-section-numbers)
  11322. (while (setq line (pop lines))
  11323. ;; Remove the quoted HTML tags.
  11324. (setq line (org-html-expand-for-ascii line))
  11325. ;; Remove targets
  11326. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  11327. (setq line (replace-match "" t t line)))
  11328. ;; Replace internal links
  11329. (while (string-match org-bracket-link-regexp line)
  11330. (setq line (replace-match
  11331. (if (match-end 3) "[\\3]" "[\\1]")
  11332. t nil line)))
  11333. (cond
  11334. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  11335. ;; a Headline
  11336. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  11337. txt (match-string 2 line))
  11338. (org-ascii-level-start level txt umax))
  11339. (t (insert line "\n"))))
  11340. (normal-mode)
  11341. (save-buffer)
  11342. ;; remove display and invisible chars
  11343. (let (beg end s)
  11344. (goto-char (point-min))
  11345. (while (setq beg (next-single-property-change (point) 'display))
  11346. (setq end (next-single-property-change beg 'display))
  11347. (delete-region beg end)
  11348. (goto-char beg)
  11349. (insert "=>"))
  11350. (goto-char (point-min))
  11351. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  11352. (setq end (next-single-property-change beg 'org-cwidth))
  11353. (delete-region beg end)
  11354. (goto-char beg)))
  11355. (goto-char (point-min))))
  11356. (defun org-search-todo-below (line lines level)
  11357. "Search the subtree below LINE for any TODO entries."
  11358. (let ((rest (cdr (memq line lines)))
  11359. (re org-todo-line-regexp)
  11360. line lv todo)
  11361. (catch 'exit
  11362. (while (setq line (pop rest))
  11363. (if (string-match re line)
  11364. (progn
  11365. (setq lv (- (match-end 1) (match-beginning 1))
  11366. todo (and (match-beginning 2)
  11367. (not (equal (match-string 2 line)
  11368. org-done-string))))
  11369. ; TODO, not DONE
  11370. (if (<= lv level) (throw 'exit nil))
  11371. (if todo (throw 'exit t))))))))
  11372. ;; FIXME: Try to handle <b> and <i> as faces via text properties.
  11373. ;; We could also implement *bold*,/italic/ and _underline_ for ASCII export
  11374. (defun org-html-expand-for-ascii (line)
  11375. "Handle quoted HTML for ASCII export."
  11376. (if org-export-html-expand
  11377. (while (string-match "@<[^<>\n]*>" line)
  11378. ;; We just remove the tags for now.
  11379. (setq line (replace-match "" nil nil line))))
  11380. line)
  11381. (defun org-insert-centered (s &optional underline)
  11382. "Insert the string S centered and underline it with character UNDERLINE."
  11383. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  11384. (insert (make-string ind ?\ ) s "\n")
  11385. (if underline
  11386. (insert (make-string ind ?\ )
  11387. (make-string (string-width s) underline)
  11388. "\n"))))
  11389. (defun org-ascii-level-start (level title umax)
  11390. "Insert a new level in ASCII export."
  11391. (let (char)
  11392. (if (> level umax)
  11393. (insert (make-string (* 2 (- level umax 1)) ?\ ) "* " title "\n")
  11394. (if (or (not (equal (char-before) ?\n))
  11395. (not (equal (char-before (1- (point))) ?\n)))
  11396. (insert "\n"))
  11397. (setq char (nth (- umax level) (reverse org-ascii-underline)))
  11398. (if org-export-with-section-numbers
  11399. (setq title (concat (org-section-number level) " " title)))
  11400. (insert title "\n" (make-string (string-width title) char) "\n"))))
  11401. (defun org-export-visible (type arg)
  11402. "Create a copy of the visible part of the current buffer, and export it.
  11403. The copy is created in a temporary buffer and removed after use.
  11404. TYPE is the final key (as a string) of the `C-c C-x' key sequence that will
  11405. run the export command - in interactive use, the command prompts for this
  11406. key. As a special case, if the you type SPC at the prompt, the temporary
  11407. org-mode file will not be removed but presented to you so that you can
  11408. continue to use it. The prefix arg ARG is passed through to the exporting
  11409. command."
  11410. (interactive
  11411. (list (progn
  11412. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  11413. (char-to-string (read-char-exclusive)))
  11414. current-prefix-arg))
  11415. (if (not (member type '("a" "\C-a" "b" "\C-b" "h" "x" " ")))
  11416. (error "Invalid export key"))
  11417. (let* ((binding (key-binding (concat "\C-c\C-x" type)))
  11418. (keepp (equal type " "))
  11419. (file buffer-file-name)
  11420. (buffer (get-buffer-create "*Org Export Visible*"))
  11421. s e)
  11422. (with-current-buffer buffer (erase-buffer))
  11423. (save-excursion
  11424. (setq s (goto-char (point-min)))
  11425. (while (not (= (point) (point-max)))
  11426. (goto-char (org-find-invisible))
  11427. (append-to-buffer buffer s (point))
  11428. (setq s (goto-char (org-find-visible))))
  11429. (goto-char (point-min))
  11430. (unless keepp
  11431. ;; Copy all comment lines to the end, to make sure #+ settings are
  11432. ;; still available for the second export step. Kind of a hack, but
  11433. ;; does do the trick.
  11434. (if (looking-at "#[^\r\n]*")
  11435. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  11436. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  11437. (append-to-buffer buffer (1+ (match-beginning 0))
  11438. (min (point-max) (1+ (match-end 0))))))
  11439. (set-buffer buffer)
  11440. (let ((buffer-file-name file)
  11441. (org-inhibit-startup t))
  11442. (org-mode)
  11443. (show-all)
  11444. (unless keepp (funcall binding arg))))
  11445. (if (not keepp)
  11446. (kill-buffer buffer)
  11447. (switch-to-buffer-other-window buffer)
  11448. (goto-char (point-min)))))
  11449. (defun org-find-visible ()
  11450. (if (featurep 'noutline)
  11451. (let ((s (point)))
  11452. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  11453. (get-char-property s 'invisible)))
  11454. s)
  11455. (skip-chars-forward "^\n")
  11456. (point)))
  11457. (defun org-find-invisible ()
  11458. (if (featurep 'noutline)
  11459. (let ((s (point)))
  11460. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  11461. (not (get-char-property s 'invisible))))
  11462. s)
  11463. (skip-chars-forward "^\r")
  11464. (point)))
  11465. ;; HTML
  11466. (defun org-get-current-options ()
  11467. "Return a string with current options as keyword options.
  11468. Does include HTML export options as well as TODO and CATEGORY stuff."
  11469. (format
  11470. "#+TITLE: %s
  11471. #+AUTHOR: %s
  11472. #+EMAIL: %s
  11473. #+LANGUAGE: %s
  11474. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  11475. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
  11476. #+CATEGORY: %s
  11477. #+SEQ_TODO: %s
  11478. #+TYP_TODO: %s
  11479. #+STARTUP: %s %s %s %s %s %s
  11480. #+TAGS: %s
  11481. #+ARCHIVE: %s
  11482. "
  11483. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  11484. org-export-headline-levels
  11485. org-export-with-section-numbers
  11486. org-export-with-toc
  11487. org-export-preserve-breaks
  11488. org-export-html-expand
  11489. org-export-with-fixed-width
  11490. org-export-with-tables
  11491. org-export-with-sub-superscripts
  11492. org-export-with-emphasize
  11493. org-export-with-TeX-macros
  11494. (file-name-nondirectory buffer-file-name)
  11495. (if (equal org-todo-interpretation 'sequence)
  11496. (mapconcat 'identity org-todo-keywords " ")
  11497. "TODO FEEDBACK VERIFY DONE")
  11498. (if (equal org-todo-interpretation 'type)
  11499. (mapconcat 'identity org-todo-keywords " ")
  11500. "Me Jason Marie DONE")
  11501. (cdr (assoc org-startup-folded
  11502. '((nil . "showall") (t . "overview") (content . "content"))))
  11503. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  11504. (if org-odd-levels-only "odd" "oddeven")
  11505. (if org-hide-leading-stars "hidestars" "showstars")
  11506. (if org-startup-align-all-tables "align" "noalign")
  11507. (if org-log-done "logging" "nologging")
  11508. (if org-tag-alist (mapconcat 'car org-tag-alist " ") "")
  11509. org-archive-location
  11510. ))
  11511. (defun org-insert-export-options-template ()
  11512. "Insert into the buffer a template with information for exporting."
  11513. (interactive)
  11514. (if (not (bolp)) (newline))
  11515. (let ((s (org-get-current-options)))
  11516. (and (string-match "#\\+CATEGORY" s)
  11517. (setq s (substring s 0 (match-beginning 0))))
  11518. (insert s)))
  11519. (defun org-toggle-fixed-width-section (arg)
  11520. "Toggle the fixed-width export.
  11521. If there is no active region, the QUOTE keyword at the current headline is
  11522. inserted or removed. When present, it causes the text between this headline
  11523. and the next to be exported as fixed-width text, and unmodified.
  11524. If there is an active region, this command adds or removes a colon as the
  11525. first character of this line. If the first character of a line is a colon,
  11526. this line is also exported in fixed-width font."
  11527. (interactive "P")
  11528. (let* ((cc 0)
  11529. (regionp (org-region-active-p))
  11530. (beg (if regionp (region-beginning) (point)))
  11531. (end (if regionp (region-end)))
  11532. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  11533. (re "[ \t]*\\(:\\)")
  11534. off)
  11535. (if regionp
  11536. (save-excursion
  11537. (goto-char beg)
  11538. (setq cc (current-column))
  11539. (beginning-of-line 1)
  11540. (setq off (looking-at re))
  11541. (while (> nlines 0)
  11542. (setq nlines (1- nlines))
  11543. (beginning-of-line 1)
  11544. (cond
  11545. (arg
  11546. (move-to-column cc t)
  11547. (insert ":\n")
  11548. (forward-line -1))
  11549. ((and off (looking-at re))
  11550. (replace-match "" t t nil 1))
  11551. ((not off) (move-to-column cc t) (insert ":")))
  11552. (forward-line 1)))
  11553. (save-excursion
  11554. (org-back-to-heading)
  11555. (if (looking-at (concat outline-regexp
  11556. "\\( +\\<" org-quote-string "\\>\\)"))
  11557. (replace-match "" t t nil 1)
  11558. (if (looking-at outline-regexp)
  11559. (progn
  11560. (goto-char (match-end 0))
  11561. (insert " " org-quote-string))))))))
  11562. (defun org-export-as-html-and-open (arg)
  11563. "Export the outline as HTML and immediately open it with a browser.
  11564. If there is an active region, export only the region.
  11565. The prefix ARG specifies how many levels of the outline should become
  11566. headlines. The default is 3. Lower levels will become bulleted lists."
  11567. (interactive "P")
  11568. (org-export-as-html arg 'hidden)
  11569. (org-open-file buffer-file-name))
  11570. (defun org-export-as-html-batch ()
  11571. "Call `org-export-as-html', may be used in batch processing as
  11572. emacs --batch
  11573. --load=$HOME/lib/emacs/org.el
  11574. --eval \"(setq org-export-headline-levels 2)\"
  11575. --visit=MyFile --funcall org-export-as-html-batch"
  11576. (org-export-as-html org-export-headline-levels 'hidden))
  11577. (defun org-export-as-html (arg &optional hidden ext-plist)
  11578. "Export the outline as a pretty HTML file.
  11579. If there is an active region, export only the region.
  11580. The prefix ARG specifies how many levels of the outline should become
  11581. headlines. The default is 3. Lower levels will become bulleted lists.
  11582. When HIDDEN is non-nil, don't display the HTML buffer.
  11583. EXT-PLIST is a property list with external parameters overriding
  11584. org-mode's default settings, but still inferior to file-local settings."
  11585. (interactive "P")
  11586. (setq-default org-todo-line-regexp org-todo-line-regexp)
  11587. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  11588. (setq-default org-done-string org-done-string)
  11589. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  11590. ext-plist
  11591. (org-infile-export-plist)))
  11592. (style (plist-get opt-plist :style))
  11593. (odd org-odd-levels-only)
  11594. (region-p (org-region-active-p))
  11595. (region
  11596. (buffer-substring
  11597. (if region-p (region-beginning) (point-min))
  11598. (if region-p (region-end) (point-max))))
  11599. (all_lines
  11600. (org-skip-comments (org-split-string
  11601. (org-cleaned-string-for-export region)
  11602. "[\r\n]")))
  11603. (lines (org-export-find-first-heading-line all_lines))
  11604. (level 0) (line "") (origline "") txt todo
  11605. (umax nil)
  11606. (filename (concat (file-name-as-directory
  11607. (org-export-directory :html opt-plist))
  11608. (file-name-sans-extension
  11609. (file-name-nondirectory buffer-file-name))
  11610. ".html"))
  11611. (buffer (find-file-noselect filename))
  11612. (levels-open (make-vector org-level-max nil))
  11613. (date (format-time-string "%Y/%m/%d" (current-time)))
  11614. (time (format-time-string "%X" (org-current-time)))
  11615. (author (plist-get opt-plist :author))
  11616. (title (or (plist-get opt-plist :title)
  11617. (file-name-sans-extension
  11618. (file-name-nondirectory buffer-file-name))))
  11619. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  11620. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  11621. (inquote nil)
  11622. (infixed nil)
  11623. (in-local-list nil)
  11624. (local-list-num nil)
  11625. (local-list-indent nil)
  11626. (llt org-plain-list-ordered-item-terminator)
  11627. (email (plist-get opt-plist :email))
  11628. (language (plist-get opt-plist :language))
  11629. (text (plist-get opt-plist :text))
  11630. (lang-words nil)
  11631. (target-alist nil) tg
  11632. (head-count 0) cnt
  11633. (start 0)
  11634. ;; FIXME: The following returns always nil under XEmacs
  11635. (coding-system (and (fboundp 'coding-system-get)
  11636. (boundp 'buffer-file-coding-system)
  11637. buffer-file-coding-system))
  11638. (coding-system-for-write (or coding-system coding-system-for-write))
  11639. (save-buffer-coding-system (or coding-system save-buffer-coding-system))
  11640. (charset (and coding-system
  11641. (coding-system-get coding-system 'mime-charset)))
  11642. table-open type
  11643. table-buffer table-orig-buffer
  11644. ind start-is-num starter
  11645. rpl path desc descp desc1 desc2 link
  11646. )
  11647. (message "Exporting...")
  11648. (setq org-last-level 1)
  11649. (org-init-section-numbers)
  11650. ;; Get the language-dependent settings
  11651. (setq lang-words (or (assoc language org-export-language-setup)
  11652. (assoc "en" org-export-language-setup)))
  11653. ;; Switch to the output buffer
  11654. (if (or hidden (not org-export-html-show-new-buffer))
  11655. (set-buffer buffer)
  11656. (switch-to-buffer-other-window buffer))
  11657. (erase-buffer)
  11658. (fundamental-mode)
  11659. (let ((case-fold-search nil)
  11660. (org-odd-levels-only odd))
  11661. ;; create local variables for all options, to make sure all called
  11662. ;; functions get the correct information
  11663. (mapcar (lambda (x)
  11664. (set (make-local-variable (cdr x))
  11665. (plist-get opt-plist (car x))))
  11666. org-export-plist-vars)
  11667. (setq umax (if arg (prefix-numeric-value arg)
  11668. org-export-headline-levels))
  11669. ;; File header
  11670. (insert (format
  11671. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"
  11672. \"http://www.w3.org/TR/REC-html40/loose.dtd\">
  11673. <html lang=\"%s\"><head>
  11674. <title>%s</title>
  11675. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\">
  11676. <meta name=generator content=\"Org-mode\">
  11677. <meta name=generated content=\"%s %s\">
  11678. <meta name=author content=\"%s\">
  11679. %s
  11680. </head><body>
  11681. "
  11682. language (org-html-expand title) (or charset "iso-8859-1")
  11683. date time author style))
  11684. (insert (or (plist-get opt-plist :preamble) ""))
  11685. (when (plist-get opt-plist :auto-preamble)
  11686. (if title (insert (concat "<H1 class=\"title\">"
  11687. (org-html-expand title) "</H1>\n")))
  11688. ; (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
  11689. ; (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
  11690. ; email "&gt;</a>\n")))
  11691. ; (if (or author email) (insert "<br>\n"))
  11692. ; (if (and date time) (insert (concat (nth 2 lang-words) ": "
  11693. ; date " " time "<br>\n")))
  11694. (if text (insert (concat "<p>\n" (org-html-expand text)))))
  11695. (if org-export-with-toc
  11696. (progn
  11697. (insert (format "<H2>%s</H2>\n" (nth 3 lang-words)))
  11698. (insert "<ul>\n")
  11699. (setq lines
  11700. (mapcar '(lambda (line)
  11701. (if (string-match org-todo-line-regexp line)
  11702. ;; This is a headline
  11703. (progn
  11704. (setq level (- (match-end 1) (match-beginning 1))
  11705. level (org-tr-level level)
  11706. txt (save-match-data
  11707. (org-html-expand
  11708. (org-html-cleanup-toc-line
  11709. (match-string 3 line))))
  11710. todo
  11711. (or (and org-export-mark-todo-in-toc
  11712. (match-beginning 2)
  11713. (not (equal (match-string 2 line)
  11714. org-done-string)))
  11715. ; TODO, not DONE
  11716. (and org-export-mark-todo-in-toc
  11717. (= level umax)
  11718. (org-search-todo-below
  11719. line lines level))))
  11720. (if org-export-with-section-numbers
  11721. (setq txt (concat (org-section-number level)
  11722. " " txt)))
  11723. (if (<= level umax)
  11724. (progn
  11725. (setq head-count (+ head-count 1))
  11726. (if (> level org-last-level)
  11727. (progn
  11728. (setq cnt (- level org-last-level))
  11729. (while (>= (setq cnt (1- cnt)) 0)
  11730. (insert "<ul>"))
  11731. (insert "\n")))
  11732. (if (< level org-last-level)
  11733. (progn
  11734. (setq cnt (- org-last-level level))
  11735. (while (>= (setq cnt (1- cnt)) 0)
  11736. (insert "</ul>"))
  11737. (insert "\n")))
  11738. ;; Check for targets
  11739. (while (string-match org-target-regexp line)
  11740. (setq tg (match-string 1 line)
  11741. line (replace-match
  11742. (concat "@<span class=\"target\">" tg "@</span> ")
  11743. t t line))
  11744. (push (cons (org-solidify-link-text tg)
  11745. (format "sec-%d" head-count))
  11746. target-alist))
  11747. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  11748. (setq txt (replace-match "" t t txt)))
  11749. (insert
  11750. (format
  11751. (if todo
  11752. "<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>\n"
  11753. "<li><a href=\"#sec-%d\">%s</a>\n")
  11754. head-count txt))
  11755. (setq org-last-level level))
  11756. )))
  11757. line)
  11758. lines))
  11759. (while (> org-last-level 0)
  11760. (setq org-last-level (1- org-last-level))
  11761. (insert "</ul>\n"))
  11762. ))
  11763. (setq head-count 0)
  11764. (org-init-section-numbers)
  11765. (while (setq line (pop lines) origline line)
  11766. (catch 'nextline
  11767. ;; end of quote section?
  11768. (when (and inquote (string-match "^\\*+" line))
  11769. (insert "</pre>\n")
  11770. (setq inquote nil))
  11771. ;; inside a quote section?
  11772. (when inquote
  11773. (insert (org-html-protect line) "\n")
  11774. (throw 'nextline nil))
  11775. ;; verbatim lines
  11776. (when (and org-export-with-fixed-width
  11777. (string-match "^[ \t]*:\\(.*\\)" line))
  11778. (when (not infixed)
  11779. (setq infixed t)
  11780. (insert "<pre>\n"))
  11781. (insert (org-html-protect (match-string 1 line)) "\n")
  11782. (when (and lines
  11783. (not (string-match "^[ \t]*\\(:.*\\)"
  11784. (car lines))))
  11785. (setq infixed nil)
  11786. (insert "</pre>\n"))
  11787. (throw 'nextline nil))
  11788. ;; make targets to anchors
  11789. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  11790. (cond
  11791. ((match-end 2)
  11792. (setq line (replace-match
  11793. (concat "@<a name=\""
  11794. (org-solidify-link-text (match-string 1 line))
  11795. "\">\\nbsp@</a>")
  11796. t t line)))
  11797. ((and org-export-with-toc (equal (string-to-char line) ?*))
  11798. (setq line (replace-match
  11799. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  11800. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  11801. t t line)))
  11802. (t
  11803. (setq line (replace-match
  11804. (concat "@<a name=\""
  11805. (org-solidify-link-text (match-string 1 line))
  11806. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  11807. t t line)))))
  11808. (setq line (org-html-handle-time-stamps line))
  11809. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  11810. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  11811. ;; Also handle sub_superscripts and checkboxes FIXME
  11812. (setq line (org-html-expand line))
  11813. ;; Format the links
  11814. (setq start 0)
  11815. (while (string-match org-bracket-link-analytic-regexp line start)
  11816. (setq start (match-beginning 0))
  11817. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  11818. (setq path (match-string 3 line))
  11819. (setq desc1 (if (match-end 5) (match-string 5 line))
  11820. desc2 (if (match-end 2) (concat type ":" path) path)
  11821. descp (and desc1 (not (equal desc1 desc2)))
  11822. desc (or desc1 desc2))
  11823. ;; FIXME: do we need to unescape here somewhere?
  11824. (cond
  11825. ((equal type "internal")
  11826. (setq rpl
  11827. (concat
  11828. "<a href=\"#"
  11829. (org-solidify-link-text path target-alist)
  11830. "\">" desc "</a>")))
  11831. ((member type '("http" "https" "ftp" "mailto" "news"))
  11832. ;; standard URL
  11833. (setq link (concat type ":" path))
  11834. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  11835. ((string= type "file")
  11836. ;; FILE link
  11837. (let* ((filename path)
  11838. (abs-p (file-name-absolute-p filename))
  11839. thefile file-is-image-p search)
  11840. (save-match-data
  11841. (if (string-match "::\\(.*\\)" filename)
  11842. (setq search (match-string 1 filename)
  11843. filename (replace-match "" nil nil filename)))
  11844. (setq file-is-image-p
  11845. (string-match (org-image-file-name-regexp) filename))
  11846. (setq thefile (if abs-p (expand-file-name filename) filename))
  11847. (when (and org-export-html-link-org-files-as-html
  11848. (string-match "\\.org$" thefile))
  11849. (setq thefile (concat (substring thefile 0
  11850. (match-beginning 0))
  11851. ".html"))
  11852. (if (and search
  11853. ;; make sure this is can be used as target search
  11854. (not (string-match "^[0-9]*$" search))
  11855. (not (string-match "^\\*" search))
  11856. (not (string-match "^/.*/$" search)))
  11857. (setq thefile (concat thefile "#"
  11858. (org-solidify-link-text
  11859. (org-link-unescape search)))))
  11860. (when (string-match "^file:" desc)
  11861. (setq desc (replace-match "" t t desc))
  11862. (if (string-match "\\.org$" desc)
  11863. (setq desc (replace-match "" t t desc))))))
  11864. (setq rpl (if (and file-is-image-p
  11865. (or (eq t org-export-html-inline-images)
  11866. (and org-export-html-inline-images
  11867. (not descp))))
  11868. (concat "<img src=\"" thefile "\"/>")
  11869. (concat "<a href=\"" thefile "\">" desc "</a>")))))
  11870. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  11871. (setq rpl (concat "<i>&lt;" type ":"
  11872. (save-match-data (org-link-unescape path))
  11873. "&gt;</i>"))))
  11874. (setq line (replace-match rpl t t line)
  11875. start (+ start (length rpl))))
  11876. ;; TODO items
  11877. (if (and (string-match org-todo-line-regexp line)
  11878. (match-beginning 2))
  11879. (if (equal (match-string 2 line) org-done-string)
  11880. (setq line (replace-match
  11881. "<span class=\"done\">\\2</span>"
  11882. nil nil line 2))
  11883. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  11884. nil nil line 2))))
  11885. (cond
  11886. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  11887. ;; This is a headline
  11888. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  11889. txt (match-string 2 line))
  11890. (if (string-match quote-re0 txt)
  11891. (setq txt (replace-match "" t t txt)))
  11892. (if (<= level umax) (setq head-count (+ head-count 1)))
  11893. (when in-local-list
  11894. ;; Close any local lists before inserting a new header line
  11895. (while local-list-num
  11896. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  11897. (pop local-list-num))
  11898. (setq local-list-indent nil
  11899. in-local-list nil))
  11900. (org-html-level-start level txt umax
  11901. (and org-export-with-toc (<= level umax))
  11902. head-count)
  11903. ;; QUOTES
  11904. (when (string-match quote-re line)
  11905. (insert "<pre>")
  11906. (setq inquote t)))
  11907. ((and org-export-with-tables
  11908. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  11909. (if (not table-open)
  11910. ;; New table starts
  11911. (setq table-open t table-buffer nil table-orig-buffer nil))
  11912. ;; Accumulate lines
  11913. (setq table-buffer (cons line table-buffer)
  11914. table-orig-buffer (cons origline table-orig-buffer))
  11915. (when (or (not lines)
  11916. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  11917. (car lines))))
  11918. (setq table-open nil
  11919. table-buffer (nreverse table-buffer)
  11920. table-orig-buffer (nreverse table-orig-buffer))
  11921. (insert (org-format-table-html table-buffer table-orig-buffer))))
  11922. (t
  11923. ;; Normal lines
  11924. (when (string-match
  11925. (cond
  11926. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  11927. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  11928. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  11929. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  11930. line)
  11931. (setq ind (org-get-string-indentation line)
  11932. start-is-num (match-beginning 4)
  11933. starter (if (match-beginning 2)
  11934. (substring (match-string 2 line) 0 -1))
  11935. line (substring line (match-beginning 5)))
  11936. (unless (string-match "[^ \t]" line)
  11937. ;; empty line. Pretend indentation is large.
  11938. (setq ind (1+ (or (car local-list-indent) 1))))
  11939. (while (and in-local-list
  11940. (or (and (= ind (car local-list-indent))
  11941. (not starter))
  11942. (< ind (car local-list-indent))))
  11943. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  11944. (pop local-list-num) (pop local-list-indent)
  11945. (setq in-local-list local-list-indent))
  11946. (cond
  11947. ((and starter
  11948. (or (not in-local-list)
  11949. (> ind (car local-list-indent))))
  11950. ;; Start new (level of ) list
  11951. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  11952. (push start-is-num local-list-num)
  11953. (push ind local-list-indent)
  11954. (setq in-local-list t))
  11955. (starter
  11956. ;; continue current list
  11957. (insert "<li>\n")))
  11958. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  11959. (setq line
  11960. (replace-match
  11961. (if (equal (match-string 1 line) "X")
  11962. "<b>[X]</b>"
  11963. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  11964. t t line))))
  11965. ;; Empty lines start a new paragraph. If hand-formatted lists
  11966. ;; are not fully interpreted, lines starting with "-", "+", "*"
  11967. ;; also start a new paragraph.
  11968. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (insert "<p>"))
  11969. (insert line (if org-export-preserve-breaks "<br>\n" "\n"))))
  11970. ))
  11971. ;; Properly close all local lists and other lists
  11972. (when inquote (insert "</pre>\n"))
  11973. (when in-local-list
  11974. ;; Close any local lists before inserting a new header line
  11975. (while local-list-num
  11976. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  11977. (pop local-list-num))
  11978. (setq local-list-indent nil
  11979. in-local-list nil))
  11980. (org-html-level-start 1 nil umax
  11981. (and org-export-with-toc (<= level umax))
  11982. head-count)
  11983. (when (plist-get opt-plist :auto-postamble)
  11984. (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
  11985. (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
  11986. email "&gt;</a>\n")))
  11987. (if (or author email) (insert "<br>\n"))
  11988. (if (and date time) (insert (concat (nth 2 lang-words) ": "
  11989. date " " time "<br>\n"))))
  11990. (if org-export-html-with-timestamp
  11991. (insert org-export-html-html-helper-timestamp))
  11992. (insert (or (plist-get opt-plist :postamble) ""))
  11993. (insert "</body>\n</html>\n")
  11994. (normal-mode)
  11995. (save-buffer)
  11996. (goto-char (point-min)))))
  11997. (defun org-format-table-html (lines olines)
  11998. "Find out which HTML converter to use and return the HTML code."
  11999. (if (string-match "^[ \t]*|" (car lines))
  12000. ;; A normal org table
  12001. (org-format-org-table-html lines)
  12002. ;; Table made by table.el - test for spanning
  12003. (let* ((hlines (delq nil (mapcar
  12004. (lambda (x)
  12005. (if (string-match "^[ \t]*\\+-" x) x
  12006. nil))
  12007. lines)))
  12008. (first (car hlines))
  12009. (ll (and (string-match "\\S-+" first)
  12010. (match-string 0 first)))
  12011. (re (concat "^[ \t]*" (regexp-quote ll)))
  12012. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  12013. hlines))))
  12014. (if (and (not spanning)
  12015. (not org-export-prefer-native-exporter-for-tables))
  12016. ;; We can use my own converter with HTML conversions
  12017. (org-format-table-table-html lines)
  12018. ;; Need to use the code generator in table.el, with the original text.
  12019. (org-format-table-table-html-using-table-generate-source olines)))))
  12020. (defun org-format-org-table-html (lines)
  12021. "Format a table into HTML."
  12022. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  12023. (setq lines (nreverse lines))
  12024. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  12025. (setq lines (nreverse lines))
  12026. (when org-export-table-remove-special-lines
  12027. ;; Check if the table has a marking column. If yes remove the
  12028. ;; column and the special lines
  12029. (let* ((special
  12030. (not
  12031. (memq nil
  12032. (mapcar
  12033. (lambda (x)
  12034. (or (string-match "^[ \t]*|-" x)
  12035. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  12036. lines)))))
  12037. (if special
  12038. (setq lines
  12039. (delq nil
  12040. (mapcar
  12041. (lambda (x)
  12042. (if (string-match "^[ \t]*| *[!_^] *|" x)
  12043. nil ; ignore this line
  12044. (and (or (string-match "^[ \t]*|-+\\+" x)
  12045. (string-match "^[ \t]*|[^|]*|" x))
  12046. (replace-match "|" t t x))))
  12047. lines))))))
  12048. (let ((head (and org-export-highlight-first-table-line
  12049. (delq nil (mapcar
  12050. (lambda (x) (string-match "^[ \t]*|-" x))
  12051. (cdr lines)))))
  12052. line fields html)
  12053. (setq html (concat org-export-html-table-tag "\n"))
  12054. (while (setq line (pop lines))
  12055. (catch 'next-line
  12056. (if (string-match "^[ \t]*|-" line)
  12057. (progn
  12058. (setq head nil) ;; head ends here, first time around
  12059. ;; ignore this line
  12060. (throw 'next-line t)))
  12061. ;; Break the line into fields
  12062. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  12063. (setq html (concat
  12064. html
  12065. "<tr>"
  12066. (mapconcat (lambda (x)
  12067. (if head
  12068. (concat "<th>" x "</th>")
  12069. (concat "<td>" x "</td>")))
  12070. fields "")
  12071. "</tr>\n"))))
  12072. (setq html (concat html "</table>\n"))
  12073. html))
  12074. (defun org-fake-empty-table-line (line)
  12075. "Replace everything except \"|\" with spaces."
  12076. (let ((i (length line))
  12077. (newstr (copy-sequence line)))
  12078. (while (> i 0)
  12079. (setq i (1- i))
  12080. (if (not (eq (aref newstr i) ?|))
  12081. (aset newstr i ?\ )))
  12082. newstr))
  12083. (defun org-format-table-table-html (lines)
  12084. "Format a table generated by table.el into HTML.
  12085. This conversion does *not* use `table-generate-source' from table.el.
  12086. This has the advantage that Org-mode's HTML conversions can be used.
  12087. But it has the disadvantage, that no cell- or row-spanning is allowed."
  12088. (let (line field-buffer
  12089. (head org-export-highlight-first-table-line)
  12090. fields html empty)
  12091. (setq html (concat org-export-html-table-tag "\n"))
  12092. (while (setq line (pop lines))
  12093. (setq empty "&nbsp")
  12094. (catch 'next-line
  12095. (if (string-match "^[ \t]*\\+-" line)
  12096. (progn
  12097. (if field-buffer
  12098. (progn
  12099. (setq html (concat
  12100. html
  12101. "<tr>"
  12102. (mapconcat
  12103. (lambda (x)
  12104. (if (equal x "") (setq x empty))
  12105. (if head
  12106. (concat "<th>" x "</th>\n")
  12107. (concat "<td>" x "</td>\n")))
  12108. field-buffer "\n")
  12109. "</tr>\n"))
  12110. (setq head nil)
  12111. (setq field-buffer nil)))
  12112. ;; Ignore this line
  12113. (throw 'next-line t)))
  12114. ;; Break the line into fields and store the fields
  12115. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  12116. (if field-buffer
  12117. (setq field-buffer (mapcar
  12118. (lambda (x)
  12119. (concat x "<br>" (pop fields)))
  12120. field-buffer))
  12121. (setq field-buffer fields))))
  12122. (setq html (concat html "</table>\n"))
  12123. html))
  12124. (defun org-format-table-table-html-using-table-generate-source (lines)
  12125. "Format a table into html, using `table-generate-source' from table.el.
  12126. This has the advantage that cell- or row-spanning is allowed.
  12127. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  12128. (require 'table)
  12129. (with-current-buffer (get-buffer-create " org-tmp1 ")
  12130. (erase-buffer)
  12131. (insert (mapconcat 'identity lines "\n"))
  12132. (goto-char (point-min))
  12133. (if (not (re-search-forward "|[^+]" nil t))
  12134. (error "Error processing table"))
  12135. (table-recognize-table)
  12136. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  12137. (table-generate-source 'html " org-tmp2 ")
  12138. (set-buffer " org-tmp2 ")
  12139. (buffer-substring (point-min) (point-max))))
  12140. (defun org-html-handle-time-stamps (s)
  12141. "Format time stamps in string S, or remove them."
  12142. (let (r b)
  12143. (while (string-match org-maybe-keyword-time-regexp s)
  12144. (or b (setq b (substring s 0 (match-beginning 0))))
  12145. (if (not org-export-with-timestamps)
  12146. (setq r (concat r (substring s 0 (match-beginning 0)))
  12147. s (substring s (match-end 0)))
  12148. (setq r (concat
  12149. r (substring s 0 (match-beginning 0))
  12150. (if (match-end 1)
  12151. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  12152. (match-string 1 s)))
  12153. (format " @<span class=\"timestamp\">%s@</span>"
  12154. (substring (match-string 3 s) 1 -1)))
  12155. s (substring s (match-end 0)))))
  12156. ;; Line break of line started and ended with time stamp stuff
  12157. (if (not r)
  12158. s
  12159. (setq r (concat r s))
  12160. (unless (string-match "\\S-" (concat b s))
  12161. (setq r (concat r "@<br>")))
  12162. r)))
  12163. (defun org-html-protect (s)
  12164. ;; convert & to &amp;, < to &lt; and > to &gt;
  12165. (let ((start 0))
  12166. (while (string-match "&" s start)
  12167. (setq s (replace-match "&amp;" t t s)
  12168. start (1+ (match-beginning 0))))
  12169. (while (string-match "<" s)
  12170. (setq s (replace-match "&lt;" t t s)))
  12171. (while (string-match ">" s)
  12172. (setq s (replace-match "&gt;" t t s))))
  12173. s)
  12174. (defun org-html-cleanup-toc-line (s)
  12175. "Remove tags and time staps from lines going into the toc."
  12176. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  12177. (setq s (replace-match "" t t s)))
  12178. (while (string-match org-maybe-keyword-time-regexp s)
  12179. (setq s (replace-match "" t t s)))
  12180. s)
  12181. (defun org-html-expand (string)
  12182. "Prepare STRING for HTML export. Applies all active conversions.
  12183. If there are links in the string, don't modify these."
  12184. (let* (m s l res)
  12185. (while (setq m (string-match org-bracket-link-regexp string))
  12186. (setq s (substring string 0 m)
  12187. l (match-string 0 string)
  12188. string (substring string (match-end 0)))
  12189. (push (org-html-do-expand s) res)
  12190. (push l res))
  12191. (push (org-html-do-expand string) res)
  12192. (apply 'concat (nreverse res))))
  12193. (defun org-html-do-expand (s)
  12194. "Apply all active conversions to translate special ASCII to HTML."
  12195. (setq s (org-html-protect s))
  12196. (if org-export-html-expand
  12197. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  12198. (setq s (replace-match "<\\1>" nil nil s))))
  12199. (if org-export-with-emphasize
  12200. (setq s (org-export-html-convert-emphasize s)))
  12201. (if org-export-with-sub-superscripts
  12202. (setq s (org-export-html-convert-sub-super s)))
  12203. (if org-export-with-TeX-macros
  12204. (let ((start 0) wd ass)
  12205. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  12206. (setq wd (match-string 1 s))
  12207. (if (setq ass (assoc wd org-html-entities))
  12208. (setq s (replace-match (or (cdr ass)
  12209. (concat "&" (car ass) ";"))
  12210. t t s))
  12211. (setq start (+ start (length wd)))))))
  12212. s)
  12213. (defun org-create-multibrace-regexp (left right n)
  12214. "Create a regular expression which will match a balanced sexp.
  12215. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  12216. as single character strings.
  12217. The regexp returned will match the entire expression including the
  12218. delimiters. It will also define a single group which contains the
  12219. match except for the outermost delimiters. The maximum depth of
  12220. stacked delimiters is N. Escaping delimiters is not possible."
  12221. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  12222. (or "\\|")
  12223. (re nothing)
  12224. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  12225. (while (> n 1)
  12226. (setq n (1- n)
  12227. re (concat re or next)
  12228. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  12229. (concat left "\\(" re "\\)" right)))
  12230. (defvar org-match-substring-regexp
  12231. (concat
  12232. "\\([^\\]\\)\\([_^]\\)\\("
  12233. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  12234. "\\|"
  12235. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  12236. "\\|"
  12237. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  12238. ;;;;;;;;;;;;;;;;;;;;;;;;;^ FIXME
  12239. "The regular expression matching a sub- or superscript.")
  12240. (defun org-export-html-convert-sub-super (string)
  12241. "Convert sub- and superscripts in STRING to HTML."
  12242. (let (key c)
  12243. (while (string-match org-match-substring-regexp string)
  12244. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  12245. (setq c (or (match-string 8 string)
  12246. (match-string 6 string)
  12247. (match-string 5 string)))
  12248. (setq string (replace-match
  12249. (concat (match-string 1 string)
  12250. "<" key ">" c "</" key ">")
  12251. t t string)))
  12252. (while (string-match "\\\\\\([_^]\\)" string)
  12253. (setq string (replace-match (match-string 1 string) t t string))))
  12254. string)
  12255. (defun org-export-html-convert-emphasize (string)
  12256. (while (string-match org-italic-re string)
  12257. (setq string (replace-match "\\1<i>\\3</i>\\4" t nil string)))
  12258. (while (string-match org-bold-re string)
  12259. (setq string (replace-match "\\1<b>\\3</b>\\4" t nil string)))
  12260. (while (string-match org-underline-re string)
  12261. (setq string (replace-match "\\1<u>\\3</u>\\4" t nil string)))
  12262. string)
  12263. (defun org-html-level-start (level title umax with-toc head-count)
  12264. "Insert a new level in HTML export.
  12265. When TITLE is nil, just close all open levels."
  12266. (let ((l (1+ (max level umax))))
  12267. (while (<= l org-level-max)
  12268. (if (aref levels-open (1- l))
  12269. (progn
  12270. (org-html-level-close l)
  12271. (aset levels-open (1- l) nil)))
  12272. (setq l (1+ l)))
  12273. (when title
  12274. ;; If title is nil, this means this function is called to close
  12275. ;; all levels, so the rest is done only if title is given
  12276. (if (> level umax)
  12277. (progn
  12278. (if (aref levels-open (1- level))
  12279. (insert "<li>" title "<p>\n")
  12280. (aset levels-open (1- level) t)
  12281. (insert "<ul><li>" title "<p>\n")))
  12282. (if org-export-with-section-numbers
  12283. (setq title (concat (org-section-number level) " " title)))
  12284. (setq level (+ level 1))
  12285. ;; FIXME: here we need to handle the tags, somehow.
  12286. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  12287. (setq title (replace-match
  12288. (if org-export-with-tags
  12289. (save-match-data
  12290. (concat
  12291. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  12292. (mapconcat 'identity (org-split-string
  12293. (match-string 1 title) ":")
  12294. "&nbsp;")
  12295. "</span>"))
  12296. "")
  12297. t t title)))
  12298. (if with-toc
  12299. (insert (format "\n<H%d><a name=\"sec-%d\">%s</a></H%d>\n"
  12300. level head-count title level))
  12301. (insert (format "\n<H%d>%s</H%d>\n" level title level)))))))
  12302. (defun org-html-level-close (&rest args)
  12303. "Terminate one level in HTML export."
  12304. (insert "</ul>"))
  12305. ;; Variable holding the vector with section numbers
  12306. (defvar org-section-numbers (make-vector org-level-max 0))
  12307. (defun org-init-section-numbers ()
  12308. "Initialize the vector for the section numbers."
  12309. (let* ((level -1)
  12310. (numbers (nreverse (org-split-string "" "\\.")))
  12311. (depth (1- (length org-section-numbers)))
  12312. (i depth) number-string)
  12313. (while (>= i 0)
  12314. (if (> i level)
  12315. (aset org-section-numbers i 0)
  12316. (setq number-string (or (car numbers) "0"))
  12317. (if (string-match "\\`[A-Z]\\'" number-string)
  12318. (aset org-section-numbers i
  12319. (- (string-to-char number-string) ?A -1))
  12320. (aset org-section-numbers i (string-to-number number-string)))
  12321. (pop numbers))
  12322. (setq i (1- i)))))
  12323. (defun org-section-number (&optional level)
  12324. "Return a string with the current section number.
  12325. When LEVEL is non-nil, increase section numbers on that level."
  12326. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  12327. (when level
  12328. (when (> level -1)
  12329. (aset org-section-numbers
  12330. level (1+ (aref org-section-numbers level))))
  12331. (setq idx (1+ level))
  12332. (while (<= idx depth)
  12333. (if (not (= idx 1))
  12334. (aset org-section-numbers idx 0))
  12335. (setq idx (1+ idx))))
  12336. (setq idx 0)
  12337. (while (<= idx depth)
  12338. (setq n (aref org-section-numbers idx))
  12339. (setq string (concat string (if (not (string= string "")) "." "")
  12340. (int-to-string n)))
  12341. (setq idx (1+ idx)))
  12342. (save-match-data
  12343. (if (string-match "\\`\\([@0]\\.\\)+" string)
  12344. (setq string (replace-match "" nil nil string)))
  12345. (if (string-match "\\(\\.0\\)+\\'" string)
  12346. (setq string (replace-match "" nil nil string))))
  12347. string))
  12348. (defun org-export-icalendar-this-file ()
  12349. "Export current file as an iCalendar file.
  12350. The iCalendar file will be located in the same directory as the Org-mode
  12351. file, but with extension `.ics'."
  12352. (interactive)
  12353. (org-export-icalendar nil buffer-file-name))
  12354. (defun org-export-as-xml (arg)
  12355. "Export current buffer as XOXO XML buffer."
  12356. (interactive "P")
  12357. (cond ((eq org-export-xml-type 'xoxo)
  12358. (org-export-as-xoxo (current-buffer)))))
  12359. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  12360. (with-current-buffer buffer
  12361. (apply 'insert output)))
  12362. (defun org-export-as-xoxo (&optional buffer)
  12363. "Export the org buffer as XOXO.
  12364. The XOXO buffer is named *xoxo-<source buffer name>*"
  12365. (interactive (list (current-buffer)))
  12366. ;; A quickie abstraction
  12367. ;; Output everything as XOXO
  12368. (with-current-buffer (get-buffer buffer)
  12369. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  12370. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12371. (org-infile-export-plist)))
  12372. (filename (concat (file-name-as-directory
  12373. (org-export-directory :xoxo opt-plist))
  12374. (file-name-sans-extension
  12375. (file-name-nondirectory buffer-file-name))
  12376. ".html"))
  12377. (out (find-file-noselect filename))
  12378. (last-level 1)
  12379. (hanging-li nil))
  12380. ;; Check the output buffer is empty.
  12381. (with-current-buffer out (erase-buffer))
  12382. ;; Kick off the output
  12383. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  12384. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  12385. (let* ((hd (match-string-no-properties 1))
  12386. (level (length hd))
  12387. (text (concat
  12388. (match-string-no-properties 2)
  12389. (save-excursion
  12390. (goto-char (match-end 0))
  12391. (let ((str ""))
  12392. (catch 'loop
  12393. (while 't
  12394. (forward-line)
  12395. (if (looking-at "^[ \t]\\(.*\\)")
  12396. (setq str (concat str (match-string-no-properties 1)))
  12397. (throw 'loop str)))))))))
  12398. ;; Handle level rendering
  12399. (cond
  12400. ((> level last-level)
  12401. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  12402. ((< level last-level)
  12403. (dotimes (- (- last-level level) 1)
  12404. (if hanging-li
  12405. (org-export-as-xoxo-insert-into out "</li>\n"))
  12406. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12407. (when hanging-li
  12408. (org-export-as-xoxo-insert-into out "</li>\n")
  12409. (setq hanging-li nil)))
  12410. ((equal level last-level)
  12411. (if hanging-li
  12412. (org-export-as-xoxo-insert-into out "</li>\n")))
  12413. )
  12414. (setq last-level level)
  12415. ;; And output the new li
  12416. (setq hanging-li 't)
  12417. (if (equal ?+ (elt text 0))
  12418. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  12419. (org-export-as-xoxo-insert-into out "<li>" text))))
  12420. ;; Finally finish off the ol
  12421. (dotimes (- last-level 1)
  12422. (if hanging-li
  12423. (org-export-as-xoxo-insert-into out "</li>\n"))
  12424. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12425. ;; Finish the buffer off and clean it up.
  12426. (switch-to-buffer-other-window out)
  12427. (indent-region (point-min) (point-max) nil)
  12428. (save-buffer)
  12429. (goto-char (point-min))
  12430. )))
  12431. ;;;###autoload
  12432. (defun org-export-icalendar-all-agenda-files ()
  12433. "Export all files in `org-agenda-files' to iCalendar .ics files.
  12434. Each iCalendar file will be located in the same directory as the Org-mode
  12435. file, but with extension `.ics'."
  12436. (interactive)
  12437. (apply 'org-export-icalendar nil (org-agenda-files t)))
  12438. ;;;###autoload
  12439. (defun org-export-icalendar-combine-agenda-files ()
  12440. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  12441. The file is stored under the name `org-combined-agenda-icalendar-file'."
  12442. (interactive)
  12443. (apply 'org-export-icalendar t (org-agenda-files t)))
  12444. (defun org-export-icalendar (combine &rest files)
  12445. "Create iCalendar files for all elements of FILES.
  12446. If COMBINE is non-nil, combine all calendar entries into a single large
  12447. file and store it under the name `org-combined-agenda-icalendar-file'."
  12448. (save-excursion
  12449. (let* ((dir (org-export-directory
  12450. :ical (list :publishing-directory
  12451. org-export-publishing-directory)))
  12452. file ical-file ical-buffer category started org-agenda-new-buffers)
  12453. (when combine
  12454. (setq ical-file
  12455. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  12456. org-combined-agenda-icalendar-file
  12457. (expand-file-name org-combined-agenda-icalendar-file dir))
  12458. ical-buffer (org-get-agenda-file-buffer ical-file))
  12459. (set-buffer ical-buffer) (erase-buffer))
  12460. (while (setq file (pop files))
  12461. (catch 'nextfile
  12462. (org-check-agenda-file file)
  12463. (set-buffer (org-get-agenda-file-buffer file))
  12464. (unless combine
  12465. (setq ical-file (concat (file-name-as-directory dir)
  12466. (file-name-sans-extension
  12467. (file-name-nondirectory buffer-file-name))
  12468. ".ics"))
  12469. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  12470. (with-current-buffer ical-buffer (erase-buffer)))
  12471. (setq category (or org-category
  12472. (file-name-sans-extension
  12473. (file-name-nondirectory buffer-file-name))))
  12474. (if (symbolp category) (setq category (symbol-name category)))
  12475. (let ((standard-output ical-buffer))
  12476. (if combine
  12477. (and (not started) (setq started t)
  12478. (org-start-icalendar-file org-icalendar-combined-name))
  12479. (org-start-icalendar-file category))
  12480. (org-print-icalendar-entries combine category)
  12481. (when (or (and combine (not files)) (not combine))
  12482. (org-finish-icalendar-file)
  12483. (set-buffer ical-buffer)
  12484. (save-buffer)
  12485. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  12486. (org-release-buffers org-agenda-new-buffers))))
  12487. (defvar org-after-save-iCalendar-file-hook nil
  12488. "Hook run after an iCalendar file has been saved.
  12489. The iCalendar buffer is still current when this hook is run.
  12490. A good way to use this is to tell a desktop calenndar application to re-read
  12491. the iCalendar file.")
  12492. (defun org-print-icalendar-entries (&optional combine category)
  12493. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  12494. When COMBINE is non nil, add the category to each line."
  12495. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  12496. (dts (org-ical-ts-to-string
  12497. (format-time-string (cdr org-time-stamp-formats) (current-time))
  12498. "DTSTART"))
  12499. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  12500. (save-excursion
  12501. (goto-char (point-min))
  12502. (while (re-search-forward org-ts-regexp nil t)
  12503. (setq pos (match-beginning 0)
  12504. ts (match-string 0)
  12505. inc t
  12506. hd (org-get-heading))
  12507. (if (looking-at re2)
  12508. (progn
  12509. (goto-char (match-end 0))
  12510. (setq ts2 (match-string 1) inc nil))
  12511. (setq ts2 ts
  12512. tmp (buffer-substring (max (point-min)
  12513. (- pos org-ds-keyword-length))
  12514. pos)
  12515. deadlinep (string-match org-deadline-regexp tmp)
  12516. scheduledp (string-match org-scheduled-regexp tmp)
  12517. ;; donep (org-entry-is-done-p)
  12518. ))
  12519. (if (or (string-match org-tr-regexp hd)
  12520. (string-match org-ts-regexp hd))
  12521. (setq hd (replace-match "" t t hd)))
  12522. (if combine
  12523. (setq hd (concat hd " (category " category ")")))
  12524. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  12525. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  12526. (princ (format "BEGIN:VEVENT
  12527. %s
  12528. %s
  12529. SUMMARY:%s
  12530. END:VEVENT\n"
  12531. (org-ical-ts-to-string ts "DTSTART")
  12532. (org-ical-ts-to-string ts2 "DTEND" inc)
  12533. hd)))
  12534. (when org-icalendar-include-todo
  12535. (goto-char (point-min))
  12536. (while (re-search-forward org-todo-line-regexp nil t)
  12537. (setq state (match-string 1))
  12538. (unless (equal state org-done-string)
  12539. (setq hd (match-string 3))
  12540. (if (string-match org-priority-regexp hd)
  12541. (setq pri (string-to-char (match-string 2 hd))
  12542. hd (concat (substring hd 0 (match-beginning 1))
  12543. (substring hd (- (match-end 1)))))
  12544. (setq pri org-default-priority))
  12545. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  12546. (- org-lowest-priority ?A))))))
  12547. (princ (format "BEGIN:VTODO
  12548. %s
  12549. SUMMARY:%s
  12550. SEQUENCE:1
  12551. PRIORITY:%d
  12552. END:VTODO\n"
  12553. dts hd pri))))))))
  12554. (defun org-start-icalendar-file (name)
  12555. "Start an iCalendar file by inserting the header."
  12556. (let ((user user-full-name)
  12557. (name (or name "unknown"))
  12558. (timezone (cadr (current-time-zone))))
  12559. (princ
  12560. (format "BEGIN:VCALENDAR
  12561. VERSION:2.0
  12562. X-WR-CALNAME:%s
  12563. PRODID:-//%s//Emacs with Org-mode//EN
  12564. X-WR-TIMEZONE:%s
  12565. CALSCALE:GREGORIAN\n" name user timezone))))
  12566. (defun org-finish-icalendar-file ()
  12567. "Finish an iCalendar file by inserting the END statement."
  12568. (princ "END:VCALENDAR\n"))
  12569. (defun org-ical-ts-to-string (s keyword &optional inc)
  12570. "Take a time string S and convert it to iCalendar format.
  12571. KEYWORD is added in front, to make a complete line like DTSTART....
  12572. When INC is non-nil, increase the hour by two (if time string contains
  12573. a time), or the day by one (if it does not contain a time)."
  12574. (let ((t1 (org-parse-time-string s 'nodefault))
  12575. t2 fmt have-time time)
  12576. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  12577. (setq t2 t1 have-time t)
  12578. (setq t2 (org-parse-time-string s)))
  12579. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  12580. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  12581. (when inc
  12582. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  12583. (setq time (encode-time s mi h d m y)))
  12584. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  12585. (concat keyword (format-time-string fmt time))))
  12586. ;;; Key bindings
  12587. ;; - Bindings in Org-mode map are currently
  12588. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  12589. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  12590. ;; e (?) useful from outline-mode
  12591. ;; i k @ expendable from outline-mode
  12592. ;; 0123456789 % & ()_{} " ` free
  12593. ;; Make `C-c C-x' a prefix key
  12594. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12595. (define-key org-mode-map "\C-c\C-e" (make-sparse-keymap))
  12596. ;; TAB key with modifiers
  12597. (define-key org-mode-map "\C-i" 'org-cycle)
  12598. (define-key org-mode-map [(tab)] 'org-cycle)
  12599. (define-key org-mode-map [(meta tab)] 'org-complete)
  12600. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  12601. ;; The following line is necessary under Suse GNU/Linux
  12602. (unless (featurep 'xemacs)
  12603. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  12604. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  12605. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  12606. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  12607. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12608. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  12609. (define-key org-mode-map [(meta return)] 'org-meta-return)
  12610. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  12611. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  12612. ;; Cursor keys with modifiers
  12613. (define-key org-mode-map [(meta left)] 'org-metaleft)
  12614. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  12615. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  12616. (define-key org-mode-map [(meta right)] 'org-metaright)
  12617. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  12618. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  12619. (define-key org-mode-map [(meta up)] 'org-metaup)
  12620. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  12621. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  12622. (define-key org-mode-map [(meta down)] 'org-metadown)
  12623. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  12624. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  12625. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12626. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  12627. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12628. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  12629. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12630. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  12631. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12632. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  12633. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  12634. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  12635. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  12636. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  12637. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  12638. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  12639. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  12640. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  12641. ;; All the other keys
  12642. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12643. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  12644. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  12645. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  12646. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  12647. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  12648. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  12649. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12650. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  12651. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  12652. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12653. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  12654. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  12655. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  12656. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  12657. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  12658. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  12659. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  12660. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12661. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12662. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12663. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12664. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  12665. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  12666. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12667. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12668. (define-key org-mode-map "\C-c]" 'org-remove-file)
  12669. (define-key org-mode-map "\C-c\C-r" 'org-timeline)
  12670. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  12671. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  12672. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12673. (define-key org-mode-map "\C-m" 'org-return)
  12674. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  12675. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  12676. (define-key org-mode-map "\C-c+" 'org-table-sum)
  12677. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  12678. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  12679. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  12680. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12681. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  12682. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12683. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12684. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12685. (define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
  12686. (define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
  12687. (define-key org-mode-map "\C-c\C-xv" 'org-export-visible)
  12688. (define-key org-mode-map "\C-c\C-x\C-v" 'org-export-visible)
  12689. ;; OPML support is only an option for the future
  12690. ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
  12691. ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
  12692. (define-key org-mode-map "\C-c\C-xi" 'org-export-icalendar-this-file)
  12693. (define-key org-mode-map "\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files)
  12694. (define-key org-mode-map "\C-c\C-xc" 'org-export-icalendar-combine-agenda-files)
  12695. (define-key org-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  12696. (define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
  12697. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12698. (define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
  12699. (define-key org-mode-map "\C-c\C-xx" 'org-export-as-xml)
  12700. (define-key org-mode-map "\C-c\C-x\C-x" 'org-export-as-xml)
  12701. (define-key org-mode-map "\C-c\C-xb" 'org-export-as-html-and-open)
  12702. (define-key org-mode-map "\C-c\C-x\C-b" 'org-export-as-html-and-open)
  12703. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12704. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12705. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12706. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12707. (define-key org-mode-map "\C-c\C-ef" 'org-publish-current-file)
  12708. (define-key org-mode-map "\C-c\C-ep" 'org-publish-current-project)
  12709. (define-key org-mode-map "\C-c\C-ec" 'org-publish)
  12710. (define-key org-mode-map "\C-c\C-ea" 'org-publish-all)
  12711. (define-key org-mode-map "\C-c\C-e\C-f" 'org-publish-current-file)
  12712. (define-key org-mode-map "\C-c\C-e\C-p" 'org-publish-current-project)
  12713. (define-key org-mode-map "\C-c\C-e\C-c" 'org-publish)
  12714. (define-key org-mode-map "\C-c\C-e\C-a" 'org-publish-all)
  12715. (when (featurep 'xemacs)
  12716. (define-key org-mode-map 'button3 'popup-mode-menu))
  12717. (defsubst org-table-p () (org-at-table-p))
  12718. (defun org-self-insert-command (N)
  12719. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12720. If the cursor is in a table looking at whitespace, the whitespace is
  12721. overwritten, and the table is not marked as requiring realignment."
  12722. (interactive "p")
  12723. (if (and (org-table-p)
  12724. (progn
  12725. ;; check if we blank the field, and if that triggers align
  12726. (and org-table-auto-blank-field
  12727. (member last-command
  12728. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12729. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12730. ;; got extra space, this field does not determine column width
  12731. (let (org-table-may-need-update) (org-table-blank-field))
  12732. ;; no extra space, this field may determine column width
  12733. (org-table-blank-field)))
  12734. t)
  12735. (eq N 1)
  12736. (looking-at "[^|\n]* |"))
  12737. (let (org-table-may-need-update)
  12738. (goto-char (1- (match-end 0)))
  12739. (delete-backward-char 1)
  12740. (goto-char (match-beginning 0))
  12741. (self-insert-command N))
  12742. (setq org-table-may-need-update t)
  12743. (self-insert-command N)))
  12744. (defun org-delete-backward-char (N)
  12745. "Like `delete-backward-char', insert whitespace at field end in tables.
  12746. When deleting backwards, in tables this function will insert whitespace in
  12747. front of the next \"|\" separator, to keep the table aligned. The table will
  12748. still be marked for re-alignment if the field did fill the entire column,
  12749. because, in this case the deletion might narrow the column."
  12750. (interactive "p")
  12751. (if (and (org-table-p)
  12752. (eq N 1)
  12753. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12754. (looking-at ".*?|"))
  12755. (let ((pos (point))
  12756. (noalign (looking-at "[^|\n\r]* |"))
  12757. (c org-table-may-need-update))
  12758. (backward-delete-char N)
  12759. (skip-chars-forward "^|")
  12760. (insert " ")
  12761. (goto-char (1- pos))
  12762. ;; noalign: if there were two spaces at the end, this field
  12763. ;; does not determine the width of the column.
  12764. (if noalign (setq org-table-may-need-update c)))
  12765. (backward-delete-char N)))
  12766. (defun org-delete-char (N)
  12767. "Like `delete-char', but insert whitespace at field end in tables.
  12768. When deleting characters, in tables this function will insert whitespace in
  12769. front of the next \"|\" separator, to keep the table aligned. The table will
  12770. still be marked for re-alignment if the field did fill the entire column,
  12771. because, in this case the deletion might narrow the column."
  12772. (interactive "p")
  12773. (if (and (org-table-p)
  12774. (not (bolp))
  12775. (not (= (char-after) ?|))
  12776. (eq N 1))
  12777. (if (looking-at ".*?|")
  12778. (let ((pos (point))
  12779. (noalign (looking-at "[^|\n\r]* |"))
  12780. (c org-table-may-need-update))
  12781. (replace-match (concat
  12782. (substring (match-string 0) 1 -1)
  12783. " |"))
  12784. (goto-char pos)
  12785. ;; noalign: if there were two spaces at the end, this field
  12786. ;; does not determine the width of the column.
  12787. (if noalign (setq org-table-may-need-update c)))
  12788. (delete-char N))
  12789. (delete-char N)))
  12790. ;; How to do this: Measure non-white length of current string
  12791. ;; If equal to column width, we should realign.
  12792. (defun org-remap (map &rest commands)
  12793. "In MAP, remap the functions given in COMMANDS.
  12794. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12795. (let (new old)
  12796. (while commands
  12797. (setq old (pop commands) new (pop commands))
  12798. (if (fboundp 'command-remapping)
  12799. (define-key map (vector 'remap old) new)
  12800. (substitute-key-definition old new map global-map)))))
  12801. (when (eq org-enable-table-editor 'optimized)
  12802. ;; If the user wants maximum table support, we need to hijack
  12803. ;; some standard editing functions
  12804. (org-remap org-mode-map
  12805. 'self-insert-command 'org-self-insert-command
  12806. 'delete-char 'org-delete-char
  12807. 'delete-backward-char 'org-delete-backward-char)
  12808. (define-key org-mode-map "|" 'org-force-self-insert))
  12809. (defun org-shiftcursor-error ()
  12810. "Throw an error because Shift-Cursor command was applied in wrong context."
  12811. (error "This command is active in special context like tables, headlines or timestamps"))
  12812. (defun org-shifttab ()
  12813. "Global visibility cycling or move to previous table field.
  12814. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12815. on context.
  12816. See the individual commands for more information."
  12817. (interactive)
  12818. (cond
  12819. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12820. (t (call-interactively 'org-global-cycle))))
  12821. (defun org-shiftmetaleft ()
  12822. "Promote subtree or delete table column.
  12823. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  12824. See the individual commands for more information."
  12825. (interactive)
  12826. (cond
  12827. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12828. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12829. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12830. (t (org-shiftcursor-error))))
  12831. (defun org-shiftmetaright ()
  12832. "Demote subtree or insert table column.
  12833. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  12834. See the individual commands for more information."
  12835. (interactive)
  12836. (cond
  12837. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12838. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12839. ((org-at-item-p) (call-interactively 'org-indent-item))
  12840. (t (org-shiftcursor-error))))
  12841. (defun org-shiftmetaup (&optional arg)
  12842. "Move subtree up or kill table row.
  12843. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12844. `org-move-item-up' depending on context. See the individual commands
  12845. for more information."
  12846. (interactive "P")
  12847. (cond
  12848. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12849. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12850. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12851. (t (org-shiftcursor-error))))
  12852. (defun org-shiftmetadown (&optional arg)
  12853. "Move subtree down or insert table row.
  12854. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12855. `org-move-item-down', depending on context. See the individual
  12856. commands for more information."
  12857. (interactive "P")
  12858. (cond
  12859. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12860. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12861. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12862. (t (org-shiftcursor-error))))
  12863. (defun org-metaleft (&optional arg)
  12864. "Promote heading or move table column to left.
  12865. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12866. With no specific context, calls the Emacs default `backward-word'.
  12867. See the individual commands for more information."
  12868. (interactive "P")
  12869. (cond
  12870. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12871. ((or (org-on-heading-p) (org-region-active-p))
  12872. (call-interactively 'org-do-promote))
  12873. (t (call-interactively 'backward-word))))
  12874. (defun org-metaright (&optional arg)
  12875. "Demote subtree or move table column to right.
  12876. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12877. With no specific context, calls the Emacs default `forward-word'.
  12878. See the individual commands for more information."
  12879. (interactive "P")
  12880. (cond
  12881. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12882. ((or (org-on-heading-p) (org-region-active-p))
  12883. (call-interactively 'org-do-demote))
  12884. (t (call-interactively 'forward-word))))
  12885. (defun org-metaup (&optional arg)
  12886. "Move subtree up or move table row up.
  12887. Calls `org-move-subtree-up' or `org-table-move-row' or
  12888. `org-move-item-up', depending on context. See the individual commands
  12889. for more information."
  12890. (interactive "P")
  12891. (cond
  12892. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12893. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12894. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12895. (t (org-shiftcursor-error))))
  12896. (defun org-metadown (&optional arg)
  12897. "Move subtree down or move table row down.
  12898. Calls `org-move-subtree-down' or `org-table-move-row' or
  12899. `org-move-item-down', depending on context. See the individual
  12900. commands for more information."
  12901. (interactive "P")
  12902. (cond
  12903. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12904. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12905. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12906. (t (org-shiftcursor-error))))
  12907. (defun org-shiftup (&optional arg)
  12908. "Increase item in timestamp or increase priority of current headline.
  12909. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  12910. See the individual commands for more information."
  12911. (interactive "P")
  12912. (cond
  12913. ((org-at-timestamp-p) (call-interactively 'org-timestamp-up))
  12914. ((org-on-heading-p) (call-interactively 'org-priority-up))
  12915. ((org-at-item-p) (call-interactively 'org-previous-item))
  12916. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  12917. (defun org-shiftdown (&optional arg)
  12918. "Decrease item in timestamp or decrease priority of current headline.
  12919. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  12920. See the individual commands for more information."
  12921. (interactive "P")
  12922. (cond
  12923. ((org-at-timestamp-p) (call-interactively 'org-timestamp-down))
  12924. ((org-on-heading-p) (call-interactively 'org-priority-down))
  12925. (t (call-interactively 'org-next-item))))
  12926. (defun org-shiftright ()
  12927. "Next TODO keyword or timestamp one day later, depending on context."
  12928. (interactive)
  12929. (cond
  12930. ((org-at-timestamp-p) (call-interactively 'org-timestamp-up-day))
  12931. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  12932. (t (org-shiftcursor-error))))
  12933. (defun org-shiftleft ()
  12934. "Previous TODO keyword or timestamp one day earlier, depending on context."
  12935. (interactive)
  12936. (cond
  12937. ((org-at-timestamp-p) (call-interactively 'org-timestamp-down-day))
  12938. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  12939. (t (org-shiftcursor-error))))
  12940. (defun org-copy-special ()
  12941. "Copy region in table or copy current subtree.
  12942. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12943. See the individual commands for more information."
  12944. (interactive)
  12945. (call-interactively
  12946. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12947. (defun org-cut-special ()
  12948. "Cut region in table or cut current subtree.
  12949. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12950. See the individual commands for more information."
  12951. (interactive)
  12952. (call-interactively
  12953. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12954. (defun org-paste-special (arg)
  12955. "Paste rectangular region into table, or past subtree relative to level.
  12956. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12957. See the individual commands for more information."
  12958. (interactive "P")
  12959. (if (org-at-table-p)
  12960. (org-table-paste-rectangle)
  12961. (org-paste-subtree arg)))
  12962. (defun org-ctrl-c-ctrl-c (&optional arg)
  12963. "Set tags in headline, or update according to changed information at point.
  12964. This command does many different things, depending on context:
  12965. - If the cursor is in a headline, prompt for tags and insert them
  12966. into the current line, aligned to `org-tags-column'. When called
  12967. with prefix arg, realign all tags in the current buffer.
  12968. - If the cursor is in one of the special #+KEYWORD lines, this
  12969. triggers scanning the buffer for these lines and updating the
  12970. information.
  12971. - If the cursor is inside a table, realign the table. This command
  12972. works even if the automatic table editor has been turned off.
  12973. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12974. the entire table.
  12975. - If the cursor is inside a table created by the table.el package,
  12976. activate that table.
  12977. - If the current buffer is a remember buffer, close note and file it.
  12978. with a prefix argument, file it without further interaction to the default
  12979. location.
  12980. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12981. links in this buffer.
  12982. - If the cursor is on a numbered item in a plain list, renumber the
  12983. ordered list."
  12984. (interactive "P")
  12985. (let ((org-enable-table-editor t))
  12986. (cond
  12987. ((and (local-variable-p 'org-finish-function (current-buffer))
  12988. (fboundp org-finish-function))
  12989. (funcall org-finish-function))
  12990. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12991. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12992. ((org-at-table.el-p)
  12993. (require 'table)
  12994. (beginning-of-line 1)
  12995. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12996. (call-interactively 'table-recognize-table))
  12997. ((org-at-table-p)
  12998. (org-table-maybe-eval-formula)
  12999. (if arg
  13000. (call-interactively 'org-table-recalculate)
  13001. (org-table-maybe-recalculate-line))
  13002. (call-interactively 'org-table-align))
  13003. ((org-at-item-checkbox-p)
  13004. (call-interactively 'org-toggle-checkbox))
  13005. ((org-at-item-p)
  13006. (call-interactively 'org-renumber-ordered-list))
  13007. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13008. (cond
  13009. ((equal (match-string 1) "TBLFM")
  13010. ;; Recalculate the table before this line
  13011. (save-excursion
  13012. (beginning-of-line 1)
  13013. (skip-chars-backward " \r\n\t")
  13014. (if (org-at-table-p)
  13015. (org-call-with-arg 'org-table-recalculate t))))
  13016. (t
  13017. (call-interactively 'org-mode-restart))))
  13018. (t (error "C-c C-c can do nothing useful at this location.")))))
  13019. (defun org-mode-restart ()
  13020. "Restart Org-mode, to scan again for special lines.
  13021. Also updates the keyword regular expressions."
  13022. (interactive)
  13023. (let ((org-inhibit-startup t)) (org-mode))
  13024. (message "Org-mode restarted to refresh keyword and special line setup"))
  13025. (defun org-return ()
  13026. "Goto next table row or insert a newline.
  13027. Calls `org-table-next-row' or `newline', depending on context.
  13028. See the individual commands for more information."
  13029. (interactive)
  13030. (cond
  13031. ((org-at-table-p)
  13032. (org-table-justify-field-maybe)
  13033. (call-interactively 'org-table-next-row))
  13034. (t (newline))))
  13035. (defun org-meta-return (&optional arg)
  13036. "Insert a new heading or wrap a region in a table.
  13037. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13038. See the individual commands for more information."
  13039. (interactive "P")
  13040. (cond
  13041. ((org-at-table-p)
  13042. (call-interactively 'org-table-wrap-region))
  13043. (t (call-interactively 'org-insert-heading))))
  13044. ;;; Menu entries
  13045. ;; Define the Org-mode menus
  13046. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13047. '("Tbl"
  13048. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  13049. ["Next Field" org-cycle (org-at-table-p)]
  13050. ["Previous Field" org-shifttab (org-at-table-p)]
  13051. ["Next Row" org-return (org-at-table-p)]
  13052. "--"
  13053. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13054. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13055. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13056. "--"
  13057. ("Column"
  13058. ["Move Column Left" org-metaleft (org-at-table-p)]
  13059. ["Move Column Right" org-metaright (org-at-table-p)]
  13060. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13061. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  13062. "--"
  13063. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  13064. ("Row"
  13065. ["Move Row Up" org-metaup (org-at-table-p)]
  13066. ["Move Row Down" org-metadown (org-at-table-p)]
  13067. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13068. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13069. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13070. "--"
  13071. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  13072. ("Rectangle"
  13073. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13074. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13075. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13076. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13077. "--"
  13078. ("Calculate"
  13079. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13080. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13081. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  13082. "--"
  13083. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13084. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13085. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13086. "--"
  13087. ["Sum Column/Rectangle" org-table-sum
  13088. (or (org-at-table-p) (org-region-active-p))]
  13089. ["Which Column?" org-table-current-column (org-at-table-p)])
  13090. ["Debug Formulas"
  13091. (setq org-table-formula-debug (not org-table-formula-debug))
  13092. :style toggle :selected org-table-formula-debug]
  13093. "--"
  13094. ["Create" org-table-create (and (not (org-at-table-p))
  13095. org-enable-table-editor)]
  13096. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13097. ["Import from File" org-table-import (not (org-at-table-p))]
  13098. ["Export to File" org-table-export (org-at-table-p)]
  13099. "--"
  13100. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13101. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13102. '("Org"
  13103. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  13104. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  13105. ["Sparse Tree" org-occur t]
  13106. ["Show All" show-all t]
  13107. "--"
  13108. ["New Heading" org-insert-heading t]
  13109. ("Navigate Headings"
  13110. ["Up" outline-up-heading t]
  13111. ["Next" outline-next-visible-heading t]
  13112. ["Previous" outline-previous-visible-heading t]
  13113. ["Next Same Level" outline-forward-same-level t]
  13114. ["Previous Same Level" outline-backward-same-level t]
  13115. "--"
  13116. ["Jump" org-goto t])
  13117. ("Edit Structure"
  13118. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13119. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13120. "--"
  13121. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13122. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13123. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13124. "--"
  13125. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13126. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13127. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13128. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13129. "--"
  13130. ["Archive Subtree" org-archive-subtree t]
  13131. "--"
  13132. ["Convert to odd levels" org-convert-to-odd-levels t]
  13133. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13134. "--"
  13135. ("TODO Lists"
  13136. ["TODO/DONE/-" org-todo t]
  13137. ["Show TODO Tree" org-show-todo-tree t]
  13138. ["Global TODO list" org-todo-list t]
  13139. "--"
  13140. ["Set Priority" org-priority t]
  13141. ["Priority Up" org-shiftup t]
  13142. ["Priority Down" org-shiftdown t])
  13143. ("Dates and Scheduling"
  13144. ["Timestamp" org-time-stamp t]
  13145. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13146. ("Change Date"
  13147. ["1 Day Later" org-timestamp-up-day t]
  13148. ["1 Day Earlier" org-timestamp-down-day t]
  13149. ["1 ... Later" org-shiftup t]
  13150. ["1 ... Earlier" org-shiftdown t])
  13151. ["Compute Time Range" org-evaluate-time-range t]
  13152. ["Schedule Item" org-schedule t]
  13153. ["Deadline" org-deadline t]
  13154. "--"
  13155. ["Goto Calendar" org-goto-calendar t]
  13156. ["Date from Calendar" org-date-from-calendar t])
  13157. "--"
  13158. ["Agenda Command" org-agenda t]
  13159. ("File List for Agenda")
  13160. ("Special views current file"
  13161. ["TODO Tree" org-show-todo-tree t]
  13162. ["Check Deadlines" org-check-deadlines t]
  13163. ["Timeline" org-timeline t]
  13164. ["Tags Tree" org-tags-sparse-tree t])
  13165. "--"
  13166. ("Hyperlinks"
  13167. ["Store Link (Global)" org-store-link t]
  13168. ["Insert Link" org-insert-link t]
  13169. ["Follow Link" org-open-at-point t]
  13170. "--"
  13171. ["Descriptive Links"
  13172. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13173. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  13174. ["Literal Links"
  13175. (progn
  13176. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13177. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  13178. "--"
  13179. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  13180. (save-excursion (goto-char (point-min))
  13181. (re-search-forward "<[a-z]+:" nil t))])
  13182. "--"
  13183. ("Export"
  13184. ["ASCII" org-export-as-ascii t]
  13185. ["Export visible part..." org-export-visible t]
  13186. ["HTML" org-export-as-html t]
  13187. ["HTML and Open" org-export-as-html-and-open t]
  13188. ["XOXO" org-export-as-xml t]
  13189. "--"
  13190. ["iCalendar this file" org-export-icalendar-this-file t]
  13191. ["iCalendar all agenda files" org-export-icalendar-all-agenda-files
  13192. :active t :keys "C-c C-x C-i"]
  13193. ["iCalendar combined" org-export-icalendar-combine-agenda-files t]
  13194. "--"
  13195. ["Option Template" org-insert-export-options-template t]
  13196. ["Toggle Fixed Width" org-toggle-fixed-width-section t])
  13197. ("Publish"
  13198. ["Current File" org-publish-current-file t]
  13199. ["Current Project" org-publish-current-project t]
  13200. ["Project..." org-publish t]
  13201. ["All Projects" org-publish-all t])
  13202. "--"
  13203. ("Documentation"
  13204. ["Show Version" org-version t]
  13205. ["Info Documentation" org-info t])
  13206. ("Customize"
  13207. ["Browse Org Group" org-customize t]
  13208. "--"
  13209. ["Expand This Menu" org-create-customize-menu
  13210. (fboundp 'customize-menu-create)])
  13211. "--"
  13212. ["Refresh setup" org-mode-restart t]
  13213. ))
  13214. (defun org-info (&optional node)
  13215. "Read documentation for Org-mode in the info system.
  13216. With optional NODE, go directly to that node."
  13217. (interactive)
  13218. (require 'info)
  13219. (Info-goto-node (format "(org)%s" (or node ""))))
  13220. (defun org-install-agenda-files-menu ()
  13221. (let ((bl (buffer-list)))
  13222. (save-excursion
  13223. (while bl
  13224. (set-buffer (pop bl))
  13225. (if (eq major-mode 'org-mode) (setq bl nil)))
  13226. (when (eq major-mode 'org-mode)
  13227. (easy-menu-change
  13228. '("Org") "File List for Agenda"
  13229. (append
  13230. (list
  13231. ["Edit File List" (org-edit-agenda-file-list) t]
  13232. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13233. ["Remove Current File from List" org-remove-file t]
  13234. ["Cycle through agenda files" org-cycle-agenda-files t]
  13235. "--")
  13236. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13237. ;;; Documentation
  13238. (defun org-customize ()
  13239. "Call the customize function with org as argument."
  13240. (interactive)
  13241. (customize-browse 'org))
  13242. (defun org-create-customize-menu ()
  13243. "Create a full customization menu for Org-mode, insert it into the menu."
  13244. (interactive)
  13245. (if (fboundp 'customize-menu-create)
  13246. (progn
  13247. (easy-menu-change
  13248. '("Org") "Customize"
  13249. `(["Browse Org group" org-customize t]
  13250. "--"
  13251. ,(customize-menu-create 'org)
  13252. ["Set" Custom-set t]
  13253. ["Save" Custom-save t]
  13254. ["Reset to Current" Custom-reset-current t]
  13255. ["Reset to Saved" Custom-reset-saved t]
  13256. ["Reset to Standard Settings" Custom-reset-standard t]))
  13257. (message "\"Org\"-menu now contains full customization menu"))
  13258. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13259. ;;; Miscellaneous stuff
  13260. (defun org-move-line-down (arg)
  13261. "Move the current line down. With prefix argument, move it past ARG lines."
  13262. (interactive "p")
  13263. (let ((col (current-column))
  13264. beg end pos)
  13265. (beginning-of-line 1) (setq beg (point))
  13266. (beginning-of-line 2) (setq end (point))
  13267. (beginning-of-line (+ 1 arg))
  13268. (setq pos (move-marker (make-marker) (point)))
  13269. (insert (delete-and-extract-region beg end))
  13270. (goto-char pos)
  13271. (move-to-column col)))
  13272. (defun org-move-line-up (arg)
  13273. "Move the current line up. With prefix argument, move it past ARG lines."
  13274. (interactive "p")
  13275. (let ((col (current-column))
  13276. beg end pos)
  13277. (beginning-of-line 1) (setq beg (point))
  13278. (beginning-of-line 2) (setq end (point))
  13279. (beginning-of-line (- arg))
  13280. (setq pos (move-marker (make-marker) (point)))
  13281. (insert (delete-and-extract-region beg end))
  13282. (goto-char pos)
  13283. (move-to-column col)))
  13284. ;; Paragraph filling stuff.
  13285. ;; We want this to be just right, so use the full arsenal.
  13286. ;; FIXME: This very likely does not work correctly for XEmacs, because the
  13287. ;; filladapt package works slightly differently.
  13288. (defun org-set-autofill-regexps ()
  13289. (interactive)
  13290. ;; In the paragraph separator we include headlines, because filling
  13291. ;; text in a line directly attached to a headline would otherwise
  13292. ;; fill the headline as well.
  13293. (set (make-local-variable 'paragraph-separate) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  13294. ;; The paragraph starter includes hand-formatted lists.
  13295. (set (make-local-variable 'paragraph-start)
  13296. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13297. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13298. ;; But only if the user has not turned off tables or fixed-width regions
  13299. (set (make-local-variable 'auto-fill-inhibit-regexp)
  13300. (concat "\\*\\|#"
  13301. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13302. (concat
  13303. "\\|[ \t]*["
  13304. (if org-enable-table-editor "|" "")
  13305. (if org-enable-fixed-width-editor ":" "")
  13306. "]"))))
  13307. ;; We use our own fill-paragraph function, to make sure that tables
  13308. ;; and fixed-width regions are not wrapped. That function will pass
  13309. ;; through to `fill-paragraph' when appropriate.
  13310. (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
  13311. ;; Adaptive filling: To get full control, first make sure that
  13312. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13313. (set (make-local-variable 'adaptive-fill-regexp) "\000")
  13314. (set (make-local-variable 'adaptive-fill-function)
  13315. 'org-adaptive-fill-function))
  13316. (defun org-fill-paragraph (&optional justify)
  13317. "Re-align a table, pass through to fill-paragraph if no table."
  13318. (let ((table-p (org-at-table-p))
  13319. (table.el-p (org-at-table.el-p)))
  13320. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  13321. (table.el-p t) ; skip table.el tables
  13322. (table-p (org-table-align) t) ; align org-mode tables
  13323. (t nil)))) ; call paragraph-fill
  13324. ;; For reference, this is the default value of adaptive-fill-regexp
  13325. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13326. (defun org-adaptive-fill-function ()
  13327. "Return a fill prefix for org-mode files.
  13328. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13329. work correctly."
  13330. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  13331. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  13332. ;; Functions needed for Emacs/XEmacs region compatibility
  13333. (defun org-add-hook (hook function &optional append local)
  13334. "Add-hook, compatible with both Emacsen."
  13335. (if (and local (featurep 'xemacs))
  13336. (add-local-hook hook function append)
  13337. (add-hook hook function append local)))
  13338. (defun org-region-active-p ()
  13339. "Is `transient-mark-mode' on and the region active?
  13340. Works on both Emacs and XEmacs."
  13341. (if org-ignore-region
  13342. nil
  13343. (if (featurep 'xemacs)
  13344. (and zmacs-regions (region-active-p))
  13345. (and transient-mark-mode mark-active))))
  13346. (defun org-add-to-invisibility-spec (arg)
  13347. "Add elements to `buffer-invisibility-spec'.
  13348. See documentation for `buffer-invisibility-spec' for the kind of elements
  13349. that can be added."
  13350. (cond
  13351. ((fboundp 'add-to-invisibility-spec)
  13352. (add-to-invisibility-spec arg))
  13353. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  13354. (setq buffer-invisibility-spec (list arg)))
  13355. (t
  13356. (setq buffer-invisibility-spec
  13357. (cons arg buffer-invisibility-spec)))))
  13358. (defun org-remove-from-invisibility-spec (arg)
  13359. "Remove elements from `buffer-invisibility-spec'."
  13360. (if (fboundp 'remove-from-invisibility-spec)
  13361. (remove-from-invisibility-spec arg)
  13362. (if (consp buffer-invisibility-spec)
  13363. (setq buffer-invisibility-spec
  13364. (delete arg buffer-invisibility-spec)))))
  13365. (defun org-in-invisibility-spec-p (arg)
  13366. "Is ARG a member of `buffer-invisibility-spec'?"
  13367. (if (consp buffer-invisibility-spec)
  13368. (member arg buffer-invisibility-spec)
  13369. nil))
  13370. (defun org-image-file-name-regexp ()
  13371. "Return regexp matching the file names of images."
  13372. (if (fboundp 'image-file-name-regexp)
  13373. (image-file-name-regexp)
  13374. (let ((image-file-name-extensions
  13375. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13376. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13377. (concat "\\."
  13378. (regexp-opt (nconc (mapcar 'upcase
  13379. image-file-name-extensions)
  13380. image-file-name-extensions)
  13381. t)
  13382. "\\'"))))
  13383. ;; Functions needed for compatibility with old outline.el.
  13384. ;; Programming for the old outline.el (that uses selective display
  13385. ;; instead of `invisible' text properties) is a nightmare, mostly
  13386. ;; because regular expressions can no longer be anchored at
  13387. ;; beginning/end of line. Therefore a number of function need special
  13388. ;; treatment when the old outline.el is being used.
  13389. ;; The following functions capture almost the entire compatibility code
  13390. ;; between the different versions of outline-mode. The only other
  13391. ;; places where this is important are the font-lock-keywords, and in
  13392. ;; `org-export-visible'. Search for `org-noutline-p' to find them.
  13393. ;; C-a should go to the beginning of a *visible* line, also in the
  13394. ;; new outline.el. I guess this should be patched into Emacs?
  13395. (defun org-beginning-of-line ()
  13396. "Go to the beginning of the current line. If that is invisible, continue
  13397. to a visible line beginning. This makes the function of C-a more intuitive."
  13398. (interactive)
  13399. (beginning-of-line 1)
  13400. (if (bobp)
  13401. nil
  13402. (backward-char 1)
  13403. (if (org-invisible-p)
  13404. (while (and (not (bobp)) (org-invisible-p))
  13405. (backward-char 1)
  13406. (beginning-of-line 1))
  13407. (forward-char 1))))
  13408. (when org-noutline-p
  13409. (define-key org-mode-map "\C-a" 'org-beginning-of-line))
  13410. ;; FIXME: should I use substitute-key-definition to reach other bindings
  13411. ;; of beginning-of-line?
  13412. (defun org-invisible-p ()
  13413. "Check if point is at a character currently not visible."
  13414. (if org-noutline-p
  13415. ;; Early versions of noutline don't have `outline-invisible-p'.
  13416. (if (fboundp 'outline-invisible-p)
  13417. (outline-invisible-p)
  13418. (get-char-property (point) 'invisible))
  13419. (save-excursion
  13420. (skip-chars-backward "^\r\n")
  13421. (equal (char-before) ?\r))))
  13422. (defun org-invisible-p2 ()
  13423. "Check if point is at a character currently not visible."
  13424. (save-excursion
  13425. (if org-noutline-p
  13426. (progn
  13427. (if (and (eolp) (not (bobp))) (backward-char 1))
  13428. ;; Early versions of noutline don't have `outline-invisible-p'.
  13429. (if (fboundp 'outline-invisible-p)
  13430. (outline-invisible-p)
  13431. (get-char-property (point) 'invisible)))
  13432. (skip-chars-backward "^\r\n")
  13433. (equal (char-before) ?\r))))
  13434. (defun org-back-to-heading (&optional invisible-ok)
  13435. "Move to previous heading line, or beg of this line if it's a heading.
  13436. Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
  13437. (if org-noutline-p
  13438. (outline-back-to-heading invisible-ok)
  13439. (if (and (or (bobp) (memq (char-before) '(?\n ?\r)))
  13440. (looking-at outline-regexp))
  13441. t
  13442. (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
  13443. outline-regexp)
  13444. nil t)
  13445. (if invisible-ok
  13446. (progn (goto-char (or (match-end 1) (match-beginning 0)))
  13447. (looking-at outline-regexp)))
  13448. (error "Before first heading")))))
  13449. (defun org-on-heading-p (&optional invisible-ok)
  13450. "Return t if point is on a (visible) heading line.
  13451. If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
  13452. (if org-noutline-p
  13453. (outline-on-heading-p 'invisible-ok)
  13454. (save-excursion
  13455. (skip-chars-backward "^\n\r")
  13456. (and (looking-at outline-regexp)
  13457. (or invisible-ok
  13458. (bobp)
  13459. (equal (char-before) ?\n))))))
  13460. (defun org-on-target-p ()
  13461. (let ((pos (point)))
  13462. (save-excursion
  13463. (skip-chars-forward "<")
  13464. (and (re-search-backward "<<" nil t)
  13465. (or (looking-at org-radio-target-regexp)
  13466. (looking-at org-target-regexp))
  13467. (<= (match-beginning 0) pos)
  13468. (>= (1+ (match-end 0)) pos)))))
  13469. (defun org-up-heading-all (arg)
  13470. "Move to the heading line of which the present line is a subheading.
  13471. This function considers both visible and invisible heading lines.
  13472. With argument, move up ARG levels."
  13473. (if org-noutline-p
  13474. (if (fboundp 'outline-up-heading-all)
  13475. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13476. (outline-up-heading arg t)) ; emacs 22 version of outline.el
  13477. (org-back-to-heading t)
  13478. (looking-at outline-regexp)
  13479. (if (<= (- (match-end 0) (match-beginning 0)) arg)
  13480. (error "Cannot move up %d levels" arg)
  13481. (re-search-backward
  13482. (concat "[\n\r]" (regexp-quote
  13483. (make-string (- (match-end 0) (match-beginning 0) arg)
  13484. ?*))
  13485. "[^*]"))
  13486. (forward-char 1))))
  13487. (defun org-show-hidden-entry ()
  13488. "Show an entry where even the heading is hidden."
  13489. (save-excursion
  13490. (if (not org-noutline-p)
  13491. (progn
  13492. (org-back-to-heading t)
  13493. (org-flag-heading nil)))
  13494. (org-show-entry)))
  13495. (defun org-check-occur-regexp (regexp)
  13496. "If REGEXP starts with \"^\", modify it to check for \\r as well.
  13497. Of course, only for the old outline mode."
  13498. (if org-noutline-p
  13499. regexp
  13500. (if (string-match "^\\^" regexp)
  13501. (concat "[\n\r]" (substring regexp 1))
  13502. regexp)))
  13503. (defun org-flag-heading (flag &optional entry)
  13504. "Flag the current heading. FLAG non-nil means make invisible.
  13505. When ENTRY is non-nil, show the entire entry."
  13506. (save-excursion
  13507. (org-back-to-heading t)
  13508. (if (not org-noutline-p)
  13509. ;; Make the current headline visible
  13510. (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
  13511. ;; Check if we should show the entire entry
  13512. (if entry
  13513. (progn
  13514. (org-show-entry)
  13515. (save-excursion
  13516. (and (outline-next-heading)
  13517. (org-flag-heading nil))))
  13518. (outline-flag-region (max 1 (1- (point)))
  13519. (save-excursion (outline-end-of-heading) (point))
  13520. (if org-noutline-p
  13521. flag
  13522. (if flag ?\r ?\n))))))
  13523. (defun org-end-of-subtree (&optional invisible-OK)
  13524. ;; This is an exact copy of the original function, but it uses
  13525. ;; `org-back-to-heading', to make it work also in invisible
  13526. ;; trees. And is uses an invisible-OK argument.
  13527. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13528. (org-back-to-heading invisible-OK)
  13529. (let ((first t)
  13530. (level (funcall outline-level)))
  13531. (while (and (not (eobp))
  13532. (or first (> (funcall outline-level) level)))
  13533. (setq first nil)
  13534. (outline-next-heading))
  13535. (if (memq (preceding-char) '(?\n ?\^M))
  13536. (progn
  13537. ;; Go to end of line before heading
  13538. (forward-char -1)
  13539. (if (memq (preceding-char) '(?\n ?\^M))
  13540. ;; leave blank line before heading
  13541. (forward-char -1))))))
  13542. (defun org-show-subtree ()
  13543. "Show everything after this heading at deeper levels."
  13544. (outline-flag-region
  13545. (point)
  13546. (save-excursion
  13547. (outline-end-of-subtree) (outline-next-heading) (point))
  13548. (if org-noutline-p nil ?\n)))
  13549. (defun org-show-entry ()
  13550. "Show the body directly following this heading.
  13551. Show the heading too, if it is currently invisible."
  13552. (interactive)
  13553. (save-excursion
  13554. (org-back-to-heading t)
  13555. (outline-flag-region
  13556. (max 1 (1- (point)))
  13557. (save-excursion
  13558. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13559. (or (match-beginning 1) (point-max)))
  13560. (if org-noutline-p nil ?\n))))
  13561. (defun org-make-options-regexp (kwds)
  13562. "Make a regular expression for keyword lines."
  13563. (concat
  13564. (if org-noutline-p "^" "[\n\r]")
  13565. "#?[ \t]*\\+\\("
  13566. (mapconcat 'regexp-quote kwds "\\|")
  13567. "\\):[ \t]*"
  13568. (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
  13569. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13570. (eval-after-load "bookmark"
  13571. '(if (boundp 'bookmark-after-jump-hook)
  13572. ;; We can use the hook
  13573. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13574. ;; Hook not available, use advice
  13575. (defadvice bookmark-jump (after org-make-visible activate)
  13576. "Make the position visible."
  13577. (org-bookmark-jump-unhide))))
  13578. (defun org-bookmark-jump-unhide ()
  13579. "Unhide the current position, to show the bookmark location."
  13580. (and (eq major-mode 'org-mode)
  13581. (or (org-invisible-p)
  13582. (save-excursion (goto-char (max (point-min) (1- (point))))
  13583. (org-invisible-p)))
  13584. (org-show-hierarchy-above)))
  13585. ;;; Finish up
  13586. (provide 'org)
  13587. (run-hooks 'org-load-hook)
  13588. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13589. ;;; org.el ends here
  13590. (defun test ()
  13591. ""
  13592. (interactive)
  13593. (org-up-heading-all 1))