org.el 534 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.11pre01
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.11pre01"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-default-extensions'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global id's for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " mouse: Additional mouse support" org-mouse)
  155. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  156. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  157. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  158. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  159. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  160. (const :tag "C eval: Include command output as text" org-eval)
  161. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  162. (const :tag "C id: Global id's for identifying entries" org-id)
  163. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  164. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  165. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  166. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  167. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  168. (const :tag "C registry: A registry for Org links" org-registry)
  169. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  170. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  171. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  172. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  173. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  174. (defgroup org-startup nil
  175. "Options concerning startup of Org-mode."
  176. :tag "Org Startup"
  177. :group 'org)
  178. (defcustom org-startup-folded t
  179. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  180. This can also be configured on a per-file basis by adding one of
  181. the following lines anywhere in the buffer:
  182. #+STARTUP: fold
  183. #+STARTUP: nofold
  184. #+STARTUP: content"
  185. :group 'org-startup
  186. :type '(choice
  187. (const :tag "nofold: show all" nil)
  188. (const :tag "fold: overview" t)
  189. (const :tag "content: all headlines" content)))
  190. (defcustom org-startup-truncated t
  191. "Non-nil means, entering Org-mode will set `truncate-lines'.
  192. This is useful since some lines containing links can be very long and
  193. uninteresting. Also tables look terrible when wrapped."
  194. :group 'org-startup
  195. :type 'boolean)
  196. (defcustom org-startup-align-all-tables nil
  197. "Non-nil means, align all tables when visiting a file.
  198. This is useful when the column width in tables is forced with <N> cookies
  199. in table fields. Such tables will look correct only after the first re-align.
  200. This can also be configured on a per-file basis by adding one of
  201. the following lines anywhere in the buffer:
  202. #+STARTUP: align
  203. #+STARTUP: noalign"
  204. :group 'org-startup
  205. :type 'boolean)
  206. (defcustom org-insert-mode-line-in-empty-file nil
  207. "Non-nil means insert the first line setting Org-mode in empty files.
  208. When the function `org-mode' is called interactively in an empty file, this
  209. normally means that the file name does not automatically trigger Org-mode.
  210. To ensure that the file will always be in Org-mode in the future, a
  211. line enforcing Org-mode will be inserted into the buffer, if this option
  212. has been set."
  213. :group 'org-startup
  214. :type 'boolean)
  215. (defcustom org-replace-disputed-keys nil
  216. "Non-nil means use alternative key bindings for some keys.
  217. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  218. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  219. If you want to use Org-mode together with one of these other modes,
  220. or more generally if you would like to move some Org-mode commands to
  221. other keys, set this variable and configure the keys with the variable
  222. `org-disputed-keys'.
  223. This option is only relevant at load-time of Org-mode, and must be set
  224. *before* org.el is loaded. Changing it requires a restart of Emacs to
  225. become effective."
  226. :group 'org-startup
  227. :type 'boolean)
  228. (defcustom org-use-extra-keys nil
  229. "Non-nil means use extra key sequence definitions for certain
  230. commands. This happens automatically if you run XEmacs or if
  231. window-system is nil. This variable lets you do the same
  232. manually. You must set it before loading org.
  233. Example: on Carbon Emacs 22 running graphically, with an external
  234. keyboard on a Powerbook, the default way of setting M-left might
  235. not work for either Alt or ESC. Setting this variable will make
  236. it work for ESC."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (if (fboundp 'defvaralias)
  240. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  241. (defcustom org-disputed-keys
  242. '(([(shift up)] . [(meta p)])
  243. ([(shift down)] . [(meta n)])
  244. ([(shift left)] . [(meta -)])
  245. ([(shift right)] . [(meta +)])
  246. ([(control shift right)] . [(meta shift +)])
  247. ([(control shift left)] . [(meta shift -)]))
  248. "Keys for which Org-mode and other modes compete.
  249. This is an alist, cars are the default keys, second element specifies
  250. the alternative to use when `org-replace-disputed-keys' is t.
  251. Keys can be specified in any syntax supported by `define-key'.
  252. The value of this option takes effect only at Org-mode's startup,
  253. therefore you'll have to restart Emacs to apply it after changing."
  254. :group 'org-startup
  255. :type 'alist)
  256. (defun org-key (key)
  257. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  258. Or return the original if not disputed."
  259. (if org-replace-disputed-keys
  260. (let* ((nkey (key-description key))
  261. (x (org-find-if (lambda (x)
  262. (equal (key-description (car x)) nkey))
  263. org-disputed-keys)))
  264. (if x (cdr x) key))
  265. key))
  266. (defun org-find-if (predicate seq)
  267. (catch 'exit
  268. (while seq
  269. (if (funcall predicate (car seq))
  270. (throw 'exit (car seq))
  271. (pop seq)))))
  272. (defun org-defkey (keymap key def)
  273. "Define a key, possibly translated, as returned by `org-key'."
  274. (define-key keymap (org-key key) def))
  275. (defcustom org-ellipsis nil
  276. "The ellipsis to use in the Org-mode outline.
  277. When nil, just use the standard three dots. When a string, use that instead,
  278. When a face, use the standart 3 dots, but with the specified face.
  279. The change affects only Org-mode (which will then use its own display table).
  280. Changing this requires executing `M-x org-mode' in a buffer to become
  281. effective."
  282. :group 'org-startup
  283. :type '(choice (const :tag "Default" nil)
  284. (face :tag "Face" :value org-warning)
  285. (string :tag "String" :value "...#")))
  286. (defvar org-display-table nil
  287. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  288. (defgroup org-keywords nil
  289. "Keywords in Org-mode."
  290. :tag "Org Keywords"
  291. :group 'org)
  292. (defcustom org-deadline-string "DEADLINE:"
  293. "String to mark deadline entries.
  294. A deadline is this string, followed by a time stamp. Should be a word,
  295. terminated by a colon. You can insert a schedule keyword and
  296. a timestamp with \\[org-deadline].
  297. Changes become only effective after restarting Emacs."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-scheduled-string "SCHEDULED:"
  301. "String to mark scheduled TODO entries.
  302. A schedule is this string, followed by a time stamp. Should be a word,
  303. terminated by a colon. You can insert a schedule keyword and
  304. a timestamp with \\[org-schedule].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-closed-string "CLOSED:"
  309. "String used as the prefix for timestamps logging closing a TODO entry."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-clock-string "CLOCK:"
  313. "String used as prefix for timestamps clocking work hours on an item."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-comment-string "COMMENT"
  317. "Entries starting with this keyword will never be exported.
  318. An entry can be toggled between COMMENT and normal with
  319. \\[org-toggle-comment].
  320. Changes become only effective after restarting Emacs."
  321. :group 'org-keywords
  322. :type 'string)
  323. (defcustom org-quote-string "QUOTE"
  324. "Entries starting with this keyword will be exported in fixed-width font.
  325. Quoting applies only to the text in the entry following the headline, and does
  326. not extend beyond the next headline, even if that is lower level.
  327. An entry can be toggled between QUOTE and normal with
  328. \\[org-toggle-fixed-width-section]."
  329. :group 'org-keywords
  330. :type 'string)
  331. (defconst org-repeat-re
  332. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  333. "Regular expression for specifying repeated events.
  334. After a match, group 1 contains the repeat expression.")
  335. (defgroup org-structure nil
  336. "Options concerning the general structure of Org-mode files."
  337. :tag "Org Structure"
  338. :group 'org)
  339. (defgroup org-reveal-location nil
  340. "Options about how to make context of a location visible."
  341. :tag "Org Reveal Location"
  342. :group 'org-structure)
  343. (defconst org-context-choice
  344. '(choice
  345. (const :tag "Always" t)
  346. (const :tag "Never" nil)
  347. (repeat :greedy t :tag "Individual contexts"
  348. (cons
  349. (choice :tag "Context"
  350. (const agenda)
  351. (const org-goto)
  352. (const occur-tree)
  353. (const tags-tree)
  354. (const link-search)
  355. (const mark-goto)
  356. (const bookmark-jump)
  357. (const isearch)
  358. (const default))
  359. (boolean))))
  360. "Contexts for the reveal options.")
  361. (defcustom org-show-hierarchy-above '((default . t))
  362. "Non-nil means, show full hierarchy when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the hierarchy of headings
  365. above the exposed location is shown.
  366. Turning this off for example for sparse trees makes them very compact.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. Valid contexts are
  369. agenda when exposing an entry from the agenda
  370. org-goto when using the command `org-goto' on key C-c C-j
  371. occur-tree when using the command `org-occur' on key C-c /
  372. tags-tree when constructing a sparse tree based on tags matches
  373. link-search when exposing search matches associated with a link
  374. mark-goto when exposing the jump goal of a mark
  375. bookmark-jump when exposing a bookmark location
  376. isearch when exiting from an incremental search
  377. default default for all contexts not set explicitly"
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-following-heading '((default . nil))
  381. "Non-nil means, show following heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the heading following the
  384. match is shown.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-siblings '((default . nil) (isearch t))
  393. "Non-nil means, show all sibling heading when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the sibling of the current entry
  396. heading are all made visible. If `org-show-hierarchy-above' is t,
  397. the same happens on each level of the hierarchy above the current entry.
  398. By default this is on for the isearch context, off for all other contexts.
  399. Turning this off for example for sparse trees makes them very compact,
  400. but makes it harder to edit the location of the match. In such a case,
  401. use the command \\[org-reveal] to show more context.
  402. Instead of t, this can also be an alist specifying this option for different
  403. contexts. See `org-show-hierarchy-above' for valid contexts."
  404. :group 'org-reveal-location
  405. :type org-context-choice)
  406. (defcustom org-show-entry-below '((default . nil))
  407. "Non-nil means, show the entry below a headline when revealing a location.
  408. Org-mode often shows locations in an org-mode file which might have
  409. been invisible before. When this is set, the text below the headline that is
  410. exposed is also shown.
  411. By default this is off for all contexts.
  412. Instead of t, this can also be an alist specifying this option for different
  413. contexts. See `org-show-hierarchy-above' for valid contexts."
  414. :group 'org-reveal-location
  415. :type org-context-choice)
  416. (defcustom org-indirect-buffer-display 'other-window
  417. "How should indirect tree buffers be displayed?
  418. This applies to indirect buffers created with the commands
  419. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  420. Valid values are:
  421. current-window Display in the current window
  422. other-window Just display in another window.
  423. dedicated-frame Create one new frame, and re-use it each time.
  424. new-frame Make a new frame each time. Note that in this case
  425. previously-made indirect buffers are kept, and you need to
  426. kill these buffers yourself."
  427. :group 'org-structure
  428. :group 'org-agenda-windows
  429. :type '(choice
  430. (const :tag "In current window" current-window)
  431. (const :tag "In current frame, other window" other-window)
  432. (const :tag "Each time a new frame" new-frame)
  433. (const :tag "One dedicated frame" dedicated-frame)))
  434. (defgroup org-cycle nil
  435. "Options concerning visibility cycling in Org-mode."
  436. :tag "Org Cycle"
  437. :group 'org-structure)
  438. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  439. "Names of drawers. Drawers are not opened by cycling on the headline above.
  440. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  441. this:
  442. :DRAWERNAME:
  443. .....
  444. :END:
  445. The drawer \"PROPERTIES\" is special for capturing properties through
  446. the property API.
  447. Drawers can be defined on the per-file basis with a line like:
  448. #+DRAWERS: HIDDEN STATE PROPERTIES"
  449. :group 'org-structure
  450. :type '(repeat (string :tag "Drawer Name")))
  451. (defcustom org-cycle-global-at-bob nil
  452. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  453. This makes it possible to do global cycling without having to use S-TAB or
  454. C-u TAB. For this special case to work, the first line of the buffer
  455. must not be a headline - it may be empty ot some other text. When used in
  456. this way, `org-cycle-hook' is disables temporarily, to make sure the
  457. cursor stays at the beginning of the buffer.
  458. When this option is nil, don't do anything special at the beginning
  459. of the buffer."
  460. :group 'org-cycle
  461. :type 'boolean)
  462. (defcustom org-cycle-emulate-tab t
  463. "Where should `org-cycle' emulate TAB.
  464. nil Never
  465. white Only in completely white lines
  466. whitestart Only at the beginning of lines, before the first non-white char
  467. t Everywhere except in headlines
  468. exc-hl-bol Everywhere except at the start of a headline
  469. If TAB is used in a place where it does not emulate TAB, the current subtree
  470. visibility is cycled."
  471. :group 'org-cycle
  472. :type '(choice (const :tag "Never" nil)
  473. (const :tag "Only in completely white lines" white)
  474. (const :tag "Before first char in a line" whitestart)
  475. (const :tag "Everywhere except in headlines" t)
  476. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  477. ))
  478. (defcustom org-cycle-separator-lines 2
  479. "Number of empty lines needed to keep an empty line between collapsed trees.
  480. If you leave an empty line between the end of a subtree and the following
  481. headline, this empty line is hidden when the subtree is folded.
  482. Org-mode will leave (exactly) one empty line visible if the number of
  483. empty lines is equal or larger to the number given in this variable.
  484. So the default 2 means, at least 2 empty lines after the end of a subtree
  485. are needed to produce free space between a collapsed subtree and the
  486. following headline.
  487. Special case: when 0, never leave empty lines in collapsed view."
  488. :group 'org-cycle
  489. :type 'integer)
  490. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  491. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  492. org-cycle-hide-drawers
  493. org-cycle-show-empty-lines
  494. org-optimize-window-after-visibility-change)
  495. "Hook that is run after `org-cycle' has changed the buffer visibility.
  496. The function(s) in this hook must accept a single argument which indicates
  497. the new state that was set by the most recent `org-cycle' command. The
  498. argument is a symbol. After a global state change, it can have the values
  499. `overview', `content', or `all'. After a local state change, it can have
  500. the values `folded', `children', or `subtree'."
  501. :group 'org-cycle
  502. :type 'hook)
  503. (defgroup org-edit-structure nil
  504. "Options concerning structure editing in Org-mode."
  505. :tag "Org Edit Structure"
  506. :group 'org-structure)
  507. (defcustom org-odd-levels-only nil
  508. "Non-nil means, skip even levels and only use odd levels for the outline.
  509. This has the effect that two stars are being added/taken away in
  510. promotion/demotion commands. It also influences how levels are
  511. handled by the exporters.
  512. Changing it requires restart of `font-lock-mode' to become effective
  513. for fontification also in regions already fontified.
  514. You may also set this on a per-file basis by adding one of the following
  515. lines to the buffer:
  516. #+STARTUP: odd
  517. #+STARTUP: oddeven"
  518. :group 'org-edit-structure
  519. :group 'org-font-lock
  520. :type 'boolean)
  521. (defcustom org-adapt-indentation t
  522. "Non-nil means, adapt indentation when promoting and demoting.
  523. When this is set and the *entire* text in an entry is indented, the
  524. indentation is increased by one space in a demotion command, and
  525. decreased by one in a promotion command. If any line in the entry
  526. body starts at column 0, indentation is not changed at all."
  527. :group 'org-edit-structure
  528. :type 'boolean)
  529. (defcustom org-special-ctrl-a/e nil
  530. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  531. When t, `C-a' will bring back the cursor to the beginning of the
  532. headline text, i.e. after the stars and after a possible TODO keyword.
  533. In an item, this will be the position after the bullet.
  534. When the cursor is already at that position, another `C-a' will bring
  535. it to the beginning of the line.
  536. `C-e' will jump to the end of the headline, ignoring the presence of tags
  537. in the headline. A second `C-e' will then jump to the true end of the
  538. line, after any tags.
  539. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  540. and only a directly following, identical keypress will bring the cursor
  541. to the special positions."
  542. :group 'org-edit-structure
  543. :type '(choice
  544. (const :tag "off" nil)
  545. (const :tag "after bullet first" t)
  546. (const :tag "border first" reversed)))
  547. (if (fboundp 'defvaralias)
  548. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  549. (defcustom org-special-ctrl-k nil
  550. "Non-nil means `C-k' will behave specially in headlines.
  551. When nil, `C-k' will call the default `kill-line' command.
  552. When t, the following will happen while the cursor is in the headline:
  553. - When the cursor is at the beginning of a headline, kill the entire
  554. line and possible the folded subtree below the line.
  555. - When in the middle of the headline text, kill the headline up to the tags.
  556. - When after the headline text, kill the tags."
  557. :group 'org-edit-structure
  558. :type 'boolean)
  559. (defcustom org-yank-folded-subtrees t
  560. "Non-nil means, when yanking subtrees, fold them.
  561. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  562. it starts with a heading and all other headings in it are either children
  563. or siblings, then fold all the subtrees."
  564. :group 'org-edit-structure
  565. :type 'boolean)
  566. (defcustom org-yank-adjusted-subtrees t
  567. "Non-nil means, when yanking subtrees, adjust the level.
  568. With this setting, `org-paste-subtree' is used to insert the subtree, see
  569. this function for details."
  570. :group 'org-edit-structure
  571. :type 'boolean)
  572. (defcustom org-M-RET-may-split-line '((default . t))
  573. "Non-nil means, M-RET will split the line at the cursor position.
  574. When nil, it will go to the end of the line before making a
  575. new line.
  576. You may also set this option in a different way for different
  577. contexts. Valid contexts are:
  578. headline when creating a new headline
  579. item when creating a new item
  580. table in a table field
  581. default the value to be used for all contexts not explicitly
  582. customized"
  583. :group 'org-structure
  584. :group 'org-table
  585. :type '(choice
  586. (const :tag "Always" t)
  587. (const :tag "Never" nil)
  588. (repeat :greedy t :tag "Individual contexts"
  589. (cons
  590. (choice :tag "Context"
  591. (const headline)
  592. (const item)
  593. (const table)
  594. (const default))
  595. (boolean)))))
  596. (defcustom org-insert-heading-respect-content nil
  597. "Non-nil means, insert new headings after the current subtree.
  598. When nil, the new heading is created directly after the current line.
  599. The commands \\[org-insert-heading-respect-content] and
  600. \\[org-insert-todo-heading-respect-content] turn this variable on
  601. for the duration of the command."
  602. :group 'org-structure
  603. :type 'boolean)
  604. (defcustom org-blank-before-new-entry '((heading . nil)
  605. (plain-list-item . nil))
  606. "Should `org-insert-heading' leave a blank line before new heading/item?
  607. The value is an alist, with `heading' and `plain-list-item' as car,
  608. and a boolean flag as cdr."
  609. :group 'org-edit-structure
  610. :type '(list
  611. (cons (const heading) (boolean))
  612. (cons (const plain-list-item) (boolean))))
  613. (defcustom org-insert-heading-hook nil
  614. "Hook being run after inserting a new heading."
  615. :group 'org-edit-structure
  616. :type 'hook)
  617. (defcustom org-enable-fixed-width-editor t
  618. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  619. This currently only means, they are never auto-wrapped.
  620. When nil, such lines will be treated like ordinary lines.
  621. See also the QUOTE keyword."
  622. :group 'org-edit-structure
  623. :type 'boolean)
  624. (defcustom org-edit-src-region-extra nil
  625. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  626. For examples see the function `org-edit-src-find-region-and-lang'.
  627. The regular expression identifying the begin marker should end with a newline,
  628. and the regexp marking the end line should start with a newline, to make sure
  629. there are kept outside the narrowed region."
  630. :group 'org-edit-structure
  631. :type '(repeat
  632. (list
  633. (regexp :tag "begin regexp")
  634. (regexp :tag "end regexp")
  635. (choice :tag "language"
  636. (string :tag "specify")
  637. (integer :tag "from match group")
  638. (const :tag "from `lang' element")
  639. (const :tag "from `style' element")))))
  640. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  641. "The mode that should be used to edit fixed-width regions.
  642. These are the regions where each line starts with a colon."
  643. :group 'org-edit-structure
  644. :type '(choice
  645. (const artist-mode)
  646. (const picture-mode)
  647. (const fundamental-mode)
  648. (function :tag "Other (specify)")))
  649. (defcustom org-goto-auto-isearch t
  650. "Non-nil means, typing characters in org-goto starts incremental search."
  651. :group 'org-edit-structure
  652. :type 'boolean)
  653. (defgroup org-sparse-trees nil
  654. "Options concerning sparse trees in Org-mode."
  655. :tag "Org Sparse Trees"
  656. :group 'org-structure)
  657. (defcustom org-highlight-sparse-tree-matches t
  658. "Non-nil means, highlight all matches that define a sparse tree.
  659. The highlights will automatically disappear the next time the buffer is
  660. changed by an edit command."
  661. :group 'org-sparse-trees
  662. :type 'boolean)
  663. (defcustom org-remove-highlights-with-change t
  664. "Non-nil means, any change to the buffer will remove temporary highlights.
  665. Such highlights are created by `org-occur' and `org-clock-display'.
  666. When nil, `C-c C-c needs to be used to get rid of the highlights.
  667. The highlights created by `org-preview-latex-fragment' always need
  668. `C-c C-c' to be removed."
  669. :group 'org-sparse-trees
  670. :group 'org-time
  671. :type 'boolean)
  672. (defcustom org-occur-hook '(org-first-headline-recenter)
  673. "Hook that is run after `org-occur' has constructed a sparse tree.
  674. This can be used to recenter the window to show as much of the structure
  675. as possible."
  676. :group 'org-sparse-trees
  677. :type 'hook)
  678. (defgroup org-imenu-and-speedbar nil
  679. "Options concerning imenu and speedbar in Org-mode."
  680. :tag "Org Imenu and Speedbar"
  681. :group 'org-structure)
  682. (defcustom org-imenu-depth 2
  683. "The maximum level for Imenu access to Org-mode headlines.
  684. This also applied for speedbar access."
  685. :group 'org-imenu-and-speedbar
  686. :type 'number)
  687. (defgroup org-table nil
  688. "Options concerning tables in Org-mode."
  689. :tag "Org Table"
  690. :group 'org)
  691. (defcustom org-enable-table-editor 'optimized
  692. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  693. When nil, such lines will be treated like ordinary lines.
  694. When equal to the symbol `optimized', the table editor will be optimized to
  695. do the following:
  696. - Automatic overwrite mode in front of whitespace in table fields.
  697. This makes the structure of the table stay in tact as long as the edited
  698. field does not exceed the column width.
  699. - Minimize the number of realigns. Normally, the table is aligned each time
  700. TAB or RET are pressed to move to another field. With optimization this
  701. happens only if changes to a field might have changed the column width.
  702. Optimization requires replacing the functions `self-insert-command',
  703. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  704. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  705. very good at guessing when a re-align will be necessary, but you can always
  706. force one with \\[org-ctrl-c-ctrl-c].
  707. If you would like to use the optimized version in Org-mode, but the
  708. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  709. This variable can be used to turn on and off the table editor during a session,
  710. but in order to toggle optimization, a restart is required.
  711. See also the variable `org-table-auto-blank-field'."
  712. :group 'org-table
  713. :type '(choice
  714. (const :tag "off" nil)
  715. (const :tag "on" t)
  716. (const :tag "on, optimized" optimized)))
  717. (defcustom org-table-tab-recognizes-table.el t
  718. "Non-nil means, TAB will automatically notice a table.el table.
  719. When it sees such a table, it moves point into it and - if necessary -
  720. calls `table-recognize-table'."
  721. :group 'org-table-editing
  722. :type 'boolean)
  723. (defgroup org-link nil
  724. "Options concerning links in Org-mode."
  725. :tag "Org Link"
  726. :group 'org)
  727. (defvar org-link-abbrev-alist-local nil
  728. "Buffer-local version of `org-link-abbrev-alist', which see.
  729. The value of this is taken from the #+LINK lines.")
  730. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  731. (defcustom org-link-abbrev-alist nil
  732. "Alist of link abbreviations.
  733. The car of each element is a string, to be replaced at the start of a link.
  734. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  735. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  736. [[linkkey:tag][description]]
  737. If REPLACE is a string, the tag will simply be appended to create the link.
  738. If the string contains \"%s\", the tag will be inserted there.
  739. REPLACE may also be a function that will be called with the tag as the
  740. only argument to create the link, which should be returned as a string.
  741. See the manual for examples."
  742. :group 'org-link
  743. :type '(repeat
  744. (cons
  745. (string :tag "Protocol")
  746. (choice
  747. (string :tag "Format")
  748. (function)))))
  749. (defcustom org-descriptive-links t
  750. "Non-nil means, hide link part and only show description of bracket links.
  751. Bracket links are like [[link][descritpion]]. This variable sets the initial
  752. state in new org-mode buffers. The setting can then be toggled on a
  753. per-buffer basis from the Org->Hyperlinks menu."
  754. :group 'org-link
  755. :type 'boolean)
  756. (defcustom org-link-file-path-type 'adaptive
  757. "How the path name in file links should be stored.
  758. Valid values are:
  759. relative Relative to the current directory, i.e. the directory of the file
  760. into which the link is being inserted.
  761. absolute Absolute path, if possible with ~ for home directory.
  762. noabbrev Absolute path, no abbreviation of home directory.
  763. adaptive Use relative path for files in the current directory and sub-
  764. directories of it. For other files, use an absolute path."
  765. :group 'org-link
  766. :type '(choice
  767. (const relative)
  768. (const absolute)
  769. (const noabbrev)
  770. (const adaptive)))
  771. (defcustom org-activate-links '(bracket angle plain radio tag date)
  772. "Types of links that should be activated in Org-mode files.
  773. This is a list of symbols, each leading to the activation of a certain link
  774. type. In principle, it does not hurt to turn on most link types - there may
  775. be a small gain when turning off unused link types. The types are:
  776. bracket The recommended [[link][description]] or [[link]] links with hiding.
  777. angular Links in angular brackes that may contain whitespace like
  778. <bbdb:Carsten Dominik>.
  779. plain Plain links in normal text, no whitespace, like http://google.com.
  780. radio Text that is matched by a radio target, see manual for details.
  781. tag Tag settings in a headline (link to tag search).
  782. date Time stamps (link to calendar).
  783. Changing this variable requires a restart of Emacs to become effective."
  784. :group 'org-link
  785. :type '(set (const :tag "Double bracket links (new style)" bracket)
  786. (const :tag "Angular bracket links (old style)" angular)
  787. (const :tag "Plain text links" plain)
  788. (const :tag "Radio target matches" radio)
  789. (const :tag "Tags" tag)
  790. (const :tag "Timestamps" date)))
  791. (defcustom org-make-link-description-function nil
  792. "Function to use to generate link descriptions from links. If
  793. nil the link location will be used. This function must take two
  794. parameters; the first is the link and the second the description
  795. org-insert-link has generated, and should return the description
  796. to use."
  797. :group 'org-link
  798. :type 'function)
  799. (defgroup org-link-store nil
  800. "Options concerning storing links in Org-mode."
  801. :tag "Org Store Link"
  802. :group 'org-link)
  803. (defcustom org-email-link-description-format "Email %c: %.30s"
  804. "Format of the description part of a link to an email or usenet message.
  805. The following %-excapes will be replaced by corresponding information:
  806. %F full \"From\" field
  807. %f name, taken from \"From\" field, address if no name
  808. %T full \"To\" field
  809. %t first name in \"To\" field, address if no name
  810. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  811. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  812. %s subject
  813. %m message-id.
  814. You may use normal field width specification between the % and the letter.
  815. This is for example useful to limit the length of the subject.
  816. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  817. :group 'org-link-store
  818. :type 'string)
  819. (defcustom org-from-is-user-regexp
  820. (let (r1 r2)
  821. (when (and user-mail-address (not (string= user-mail-address "")))
  822. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  823. (when (and user-full-name (not (string= user-full-name "")))
  824. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  825. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  826. "Regexp mached against the \"From:\" header of an email or usenet message.
  827. It should match if the message is from the user him/herself."
  828. :group 'org-link-store
  829. :type 'regexp)
  830. (defcustom org-context-in-file-links t
  831. "Non-nil means, file links from `org-store-link' contain context.
  832. A search string will be added to the file name with :: as separator and
  833. used to find the context when the link is activated by the command
  834. `org-open-at-point'.
  835. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  836. negates this setting for the duration of the command."
  837. :group 'org-link-store
  838. :type 'boolean)
  839. (defcustom org-keep-stored-link-after-insertion nil
  840. "Non-nil means, keep link in list for entire session.
  841. The command `org-store-link' adds a link pointing to the current
  842. location to an internal list. These links accumulate during a session.
  843. The command `org-insert-link' can be used to insert links into any
  844. Org-mode file (offering completion for all stored links). When this
  845. option is nil, every link which has been inserted once using \\[org-insert-link]
  846. will be removed from the list, to make completing the unused links
  847. more efficient."
  848. :group 'org-link-store
  849. :type 'boolean)
  850. (defgroup org-link-follow nil
  851. "Options concerning following links in Org-mode."
  852. :tag "Org Follow Link"
  853. :group 'org-link)
  854. (defcustom org-follow-link-hook nil
  855. "Hook that is run after a link has been followed."
  856. :group 'org-link-follow
  857. :type 'hook)
  858. (defcustom org-tab-follows-link nil
  859. "Non-nil means, on links TAB will follow the link.
  860. Needs to be set before org.el is loaded."
  861. :group 'org-link-follow
  862. :type 'boolean)
  863. (defcustom org-return-follows-link nil
  864. "Non-nil means, on links RET will follow the link.
  865. Needs to be set before org.el is loaded."
  866. :group 'org-link-follow
  867. :type 'boolean)
  868. (defcustom org-mouse-1-follows-link
  869. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  870. "Non-nil means, mouse-1 on a link will follow the link.
  871. A longer mouse click will still set point. Does not work on XEmacs.
  872. Needs to be set before org.el is loaded."
  873. :group 'org-link-follow
  874. :type 'boolean)
  875. (defcustom org-mark-ring-length 4
  876. "Number of different positions to be recorded in the ring
  877. Changing this requires a restart of Emacs to work correctly."
  878. :group 'org-link-follow
  879. :type 'interger)
  880. (defcustom org-link-frame-setup
  881. '((vm . vm-visit-folder-other-frame)
  882. (gnus . gnus-other-frame)
  883. (file . find-file-other-window))
  884. "Setup the frame configuration for following links.
  885. When following a link with Emacs, it may often be useful to display
  886. this link in another window or frame. This variable can be used to
  887. set this up for the different types of links.
  888. For VM, use any of
  889. `vm-visit-folder'
  890. `vm-visit-folder-other-frame'
  891. For Gnus, use any of
  892. `gnus'
  893. `gnus-other-frame'
  894. `org-gnus-no-new-news'
  895. For FILE, use any of
  896. `find-file'
  897. `find-file-other-window'
  898. `find-file-other-frame'
  899. For the calendar, use the variable `calendar-setup'.
  900. For BBDB, it is currently only possible to display the matches in
  901. another window."
  902. :group 'org-link-follow
  903. :type '(list
  904. (cons (const vm)
  905. (choice
  906. (const vm-visit-folder)
  907. (const vm-visit-folder-other-window)
  908. (const vm-visit-folder-other-frame)))
  909. (cons (const gnus)
  910. (choice
  911. (const gnus)
  912. (const gnus-other-frame)
  913. (const org-gnus-no-new-news)))
  914. (cons (const file)
  915. (choice
  916. (const find-file)
  917. (const find-file-other-window)
  918. (const find-file-other-frame)))))
  919. (defcustom org-display-internal-link-with-indirect-buffer nil
  920. "Non-nil means, use indirect buffer to display infile links.
  921. Activating internal links (from one location in a file to another location
  922. in the same file) normally just jumps to the location. When the link is
  923. activated with a C-u prefix (or with mouse-3), the link is displayed in
  924. another window. When this option is set, the other window actually displays
  925. an indirect buffer clone of the current buffer, to avoid any visibility
  926. changes to the current buffer."
  927. :group 'org-link-follow
  928. :type 'boolean)
  929. (defcustom org-open-non-existing-files nil
  930. "Non-nil means, `org-open-file' will open non-existing files.
  931. When nil, an error will be generated."
  932. :group 'org-link-follow
  933. :type 'boolean)
  934. (defcustom org-open-directory-means-index-dot-org nil
  935. "Non-nil means, a link to a directory really means to index.org.
  936. When nil, following a directory link will run dired or open a finder/explorer
  937. window on that directory."
  938. :group 'org-link-follow
  939. :type 'boolean)
  940. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  941. "Function and arguments to call for following mailto links.
  942. This is a list with the first element being a lisp function, and the
  943. remaining elements being arguments to the function. In string arguments,
  944. %a will be replaced by the address, and %s will be replaced by the subject
  945. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  946. :group 'org-link-follow
  947. :type '(choice
  948. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  949. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  950. (const :tag "message-mail" (message-mail "%a" "%s"))
  951. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  952. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  953. "Non-nil means, ask for confirmation before executing shell links.
  954. Shell links can be dangerous: just think about a link
  955. [[shell:rm -rf ~/*][Google Search]]
  956. This link would show up in your Org-mode document as \"Google Search\",
  957. but really it would remove your entire home directory.
  958. Therefore we advise against setting this variable to nil.
  959. Just change it to `y-or-n-p' of you want to confirm with a
  960. single keystroke rather than having to type \"yes\"."
  961. :group 'org-link-follow
  962. :type '(choice
  963. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  964. (const :tag "with y-or-n (faster)" y-or-n-p)
  965. (const :tag "no confirmation (dangerous)" nil)))
  966. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  967. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  968. Elisp links can be dangerous: just think about a link
  969. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  970. This link would show up in your Org-mode document as \"Google Search\",
  971. but really it would remove your entire home directory.
  972. Therefore we advise against setting this variable to nil.
  973. Just change it to `y-or-n-p' of you want to confirm with a
  974. single keystroke rather than having to type \"yes\"."
  975. :group 'org-link-follow
  976. :type '(choice
  977. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  978. (const :tag "with y-or-n (faster)" y-or-n-p)
  979. (const :tag "no confirmation (dangerous)" nil)))
  980. (defconst org-file-apps-defaults-gnu
  981. '((remote . emacs)
  982. (system . mailcap)
  983. (t . mailcap))
  984. "Default file applications on a UNIX or GNU/Linux system.
  985. See `org-file-apps'.")
  986. (defconst org-file-apps-defaults-macosx
  987. '((remote . emacs)
  988. (t . "open %s")
  989. (system . "open %s")
  990. ("ps.gz" . "gv %s")
  991. ("eps.gz" . "gv %s")
  992. ("dvi" . "xdvi %s")
  993. ("fig" . "xfig %s"))
  994. "Default file applications on a MacOS X system.
  995. The system \"open\" is known as a default, but we use X11 applications
  996. for some files for which the OS does not have a good default.
  997. See `org-file-apps'.")
  998. (defconst org-file-apps-defaults-windowsnt
  999. (list
  1000. '(remote . emacs)
  1001. (cons t
  1002. (list (if (featurep 'xemacs)
  1003. 'mswindows-shell-execute
  1004. 'w32-shell-execute)
  1005. "open" 'file))
  1006. (cons 'system
  1007. (list (if (featurep 'xemacs)
  1008. 'mswindows-shell-execute
  1009. 'w32-shell-execute)
  1010. "open" 'file)))
  1011. "Default file applications on a Windows NT system.
  1012. The system \"open\" is used for most files.
  1013. See `org-file-apps'.")
  1014. (defcustom org-file-apps
  1015. '(
  1016. (auto-mode . emacs)
  1017. ("\\.x?html?\\'" . default)
  1018. ("\\.pdf\\'" . default)
  1019. )
  1020. "External applications for opening `file:path' items in a document.
  1021. Org-mode uses system defaults for different file types, but
  1022. you can use this variable to set the application for a given file
  1023. extension. The entries in this list are cons cells where the car identifies
  1024. files and the cdr the corresponding command. Possible values for the
  1025. file identifier are
  1026. \"regex\" Regular expression matched against the file name. For backward
  1027. compatibility, this can also be a string with only alphanumeric
  1028. characters, which is then interpreted as an extension.
  1029. `directory' Matches a directory
  1030. `remote' Matches a remote file, accessible through tramp or efs.
  1031. Remote files most likely should be visited through Emacs
  1032. because external applications cannot handle such paths.
  1033. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1034. so all files Emacs knows how to handle. Using this with
  1035. command `emacs' will open most files in Emacs. Beware that this
  1036. will also open html files insite Emacs, unless you add
  1037. (\"html\" . default) to the list as well.
  1038. t Default for files not matched by any of the other options.
  1039. `system' The system command to open files, like `open' on Windows
  1040. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1041. that will be selected if you call `C-c C-o' with a double
  1042. `C-u C-u' prefix.
  1043. Possible values for the command are:
  1044. `emacs' The file will be visited by the current Emacs process.
  1045. `default' Use the default application for this file type, which is the
  1046. association for t in the list, most likely in the system-specific
  1047. part.
  1048. This can be used to overrule an unwanted seting in the
  1049. system-specific variable.
  1050. `system' Use the system command for opening files, like \"open\".
  1051. This command is specified by the entry whose car is `system'.
  1052. Most likely, the system-specific version of this variable
  1053. does define this command, but you can overrule/replace it
  1054. here.
  1055. string A command to be executed by a shell; %s will be replaced
  1056. by the path to the file.
  1057. sexp A Lisp form which will be evaluated. The file path will
  1058. be available in the Lisp variable `file'.
  1059. For more examples, see the system specific constants
  1060. `org-file-apps-defaults-macosx'
  1061. `org-file-apps-defaults-windowsnt'
  1062. `org-file-apps-defaults-gnu'."
  1063. :group 'org-link-follow
  1064. :type '(repeat
  1065. (cons (choice :value ""
  1066. (string :tag "Extension")
  1067. (const :tag "System command to open files" system)
  1068. (const :tag "Default for unrecognized files" t)
  1069. (const :tag "Remote file" remote)
  1070. (const :tag "Links to a directory" directory)
  1071. (const :tag "Any files that have Emacs modes"
  1072. auto-mode))
  1073. (choice :value ""
  1074. (const :tag "Visit with Emacs" emacs)
  1075. (const :tag "Use default" default)
  1076. (const :tag "Use the system command" system)
  1077. (string :tag "Command")
  1078. (sexp :tag "Lisp form")))))
  1079. (defgroup org-refile nil
  1080. "Options concerning refiling entries in Org-mode."
  1081. :tag "Org Remember"
  1082. :group 'org)
  1083. (defcustom org-directory "~/org"
  1084. "Directory with org files.
  1085. This directory will be used as default to prompt for org files.
  1086. Used by the hooks for remember.el."
  1087. :group 'org-refile
  1088. :group 'org-remember
  1089. :type 'directory)
  1090. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1091. "Default target for storing notes.
  1092. Used by the hooks for remember.el. This can be a string, or nil to mean
  1093. the value of `remember-data-file'.
  1094. You can set this on a per-template basis with the variable
  1095. `org-remember-templates'."
  1096. :group 'org-refile
  1097. :group 'org-remember
  1098. :type '(choice
  1099. (const :tag "Default from remember-data-file" nil)
  1100. file))
  1101. (defcustom org-goto-interface 'outline
  1102. "The default interface to be used for `org-goto'.
  1103. Allowed vaues are:
  1104. outline The interface shows an outline of the relevant file
  1105. and the correct heading is found by moving through
  1106. the outline or by searching with incremental search.
  1107. outline-path-completion Headlines in the current buffer are offered via
  1108. completion."
  1109. :group 'org-refile
  1110. :type '(choice
  1111. (const :tag "Outline" outline)
  1112. (const :tag "Outline-path-completion" outline-path-completion)))
  1113. (defcustom org-reverse-note-order nil
  1114. "Non-nil means, store new notes at the beginning of a file or entry.
  1115. When nil, new notes will be filed to the end of a file or entry.
  1116. This can also be a list with cons cells of regular expressions that
  1117. are matched against file names, and values."
  1118. :group 'org-remember
  1119. :type '(choice
  1120. (const :tag "Reverse always" t)
  1121. (const :tag "Reverse never" nil)
  1122. (repeat :tag "By file name regexp"
  1123. (cons regexp boolean))))
  1124. (defcustom org-refile-targets nil
  1125. "Targets for refiling entries with \\[org-refile].
  1126. This is list of cons cells. Each cell contains:
  1127. - a specification of the files to be considered, either a list of files,
  1128. or a symbol whose function or variable value will be used to retrieve
  1129. a file name or a list of file names. Nil means, refile to a different
  1130. heading in the current buffer.
  1131. - A specification of how to find candidate refile targets. This may be
  1132. any of
  1133. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1134. This tag has to be present in all target headlines, inheritance will
  1135. not be considered.
  1136. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1137. todo keyword.
  1138. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1139. headlines that are refiling targets.
  1140. - a cons cell (:level . N). Any headline of level N is considered a target.
  1141. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1142. When this variable is nil, all top-level headlines in the current buffer
  1143. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1144. :group 'org-remember
  1145. :type '(repeat
  1146. (cons
  1147. (choice :value org-agenda-files
  1148. (const :tag "All agenda files" org-agenda-files)
  1149. (const :tag "Current buffer" nil)
  1150. (function) (variable) (file))
  1151. (choice :tag "Identify target headline by"
  1152. (cons :tag "Specific tag" (const :tag) (string))
  1153. (cons :tag "TODO keyword" (const :todo) (string))
  1154. (cons :tag "Regular expression" (const :regexp) (regexp))
  1155. (cons :tag "Level number" (const :level) (integer))
  1156. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1157. (defcustom org-refile-use-outline-path nil
  1158. "Non-nil means, provide refile targets as paths.
  1159. So a level 3 headline will be available as level1/level2/level3.
  1160. When the value is `file', also include the file name (without directory)
  1161. into the path. When `full-file-path', include the full file path."
  1162. :group 'org-remember
  1163. :type '(choice
  1164. (const :tag "Not" nil)
  1165. (const :tag "Yes" t)
  1166. (const :tag "Start with file name" file)
  1167. (const :tag "Start with full file path" full-file-path)))
  1168. (defgroup org-todo nil
  1169. "Options concerning TODO items in Org-mode."
  1170. :tag "Org TODO"
  1171. :group 'org)
  1172. (defgroup org-progress nil
  1173. "Options concerning Progress logging in Org-mode."
  1174. :tag "Org Progress"
  1175. :group 'org-time)
  1176. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1177. "List of TODO entry keyword sequences and their interpretation.
  1178. \\<org-mode-map>This is a list of sequences.
  1179. Each sequence starts with a symbol, either `sequence' or `type',
  1180. indicating if the keywords should be interpreted as a sequence of
  1181. action steps, or as different types of TODO items. The first
  1182. keywords are states requiring action - these states will select a headline
  1183. for inclusion into the global TODO list Org-mode produces. If one of
  1184. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1185. signify that no further action is necessary. If \"|\" is not found,
  1186. the last keyword is treated as the only DONE state of the sequence.
  1187. The command \\[org-todo] cycles an entry through these states, and one
  1188. additional state where no keyword is present. For details about this
  1189. cycling, see the manual.
  1190. TODO keywords and interpretation can also be set on a per-file basis with
  1191. the special #+SEQ_TODO and #+TYP_TODO lines.
  1192. Each keyword can optionally specify a character for fast state selection
  1193. \(in combination with the variable `org-use-fast-todo-selection')
  1194. and specifiers for state change logging, using the same syntax
  1195. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1196. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1197. indicates to record a time stamp each time this state is selected.
  1198. Each keyword may also specify if a timestamp or a note should be
  1199. recorded when entering or leaving the state, by adding additional
  1200. characters in the parenthesis after the keyword. This looks like this:
  1201. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1202. record only the time of the state change. With X and Y being either
  1203. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1204. Y when leaving the state if and only if the *target* state does not
  1205. define X. You may omit any of the fast-selection key or X or /Y,
  1206. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1207. For backward compatibility, this variable may also be just a list
  1208. of keywords - in this case the interptetation (sequence or type) will be
  1209. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1210. :group 'org-todo
  1211. :group 'org-keywords
  1212. :type '(choice
  1213. (repeat :tag "Old syntax, just keywords"
  1214. (string :tag "Keyword"))
  1215. (repeat :tag "New syntax"
  1216. (cons
  1217. (choice
  1218. :tag "Interpretation"
  1219. (const :tag "Sequence (cycling hits every state)" sequence)
  1220. (const :tag "Type (cycling directly to DONE)" type))
  1221. (repeat
  1222. (string :tag "Keyword"))))))
  1223. (defvar org-todo-keywords-1 nil
  1224. "All TODO and DONE keywords active in a buffer.")
  1225. (make-variable-buffer-local 'org-todo-keywords-1)
  1226. (defvar org-todo-keywords-for-agenda nil)
  1227. (defvar org-done-keywords-for-agenda nil)
  1228. (defvar org-todo-keyword-alist-for-agenda nil)
  1229. (defvar org-tag-alist-for-agenda nil)
  1230. (defvar org-agenda-contributing-files nil)
  1231. (defvar org-not-done-keywords nil)
  1232. (make-variable-buffer-local 'org-not-done-keywords)
  1233. (defvar org-done-keywords nil)
  1234. (make-variable-buffer-local 'org-done-keywords)
  1235. (defvar org-todo-heads nil)
  1236. (make-variable-buffer-local 'org-todo-heads)
  1237. (defvar org-todo-sets nil)
  1238. (make-variable-buffer-local 'org-todo-sets)
  1239. (defvar org-todo-log-states nil)
  1240. (make-variable-buffer-local 'org-todo-log-states)
  1241. (defvar org-todo-kwd-alist nil)
  1242. (make-variable-buffer-local 'org-todo-kwd-alist)
  1243. (defvar org-todo-key-alist nil)
  1244. (make-variable-buffer-local 'org-todo-key-alist)
  1245. (defvar org-todo-key-trigger nil)
  1246. (make-variable-buffer-local 'org-todo-key-trigger)
  1247. (defcustom org-todo-interpretation 'sequence
  1248. "Controls how TODO keywords are interpreted.
  1249. This variable is in principle obsolete and is only used for
  1250. backward compatibility, if the interpretation of todo keywords is
  1251. not given already in `org-todo-keywords'. See that variable for
  1252. more information."
  1253. :group 'org-todo
  1254. :group 'org-keywords
  1255. :type '(choice (const sequence)
  1256. (const type)))
  1257. (defcustom org-use-fast-todo-selection 'prefix
  1258. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1259. This variable describes if and under what circumstances the cycling
  1260. mechanism for TODO keywords will be replaced by a single-key, direct
  1261. selection scheme.
  1262. When nil, fast selection is never used.
  1263. When the symbol `prefix', it will be used when `org-todo' is called with
  1264. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1265. in an agenda buffer.
  1266. When t, fast selection is used by default. In this case, the prefix
  1267. argument forces cycling instead.
  1268. In all cases, the special interface is only used if access keys have actually
  1269. been assigned by the user, i.e. if keywords in the configuration are followed
  1270. by a letter in parenthesis, like TODO(t)."
  1271. :group 'org-todo
  1272. :type '(choice
  1273. (const :tag "Never" nil)
  1274. (const :tag "By default" t)
  1275. (const :tag "Only with C-u C-c C-t" prefix)))
  1276. (defcustom org-provide-todo-statistics t
  1277. "Non-nil means, update todo statistics after insert and toggle.
  1278. When this is set, todo statistics is updated in the parent of the current
  1279. entry each time a todo state is changed."
  1280. :group 'org-todo
  1281. :type 'boolean)
  1282. (defcustom org-after-todo-state-change-hook nil
  1283. "Hook which is run after the state of a TODO item was changed.
  1284. The new state (a string with a TODO keyword, or nil) is available in the
  1285. Lisp variable `state'."
  1286. :group 'org-todo
  1287. :type 'hook)
  1288. (defcustom org-todo-state-tags-triggers nil
  1289. "Tag changes that should be triggered by TODO state changes.
  1290. This is a list. Each entry is
  1291. (state-change (tag . flag) .......)
  1292. State-change can be a string with a state, and empty string to indicate the
  1293. state that has no TODO keyword, or it can be one of the symbols `todo'
  1294. or `done', meaning any not-done or done state, respectively."
  1295. :group 'org-todo
  1296. :group 'org-tags
  1297. :type '(repeat
  1298. (cons (choice :tag "When changing to"
  1299. (const :tag "Not-done state" todo)
  1300. (const :tag "Done state" done)
  1301. (string :tag "State"))
  1302. (repeat
  1303. (cons :tag "Tag action"
  1304. (string :tag "Tag")
  1305. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1306. (defcustom org-log-done nil
  1307. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1308. When equal to the list (done), also prompt for a closing note.
  1309. This can also be configured on a per-file basis by adding one of
  1310. the following lines anywhere in the buffer:
  1311. #+STARTUP: logdone
  1312. #+STARTUP: lognotedone
  1313. #+STARTUP: nologdone"
  1314. :group 'org-todo
  1315. :group 'org-progress
  1316. :type '(choice
  1317. (const :tag "No logging" nil)
  1318. (const :tag "Record CLOSED timestamp" time)
  1319. (const :tag "Record CLOSED timestamp with closing note." note)))
  1320. ;; Normalize old uses of org-log-done.
  1321. (cond
  1322. ((eq org-log-done t) (setq org-log-done 'time))
  1323. ((and (listp org-log-done) (memq 'done org-log-done))
  1324. (setq org-log-done 'note)))
  1325. (defcustom org-log-note-clock-out nil
  1326. "Non-nil means, record a note when clocking out of an item.
  1327. This can also be configured on a per-file basis by adding one of
  1328. the following lines anywhere in the buffer:
  1329. #+STARTUP: lognoteclock-out
  1330. #+STARTUP: nolognoteclock-out"
  1331. :group 'org-todo
  1332. :group 'org-progress
  1333. :type 'boolean)
  1334. (defcustom org-log-done-with-time t
  1335. "Non-nil means, the CLOSED time stamp will contain date and time.
  1336. When nil, only the date will be recorded."
  1337. :group 'org-progress
  1338. :type 'boolean)
  1339. (defcustom org-log-note-headings
  1340. '((done . "CLOSING NOTE %t")
  1341. (state . "State %-12s %t")
  1342. (note . "Note taken on %t")
  1343. (clock-out . ""))
  1344. "Headings for notes added to entries.
  1345. The value is an alist, with the car being a symbol indicating the note
  1346. context, and the cdr is the heading to be used. The heading may also be the
  1347. empty string.
  1348. %t in the heading will be replaced by a time stamp.
  1349. %s will be replaced by the new TODO state, in double quotes.
  1350. %u will be replaced by the user name.
  1351. %U will be replaced by the full user name."
  1352. :group 'org-todo
  1353. :group 'org-progress
  1354. :type '(list :greedy t
  1355. (cons (const :tag "Heading when closing an item" done) string)
  1356. (cons (const :tag
  1357. "Heading when changing todo state (todo sequence only)"
  1358. state) string)
  1359. (cons (const :tag "Heading when just taking a note" note) string)
  1360. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1361. (unless (assq 'note org-log-note-headings)
  1362. (push '(note . "%t") org-log-note-headings))
  1363. (defcustom org-log-state-notes-insert-after-drawers nil
  1364. "Non-nil means, insert state change notes after any drawers in entry.
  1365. Only the drawers that *immediately* follow the headline and the
  1366. deadline/scheduled line are skipped.
  1367. When nil, insert notes right after the heading and perhaps the line
  1368. with deadline/scheduling if present."
  1369. :group 'org-todo
  1370. :group 'org-progress
  1371. :type 'boolean)
  1372. (defcustom org-log-states-order-reversed t
  1373. "Non-nil means, the latest state change note will be directly after heading.
  1374. When nil, the notes will be orderer according to time."
  1375. :group 'org-todo
  1376. :group 'org-progress
  1377. :type 'boolean)
  1378. (defcustom org-log-repeat 'time
  1379. "Non-nil means, record moving through the DONE state when triggering repeat.
  1380. An auto-repeating tasks is immediately switched back to TODO when marked
  1381. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1382. the TODO keyword definition, or recording a closing note by setting
  1383. `org-log-done', there will be no record of the task moving through DONE.
  1384. This variable forces taking a note anyway. Possible values are:
  1385. nil Don't force a record
  1386. time Record a time stamp
  1387. note Record a note
  1388. This option can also be set with on a per-file-basis with
  1389. #+STARTUP: logrepeat
  1390. #+STARTUP: lognoterepeat
  1391. #+STARTUP: nologrepeat
  1392. You can have local logging settings for a subtree by setting the LOGGING
  1393. property to one or more of these keywords."
  1394. :group 'org-todo
  1395. :group 'org-progress
  1396. :type '(choice
  1397. (const :tag "Don't force a record" nil)
  1398. (const :tag "Force recording the DONE state" time)
  1399. (const :tag "Force recording a note with the DONE state" note)))
  1400. (defgroup org-priorities nil
  1401. "Priorities in Org-mode."
  1402. :tag "Org Priorities"
  1403. :group 'org-todo)
  1404. (defcustom org-highest-priority ?A
  1405. "The highest priority of TODO items. A character like ?A, ?B etc.
  1406. Must have a smaller ASCII number than `org-lowest-priority'."
  1407. :group 'org-priorities
  1408. :type 'character)
  1409. (defcustom org-lowest-priority ?C
  1410. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1411. Must have a larger ASCII number than `org-highest-priority'."
  1412. :group 'org-priorities
  1413. :type 'character)
  1414. (defcustom org-default-priority ?B
  1415. "The default priority of TODO items.
  1416. This is the priority an item get if no explicit priority is given."
  1417. :group 'org-priorities
  1418. :type 'character)
  1419. (defcustom org-priority-start-cycle-with-default t
  1420. "Non-nil means, start with default priority when starting to cycle.
  1421. When this is nil, the first step in the cycle will be (depending on the
  1422. command used) one higher or lower that the default priority."
  1423. :group 'org-priorities
  1424. :type 'boolean)
  1425. (defgroup org-time nil
  1426. "Options concerning time stamps and deadlines in Org-mode."
  1427. :tag "Org Time"
  1428. :group 'org)
  1429. (defcustom org-insert-labeled-timestamps-at-point nil
  1430. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1431. When nil, these labeled time stamps are forces into the second line of an
  1432. entry, just after the headline. When scheduling from the global TODO list,
  1433. the time stamp will always be forced into the second line."
  1434. :group 'org-time
  1435. :type 'boolean)
  1436. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1437. "Formats for `format-time-string' which are used for time stamps.
  1438. It is not recommended to change this constant.")
  1439. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1440. "Number of minutes to round time stamps to.
  1441. These are two values, the first applies when first creating a time stamp.
  1442. The second applies when changing it with the commands `S-up' and `S-down'.
  1443. When changing the time stamp, this means that it will change in steps
  1444. of N minutes, as given by the second value.
  1445. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1446. numbers should be factors of 60, so for example 5, 10, 15.
  1447. When this is larger than 1, you can still force an exact time-stamp by using
  1448. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1449. and by using a prefix arg to `S-up/down' to specify the exact number
  1450. of minutes to shift."
  1451. :group 'org-time
  1452. :get '(lambda (var) ; Make sure all entries have 5 elements
  1453. (if (integerp (default-value var))
  1454. (list (default-value var) 5)
  1455. (default-value var)))
  1456. :type '(list
  1457. (integer :tag "when inserting times")
  1458. (integer :tag "when modifying times")))
  1459. ;; Normalize old customizations of this variable.
  1460. (when (integerp org-time-stamp-rounding-minutes)
  1461. (setq org-time-stamp-rounding-minutes
  1462. (list org-time-stamp-rounding-minutes
  1463. org-time-stamp-rounding-minutes)))
  1464. (defcustom org-display-custom-times nil
  1465. "Non-nil means, overlay custom formats over all time stamps.
  1466. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1467. To turn this on on a per-file basis, insert anywhere in the file:
  1468. #+STARTUP: customtime"
  1469. :group 'org-time
  1470. :set 'set-default
  1471. :type 'sexp)
  1472. (make-variable-buffer-local 'org-display-custom-times)
  1473. (defcustom org-time-stamp-custom-formats
  1474. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1475. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1476. These are overlayed over the default ISO format if the variable
  1477. `org-display-custom-times' is set. Time like %H:%M should be at the
  1478. end of the second format."
  1479. :group 'org-time
  1480. :type 'sexp)
  1481. (defun org-time-stamp-format (&optional long inactive)
  1482. "Get the right format for a time string."
  1483. (let ((f (if long (cdr org-time-stamp-formats)
  1484. (car org-time-stamp-formats))))
  1485. (if inactive
  1486. (concat "[" (substring f 1 -1) "]")
  1487. f)))
  1488. (defcustom org-time-clocksum-format "%d:%02d"
  1489. "The format string used when creating CLOCKSUM lines, or when
  1490. org-mode generates a time duration."
  1491. :group 'org-time
  1492. :type 'string)
  1493. (defcustom org-deadline-warning-days 14
  1494. "No. of days before expiration during which a deadline becomes active.
  1495. This variable governs the display in sparse trees and in the agenda.
  1496. When 0 or negative, it means use this number (the absolute value of it)
  1497. even if a deadline has a different individual lead time specified."
  1498. :group 'org-time
  1499. :group 'org-agenda-daily/weekly
  1500. :type 'number)
  1501. (defcustom org-read-date-prefer-future t
  1502. "Non-nil means, assume future for incomplete date input from user.
  1503. This affects the following situations:
  1504. 1. The user gives a day, but no month.
  1505. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1506. read this as the third of *next* month. However, if you enter \"17\",
  1507. it will be considered as *this* month.
  1508. 2. The user gives a month but not a year.
  1509. For example, if it is april and you enter \"feb 2\", this will be read
  1510. as feb 2, *next* year. \"May 5\", however, will be this year.
  1511. Currently this does not work for ISO week specifications.
  1512. When this option is nil, the current month and year will always be used
  1513. as defaults."
  1514. :group 'org-time
  1515. :type 'boolean)
  1516. (defcustom org-read-date-display-live t
  1517. "Non-nil means, display current interpretation of date prompt live.
  1518. This display will be in an overlay, in the minibuffer."
  1519. :group 'org-time
  1520. :type 'boolean)
  1521. (defcustom org-read-date-popup-calendar t
  1522. "Non-nil means, pop up a calendar when prompting for a date.
  1523. In the calendar, the date can be selected with mouse-1. However, the
  1524. minibuffer will also be active, and you can simply enter the date as well.
  1525. When nil, only the minibuffer will be available."
  1526. :group 'org-time
  1527. :type 'boolean)
  1528. (if (fboundp 'defvaralias)
  1529. (defvaralias 'org-popup-calendar-for-date-prompt
  1530. 'org-read-date-popup-calendar))
  1531. (defcustom org-extend-today-until 0
  1532. "The hour when your day really ends. Must be an integer.
  1533. This has influence for the following applications:
  1534. - When switching the agenda to \"today\". It it is still earlier than
  1535. the time given here, the day recognized as TODAY is actually yesterday.
  1536. - When a date is read from the user and it is still before the time given
  1537. here, the current date and time will be assumed to be yesterday, 23:59.
  1538. Also, timestamps inserted in remember templates follow this rule.
  1539. IMPORTANT: This is a feature whose implementation is and likely will
  1540. remain incomplete. Really, it is only here because past midnight seems to
  1541. be the favorite working time of John Wiegley :-)"
  1542. :group 'org-time
  1543. :type 'number)
  1544. (defcustom org-edit-timestamp-down-means-later nil
  1545. "Non-nil means, S-down will increase the time in a time stamp.
  1546. When nil, S-up will increase."
  1547. :group 'org-time
  1548. :type 'boolean)
  1549. (defcustom org-calendar-follow-timestamp-change t
  1550. "Non-nil means, make the calendar window follow timestamp changes.
  1551. When a timestamp is modified and the calendar window is visible, it will be
  1552. moved to the new date."
  1553. :group 'org-time
  1554. :type 'boolean)
  1555. (defgroup org-tags nil
  1556. "Options concerning tags in Org-mode."
  1557. :tag "Org Tags"
  1558. :group 'org)
  1559. (defcustom org-tag-alist nil
  1560. "List of tags allowed in Org-mode files.
  1561. When this list is nil, Org-mode will base TAG input on what is already in the
  1562. buffer.
  1563. The value of this variable is an alist, the car of each entry must be a
  1564. keyword as a string, the cdr may be a character that is used to select
  1565. that tag through the fast-tag-selection interface.
  1566. See the manual for details."
  1567. :group 'org-tags
  1568. :type '(repeat
  1569. (choice
  1570. (cons (string :tag "Tag name")
  1571. (character :tag "Access char"))
  1572. (const :tag "Start radio group" (:startgroup))
  1573. (const :tag "End radio group" (:endgroup)))))
  1574. (defvar org-file-tags nil
  1575. "List of tags that can be inherited by all entries in the file.
  1576. The tags will be inherited if the variable `org-use-tag-inheritance'
  1577. says they should be.
  1578. This variable is populated from #+TAG lines.")
  1579. (defcustom org-use-fast-tag-selection 'auto
  1580. "Non-nil means, use fast tag selection scheme.
  1581. This is a special interface to select and deselect tags with single keys.
  1582. When nil, fast selection is never used.
  1583. When the symbol `auto', fast selection is used if and only if selection
  1584. characters for tags have been configured, either through the variable
  1585. `org-tag-alist' or through a #+TAGS line in the buffer.
  1586. When t, fast selection is always used and selection keys are assigned
  1587. automatically if necessary."
  1588. :group 'org-tags
  1589. :type '(choice
  1590. (const :tag "Always" t)
  1591. (const :tag "Never" nil)
  1592. (const :tag "When selection characters are configured" 'auto)))
  1593. (defcustom org-fast-tag-selection-single-key nil
  1594. "Non-nil means, fast tag selection exits after first change.
  1595. When nil, you have to press RET to exit it.
  1596. During fast tag selection, you can toggle this flag with `C-c'.
  1597. This variable can also have the value `expert'. In this case, the window
  1598. displaying the tags menu is not even shown, until you press C-c again."
  1599. :group 'org-tags
  1600. :type '(choice
  1601. (const :tag "No" nil)
  1602. (const :tag "Yes" t)
  1603. (const :tag "Expert" expert)))
  1604. (defvar org-fast-tag-selection-include-todo nil
  1605. "Non-nil means, fast tags selection interface will also offer TODO states.
  1606. This is an undocumented feature, you should not rely on it.")
  1607. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1608. "The column to which tags should be indented in a headline.
  1609. If this number is positive, it specifies the column. If it is negative,
  1610. it means that the tags should be flushright to that column. For example,
  1611. -80 works well for a normal 80 character screen."
  1612. :group 'org-tags
  1613. :type 'integer)
  1614. (defcustom org-auto-align-tags t
  1615. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1616. These operations change the length of a headline and therefore shift
  1617. the tags around. With this options turned on, after each such operation
  1618. the tags are again aligned to `org-tags-column'."
  1619. :group 'org-tags
  1620. :type 'boolean)
  1621. (defcustom org-use-tag-inheritance t
  1622. "Non-nil means, tags in levels apply also for sublevels.
  1623. When nil, only the tags directly given in a specific line apply there.
  1624. If this option is t, a match early-on in a tree can lead to a large
  1625. number of matches in the subtree. If you only want to see the first
  1626. match in a tree during a search, check out the variable
  1627. `org-tags-match-list-sublevels'.
  1628. This may also be a list of tags that should be inherited, or a regexp that
  1629. matches tags that should be inherited."
  1630. :group 'org-tags
  1631. :type '(choice
  1632. (const :tag "Not" nil)
  1633. (const :tag "Always" t)
  1634. (repeat :tag "Specific tags" (string :tag "Tag"))
  1635. (regexp :tag "Tags matched by regexp")))
  1636. (defun org-tag-inherit-p (tag)
  1637. "Check if TAG is one that should be inherited."
  1638. (cond
  1639. ((eq org-use-tag-inheritance t) t)
  1640. ((not org-use-tag-inheritance) nil)
  1641. ((stringp org-use-tag-inheritance)
  1642. (string-match org-use-tag-inheritance tag))
  1643. ((listp org-use-tag-inheritance)
  1644. (member tag org-use-tag-inheritance))
  1645. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1646. (defcustom org-tags-match-list-sublevels t
  1647. "Non-nil means list also sublevels of headlines matching tag search.
  1648. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1649. the sublevels of a headline matching a tag search often also match
  1650. the same search. Listing all of them can create very long lists.
  1651. Setting this variable to nil causes subtrees of a match to be skipped.
  1652. This option is off by default, because inheritance in on. If you turn
  1653. inheritance off, you very likely want to turn this option on.
  1654. As a special case, if the tag search is restricted to TODO items, the
  1655. value of this variable is ignored and sublevels are always checked, to
  1656. make sure all corresponding TODO items find their way into the list."
  1657. :group 'org-tags
  1658. :type 'boolean)
  1659. (defvar org-tags-history nil
  1660. "History of minibuffer reads for tags.")
  1661. (defvar org-last-tags-completion-table nil
  1662. "The last used completion table for tags.")
  1663. (defvar org-after-tags-change-hook nil
  1664. "Hook that is run after the tags in a line have changed.")
  1665. (defgroup org-properties nil
  1666. "Options concerning properties in Org-mode."
  1667. :tag "Org Properties"
  1668. :group 'org)
  1669. (defcustom org-property-format "%-10s %s"
  1670. "How property key/value pairs should be formatted by `indent-line'.
  1671. When `indent-line' hits a property definition, it will format the line
  1672. according to this format, mainly to make sure that the values are
  1673. lined-up with respect to each other."
  1674. :group 'org-properties
  1675. :type 'string)
  1676. (defcustom org-use-property-inheritance nil
  1677. "Non-nil means, properties apply also for sublevels.
  1678. This setting is chiefly used during property searches. Turning it on can
  1679. cause significant overhead when doing a search, which is why it is not
  1680. on by default.
  1681. When nil, only the properties directly given in the current entry count.
  1682. When t, every property is inherited. The value may also be a list of
  1683. properties that should have inheritance, or a regular expression matching
  1684. properties that should be inherited.
  1685. However, note that some special properties use inheritance under special
  1686. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1687. and the properties ending in \"_ALL\" when they are used as descriptor
  1688. for valid values of a property.
  1689. Note for programmers:
  1690. When querying an entry with `org-entry-get', you can control if inheritance
  1691. should be used. By default, `org-entry-get' looks only at the local
  1692. properties. You can request inheritance by setting the inherit argument
  1693. to t (to force inheritance) or to `selective' (to respect the setting
  1694. in this variable)."
  1695. :group 'org-properties
  1696. :type '(choice
  1697. (const :tag "Not" nil)
  1698. (const :tag "Always" t)
  1699. (repeat :tag "Specific properties" (string :tag "Property"))
  1700. (regexp :tag "Properties matched by regexp")))
  1701. (defun org-property-inherit-p (property)
  1702. "Check if PROPERTY is one that should be inherited."
  1703. (cond
  1704. ((eq org-use-property-inheritance t) t)
  1705. ((not org-use-property-inheritance) nil)
  1706. ((stringp org-use-property-inheritance)
  1707. (string-match org-use-property-inheritance property))
  1708. ((listp org-use-property-inheritance)
  1709. (member property org-use-property-inheritance))
  1710. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1711. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1712. "The default column format, if no other format has been defined.
  1713. This variable can be set on the per-file basis by inserting a line
  1714. #+COLUMNS: %25ITEM ....."
  1715. :group 'org-properties
  1716. :type 'string)
  1717. (defcustom org-columns-ellipses ".."
  1718. "The ellipses to be used when a field in column view is truncated.
  1719. When this is the empty string, as many characters as possible are shown,
  1720. but then there will be no visual indication that the field has been truncated.
  1721. When this is a string of length N, the last N characters of a truncated
  1722. field are replaced by this string. If the column is narrower than the
  1723. ellipses string, only part of the ellipses string will be shown."
  1724. :group 'org-properties
  1725. :type 'string)
  1726. (defcustom org-columns-modify-value-for-display-function nil
  1727. "Function that modifies values for display in column view.
  1728. For example, it can be used to cut out a certain part from a time stamp.
  1729. The function must take 2 argments:
  1730. column-title The tite of the column (*not* the property name)
  1731. value The value that should be modified.
  1732. The function should return the value that should be displayed,
  1733. or nil if the normal value should be used."
  1734. :group 'org-properties
  1735. :type 'function)
  1736. (defcustom org-effort-property "Effort"
  1737. "The property that is being used to keep track of effort estimates.
  1738. Effort estimates given in this property need to have the format H:MM."
  1739. :group 'org-properties
  1740. :group 'org-progress
  1741. :type '(string :tag "Property"))
  1742. (defconst org-global-properties-fixed
  1743. '(("VISIBILITY_ALL" . "folded children content all"))
  1744. "List of property/value pairs that can be inherited by any entry.
  1745. These are fixed values, for the preset properties.")
  1746. (defcustom org-global-properties nil
  1747. "List of property/value pairs that can be inherited by any entry.
  1748. You can set buffer-local values for this by adding lines like
  1749. #+PROPERTY: NAME VALUE"
  1750. :group 'org-properties
  1751. :type '(repeat
  1752. (cons (string :tag "Property")
  1753. (string :tag "Value"))))
  1754. (defvar org-file-properties nil
  1755. "List of property/value pairs that can be inherited by any entry.
  1756. Valid for the current buffer.
  1757. This variable is populated from #+PROPERTY lines.")
  1758. (make-variable-buffer-local 'org-file-properties)
  1759. (defgroup org-agenda nil
  1760. "Options concerning agenda views in Org-mode."
  1761. :tag "Org Agenda"
  1762. :group 'org)
  1763. (defvar org-category nil
  1764. "Variable used by org files to set a category for agenda display.
  1765. Such files should use a file variable to set it, for example
  1766. # -*- mode: org; org-category: \"ELisp\"
  1767. or contain a special line
  1768. #+CATEGORY: ELisp
  1769. If the file does not specify a category, then file's base name
  1770. is used instead.")
  1771. (make-variable-buffer-local 'org-category)
  1772. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1773. (defcustom org-agenda-files nil
  1774. "The files to be used for agenda display.
  1775. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1776. \\[org-remove-file]. You can also use customize to edit the list.
  1777. If an entry is a directory, all files in that directory that are matched by
  1778. `org-agenda-file-regexp' will be part of the file list.
  1779. If the value of the variable is not a list but a single file name, then
  1780. the list of agenda files is actually stored and maintained in that file, one
  1781. agenda file per line."
  1782. :group 'org-agenda
  1783. :type '(choice
  1784. (repeat :tag "List of files and directories" file)
  1785. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1786. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1787. "Regular expression to match files for `org-agenda-files'.
  1788. If any element in the list in that variable contains a directory instead
  1789. of a normal file, all files in that directory that are matched by this
  1790. regular expression will be included."
  1791. :group 'org-agenda
  1792. :type 'regexp)
  1793. (defcustom org-agenda-text-search-extra-files nil
  1794. "List of extra files to be searched by text search commands.
  1795. These files will be search in addition to the agenda files by the
  1796. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1797. Note that these files will only be searched for text search commands,
  1798. not for the other agenda views like todo lists, tag searches or the weekly
  1799. agenda. This variable is intended to list notes and possibly archive files
  1800. that should also be searched by these two commands.
  1801. In fact, if the first element in the list is the symbol `agenda-archives',
  1802. than all archive files of all agenda files will be added to the search
  1803. scope."
  1804. :group 'org-agenda
  1805. :type '(set :greedy t
  1806. (const :tag "Agenda Archives" agenda-archives)
  1807. (repeat :inline t (file))))
  1808. (if (fboundp 'defvaralias)
  1809. (defvaralias 'org-agenda-multi-occur-extra-files
  1810. 'org-agenda-text-search-extra-files))
  1811. (defcustom org-agenda-skip-unavailable-files nil
  1812. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1813. A nil value means to remove them, after a query, from the list."
  1814. :group 'org-agenda
  1815. :type 'boolean)
  1816. (defcustom org-calendar-to-agenda-key [?c]
  1817. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1818. The command `org-calendar-goto-agenda' will be bound to this key. The
  1819. default is the character `c' because then `c' can be used to switch back and
  1820. forth between agenda and calendar."
  1821. :group 'org-agenda
  1822. :type 'sexp)
  1823. (defcustom org-calendar-agenda-action-key [?k]
  1824. "The key to be installed in `calendar-mode-map' for agenda-action.
  1825. The command `org-agenda-action' will be bound to this key. The
  1826. default is the character `k' because we use the same key in the agenda."
  1827. :group 'org-agenda
  1828. :type 'sexp)
  1829. (eval-after-load "calendar"
  1830. '(progn
  1831. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1832. 'org-calendar-goto-agenda)
  1833. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1834. 'org-agenda-action)))
  1835. (defgroup org-latex nil
  1836. "Options for embedding LaTeX code into Org-mode."
  1837. :tag "Org LaTeX"
  1838. :group 'org)
  1839. (defcustom org-format-latex-options
  1840. '(:foreground default :background default :scale 1.0
  1841. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1842. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1843. "Options for creating images from LaTeX fragments.
  1844. This is a property list with the following properties:
  1845. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1846. `default' means use the foreground of the default face.
  1847. :background the background color, or \"Transparent\".
  1848. `default' means use the background of the default face.
  1849. :scale a scaling factor for the size of the images.
  1850. :html-foreground, :html-background, :html-scale
  1851. the same numbers for HTML export.
  1852. :matchers a list indicating which matchers should be used to
  1853. find LaTeX fragments. Valid members of this list are:
  1854. \"begin\" find environments
  1855. \"$\" find math expressions surrounded by $...$
  1856. \"$$\" find math expressions surrounded by $$....$$
  1857. \"\\(\" find math expressions surrounded by \\(...\\)
  1858. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1859. :group 'org-latex
  1860. :type 'plist)
  1861. (defcustom org-format-latex-header "\\documentclass{article}
  1862. \\usepackage{fullpage} % do not remove
  1863. \\usepackage{amssymb}
  1864. \\usepackage[usenames]{color}
  1865. \\usepackage{amsmath}
  1866. \\usepackage{latexsym}
  1867. \\usepackage[mathscr]{eucal}
  1868. \\pagestyle{empty} % do not remove"
  1869. "The document header used for processing LaTeX fragments."
  1870. :group 'org-latex
  1871. :type 'string)
  1872. (defgroup org-font-lock nil
  1873. "Font-lock settings for highlighting in Org-mode."
  1874. :tag "Org Font Lock"
  1875. :group 'org)
  1876. (defcustom org-level-color-stars-only nil
  1877. "Non-nil means fontify only the stars in each headline.
  1878. When nil, the entire headline is fontified.
  1879. Changing it requires restart of `font-lock-mode' to become effective
  1880. also in regions already fontified."
  1881. :group 'org-font-lock
  1882. :type 'boolean)
  1883. (defcustom org-hide-leading-stars nil
  1884. "Non-nil means, hide the first N-1 stars in a headline.
  1885. This works by using the face `org-hide' for these stars. This
  1886. face is white for a light background, and black for a dark
  1887. background. You may have to customize the face `org-hide' to
  1888. make this work.
  1889. Changing it requires restart of `font-lock-mode' to become effective
  1890. also in regions already fontified.
  1891. You may also set this on a per-file basis by adding one of the following
  1892. lines to the buffer:
  1893. #+STARTUP: hidestars
  1894. #+STARTUP: showstars"
  1895. :group 'org-font-lock
  1896. :type 'boolean)
  1897. (defcustom org-fontify-done-headline nil
  1898. "Non-nil means, change the face of a headline if it is marked DONE.
  1899. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1900. When this is non-nil, the headline after the keyword is set to the
  1901. `org-headline-done' as an additional indication."
  1902. :group 'org-font-lock
  1903. :type 'boolean)
  1904. (defcustom org-fontify-emphasized-text t
  1905. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1906. Changing this variable requires a restart of Emacs to take effect."
  1907. :group 'org-font-lock
  1908. :type 'boolean)
  1909. (defcustom org-highlight-latex-fragments-and-specials nil
  1910. "Non-nil means, fontify what is treated specially by the exporters."
  1911. :group 'org-font-lock
  1912. :type 'boolean)
  1913. (defcustom org-hide-emphasis-markers nil
  1914. "Non-nil mean font-lock should hide the emphasis marker characters."
  1915. :group 'org-font-lock
  1916. :type 'boolean)
  1917. (defvar org-emph-re nil
  1918. "Regular expression for matching emphasis.")
  1919. (defvar org-verbatim-re nil
  1920. "Regular expression for matching verbatim text.")
  1921. (defvar org-emphasis-regexp-components) ; defined just below
  1922. (defvar org-emphasis-alist) ; defined just below
  1923. (defun org-set-emph-re (var val)
  1924. "Set variable and compute the emphasis regular expression."
  1925. (set var val)
  1926. (when (and (boundp 'org-emphasis-alist)
  1927. (boundp 'org-emphasis-regexp-components)
  1928. org-emphasis-alist org-emphasis-regexp-components)
  1929. (let* ((e org-emphasis-regexp-components)
  1930. (pre (car e))
  1931. (post (nth 1 e))
  1932. (border (nth 2 e))
  1933. (body (nth 3 e))
  1934. (nl (nth 4 e))
  1935. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1936. (body1 (concat body "*?"))
  1937. (markers (mapconcat 'car org-emphasis-alist ""))
  1938. (vmarkers (mapconcat
  1939. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1940. org-emphasis-alist "")))
  1941. ;; make sure special characters appear at the right position in the class
  1942. (if (string-match "\\^" markers)
  1943. (setq markers (concat (replace-match "" t t markers) "^")))
  1944. (if (string-match "-" markers)
  1945. (setq markers (concat (replace-match "" t t markers) "-")))
  1946. (if (string-match "\\^" vmarkers)
  1947. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1948. (if (string-match "-" vmarkers)
  1949. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1950. (if (> nl 0)
  1951. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1952. (int-to-string nl) "\\}")))
  1953. ;; Make the regexp
  1954. (setq org-emph-re
  1955. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1956. "\\("
  1957. "\\([" markers "]\\)"
  1958. "\\("
  1959. "[^" border "]\\|"
  1960. "[^" border (if (and nil stacked) markers) "]"
  1961. body1
  1962. "[^" border (if (and nil stacked) markers) "]"
  1963. "\\)"
  1964. "\\3\\)"
  1965. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1966. (setq org-verbatim-re
  1967. (concat "\\([" pre "]\\|^\\)"
  1968. "\\("
  1969. "\\([" vmarkers "]\\)"
  1970. "\\("
  1971. "[^" border "]\\|"
  1972. "[^" border "]"
  1973. body1
  1974. "[^" border "]"
  1975. "\\)"
  1976. "\\3\\)"
  1977. "\\([" post "]\\|$\\)")))))
  1978. (defcustom org-emphasis-regexp-components
  1979. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1980. "Components used to build the regular expression for emphasis.
  1981. This is a list with 6 entries. Terminology: In an emphasis string
  1982. like \" *strong word* \", we call the initial space PREMATCH, the final
  1983. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1984. and \"trong wor\" is the body. The different components in this variable
  1985. specify what is allowed/forbidden in each part:
  1986. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1987. post Chars allowed as postmatch. End of line will be allowed too.
  1988. border The chars *forbidden* as border characters.
  1989. body-regexp A regexp like \".\" to match a body character. Don't use
  1990. non-shy groups here, and don't allow newline here.
  1991. newline The maximum number of newlines allowed in an emphasis exp.
  1992. Use customize to modify this, or restart Emacs after changing it."
  1993. :group 'org-font-lock
  1994. :set 'org-set-emph-re
  1995. :type '(list
  1996. (sexp :tag "Allowed chars in pre ")
  1997. (sexp :tag "Allowed chars in post ")
  1998. (sexp :tag "Forbidden chars in border ")
  1999. (sexp :tag "Regexp for body ")
  2000. (integer :tag "number of newlines allowed")
  2001. (option (boolean :tag "Please ignore this button"))))
  2002. (defcustom org-emphasis-alist
  2003. `(("*" bold "<b>" "</b>")
  2004. ("/" italic "<i>" "</i>")
  2005. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2006. ("=" org-code "<code>" "</code>" verbatim)
  2007. ("~" org-verbatim "<code>" "</code>" verbatim)
  2008. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2009. "<del>" "</del>")
  2010. )
  2011. "Special syntax for emphasized text.
  2012. Text starting and ending with a special character will be emphasized, for
  2013. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2014. characters, the face to be used by font-lock for highlighting in Org-mode
  2015. Emacs buffers, and the HTML tags to be used for this.
  2016. Use customize to modify this, or restart Emacs after changing it."
  2017. :group 'org-font-lock
  2018. :set 'org-set-emph-re
  2019. :type '(repeat
  2020. (list
  2021. (string :tag "Marker character")
  2022. (choice
  2023. (face :tag "Font-lock-face")
  2024. (plist :tag "Face property list"))
  2025. (string :tag "HTML start tag")
  2026. (string :tag "HTML end tag")
  2027. (option (const verbatim)))))
  2028. ;;; Miscellaneous options
  2029. (defgroup org-completion nil
  2030. "Completion in Org-mode."
  2031. :tag "Org Completion"
  2032. :group 'org)
  2033. (defcustom org-completion-fallback-command 'hippie-expand
  2034. "The expansion command called by \\[org-complete] in normal context.
  2035. Normal means, no org-mode-specific context."
  2036. :group 'org-completion
  2037. :type 'function)
  2038. ;;; Functions and variables from ther packages
  2039. ;; Declared here to avoid compiler warnings
  2040. ;; XEmacs only
  2041. (defvar outline-mode-menu-heading)
  2042. (defvar outline-mode-menu-show)
  2043. (defvar outline-mode-menu-hide)
  2044. (defvar zmacs-regions) ; XEmacs regions
  2045. ;; Emacs only
  2046. (defvar mark-active)
  2047. ;; Various packages
  2048. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2049. (declare-function calendar-forward-day "cal-move" (arg))
  2050. (declare-function calendar-goto-date "cal-move" (date))
  2051. (declare-function calendar-goto-today "cal-move" ())
  2052. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2053. (defvar calc-embedded-close-formula)
  2054. (defvar calc-embedded-open-formula)
  2055. (declare-function cdlatex-tab "ext:cdlatex" ())
  2056. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2057. (defvar font-lock-unfontify-region-function)
  2058. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2059. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2060. (defvar iswitchb-temp-buflist)
  2061. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2062. (declare-function org-agenda-skip "org-agenda" ())
  2063. (declare-function org-format-agenda-item "org-agenda"
  2064. (extra txt &optional category tags dotime noprefix remove-re))
  2065. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2066. (declare-function org-agenda-change-all-lines "org-agenda"
  2067. (newhead hdmarker &optional fixface))
  2068. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2069. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2070. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2071. (beg end))
  2072. (declare-function parse-time-string "parse-time" (string))
  2073. (declare-function remember "remember" (&optional initial))
  2074. (declare-function remember-buffer-desc "remember" ())
  2075. (declare-function remember-finalize "remember" ())
  2076. (defvar remember-save-after-remembering)
  2077. (defvar remember-data-file)
  2078. (defvar remember-register)
  2079. (defvar remember-buffer)
  2080. (defvar remember-handler-functions)
  2081. (defvar remember-annotation-functions)
  2082. (defvar texmathp-why)
  2083. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2084. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2085. (defvar w3m-current-url)
  2086. (defvar w3m-current-title)
  2087. (defvar org-latex-regexps)
  2088. ;;; Autoload and prepare some org modules
  2089. ;; Some table stuff that needs to be defined here, because it is used
  2090. ;; by the functions setting up org-mode or checking for table context.
  2091. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2092. "Detects an org-type or table-type table.")
  2093. (defconst org-table-line-regexp "^[ \t]*|"
  2094. "Detects an org-type table line.")
  2095. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2096. "Detects an org-type table line.")
  2097. (defconst org-table-hline-regexp "^[ \t]*|-"
  2098. "Detects an org-type table hline.")
  2099. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2100. "Detects a table-type table hline.")
  2101. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2102. "Searching from within a table (any type) this finds the first line
  2103. outside the table.")
  2104. ;; Autoload the functions in org-table.el that are needed by functions here.
  2105. (eval-and-compile
  2106. (org-autoload "org-table"
  2107. '(org-table-align org-table-begin org-table-blank-field
  2108. org-table-convert org-table-convert-region org-table-copy-down
  2109. org-table-copy-region org-table-create
  2110. org-table-create-or-convert-from-region
  2111. org-table-create-with-table.el org-table-current-dline
  2112. org-table-cut-region org-table-delete-column org-table-edit-field
  2113. org-table-edit-formulas org-table-end org-table-eval-formula
  2114. org-table-export org-table-field-info
  2115. org-table-get-stored-formulas org-table-goto-column
  2116. org-table-hline-and-move org-table-import org-table-insert-column
  2117. org-table-insert-hline org-table-insert-row org-table-iterate
  2118. org-table-justify-field-maybe org-table-kill-row
  2119. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2120. org-table-move-column org-table-move-column-left
  2121. org-table-move-column-right org-table-move-row
  2122. org-table-move-row-down org-table-move-row-up
  2123. org-table-next-field org-table-next-row org-table-paste-rectangle
  2124. org-table-previous-field org-table-recalculate
  2125. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2126. org-table-toggle-coordinate-overlays
  2127. org-table-toggle-formula-debugger org-table-wrap-region
  2128. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2129. (defun org-at-table-p (&optional table-type)
  2130. "Return t if the cursor is inside an org-type table.
  2131. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2132. (if org-enable-table-editor
  2133. (save-excursion
  2134. (beginning-of-line 1)
  2135. (looking-at (if table-type org-table-any-line-regexp
  2136. org-table-line-regexp)))
  2137. nil))
  2138. (defsubst org-table-p () (org-at-table-p))
  2139. (defun org-at-table.el-p ()
  2140. "Return t if and only if we are at a table.el table."
  2141. (and (org-at-table-p 'any)
  2142. (save-excursion
  2143. (goto-char (org-table-begin 'any))
  2144. (looking-at org-table1-hline-regexp))))
  2145. (defun org-table-recognize-table.el ()
  2146. "If there is a table.el table nearby, recognize it and move into it."
  2147. (if org-table-tab-recognizes-table.el
  2148. (if (org-at-table.el-p)
  2149. (progn
  2150. (beginning-of-line 1)
  2151. (if (looking-at org-table-dataline-regexp)
  2152. nil
  2153. (if (looking-at org-table1-hline-regexp)
  2154. (progn
  2155. (beginning-of-line 2)
  2156. (if (looking-at org-table-any-border-regexp)
  2157. (beginning-of-line -1)))))
  2158. (if (re-search-forward "|" (org-table-end t) t)
  2159. (progn
  2160. (require 'table)
  2161. (if (table--at-cell-p (point))
  2162. t
  2163. (message "recognizing table.el table...")
  2164. (table-recognize-table)
  2165. (message "recognizing table.el table...done")))
  2166. (error "This should not happen..."))
  2167. t)
  2168. nil)
  2169. nil))
  2170. (defun org-at-table-hline-p ()
  2171. "Return t if the cursor is inside a hline in a table."
  2172. (if org-enable-table-editor
  2173. (save-excursion
  2174. (beginning-of-line 1)
  2175. (looking-at org-table-hline-regexp))
  2176. nil))
  2177. (defvar org-table-clean-did-remove-column nil)
  2178. (defun org-table-map-tables (function)
  2179. "Apply FUNCTION to the start of all tables in the buffer."
  2180. (save-excursion
  2181. (save-restriction
  2182. (widen)
  2183. (goto-char (point-min))
  2184. (while (re-search-forward org-table-any-line-regexp nil t)
  2185. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2186. (beginning-of-line 1)
  2187. (if (looking-at org-table-line-regexp)
  2188. (save-excursion (funcall function)))
  2189. (re-search-forward org-table-any-border-regexp nil 1))))
  2190. (message "Mapping tables: done"))
  2191. ;; Declare and autoload functions from org-exp.el
  2192. (declare-function org-default-export-plist "org-exp")
  2193. (declare-function org-infile-export-plist "org-exp")
  2194. (declare-function org-get-current-options "org-exp")
  2195. (eval-and-compile
  2196. (org-autoload "org-exp"
  2197. '(org-export org-export-as-ascii org-export-visible
  2198. org-insert-export-options-template org-export-as-html-and-open
  2199. org-export-as-html-batch org-export-as-html-to-buffer
  2200. org-replace-region-by-html org-export-region-as-html
  2201. org-export-as-html org-export-icalendar-this-file
  2202. org-export-icalendar-all-agenda-files
  2203. org-table-clean-before-export
  2204. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2205. ;; Declare and autoload functions from org-agenda.el
  2206. (eval-and-compile
  2207. (org-autoload "org-agenda"
  2208. '(org-agenda org-agenda-list org-search-view
  2209. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2210. org-diary org-agenda-to-appt)))
  2211. ;; Autoload org-remember
  2212. (eval-and-compile
  2213. (org-autoload "org-remember"
  2214. '(org-remember-insinuate org-remember-annotation
  2215. org-remember-apply-template org-remember org-remember-handler)))
  2216. ;; Autoload org-clock.el
  2217. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2218. (beg end))
  2219. (declare-function org-update-mode-line "org-clock" ())
  2220. (defvar org-clock-start-time)
  2221. (defvar org-clock-marker (make-marker)
  2222. "Marker recording the last clock-in.")
  2223. (eval-and-compile
  2224. (org-autoload
  2225. "org-clock"
  2226. '(org-clock-in org-clock-out org-clock-cancel
  2227. org-clock-goto org-clock-sum org-clock-display
  2228. org-remove-clock-overlays org-clock-report
  2229. org-clocktable-shift org-dblock-write:clocktable
  2230. org-get-clocktable)))
  2231. (defun org-clock-update-time-maybe ()
  2232. "If this is a CLOCK line, update it and return t.
  2233. Otherwise, return nil."
  2234. (interactive)
  2235. (save-excursion
  2236. (beginning-of-line 1)
  2237. (skip-chars-forward " \t")
  2238. (when (looking-at org-clock-string)
  2239. (let ((re (concat "[ \t]*" org-clock-string
  2240. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2241. "\\([ \t]*=>.*\\)?\\)?"))
  2242. ts te h m s neg)
  2243. (cond
  2244. ((not (looking-at re))
  2245. nil)
  2246. ((not (match-end 2))
  2247. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2248. (> org-clock-marker (point))
  2249. (<= org-clock-marker (point-at-eol)))
  2250. ;; The clock is running here
  2251. (setq org-clock-start-time
  2252. (apply 'encode-time
  2253. (org-parse-time-string (match-string 1))))
  2254. (org-update-mode-line)))
  2255. (t
  2256. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2257. (end-of-line 1)
  2258. (setq ts (match-string 1)
  2259. te (match-string 3))
  2260. (setq s (- (time-to-seconds
  2261. (apply 'encode-time (org-parse-time-string te)))
  2262. (time-to-seconds
  2263. (apply 'encode-time (org-parse-time-string ts))))
  2264. neg (< s 0)
  2265. s (abs s)
  2266. h (floor (/ s 3600))
  2267. s (- s (* 3600 h))
  2268. m (floor (/ s 60))
  2269. s (- s (* 60 s)))
  2270. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2271. t))))))
  2272. (defun org-check-running-clock ()
  2273. "Check if the current buffer contains the running clock.
  2274. If yes, offer to stop it and to save the buffer with the changes."
  2275. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2276. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2277. (buffer-name))))
  2278. (org-clock-out)
  2279. (when (y-or-n-p "Save changed buffer?")
  2280. (save-buffer))))
  2281. (defun org-clocktable-try-shift (dir n)
  2282. "Check if this line starts a clock table, if yes, shift the time block."
  2283. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2284. (org-clocktable-shift dir n)))
  2285. ;; Autoload archiving code
  2286. ;; The stuff that is needed for cycling and tags has to be defined here.
  2287. (defgroup org-archive nil
  2288. "Options concerning archiving in Org-mode."
  2289. :tag "Org Archive"
  2290. :group 'org-structure)
  2291. (defcustom org-archive-location "%s_archive::"
  2292. "The location where subtrees should be archived.
  2293. Otherwise, the value of this variable is a string, consisting of two
  2294. parts, separated by a double-colon.
  2295. The first part is a file name - when omitted, archiving happens in the same
  2296. file. %s will be replaced by the current file name (without directory part).
  2297. Archiving to a different file is useful to keep archived entries from
  2298. contributing to the Org-mode Agenda.
  2299. The part after the double colon is a headline. The archived entries will be
  2300. filed under that headline. When omitted, the subtrees are simply filed away
  2301. at the end of the file, as top-level entries.
  2302. Here are a few examples:
  2303. \"%s_archive::\"
  2304. If the current file is Projects.org, archive in file
  2305. Projects.org_archive, as top-level trees. This is the default.
  2306. \"::* Archived Tasks\"
  2307. Archive in the current file, under the top-level headline
  2308. \"* Archived Tasks\".
  2309. \"~/org/archive.org::\"
  2310. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2311. \"basement::** Finished Tasks\"
  2312. Archive in file ./basement (relative path), as level 3 trees
  2313. below the level 2 heading \"** Finished Tasks\".
  2314. You may set this option on a per-file basis by adding to the buffer a
  2315. line like
  2316. #+ARCHIVE: basement::** Finished Tasks
  2317. You may also define it locally for a subtree by setting an ARCHIVE property
  2318. in the entry. If such a property is found in an entry, or anywhere up
  2319. the hierarchy, it will be used."
  2320. :group 'org-archive
  2321. :type 'string)
  2322. (defcustom org-archive-tag "ARCHIVE"
  2323. "The tag that marks a subtree as archived.
  2324. An archived subtree does not open during visibility cycling, and does
  2325. not contribute to the agenda listings.
  2326. After changing this, font-lock must be restarted in the relevant buffers to
  2327. get the proper fontification."
  2328. :group 'org-archive
  2329. :group 'org-keywords
  2330. :type 'string)
  2331. (defcustom org-agenda-skip-archived-trees t
  2332. "Non-nil means, the agenda will skip any items located in archived trees.
  2333. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2334. variable is no longer recommended, you should leave it at the value t.
  2335. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2336. :group 'org-archive
  2337. :group 'org-agenda-skip
  2338. :type 'boolean)
  2339. (defcustom org-cycle-open-archived-trees nil
  2340. "Non-nil means, `org-cycle' will open archived trees.
  2341. An archived tree is a tree marked with the tag ARCHIVE.
  2342. When nil, archived trees will stay folded. You can still open them with
  2343. normal outline commands like `show-all', but not with the cycling commands."
  2344. :group 'org-archive
  2345. :group 'org-cycle
  2346. :type 'boolean)
  2347. (defcustom org-sparse-tree-open-archived-trees nil
  2348. "Non-nil means sparse tree construction shows matches in archived trees.
  2349. When nil, matches in these trees are highlighted, but the trees are kept in
  2350. collapsed state."
  2351. :group 'org-archive
  2352. :group 'org-sparse-trees
  2353. :type 'boolean)
  2354. (defun org-cycle-hide-archived-subtrees (state)
  2355. "Re-hide all archived subtrees after a visibility state change."
  2356. (when (and (not org-cycle-open-archived-trees)
  2357. (not (memq state '(overview folded))))
  2358. (save-excursion
  2359. (let* ((globalp (memq state '(contents all)))
  2360. (beg (if globalp (point-min) (point)))
  2361. (end (if globalp (point-max) (org-end-of-subtree t))))
  2362. (org-hide-archived-subtrees beg end)
  2363. (goto-char beg)
  2364. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2365. (message "%s" (substitute-command-keys
  2366. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2367. (defun org-force-cycle-archived ()
  2368. "Cycle subtree even if it is archived."
  2369. (interactive)
  2370. (setq this-command 'org-cycle)
  2371. (let ((org-cycle-open-archived-trees t))
  2372. (call-interactively 'org-cycle)))
  2373. (defun org-hide-archived-subtrees (beg end)
  2374. "Re-hide all archived subtrees after a visibility state change."
  2375. (save-excursion
  2376. (let* ((re (concat ":" org-archive-tag ":")))
  2377. (goto-char beg)
  2378. (while (re-search-forward re end t)
  2379. (and (org-on-heading-p) (hide-subtree))
  2380. (org-end-of-subtree t)))))
  2381. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2382. (eval-and-compile
  2383. (org-autoload "org-archive"
  2384. '(org-add-archive-files org-archive-subtree
  2385. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2386. ;; Autoload Column View Code
  2387. (declare-function org-columns-number-to-string "org-colview")
  2388. (declare-function org-columns-get-format-and-top-level "org-colview")
  2389. (declare-function org-columns-compute "org-colview")
  2390. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2391. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2392. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2393. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2394. ;; Autoload ID code
  2395. (org-autoload "org-id"
  2396. '(org-id-get-create org-id-new org-id-copy org-id-get
  2397. org-id-get-with-outline-path-completion
  2398. org-id-get-with-outline-drilling
  2399. org-id-goto org-id-find))
  2400. ;;; Variables for pre-computed regular expressions, all buffer local
  2401. (defvar org-drawer-regexp nil
  2402. "Matches first line of a hidden block.")
  2403. (make-variable-buffer-local 'org-drawer-regexp)
  2404. (defvar org-todo-regexp nil
  2405. "Matches any of the TODO state keywords.")
  2406. (make-variable-buffer-local 'org-todo-regexp)
  2407. (defvar org-not-done-regexp nil
  2408. "Matches any of the TODO state keywords except the last one.")
  2409. (make-variable-buffer-local 'org-not-done-regexp)
  2410. (defvar org-todo-line-regexp nil
  2411. "Matches a headline and puts TODO state into group 2 if present.")
  2412. (make-variable-buffer-local 'org-todo-line-regexp)
  2413. (defvar org-complex-heading-regexp nil
  2414. "Matches a headline and puts everything into groups:
  2415. group 1: the stars
  2416. group 2: The todo keyword, maybe
  2417. group 3: Priority cookie
  2418. group 4: True headline
  2419. group 5: Tags")
  2420. (make-variable-buffer-local 'org-complex-heading-regexp)
  2421. (defvar org-todo-line-tags-regexp nil
  2422. "Matches a headline and puts TODO state into group 2 if present.
  2423. Also put tags into group 4 if tags are present.")
  2424. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2425. (defvar org-nl-done-regexp nil
  2426. "Matches newline followed by a headline with the DONE keyword.")
  2427. (make-variable-buffer-local 'org-nl-done-regexp)
  2428. (defvar org-looking-at-done-regexp nil
  2429. "Matches the DONE keyword a point.")
  2430. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2431. (defvar org-ds-keyword-length 12
  2432. "Maximum length of the Deadline and SCHEDULED keywords.")
  2433. (make-variable-buffer-local 'org-ds-keyword-length)
  2434. (defvar org-deadline-regexp nil
  2435. "Matches the DEADLINE keyword.")
  2436. (make-variable-buffer-local 'org-deadline-regexp)
  2437. (defvar org-deadline-time-regexp nil
  2438. "Matches the DEADLINE keyword together with a time stamp.")
  2439. (make-variable-buffer-local 'org-deadline-time-regexp)
  2440. (defvar org-deadline-line-regexp nil
  2441. "Matches the DEADLINE keyword and the rest of the line.")
  2442. (make-variable-buffer-local 'org-deadline-line-regexp)
  2443. (defvar org-scheduled-regexp nil
  2444. "Matches the SCHEDULED keyword.")
  2445. (make-variable-buffer-local 'org-scheduled-regexp)
  2446. (defvar org-scheduled-time-regexp nil
  2447. "Matches the SCHEDULED keyword together with a time stamp.")
  2448. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2449. (defvar org-closed-time-regexp nil
  2450. "Matches the CLOSED keyword together with a time stamp.")
  2451. (make-variable-buffer-local 'org-closed-time-regexp)
  2452. (defvar org-keyword-time-regexp nil
  2453. "Matches any of the 4 keywords, together with the time stamp.")
  2454. (make-variable-buffer-local 'org-keyword-time-regexp)
  2455. (defvar org-keyword-time-not-clock-regexp nil
  2456. "Matches any of the 3 keywords, together with the time stamp.")
  2457. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2458. (defvar org-maybe-keyword-time-regexp nil
  2459. "Matches a timestamp, possibly preceeded by a keyword.")
  2460. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2461. (defvar org-planning-or-clock-line-re nil
  2462. "Matches a line with planning or clock info.")
  2463. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2464. (defconst org-plain-time-of-day-regexp
  2465. (concat
  2466. "\\(\\<[012]?[0-9]"
  2467. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2468. "\\(--?"
  2469. "\\(\\<[012]?[0-9]"
  2470. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2471. "\\)?")
  2472. "Regular expression to match a plain time or time range.
  2473. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2474. groups carry important information:
  2475. 0 the full match
  2476. 1 the first time, range or not
  2477. 8 the second time, if it is a range.")
  2478. (defconst org-plain-time-extension-regexp
  2479. (concat
  2480. "\\(\\<[012]?[0-9]"
  2481. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2482. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2483. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2484. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2485. groups carry important information:
  2486. 0 the full match
  2487. 7 hours of duration
  2488. 9 minutes of duration")
  2489. (defconst org-stamp-time-of-day-regexp
  2490. (concat
  2491. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2492. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2493. "\\(--?"
  2494. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2495. "Regular expression to match a timestamp time or time range.
  2496. After a match, the following groups carry important information:
  2497. 0 the full match
  2498. 1 date plus weekday, for backreferencing to make sure both times on same day
  2499. 2 the first time, range or not
  2500. 4 the second time, if it is a range.")
  2501. (defconst org-startup-options
  2502. '(("fold" org-startup-folded t)
  2503. ("overview" org-startup-folded t)
  2504. ("nofold" org-startup-folded nil)
  2505. ("showall" org-startup-folded nil)
  2506. ("content" org-startup-folded content)
  2507. ("hidestars" org-hide-leading-stars t)
  2508. ("showstars" org-hide-leading-stars nil)
  2509. ("odd" org-odd-levels-only t)
  2510. ("oddeven" org-odd-levels-only nil)
  2511. ("align" org-startup-align-all-tables t)
  2512. ("noalign" org-startup-align-all-tables nil)
  2513. ("customtime" org-display-custom-times t)
  2514. ("logdone" org-log-done time)
  2515. ("lognotedone" org-log-done note)
  2516. ("nologdone" org-log-done nil)
  2517. ("lognoteclock-out" org-log-note-clock-out t)
  2518. ("nolognoteclock-out" org-log-note-clock-out nil)
  2519. ("logrepeat" org-log-repeat state)
  2520. ("lognoterepeat" org-log-repeat note)
  2521. ("nologrepeat" org-log-repeat nil)
  2522. ("constcgs" constants-unit-system cgs)
  2523. ("constSI" constants-unit-system SI))
  2524. "Variable associated with STARTUP options for org-mode.
  2525. Each element is a list of three items: The startup options as written
  2526. in the #+STARTUP line, the corresponding variable, and the value to
  2527. set this variable to if the option is found. An optional forth element PUSH
  2528. means to push this value onto the list in the variable.")
  2529. (defun org-set-regexps-and-options ()
  2530. "Precompute regular expressions for current buffer."
  2531. (when (org-mode-p)
  2532. (org-set-local 'org-todo-kwd-alist nil)
  2533. (org-set-local 'org-todo-key-alist nil)
  2534. (org-set-local 'org-todo-key-trigger nil)
  2535. (org-set-local 'org-todo-keywords-1 nil)
  2536. (org-set-local 'org-done-keywords nil)
  2537. (org-set-local 'org-todo-heads nil)
  2538. (org-set-local 'org-todo-sets nil)
  2539. (org-set-local 'org-todo-log-states nil)
  2540. (org-set-local 'org-file-properties nil)
  2541. (org-set-local 'org-file-tags nil)
  2542. (let ((re (org-make-options-regexp
  2543. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2544. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2545. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2546. (splitre "[ \t]+")
  2547. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2548. tail sep kws1 prio props ftags drawers
  2549. ext-setup-or-nil setup-contents (start 0))
  2550. (save-excursion
  2551. (save-restriction
  2552. (widen)
  2553. (goto-char (point-min))
  2554. (while (or (and ext-setup-or-nil
  2555. (string-match re ext-setup-or-nil start)
  2556. (setq start (match-end 0)))
  2557. (and (setq ext-setup-or-nil nil start 0)
  2558. (re-search-forward re nil t)))
  2559. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2560. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2561. (cond
  2562. ((equal key "CATEGORY")
  2563. (if (string-match "[ \t]+$" value)
  2564. (setq value (replace-match "" t t value)))
  2565. (setq cat value))
  2566. ((member key '("SEQ_TODO" "TODO"))
  2567. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2568. ((equal key "TYP_TODO")
  2569. (push (cons 'type (org-split-string value splitre)) kwds))
  2570. ((equal key "TAGS")
  2571. (setq tags (append tags (org-split-string value splitre))))
  2572. ((equal key "COLUMNS")
  2573. (org-set-local 'org-columns-default-format value))
  2574. ((equal key "LINK")
  2575. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2576. (push (cons (match-string 1 value)
  2577. (org-trim (match-string 2 value)))
  2578. links)))
  2579. ((equal key "PRIORITIES")
  2580. (setq prio (org-split-string value " +")))
  2581. ((equal key "PROPERTY")
  2582. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2583. (push (cons (match-string 1 value) (match-string 2 value))
  2584. props)))
  2585. ((equal key "FILETAGS")
  2586. (when (string-match "\\S-" value)
  2587. (setq ftags
  2588. (append
  2589. ftags
  2590. (apply 'append
  2591. (mapcar (lambda (x) (org-split-string x ":"))
  2592. (org-split-string value)))))))
  2593. ((equal key "DRAWERS")
  2594. (setq drawers (org-split-string value splitre)))
  2595. ((equal key "CONSTANTS")
  2596. (setq const (append const (org-split-string value splitre))))
  2597. ((equal key "STARTUP")
  2598. (let ((opts (org-split-string value splitre))
  2599. l var val)
  2600. (while (setq l (pop opts))
  2601. (when (setq l (assoc l org-startup-options))
  2602. (setq var (nth 1 l) val (nth 2 l))
  2603. (if (not (nth 3 l))
  2604. (set (make-local-variable var) val)
  2605. (if (not (listp (symbol-value var)))
  2606. (set (make-local-variable var) nil))
  2607. (set (make-local-variable var) (symbol-value var))
  2608. (add-to-list var val))))))
  2609. ((equal key "ARCHIVE")
  2610. (string-match " *$" value)
  2611. (setq arch (replace-match "" t t value))
  2612. (remove-text-properties 0 (length arch)
  2613. '(face t fontified t) arch))
  2614. ((equal key "SETUPFILE")
  2615. (setq setup-contents (org-file-contents
  2616. (expand-file-name
  2617. (org-remove-double-quotes value))
  2618. 'noerror))
  2619. (if (not ext-setup-or-nil)
  2620. (setq ext-setup-or-nil setup-contents start 0)
  2621. (setq ext-setup-or-nil
  2622. (concat (substring ext-setup-or-nil 0 start)
  2623. "\n" setup-contents "\n"
  2624. (substring ext-setup-or-nil start)))))
  2625. ))))
  2626. (when cat
  2627. (org-set-local 'org-category (intern cat))
  2628. (push (cons "CATEGORY" cat) props))
  2629. (when prio
  2630. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2631. (setq prio (mapcar 'string-to-char prio))
  2632. (org-set-local 'org-highest-priority (nth 0 prio))
  2633. (org-set-local 'org-lowest-priority (nth 1 prio))
  2634. (org-set-local 'org-default-priority (nth 2 prio)))
  2635. (and props (org-set-local 'org-file-properties (nreverse props)))
  2636. (and ftags (org-set-local 'org-file-tags ftags))
  2637. (and drawers (org-set-local 'org-drawers drawers))
  2638. (and arch (org-set-local 'org-archive-location arch))
  2639. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2640. ;; Process the TODO keywords
  2641. (unless kwds
  2642. ;; Use the global values as if they had been given locally.
  2643. (setq kwds (default-value 'org-todo-keywords))
  2644. (if (stringp (car kwds))
  2645. (setq kwds (list (cons org-todo-interpretation
  2646. (default-value 'org-todo-keywords)))))
  2647. (setq kwds (reverse kwds)))
  2648. (setq kwds (nreverse kwds))
  2649. (let (inter kws kw)
  2650. (while (setq kws (pop kwds))
  2651. (setq inter (pop kws) sep (member "|" kws)
  2652. kws0 (delete "|" (copy-sequence kws))
  2653. kwsa nil
  2654. kws1 (mapcar
  2655. (lambda (x)
  2656. ;; 1 2
  2657. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2658. (progn
  2659. (setq kw (match-string 1 x)
  2660. key (and (match-end 2) (match-string 2 x))
  2661. log (org-extract-log-state-settings x))
  2662. (push (cons kw (and key (string-to-char key))) kwsa)
  2663. (and log (push log org-todo-log-states))
  2664. kw)
  2665. (error "Invalid TODO keyword %s" x)))
  2666. kws0)
  2667. kwsa (if kwsa (append '((:startgroup))
  2668. (nreverse kwsa)
  2669. '((:endgroup))))
  2670. hw (car kws1)
  2671. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2672. tail (list inter hw (car dws) (org-last dws)))
  2673. (add-to-list 'org-todo-heads hw 'append)
  2674. (push kws1 org-todo-sets)
  2675. (setq org-done-keywords (append org-done-keywords dws nil))
  2676. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2677. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2678. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2679. (setq org-todo-sets (nreverse org-todo-sets)
  2680. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2681. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2682. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2683. ;; Process the constants
  2684. (when const
  2685. (let (e cst)
  2686. (while (setq e (pop const))
  2687. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2688. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2689. (setq org-table-formula-constants-local cst)))
  2690. ;; Process the tags.
  2691. (when tags
  2692. (let (e tgs)
  2693. (while (setq e (pop tags))
  2694. (cond
  2695. ((equal e "{") (push '(:startgroup) tgs))
  2696. ((equal e "}") (push '(:endgroup) tgs))
  2697. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2698. (push (cons (match-string 1 e)
  2699. (string-to-char (match-string 2 e)))
  2700. tgs))
  2701. (t (push (list e) tgs))))
  2702. (org-set-local 'org-tag-alist nil)
  2703. (while (setq e (pop tgs))
  2704. (or (and (stringp (car e))
  2705. (assoc (car e) org-tag-alist))
  2706. (push e org-tag-alist)))))
  2707. ;; Compute the regular expressions and other local variables
  2708. (if (not org-done-keywords)
  2709. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2710. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2711. (length org-scheduled-string)
  2712. (length org-clock-string)
  2713. (length org-closed-string)))
  2714. org-drawer-regexp
  2715. (concat "^[ \t]*:\\("
  2716. (mapconcat 'regexp-quote org-drawers "\\|")
  2717. "\\):[ \t]*$")
  2718. org-not-done-keywords
  2719. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2720. org-todo-regexp
  2721. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2722. "\\|") "\\)\\>")
  2723. org-not-done-regexp
  2724. (concat "\\<\\("
  2725. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2726. "\\)\\>")
  2727. org-todo-line-regexp
  2728. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2729. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2730. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2731. org-complex-heading-regexp
  2732. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2733. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2734. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2735. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2736. org-nl-done-regexp
  2737. (concat "\n\\*+[ \t]+"
  2738. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2739. "\\)" "\\>")
  2740. org-todo-line-tags-regexp
  2741. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2742. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2743. (org-re
  2744. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2745. org-looking-at-done-regexp
  2746. (concat "^" "\\(?:"
  2747. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2748. "\\>")
  2749. org-deadline-regexp (concat "\\<" org-deadline-string)
  2750. org-deadline-time-regexp
  2751. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2752. org-deadline-line-regexp
  2753. (concat "\\<\\(" org-deadline-string "\\).*")
  2754. org-scheduled-regexp
  2755. (concat "\\<" org-scheduled-string)
  2756. org-scheduled-time-regexp
  2757. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2758. org-closed-time-regexp
  2759. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2760. org-keyword-time-regexp
  2761. (concat "\\<\\(" org-scheduled-string
  2762. "\\|" org-deadline-string
  2763. "\\|" org-closed-string
  2764. "\\|" org-clock-string "\\)"
  2765. " *[[<]\\([^]>]+\\)[]>]")
  2766. org-keyword-time-not-clock-regexp
  2767. (concat "\\<\\(" org-scheduled-string
  2768. "\\|" org-deadline-string
  2769. "\\|" org-closed-string
  2770. "\\)"
  2771. " *[[<]\\([^]>]+\\)[]>]")
  2772. org-maybe-keyword-time-regexp
  2773. (concat "\\(\\<\\(" org-scheduled-string
  2774. "\\|" org-deadline-string
  2775. "\\|" org-closed-string
  2776. "\\|" org-clock-string "\\)\\)?"
  2777. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2778. org-planning-or-clock-line-re
  2779. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2780. "\\|" org-deadline-string
  2781. "\\|" org-closed-string "\\|" org-clock-string
  2782. "\\)\\>\\)")
  2783. )
  2784. (org-compute-latex-and-specials-regexp)
  2785. (org-set-font-lock-defaults))))
  2786. (defun org-file-contents (file &optional noerror)
  2787. "Return the contents of FILE, as a string."
  2788. (if (or (not file)
  2789. (not (file-readable-p file)))
  2790. (if noerror
  2791. (progn
  2792. (message "Cannot read file %s" file)
  2793. (ding) (sit-for 2)
  2794. "")
  2795. (error "Cannot read file %s" file))
  2796. (with-temp-buffer
  2797. (insert-file-contents file)
  2798. (buffer-string))))
  2799. (defun org-extract-log-state-settings (x)
  2800. "Extract the log state setting from a TODO keyword string.
  2801. This will extract info from a string like \"WAIT(w@/!)\"."
  2802. (let (kw key log1 log2)
  2803. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2804. (setq kw (match-string 1 x)
  2805. key (and (match-end 2) (match-string 2 x))
  2806. log1 (and (match-end 3) (match-string 3 x))
  2807. log2 (and (match-end 4) (match-string 4 x)))
  2808. (and (or log1 log2)
  2809. (list kw
  2810. (and log1 (if (equal log1 "!") 'time 'note))
  2811. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2812. (defun org-remove-keyword-keys (list)
  2813. "Remove a pair of parenthesis at the end of each string in LIST."
  2814. (mapcar (lambda (x)
  2815. (if (string-match "(.*)$" x)
  2816. (substring x 0 (match-beginning 0))
  2817. x))
  2818. list))
  2819. ;; FIXME: this could be done much better, using second characters etc.
  2820. (defun org-assign-fast-keys (alist)
  2821. "Assign fast keys to a keyword-key alist.
  2822. Respect keys that are already there."
  2823. (let (new e k c c1 c2 (char ?a))
  2824. (while (setq e (pop alist))
  2825. (cond
  2826. ((equal e '(:startgroup)) (push e new))
  2827. ((equal e '(:endgroup)) (push e new))
  2828. (t
  2829. (setq k (car e) c2 nil)
  2830. (if (cdr e)
  2831. (setq c (cdr e))
  2832. ;; automatically assign a character.
  2833. (setq c1 (string-to-char
  2834. (downcase (substring
  2835. k (if (= (string-to-char k) ?@) 1 0)))))
  2836. (if (or (rassoc c1 new) (rassoc c1 alist))
  2837. (while (or (rassoc char new) (rassoc char alist))
  2838. (setq char (1+ char)))
  2839. (setq c2 c1))
  2840. (setq c (or c2 char)))
  2841. (push (cons k c) new))))
  2842. (nreverse new)))
  2843. ;;; Some variables used in various places
  2844. (defvar org-window-configuration nil
  2845. "Used in various places to store a window configuration.")
  2846. (defvar org-finish-function nil
  2847. "Function to be called when `C-c C-c' is used.
  2848. This is for getting out of special buffers like remember.")
  2849. ;; FIXME: Occasionally check by commenting these, to make sure
  2850. ;; no other functions uses these, forgetting to let-bind them.
  2851. (defvar entry)
  2852. (defvar state)
  2853. (defvar last-state)
  2854. (defvar date)
  2855. (defvar description)
  2856. ;; Defined somewhere in this file, but used before definition.
  2857. (defvar org-html-entities)
  2858. (defvar org-struct-menu)
  2859. (defvar org-org-menu)
  2860. (defvar org-tbl-menu)
  2861. (defvar org-agenda-keymap)
  2862. ;;;; Define the Org-mode
  2863. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2864. (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."))
  2865. ;; We use a before-change function to check if a table might need
  2866. ;; an update.
  2867. (defvar org-table-may-need-update t
  2868. "Indicates that a table might need an update.
  2869. This variable is set by `org-before-change-function'.
  2870. `org-table-align' sets it back to nil.")
  2871. (defun org-before-change-function (beg end)
  2872. "Every change indicates that a table might need an update."
  2873. (setq org-table-may-need-update t))
  2874. (defvar org-mode-map)
  2875. (defvar org-mode-hook nil)
  2876. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2877. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2878. (defvar org-table-buffer-is-an nil)
  2879. (defconst org-outline-regexp "\\*+ ")
  2880. ;;;###autoload
  2881. (define-derived-mode org-mode outline-mode "Org"
  2882. "Outline-based notes management and organizer, alias
  2883. \"Carsten's outline-mode for keeping track of everything.\"
  2884. Org-mode develops organizational tasks around a NOTES file which
  2885. contains information about projects as plain text. Org-mode is
  2886. implemented on top of outline-mode, which is ideal to keep the content
  2887. of large files well structured. It supports ToDo items, deadlines and
  2888. time stamps, which magically appear in the diary listing of the Emacs
  2889. calendar. Tables are easily created with a built-in table editor.
  2890. Plain text URL-like links connect to websites, emails (VM), Usenet
  2891. messages (Gnus), BBDB entries, and any files related to the project.
  2892. For printing and sharing of notes, an Org-mode file (or a part of it)
  2893. can be exported as a structured ASCII or HTML file.
  2894. The following commands are available:
  2895. \\{org-mode-map}"
  2896. ;; Get rid of Outline menus, they are not needed
  2897. ;; Need to do this here because define-derived-mode sets up
  2898. ;; the keymap so late. Still, it is a waste to call this each time
  2899. ;; we switch another buffer into org-mode.
  2900. (if (featurep 'xemacs)
  2901. (when (boundp 'outline-mode-menu-heading)
  2902. ;; Assume this is Greg's port, it used easymenu
  2903. (easy-menu-remove outline-mode-menu-heading)
  2904. (easy-menu-remove outline-mode-menu-show)
  2905. (easy-menu-remove outline-mode-menu-hide))
  2906. (define-key org-mode-map [menu-bar headings] 'undefined)
  2907. (define-key org-mode-map [menu-bar hide] 'undefined)
  2908. (define-key org-mode-map [menu-bar show] 'undefined))
  2909. (org-load-modules-maybe)
  2910. (easy-menu-add org-org-menu)
  2911. (easy-menu-add org-tbl-menu)
  2912. (org-install-agenda-files-menu)
  2913. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2914. (org-add-to-invisibility-spec '(org-cwidth))
  2915. (when (featurep 'xemacs)
  2916. (org-set-local 'line-move-ignore-invisible t))
  2917. (org-set-local 'outline-regexp org-outline-regexp)
  2918. (org-set-local 'outline-level 'org-outline-level)
  2919. (when (and org-ellipsis
  2920. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2921. (fboundp 'make-glyph-code))
  2922. (unless org-display-table
  2923. (setq org-display-table (make-display-table)))
  2924. (set-display-table-slot
  2925. org-display-table 4
  2926. (vconcat (mapcar
  2927. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2928. org-ellipsis)))
  2929. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2930. (setq buffer-display-table org-display-table))
  2931. (org-set-regexps-and-options)
  2932. ;; Calc embedded
  2933. (org-set-local 'calc-embedded-open-mode "# ")
  2934. (modify-syntax-entry ?# "<")
  2935. (modify-syntax-entry ?@ "w")
  2936. (if org-startup-truncated (setq truncate-lines t))
  2937. (org-set-local 'font-lock-unfontify-region-function
  2938. 'org-unfontify-region)
  2939. ;; Activate before-change-function
  2940. (org-set-local 'org-table-may-need-update t)
  2941. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2942. 'local)
  2943. ;; Check for running clock before killing a buffer
  2944. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2945. ;; Paragraphs and auto-filling
  2946. (org-set-autofill-regexps)
  2947. (setq indent-line-function 'org-indent-line-function)
  2948. (org-update-radio-target-regexp)
  2949. ;; Comment characters
  2950. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2951. (org-set-local 'comment-padding " ")
  2952. ;; Align options lines
  2953. (org-set-local
  2954. 'align-mode-rules-list
  2955. '((org-in-buffer-settings
  2956. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2957. (modes . '(org-mode)))))
  2958. ;; Imenu
  2959. (org-set-local 'imenu-create-index-function
  2960. 'org-imenu-get-tree)
  2961. ;; Make isearch reveal context
  2962. (if (or (featurep 'xemacs)
  2963. (not (boundp 'outline-isearch-open-invisible-function)))
  2964. ;; Emacs 21 and XEmacs make use of the hook
  2965. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2966. ;; Emacs 22 deals with this through a special variable
  2967. (org-set-local 'outline-isearch-open-invisible-function
  2968. (lambda (&rest ignore) (org-show-context 'isearch))))
  2969. ;; If empty file that did not turn on org-mode automatically, make it to.
  2970. (if (and org-insert-mode-line-in-empty-file
  2971. (interactive-p)
  2972. (= (point-min) (point-max)))
  2973. (insert "# -*- mode: org -*-\n\n"))
  2974. (unless org-inhibit-startup
  2975. (when org-startup-align-all-tables
  2976. (let ((bmp (buffer-modified-p)))
  2977. (org-table-map-tables 'org-table-align)
  2978. (set-buffer-modified-p bmp)))
  2979. (org-set-startup-visibility)))
  2980. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2981. (defun org-current-time ()
  2982. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2983. (if (> (car org-time-stamp-rounding-minutes) 1)
  2984. (let ((r (car org-time-stamp-rounding-minutes))
  2985. (time (decode-time)))
  2986. (apply 'encode-time
  2987. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2988. (nthcdr 2 time))))
  2989. (current-time)))
  2990. ;;;; Font-Lock stuff, including the activators
  2991. (defvar org-mouse-map (make-sparse-keymap))
  2992. (org-defkey org-mouse-map
  2993. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2994. (org-defkey org-mouse-map
  2995. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2996. (when org-mouse-1-follows-link
  2997. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2998. (when org-tab-follows-link
  2999. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3000. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3001. (when org-return-follows-link
  3002. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3003. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3004. (require 'font-lock)
  3005. (defconst org-non-link-chars "]\t\n\r<>")
  3006. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3007. "shell" "elisp"))
  3008. (defvar org-link-types-re nil
  3009. "Matches a link that has a url-like prefix like \"http:\"")
  3010. (defvar org-link-re-with-space nil
  3011. "Matches a link with spaces, optional angular brackets around it.")
  3012. (defvar org-link-re-with-space2 nil
  3013. "Matches a link with spaces, optional angular brackets around it.")
  3014. (defvar org-angle-link-re nil
  3015. "Matches link with angular brackets, spaces are allowed.")
  3016. (defvar org-plain-link-re nil
  3017. "Matches plain link, without spaces.")
  3018. (defvar org-bracket-link-regexp nil
  3019. "Matches a link in double brackets.")
  3020. (defvar org-bracket-link-analytic-regexp nil
  3021. "Regular expression used to analyze links.
  3022. Here is what the match groups contain after a match:
  3023. 1: http:
  3024. 2: http
  3025. 3: path
  3026. 4: [desc]
  3027. 5: desc")
  3028. (defvar org-any-link-re nil
  3029. "Regular expression matching any link.")
  3030. (defun org-make-link-regexps ()
  3031. "Update the link regular expressions.
  3032. This should be called after the variable `org-link-types' has changed."
  3033. (setq org-link-types-re
  3034. (concat
  3035. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3036. org-link-re-with-space
  3037. (concat
  3038. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3039. "\\([^" org-non-link-chars " ]"
  3040. "[^" org-non-link-chars "]*"
  3041. "[^" org-non-link-chars " ]\\)>?")
  3042. org-link-re-with-space2
  3043. (concat
  3044. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3045. "\\([^" org-non-link-chars " ]"
  3046. "[^\t\n\r]*"
  3047. "[^" org-non-link-chars " ]\\)>?")
  3048. org-angle-link-re
  3049. (concat
  3050. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3051. "\\([^" org-non-link-chars " ]"
  3052. "[^" org-non-link-chars "]*"
  3053. "\\)>")
  3054. org-plain-link-re
  3055. (concat
  3056. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3057. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3058. org-bracket-link-regexp
  3059. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3060. org-bracket-link-analytic-regexp
  3061. (concat
  3062. "\\[\\["
  3063. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3064. "\\([^]]+\\)"
  3065. "\\]"
  3066. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3067. "\\]")
  3068. org-any-link-re
  3069. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3070. org-angle-link-re "\\)\\|\\("
  3071. org-plain-link-re "\\)")))
  3072. (org-make-link-regexps)
  3073. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3074. "Regular expression for fast time stamp matching.")
  3075. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3076. "Regular expression for fast time stamp matching.")
  3077. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3078. "Regular expression matching time strings for analysis.
  3079. This one does not require the space after the date, so it can be used
  3080. on a string that terminates immediately after the date.")
  3081. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3082. "Regular expression matching time strings for analysis.")
  3083. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3084. "Regular expression matching time stamps, with groups.")
  3085. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3086. "Regular expression matching time stamps (also [..]), with groups.")
  3087. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3088. "Regular expression matching a time stamp range.")
  3089. (defconst org-tr-regexp-both
  3090. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3091. "Regular expression matching a time stamp range.")
  3092. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3093. org-ts-regexp "\\)?")
  3094. "Regular expression matching a time stamp or time stamp range.")
  3095. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3096. org-ts-regexp-both "\\)?")
  3097. "Regular expression matching a time stamp or time stamp range.
  3098. The time stamps may be either active or inactive.")
  3099. (defvar org-emph-face nil)
  3100. (defun org-do-emphasis-faces (limit)
  3101. "Run through the buffer and add overlays to links."
  3102. (let (rtn)
  3103. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3104. (if (not (= (char-after (match-beginning 3))
  3105. (char-after (match-beginning 4))))
  3106. (progn
  3107. (setq rtn t)
  3108. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3109. 'face
  3110. (nth 1 (assoc (match-string 3)
  3111. org-emphasis-alist)))
  3112. (add-text-properties (match-beginning 2) (match-end 2)
  3113. '(font-lock-multiline t))
  3114. (when org-hide-emphasis-markers
  3115. (add-text-properties (match-end 4) (match-beginning 5)
  3116. '(invisible org-link))
  3117. (add-text-properties (match-beginning 3) (match-end 3)
  3118. '(invisible org-link)))))
  3119. (backward-char 1))
  3120. rtn))
  3121. (defun org-emphasize (&optional char)
  3122. "Insert or change an emphasis, i.e. a font like bold or italic.
  3123. If there is an active region, change that region to a new emphasis.
  3124. If there is no region, just insert the marker characters and position
  3125. the cursor between them.
  3126. CHAR should be either the marker character, or the first character of the
  3127. HTML tag associated with that emphasis. If CHAR is a space, the means
  3128. to remove the emphasis of the selected region.
  3129. If char is not given (for example in an interactive call) it
  3130. will be prompted for."
  3131. (interactive)
  3132. (let ((eal org-emphasis-alist) e det
  3133. (erc org-emphasis-regexp-components)
  3134. (prompt "")
  3135. (string "") beg end move tag c s)
  3136. (if (org-region-active-p)
  3137. (setq beg (region-beginning) end (region-end)
  3138. string (buffer-substring beg end))
  3139. (setq move t))
  3140. (while (setq e (pop eal))
  3141. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3142. c (aref tag 0))
  3143. (push (cons c (string-to-char (car e))) det)
  3144. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3145. (substring tag 1)))))
  3146. (setq det (nreverse det))
  3147. (unless char
  3148. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3149. (setq char (read-char-exclusive)))
  3150. (setq char (or (cdr (assoc char det)) char))
  3151. (if (equal char ?\ )
  3152. (setq s "" move nil)
  3153. (unless (assoc (char-to-string char) org-emphasis-alist)
  3154. (error "No such emphasis marker: \"%c\"" char))
  3155. (setq s (char-to-string char)))
  3156. (while (and (> (length string) 1)
  3157. (equal (substring string 0 1) (substring string -1))
  3158. (assoc (substring string 0 1) org-emphasis-alist))
  3159. (setq string (substring string 1 -1)))
  3160. (setq string (concat s string s))
  3161. (if beg (delete-region beg end))
  3162. (unless (or (bolp)
  3163. (string-match (concat "[" (nth 0 erc) "\n]")
  3164. (char-to-string (char-before (point)))))
  3165. (insert " "))
  3166. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3167. (char-to-string (char-after (point))))
  3168. (insert " ") (backward-char 1))
  3169. (insert string)
  3170. (and move (backward-char 1))))
  3171. (defconst org-nonsticky-props
  3172. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3173. (defun org-activate-plain-links (limit)
  3174. "Run through the buffer and add overlays to links."
  3175. (catch 'exit
  3176. (let (f)
  3177. (while (re-search-forward org-plain-link-re limit t)
  3178. (setq f (get-text-property (match-beginning 0) 'face))
  3179. (if (or (eq f 'org-tag)
  3180. (and (listp f) (memq 'org-tag f)))
  3181. nil
  3182. (add-text-properties (match-beginning 0) (match-end 0)
  3183. (list 'mouse-face 'highlight
  3184. 'rear-nonsticky org-nonsticky-props
  3185. 'keymap org-mouse-map
  3186. ))
  3187. (throw 'exit t))))))
  3188. (defun org-activate-code (limit)
  3189. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3190. (progn
  3191. (remove-text-properties (match-beginning 0) (match-end 0)
  3192. '(display t invisible t intangible t))
  3193. t)))
  3194. (defun org-activate-angle-links (limit)
  3195. "Run through the buffer and add overlays to links."
  3196. (if (re-search-forward org-angle-link-re limit t)
  3197. (progn
  3198. (add-text-properties (match-beginning 0) (match-end 0)
  3199. (list 'mouse-face 'highlight
  3200. 'rear-nonsticky org-nonsticky-props
  3201. 'keymap org-mouse-map
  3202. ))
  3203. t)))
  3204. (defun org-activate-bracket-links (limit)
  3205. "Run through the buffer and add overlays to bracketed links."
  3206. (if (re-search-forward org-bracket-link-regexp limit t)
  3207. (let* ((help (concat "LINK: "
  3208. (org-match-string-no-properties 1)))
  3209. ;; FIXME: above we should remove the escapes.
  3210. ;; but that requires another match, protecting match data,
  3211. ;; a lot of overhead for font-lock.
  3212. (ip (org-maybe-intangible
  3213. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3214. 'keymap org-mouse-map 'mouse-face 'highlight
  3215. 'font-lock-multiline t 'help-echo help)))
  3216. (vp (list 'rear-nonsticky org-nonsticky-props
  3217. 'keymap org-mouse-map 'mouse-face 'highlight
  3218. ' font-lock-multiline t 'help-echo help)))
  3219. ;; We need to remove the invisible property here. Table narrowing
  3220. ;; may have made some of this invisible.
  3221. (remove-text-properties (match-beginning 0) (match-end 0)
  3222. '(invisible nil))
  3223. (if (match-end 3)
  3224. (progn
  3225. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3226. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3227. (add-text-properties (match-end 3) (match-end 0) ip))
  3228. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3229. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3230. (add-text-properties (match-end 1) (match-end 0) ip))
  3231. t)))
  3232. (defun org-activate-dates (limit)
  3233. "Run through the buffer and add overlays to dates."
  3234. (if (re-search-forward org-tsr-regexp-both limit t)
  3235. (progn
  3236. (add-text-properties (match-beginning 0) (match-end 0)
  3237. (list 'mouse-face 'highlight
  3238. 'rear-nonsticky org-nonsticky-props
  3239. 'keymap org-mouse-map))
  3240. (when org-display-custom-times
  3241. (if (match-end 3)
  3242. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3243. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3244. t)))
  3245. (defvar org-target-link-regexp nil
  3246. "Regular expression matching radio targets in plain text.")
  3247. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3248. "Regular expression matching a link target.")
  3249. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3250. "Regular expression matching a radio target.")
  3251. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3252. "Regular expression matching any target.")
  3253. (defun org-activate-target-links (limit)
  3254. "Run through the buffer and add overlays to target matches."
  3255. (when org-target-link-regexp
  3256. (let ((case-fold-search t))
  3257. (if (re-search-forward org-target-link-regexp limit t)
  3258. (progn
  3259. (add-text-properties (match-beginning 0) (match-end 0)
  3260. (list 'mouse-face 'highlight
  3261. 'rear-nonsticky org-nonsticky-props
  3262. 'keymap org-mouse-map
  3263. 'help-echo "Radio target link"
  3264. 'org-linked-text t))
  3265. t)))))
  3266. (defun org-update-radio-target-regexp ()
  3267. "Find all radio targets in this file and update the regular expression."
  3268. (interactive)
  3269. (when (memq 'radio org-activate-links)
  3270. (setq org-target-link-regexp
  3271. (org-make-target-link-regexp (org-all-targets 'radio)))
  3272. (org-restart-font-lock)))
  3273. (defun org-hide-wide-columns (limit)
  3274. (let (s e)
  3275. (setq s (text-property-any (point) (or limit (point-max))
  3276. 'org-cwidth t))
  3277. (when s
  3278. (setq e (next-single-property-change s 'org-cwidth))
  3279. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3280. (goto-char e)
  3281. t)))
  3282. (defvar org-latex-and-specials-regexp nil
  3283. "Regular expression for highlighting export special stuff.")
  3284. (defvar org-match-substring-regexp)
  3285. (defvar org-match-substring-with-braces-regexp)
  3286. (defvar org-export-html-special-string-regexps)
  3287. (defun org-compute-latex-and-specials-regexp ()
  3288. "Compute regular expression for stuff treated specially by exporters."
  3289. (if (not org-highlight-latex-fragments-and-specials)
  3290. (org-set-local 'org-latex-and-specials-regexp nil)
  3291. (require 'org-exp)
  3292. (let*
  3293. ((matchers (plist-get org-format-latex-options :matchers))
  3294. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3295. org-latex-regexps)))
  3296. (options (org-combine-plists (org-default-export-plist)
  3297. (org-infile-export-plist)))
  3298. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3299. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3300. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3301. (org-export-html-expand (plist-get options :expand-quoted-html))
  3302. (org-export-with-special-strings (plist-get options :special-strings))
  3303. (re-sub
  3304. (cond
  3305. ((equal org-export-with-sub-superscripts '{})
  3306. (list org-match-substring-with-braces-regexp))
  3307. (org-export-with-sub-superscripts
  3308. (list org-match-substring-regexp))
  3309. (t nil)))
  3310. (re-latex
  3311. (if org-export-with-LaTeX-fragments
  3312. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3313. (re-macros
  3314. (if org-export-with-TeX-macros
  3315. (list (concat "\\\\"
  3316. (regexp-opt
  3317. (append (mapcar 'car org-html-entities)
  3318. (if (boundp 'org-latex-entities)
  3319. org-latex-entities nil))
  3320. 'words))) ; FIXME
  3321. ))
  3322. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3323. (re-special (if org-export-with-special-strings
  3324. (mapcar (lambda (x) (car x))
  3325. org-export-html-special-string-regexps)))
  3326. (re-rest
  3327. (delq nil
  3328. (list
  3329. (if org-export-html-expand "@<[^>\n]+>")
  3330. ))))
  3331. (org-set-local
  3332. 'org-latex-and-specials-regexp
  3333. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3334. re-rest) "\\|")))))
  3335. (defun org-do-latex-and-special-faces (limit)
  3336. "Run through the buffer and add overlays to links."
  3337. (when org-latex-and-specials-regexp
  3338. (let (rtn d)
  3339. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3340. limit t))
  3341. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3342. 'face))
  3343. '(org-code org-verbatim underline)))
  3344. (progn
  3345. (setq rtn t
  3346. d (cond ((member (char-after (1+ (match-beginning 0)))
  3347. '(?_ ?^)) 1)
  3348. (t 0)))
  3349. (font-lock-prepend-text-property
  3350. (+ d (match-beginning 0)) (match-end 0)
  3351. 'face 'org-latex-and-export-specials)
  3352. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3353. '(font-lock-multiline t)))))
  3354. rtn)))
  3355. (defun org-restart-font-lock ()
  3356. "Restart font-lock-mode, to force refontification."
  3357. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3358. (font-lock-mode -1)
  3359. (font-lock-mode 1)))
  3360. (defun org-all-targets (&optional radio)
  3361. "Return a list of all targets in this file.
  3362. With optional argument RADIO, only find radio targets."
  3363. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3364. rtn)
  3365. (save-excursion
  3366. (goto-char (point-min))
  3367. (while (re-search-forward re nil t)
  3368. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3369. rtn)))
  3370. (defun org-make-target-link-regexp (targets)
  3371. "Make regular expression matching all strings in TARGETS.
  3372. The regular expression finds the targets also if there is a line break
  3373. between words."
  3374. (and targets
  3375. (concat
  3376. "\\<\\("
  3377. (mapconcat
  3378. (lambda (x)
  3379. (while (string-match " +" x)
  3380. (setq x (replace-match "\\s-+" t t x)))
  3381. x)
  3382. targets
  3383. "\\|")
  3384. "\\)\\>")))
  3385. (defun org-activate-tags (limit)
  3386. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3387. (progn
  3388. (add-text-properties (match-beginning 1) (match-end 1)
  3389. (list 'mouse-face 'highlight
  3390. 'rear-nonsticky org-nonsticky-props
  3391. 'keymap org-mouse-map))
  3392. t)))
  3393. (defun org-outline-level ()
  3394. (save-excursion
  3395. (looking-at outline-regexp)
  3396. (if (match-beginning 1)
  3397. (+ (org-get-string-indentation (match-string 1)) 1000)
  3398. (1- (- (match-end 0) (match-beginning 0))))))
  3399. (defvar org-font-lock-keywords nil)
  3400. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3401. "Regular expression matching a property line.")
  3402. (defvar org-font-lock-hook nil
  3403. "Functions to be called for special font lock stuff.")
  3404. (defun org-font-lock-hook (limit)
  3405. (run-hook-with-args 'org-font-lock-hook limit))
  3406. (defun org-set-font-lock-defaults ()
  3407. (let* ((em org-fontify-emphasized-text)
  3408. (lk org-activate-links)
  3409. (org-font-lock-extra-keywords
  3410. (list
  3411. ;; Call the hook
  3412. '(org-font-lock-hook)
  3413. ;; Headlines
  3414. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3415. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3416. ;; Table lines
  3417. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3418. (1 'org-table t))
  3419. ;; Table internals
  3420. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3421. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3422. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3423. ;; Drawers
  3424. (list org-drawer-regexp '(0 'org-special-keyword t))
  3425. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3426. ;; Properties
  3427. (list org-property-re
  3428. '(1 'org-special-keyword t)
  3429. '(3 'org-property-value t))
  3430. (if org-format-transports-properties-p
  3431. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3432. ;; Links
  3433. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3434. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3435. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3436. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3437. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3438. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3439. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3440. '(org-hide-wide-columns (0 nil append))
  3441. ;; TODO lines
  3442. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3443. '(1 (org-get-todo-face 1) t))
  3444. ;; DONE
  3445. (if org-fontify-done-headline
  3446. (list (concat "^[*]+ +\\<\\("
  3447. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3448. "\\)\\(.*\\)")
  3449. '(2 'org-headline-done t))
  3450. nil)
  3451. ;; Priorities
  3452. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3453. ;; Special keywords
  3454. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3455. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3456. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3457. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3458. ;; Emphasis
  3459. (if em
  3460. (if (featurep 'xemacs)
  3461. '(org-do-emphasis-faces (0 nil append))
  3462. '(org-do-emphasis-faces)))
  3463. ;; Checkboxes
  3464. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3465. 2 'bold prepend)
  3466. (if org-provide-checkbox-statistics
  3467. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3468. (0 (org-get-checkbox-statistics-face) t)))
  3469. ;; Description list items
  3470. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3471. 2 'bold prepend)
  3472. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3473. '(1 'org-archived prepend))
  3474. ;; Specials
  3475. '(org-do-latex-and-special-faces)
  3476. ;; Code
  3477. '(org-activate-code (1 'org-code t))
  3478. ;; COMMENT
  3479. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3480. "\\|" org-quote-string "\\)\\>")
  3481. '(1 'org-special-keyword t))
  3482. '("^#.*" (0 'font-lock-comment-face t))
  3483. )))
  3484. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3485. ;; Now set the full font-lock-keywords
  3486. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3487. (org-set-local 'font-lock-defaults
  3488. '(org-font-lock-keywords t nil nil backward-paragraph))
  3489. (kill-local-variable 'font-lock-keywords) nil))
  3490. (defvar org-m nil)
  3491. (defvar org-l nil)
  3492. (defvar org-f nil)
  3493. (defun org-get-level-face (n)
  3494. "Get the right face for match N in font-lock matching of healdines."
  3495. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3496. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3497. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3498. (cond
  3499. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3500. ((eq n 2) org-f)
  3501. (t (if org-level-color-stars-only nil org-f))))
  3502. (defun org-get-todo-face (kwd)
  3503. "Get the right face for a TODO keyword KWD.
  3504. If KWD is a number, get the corresponding match group."
  3505. (if (numberp kwd) (setq kwd (match-string kwd)))
  3506. (or (cdr (assoc kwd org-todo-keyword-faces))
  3507. (and (member kwd org-done-keywords) 'org-done)
  3508. 'org-todo))
  3509. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3510. "Remove fontification and activation overlays from links."
  3511. (font-lock-default-unfontify-region beg end)
  3512. (let* ((buffer-undo-list t)
  3513. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3514. (inhibit-modification-hooks t)
  3515. deactivate-mark buffer-file-name buffer-file-truename)
  3516. (remove-text-properties beg end
  3517. '(mouse-face t keymap t org-linked-text t
  3518. invisible t intangible t))))
  3519. ;;;; Visibility cycling, including org-goto and indirect buffer
  3520. ;;; Cycling
  3521. (defvar org-cycle-global-status nil)
  3522. (make-variable-buffer-local 'org-cycle-global-status)
  3523. (defvar org-cycle-subtree-status nil)
  3524. (make-variable-buffer-local 'org-cycle-subtree-status)
  3525. ;;;###autoload
  3526. (defun org-cycle (&optional arg)
  3527. "Visibility cycling for Org-mode.
  3528. - When this function is called with a prefix argument, rotate the entire
  3529. buffer through 3 states (global cycling)
  3530. 1. OVERVIEW: Show only top-level headlines.
  3531. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3532. 3. SHOW ALL: Show everything.
  3533. When called with two C-u C-u prefixes, switch to the startup visibility,
  3534. determined by the variable `org-startup-folded', and by any VISIBILITY
  3535. properties in the buffer.
  3536. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3537. including drawers.
  3538. - When point is at the beginning of a headline, rotate the subtree started
  3539. by this line through 3 different states (local cycling)
  3540. 1. FOLDED: Only the main headline is shown.
  3541. 2. CHILDREN: The main headline and the direct children are shown.
  3542. From this state, you can move to one of the children
  3543. and zoom in further.
  3544. 3. SUBTREE: Show the entire subtree, including body text.
  3545. - When there is a numeric prefix, go up to a heading with level ARG, do
  3546. a `show-subtree' and return to the previous cursor position. If ARG
  3547. is negative, go up that many levels.
  3548. - When point is not at the beginning of a headline, execute the global
  3549. binding for TAB, which is re-indenting the line. See the option
  3550. `org-cycle-emulate-tab' for details.
  3551. - Special case: if point is at the beginning of the buffer and there is
  3552. no headline in line 1, this function will act as if called with prefix arg.
  3553. But only if also the variable `org-cycle-global-at-bob' is t."
  3554. (interactive "P")
  3555. (org-load-modules-maybe)
  3556. (let* ((outline-regexp
  3557. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3558. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3559. outline-regexp))
  3560. (bob-special (and org-cycle-global-at-bob (bobp)
  3561. (not (looking-at outline-regexp))))
  3562. (org-cycle-hook
  3563. (if bob-special
  3564. (delq 'org-optimize-window-after-visibility-change
  3565. (copy-sequence org-cycle-hook))
  3566. org-cycle-hook))
  3567. (pos (point)))
  3568. (if (or bob-special (equal arg '(4)))
  3569. ;; special case: use global cycling
  3570. (setq arg t))
  3571. (cond
  3572. ((equal arg '(16))
  3573. (org-set-startup-visibility)
  3574. (message "Startup visibility, plus VISIBILITY properties"))
  3575. ((equal arg '(64))
  3576. (show-all)
  3577. (message "Entire buffer visible, including drawers"))
  3578. ((org-at-table-p 'any)
  3579. ;; Enter the table or move to the next field in the table
  3580. (or (org-table-recognize-table.el)
  3581. (progn
  3582. (if arg (org-table-edit-field t)
  3583. (org-table-justify-field-maybe)
  3584. (call-interactively 'org-table-next-field)))))
  3585. ((eq arg t) ;; Global cycling
  3586. (cond
  3587. ((and (eq last-command this-command)
  3588. (eq org-cycle-global-status 'overview))
  3589. ;; We just created the overview - now do table of contents
  3590. ;; This can be slow in very large buffers, so indicate action
  3591. (message "CONTENTS...")
  3592. (org-content)
  3593. (message "CONTENTS...done")
  3594. (setq org-cycle-global-status 'contents)
  3595. (run-hook-with-args 'org-cycle-hook 'contents))
  3596. ((and (eq last-command this-command)
  3597. (eq org-cycle-global-status 'contents))
  3598. ;; We just showed the table of contents - now show everything
  3599. (show-all)
  3600. (message "SHOW ALL")
  3601. (setq org-cycle-global-status 'all)
  3602. (run-hook-with-args 'org-cycle-hook 'all))
  3603. (t
  3604. ;; Default action: go to overview
  3605. (org-overview)
  3606. (message "OVERVIEW")
  3607. (setq org-cycle-global-status 'overview)
  3608. (run-hook-with-args 'org-cycle-hook 'overview))))
  3609. ((and org-drawers org-drawer-regexp
  3610. (save-excursion
  3611. (beginning-of-line 1)
  3612. (looking-at org-drawer-regexp)))
  3613. ;; Toggle block visibility
  3614. (org-flag-drawer
  3615. (not (get-char-property (match-end 0) 'invisible))))
  3616. ((integerp arg)
  3617. ;; Show-subtree, ARG levels up from here.
  3618. (save-excursion
  3619. (org-back-to-heading)
  3620. (outline-up-heading (if (< arg 0) (- arg)
  3621. (- (funcall outline-level) arg)))
  3622. (org-show-subtree)))
  3623. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3624. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3625. ;; At a heading: rotate between three different views
  3626. (org-back-to-heading)
  3627. (let ((goal-column 0) eoh eol eos)
  3628. ;; First, some boundaries
  3629. (save-excursion
  3630. (org-back-to-heading)
  3631. (save-excursion
  3632. (beginning-of-line 2)
  3633. (while (and (not (eobp)) ;; this is like `next-line'
  3634. (get-char-property (1- (point)) 'invisible))
  3635. (beginning-of-line 2)) (setq eol (point)))
  3636. (outline-end-of-heading) (setq eoh (point))
  3637. (org-end-of-subtree t)
  3638. (unless (eobp)
  3639. (skip-chars-forward " \t\n")
  3640. (beginning-of-line 1) ; in case this is an item
  3641. )
  3642. (setq eos (1- (point))))
  3643. ;; Find out what to do next and set `this-command'
  3644. (cond
  3645. ((= eos eoh)
  3646. ;; Nothing is hidden behind this heading
  3647. (message "EMPTY ENTRY")
  3648. (setq org-cycle-subtree-status nil)
  3649. (save-excursion
  3650. (goto-char eos)
  3651. (outline-next-heading)
  3652. (if (org-invisible-p) (org-flag-heading nil))))
  3653. ((or (>= eol eos)
  3654. (not (string-match "\\S-" (buffer-substring eol eos))))
  3655. ;; Entire subtree is hidden in one line: open it
  3656. (org-show-entry)
  3657. (show-children)
  3658. (message "CHILDREN")
  3659. (save-excursion
  3660. (goto-char eos)
  3661. (outline-next-heading)
  3662. (if (org-invisible-p) (org-flag-heading nil)))
  3663. (setq org-cycle-subtree-status 'children)
  3664. (run-hook-with-args 'org-cycle-hook 'children))
  3665. ((and (eq last-command this-command)
  3666. (eq org-cycle-subtree-status 'children))
  3667. ;; We just showed the children, now show everything.
  3668. (org-show-subtree)
  3669. (message "SUBTREE")
  3670. (setq org-cycle-subtree-status 'subtree)
  3671. (run-hook-with-args 'org-cycle-hook 'subtree))
  3672. (t
  3673. ;; Default action: hide the subtree.
  3674. (hide-subtree)
  3675. (message "FOLDED")
  3676. (setq org-cycle-subtree-status 'folded)
  3677. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3678. ;; TAB emulation and template completion
  3679. (buffer-read-only (org-back-to-heading))
  3680. ((org-try-structure-completion))
  3681. ((org-try-cdlatex-tab))
  3682. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3683. (or (not (bolp))
  3684. (not (looking-at outline-regexp))))
  3685. (call-interactively (global-key-binding "\t")))
  3686. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3687. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3688. (or (and (eq org-cycle-emulate-tab 'white)
  3689. (= (match-end 0) (point-at-eol)))
  3690. (and (eq org-cycle-emulate-tab 'whitestart)
  3691. (>= (match-end 0) pos))))
  3692. t
  3693. (eq org-cycle-emulate-tab t))
  3694. (call-interactively (global-key-binding "\t")))
  3695. (t (save-excursion
  3696. (org-back-to-heading)
  3697. (org-cycle))))))
  3698. ;;;###autoload
  3699. (defun org-global-cycle (&optional arg)
  3700. "Cycle the global visibility. For details see `org-cycle'.
  3701. With C-u prefix arg, switch to startup visibility.
  3702. With a numeric prefix, show all headlines up to that level."
  3703. (interactive "P")
  3704. (let ((org-cycle-include-plain-lists
  3705. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3706. (cond
  3707. ((integerp arg)
  3708. (show-all)
  3709. (hide-sublevels arg)
  3710. (setq org-cycle-global-status 'contents))
  3711. ((equal arg '(4))
  3712. (org-set-startup-visibility)
  3713. (message "Startup visibility, plus VISIBILITY properties."))
  3714. (t
  3715. (org-cycle '(4))))))
  3716. (defun org-set-startup-visibility ()
  3717. "Set the visibility required by startup options and properties."
  3718. (cond
  3719. ((eq org-startup-folded t)
  3720. (org-cycle '(4)))
  3721. ((eq org-startup-folded 'content)
  3722. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3723. (org-cycle '(4)) (org-cycle '(4)))))
  3724. (org-set-visibility-according-to-property 'no-cleanup)
  3725. (org-cycle-hide-archived-subtrees 'all)
  3726. (org-cycle-hide-drawers 'all)
  3727. (org-cycle-show-empty-lines 'all))
  3728. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3729. "Switch subtree visibilities according to :VISIBILITY: property."
  3730. (interactive)
  3731. (let (state)
  3732. (save-excursion
  3733. (goto-char (point-min))
  3734. (while (re-search-forward
  3735. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3736. nil t)
  3737. (setq state (match-string 1))
  3738. (save-excursion
  3739. (org-back-to-heading t)
  3740. (hide-subtree)
  3741. (org-reveal)
  3742. (cond
  3743. ((equal state '("fold" "folded"))
  3744. (hide-subtree))
  3745. ((equal state "children")
  3746. (org-show-hidden-entry)
  3747. (show-children))
  3748. ((equal state "content")
  3749. (save-excursion
  3750. (save-restriction
  3751. (org-narrow-to-subtree)
  3752. (org-content))))
  3753. ((member state '("all" "showall"))
  3754. (show-subtree)))))
  3755. (unless no-cleanup
  3756. (org-cycle-hide-archived-subtrees 'all)
  3757. (org-cycle-hide-drawers 'all)
  3758. (org-cycle-show-empty-lines 'all)))))
  3759. (defun org-overview ()
  3760. "Switch to overview mode, shoing only top-level headlines.
  3761. Really, this shows all headlines with level equal or greater than the level
  3762. of the first headline in the buffer. This is important, because if the
  3763. first headline is not level one, then (hide-sublevels 1) gives confusing
  3764. results."
  3765. (interactive)
  3766. (let ((level (save-excursion
  3767. (goto-char (point-min))
  3768. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3769. (progn
  3770. (goto-char (match-beginning 0))
  3771. (funcall outline-level))))))
  3772. (and level (hide-sublevels level))))
  3773. (defun org-content (&optional arg)
  3774. "Show all headlines in the buffer, like a table of contents.
  3775. With numerical argument N, show content up to level N."
  3776. (interactive "P")
  3777. (save-excursion
  3778. ;; Visit all headings and show their offspring
  3779. (and (integerp arg) (org-overview))
  3780. (goto-char (point-max))
  3781. (catch 'exit
  3782. (while (and (progn (condition-case nil
  3783. (outline-previous-visible-heading 1)
  3784. (error (goto-char (point-min))))
  3785. t)
  3786. (looking-at outline-regexp))
  3787. (if (integerp arg)
  3788. (show-children (1- arg))
  3789. (show-branches))
  3790. (if (bobp) (throw 'exit nil))))))
  3791. (defun org-optimize-window-after-visibility-change (state)
  3792. "Adjust the window after a change in outline visibility.
  3793. This function is the default value of the hook `org-cycle-hook'."
  3794. (when (get-buffer-window (current-buffer))
  3795. (cond
  3796. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3797. ; ((eq state 'overview) (org-beginning-of-line))
  3798. ((eq state 'content) nil)
  3799. ((eq state 'all) nil)
  3800. ((eq state 'folded) nil)
  3801. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3802. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3803. (defun org-compact-display-after-subtree-move ()
  3804. (let (beg end)
  3805. (save-excursion
  3806. (if (org-up-heading-safe)
  3807. (progn
  3808. (hide-subtree)
  3809. (show-entry)
  3810. (show-children)
  3811. (org-cycle-show-empty-lines 'children)
  3812. (org-cycle-hide-drawers 'children))
  3813. (org-overview)))))
  3814. (defun org-cycle-show-empty-lines (state)
  3815. "Show empty lines above all visible headlines.
  3816. The region to be covered depends on STATE when called through
  3817. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3818. entire buffer covered. Note that an empty line is only shown if there
  3819. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3820. (when (> org-cycle-separator-lines 0)
  3821. (save-excursion
  3822. (let* ((n org-cycle-separator-lines)
  3823. (re (cond
  3824. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3825. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3826. (t (let ((ns (number-to-string (- n 2))))
  3827. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3828. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3829. beg end)
  3830. (cond
  3831. ((memq state '(overview contents t))
  3832. (setq beg (point-min) end (point-max)))
  3833. ((memq state '(children folded))
  3834. (setq beg (point) end (progn (org-end-of-subtree t t)
  3835. (beginning-of-line 2)
  3836. (point)))))
  3837. (when beg
  3838. (goto-char beg)
  3839. (while (re-search-forward re end t)
  3840. (if (not (get-char-property (match-end 1) 'invisible))
  3841. (outline-flag-region
  3842. (match-beginning 1) (match-end 1) nil)))))))
  3843. ;; Never hide empty lines at the end of the file.
  3844. (save-excursion
  3845. (goto-char (point-max))
  3846. (outline-previous-heading)
  3847. (outline-end-of-heading)
  3848. (if (and (looking-at "[ \t\n]+")
  3849. (= (match-end 0) (point-max)))
  3850. (outline-flag-region (point) (match-end 0) nil))))
  3851. (defun org-show-empty-lines-in-parent ()
  3852. "Move to the parent and re-show empty lines before visible headlines."
  3853. (save-excursion
  3854. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3855. (org-cycle-show-empty-lines context))))
  3856. (defun org-cycle-hide-drawers (state)
  3857. "Re-hide all drawers after a visibility state change."
  3858. (when (and (org-mode-p)
  3859. (not (memq state '(overview folded))))
  3860. (save-excursion
  3861. (let* ((globalp (memq state '(contents all)))
  3862. (beg (if globalp (point-min) (point)))
  3863. (end (if globalp (point-max) (org-end-of-subtree t))))
  3864. (goto-char beg)
  3865. (while (re-search-forward org-drawer-regexp end t)
  3866. (org-flag-drawer t))))))
  3867. (defun org-flag-drawer (flag)
  3868. (save-excursion
  3869. (beginning-of-line 1)
  3870. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3871. (let ((b (match-end 0))
  3872. (outline-regexp org-outline-regexp))
  3873. (if (re-search-forward
  3874. "^[ \t]*:END:"
  3875. (save-excursion (outline-next-heading) (point)) t)
  3876. (outline-flag-region b (point-at-eol) flag)
  3877. (error ":END: line missing"))))))
  3878. (defun org-subtree-end-visible-p ()
  3879. "Is the end of the current subtree visible?"
  3880. (pos-visible-in-window-p
  3881. (save-excursion (org-end-of-subtree t) (point))))
  3882. (defun org-first-headline-recenter (&optional N)
  3883. "Move cursor to the first headline and recenter the headline.
  3884. Optional argument N means, put the headline into the Nth line of the window."
  3885. (goto-char (point-min))
  3886. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3887. (beginning-of-line)
  3888. (recenter (prefix-numeric-value N))))
  3889. ;;; Org-goto
  3890. (defvar org-goto-window-configuration nil)
  3891. (defvar org-goto-marker nil)
  3892. (defvar org-goto-map
  3893. (let ((map (make-sparse-keymap)))
  3894. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3895. (while (setq cmd (pop cmds))
  3896. (substitute-key-definition cmd cmd map global-map)))
  3897. (suppress-keymap map)
  3898. (org-defkey map "\C-m" 'org-goto-ret)
  3899. (org-defkey map [(return)] 'org-goto-ret)
  3900. (org-defkey map [(left)] 'org-goto-left)
  3901. (org-defkey map [(right)] 'org-goto-right)
  3902. (org-defkey map [(control ?g)] 'org-goto-quit)
  3903. (org-defkey map "\C-i" 'org-cycle)
  3904. (org-defkey map [(tab)] 'org-cycle)
  3905. (org-defkey map [(down)] 'outline-next-visible-heading)
  3906. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3907. (if org-goto-auto-isearch
  3908. (if (fboundp 'define-key-after)
  3909. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3910. nil)
  3911. (org-defkey map "q" 'org-goto-quit)
  3912. (org-defkey map "n" 'outline-next-visible-heading)
  3913. (org-defkey map "p" 'outline-previous-visible-heading)
  3914. (org-defkey map "f" 'outline-forward-same-level)
  3915. (org-defkey map "b" 'outline-backward-same-level)
  3916. (org-defkey map "u" 'outline-up-heading))
  3917. (org-defkey map "/" 'org-occur)
  3918. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3919. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3920. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3921. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3922. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3923. map))
  3924. (defconst org-goto-help
  3925. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3926. RET=jump to location [Q]uit and return to previous location
  3927. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3928. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3929. ;; FIXME: Docstring doe not mention both interfaces
  3930. (defun org-goto (&optional alternative-interface)
  3931. "Look up a different location in the current file, keeping current visibility.
  3932. When you want look-up or go to a different location in a document, the
  3933. fastest way is often to fold the entire buffer and then dive into the tree.
  3934. This method has the disadvantage, that the previous location will be folded,
  3935. which may not be what you want.
  3936. This command works around this by showing a copy of the current buffer
  3937. in an indirect buffer, in overview mode. You can dive into the tree in
  3938. that copy, use org-occur and incremental search to find a location.
  3939. When pressing RET or `Q', the command returns to the original buffer in
  3940. which the visibility is still unchanged. After RET is will also jump to
  3941. the location selected in the indirect buffer and expose the
  3942. the headline hierarchy above."
  3943. (interactive "P")
  3944. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3945. (org-refile-use-outline-path t)
  3946. (interface
  3947. (if (not alternative-interface)
  3948. org-goto-interface
  3949. (if (eq org-goto-interface 'outline)
  3950. 'outline-path-completion
  3951. 'outline)))
  3952. (org-goto-start-pos (point))
  3953. (selected-point
  3954. (if (eq interface 'outline)
  3955. (car (org-get-location (current-buffer) org-goto-help))
  3956. (nth 3 (org-refile-get-location "Goto: ")))))
  3957. (if selected-point
  3958. (progn
  3959. (org-mark-ring-push org-goto-start-pos)
  3960. (goto-char selected-point)
  3961. (if (or (org-invisible-p) (org-invisible-p2))
  3962. (org-show-context 'org-goto)))
  3963. (message "Quit"))))
  3964. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3965. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3966. (defvar org-goto-local-auto-isearch-map) ; defined below
  3967. (defun org-get-location (buf help)
  3968. "Let the user select a location in the Org-mode buffer BUF.
  3969. This function uses a recursive edit. It returns the selected position
  3970. or nil."
  3971. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3972. (isearch-hide-immediately nil)
  3973. (isearch-search-fun-function
  3974. (lambda () 'org-goto-local-search-headings))
  3975. (org-goto-selected-point org-goto-exit-command))
  3976. (save-excursion
  3977. (save-window-excursion
  3978. (delete-other-windows)
  3979. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3980. (switch-to-buffer
  3981. (condition-case nil
  3982. (make-indirect-buffer (current-buffer) "*org-goto*")
  3983. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3984. (with-output-to-temp-buffer "*Help*"
  3985. (princ help))
  3986. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  3987. (setq buffer-read-only nil)
  3988. (let ((org-startup-truncated t)
  3989. (org-startup-folded nil)
  3990. (org-startup-align-all-tables nil))
  3991. (org-mode)
  3992. (org-overview))
  3993. (setq buffer-read-only t)
  3994. (if (and (boundp 'org-goto-start-pos)
  3995. (integer-or-marker-p org-goto-start-pos))
  3996. (let ((org-show-hierarchy-above t)
  3997. (org-show-siblings t)
  3998. (org-show-following-heading t))
  3999. (goto-char org-goto-start-pos)
  4000. (and (org-invisible-p) (org-show-context)))
  4001. (goto-char (point-min)))
  4002. (org-beginning-of-line)
  4003. (message "Select location and press RET")
  4004. (use-local-map org-goto-map)
  4005. (recursive-edit)
  4006. ))
  4007. (kill-buffer "*org-goto*")
  4008. (cons org-goto-selected-point org-goto-exit-command)))
  4009. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4010. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4011. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4012. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4013. (defun org-goto-local-search-headings (string bound noerror)
  4014. "Search and make sure that any matches are in headlines."
  4015. (catch 'return
  4016. (while (if isearch-forward
  4017. (search-forward string bound noerror)
  4018. (search-backward string bound noerror))
  4019. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4020. (and (member :headline context)
  4021. (not (member :tags context))))
  4022. (throw 'return (point))))))
  4023. (defun org-goto-local-auto-isearch ()
  4024. "Start isearch."
  4025. (interactive)
  4026. (goto-char (point-min))
  4027. (let ((keys (this-command-keys)))
  4028. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4029. (isearch-mode t)
  4030. (isearch-process-search-char (string-to-char keys)))))
  4031. (defun org-goto-ret (&optional arg)
  4032. "Finish `org-goto' by going to the new location."
  4033. (interactive "P")
  4034. (setq org-goto-selected-point (point)
  4035. org-goto-exit-command 'return)
  4036. (throw 'exit nil))
  4037. (defun org-goto-left ()
  4038. "Finish `org-goto' by going to the new location."
  4039. (interactive)
  4040. (if (org-on-heading-p)
  4041. (progn
  4042. (beginning-of-line 1)
  4043. (setq org-goto-selected-point (point)
  4044. org-goto-exit-command 'left)
  4045. (throw 'exit nil))
  4046. (error "Not on a heading")))
  4047. (defun org-goto-right ()
  4048. "Finish `org-goto' by going to the new location."
  4049. (interactive)
  4050. (if (org-on-heading-p)
  4051. (progn
  4052. (setq org-goto-selected-point (point)
  4053. org-goto-exit-command 'right)
  4054. (throw 'exit nil))
  4055. (error "Not on a heading")))
  4056. (defun org-goto-quit ()
  4057. "Finish `org-goto' without cursor motion."
  4058. (interactive)
  4059. (setq org-goto-selected-point nil)
  4060. (setq org-goto-exit-command 'quit)
  4061. (throw 'exit nil))
  4062. ;;; Indirect buffer display of subtrees
  4063. (defvar org-indirect-dedicated-frame nil
  4064. "This is the frame being used for indirect tree display.")
  4065. (defvar org-last-indirect-buffer nil)
  4066. (defun org-tree-to-indirect-buffer (&optional arg)
  4067. "Create indirect buffer and narrow it to current subtree.
  4068. With numerical prefix ARG, go up to this level and then take that tree.
  4069. If ARG is negative, go up that many levels.
  4070. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4071. indirect buffer previously made with this command, to avoid proliferation of
  4072. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4073. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4074. is kept so that you can work with several indirect buffers at the same time.
  4075. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4076. requests that a new frame be made for the new buffer, so that the dedicated
  4077. frame is not changed."
  4078. (interactive "P")
  4079. (let ((cbuf (current-buffer))
  4080. (cwin (selected-window))
  4081. (pos (point))
  4082. beg end level heading ibuf)
  4083. (save-excursion
  4084. (org-back-to-heading t)
  4085. (when (numberp arg)
  4086. (setq level (org-outline-level))
  4087. (if (< arg 0) (setq arg (+ level arg)))
  4088. (while (> (setq level (org-outline-level)) arg)
  4089. (outline-up-heading 1 t)))
  4090. (setq beg (point)
  4091. heading (org-get-heading))
  4092. (org-end-of-subtree t) (setq end (point)))
  4093. (if (and (buffer-live-p org-last-indirect-buffer)
  4094. (not (eq org-indirect-buffer-display 'new-frame))
  4095. (not arg))
  4096. (kill-buffer org-last-indirect-buffer))
  4097. (setq ibuf (org-get-indirect-buffer cbuf)
  4098. org-last-indirect-buffer ibuf)
  4099. (cond
  4100. ((or (eq org-indirect-buffer-display 'new-frame)
  4101. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4102. (select-frame (make-frame))
  4103. (delete-other-windows)
  4104. (switch-to-buffer ibuf)
  4105. (org-set-frame-title heading))
  4106. ((eq org-indirect-buffer-display 'dedicated-frame)
  4107. (raise-frame
  4108. (select-frame (or (and org-indirect-dedicated-frame
  4109. (frame-live-p org-indirect-dedicated-frame)
  4110. org-indirect-dedicated-frame)
  4111. (setq org-indirect-dedicated-frame (make-frame)))))
  4112. (delete-other-windows)
  4113. (switch-to-buffer ibuf)
  4114. (org-set-frame-title (concat "Indirect: " heading)))
  4115. ((eq org-indirect-buffer-display 'current-window)
  4116. (switch-to-buffer ibuf))
  4117. ((eq org-indirect-buffer-display 'other-window)
  4118. (pop-to-buffer ibuf))
  4119. (t (error "Invalid value.")))
  4120. (if (featurep 'xemacs)
  4121. (save-excursion (org-mode) (turn-on-font-lock)))
  4122. (narrow-to-region beg end)
  4123. (show-all)
  4124. (goto-char pos)
  4125. (and (window-live-p cwin) (select-window cwin))))
  4126. (defun org-get-indirect-buffer (&optional buffer)
  4127. (setq buffer (or buffer (current-buffer)))
  4128. (let ((n 1) (base (buffer-name buffer)) bname)
  4129. (while (buffer-live-p
  4130. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4131. (setq n (1+ n)))
  4132. (condition-case nil
  4133. (make-indirect-buffer buffer bname 'clone)
  4134. (error (make-indirect-buffer buffer bname)))))
  4135. (defun org-set-frame-title (title)
  4136. "Set the title of the current frame to the string TITLE."
  4137. ;; FIXME: how to name a single frame in XEmacs???
  4138. (unless (featurep 'xemacs)
  4139. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4140. ;;;; Structure editing
  4141. ;;; Inserting headlines
  4142. (defun org-insert-heading (&optional force-heading)
  4143. "Insert a new heading or item with same depth at point.
  4144. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4145. If point is at the beginning of a headline, insert a sibling before the
  4146. current headline. If point is not at the beginning, do not split the line,
  4147. but create the new headline after the current line."
  4148. (interactive "P")
  4149. (if (= (buffer-size) 0)
  4150. (insert "\n* ")
  4151. (when (or force-heading (not (org-insert-item)))
  4152. (let* ((head (save-excursion
  4153. (condition-case nil
  4154. (progn
  4155. (org-back-to-heading)
  4156. (match-string 0))
  4157. (error "*"))))
  4158. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4159. pos hide-previous previous-pos)
  4160. (cond
  4161. ((and (org-on-heading-p) (bolp)
  4162. (or (bobp)
  4163. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4164. ;; insert before the current line
  4165. (open-line (if blank 2 1)))
  4166. ((and (bolp)
  4167. (or (bobp)
  4168. (save-excursion
  4169. (backward-char 1) (not (org-invisible-p)))))
  4170. ;; insert right here
  4171. nil)
  4172. (t
  4173. ;; somewhere in the line
  4174. (save-excursion
  4175. (setq previous-pos (point-at-bol))
  4176. (end-of-line)
  4177. (setq hide-previous (org-invisible-p)))
  4178. (and org-insert-heading-respect-content (org-show-subtree))
  4179. (let ((split
  4180. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4181. (save-excursion
  4182. (let ((p (point)))
  4183. (goto-char (point-at-bol))
  4184. (and (looking-at org-complex-heading-regexp)
  4185. (> p (match-beginning 4)))))))
  4186. tags pos)
  4187. (cond
  4188. (org-insert-heading-respect-content
  4189. (org-end-of-subtree nil t)
  4190. (or (bolp) (newline))
  4191. (open-line 1))
  4192. ((org-on-heading-p)
  4193. (when hide-previous
  4194. (show-children)
  4195. (org-show-entry))
  4196. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4197. (setq tags (and (match-end 2) (match-string 2)))
  4198. (and (match-end 1)
  4199. (delete-region (match-beginning 1) (match-end 1)))
  4200. (setq pos (point-at-bol))
  4201. (or split (end-of-line 1))
  4202. (delete-horizontal-space)
  4203. (newline (if blank 2 1))
  4204. (when tags
  4205. (save-excursion
  4206. (goto-char pos)
  4207. (end-of-line 1)
  4208. (insert " " tags)
  4209. (org-set-tags nil 'align))))
  4210. (t
  4211. (or split (end-of-line 1))
  4212. (newline (if blank 2 1)))))))
  4213. (insert head) (just-one-space)
  4214. (setq pos (point))
  4215. (end-of-line 1)
  4216. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4217. (when (and org-insert-heading-respect-content hide-previous)
  4218. (save-excursion
  4219. (goto-char previous-pos)
  4220. (hide-subtree)))
  4221. (run-hooks 'org-insert-heading-hook)))))
  4222. (defun org-get-heading (&optional no-tags)
  4223. "Return the heading of the current entry, without the stars."
  4224. (save-excursion
  4225. (org-back-to-heading t)
  4226. (if (looking-at
  4227. (if no-tags
  4228. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4229. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4230. (match-string 1) "")))
  4231. (defun org-insert-heading-after-current ()
  4232. "Insert a new heading with same level as current, after current subtree."
  4233. (interactive)
  4234. (org-back-to-heading)
  4235. (org-insert-heading)
  4236. (org-move-subtree-down)
  4237. (end-of-line 1))
  4238. (defun org-insert-heading-respect-content ()
  4239. (interactive)
  4240. (let ((org-insert-heading-respect-content t))
  4241. (org-insert-heading t)))
  4242. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4243. (interactive "P")
  4244. (let ((org-insert-heading-respect-content t))
  4245. (org-insert-todo-heading force-state t)))
  4246. (defun org-insert-todo-heading (arg &optional force-heading)
  4247. "Insert a new heading with the same level and TODO state as current heading.
  4248. If the heading has no TODO state, or if the state is DONE, use the first
  4249. state (TODO by default). Also with prefix arg, force first state."
  4250. (interactive "P")
  4251. (when (or force-heading (not (org-insert-item 'checkbox)))
  4252. (org-insert-heading force-heading)
  4253. (save-excursion
  4254. (org-back-to-heading)
  4255. (outline-previous-heading)
  4256. (looking-at org-todo-line-regexp))
  4257. (if (or arg
  4258. (not (match-beginning 2))
  4259. (member (match-string 2) org-done-keywords))
  4260. (insert (car org-todo-keywords-1) " ")
  4261. (insert (match-string 2) " "))
  4262. (when org-provide-todo-statistics
  4263. (org-update-parent-todo-statistics))))
  4264. (defun org-insert-subheading (arg)
  4265. "Insert a new subheading and demote it.
  4266. Works for outline headings and for plain lists alike."
  4267. (interactive "P")
  4268. (org-insert-heading arg)
  4269. (cond
  4270. ((org-on-heading-p) (org-do-demote))
  4271. ((org-at-item-p) (org-indent-item 1))))
  4272. (defun org-insert-todo-subheading (arg)
  4273. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4274. Works for outline headings and for plain lists alike."
  4275. (interactive "P")
  4276. (org-insert-todo-heading arg)
  4277. (cond
  4278. ((org-on-heading-p) (org-do-demote))
  4279. ((org-at-item-p) (org-indent-item 1))))
  4280. ;;; Promotion and Demotion
  4281. (defun org-promote-subtree ()
  4282. "Promote the entire subtree.
  4283. See also `org-promote'."
  4284. (interactive)
  4285. (save-excursion
  4286. (org-map-tree 'org-promote))
  4287. (org-fix-position-after-promote))
  4288. (defun org-demote-subtree ()
  4289. "Demote the entire subtree. See `org-demote'.
  4290. See also `org-promote'."
  4291. (interactive)
  4292. (save-excursion
  4293. (org-map-tree 'org-demote))
  4294. (org-fix-position-after-promote))
  4295. (defun org-do-promote ()
  4296. "Promote the current heading higher up the tree.
  4297. If the region is active in `transient-mark-mode', promote all headings
  4298. in the region."
  4299. (interactive)
  4300. (save-excursion
  4301. (if (org-region-active-p)
  4302. (org-map-region 'org-promote (region-beginning) (region-end))
  4303. (org-promote)))
  4304. (org-fix-position-after-promote))
  4305. (defun org-do-demote ()
  4306. "Demote the current heading lower down the tree.
  4307. If the region is active in `transient-mark-mode', demote all headings
  4308. in the region."
  4309. (interactive)
  4310. (save-excursion
  4311. (if (org-region-active-p)
  4312. (org-map-region 'org-demote (region-beginning) (region-end))
  4313. (org-demote)))
  4314. (org-fix-position-after-promote))
  4315. (defun org-fix-position-after-promote ()
  4316. "Make sure that after pro/demotion cursor position is right."
  4317. (let ((pos (point)))
  4318. (when (save-excursion
  4319. (beginning-of-line 1)
  4320. (looking-at org-todo-line-regexp)
  4321. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4322. (cond ((eobp) (insert " "))
  4323. ((eolp) (insert " "))
  4324. ((equal (char-after) ?\ ) (forward-char 1))))))
  4325. (defun org-reduced-level (l)
  4326. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4327. (defun org-get-valid-level (level &optional change)
  4328. "Rectify a level change under the influence of `org-odd-levels-only'
  4329. LEVEL is a current level, CHANGE is by how much the level should be
  4330. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4331. even level numbers will become the next higher odd number."
  4332. (if org-odd-levels-only
  4333. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4334. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4335. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4336. (max 1 (+ level change))))
  4337. (if (boundp 'define-obsolete-function-alias)
  4338. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4339. (define-obsolete-function-alias 'org-get-legal-level
  4340. 'org-get-valid-level)
  4341. (define-obsolete-function-alias 'org-get-legal-level
  4342. 'org-get-valid-level "23.1")))
  4343. (defun org-promote ()
  4344. "Promote the current heading higher up the tree.
  4345. If the region is active in `transient-mark-mode', promote all headings
  4346. in the region."
  4347. (org-back-to-heading t)
  4348. (let* ((level (save-match-data (funcall outline-level)))
  4349. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4350. (diff (abs (- level (length up-head) -1))))
  4351. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4352. (replace-match up-head nil t)
  4353. ;; Fixup tag positioning
  4354. (and org-auto-align-tags (org-set-tags nil t))
  4355. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4356. (defun org-demote ()
  4357. "Demote the current heading lower down the tree.
  4358. If the region is active in `transient-mark-mode', demote all headings
  4359. in the region."
  4360. (org-back-to-heading t)
  4361. (let* ((level (save-match-data (funcall outline-level)))
  4362. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4363. (diff (abs (- level (length down-head) -1))))
  4364. (replace-match down-head nil t)
  4365. ;; Fixup tag positioning
  4366. (and org-auto-align-tags (org-set-tags nil t))
  4367. (if org-adapt-indentation (org-fixup-indentation diff))))
  4368. (defun org-map-tree (fun)
  4369. "Call FUN for every heading underneath the current one."
  4370. (org-back-to-heading)
  4371. (let ((level (funcall outline-level)))
  4372. (save-excursion
  4373. (funcall fun)
  4374. (while (and (progn
  4375. (outline-next-heading)
  4376. (> (funcall outline-level) level))
  4377. (not (eobp)))
  4378. (funcall fun)))))
  4379. (defun org-map-region (fun beg end)
  4380. "Call FUN for every heading between BEG and END."
  4381. (let ((org-ignore-region t))
  4382. (save-excursion
  4383. (setq end (copy-marker end))
  4384. (goto-char beg)
  4385. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4386. (< (point) end))
  4387. (funcall fun))
  4388. (while (and (progn
  4389. (outline-next-heading)
  4390. (< (point) end))
  4391. (not (eobp)))
  4392. (funcall fun)))))
  4393. (defun org-fixup-indentation (diff)
  4394. "Change the indentation in the current entry by DIFF
  4395. However, if any line in the current entry has no indentation, or if it
  4396. would end up with no indentation after the change, nothing at all is done."
  4397. (save-excursion
  4398. (let ((end (save-excursion (outline-next-heading)
  4399. (point-marker)))
  4400. (prohibit (if (> diff 0)
  4401. "^\\S-"
  4402. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4403. col)
  4404. (unless (save-excursion (end-of-line 1)
  4405. (re-search-forward prohibit end t))
  4406. (while (and (< (point) end)
  4407. (re-search-forward "^[ \t]+" end t))
  4408. (goto-char (match-end 0))
  4409. (setq col (current-column))
  4410. (if (< diff 0) (replace-match ""))
  4411. (indent-to (+ diff col))))
  4412. (move-marker end nil))))
  4413. (defun org-convert-to-odd-levels ()
  4414. "Convert an org-mode file with all levels allowed to one with odd levels.
  4415. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4416. level 5 etc."
  4417. (interactive)
  4418. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4419. (let ((org-odd-levels-only nil) n)
  4420. (save-excursion
  4421. (goto-char (point-min))
  4422. (while (re-search-forward "^\\*\\*+ " nil t)
  4423. (setq n (- (length (match-string 0)) 2))
  4424. (while (>= (setq n (1- n)) 0)
  4425. (org-demote))
  4426. (end-of-line 1))))))
  4427. (defun org-convert-to-oddeven-levels ()
  4428. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4429. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4430. section with an even level, conversion would destroy the structure of the file. An error
  4431. is signaled in this case."
  4432. (interactive)
  4433. (goto-char (point-min))
  4434. ;; First check if there are no even levels
  4435. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4436. (org-show-context t)
  4437. (error "Not all levels are odd in this file. Conversion not possible."))
  4438. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4439. (let ((org-odd-levels-only nil) n)
  4440. (save-excursion
  4441. (goto-char (point-min))
  4442. (while (re-search-forward "^\\*\\*+ " nil t)
  4443. (setq n (/ (1- (length (match-string 0))) 2))
  4444. (while (>= (setq n (1- n)) 0)
  4445. (org-promote))
  4446. (end-of-line 1))))))
  4447. (defun org-tr-level (n)
  4448. "Make N odd if required."
  4449. (if org-odd-levels-only (1+ (/ n 2)) n))
  4450. ;;; Vertical tree motion, cutting and pasting of subtrees
  4451. (defun org-move-subtree-up (&optional arg)
  4452. "Move the current subtree up past ARG headlines of the same level."
  4453. (interactive "p")
  4454. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4455. (defun org-move-subtree-down (&optional arg)
  4456. "Move the current subtree down past ARG headlines of the same level."
  4457. (interactive "p")
  4458. (setq arg (prefix-numeric-value arg))
  4459. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4460. 'outline-get-last-sibling))
  4461. (ins-point (make-marker))
  4462. (cnt (abs arg))
  4463. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4464. ;; Select the tree
  4465. (org-back-to-heading)
  4466. (setq beg0 (point))
  4467. (save-excursion
  4468. (setq ne-beg (org-back-over-empty-lines))
  4469. (setq beg (point)))
  4470. (save-match-data
  4471. (save-excursion (outline-end-of-heading)
  4472. (setq folded (org-invisible-p)))
  4473. (outline-end-of-subtree))
  4474. (outline-next-heading)
  4475. (setq ne-end (org-back-over-empty-lines))
  4476. (setq end (point))
  4477. (goto-char beg0)
  4478. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4479. ;; include less whitespace
  4480. (save-excursion
  4481. (goto-char beg)
  4482. (forward-line (- ne-beg ne-end))
  4483. (setq beg (point))))
  4484. ;; Find insertion point, with error handling
  4485. (while (> cnt 0)
  4486. (or (and (funcall movfunc) (looking-at outline-regexp))
  4487. (progn (goto-char beg0)
  4488. (error "Cannot move past superior level or buffer limit")))
  4489. (setq cnt (1- cnt)))
  4490. (if (> arg 0)
  4491. ;; Moving forward - still need to move over subtree
  4492. (progn (org-end-of-subtree t t)
  4493. (save-excursion
  4494. (org-back-over-empty-lines)
  4495. (or (bolp) (newline)))))
  4496. (setq ne-ins (org-back-over-empty-lines))
  4497. (move-marker ins-point (point))
  4498. (setq txt (buffer-substring beg end))
  4499. (org-save-markers-in-region beg end)
  4500. (delete-region beg end)
  4501. (outline-flag-region (1- beg) beg nil)
  4502. (outline-flag-region (1- (point)) (point) nil)
  4503. (let ((bbb (point)))
  4504. (insert-before-markers txt)
  4505. (org-reinstall-markers-in-region bbb)
  4506. (move-marker ins-point bbb))
  4507. (or (bolp) (insert "\n"))
  4508. (setq ins-end (point))
  4509. (goto-char ins-point)
  4510. (org-skip-whitespace)
  4511. (when (and (< arg 0)
  4512. (org-first-sibling-p)
  4513. (> ne-ins ne-beg))
  4514. ;; Move whitespace back to beginning
  4515. (save-excursion
  4516. (goto-char ins-end)
  4517. (let ((kill-whole-line t))
  4518. (kill-line (- ne-ins ne-beg)) (point)))
  4519. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4520. (move-marker ins-point nil)
  4521. (org-compact-display-after-subtree-move)
  4522. (org-show-empty-lines-in-parent)
  4523. (unless folded
  4524. (org-show-entry)
  4525. (show-children)
  4526. (org-cycle-hide-drawers 'children))))
  4527. (defvar org-subtree-clip ""
  4528. "Clipboard for cut and paste of subtrees.
  4529. This is actually only a copy of the kill, because we use the normal kill
  4530. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4531. (defvar org-subtree-clip-folded nil
  4532. "Was the last copied subtree folded?
  4533. This is used to fold the tree back after pasting.")
  4534. (defun org-cut-subtree (&optional n)
  4535. "Cut the current subtree into the clipboard.
  4536. With prefix arg N, cut this many sequential subtrees.
  4537. This is a short-hand for marking the subtree and then cutting it."
  4538. (interactive "p")
  4539. (org-copy-subtree n 'cut))
  4540. (defun org-copy-subtree (&optional n cut force-store-markers)
  4541. "Cut the current subtree into the clipboard.
  4542. With prefix arg N, cut this many sequential subtrees.
  4543. This is a short-hand for marking the subtree and then copying it.
  4544. If CUT is non-nil, actually cut the subtree.
  4545. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4546. of some markers in the region, even if CUT is non-nil. This is
  4547. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4548. (interactive "p")
  4549. (let (beg end folded (beg0 (point)))
  4550. (if (interactive-p)
  4551. (org-back-to-heading nil) ; take what looks like a subtree
  4552. (org-back-to-heading t)) ; take what is really there
  4553. (org-back-over-empty-lines)
  4554. (setq beg (point))
  4555. (skip-chars-forward " \t\r\n")
  4556. (save-match-data
  4557. (save-excursion (outline-end-of-heading)
  4558. (setq folded (org-invisible-p)))
  4559. (condition-case nil
  4560. (outline-forward-same-level (1- n))
  4561. (error nil))
  4562. (org-end-of-subtree t t))
  4563. (org-back-over-empty-lines)
  4564. (setq end (point))
  4565. (goto-char beg0)
  4566. (when (> end beg)
  4567. (setq org-subtree-clip-folded folded)
  4568. (when (or cut force-store-markers)
  4569. (org-save-markers-in-region beg end))
  4570. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4571. (setq org-subtree-clip (current-kill 0))
  4572. (message "%s: Subtree(s) with %d characters"
  4573. (if cut "Cut" "Copied")
  4574. (length org-subtree-clip)))))
  4575. (defun org-paste-subtree (&optional level tree for-yank)
  4576. "Paste the clipboard as a subtree, with modification of headline level.
  4577. The entire subtree is promoted or demoted in order to match a new headline
  4578. level.
  4579. If the cursor is at the beginning of a headline, the same level as
  4580. that headline is used to paste the tree
  4581. If not, the new level is derived from the *visible* headings
  4582. before and after the insertion point, and taken to be the inferior headline
  4583. level of the two. So if the previous visible heading is level 3 and the
  4584. next is level 4 (or vice versa), level 4 will be used for insertion.
  4585. This makes sure that the subtree remains an independent subtree and does
  4586. not swallow low level entries.
  4587. You can also force a different level, either by using a numeric prefix
  4588. argument, or by inserting the heading marker by hand. For example, if the
  4589. cursor is after \"*****\", then the tree will be shifted to level 5.
  4590. If optional TREE is given, use this text instead of the kill ring.
  4591. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4592. move back over whitespace before inserting, and move point to the end of
  4593. the inserted text when done."
  4594. (interactive "P")
  4595. (unless (org-kill-is-subtree-p tree)
  4596. (error "%s"
  4597. (substitute-command-keys
  4598. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4599. (let* ((visp (not (org-invisible-p)))
  4600. (txt (or tree (and kill-ring (current-kill 0))))
  4601. (^re (concat "^\\(" outline-regexp "\\)"))
  4602. (re (concat "\\(" outline-regexp "\\)"))
  4603. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4604. (old-level (if (string-match ^re txt)
  4605. (- (match-end 0) (match-beginning 0) 1)
  4606. -1))
  4607. (force-level (cond (level (prefix-numeric-value level))
  4608. ((and (looking-at "[ \t]*$")
  4609. (string-match
  4610. ^re_ (buffer-substring
  4611. (point-at-bol) (point))))
  4612. (- (match-end 1) (match-beginning 1)))
  4613. ((and (bolp)
  4614. (looking-at org-outline-regexp))
  4615. (- (match-end 0) (point) 1))
  4616. (t nil)))
  4617. (previous-level (save-excursion
  4618. (condition-case nil
  4619. (progn
  4620. (outline-previous-visible-heading 1)
  4621. (if (looking-at re)
  4622. (- (match-end 0) (match-beginning 0) 1)
  4623. 1))
  4624. (error 1))))
  4625. (next-level (save-excursion
  4626. (condition-case nil
  4627. (progn
  4628. (or (looking-at outline-regexp)
  4629. (outline-next-visible-heading 1))
  4630. (if (looking-at re)
  4631. (- (match-end 0) (match-beginning 0) 1)
  4632. 1))
  4633. (error 1))))
  4634. (new-level (or force-level (max previous-level next-level)))
  4635. (shift (if (or (= old-level -1)
  4636. (= new-level -1)
  4637. (= old-level new-level))
  4638. 0
  4639. (- new-level old-level)))
  4640. (delta (if (> shift 0) -1 1))
  4641. (func (if (> shift 0) 'org-demote 'org-promote))
  4642. (org-odd-levels-only nil)
  4643. beg end newend)
  4644. ;; Remove the forced level indicator
  4645. (if force-level
  4646. (delete-region (point-at-bol) (point)))
  4647. ;; Paste
  4648. (beginning-of-line 1)
  4649. (unless for-yank (org-back-over-empty-lines))
  4650. (setq beg (point))
  4651. (insert-before-markers txt)
  4652. (unless (string-match "\n\\'" txt) (insert "\n"))
  4653. (setq newend (point))
  4654. (org-reinstall-markers-in-region beg)
  4655. (setq end (point))
  4656. (goto-char beg)
  4657. (skip-chars-forward " \t\n\r")
  4658. (setq beg (point))
  4659. (if (and (org-invisible-p) visp)
  4660. (save-excursion (outline-show-heading)))
  4661. ;; Shift if necessary
  4662. (unless (= shift 0)
  4663. (save-restriction
  4664. (narrow-to-region beg end)
  4665. (while (not (= shift 0))
  4666. (org-map-region func (point-min) (point-max))
  4667. (setq shift (+ delta shift)))
  4668. (goto-char (point-min))
  4669. (setq newend (point-max))))
  4670. (when (or (interactive-p) for-yank)
  4671. (message "Clipboard pasted as level %d subtree" new-level))
  4672. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4673. kill-ring
  4674. (eq org-subtree-clip (current-kill 0))
  4675. org-subtree-clip-folded)
  4676. ;; The tree was folded before it was killed/copied
  4677. (hide-subtree))
  4678. (and for-yank (goto-char newend))))
  4679. (defun org-kill-is-subtree-p (&optional txt)
  4680. "Check if the current kill is an outline subtree, or a set of trees.
  4681. Returns nil if kill does not start with a headline, or if the first
  4682. headline level is not the largest headline level in the tree.
  4683. So this will actually accept several entries of equal levels as well,
  4684. which is OK for `org-paste-subtree'.
  4685. If optional TXT is given, check this string instead of the current kill."
  4686. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4687. (start-level (and kill
  4688. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4689. org-outline-regexp "\\)")
  4690. kill)
  4691. (- (match-end 2) (match-beginning 2) 1)))
  4692. (re (concat "^" org-outline-regexp))
  4693. (start (1+ (or (match-beginning 2) -1))))
  4694. (if (not start-level)
  4695. (progn
  4696. nil) ;; does not even start with a heading
  4697. (catch 'exit
  4698. (while (setq start (string-match re kill (1+ start)))
  4699. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4700. (throw 'exit nil)))
  4701. t))))
  4702. (defvar org-markers-to-move nil
  4703. "Markers that should be moved with a cut-and-paste operation.
  4704. Those markers are stored together with their positions relative to
  4705. the start of the region.")
  4706. (defun org-save-markers-in-region (beg end)
  4707. "Check markers in region.
  4708. If these markers are between BEG and END, record their position relative
  4709. to BEG, so that after moving the block of text, we can put the markers back
  4710. into place.
  4711. This function gets called just before an entry or tree gets cut from the
  4712. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4713. called immediately, to move the markers with the entries."
  4714. (setq org-markers-to-move nil)
  4715. (when (featurep 'org-clock)
  4716. (org-clock-save-markers-for-cut-and-paste beg end))
  4717. (when (featurep 'org-agenda)
  4718. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4719. (defun org-check-and-save-marker (marker beg end)
  4720. "Check if MARKER is between BEG and END.
  4721. If yes, remember the marker and the distance to BEG."
  4722. (when (and (marker-buffer marker)
  4723. (equal (marker-buffer marker) (current-buffer)))
  4724. (if (and (>= marker beg) (< marker end))
  4725. (push (cons marker (- marker beg)) org-markers-to-move))))
  4726. (defun org-reinstall-markers-in-region (beg)
  4727. "Move all remembered markers to their position relative to BEG."
  4728. (mapc (lambda (x)
  4729. (move-marker (car x) (+ beg (cdr x))))
  4730. org-markers-to-move)
  4731. (setq org-markers-to-move nil))
  4732. (defun org-narrow-to-subtree ()
  4733. "Narrow buffer to the current subtree."
  4734. (interactive)
  4735. (save-excursion
  4736. (save-match-data
  4737. (narrow-to-region
  4738. (progn (org-back-to-heading) (point))
  4739. (progn (org-end-of-subtree t) (point))))))
  4740. ;;; Outline Sorting
  4741. (defun org-sort (with-case)
  4742. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4743. Optional argument WITH-CASE means sort case-sensitively."
  4744. (interactive "P")
  4745. (if (org-at-table-p)
  4746. (org-call-with-arg 'org-table-sort-lines with-case)
  4747. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4748. (defun org-sort-remove-invisible (s)
  4749. (remove-text-properties 0 (length s) org-rm-props s)
  4750. (while (string-match org-bracket-link-regexp s)
  4751. (setq s (replace-match (if (match-end 2)
  4752. (match-string 3 s)
  4753. (match-string 1 s)) t t s)))
  4754. s)
  4755. (defvar org-priority-regexp) ; defined later in the file
  4756. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4757. "Sort entries on a certain level of an outline tree.
  4758. If there is an active region, the entries in the region are sorted.
  4759. Else, if the cursor is before the first entry, sort the top-level items.
  4760. Else, the children of the entry at point are sorted.
  4761. Sorting can be alphabetically, numerically, and by date/time as given by
  4762. the first time stamp in the entry. The command prompts for the sorting
  4763. type unless it has been given to the function through the SORTING-TYPE
  4764. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4765. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4766. called with point at the beginning of the record. It must return either
  4767. a string or a number that should serve as the sorting key for that record.
  4768. Comparing entries ignores case by default. However, with an optional argument
  4769. WITH-CASE, the sorting considers case as well."
  4770. (interactive "P")
  4771. (let ((case-func (if with-case 'identity 'downcase))
  4772. start beg end stars re re2
  4773. txt what tmp plain-list-p)
  4774. ;; Find beginning and end of region to sort
  4775. (cond
  4776. ((org-region-active-p)
  4777. ;; we will sort the region
  4778. (setq end (region-end)
  4779. what "region")
  4780. (goto-char (region-beginning))
  4781. (if (not (org-on-heading-p)) (outline-next-heading))
  4782. (setq start (point)))
  4783. ((org-at-item-p)
  4784. ;; we will sort this plain list
  4785. (org-beginning-of-item-list) (setq start (point))
  4786. (org-end-of-item-list) (setq end (point))
  4787. (goto-char start)
  4788. (setq plain-list-p t
  4789. what "plain list"))
  4790. ((or (org-on-heading-p)
  4791. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4792. ;; we will sort the children of the current headline
  4793. (org-back-to-heading)
  4794. (setq start (point)
  4795. end (progn (org-end-of-subtree t t)
  4796. (org-back-over-empty-lines)
  4797. (point))
  4798. what "children")
  4799. (goto-char start)
  4800. (show-subtree)
  4801. (outline-next-heading))
  4802. (t
  4803. ;; we will sort the top-level entries in this file
  4804. (goto-char (point-min))
  4805. (or (org-on-heading-p) (outline-next-heading))
  4806. (setq start (point) end (point-max) what "top-level")
  4807. (goto-char start)
  4808. (show-all)))
  4809. (setq beg (point))
  4810. (if (>= beg end) (error "Nothing to sort"))
  4811. (unless plain-list-p
  4812. (looking-at "\\(\\*+\\)")
  4813. (setq stars (match-string 1)
  4814. re (concat "^" (regexp-quote stars) " +")
  4815. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4816. txt (buffer-substring beg end))
  4817. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4818. (if (and (not (equal stars "*")) (string-match re2 txt))
  4819. (error "Region to sort contains a level above the first entry")))
  4820. (unless sorting-type
  4821. (message
  4822. (if plain-list-p
  4823. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4824. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  4825. what)
  4826. (setq sorting-type (read-char-exclusive))
  4827. (and (= (downcase sorting-type) ?f)
  4828. (setq getkey-func
  4829. (completing-read "Sort using function: "
  4830. obarray 'fboundp t nil nil))
  4831. (setq getkey-func (intern getkey-func)))
  4832. (and (= (downcase sorting-type) ?r)
  4833. (setq property
  4834. (completing-read "Property: "
  4835. (mapcar 'list (org-buffer-property-keys t))
  4836. nil t))))
  4837. (message "Sorting entries...")
  4838. (save-restriction
  4839. (narrow-to-region start end)
  4840. (let ((dcst (downcase sorting-type))
  4841. (now (current-time)))
  4842. (sort-subr
  4843. (/= dcst sorting-type)
  4844. ;; This function moves to the beginning character of the "record" to
  4845. ;; be sorted.
  4846. (if plain-list-p
  4847. (lambda nil
  4848. (if (org-at-item-p) t (goto-char (point-max))))
  4849. (lambda nil
  4850. (if (re-search-forward re nil t)
  4851. (goto-char (match-beginning 0))
  4852. (goto-char (point-max)))))
  4853. ;; This function moves to the last character of the "record" being
  4854. ;; sorted.
  4855. (if plain-list-p
  4856. 'org-end-of-item
  4857. (lambda nil
  4858. (save-match-data
  4859. (condition-case nil
  4860. (outline-forward-same-level 1)
  4861. (error
  4862. (goto-char (point-max)))))))
  4863. ;; This function returns the value that gets sorted against.
  4864. (if plain-list-p
  4865. (lambda nil
  4866. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4867. (cond
  4868. ((= dcst ?n)
  4869. (string-to-number (buffer-substring (match-end 0)
  4870. (point-at-eol))))
  4871. ((= dcst ?a)
  4872. (buffer-substring (match-end 0) (point-at-eol)))
  4873. ((= dcst ?t)
  4874. (if (re-search-forward org-ts-regexp
  4875. (point-at-eol) t)
  4876. (org-time-string-to-time (match-string 0))
  4877. now))
  4878. ((= dcst ?f)
  4879. (if getkey-func
  4880. (progn
  4881. (setq tmp (funcall getkey-func))
  4882. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4883. tmp)
  4884. (error "Invalid key function `%s'" getkey-func)))
  4885. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4886. (lambda nil
  4887. (cond
  4888. ((= dcst ?n)
  4889. (if (looking-at org-complex-heading-regexp)
  4890. (string-to-number (match-string 4))
  4891. nil))
  4892. ((= dcst ?a)
  4893. (if (looking-at org-complex-heading-regexp)
  4894. (funcall case-func (match-string 4))
  4895. nil))
  4896. ((= dcst ?t)
  4897. (if (re-search-forward org-ts-regexp
  4898. (save-excursion
  4899. (forward-line 2)
  4900. (point)) t)
  4901. (org-time-string-to-time (match-string 0))
  4902. now))
  4903. ((= dcst ?p)
  4904. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4905. (string-to-char (match-string 2))
  4906. org-default-priority))
  4907. ((= dcst ?r)
  4908. (or (org-entry-get nil property) ""))
  4909. ((= dcst ?o)
  4910. (if (looking-at org-complex-heading-regexp)
  4911. (- 9999 (length (member (match-string 2)
  4912. org-todo-keywords-1)))))
  4913. ((= dcst ?f)
  4914. (if getkey-func
  4915. (progn
  4916. (setq tmp (funcall getkey-func))
  4917. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4918. tmp)
  4919. (error "Invalid key function `%s'" getkey-func)))
  4920. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4921. nil
  4922. (cond
  4923. ((= dcst ?a) 'string<)
  4924. ((= dcst ?t) 'time-less-p)
  4925. (t nil)))))
  4926. (message "Sorting entries...done")))
  4927. (defun org-do-sort (table what &optional with-case sorting-type)
  4928. "Sort TABLE of WHAT according to SORTING-TYPE.
  4929. The user will be prompted for the SORTING-TYPE if the call to this
  4930. function does not specify it. WHAT is only for the prompt, to indicate
  4931. what is being sorted. The sorting key will be extracted from
  4932. the car of the elements of the table.
  4933. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4934. (unless sorting-type
  4935. (message
  4936. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4937. what)
  4938. (setq sorting-type (read-char-exclusive)))
  4939. (let ((dcst (downcase sorting-type))
  4940. extractfun comparefun)
  4941. ;; Define the appropriate functions
  4942. (cond
  4943. ((= dcst ?n)
  4944. (setq extractfun 'string-to-number
  4945. comparefun (if (= dcst sorting-type) '< '>)))
  4946. ((= dcst ?a)
  4947. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4948. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4949. comparefun (if (= dcst sorting-type)
  4950. 'string<
  4951. (lambda (a b) (and (not (string< a b))
  4952. (not (string= a b)))))))
  4953. ((= dcst ?t)
  4954. (setq extractfun
  4955. (lambda (x)
  4956. (if (string-match org-ts-regexp x)
  4957. (time-to-seconds
  4958. (org-time-string-to-time (match-string 0 x)))
  4959. 0))
  4960. comparefun (if (= dcst sorting-type) '< '>)))
  4961. (t (error "Invalid sorting type `%c'" sorting-type)))
  4962. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4963. table)
  4964. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4965. ;;; Editing source examples
  4966. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4967. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4968. (defvar org-edit-src-force-single-line nil)
  4969. (defvar org-edit-src-from-org-mode nil)
  4970. (defvar org-edit-src-picture nil)
  4971. (define-minor-mode org-exit-edit-mode
  4972. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4973. (defun org-edit-src-code ()
  4974. "Edit the source code example at point.
  4975. An indirect buffer is created, and that buffer is then narrowed to the
  4976. example at point and switched to the correct language mode. When done,
  4977. exit by killing the buffer with \\[org-edit-src-exit]."
  4978. (interactive)
  4979. (let ((line (org-current-line))
  4980. (case-fold-search t)
  4981. (msg (substitute-command-keys
  4982. "Edit, then exit with C-c ' (C-c and single quote)"))
  4983. (info (org-edit-src-find-region-and-lang))
  4984. (org-mode-p (eq major-mode 'org-mode))
  4985. beg end lang lang-f single)
  4986. (if (not info)
  4987. nil
  4988. (setq beg (nth 0 info)
  4989. end (nth 1 info)
  4990. lang (nth 2 info)
  4991. single (nth 3 info)
  4992. lang-f (intern (concat lang "-mode")))
  4993. (unless (functionp lang-f)
  4994. (error "No such language mode: %s" lang-f))
  4995. (goto-line line)
  4996. (if (get-buffer "*Org Edit Src Example*")
  4997. (kill-buffer "*Org Edit Src Example*"))
  4998. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4999. "*Org Edit Src Example*"))
  5000. (narrow-to-region beg end)
  5001. (remove-text-properties beg end '(display nil invisible nil
  5002. intangible nil))
  5003. (let ((org-inhibit-startup t))
  5004. (funcall lang-f))
  5005. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5006. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5007. (when org-mode-p
  5008. (goto-char (point-min))
  5009. (while (re-search-forward "^," nil t)
  5010. (replace-match "")))
  5011. (goto-line line)
  5012. (org-exit-edit-mode)
  5013. (org-set-local 'header-line-format msg)
  5014. (message "%s" msg)
  5015. t)))
  5016. (defun org-edit-fixed-width-region ()
  5017. "Edit the fixed-width ascii drawing at point.
  5018. This must be a region where each line starts with ca colon followed by
  5019. a space character.
  5020. An indirect buffer is created, and that buffer is then narrowed to the
  5021. example at point and switched to artist-mode. When done,
  5022. exit by killing the buffer with \\[org-edit-src-exit]."
  5023. (interactive)
  5024. (let ((line (org-current-line))
  5025. (case-fold-search t)
  5026. (msg (substitute-command-keys
  5027. "Edit, then exit with C-c ' (C-c and single quote)"))
  5028. (org-mode-p (eq major-mode 'org-mode))
  5029. beg end lang lang-f)
  5030. (beginning-of-line 1)
  5031. (if (looking-at "[ \t]*[^:\n \t]")
  5032. nil
  5033. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  5034. (setq beg (point) end (match-end 0))
  5035. (save-excursion
  5036. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5037. (setq beg (point-at-bol 2))
  5038. (setq beg (point))))
  5039. (save-excursion
  5040. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5041. (setq end (1- (match-beginning 0)))
  5042. (setq end (point))))
  5043. (goto-line line)
  5044. (if (get-buffer "*Org Edit Picture*")
  5045. (kill-buffer "*Org Edit Picture*"))
  5046. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5047. "*Org Edit Picture*"))
  5048. (narrow-to-region beg end)
  5049. (remove-text-properties beg end '(display nil invisible nil
  5050. intangible nil))
  5051. (when (fboundp 'font-lock-unfontify-region)
  5052. (font-lock-unfontify-region (point-min) (point-max)))
  5053. (cond
  5054. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5055. (fundamental-mode)
  5056. (artist-mode 1))
  5057. (t (funcall org-edit-fixed-width-region-mode)))
  5058. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5059. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5060. (set (make-local-variable 'org-edit-src-picture) t)
  5061. (goto-char (point-min))
  5062. (while (re-search-forward "^[ \t]*: " nil t)
  5063. (replace-match ""))
  5064. (goto-line line)
  5065. (org-exit-edit-mode)
  5066. (org-set-local 'header-line-format msg)
  5067. (message "%s" msg)
  5068. t))))
  5069. (defun org-edit-src-find-region-and-lang ()
  5070. "Find the region and language for a local edit.
  5071. Return a list with beginning and end of the region, a string representing
  5072. the language, a switch telling of the content should be in a single line."
  5073. (let ((re-list
  5074. (append
  5075. org-edit-src-region-extra
  5076. '(
  5077. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5078. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5079. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5080. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5081. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5082. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5083. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5084. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5085. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5086. ("^#\\+html:" "\n" "html" single-line)
  5087. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5088. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5089. ("^#\\+latex:" "\n" "latex" single-line)
  5090. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5091. ("^#\\+ascii:" "\n" "ascii" single-line)
  5092. )))
  5093. (pos (point))
  5094. re re1 re2 single beg end lang)
  5095. (catch 'exit
  5096. (while (setq entry (pop re-list))
  5097. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5098. single (nth 3 entry))
  5099. (save-excursion
  5100. (if (or (looking-at re1)
  5101. (re-search-backward re1 nil t))
  5102. (progn
  5103. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5104. (if (and (re-search-forward re2 nil t)
  5105. (>= (match-end 0) pos))
  5106. (throw 'exit (list beg (match-beginning 0) lang single))))
  5107. (if (or (looking-at re2)
  5108. (re-search-forward re2 nil t))
  5109. (progn
  5110. (setq end (match-beginning 0))
  5111. (if (and (re-search-backward re1 nil t)
  5112. (<= (match-beginning 0) pos))
  5113. (throw 'exit
  5114. (list (match-end 0) end
  5115. (org-edit-src-get-lang lang) single)))))))))))
  5116. (defun org-edit-src-get-lang (lang)
  5117. "Extract the src language."
  5118. (let ((m (match-string 0)))
  5119. (cond
  5120. ((stringp lang) lang)
  5121. ((integerp lang) (match-string lang))
  5122. ((and (eq lang 'lang)
  5123. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5124. (match-string 1 m))
  5125. ((and (eq lang 'style)
  5126. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5127. (match-string 1 m))
  5128. (t "fundamental"))))
  5129. (defun org-edit-src-exit ()
  5130. "Exit special edit and protect problematic lines."
  5131. (interactive)
  5132. (unless (buffer-base-buffer (current-buffer))
  5133. (error "This is not an indirect buffer, something is wrong..."))
  5134. (unless (> (point-min) 1)
  5135. (error "This buffer is not narrowed, something is wrong..."))
  5136. (goto-char (point-min))
  5137. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5138. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5139. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5140. (goto-char (point-min))
  5141. (while (re-search-forward "\n" nil t)
  5142. (replace-match " "))
  5143. (goto-char (point-min))
  5144. (if (looking-at "\\s-*") (replace-match " "))
  5145. (if (re-search-forward "\\s-+\\'" nil t)
  5146. (replace-match "")))
  5147. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5148. (goto-char (point-min))
  5149. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5150. (replace-match ",\\1"))
  5151. (when font-lock-mode
  5152. (font-lock-unfontify-region (point-min) (point-max)))
  5153. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5154. (when (org-bound-and-true-p org-edit-src-picture)
  5155. (goto-char (point-min))
  5156. (while (re-search-forward "^" nil t)
  5157. (replace-match ": "))
  5158. (when font-lock-mode
  5159. (font-lock-unfontify-region (point-min) (point-max)))
  5160. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5161. (kill-buffer (current-buffer))
  5162. (and (org-mode-p) (org-restart-font-lock)))
  5163. ;;; The orgstruct minor mode
  5164. ;; Define a minor mode which can be used in other modes in order to
  5165. ;; integrate the org-mode structure editing commands.
  5166. ;; This is really a hack, because the org-mode structure commands use
  5167. ;; keys which normally belong to the major mode. Here is how it
  5168. ;; works: The minor mode defines all the keys necessary to operate the
  5169. ;; structure commands, but wraps the commands into a function which
  5170. ;; tests if the cursor is currently at a headline or a plain list
  5171. ;; item. If that is the case, the structure command is used,
  5172. ;; temporarily setting many Org-mode variables like regular
  5173. ;; expressions for filling etc. However, when any of those keys is
  5174. ;; used at a different location, function uses `key-binding' to look
  5175. ;; up if the key has an associated command in another currently active
  5176. ;; keymap (minor modes, major mode, global), and executes that
  5177. ;; command. There might be problems if any of the keys is otherwise
  5178. ;; used as a prefix key.
  5179. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5180. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5181. ;; addresses this by checking explicitly for both bindings.
  5182. (defvar orgstruct-mode-map (make-sparse-keymap)
  5183. "Keymap for the minor `orgstruct-mode'.")
  5184. (defvar org-local-vars nil
  5185. "List of local variables, for use by `orgstruct-mode'")
  5186. ;;;###autoload
  5187. (define-minor-mode orgstruct-mode
  5188. "Toggle the minor more `orgstruct-mode'.
  5189. This mode is for using Org-mode structure commands in other modes.
  5190. The following key behave as if Org-mode was active, if the cursor
  5191. is on a headline, or on a plain list item (both in the definition
  5192. of Org-mode).
  5193. M-up Move entry/item up
  5194. M-down Move entry/item down
  5195. M-left Promote
  5196. M-right Demote
  5197. M-S-up Move entry/item up
  5198. M-S-down Move entry/item down
  5199. M-S-left Promote subtree
  5200. M-S-right Demote subtree
  5201. M-q Fill paragraph and items like in Org-mode
  5202. C-c ^ Sort entries
  5203. C-c - Cycle list bullet
  5204. TAB Cycle item visibility
  5205. M-RET Insert new heading/item
  5206. S-M-RET Insert new TODO heading / Chekbox item
  5207. C-c C-c Set tags / toggle checkbox"
  5208. nil " OrgStruct" nil
  5209. (org-load-modules-maybe)
  5210. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5211. ;;;###autoload
  5212. (defun turn-on-orgstruct ()
  5213. "Unconditionally turn on `orgstruct-mode'."
  5214. (orgstruct-mode 1))
  5215. ;;;###autoload
  5216. (defun turn-on-orgstruct++ ()
  5217. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5218. In addition to setting orgstruct-mode, this also exports all indentation and
  5219. autofilling variables from org-mode into the buffer. Note that turning
  5220. off orgstruct-mode will *not* remove these additional settings."
  5221. (orgstruct-mode 1)
  5222. (let (var val)
  5223. (mapc
  5224. (lambda (x)
  5225. (when (string-match
  5226. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5227. (symbol-name (car x)))
  5228. (setq var (car x) val (nth 1 x))
  5229. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5230. org-local-vars)))
  5231. (defun orgstruct-error ()
  5232. "Error when there is no default binding for a structure key."
  5233. (interactive)
  5234. (error "This key has no function outside structure elements"))
  5235. (defun orgstruct-setup ()
  5236. "Setup orgstruct keymaps."
  5237. (let ((nfunc 0)
  5238. (bindings
  5239. (list
  5240. '([(meta up)] org-metaup)
  5241. '([(meta down)] org-metadown)
  5242. '([(meta left)] org-metaleft)
  5243. '([(meta right)] org-metaright)
  5244. '([(meta shift up)] org-shiftmetaup)
  5245. '([(meta shift down)] org-shiftmetadown)
  5246. '([(meta shift left)] org-shiftmetaleft)
  5247. '([(meta shift right)] org-shiftmetaright)
  5248. '([(shift up)] org-shiftup)
  5249. '([(shift down)] org-shiftdown)
  5250. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5251. '("\M-q" fill-paragraph)
  5252. '("\C-c^" org-sort)
  5253. '("\C-c-" org-cycle-list-bullet)))
  5254. elt key fun cmd)
  5255. (while (setq elt (pop bindings))
  5256. (setq nfunc (1+ nfunc))
  5257. (setq key (org-key (car elt))
  5258. fun (nth 1 elt)
  5259. cmd (orgstruct-make-binding fun nfunc key))
  5260. (org-defkey orgstruct-mode-map key cmd))
  5261. ;; Special treatment needed for TAB and RET
  5262. (org-defkey orgstruct-mode-map [(tab)]
  5263. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5264. (org-defkey orgstruct-mode-map "\C-i"
  5265. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5266. (org-defkey orgstruct-mode-map "\M-\C-m"
  5267. (orgstruct-make-binding 'org-insert-heading 105
  5268. "\M-\C-m" [(meta return)]))
  5269. (org-defkey orgstruct-mode-map [(meta return)]
  5270. (orgstruct-make-binding 'org-insert-heading 106
  5271. [(meta return)] "\M-\C-m"))
  5272. (org-defkey orgstruct-mode-map [(shift meta return)]
  5273. (orgstruct-make-binding 'org-insert-todo-heading 107
  5274. [(meta return)] "\M-\C-m"))
  5275. (unless org-local-vars
  5276. (setq org-local-vars (org-get-local-variables)))
  5277. t))
  5278. (defun orgstruct-make-binding (fun n &rest keys)
  5279. "Create a function for binding in the structure minor mode.
  5280. FUN is the command to call inside a table. N is used to create a unique
  5281. command name. KEYS are keys that should be checked in for a command
  5282. to execute outside of tables."
  5283. (eval
  5284. (list 'defun
  5285. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5286. '(arg)
  5287. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5288. "Outside of structure, run the binding of `"
  5289. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5290. "'.")
  5291. '(interactive "p")
  5292. (list 'if
  5293. '(org-context-p 'headline 'item)
  5294. (list 'org-run-like-in-org-mode (list 'quote fun))
  5295. (list 'let '(orgstruct-mode)
  5296. (list 'call-interactively
  5297. (append '(or)
  5298. (mapcar (lambda (k)
  5299. (list 'key-binding k))
  5300. keys)
  5301. '('orgstruct-error))))))))
  5302. (defun org-context-p (&rest contexts)
  5303. "Check if local context is any of CONTEXTS.
  5304. Possible values in the list of contexts are `table', `headline', and `item'."
  5305. (let ((pos (point)))
  5306. (goto-char (point-at-bol))
  5307. (prog1 (or (and (memq 'table contexts)
  5308. (looking-at "[ \t]*|"))
  5309. (and (memq 'headline contexts)
  5310. ;;????????? (looking-at "\\*+"))
  5311. (looking-at outline-regexp))
  5312. (and (memq 'item contexts)
  5313. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5314. (goto-char pos))))
  5315. (defun org-get-local-variables ()
  5316. "Return a list of all local variables in an org-mode buffer."
  5317. (let (varlist)
  5318. (with-current-buffer (get-buffer-create "*Org tmp*")
  5319. (erase-buffer)
  5320. (org-mode)
  5321. (setq varlist (buffer-local-variables)))
  5322. (kill-buffer "*Org tmp*")
  5323. (delq nil
  5324. (mapcar
  5325. (lambda (x)
  5326. (setq x
  5327. (if (symbolp x)
  5328. (list x)
  5329. (list (car x) (list 'quote (cdr x)))))
  5330. (if (string-match
  5331. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5332. (symbol-name (car x)))
  5333. x nil))
  5334. varlist))))
  5335. ;;;###autoload
  5336. (defun org-run-like-in-org-mode (cmd)
  5337. (org-load-modules-maybe)
  5338. (unless org-local-vars
  5339. (setq org-local-vars (org-get-local-variables)))
  5340. (eval (list 'let org-local-vars
  5341. (list 'call-interactively (list 'quote cmd)))))
  5342. ;;;; Archiving
  5343. (defun org-get-category (&optional pos)
  5344. "Get the category applying to position POS."
  5345. (get-text-property (or pos (point)) 'org-category))
  5346. (defun org-refresh-category-properties ()
  5347. "Refresh category text properties in the buffer."
  5348. (let ((def-cat (cond
  5349. ((null org-category)
  5350. (if buffer-file-name
  5351. (file-name-sans-extension
  5352. (file-name-nondirectory buffer-file-name))
  5353. "???"))
  5354. ((symbolp org-category) (symbol-name org-category))
  5355. (t org-category)))
  5356. beg end cat pos optionp)
  5357. (org-unmodified
  5358. (save-excursion
  5359. (save-restriction
  5360. (widen)
  5361. (goto-char (point-min))
  5362. (put-text-property (point) (point-max) 'org-category def-cat)
  5363. (while (re-search-forward
  5364. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5365. (setq pos (match-end 0)
  5366. optionp (equal (char-after (match-beginning 0)) ?#)
  5367. cat (org-trim (match-string 2)))
  5368. (if optionp
  5369. (setq beg (point-at-bol) end (point-max))
  5370. (org-back-to-heading t)
  5371. (setq beg (point) end (org-end-of-subtree t t)))
  5372. (put-text-property beg end 'org-category cat)
  5373. (goto-char pos)))))))
  5374. ;;;; Link Stuff
  5375. ;;; Link abbreviations
  5376. (defun org-link-expand-abbrev (link)
  5377. "Apply replacements as defined in `org-link-abbrev-alist."
  5378. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5379. (let* ((key (match-string 1 link))
  5380. (as (or (assoc key org-link-abbrev-alist-local)
  5381. (assoc key org-link-abbrev-alist)))
  5382. (tag (and (match-end 2) (match-string 3 link)))
  5383. rpl)
  5384. (if (not as)
  5385. link
  5386. (setq rpl (cdr as))
  5387. (cond
  5388. ((symbolp rpl) (funcall rpl tag))
  5389. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5390. (t (concat rpl tag)))))
  5391. link))
  5392. ;;; Storing and inserting links
  5393. (defvar org-insert-link-history nil
  5394. "Minibuffer history for links inserted with `org-insert-link'.")
  5395. (defvar org-stored-links nil
  5396. "Contains the links stored with `org-store-link'.")
  5397. (defvar org-store-link-plist nil
  5398. "Plist with info about the most recently link created with `org-store-link'.")
  5399. (defvar org-link-protocols nil
  5400. "Link protocols added to Org-mode using `org-add-link-type'.")
  5401. (defvar org-store-link-functions nil
  5402. "List of functions that are called to create and store a link.
  5403. Each function will be called in turn until one returns a non-nil
  5404. value. Each function should check if it is responsible for creating
  5405. this link (for example by looking at the major mode).
  5406. If not, it must exit and return nil.
  5407. If yes, it should return a non-nil value after a calling
  5408. `org-store-link-props' with a list of properties and values.
  5409. Special properties are:
  5410. :type The link prefix. like \"http\". This must be given.
  5411. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5412. This is obligatory as well.
  5413. :description Optional default description for the second pair
  5414. of brackets in an Org-mode link. The user can still change
  5415. this when inserting this link into an Org-mode buffer.
  5416. In addition to these, any additional properties can be specified
  5417. and then used in remember templates.")
  5418. (defun org-add-link-type (type &optional follow export)
  5419. "Add TYPE to the list of `org-link-types'.
  5420. Re-compute all regular expressions depending on `org-link-types'
  5421. FOLLOW and EXPORT are two functions.
  5422. FOLLOW should take the link path as the single argument and do whatever
  5423. is necessary to follow the link, for example find a file or display
  5424. a mail message.
  5425. EXPORT should format the link path for export to one of the export formats.
  5426. It should be a function accepting three arguments:
  5427. path the path of the link, the text after the prefix (like \"http:\")
  5428. desc the description of the link, if any, nil if there was no descripton
  5429. format the export format, a symbol like `html' or `latex'.
  5430. The function may use the FORMAT information to return different values
  5431. depending on the format. The return value will be put literally into
  5432. the exported file.
  5433. Org-mode has a built-in default for exporting links. If you are happy with
  5434. this default, there is no need to define an export function for the link
  5435. type. For a simple example of an export function, see `org-bbdb.el'."
  5436. (add-to-list 'org-link-types type t)
  5437. (org-make-link-regexps)
  5438. (if (assoc type org-link-protocols)
  5439. (setcdr (assoc type org-link-protocols) (list follow export))
  5440. (push (list type follow export) org-link-protocols)))
  5441. ;;;###autoload
  5442. (defun org-store-link (arg)
  5443. "\\<org-mode-map>Store an org-link to the current location.
  5444. This link is added to `org-stored-links' and can later be inserted
  5445. into an org-buffer with \\[org-insert-link].
  5446. For some link types, a prefix arg is interpreted:
  5447. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5448. For file links, arg negates `org-context-in-file-links'."
  5449. (interactive "P")
  5450. (org-load-modules-maybe)
  5451. (setq org-store-link-plist nil) ; reset
  5452. (let (link cpltxt desc description search txt)
  5453. (cond
  5454. ((run-hook-with-args-until-success 'org-store-link-functions)
  5455. (setq link (plist-get org-store-link-plist :link)
  5456. desc (or (plist-get org-store-link-plist :description) link)))
  5457. ((eq major-mode 'calendar-mode)
  5458. (let ((cd (calendar-cursor-to-date)))
  5459. (setq link
  5460. (format-time-string
  5461. (car org-time-stamp-formats)
  5462. (apply 'encode-time
  5463. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5464. nil nil nil))))
  5465. (org-store-link-props :type "calendar" :date cd)))
  5466. ((eq major-mode 'w3-mode)
  5467. (setq cpltxt (url-view-url t)
  5468. link (org-make-link cpltxt))
  5469. (org-store-link-props :type "w3" :url (url-view-url t)))
  5470. ((eq major-mode 'w3m-mode)
  5471. (setq cpltxt (or w3m-current-title w3m-current-url)
  5472. link (org-make-link w3m-current-url))
  5473. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5474. ((setq search (run-hook-with-args-until-success
  5475. 'org-create-file-search-functions))
  5476. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5477. "::" search))
  5478. (setq cpltxt (or description link)))
  5479. ((eq major-mode 'image-mode)
  5480. (setq cpltxt (concat "file:"
  5481. (abbreviate-file-name buffer-file-name))
  5482. link (org-make-link cpltxt))
  5483. (org-store-link-props :type "image" :file buffer-file-name))
  5484. ((eq major-mode 'dired-mode)
  5485. ;; link to the file in the current line
  5486. (setq cpltxt (concat "file:"
  5487. (abbreviate-file-name
  5488. (expand-file-name
  5489. (dired-get-filename nil t))))
  5490. link (org-make-link cpltxt)))
  5491. ((and buffer-file-name (org-mode-p))
  5492. ;; Just link to current headline
  5493. (setq cpltxt (concat "file:"
  5494. (abbreviate-file-name buffer-file-name)))
  5495. ;; Add a context search string
  5496. (when (org-xor org-context-in-file-links arg)
  5497. ;; Check if we are on a target
  5498. (if (org-in-regexp "<<\\(.*?\\)>>")
  5499. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5500. (setq txt (cond
  5501. ((org-on-heading-p) nil)
  5502. ((org-region-active-p)
  5503. (buffer-substring (region-beginning) (region-end)))
  5504. (t nil)))
  5505. (when (or (null txt) (string-match "\\S-" txt))
  5506. (setq cpltxt
  5507. (concat cpltxt "::"
  5508. (condition-case nil
  5509. (org-make-org-heading-search-string txt)
  5510. (error "")))
  5511. desc "NONE"))))
  5512. (if (string-match "::\\'" cpltxt)
  5513. (setq cpltxt (substring cpltxt 0 -2)))
  5514. (setq link (org-make-link cpltxt)))
  5515. ((buffer-file-name (buffer-base-buffer))
  5516. ;; Just link to this file here.
  5517. (setq cpltxt (concat "file:"
  5518. (abbreviate-file-name
  5519. (buffer-file-name (buffer-base-buffer)))))
  5520. ;; Add a context string
  5521. (when (org-xor org-context-in-file-links arg)
  5522. (setq txt (if (org-region-active-p)
  5523. (buffer-substring (region-beginning) (region-end))
  5524. (buffer-substring (point-at-bol) (point-at-eol))))
  5525. ;; Only use search option if there is some text.
  5526. (when (string-match "\\S-" txt)
  5527. (setq cpltxt
  5528. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5529. desc "NONE")))
  5530. (setq link (org-make-link cpltxt)))
  5531. ((interactive-p)
  5532. (error "Cannot link to a buffer which is not visiting a file"))
  5533. (t (setq link nil)))
  5534. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5535. (setq link (or link cpltxt)
  5536. desc (or desc cpltxt))
  5537. (if (equal desc "NONE") (setq desc nil))
  5538. (if (and (interactive-p) link)
  5539. (progn
  5540. (setq org-stored-links
  5541. (cons (list link desc) org-stored-links))
  5542. (message "Stored: %s" (or desc link)))
  5543. (and link (org-make-link-string link desc)))))
  5544. (defun org-store-link-props (&rest plist)
  5545. "Store link properties, extract names and addresses."
  5546. (let (x adr)
  5547. (when (setq x (plist-get plist :from))
  5548. (setq adr (mail-extract-address-components x))
  5549. (setq plist (plist-put plist :fromname (car adr)))
  5550. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5551. (when (setq x (plist-get plist :to))
  5552. (setq adr (mail-extract-address-components x))
  5553. (setq plist (plist-put plist :toname (car adr)))
  5554. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5555. (let ((from (plist-get plist :from))
  5556. (to (plist-get plist :to)))
  5557. (when (and from to org-from-is-user-regexp)
  5558. (setq plist
  5559. (plist-put plist :fromto
  5560. (if (string-match org-from-is-user-regexp from)
  5561. (concat "to %t")
  5562. (concat "from %f"))))))
  5563. (setq org-store-link-plist plist))
  5564. (defun org-add-link-props (&rest plist)
  5565. "Add these properties to the link property list."
  5566. (let (key value)
  5567. (while plist
  5568. (setq key (pop plist) value (pop plist))
  5569. (setq org-store-link-plist
  5570. (plist-put org-store-link-plist key value)))))
  5571. (defun org-email-link-description (&optional fmt)
  5572. "Return the description part of an email link.
  5573. This takes information from `org-store-link-plist' and formats it
  5574. according to FMT (default from `org-email-link-description-format')."
  5575. (setq fmt (or fmt org-email-link-description-format))
  5576. (let* ((p org-store-link-plist)
  5577. (to (plist-get p :toaddress))
  5578. (from (plist-get p :fromaddress))
  5579. (table
  5580. (list
  5581. (cons "%c" (plist-get p :fromto))
  5582. (cons "%F" (plist-get p :from))
  5583. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5584. (cons "%T" (plist-get p :to))
  5585. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5586. (cons "%s" (plist-get p :subject))
  5587. (cons "%m" (plist-get p :message-id)))))
  5588. (when (string-match "%c" fmt)
  5589. ;; Check if the user wrote this message
  5590. (if (and org-from-is-user-regexp from to
  5591. (save-match-data (string-match org-from-is-user-regexp from)))
  5592. (setq fmt (replace-match "to %t" t t fmt))
  5593. (setq fmt (replace-match "from %f" t t fmt))))
  5594. (org-replace-escapes fmt table)))
  5595. (defun org-make-org-heading-search-string (&optional string heading)
  5596. "Make search string for STRING or current headline."
  5597. (interactive)
  5598. (let ((s (or string (org-get-heading))))
  5599. (unless (and string (not heading))
  5600. ;; We are using a headline, clean up garbage in there.
  5601. (if (string-match org-todo-regexp s)
  5602. (setq s (replace-match "" t t s)))
  5603. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5604. (setq s (replace-match "" t t s)))
  5605. (setq s (org-trim s))
  5606. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5607. org-comment-string "\\)") s)
  5608. (setq s (replace-match "" t t s)))
  5609. (while (string-match org-ts-regexp s)
  5610. (setq s (replace-match "" t t s))))
  5611. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5612. (setq s (replace-match " " t t s)))
  5613. (or string (setq s (concat "*" s))) ; Add * for headlines
  5614. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5615. (defun org-make-link (&rest strings)
  5616. "Concatenate STRINGS."
  5617. (apply 'concat strings))
  5618. (defun org-make-link-string (link &optional description)
  5619. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5620. (unless (string-match "\\S-" link)
  5621. (error "Empty link"))
  5622. (when (stringp description)
  5623. ;; Remove brackets from the description, they are fatal.
  5624. (while (string-match "\\[" description)
  5625. (setq description (replace-match "{" t t description)))
  5626. (while (string-match "\\]" description)
  5627. (setq description (replace-match "}" t t description))))
  5628. (when (equal (org-link-escape link) description)
  5629. ;; No description needed, it is identical
  5630. (setq description nil))
  5631. (when (and (not description)
  5632. (not (equal link (org-link-escape link))))
  5633. (setq description (org-extract-attributes link)))
  5634. (concat "[[" (org-link-escape link) "]"
  5635. (if description (concat "[" description "]") "")
  5636. "]"))
  5637. (defconst org-link-escape-chars
  5638. '((?\ . "%20")
  5639. (?\[ . "%5B")
  5640. (?\] . "%5D")
  5641. (?\340 . "%E0") ; `a
  5642. (?\342 . "%E2") ; ^a
  5643. (?\347 . "%E7") ; ,c
  5644. (?\350 . "%E8") ; `e
  5645. (?\351 . "%E9") ; 'e
  5646. (?\352 . "%EA") ; ^e
  5647. (?\356 . "%EE") ; ^i
  5648. (?\364 . "%F4") ; ^o
  5649. (?\371 . "%F9") ; `u
  5650. (?\373 . "%FB") ; ^u
  5651. (?\; . "%3B")
  5652. (?? . "%3F")
  5653. (?= . "%3D")
  5654. (?+ . "%2B")
  5655. )
  5656. "Association list of escapes for some characters problematic in links.
  5657. This is the list that is used for internal purposes.")
  5658. (defconst org-link-escape-chars-browser
  5659. '((?\ . "%20")) ; 32 for the SPC char
  5660. "Association list of escapes for some characters problematic in links.
  5661. This is the list that is used before handing over to the browser.")
  5662. (defun org-link-escape (text &optional table)
  5663. "Escape charaters in TEXT that are problematic for links."
  5664. (setq table (or table org-link-escape-chars))
  5665. (when text
  5666. (let ((re (mapconcat (lambda (x) (regexp-quote
  5667. (char-to-string (car x))))
  5668. table "\\|")))
  5669. (while (string-match re text)
  5670. (setq text
  5671. (replace-match
  5672. (cdr (assoc (string-to-char (match-string 0 text))
  5673. table))
  5674. t t text)))
  5675. text)))
  5676. (defun org-link-unescape (text &optional table)
  5677. "Reverse the action of `org-link-escape'."
  5678. (setq table (or table org-link-escape-chars))
  5679. (when text
  5680. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5681. table "\\|")))
  5682. (while (string-match re text)
  5683. (setq text
  5684. (replace-match
  5685. (char-to-string (car (rassoc (match-string 0 text) table)))
  5686. t t text)))
  5687. text)))
  5688. (defun org-xor (a b)
  5689. "Exclusive or."
  5690. (if a (not b) b))
  5691. (defun org-get-header (header)
  5692. "Find a header field in the current buffer."
  5693. (save-excursion
  5694. (goto-char (point-min))
  5695. (let ((case-fold-search t) s)
  5696. (cond
  5697. ((eq header 'from)
  5698. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5699. (setq s (match-string 1)))
  5700. (while (string-match "\"" s)
  5701. (setq s (replace-match "" t t s)))
  5702. (if (string-match "[<(].*" s)
  5703. (setq s (replace-match "" t t s))))
  5704. ((eq header 'message-id)
  5705. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5706. (setq s (match-string 1))))
  5707. ((eq header 'subject)
  5708. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5709. (setq s (match-string 1)))))
  5710. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5711. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5712. s)))
  5713. (defun org-fixup-message-id-for-http (s)
  5714. "Replace special characters in a message id, so it can be used in an http query."
  5715. (while (string-match "<" s)
  5716. (setq s (replace-match "%3C" t t s)))
  5717. (while (string-match ">" s)
  5718. (setq s (replace-match "%3E" t t s)))
  5719. (while (string-match "@" s)
  5720. (setq s (replace-match "%40" t t s)))
  5721. s)
  5722. ;;;###autoload
  5723. (defun org-insert-link-global ()
  5724. "Insert a link like Org-mode does.
  5725. This command can be called in any mode to insert a link in Org-mode syntax."
  5726. (interactive)
  5727. (org-load-modules-maybe)
  5728. (org-run-like-in-org-mode 'org-insert-link))
  5729. (defun org-insert-link (&optional complete-file link-location)
  5730. "Insert a link. At the prompt, enter the link.
  5731. Completion can be used to insert any of the link protocol prefixes like
  5732. http or ftp in use.
  5733. The history can be used to select a link previously stored with
  5734. `org-store-link'. When the empty string is entered (i.e. if you just
  5735. press RET at the prompt), the link defaults to the most recently
  5736. stored link. As SPC triggers completion in the minibuffer, you need to
  5737. use M-SPC or C-q SPC to force the insertion of a space character.
  5738. You will also be prompted for a description, and if one is given, it will
  5739. be displayed in the buffer instead of the link.
  5740. If there is already a link at point, this command will allow you to edit link
  5741. and description parts.
  5742. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5743. be selected using completion. The path to the file will be relative to the
  5744. current directory if the file is in the current directory or a subdirectory.
  5745. Otherwise, the link will be the absolute path as completed in the minibuffer
  5746. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5747. option `org-link-file-path-type'.
  5748. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5749. the current directory or below.
  5750. With three \\[universal-argument] prefixes, negate the meaning of
  5751. `org-keep-stored-link-after-insertion'.
  5752. If `org-make-link-description-function' is non-nil, this function will be
  5753. called with the link target, and the result will be the default
  5754. link description.
  5755. If the LINK-LOCATION parameter is non-nil, this value will be
  5756. used as the link location instead of reading one interactively."
  5757. (interactive "P")
  5758. (let* ((wcf (current-window-configuration))
  5759. (region (if (org-region-active-p)
  5760. (buffer-substring (region-beginning) (region-end))))
  5761. (remove (and region (list (region-beginning) (region-end))))
  5762. (desc region)
  5763. tmphist ; byte-compile incorrectly complains about this
  5764. (link link-location)
  5765. entry file)
  5766. (cond
  5767. (link-location) ; specified by arg, just use it.
  5768. ((org-in-regexp org-bracket-link-regexp 1)
  5769. ;; We do have a link at point, and we are going to edit it.
  5770. (setq remove (list (match-beginning 0) (match-end 0)))
  5771. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5772. (setq link (read-string "Link: "
  5773. (org-link-unescape
  5774. (org-match-string-no-properties 1)))))
  5775. ((or (org-in-regexp org-angle-link-re)
  5776. (org-in-regexp org-plain-link-re))
  5777. ;; Convert to bracket link
  5778. (setq remove (list (match-beginning 0) (match-end 0))
  5779. link (read-string "Link: "
  5780. (org-remove-angle-brackets (match-string 0)))))
  5781. ((member complete-file '((4) (16)))
  5782. ;; Completing read for file names.
  5783. (setq file (read-file-name "File: "))
  5784. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5785. (pwd1 (file-name-as-directory (abbreviate-file-name
  5786. (expand-file-name ".")))))
  5787. (cond
  5788. ((equal complete-file '(16))
  5789. (setq link (org-make-link
  5790. "file:"
  5791. (abbreviate-file-name (expand-file-name file)))))
  5792. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5793. (setq link (org-make-link "file:" (match-string 1 file))))
  5794. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5795. (expand-file-name file))
  5796. (setq link (org-make-link
  5797. "file:" (match-string 1 (expand-file-name file)))))
  5798. (t (setq link (org-make-link "file:" file))))))
  5799. (t
  5800. ;; Read link, with completion for stored links.
  5801. (with-output-to-temp-buffer "*Org Links*"
  5802. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5803. (when org-stored-links
  5804. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5805. (princ (mapconcat
  5806. (lambda (x)
  5807. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5808. (reverse org-stored-links) "\n"))))
  5809. (let ((cw (selected-window)))
  5810. (select-window (get-buffer-window "*Org Links*"))
  5811. (org-fit-window-to-buffer)
  5812. (setq truncate-lines t)
  5813. (select-window cw))
  5814. ;; Fake a link history, containing the stored links.
  5815. (setq tmphist (append (mapcar 'car org-stored-links)
  5816. org-insert-link-history))
  5817. (unwind-protect
  5818. (setq link (org-completing-read
  5819. "Link: "
  5820. (append
  5821. (mapcar (lambda (x) (list (concat (car x) ":")))
  5822. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5823. (mapcar (lambda (x) (list (concat x ":")))
  5824. org-link-types))
  5825. nil nil nil
  5826. 'tmphist
  5827. (or (car (car org-stored-links)))))
  5828. (set-window-configuration wcf)
  5829. (kill-buffer "*Org Links*"))
  5830. (setq entry (assoc link org-stored-links))
  5831. (or entry (push link org-insert-link-history))
  5832. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5833. (not org-keep-stored-link-after-insertion))
  5834. (setq org-stored-links (delq (assoc link org-stored-links)
  5835. org-stored-links)))
  5836. (setq desc (or desc (nth 1 entry)))))
  5837. (if (string-match org-plain-link-re link)
  5838. ;; URL-like link, normalize the use of angular brackets.
  5839. (setq link (org-make-link (org-remove-angle-brackets link))))
  5840. ;; Check if we are linking to the current file with a search option
  5841. ;; If yes, simplify the link by using only the search option.
  5842. (when (and buffer-file-name
  5843. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5844. (let* ((path (match-string 1 link))
  5845. (case-fold-search nil)
  5846. (search (match-string 2 link)))
  5847. (save-match-data
  5848. (if (equal (file-truename buffer-file-name) (file-truename path))
  5849. ;; We are linking to this same file, with a search option
  5850. (setq link search)))))
  5851. ;; Check if we can/should use a relative path. If yes, simplify the link
  5852. (when (string-match "\\<file:\\(.*\\)" link)
  5853. (let* ((path (match-string 1 link))
  5854. (origpath path)
  5855. (case-fold-search nil))
  5856. (cond
  5857. ((or (eq org-link-file-path-type 'absolute)
  5858. (equal complete-file '(16)))
  5859. (setq path (abbreviate-file-name (expand-file-name path))))
  5860. ((eq org-link-file-path-type 'noabbrev)
  5861. (setq path (expand-file-name path)))
  5862. ((eq org-link-file-path-type 'relative)
  5863. (setq path (file-relative-name path)))
  5864. (t
  5865. (save-match-data
  5866. (if (string-match (concat "^" (regexp-quote
  5867. (file-name-as-directory
  5868. (expand-file-name "."))))
  5869. (expand-file-name path))
  5870. ;; We are linking a file with relative path name.
  5871. (setq path (substring (expand-file-name path)
  5872. (match-end 0)))
  5873. (setq path (abbreviate-file-name (expand-file-name path)))))))
  5874. (setq link (concat "file:" path))
  5875. (if (equal desc origpath)
  5876. (setq desc path))))
  5877. (if org-make-link-description-function
  5878. (setq desc (funcall org-make-link-description-function link desc)))
  5879. (setq desc (read-string "Description: " desc))
  5880. (unless (string-match "\\S-" desc) (setq desc nil))
  5881. (if remove (apply 'delete-region remove))
  5882. (insert (org-make-link-string link desc))))
  5883. (defun org-completing-read (&rest args)
  5884. (let ((minibuffer-local-completion-map
  5885. (copy-keymap minibuffer-local-completion-map)))
  5886. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5887. (apply 'completing-read args)))
  5888. (defun org-extract-attributes (s)
  5889. "Extract the attributes cookie from a string and set as text property."
  5890. (let (a attr (start 0) key value)
  5891. (save-match-data
  5892. (when (string-match "{{\\([^}]+\\)}}$" s)
  5893. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5894. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5895. (setq key (match-string 1 a) value (match-string 2 a)
  5896. start (match-end 0)
  5897. attr (plist-put attr (intern key) value))))
  5898. (org-add-props s nil 'org-attributes attr))
  5899. s))
  5900. (defun org-attributes-to-string (plist)
  5901. "Format a property list into an HTML attribute list."
  5902. (let ((s "") key value)
  5903. (while plist
  5904. (setq key (pop plist) value (pop plist))
  5905. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5906. s))
  5907. ;;; Opening/following a link
  5908. (defvar org-link-search-failed nil)
  5909. (defun org-next-link ()
  5910. "Move forward to the next link.
  5911. If the link is in hidden text, expose it."
  5912. (interactive)
  5913. (when (and org-link-search-failed (eq this-command last-command))
  5914. (goto-char (point-min))
  5915. (message "Link search wrapped back to beginning of buffer"))
  5916. (setq org-link-search-failed nil)
  5917. (let* ((pos (point))
  5918. (ct (org-context))
  5919. (a (assoc :link ct)))
  5920. (if a (goto-char (nth 2 a)))
  5921. (if (re-search-forward org-any-link-re nil t)
  5922. (progn
  5923. (goto-char (match-beginning 0))
  5924. (if (org-invisible-p) (org-show-context)))
  5925. (goto-char pos)
  5926. (setq org-link-search-failed t)
  5927. (error "No further link found"))))
  5928. (defun org-previous-link ()
  5929. "Move backward to the previous link.
  5930. If the link is in hidden text, expose it."
  5931. (interactive)
  5932. (when (and org-link-search-failed (eq this-command last-command))
  5933. (goto-char (point-max))
  5934. (message "Link search wrapped back to end of buffer"))
  5935. (setq org-link-search-failed nil)
  5936. (let* ((pos (point))
  5937. (ct (org-context))
  5938. (a (assoc :link ct)))
  5939. (if a (goto-char (nth 1 a)))
  5940. (if (re-search-backward org-any-link-re nil t)
  5941. (progn
  5942. (goto-char (match-beginning 0))
  5943. (if (org-invisible-p) (org-show-context)))
  5944. (goto-char pos)
  5945. (setq org-link-search-failed t)
  5946. (error "No further link found"))))
  5947. (defun org-find-file-at-mouse (ev)
  5948. "Open file link or URL at mouse."
  5949. (interactive "e")
  5950. (mouse-set-point ev)
  5951. (org-open-at-point 'in-emacs))
  5952. (defun org-open-at-mouse (ev)
  5953. "Open file link or URL at mouse."
  5954. (interactive "e")
  5955. (mouse-set-point ev)
  5956. (org-open-at-point))
  5957. (defvar org-window-config-before-follow-link nil
  5958. "The window configuration before following a link.
  5959. This is saved in case the need arises to restore it.")
  5960. (defvar org-open-link-marker (make-marker)
  5961. "Marker pointing to the location where `org-open-at-point; was called.")
  5962. ;;;###autoload
  5963. (defun org-open-at-point-global ()
  5964. "Follow a link like Org-mode does.
  5965. This command can be called in any mode to follow a link that has
  5966. Org-mode syntax."
  5967. (interactive)
  5968. (org-run-like-in-org-mode 'org-open-at-point))
  5969. ;;;###autoload
  5970. (defun org-open-link-from-string (s &optional arg)
  5971. "Open a link in the string S, as if it was in Org-mode."
  5972. (interactive "sLink: \nP")
  5973. (with-temp-buffer
  5974. (let ((org-inhibit-startup t))
  5975. (org-mode)
  5976. (insert s)
  5977. (goto-char (point-min))
  5978. (org-open-at-point arg))))
  5979. (defun org-open-at-point (&optional in-emacs)
  5980. "Open link at or after point.
  5981. If there is no link at point, this function will search forward up to
  5982. the end of the current subtree.
  5983. Normally, files will be opened by an appropriate application. If the
  5984. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  5985. With a double prefix argument, try to open outside of Emacs, in the
  5986. application the system uses for this file type."
  5987. (interactive "P")
  5988. (org-load-modules-maybe)
  5989. (move-marker org-open-link-marker (point))
  5990. (setq org-window-config-before-follow-link (current-window-configuration))
  5991. (org-remove-occur-highlights nil nil t)
  5992. (if (org-at-timestamp-p t)
  5993. (org-follow-timestamp-link)
  5994. (let (type path link line search (pos (point)))
  5995. (catch 'match
  5996. (save-excursion
  5997. (skip-chars-forward "^]\n\r")
  5998. (when (org-in-regexp org-bracket-link-regexp)
  5999. (setq link (org-extract-attributes
  6000. (org-link-unescape (org-match-string-no-properties 1))))
  6001. (while (string-match " *\n *" link)
  6002. (setq link (replace-match " " t t link)))
  6003. (setq link (org-link-expand-abbrev link))
  6004. (cond
  6005. ((or (file-name-absolute-p link)
  6006. (string-match "^\\.\\.?/" link))
  6007. (setq type "file" path link))
  6008. ((string-match org-link-re-with-space2 link)
  6009. (setq type (match-string 1 link) path (match-string 2 link)))
  6010. (t (setq type "thisfile" path link)))
  6011. (throw 'match t)))
  6012. (when (get-text-property (point) 'org-linked-text)
  6013. (setq type "thisfile"
  6014. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6015. (1+ (point)) (point))
  6016. path (buffer-substring
  6017. (previous-single-property-change pos 'org-linked-text)
  6018. (next-single-property-change pos 'org-linked-text)))
  6019. (throw 'match t))
  6020. (save-excursion
  6021. (when (or (org-in-regexp org-angle-link-re)
  6022. (org-in-regexp org-plain-link-re))
  6023. (setq type (match-string 1) path (match-string 2))
  6024. (throw 'match t)))
  6025. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6026. (setq type "tree-match"
  6027. path (match-string 1))
  6028. (throw 'match t))
  6029. (save-excursion
  6030. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6031. (setq type "tags"
  6032. path (match-string 1))
  6033. (while (string-match ":" path)
  6034. (setq path (replace-match "+" t t path)))
  6035. (throw 'match t))))
  6036. (unless path
  6037. (error "No link found"))
  6038. ;; Remove any trailing spaces in path
  6039. (if (string-match " +\\'" path)
  6040. (setq path (replace-match "" t t path)))
  6041. (cond
  6042. ((assoc type org-link-protocols)
  6043. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6044. ((equal type "mailto")
  6045. (let ((cmd (car org-link-mailto-program))
  6046. (args (cdr org-link-mailto-program)) args1
  6047. (address path) (subject "") a)
  6048. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6049. (setq address (match-string 1 path)
  6050. subject (org-link-escape (match-string 2 path))))
  6051. (while args
  6052. (cond
  6053. ((not (stringp (car args))) (push (pop args) args1))
  6054. (t (setq a (pop args))
  6055. (if (string-match "%a" a)
  6056. (setq a (replace-match address t t a)))
  6057. (if (string-match "%s" a)
  6058. (setq a (replace-match subject t t a)))
  6059. (push a args1))))
  6060. (apply cmd (nreverse args1))))
  6061. ((member type '("http" "https" "ftp" "news"))
  6062. (browse-url (concat type ":" (org-link-escape
  6063. path org-link-escape-chars-browser))))
  6064. ((member type '("message"))
  6065. (browse-url (concat type ":" path)))
  6066. ((string= type "tags")
  6067. (org-tags-view in-emacs path))
  6068. ((string= type "thisfile")
  6069. (if in-emacs
  6070. (switch-to-buffer-other-window
  6071. (org-get-buffer-for-internal-link (current-buffer)))
  6072. (org-mark-ring-push))
  6073. (let ((cmd `(org-link-search
  6074. ,path
  6075. ,(cond ((equal in-emacs '(4)) 'occur)
  6076. ((equal in-emacs '(16)) 'org-occur)
  6077. (t nil))
  6078. ,pos)))
  6079. (condition-case nil (eval cmd)
  6080. (error (progn (widen) (eval cmd))))))
  6081. ((string= type "tree-match")
  6082. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6083. ((string= type "file")
  6084. (if (string-match "::\\([0-9]+\\)\\'" path)
  6085. (setq line (string-to-number (match-string 1 path))
  6086. path (substring path 0 (match-beginning 0)))
  6087. (if (string-match "::\\(.+\\)\\'" path)
  6088. (setq search (match-string 1 path)
  6089. path (substring path 0 (match-beginning 0)))))
  6090. (if (string-match "[*?{]" (file-name-nondirectory path))
  6091. (dired path)
  6092. (org-open-file path in-emacs line search)))
  6093. ((string= type "news")
  6094. (require 'org-gnus)
  6095. (org-gnus-follow-link path))
  6096. ((string= type "shell")
  6097. (let ((cmd path))
  6098. (if (or (not org-confirm-shell-link-function)
  6099. (funcall org-confirm-shell-link-function
  6100. (format "Execute \"%s\" in shell? "
  6101. (org-add-props cmd nil
  6102. 'face 'org-warning))))
  6103. (progn
  6104. (message "Executing %s" cmd)
  6105. (shell-command cmd))
  6106. (error "Abort"))))
  6107. ((string= type "elisp")
  6108. (let ((cmd path))
  6109. (if (or (not org-confirm-elisp-link-function)
  6110. (funcall org-confirm-elisp-link-function
  6111. (format "Execute \"%s\" as elisp? "
  6112. (org-add-props cmd nil
  6113. 'face 'org-warning))))
  6114. (message "%s => %s" cmd (eval (read cmd)))
  6115. (error "Abort"))))
  6116. (t
  6117. (browse-url-at-point)))))
  6118. (move-marker org-open-link-marker nil)
  6119. (run-hook-with-args 'org-follow-link-hook))
  6120. ;;;; Time estimates
  6121. (defun org-get-effort (&optional pom)
  6122. "Get the effort estimate for the current entry."
  6123. (org-entry-get pom org-effort-property))
  6124. ;;; File search
  6125. (defvar org-create-file-search-functions nil
  6126. "List of functions to construct the right search string for a file link.
  6127. These functions are called in turn with point at the location to
  6128. which the link should point.
  6129. A function in the hook should first test if it would like to
  6130. handle this file type, for example by checking the major-mode or
  6131. the file extension. If it decides not to handle this file, it
  6132. should just return nil to give other functions a chance. If it
  6133. does handle the file, it must return the search string to be used
  6134. when following the link. The search string will be part of the
  6135. file link, given after a double colon, and `org-open-at-point'
  6136. will automatically search for it. If special measures must be
  6137. taken to make the search successful, another function should be
  6138. added to the companion hook `org-execute-file-search-functions',
  6139. which see.
  6140. A function in this hook may also use `setq' to set the variable
  6141. `description' to provide a suggestion for the descriptive text to
  6142. be used for this link when it gets inserted into an Org-mode
  6143. buffer with \\[org-insert-link].")
  6144. (defvar org-execute-file-search-functions nil
  6145. "List of functions to execute a file search triggered by a link.
  6146. Functions added to this hook must accept a single argument, the
  6147. search string that was part of the file link, the part after the
  6148. double colon. The function must first check if it would like to
  6149. handle this search, for example by checking the major-mode or the
  6150. file extension. If it decides not to handle this search, it
  6151. should just return nil to give other functions a chance. If it
  6152. does handle the search, it must return a non-nil value to keep
  6153. other functions from trying.
  6154. Each function can access the current prefix argument through the
  6155. variable `current-prefix-argument'. Note that a single prefix is
  6156. used to force opening a link in Emacs, so it may be good to only
  6157. use a numeric or double prefix to guide the search function.
  6158. In case this is needed, a function in this hook can also restore
  6159. the window configuration before `org-open-at-point' was called using:
  6160. (set-window-configuration org-window-config-before-follow-link)")
  6161. (defun org-link-search (s &optional type avoid-pos)
  6162. "Search for a link search option.
  6163. If S is surrounded by forward slashes, it is interpreted as a
  6164. regular expression. In org-mode files, this will create an `org-occur'
  6165. sparse tree. In ordinary files, `occur' will be used to list matches.
  6166. If the current buffer is in `dired-mode', grep will be used to search
  6167. in all files. If AVOID-POS is given, ignore matches near that position."
  6168. (let ((case-fold-search t)
  6169. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6170. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6171. (append '(("") (" ") ("\t") ("\n"))
  6172. org-emphasis-alist)
  6173. "\\|") "\\)"))
  6174. (pos (point))
  6175. (pre nil) (post nil)
  6176. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6177. (cond
  6178. ;; First check if there are any special
  6179. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6180. ;; Now try the builtin stuff
  6181. ((save-excursion
  6182. (goto-char (point-min))
  6183. (and
  6184. (re-search-forward
  6185. (concat "<<" (regexp-quote s0) ">>") nil t)
  6186. (setq type 'dedicated
  6187. pos (match-beginning 0))))
  6188. ;; There is an exact target for this
  6189. (goto-char pos))
  6190. ((string-match "^/\\(.*\\)/$" s)
  6191. ;; A regular expression
  6192. (cond
  6193. ((org-mode-p)
  6194. (org-occur (match-string 1 s)))
  6195. ;;((eq major-mode 'dired-mode)
  6196. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6197. (t (org-do-occur (match-string 1 s)))))
  6198. (t
  6199. ;; A normal search strings
  6200. (when (equal (string-to-char s) ?*)
  6201. ;; Anchor on headlines, post may include tags.
  6202. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6203. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6204. s (substring s 1)))
  6205. (remove-text-properties
  6206. 0 (length s)
  6207. '(face nil mouse-face nil keymap nil fontified nil) s)
  6208. ;; Make a series of regular expressions to find a match
  6209. (setq words (org-split-string s "[ \n\r\t]+")
  6210. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6211. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6212. "\\)" markers)
  6213. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6214. re2a (concat "[ \t\r\n]" re2a_)
  6215. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6216. re4 (concat "[^a-zA-Z_]" re4_)
  6217. re1 (concat pre re2 post)
  6218. re3 (concat pre (if pre re4_ re4) post)
  6219. re5 (concat pre ".*" re4)
  6220. re2 (concat pre re2)
  6221. re2a (concat pre (if pre re2a_ re2a))
  6222. re4 (concat pre (if pre re4_ re4))
  6223. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6224. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6225. re5 "\\)"
  6226. ))
  6227. (cond
  6228. ((eq type 'org-occur) (org-occur reall))
  6229. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6230. (t (goto-char (point-min))
  6231. (setq type 'fuzzy)
  6232. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6233. (org-search-not-self 1 re1 nil t)
  6234. (org-search-not-self 1 re2 nil t)
  6235. (org-search-not-self 1 re2a nil t)
  6236. (org-search-not-self 1 re3 nil t)
  6237. (org-search-not-self 1 re4 nil t)
  6238. (org-search-not-self 1 re5 nil t)
  6239. )
  6240. (goto-char (match-beginning 1))
  6241. (goto-char pos)
  6242. (error "No match")))))
  6243. (t
  6244. ;; Normal string-search
  6245. (goto-char (point-min))
  6246. (if (search-forward s nil t)
  6247. (goto-char (match-beginning 0))
  6248. (error "No match"))))
  6249. (and (org-mode-p) (org-show-context 'link-search))
  6250. type))
  6251. (defun org-search-not-self (group &rest args)
  6252. "Execute `re-search-forward', but only accept matches that do not
  6253. enclose the position of `org-open-link-marker'."
  6254. (let ((m org-open-link-marker))
  6255. (catch 'exit
  6256. (while (apply 're-search-forward args)
  6257. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6258. (goto-char (match-end group))
  6259. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6260. (> (match-beginning 0) (marker-position m))
  6261. (< (match-end 0) (marker-position m)))
  6262. (save-match-data
  6263. (or (not (org-in-regexp
  6264. org-bracket-link-analytic-regexp 1))
  6265. (not (match-end 4)) ; no description
  6266. (and (<= (match-beginning 4) (point))
  6267. (>= (match-end 4) (point))))))
  6268. (throw 'exit (point))))))))
  6269. (defun org-get-buffer-for-internal-link (buffer)
  6270. "Return a buffer to be used for displaying the link target of internal links."
  6271. (cond
  6272. ((not org-display-internal-link-with-indirect-buffer)
  6273. buffer)
  6274. ((string-match "(Clone)$" (buffer-name buffer))
  6275. (message "Buffer is already a clone, not making another one")
  6276. ;; we also do not modify visibility in this case
  6277. buffer)
  6278. (t ; make a new indirect buffer for displaying the link
  6279. (let* ((bn (buffer-name buffer))
  6280. (ibn (concat bn "(Clone)"))
  6281. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6282. (with-current-buffer ib (org-overview))
  6283. ib))))
  6284. (defun org-do-occur (regexp &optional cleanup)
  6285. "Call the Emacs command `occur'.
  6286. If CLEANUP is non-nil, remove the printout of the regular expression
  6287. in the *Occur* buffer. This is useful if the regex is long and not useful
  6288. to read."
  6289. (occur regexp)
  6290. (when cleanup
  6291. (let ((cwin (selected-window)) win beg end)
  6292. (when (setq win (get-buffer-window "*Occur*"))
  6293. (select-window win))
  6294. (goto-char (point-min))
  6295. (when (re-search-forward "match[a-z]+" nil t)
  6296. (setq beg (match-end 0))
  6297. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6298. (setq end (1- (match-beginning 0)))))
  6299. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6300. (goto-char (point-min))
  6301. (select-window cwin))))
  6302. ;;; The mark ring for links jumps
  6303. (defvar org-mark-ring nil
  6304. "Mark ring for positions before jumps in Org-mode.")
  6305. (defvar org-mark-ring-last-goto nil
  6306. "Last position in the mark ring used to go back.")
  6307. ;; Fill and close the ring
  6308. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6309. (loop for i from 1 to org-mark-ring-length do
  6310. (push (make-marker) org-mark-ring))
  6311. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6312. org-mark-ring)
  6313. (defun org-mark-ring-push (&optional pos buffer)
  6314. "Put the current position or POS into the mark ring and rotate it."
  6315. (interactive)
  6316. (setq pos (or pos (point)))
  6317. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6318. (move-marker (car org-mark-ring)
  6319. (or pos (point))
  6320. (or buffer (current-buffer)))
  6321. (message "%s"
  6322. (substitute-command-keys
  6323. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6324. (defun org-mark-ring-goto (&optional n)
  6325. "Jump to the previous position in the mark ring.
  6326. With prefix arg N, jump back that many stored positions. When
  6327. called several times in succession, walk through the entire ring.
  6328. Org-mode commands jumping to a different position in the current file,
  6329. or to another Org-mode file, automatically push the old position
  6330. onto the ring."
  6331. (interactive "p")
  6332. (let (p m)
  6333. (if (eq last-command this-command)
  6334. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6335. (setq p org-mark-ring))
  6336. (setq org-mark-ring-last-goto p)
  6337. (setq m (car p))
  6338. (switch-to-buffer (marker-buffer m))
  6339. (goto-char m)
  6340. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6341. (defun org-remove-angle-brackets (s)
  6342. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6343. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6344. s)
  6345. (defun org-add-angle-brackets (s)
  6346. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6347. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6348. s)
  6349. (defun org-remove-double-quotes (s)
  6350. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6351. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6352. s)
  6353. ;;; Following specific links
  6354. (defun org-follow-timestamp-link ()
  6355. (cond
  6356. ((org-at-date-range-p t)
  6357. (let ((org-agenda-start-on-weekday)
  6358. (t1 (match-string 1))
  6359. (t2 (match-string 2)))
  6360. (setq t1 (time-to-days (org-time-string-to-time t1))
  6361. t2 (time-to-days (org-time-string-to-time t2)))
  6362. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6363. ((org-at-timestamp-p t)
  6364. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6365. (substring (match-string 1) 0 10)))
  6366. 1))
  6367. (t (error "This should not happen"))))
  6368. ;;; Following file links
  6369. (defvar org-wait nil)
  6370. (defun org-open-file (path &optional in-emacs line search)
  6371. "Open the file at PATH.
  6372. First, this expands any special file name abbreviations. Then the
  6373. configuration variable `org-file-apps' is checked if it contains an
  6374. entry for this file type, and if yes, the corresponding command is launched.
  6375. If no application is found, Emacs simply visits the file.
  6376. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6377. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6378. and o use an external application to visit the file.
  6379. Optional LINE specifies a line to go to, optional SEARCH a string to
  6380. search for. If LINE or SEARCH is given, the file will always be
  6381. opened in Emacs.
  6382. If the file does not exist, an error is thrown."
  6383. (setq in-emacs (or in-emacs line search))
  6384. (let* ((file (if (equal path "")
  6385. buffer-file-name
  6386. (substitute-in-file-name (expand-file-name path))))
  6387. (apps (append org-file-apps (org-default-apps)))
  6388. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6389. (dirp (if remp nil (file-directory-p file)))
  6390. (file (if (and dirp org-open-directory-means-index-dot-org)
  6391. (concat (file-name-as-directory file) "index.org")
  6392. file))
  6393. (a-m-a-p (assq 'auto-mode apps))
  6394. (dfile (downcase file))
  6395. (old-buffer (current-buffer))
  6396. (old-pos (point))
  6397. (old-mode major-mode)
  6398. ext cmd)
  6399. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6400. (setq ext (match-string 1 dfile))
  6401. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6402. (setq ext (match-string 1 dfile))))
  6403. (cond
  6404. ((equal in-emacs '(16))
  6405. (setq cmd (cdr (assoc 'system apps))))
  6406. (in-emacs (setq cmd 'emacs))
  6407. (t
  6408. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6409. (and dirp (cdr (assoc 'directory apps)))
  6410. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6411. 'string-match)
  6412. (cdr (assoc ext apps))
  6413. (cdr (assoc t apps))))))
  6414. (when (eq cmd 'system)
  6415. (setq cmd (cdr (assoc 'system apps))))
  6416. (when (eq cmd 'default)
  6417. (setq cmd (cdr (assoc t apps))))
  6418. (when (eq cmd 'mailcap)
  6419. (require 'mailcap)
  6420. (mailcap-parse-mailcaps)
  6421. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6422. (command (mailcap-mime-info mime-type)))
  6423. (if (stringp command)
  6424. (setq cmd command)
  6425. (setq cmd 'emacs))))
  6426. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6427. (not (file-exists-p file))
  6428. (not org-open-non-existing-files))
  6429. (error "No such file: %s" file))
  6430. (cond
  6431. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6432. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6433. (while (string-match "['\"]%s['\"]" cmd)
  6434. (setq cmd (replace-match "%s" t t cmd)))
  6435. (while (string-match "%s" cmd)
  6436. (setq cmd (replace-match
  6437. (save-match-data
  6438. (shell-quote-argument
  6439. (convert-standard-filename file)))
  6440. t t cmd)))
  6441. (save-window-excursion
  6442. (start-process-shell-command cmd nil cmd)
  6443. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6444. ))
  6445. ((or (stringp cmd)
  6446. (eq cmd 'emacs))
  6447. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6448. (widen)
  6449. (if line (goto-line line)
  6450. (if search (org-link-search search))))
  6451. ((consp cmd)
  6452. (let ((file (convert-standard-filename file)))
  6453. (eval cmd)))
  6454. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6455. (and (org-mode-p) (eq old-mode 'org-mode)
  6456. (or (not (equal old-buffer (current-buffer)))
  6457. (not (equal old-pos (point))))
  6458. (org-mark-ring-push old-pos old-buffer))))
  6459. (defun org-default-apps ()
  6460. "Return the default applications for this operating system."
  6461. (cond
  6462. ((eq system-type 'darwin)
  6463. org-file-apps-defaults-macosx)
  6464. ((eq system-type 'windows-nt)
  6465. org-file-apps-defaults-windowsnt)
  6466. (t org-file-apps-defaults-gnu)))
  6467. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6468. "Convert extensions to regular expressions in the cars of LIST.
  6469. Also, weed out any non-string entries, because the return value is used
  6470. only for regexp matching.
  6471. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6472. point to the symbol `emacs', indicating that the file should
  6473. be opened in Emacs."
  6474. (append
  6475. (delq nil
  6476. (mapcar (lambda (x)
  6477. (if (not (stringp (car x)))
  6478. nil
  6479. (if (string-match "\\W" (car x))
  6480. x
  6481. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6482. list))
  6483. (if add-auto-mode
  6484. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6485. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6486. (defun org-file-remote-p (file)
  6487. "Test whether FILE specifies a location on a remote system.
  6488. Return non-nil if the location is indeed remote.
  6489. For example, the filename \"/user@host:/foo\" specifies a location
  6490. on the system \"/user@host:\"."
  6491. (cond ((fboundp 'file-remote-p)
  6492. (file-remote-p file))
  6493. ((fboundp 'tramp-handle-file-remote-p)
  6494. (tramp-handle-file-remote-p file))
  6495. ((and (boundp 'ange-ftp-name-format)
  6496. (string-match (car ange-ftp-name-format) file))
  6497. t)
  6498. (t nil)))
  6499. ;;;; Refiling
  6500. (defun org-get-org-file ()
  6501. "Read a filename, with default directory `org-directory'."
  6502. (let ((default (or org-default-notes-file remember-data-file)))
  6503. (read-file-name (format "File name [%s]: " default)
  6504. (file-name-as-directory org-directory)
  6505. default)))
  6506. (defun org-notes-order-reversed-p ()
  6507. "Check if the current file should receive notes in reversed order."
  6508. (cond
  6509. ((not org-reverse-note-order) nil)
  6510. ((eq t org-reverse-note-order) t)
  6511. ((not (listp org-reverse-note-order)) nil)
  6512. (t (catch 'exit
  6513. (let ((all org-reverse-note-order)
  6514. entry)
  6515. (while (setq entry (pop all))
  6516. (if (string-match (car entry) buffer-file-name)
  6517. (throw 'exit (cdr entry))))
  6518. nil)))))
  6519. (defvar org-refile-target-table nil
  6520. "The list of refile targets, created by `org-refile'.")
  6521. (defvar org-agenda-new-buffers nil
  6522. "Buffers created to visit agenda files.")
  6523. (defun org-get-refile-targets (&optional default-buffer)
  6524. "Produce a table with refile targets."
  6525. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6526. targets txt re files f desc descre)
  6527. (with-current-buffer (or default-buffer (current-buffer))
  6528. (while (setq entry (pop entries))
  6529. (setq files (car entry) desc (cdr entry))
  6530. (cond
  6531. ((null files) (setq files (list (current-buffer))))
  6532. ((eq files 'org-agenda-files)
  6533. (setq files (org-agenda-files 'unrestricted)))
  6534. ((and (symbolp files) (fboundp files))
  6535. (setq files (funcall files)))
  6536. ((and (symbolp files) (boundp files))
  6537. (setq files (symbol-value files))))
  6538. (if (stringp files) (setq files (list files)))
  6539. (cond
  6540. ((eq (car desc) :tag)
  6541. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6542. ((eq (car desc) :todo)
  6543. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6544. ((eq (car desc) :regexp)
  6545. (setq descre (cdr desc)))
  6546. ((eq (car desc) :level)
  6547. (setq descre (concat "^\\*\\{" (number-to-string
  6548. (if org-odd-levels-only
  6549. (1- (* 2 (cdr desc)))
  6550. (cdr desc)))
  6551. "\\}[ \t]")))
  6552. ((eq (car desc) :maxlevel)
  6553. (setq descre (concat "^\\*\\{1," (number-to-string
  6554. (if org-odd-levels-only
  6555. (1- (* 2 (cdr desc)))
  6556. (cdr desc)))
  6557. "\\}[ \t]")))
  6558. (t (error "Bad refiling target description %s" desc)))
  6559. (while (setq f (pop files))
  6560. (save-excursion
  6561. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6562. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6563. (save-excursion
  6564. (save-restriction
  6565. (widen)
  6566. (goto-char (point-min))
  6567. (while (re-search-forward descre nil t)
  6568. (goto-char (point-at-bol))
  6569. (when (looking-at org-complex-heading-regexp)
  6570. (setq txt (org-link-display-format (match-string 4))
  6571. re (concat "^" (regexp-quote
  6572. (buffer-substring (match-beginning 1)
  6573. (match-end 4)))))
  6574. (if (match-end 5) (setq re (concat re "[ \t]+"
  6575. (regexp-quote
  6576. (match-string 5)))))
  6577. (setq re (concat re "[ \t]*$"))
  6578. (when org-refile-use-outline-path
  6579. (setq txt (mapconcat 'org-protect-slash
  6580. (append
  6581. (if (eq org-refile-use-outline-path 'file)
  6582. (list (file-name-nondirectory
  6583. (buffer-file-name (buffer-base-buffer))))
  6584. (if (eq org-refile-use-outline-path 'full-file-path)
  6585. (list (buffer-file-name (buffer-base-buffer)))))
  6586. (org-get-outline-path)
  6587. (list txt))
  6588. "/")))
  6589. (push (list txt f re (point)) targets))
  6590. (goto-char (point-at-eol))))))))
  6591. (nreverse targets))))
  6592. (defun org-protect-slash (s)
  6593. (while (string-match "/" s)
  6594. (setq s (replace-match "\\" t t s)))
  6595. s)
  6596. (defun org-get-outline-path ()
  6597. "Return the outline path to the current entry, as a list."
  6598. (let (rtn)
  6599. (save-excursion
  6600. (while (org-up-heading-safe)
  6601. (when (looking-at org-complex-heading-regexp)
  6602. (push (org-match-string-no-properties 4) rtn)))
  6603. rtn)))
  6604. (defvar org-refile-history nil
  6605. "History for refiling operations.")
  6606. (defun org-refile (&optional goto default-buffer)
  6607. "Move the entry at point to another heading.
  6608. The list of target headings is compiled using the information in
  6609. `org-refile-targets', which see. This list is created before each use
  6610. and will therefore always be up-to-date.
  6611. At the target location, the entry is filed as a subitem of the target heading.
  6612. Depending on `org-reverse-note-order', the new subitem will either be the
  6613. first or the last subitem.
  6614. With prefix arg GOTO, the command will only visit the target location,
  6615. not actually move anything.
  6616. With a double prefix `C-u C-u', go to the location where the last refiling
  6617. operation has put the subtree."
  6618. (interactive "P")
  6619. (let* ((cbuf (current-buffer))
  6620. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6621. pos it nbuf file re level reversed)
  6622. (if (equal goto '(16))
  6623. (org-refile-goto-last-stored)
  6624. (when (setq it (org-refile-get-location
  6625. (if goto "Goto: " "Refile to: ") default-buffer))
  6626. (setq file (nth 1 it)
  6627. re (nth 2 it)
  6628. pos (nth 3 it))
  6629. (setq nbuf (or (find-buffer-visiting file)
  6630. (find-file-noselect file)))
  6631. (if goto
  6632. (progn
  6633. (switch-to-buffer nbuf)
  6634. (goto-char pos)
  6635. (org-show-context 'org-goto))
  6636. (org-copy-subtree 1 nil t)
  6637. (save-excursion
  6638. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6639. (find-file-noselect file))))
  6640. (setq reversed (org-notes-order-reversed-p))
  6641. (save-excursion
  6642. (save-restriction
  6643. (widen)
  6644. (goto-char pos)
  6645. (looking-at outline-regexp)
  6646. (setq level (org-get-valid-level (funcall outline-level) 1))
  6647. (goto-char
  6648. (if reversed
  6649. (or (outline-next-heading) (point-max))
  6650. (or (save-excursion (outline-get-next-sibling))
  6651. (org-end-of-subtree t t)
  6652. (point-max))))
  6653. (if (not (bolp)) (newline))
  6654. (bookmark-set "org-refile-last-stored")
  6655. (org-paste-subtree level))))
  6656. (org-cut-subtree)
  6657. (setq org-markers-to-move nil)
  6658. (message "Entry refiled to \"%s\"" (car it)))))))
  6659. (defun org-refile-goto-last-stored ()
  6660. "Go to the location where the last refile was stored."
  6661. (interactive)
  6662. (bookmark-jump "org-refile-last-stored")
  6663. (message "This is the location of the last refile"))
  6664. (defun org-refile-get-location (&optional prompt default-buffer)
  6665. "Prompt the user for a refile location, using PROMPT."
  6666. (let ((org-refile-targets org-refile-targets)
  6667. (org-refile-use-outline-path org-refile-use-outline-path))
  6668. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6669. (unless org-refile-target-table
  6670. (error "No refile targets"))
  6671. (let* ((cbuf (current-buffer))
  6672. (cfunc (if org-refile-use-outline-path
  6673. 'org-olpath-completing-read
  6674. 'completing-read))
  6675. (extra (if org-refile-use-outline-path "/" ""))
  6676. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6677. (fname (and filename (file-truename filename)))
  6678. (tbl (mapcar
  6679. (lambda (x)
  6680. (if (not (equal fname (file-truename (nth 1 x))))
  6681. (cons (concat (car x) extra " ("
  6682. (file-name-nondirectory (nth 1 x)) ")")
  6683. (cdr x))
  6684. (cons (concat (car x) extra) (cdr x))))
  6685. org-refile-target-table))
  6686. (completion-ignore-case t))
  6687. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6688. tbl)))
  6689. (defun org-olpath-completing-read (prompt collection &rest args)
  6690. "Read an outline path like a file name."
  6691. (let ((thetable collection))
  6692. (apply
  6693. 'completing-read prompt
  6694. (lambda (string predicate &optional flag)
  6695. (let (rtn r s f (l (length string)))
  6696. (cond
  6697. ((eq flag nil)
  6698. ;; try completion
  6699. (try-completion string thetable))
  6700. ((eq flag t)
  6701. ;; all-completions
  6702. (setq rtn (all-completions string thetable predicate))
  6703. (mapcar
  6704. (lambda (x)
  6705. (setq r (substring x l))
  6706. (if (string-match " ([^)]*)$" x)
  6707. (setq f (match-string 0 x))
  6708. (setq f ""))
  6709. (if (string-match "/" r)
  6710. (concat string (substring r 0 (match-end 0)) f)
  6711. x))
  6712. rtn))
  6713. ((eq flag 'lambda)
  6714. ;; exact match?
  6715. (assoc string thetable)))
  6716. ))
  6717. args)))
  6718. ;;;; Dynamic blocks
  6719. (defun org-find-dblock (name)
  6720. "Find the first dynamic block with name NAME in the buffer.
  6721. If not found, stay at current position and return nil."
  6722. (let (pos)
  6723. (save-excursion
  6724. (goto-char (point-min))
  6725. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6726. nil t)
  6727. (match-beginning 0))))
  6728. (if pos (goto-char pos))
  6729. pos))
  6730. (defconst org-dblock-start-re
  6731. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6732. "Matches the startline of a dynamic block, with parameters.")
  6733. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6734. "Matches the end of a dyhamic block.")
  6735. (defun org-create-dblock (plist)
  6736. "Create a dynamic block section, with parameters taken from PLIST.
  6737. PLIST must containe a :name entry which is used as name of the block."
  6738. (unless (bolp) (newline))
  6739. (let ((name (plist-get plist :name)))
  6740. (insert "#+BEGIN: " name)
  6741. (while plist
  6742. (if (eq (car plist) :name)
  6743. (setq plist (cddr plist))
  6744. (insert " " (prin1-to-string (pop plist)))))
  6745. (insert "\n\n#+END:\n")
  6746. (beginning-of-line -2)))
  6747. (defun org-prepare-dblock ()
  6748. "Prepare dynamic block for refresh.
  6749. This empties the block, puts the cursor at the insert position and returns
  6750. the property list including an extra property :name with the block name."
  6751. (unless (looking-at org-dblock-start-re)
  6752. (error "Not at a dynamic block"))
  6753. (let* ((begdel (1+ (match-end 0)))
  6754. (name (org-no-properties (match-string 1)))
  6755. (params (append (list :name name)
  6756. (read (concat "(" (match-string 3) ")")))))
  6757. (unless (re-search-forward org-dblock-end-re nil t)
  6758. (error "Dynamic block not terminated"))
  6759. (setq params
  6760. (append params
  6761. (list :content (buffer-substring
  6762. begdel (match-beginning 0)))))
  6763. (delete-region begdel (match-beginning 0))
  6764. (goto-char begdel)
  6765. (open-line 1)
  6766. params))
  6767. (defun org-map-dblocks (&optional command)
  6768. "Apply COMMAND to all dynamic blocks in the current buffer.
  6769. If COMMAND is not given, use `org-update-dblock'."
  6770. (let ((cmd (or command 'org-update-dblock))
  6771. pos)
  6772. (save-excursion
  6773. (goto-char (point-min))
  6774. (while (re-search-forward org-dblock-start-re nil t)
  6775. (goto-char (setq pos (match-beginning 0)))
  6776. (condition-case nil
  6777. (funcall cmd)
  6778. (error (message "Error during update of dynamic block")))
  6779. (goto-char pos)
  6780. (unless (re-search-forward org-dblock-end-re nil t)
  6781. (error "Dynamic block not terminated"))))))
  6782. (defun org-dblock-update (&optional arg)
  6783. "User command for updating dynamic blocks.
  6784. Update the dynamic block at point. With prefix ARG, update all dynamic
  6785. blocks in the buffer."
  6786. (interactive "P")
  6787. (if arg
  6788. (org-update-all-dblocks)
  6789. (or (looking-at org-dblock-start-re)
  6790. (org-beginning-of-dblock))
  6791. (org-update-dblock)))
  6792. (defun org-update-dblock ()
  6793. "Update the dynamic block at point
  6794. This means to empty the block, parse for parameters and then call
  6795. the correct writing function."
  6796. (save-window-excursion
  6797. (let* ((pos (point))
  6798. (line (org-current-line))
  6799. (params (org-prepare-dblock))
  6800. (name (plist-get params :name))
  6801. (cmd (intern (concat "org-dblock-write:" name))))
  6802. (message "Updating dynamic block `%s' at line %d..." name line)
  6803. (funcall cmd params)
  6804. (message "Updating dynamic block `%s' at line %d...done" name line)
  6805. (goto-char pos))))
  6806. (defun org-beginning-of-dblock ()
  6807. "Find the beginning of the dynamic block at point.
  6808. Error if there is no scuh block at point."
  6809. (let ((pos (point))
  6810. beg)
  6811. (end-of-line 1)
  6812. (if (and (re-search-backward org-dblock-start-re nil t)
  6813. (setq beg (match-beginning 0))
  6814. (re-search-forward org-dblock-end-re nil t)
  6815. (> (match-end 0) pos))
  6816. (goto-char beg)
  6817. (goto-char pos)
  6818. (error "Not in a dynamic block"))))
  6819. (defun org-update-all-dblocks ()
  6820. "Update all dynamic blocks in the buffer.
  6821. This function can be used in a hook."
  6822. (when (org-mode-p)
  6823. (org-map-dblocks 'org-update-dblock)))
  6824. ;;;; Completion
  6825. (defconst org-additional-option-like-keywords
  6826. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6827. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6828. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6829. "BEGIN_QUOTE" "END_QUOTE"
  6830. "BEGIN_VERSE" "END_VERSE"
  6831. "BEGIN_SRC" "END_SRC"))
  6832. (defcustom org-structure-template-alist
  6833. '(
  6834. ("s" "#+begin_src ?\n\n#+end_src"
  6835. "<src lang=\"?\">\n\n</src>")
  6836. ("e" "#+begin_example\n?\n#+end_example"
  6837. "<example>\n?\n</example>")
  6838. ("q" "#+begin_quote\n?\n#+end_quote"
  6839. "<quote>\n?\n</quote>")
  6840. ("v" "#+begin_verse\n?\n#+end_verse"
  6841. "<verse>\n?\n/verse>")
  6842. ("l" "#+begin_latex\n?\n#+end_latex"
  6843. "<literal style=\"latex\">\n?\n</literal>")
  6844. ("L" "#+latex: "
  6845. "<literal style=\"latex\">?</literal>")
  6846. ("h" "#+begin_html\n?\n#+end_html"
  6847. "<literal style=\"html\">\n?\n</literal>")
  6848. ("H" "#+html: "
  6849. "<literal style=\"html\">?</literal>")
  6850. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6851. ("A" "#+ascii: ")
  6852. ("i" "#+include %file ?"
  6853. "<include file=%file markup=\"?\">")
  6854. )
  6855. "Structure completion elements.
  6856. This is a list of abbreviation keys and values. The value gets inserted
  6857. it you type @samp{.} followed by the key and then the completion key,
  6858. usually `M-TAB'. %file will be replaced by a file name after prompting
  6859. for the file uning completion.
  6860. There are two templates for each key, the first uses the original Org syntax,
  6861. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6862. the default when the /org-mtags.el/ module has been loaded. See also the
  6863. variable `org-mtags-prefere-muse-templates'.
  6864. This is an experimental feature, it is undecided if it is going to stay in."
  6865. :group 'org-completion
  6866. :type '(repeat
  6867. (string :tag "Key")
  6868. (string :tag "Template")
  6869. (string :tag "Muse Template")))
  6870. (defun org-try-structure-completion ()
  6871. "Try to complete a structure template before point.
  6872. This looks for strings like \"<e\" on an otherwise empty line and
  6873. expands them."
  6874. (let ((l (buffer-substring (point-at-bol) (point)))
  6875. a)
  6876. (when (and (looking-at "[ \t]*$")
  6877. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  6878. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  6879. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  6880. (match-beginning 1)) a)
  6881. t)))
  6882. (defun org-complete-expand-structure-template (start cell)
  6883. "Expand a structure template."
  6884. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  6885. (rpl (nth (if musep 2 1) cell)))
  6886. (delete-region start (point))
  6887. (when (string-match "\\`#\\+" rpl)
  6888. (cond
  6889. ((bolp))
  6890. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6891. (delete-region (point-at-bol) (point)))
  6892. (t (newline))))
  6893. (setq start (point))
  6894. (if (string-match "%file" rpl)
  6895. (setq rpl (replace-match
  6896. (concat
  6897. "\""
  6898. (save-match-data
  6899. (abbreviate-file-name (read-file-name "Include file: ")))
  6900. "\"")
  6901. t t rpl)))
  6902. (insert rpl)
  6903. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6904. (defun org-complete (&optional arg)
  6905. "Perform completion on word at point.
  6906. At the beginning of a headline, this completes TODO keywords as given in
  6907. `org-todo-keywords'.
  6908. If the current word is preceded by a backslash, completes the TeX symbols
  6909. that are supported for HTML support.
  6910. If the current word is preceded by \"#+\", completes special words for
  6911. setting file options.
  6912. In the line after \"#+STARTUP:, complete valid keywords.\"
  6913. At all other locations, this simply calls the value of
  6914. `org-completion-fallback-command'."
  6915. (interactive "P")
  6916. (org-without-partial-completion
  6917. (catch 'exit
  6918. (let* ((a nil)
  6919. (end (point))
  6920. (beg1 (save-excursion
  6921. (skip-chars-backward (org-re "[:alnum:]_@"))
  6922. (point)))
  6923. (beg (save-excursion
  6924. (skip-chars-backward "a-zA-Z0-9_:$")
  6925. (point)))
  6926. (confirm (lambda (x) (stringp (car x))))
  6927. (searchhead (equal (char-before beg) ?*))
  6928. (struct
  6929. (when (and (member (char-before beg1) '(?. ?<))
  6930. (setq a (assoc (buffer-substring beg1 (point))
  6931. org-structure-template-alist)))
  6932. (org-complete-expand-structure-template (1- beg1) a)
  6933. (throw 'exit t)))
  6934. (tag (and (equal (char-before beg1) ?:)
  6935. (equal (char-after (point-at-bol)) ?*)))
  6936. (prop (and (equal (char-before beg1) ?:)
  6937. (not (equal (char-after (point-at-bol)) ?*))))
  6938. (texp (equal (char-before beg) ?\\))
  6939. (link (equal (char-before beg) ?\[))
  6940. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6941. beg)
  6942. "#+"))
  6943. (startup (string-match "^#\\+STARTUP:.*"
  6944. (buffer-substring (point-at-bol) (point))))
  6945. (completion-ignore-case opt)
  6946. (type nil)
  6947. (tbl nil)
  6948. (table (cond
  6949. (opt
  6950. (setq type :opt)
  6951. (require 'org-exp)
  6952. (append
  6953. (mapcar
  6954. (lambda (x)
  6955. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6956. (cons (match-string 2 x) (match-string 1 x)))
  6957. (org-split-string (org-get-current-options) "\n"))
  6958. (mapcar 'list org-additional-option-like-keywords)))
  6959. (startup
  6960. (setq type :startup)
  6961. org-startup-options)
  6962. (link (append org-link-abbrev-alist-local
  6963. org-link-abbrev-alist))
  6964. (texp
  6965. (setq type :tex)
  6966. org-html-entities)
  6967. ((string-match "\\`\\*+[ \t]+\\'"
  6968. (buffer-substring (point-at-bol) beg))
  6969. (setq type :todo)
  6970. (mapcar 'list org-todo-keywords-1))
  6971. (searchhead
  6972. (setq type :searchhead)
  6973. (save-excursion
  6974. (goto-char (point-min))
  6975. (while (re-search-forward org-todo-line-regexp nil t)
  6976. (push (list
  6977. (org-make-org-heading-search-string
  6978. (match-string 3) t))
  6979. tbl)))
  6980. tbl)
  6981. (tag (setq type :tag beg beg1)
  6982. (or org-tag-alist (org-get-buffer-tags)))
  6983. (prop (setq type :prop beg beg1)
  6984. (mapcar 'list (org-buffer-property-keys nil t t)))
  6985. (t (progn
  6986. (call-interactively org-completion-fallback-command)
  6987. (throw 'exit nil)))))
  6988. (pattern (buffer-substring-no-properties beg end))
  6989. (completion (try-completion pattern table confirm)))
  6990. (cond ((eq completion t)
  6991. (if (not (assoc (upcase pattern) table))
  6992. (message "Already complete")
  6993. (if (and (equal type :opt)
  6994. (not (member (car (assoc (upcase pattern) table))
  6995. org-additional-option-like-keywords)))
  6996. (insert (substring (cdr (assoc (upcase pattern) table))
  6997. (length pattern)))
  6998. (if (memq type '(:tag :prop)) (insert ":")))))
  6999. ((null completion)
  7000. (message "Can't find completion for \"%s\"" pattern)
  7001. (ding))
  7002. ((not (string= pattern completion))
  7003. (delete-region beg end)
  7004. (if (string-match " +$" completion)
  7005. (setq completion (replace-match "" t t completion)))
  7006. (insert completion)
  7007. (if (get-buffer-window "*Completions*")
  7008. (delete-window (get-buffer-window "*Completions*")))
  7009. (if (assoc completion table)
  7010. (if (eq type :todo) (insert " ")
  7011. (if (memq type '(:tag :prop)) (insert ":"))))
  7012. (if (and (equal type :opt) (assoc completion table))
  7013. (message "%s" (substitute-command-keys
  7014. "Press \\[org-complete] again to insert example settings"))))
  7015. (t
  7016. (message "Making completion list...")
  7017. (let ((list (sort (all-completions pattern table confirm)
  7018. 'string<)))
  7019. (with-output-to-temp-buffer "*Completions*"
  7020. (condition-case nil
  7021. ;; Protection needed for XEmacs and emacs 21
  7022. (display-completion-list list pattern)
  7023. (error (display-completion-list list)))))
  7024. (message "Making completion list...%s" "done")))))))
  7025. ;;;; TODO, DEADLINE, Comments
  7026. (defun org-toggle-comment ()
  7027. "Change the COMMENT state of an entry."
  7028. (interactive)
  7029. (save-excursion
  7030. (org-back-to-heading)
  7031. (let (case-fold-search)
  7032. (if (looking-at (concat outline-regexp
  7033. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7034. (replace-match "" t t nil 1)
  7035. (if (looking-at outline-regexp)
  7036. (progn
  7037. (goto-char (match-end 0))
  7038. (insert org-comment-string " ")))))))
  7039. (defvar org-last-todo-state-is-todo nil
  7040. "This is non-nil when the last TODO state change led to a TODO state.
  7041. If the last change removed the TODO tag or switched to DONE, then
  7042. this is nil.")
  7043. (defvar org-setting-tags nil) ; dynamically skiped
  7044. (defun org-parse-local-options (string var)
  7045. "Parse STRING for startup setting relevant for variable VAR."
  7046. (let ((rtn (symbol-value var))
  7047. e opts)
  7048. (save-match-data
  7049. (if (or (not string) (not (string-match "\\S-" string)))
  7050. rtn
  7051. (setq opts (delq nil (mapcar (lambda (x)
  7052. (setq e (assoc x org-startup-options))
  7053. (if (eq (nth 1 e) var) e nil))
  7054. (org-split-string string "[ \t]+"))))
  7055. (if (not opts)
  7056. rtn
  7057. (setq rtn nil)
  7058. (while (setq e (pop opts))
  7059. (if (not (nth 3 e))
  7060. (setq rtn (nth 2 e))
  7061. (if (not (listp rtn)) (setq rtn nil))
  7062. (push (nth 2 e) rtn)))
  7063. rtn)))))
  7064. (defvar org-blocker-hook nil
  7065. "Hook for functions that are allowed to block a state change.
  7066. Each function gets as its single argument a property list, see
  7067. `org-trigger-hook' for more information about this list.
  7068. If any of the functions in this hook returns nil, the state change
  7069. is blocked.")
  7070. (defvar org-trigger-hook nil
  7071. "Hook for functions that are triggered by a state change.
  7072. Each function gets as its single argument a property list with at least
  7073. the following elements:
  7074. (:type type-of-change :position pos-at-entry-start
  7075. :from old-state :to new-state)
  7076. Depending on the type, more properties may be present.
  7077. This mechanism is currently implemented for:
  7078. TODO state changes
  7079. ------------------
  7080. :type todo-state-change
  7081. :from previous state (keyword as a string), or nil
  7082. :to new state (keyword as a string), or nil")
  7083. (defvar org-agenda-headline-snapshot-before-repeat)
  7084. (defun org-todo (&optional arg)
  7085. "Change the TODO state of an item.
  7086. The state of an item is given by a keyword at the start of the heading,
  7087. like
  7088. *** TODO Write paper
  7089. *** DONE Call mom
  7090. The different keywords are specified in the variable `org-todo-keywords'.
  7091. By default the available states are \"TODO\" and \"DONE\".
  7092. So for this example: when the item starts with TODO, it is changed to DONE.
  7093. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7094. DONE are present, add TODO at the beginning of the heading.
  7095. With C-u prefix arg, use completion to determine the new state.
  7096. With numeric prefix arg, switch to that state.
  7097. For calling through lisp, arg is also interpreted in the following way:
  7098. 'none -> empty state
  7099. \"\"(empty string) -> switch to empty state
  7100. 'done -> switch to DONE
  7101. 'nextset -> switch to the next set of keywords
  7102. 'previousset -> switch to the previous set of keywords
  7103. \"WAITING\" -> switch to the specified keyword, but only if it
  7104. really is a member of `org-todo-keywords'."
  7105. (interactive "P")
  7106. (save-excursion
  7107. (catch 'exit
  7108. (org-back-to-heading)
  7109. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7110. (or (looking-at (concat " +" org-todo-regexp " *"))
  7111. (looking-at " *"))
  7112. (let* ((match-data (match-data))
  7113. (startpos (point-at-bol))
  7114. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7115. (org-log-done org-log-done)
  7116. (org-log-repeat org-log-repeat)
  7117. (org-todo-log-states org-todo-log-states)
  7118. (this (match-string 1))
  7119. (hl-pos (match-beginning 0))
  7120. (head (org-get-todo-sequence-head this))
  7121. (ass (assoc head org-todo-kwd-alist))
  7122. (interpret (nth 1 ass))
  7123. (done-word (nth 3 ass))
  7124. (final-done-word (nth 4 ass))
  7125. (last-state (or this ""))
  7126. (completion-ignore-case t)
  7127. (member (member this org-todo-keywords-1))
  7128. (tail (cdr member))
  7129. (state (cond
  7130. ((and org-todo-key-trigger
  7131. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7132. (and (not arg) org-use-fast-todo-selection
  7133. (not (eq org-use-fast-todo-selection 'prefix)))))
  7134. ;; Use fast selection
  7135. (org-fast-todo-selection))
  7136. ((and (equal arg '(4))
  7137. (or (not org-use-fast-todo-selection)
  7138. (not org-todo-key-trigger)))
  7139. ;; Read a state with completion
  7140. (completing-read "State: " (mapcar (lambda(x) (list x))
  7141. org-todo-keywords-1)
  7142. nil t))
  7143. ((eq arg 'right)
  7144. (if this
  7145. (if tail (car tail) nil)
  7146. (car org-todo-keywords-1)))
  7147. ((eq arg 'left)
  7148. (if (equal member org-todo-keywords-1)
  7149. nil
  7150. (if this
  7151. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7152. org-todo-keywords-1)
  7153. (org-last org-todo-keywords-1))))
  7154. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7155. (setq arg nil))) ; hack to fall back to cycling
  7156. (arg
  7157. ;; user or caller requests a specific state
  7158. (cond
  7159. ((equal arg "") nil)
  7160. ((eq arg 'none) nil)
  7161. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7162. ((eq arg 'nextset)
  7163. (or (car (cdr (member head org-todo-heads)))
  7164. (car org-todo-heads)))
  7165. ((eq arg 'previousset)
  7166. (let ((org-todo-heads (reverse org-todo-heads)))
  7167. (or (car (cdr (member head org-todo-heads)))
  7168. (car org-todo-heads))))
  7169. ((car (member arg org-todo-keywords-1)))
  7170. ((nth (1- (prefix-numeric-value arg))
  7171. org-todo-keywords-1))))
  7172. ((null member) (or head (car org-todo-keywords-1)))
  7173. ((equal this final-done-word) nil) ;; -> make empty
  7174. ((null tail) nil) ;; -> first entry
  7175. ((eq interpret 'sequence)
  7176. (car tail))
  7177. ((memq interpret '(type priority))
  7178. (if (eq this-command last-command)
  7179. (car tail)
  7180. (if (> (length tail) 0)
  7181. (or done-word (car org-done-keywords))
  7182. nil)))
  7183. (t nil)))
  7184. (next (if state (concat " " state " ") " "))
  7185. (change-plist (list :type 'todo-state-change :from this :to state
  7186. :position startpos))
  7187. dolog now-done-p)
  7188. (when org-blocker-hook
  7189. (unless (save-excursion
  7190. (save-match-data
  7191. (run-hook-with-args-until-failure
  7192. 'org-blocker-hook change-plist)))
  7193. (if (interactive-p)
  7194. (error "TODO state change from %s to %s blocked" this state)
  7195. ;; fail silently
  7196. (message "TODO state change from %s to %s blocked" this state)
  7197. (throw 'exit nil))))
  7198. (store-match-data match-data)
  7199. (replace-match next t t)
  7200. (unless (pos-visible-in-window-p hl-pos)
  7201. (message "TODO state changed to %s" (org-trim next)))
  7202. (unless head
  7203. (setq head (org-get-todo-sequence-head state)
  7204. ass (assoc head org-todo-kwd-alist)
  7205. interpret (nth 1 ass)
  7206. done-word (nth 3 ass)
  7207. final-done-word (nth 4 ass)))
  7208. (when (memq arg '(nextset previousset))
  7209. (message "Keyword-Set %d/%d: %s"
  7210. (- (length org-todo-sets) -1
  7211. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7212. (length org-todo-sets)
  7213. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7214. (setq org-last-todo-state-is-todo
  7215. (not (member state org-done-keywords)))
  7216. (setq now-done-p (and (member state org-done-keywords)
  7217. (not (member this org-done-keywords))))
  7218. (and logging (org-local-logging logging))
  7219. (when (and (or org-todo-log-states org-log-done)
  7220. (not (memq arg '(nextset previousset))))
  7221. ;; we need to look at recording a time and note
  7222. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7223. (nth 2 (assoc this org-todo-log-states))))
  7224. (when (and state
  7225. (member state org-not-done-keywords)
  7226. (not (member this org-not-done-keywords)))
  7227. ;; This is now a todo state and was not one before
  7228. ;; If there was a CLOSED time stamp, get rid of it.
  7229. (org-add-planning-info nil nil 'closed))
  7230. (when (and now-done-p org-log-done)
  7231. ;; It is now done, and it was not done before
  7232. (org-add-planning-info 'closed (org-current-time))
  7233. (if (and (not dolog) (eq 'note org-log-done))
  7234. (org-add-log-setup 'done state 'findpos 'note)))
  7235. (when (and state dolog)
  7236. ;; This is a non-nil state, and we need to log it
  7237. (org-add-log-setup 'state state 'findpos dolog)))
  7238. ;; Fixup tag positioning
  7239. (org-todo-trigger-tag-changes state)
  7240. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7241. (when org-provide-todo-statistics
  7242. (org-update-parent-todo-statistics))
  7243. (run-hooks 'org-after-todo-state-change-hook)
  7244. (if (and arg (not (member state org-done-keywords)))
  7245. (setq head (org-get-todo-sequence-head state)))
  7246. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7247. ;; Do we need to trigger a repeat?
  7248. (when now-done-p
  7249. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7250. ;; This is for the agenda, take a snapshot of the headline.
  7251. (save-match-data
  7252. (setq org-agenda-headline-snapshot-before-repeat
  7253. (org-get-heading))))
  7254. (org-auto-repeat-maybe state))
  7255. ;; Fixup cursor location if close to the keyword
  7256. (if (and (outline-on-heading-p)
  7257. (not (bolp))
  7258. (save-excursion (beginning-of-line 1)
  7259. (looking-at org-todo-line-regexp))
  7260. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7261. (progn
  7262. (goto-char (or (match-end 2) (match-end 1)))
  7263. (just-one-space)))
  7264. (when org-trigger-hook
  7265. (save-excursion
  7266. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7267. (defun org-update-parent-todo-statistics ()
  7268. "Update any statistics cookie in the parent of the current headline."
  7269. (interactive)
  7270. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7271. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7272. (catch 'exit
  7273. (save-excursion
  7274. (setq level (org-up-heading-safe))
  7275. (unless (and level
  7276. (re-search-forward box-re (point-at-eol) t))
  7277. (throw 'exit nil))
  7278. (setq is-percent (match-end 2))
  7279. (save-match-data
  7280. (unless (outline-next-heading) (throw 'exit nil))
  7281. (while (looking-at org-todo-line-regexp)
  7282. (setq kwd (match-string 2))
  7283. (and kwd (setq cnt-all (1+ cnt-all)))
  7284. (and (member kwd org-done-keywords)
  7285. (setq cnt-done (1+ cnt-done)))
  7286. (condition-case nil
  7287. (org-forward-same-level 1)
  7288. (error (end-of-line 1)))))
  7289. (replace-match
  7290. (if is-percent
  7291. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7292. (format "[%d/%d]" cnt-done cnt-all)))
  7293. (run-hook-with-args 'org-after-todo-statistics-hook
  7294. cnt-done (- cnt-all cnt-done))))))
  7295. (defvar org-after-todo-statistics-hook nil
  7296. "Hook that is called after a TODO statistics cookie has been updated.
  7297. Each function is called with two arguments: the number of not-done entries
  7298. and the number of done entries.
  7299. For example, the following function, when added to this hook, will switch
  7300. an entry to DONE when all children are done, and back to TODO when new
  7301. entries are set to a TODO status. Note that this hook is only called
  7302. when there is a statistics cookie in the headline!
  7303. (defun org-summary-todo (n-done n-not-done)
  7304. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7305. (let (org-log-done org-log-states) ; turn off logging
  7306. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7307. ")
  7308. (defun org-todo-trigger-tag-changes (state)
  7309. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7310. (let ((l org-todo-state-tags-triggers)
  7311. changes)
  7312. (when (or (not state) (equal state ""))
  7313. (setq changes (append changes (cdr (assoc "" l)))))
  7314. (when (and (stringp state) (> (length state) 0))
  7315. (setq changes (append changes (cdr (assoc state l)))))
  7316. (when (member state org-not-done-keywords)
  7317. (setq changes (append changes (cdr (assoc 'todo l)))))
  7318. (when (member state org-done-keywords)
  7319. (setq changes (append changes (cdr (assoc 'done l)))))
  7320. (dolist (c changes)
  7321. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7322. (defun org-local-logging (value)
  7323. "Get logging settings from a property VALUE."
  7324. (let* (words w a)
  7325. ;; directly set the variables, they are already local.
  7326. (setq org-log-done nil
  7327. org-log-repeat nil
  7328. org-todo-log-states nil)
  7329. (setq words (org-split-string value))
  7330. (while (setq w (pop words))
  7331. (cond
  7332. ((setq a (assoc w org-startup-options))
  7333. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7334. (set (nth 1 a) (nth 2 a))))
  7335. ((setq a (org-extract-log-state-settings w))
  7336. (and (member (car a) org-todo-keywords-1)
  7337. (push a org-todo-log-states)))))))
  7338. (defun org-get-todo-sequence-head (kwd)
  7339. "Return the head of the TODO sequence to which KWD belongs.
  7340. If KWD is not set, check if there is a text property remembering the
  7341. right sequence."
  7342. (let (p)
  7343. (cond
  7344. ((not kwd)
  7345. (or (get-text-property (point-at-bol) 'org-todo-head)
  7346. (progn
  7347. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7348. nil (point-at-eol)))
  7349. (get-text-property p 'org-todo-head))))
  7350. ((not (member kwd org-todo-keywords-1))
  7351. (car org-todo-keywords-1))
  7352. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7353. (defun org-fast-todo-selection ()
  7354. "Fast TODO keyword selection with single keys.
  7355. Returns the new TODO keyword, or nil if no state change should occur."
  7356. (let* ((fulltable org-todo-key-alist)
  7357. (done-keywords org-done-keywords) ;; needed for the faces.
  7358. (maxlen (apply 'max (mapcar
  7359. (lambda (x)
  7360. (if (stringp (car x)) (string-width (car x)) 0))
  7361. fulltable)))
  7362. (expert nil)
  7363. (fwidth (+ maxlen 3 1 3))
  7364. (ncol (/ (- (window-width) 4) fwidth))
  7365. tg cnt e c tbl
  7366. groups ingroup)
  7367. (save-window-excursion
  7368. (if expert
  7369. (set-buffer (get-buffer-create " *Org todo*"))
  7370. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7371. (erase-buffer)
  7372. (org-set-local 'org-done-keywords done-keywords)
  7373. (setq tbl fulltable cnt 0)
  7374. (while (setq e (pop tbl))
  7375. (cond
  7376. ((equal e '(:startgroup))
  7377. (push '() groups) (setq ingroup t)
  7378. (when (not (= cnt 0))
  7379. (setq cnt 0)
  7380. (insert "\n"))
  7381. (insert "{ "))
  7382. ((equal e '(:endgroup))
  7383. (setq ingroup nil cnt 0)
  7384. (insert "}\n"))
  7385. (t
  7386. (setq tg (car e) c (cdr e))
  7387. (if ingroup (push tg (car groups)))
  7388. (setq tg (org-add-props tg nil 'face
  7389. (org-get-todo-face tg)))
  7390. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7391. (insert "[" c "] " tg (make-string
  7392. (- fwidth 4 (length tg)) ?\ ))
  7393. (when (= (setq cnt (1+ cnt)) ncol)
  7394. (insert "\n")
  7395. (if ingroup (insert " "))
  7396. (setq cnt 0)))))
  7397. (insert "\n")
  7398. (goto-char (point-min))
  7399. (if (not expert) (org-fit-window-to-buffer))
  7400. (message "[a-z..]:Set [SPC]:clear")
  7401. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7402. (cond
  7403. ((or (= c ?\C-g)
  7404. (and (= c ?q) (not (rassoc c fulltable))))
  7405. (setq quit-flag t))
  7406. ((= c ?\ ) nil)
  7407. ((setq e (rassoc c fulltable) tg (car e))
  7408. tg)
  7409. (t (setq quit-flag t))))))
  7410. (defun org-entry-is-todo-p ()
  7411. (member (org-get-todo-state) org-not-done-keywords))
  7412. (defun org-entry-is-done-p ()
  7413. (member (org-get-todo-state) org-done-keywords))
  7414. (defun org-get-todo-state ()
  7415. (save-excursion
  7416. (org-back-to-heading t)
  7417. (and (looking-at org-todo-line-regexp)
  7418. (match-end 2)
  7419. (match-string 2))))
  7420. (defun org-at-date-range-p (&optional inactive-ok)
  7421. "Is the cursor inside a date range?"
  7422. (interactive)
  7423. (save-excursion
  7424. (catch 'exit
  7425. (let ((pos (point)))
  7426. (skip-chars-backward "^[<\r\n")
  7427. (skip-chars-backward "<[")
  7428. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7429. (>= (match-end 0) pos)
  7430. (throw 'exit t))
  7431. (skip-chars-backward "^<[\r\n")
  7432. (skip-chars-backward "<[")
  7433. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7434. (>= (match-end 0) pos)
  7435. (throw 'exit t)))
  7436. nil)))
  7437. (defun org-get-repeat ()
  7438. "Check if there is a deadline/schedule with repeater in this entry."
  7439. (save-match-data
  7440. (save-excursion
  7441. (org-back-to-heading t)
  7442. (if (re-search-forward
  7443. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7444. (match-string 1)))))
  7445. (defvar org-last-changed-timestamp)
  7446. (defvar org-last-inserted-timestamp)
  7447. (defvar org-log-post-message)
  7448. (defvar org-log-note-purpose)
  7449. (defvar org-log-note-how)
  7450. (defvar org-log-note-extra)
  7451. (defun org-auto-repeat-maybe (done-word)
  7452. "Check if the current headline contains a repeated deadline/schedule.
  7453. If yes, set TODO state back to what it was and change the base date
  7454. of repeating deadline/scheduled time stamps to new date.
  7455. This function is run automatically after each state change to a DONE state."
  7456. ;; last-state is dynamically scoped into this function
  7457. (let* ((repeat (org-get-repeat))
  7458. (aa (assoc last-state org-todo-kwd-alist))
  7459. (interpret (nth 1 aa))
  7460. (head (nth 2 aa))
  7461. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7462. (msg "Entry repeats: ")
  7463. (org-log-done nil)
  7464. (org-todo-log-states nil)
  7465. (nshiftmax 10) (nshift 0)
  7466. re type n what ts mb0 time)
  7467. (when repeat
  7468. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7469. (org-todo (if (eq interpret 'type) last-state head))
  7470. (when org-log-repeat
  7471. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7472. (memq 'org-add-log-note post-command-hook))
  7473. ;; OK, we are already setup for some record
  7474. (if (eq org-log-repeat 'note)
  7475. ;; make sure we take a note, not only a time stamp
  7476. (setq org-log-note-how 'note))
  7477. ;; Set up for taking a record
  7478. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7479. 'findpos org-log-repeat)))
  7480. (org-back-to-heading t)
  7481. (org-add-planning-info nil nil 'closed)
  7482. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7483. org-deadline-time-regexp "\\)\\|\\("
  7484. org-ts-regexp "\\)"))
  7485. (while (re-search-forward
  7486. re (save-excursion (outline-next-heading) (point)) t)
  7487. (setq type (if (match-end 1) org-scheduled-string
  7488. (if (match-end 3) org-deadline-string "Plain:"))
  7489. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7490. mb0 (match-beginning 0))
  7491. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7492. (setq n (string-to-number (match-string 2 ts))
  7493. what (match-string 3 ts))
  7494. (if (equal what "w") (setq n (* n 7) what "d"))
  7495. ;; Preparation, see if we need to modify the start date for the change
  7496. (when (match-end 1)
  7497. (setq time (save-match-data (org-time-string-to-time ts)))
  7498. (cond
  7499. ((equal (match-string 1 ts) ".")
  7500. ;; Shift starting date to today
  7501. (org-timestamp-change
  7502. (- (time-to-days (current-time)) (time-to-days time))
  7503. 'day))
  7504. ((equal (match-string 1 ts) "+")
  7505. (while (or (= nshift 0)
  7506. (<= (time-to-days time) (time-to-days (current-time))))
  7507. (when (= (incf nshift) nshiftmax)
  7508. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7509. (error "Abort")))
  7510. (org-timestamp-change n (cdr (assoc what whata)))
  7511. (org-at-timestamp-p t)
  7512. (setq ts (match-string 1))
  7513. (setq time (save-match-data (org-time-string-to-time ts))))
  7514. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7515. ;; rematch, so that we have everything in place for the real shift
  7516. (org-at-timestamp-p t)
  7517. (setq ts (match-string 1))
  7518. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7519. (org-timestamp-change n (cdr (assoc what whata)))
  7520. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7521. (setq org-log-post-message msg)
  7522. (message "%s" msg))))
  7523. (defun org-show-todo-tree (arg)
  7524. "Make a compact tree which shows all headlines marked with TODO.
  7525. The tree will show the lines where the regexp matches, and all higher
  7526. headlines above the match.
  7527. With a \\[universal-argument] prefix, also show the DONE entries.
  7528. With a numeric prefix N, construct a sparse tree for the Nth element
  7529. of `org-todo-keywords-1'."
  7530. (interactive "P")
  7531. (let ((case-fold-search nil)
  7532. (kwd-re
  7533. (cond ((null arg) org-not-done-regexp)
  7534. ((equal arg '(4))
  7535. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7536. (mapcar 'list org-todo-keywords-1))))
  7537. (concat "\\("
  7538. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7539. "\\)\\>")))
  7540. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7541. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7542. org-todo-keywords-1)))
  7543. (t (error "Invalid prefix argument: %s" arg)))))
  7544. (message "%d TODO entries found"
  7545. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7546. (defun org-deadline (&optional remove time)
  7547. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7548. With argument REMOVE, remove any deadline from the item.
  7549. When TIME is set, it should be an internal time specification, and the
  7550. scheduling will use the corresponding date."
  7551. (interactive "P")
  7552. (if remove
  7553. (progn
  7554. (org-remove-timestamp-with-keyword org-deadline-string)
  7555. (message "Item no longer has a deadline."))
  7556. (if (org-get-repeat)
  7557. (error "Cannot change deadline on task with repeater, please do that by hand")
  7558. (org-add-planning-info 'deadline time 'closed)
  7559. (message "Deadline on %s" org-last-inserted-timestamp))))
  7560. (defun org-schedule (&optional remove time)
  7561. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7562. With argument REMOVE, remove any scheduling date from the item.
  7563. When TIME is set, it should be an internal time specification, and the
  7564. scheduling will use the corresponding date."
  7565. (interactive "P")
  7566. (if remove
  7567. (progn
  7568. (org-remove-timestamp-with-keyword org-scheduled-string)
  7569. (message "Item is no longer scheduled."))
  7570. (if (org-get-repeat)
  7571. (error "Cannot reschedule task with repeater, please do that by hand")
  7572. (org-add-planning-info 'scheduled time 'closed)
  7573. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7574. (defun org-remove-timestamp-with-keyword (keyword)
  7575. "Remove all time stamps with KEYWORD in the current entry."
  7576. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7577. beg)
  7578. (save-excursion
  7579. (org-back-to-heading t)
  7580. (setq beg (point))
  7581. (org-end-of-subtree t t)
  7582. (while (re-search-backward re beg t)
  7583. (replace-match "")
  7584. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7585. (equal (char-before) ?\ ))
  7586. (backward-delete-char 1)
  7587. (if (string-match "^[ \t]*$" (buffer-substring
  7588. (point-at-bol) (point-at-eol)))
  7589. (delete-region (point-at-bol)
  7590. (min (point-max) (1+ (point-at-eol))))))))))
  7591. (defun org-add-planning-info (what &optional time &rest remove)
  7592. "Insert new timestamp with keyword in the line directly after the headline.
  7593. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7594. If non is given, the user is prompted for a date.
  7595. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7596. be removed."
  7597. (interactive)
  7598. (let (org-time-was-given org-end-time-was-given ts
  7599. end default-time default-input)
  7600. (when (and (not time) (memq what '(scheduled deadline)))
  7601. ;; Try to get a default date/time from existing timestamp
  7602. (save-excursion
  7603. (org-back-to-heading t)
  7604. (setq end (save-excursion (outline-next-heading) (point)))
  7605. (when (re-search-forward (if (eq what 'scheduled)
  7606. org-scheduled-time-regexp
  7607. org-deadline-time-regexp)
  7608. end t)
  7609. (setq ts (match-string 1)
  7610. default-time
  7611. (apply 'encode-time (org-parse-time-string ts))
  7612. default-input (and ts (org-get-compact-tod ts))))))
  7613. (when what
  7614. ;; If necessary, get the time from the user
  7615. (setq time (or time (org-read-date nil 'to-time nil nil
  7616. default-time default-input))))
  7617. (when (and org-insert-labeled-timestamps-at-point
  7618. (member what '(scheduled deadline)))
  7619. (insert
  7620. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7621. (org-insert-time-stamp time org-time-was-given
  7622. nil nil nil (list org-end-time-was-given))
  7623. (setq what nil))
  7624. (save-excursion
  7625. (save-restriction
  7626. (let (col list elt ts buffer-invisibility-spec)
  7627. (org-back-to-heading t)
  7628. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7629. (goto-char (match-end 1))
  7630. (setq col (current-column))
  7631. (goto-char (match-end 0))
  7632. (if (eobp) (insert "\n") (forward-char 1))
  7633. (if (and (not (looking-at outline-regexp))
  7634. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7635. "[^\r\n]*"))
  7636. (not (equal (match-string 1) org-clock-string)))
  7637. (narrow-to-region (match-beginning 0) (match-end 0))
  7638. (insert-before-markers "\n")
  7639. (backward-char 1)
  7640. (narrow-to-region (point) (point))
  7641. (and org-adapt-indentation (org-indent-to-column col)))
  7642. ;; Check if we have to remove something.
  7643. (setq list (cons what remove))
  7644. (while list
  7645. (setq elt (pop list))
  7646. (goto-char (point-min))
  7647. (when (or (and (eq elt 'scheduled)
  7648. (re-search-forward org-scheduled-time-regexp nil t))
  7649. (and (eq elt 'deadline)
  7650. (re-search-forward org-deadline-time-regexp nil t))
  7651. (and (eq elt 'closed)
  7652. (re-search-forward org-closed-time-regexp nil t)))
  7653. (replace-match "")
  7654. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7655. (if (looking-at " +") (replace-match ""))))
  7656. (goto-char (point-max))
  7657. (when what
  7658. (insert
  7659. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7660. (cond ((eq what 'scheduled) org-scheduled-string)
  7661. ((eq what 'deadline) org-deadline-string)
  7662. ((eq what 'closed) org-closed-string))
  7663. " ")
  7664. (setq ts (org-insert-time-stamp
  7665. time
  7666. (or org-time-was-given
  7667. (and (eq what 'closed) org-log-done-with-time))
  7668. (eq what 'closed)
  7669. nil nil (list org-end-time-was-given)))
  7670. (end-of-line 1))
  7671. (goto-char (point-min))
  7672. (widen)
  7673. (if (and (looking-at "[ \t]+\n")
  7674. (equal (char-before) ?\n))
  7675. (delete-region (1- (point)) (point-at-eol)))
  7676. ts)))))
  7677. (defvar org-log-note-marker (make-marker))
  7678. (defvar org-log-note-purpose nil)
  7679. (defvar org-log-note-state nil)
  7680. (defvar org-log-note-how nil)
  7681. (defvar org-log-note-extra nil)
  7682. (defvar org-log-note-window-configuration nil)
  7683. (defvar org-log-note-return-to (make-marker))
  7684. (defvar org-log-post-message nil
  7685. "Message to be displayed after a log note has been stored.
  7686. The auto-repeater uses this.")
  7687. (defun org-add-note ()
  7688. "Add a note to the current entry.
  7689. This is done in the same way as adding a state change note."
  7690. (interactive)
  7691. (org-add-log-setup 'note nil 'findpos nil))
  7692. (defvar org-property-end-re)
  7693. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7694. "Set up the post command hook to take a note.
  7695. If this is about to TODO state change, the new state is expected in STATE.
  7696. When FINDPOS is non-nil, find the correct position for the note in
  7697. the current entry. If not, assume that it can be inserted at point.
  7698. HOW is an indicator what kind of note should be created.
  7699. EXTRA is additional text that will be inserted into the notes buffer."
  7700. (save-restriction
  7701. (save-excursion
  7702. (when findpos
  7703. (org-back-to-heading t)
  7704. (narrow-to-region (point) (save-excursion
  7705. (outline-next-heading) (point)))
  7706. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7707. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7708. "[^\r\n]*\\)?"))
  7709. (goto-char (match-end 0))
  7710. (when (and org-log-state-notes-insert-after-drawers
  7711. (save-excursion
  7712. (forward-line) (looking-at org-drawer-regexp)))
  7713. (progn (forward-line)
  7714. (while (looking-at org-drawer-regexp)
  7715. (goto-char (match-end 0))
  7716. (re-search-forward org-property-end-re (point-max) t)
  7717. (forward-line))
  7718. (forward-line -1)))
  7719. (unless org-log-states-order-reversed
  7720. (and (= (char-after) ?\n) (forward-char 1))
  7721. (org-skip-over-state-notes)
  7722. (skip-chars-backward " \t\n\r")))
  7723. (move-marker org-log-note-marker (point))
  7724. (setq org-log-note-purpose purpose
  7725. org-log-note-state state
  7726. org-log-note-how how
  7727. org-log-note-extra extra)
  7728. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7729. (defun org-skip-over-state-notes ()
  7730. "Skip past the list of State notes in an entry."
  7731. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7732. (while (looking-at "[ \t]*- State")
  7733. (condition-case nil
  7734. (org-next-item)
  7735. (error (org-end-of-item)))))
  7736. (defun org-add-log-note (&optional purpose)
  7737. "Pop up a window for taking a note, and add this note later at point."
  7738. (remove-hook 'post-command-hook 'org-add-log-note)
  7739. (setq org-log-note-window-configuration (current-window-configuration))
  7740. (delete-other-windows)
  7741. (move-marker org-log-note-return-to (point))
  7742. (switch-to-buffer (marker-buffer org-log-note-marker))
  7743. (goto-char org-log-note-marker)
  7744. (org-switch-to-buffer-other-window "*Org Note*")
  7745. (erase-buffer)
  7746. (if (memq org-log-note-how '(time state))
  7747. (let (current-prefix-arg) (org-store-log-note))
  7748. (let ((org-inhibit-startup t)) (org-mode))
  7749. (insert (format "# Insert note for %s.
  7750. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7751. (cond
  7752. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7753. ((eq org-log-note-purpose 'done) "closed todo item")
  7754. ((eq org-log-note-purpose 'state)
  7755. (format "state change to \"%s\"" org-log-note-state))
  7756. ((eq org-log-note-purpose 'note)
  7757. "this entry")
  7758. (t (error "This should not happen")))))
  7759. (if org-log-note-extra (insert org-log-note-extra))
  7760. (org-set-local 'org-finish-function 'org-store-log-note)))
  7761. (defvar org-note-abort nil) ; dynamically scoped
  7762. (defun org-store-log-note ()
  7763. "Finish taking a log note, and insert it to where it belongs."
  7764. (let ((txt (buffer-string))
  7765. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7766. lines ind)
  7767. (kill-buffer (current-buffer))
  7768. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7769. (setq txt (replace-match "" t t txt)))
  7770. (if (string-match "\\s-+\\'" txt)
  7771. (setq txt (replace-match "" t t txt)))
  7772. (setq lines (org-split-string txt "\n"))
  7773. (when (and note (string-match "\\S-" note))
  7774. (setq note
  7775. (org-replace-escapes
  7776. note
  7777. (list (cons "%u" (user-login-name))
  7778. (cons "%U" user-full-name)
  7779. (cons "%t" (format-time-string
  7780. (org-time-stamp-format 'long 'inactive)
  7781. (current-time)))
  7782. (cons "%s" (if org-log-note-state
  7783. (concat "\"" org-log-note-state "\"")
  7784. "")))))
  7785. (if lines (setq note (concat note " \\\\")))
  7786. (push note lines))
  7787. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7788. (when lines
  7789. (save-excursion
  7790. (set-buffer (marker-buffer org-log-note-marker))
  7791. (save-excursion
  7792. (goto-char org-log-note-marker)
  7793. (move-marker org-log-note-marker nil)
  7794. (end-of-line 1)
  7795. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7796. (indent-relative nil)
  7797. (insert "- " (pop lines))
  7798. (org-indent-line-function)
  7799. (beginning-of-line 1)
  7800. (looking-at "[ \t]*")
  7801. (setq ind (concat (match-string 0) " "))
  7802. (end-of-line 1)
  7803. (while lines (insert "\n" ind (pop lines)))))))
  7804. (set-window-configuration org-log-note-window-configuration)
  7805. (with-current-buffer (marker-buffer org-log-note-return-to)
  7806. (goto-char org-log-note-return-to))
  7807. (move-marker org-log-note-return-to nil)
  7808. (and org-log-post-message (message "%s" org-log-post-message)))
  7809. (defun org-sparse-tree (&optional arg)
  7810. "Create a sparse tree, prompt for the details.
  7811. This command can create sparse trees. You first need to select the type
  7812. of match used to create the tree:
  7813. t Show entries with a specific TODO keyword.
  7814. T Show entries selected by a tags match.
  7815. p Enter a property name and its value (both with completion on existing
  7816. names/values) and show entries with that property.
  7817. r Show entries matching a regular expression
  7818. d Show deadlines due within `org-deadline-warning-days'."
  7819. (interactive "P")
  7820. (let (ans kwd value)
  7821. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7822. (setq ans (read-char-exclusive))
  7823. (cond
  7824. ((equal ans ?d)
  7825. (call-interactively 'org-check-deadlines))
  7826. ((equal ans ?b)
  7827. (call-interactively 'org-check-before-date))
  7828. ((equal ans ?t)
  7829. (org-show-todo-tree '(4)))
  7830. ((equal ans ?T)
  7831. (call-interactively 'org-tags-sparse-tree))
  7832. ((member ans '(?p ?P))
  7833. (setq kwd (completing-read "Property: "
  7834. (mapcar 'list (org-buffer-property-keys))))
  7835. (setq value (completing-read "Value: "
  7836. (mapcar 'list (org-property-values kwd))))
  7837. (unless (string-match "\\`{.*}\\'" value)
  7838. (setq value (concat "\"" value "\"")))
  7839. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7840. ((member ans '(?r ?R ?/))
  7841. (call-interactively 'org-occur))
  7842. (t (error "No such sparse tree command \"%c\"" ans)))))
  7843. (defvar org-occur-highlights nil
  7844. "List of overlays used for occur matches.")
  7845. (make-variable-buffer-local 'org-occur-highlights)
  7846. (defvar org-occur-parameters nil
  7847. "Parameters of the active org-occur calls.
  7848. This is a list, each call to org-occur pushes as cons cell,
  7849. containing the regular expression and the callback, onto the list.
  7850. The list can contain several entries if `org-occur' has been called
  7851. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7852. will only contain one set of parameters. When the highlights are
  7853. removed (for example with `C-c C-c', or with the next edit (depending
  7854. on `org-remove-highlights-with-change'), this variable is emptied
  7855. as well.")
  7856. (make-variable-buffer-local 'org-occur-parameters)
  7857. (defun org-occur (regexp &optional keep-previous callback)
  7858. "Make a compact tree which shows all matches of REGEXP.
  7859. The tree will show the lines where the regexp matches, and all higher
  7860. headlines above the match. It will also show the heading after the match,
  7861. to make sure editing the matching entry is easy.
  7862. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7863. call to `org-occur' will be kept, to allow stacking of calls to this
  7864. command.
  7865. If CALLBACK is non-nil, it is a function which is called to confirm
  7866. that the match should indeed be shown."
  7867. (interactive "sRegexp: \nP")
  7868. (unless keep-previous
  7869. (org-remove-occur-highlights nil nil t))
  7870. (push (cons regexp callback) org-occur-parameters)
  7871. (let ((cnt 0))
  7872. (save-excursion
  7873. (goto-char (point-min))
  7874. (if (or (not keep-previous) ; do not want to keep
  7875. (not org-occur-highlights)) ; no previous matches
  7876. ;; hide everything
  7877. (org-overview))
  7878. (while (re-search-forward regexp nil t)
  7879. (when (or (not callback)
  7880. (save-match-data (funcall callback)))
  7881. (setq cnt (1+ cnt))
  7882. (when org-highlight-sparse-tree-matches
  7883. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7884. (org-show-context 'occur-tree))))
  7885. (when org-remove-highlights-with-change
  7886. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7887. nil 'local))
  7888. (unless org-sparse-tree-open-archived-trees
  7889. (org-hide-archived-subtrees (point-min) (point-max)))
  7890. (run-hooks 'org-occur-hook)
  7891. (if (interactive-p)
  7892. (message "%d match(es) for regexp %s" cnt regexp))
  7893. cnt))
  7894. (defun org-show-context (&optional key)
  7895. "Make sure point and context and visible.
  7896. How much context is shown depends upon the variables
  7897. `org-show-hierarchy-above', `org-show-following-heading'. and
  7898. `org-show-siblings'."
  7899. (let ((heading-p (org-on-heading-p t))
  7900. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7901. (following-p (org-get-alist-option org-show-following-heading key))
  7902. (entry-p (org-get-alist-option org-show-entry-below key))
  7903. (siblings-p (org-get-alist-option org-show-siblings key)))
  7904. (catch 'exit
  7905. ;; Show heading or entry text
  7906. (if (and heading-p (not entry-p))
  7907. (org-flag-heading nil) ; only show the heading
  7908. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7909. (org-show-hidden-entry))) ; show entire entry
  7910. (when following-p
  7911. ;; Show next sibling, or heading below text
  7912. (save-excursion
  7913. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7914. (org-flag-heading nil))))
  7915. (when siblings-p (org-show-siblings))
  7916. (when hierarchy-p
  7917. ;; show all higher headings, possibly with siblings
  7918. (save-excursion
  7919. (while (and (condition-case nil
  7920. (progn (org-up-heading-all 1) t)
  7921. (error nil))
  7922. (not (bobp)))
  7923. (org-flag-heading nil)
  7924. (when siblings-p (org-show-siblings))))))))
  7925. (defun org-reveal (&optional siblings)
  7926. "Show current entry, hierarchy above it, and the following headline.
  7927. This can be used to show a consistent set of context around locations
  7928. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7929. not t for the search context.
  7930. With optional argument SIBLINGS, on each level of the hierarchy all
  7931. siblings are shown. This repairs the tree structure to what it would
  7932. look like when opened with hierarchical calls to `org-cycle'."
  7933. (interactive "P")
  7934. (let ((org-show-hierarchy-above t)
  7935. (org-show-following-heading t)
  7936. (org-show-siblings (if siblings t org-show-siblings)))
  7937. (org-show-context nil)))
  7938. (defun org-highlight-new-match (beg end)
  7939. "Highlight from BEG to END and mark the highlight is an occur headline."
  7940. (let ((ov (org-make-overlay beg end)))
  7941. (org-overlay-put ov 'face 'secondary-selection)
  7942. (push ov org-occur-highlights)))
  7943. (defun org-remove-occur-highlights (&optional beg end noremove)
  7944. "Remove the occur highlights from the buffer.
  7945. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7946. from the `before-change-functions' in the current buffer."
  7947. (interactive)
  7948. (unless org-inhibit-highlight-removal
  7949. (mapc 'org-delete-overlay org-occur-highlights)
  7950. (setq org-occur-highlights nil)
  7951. (setq org-occur-parameters nil)
  7952. (unless noremove
  7953. (remove-hook 'before-change-functions
  7954. 'org-remove-occur-highlights 'local))))
  7955. ;;;; Priorities
  7956. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7957. "Regular expression matching the priority indicator.")
  7958. (defvar org-remove-priority-next-time nil)
  7959. (defun org-priority-up ()
  7960. "Increase the priority of the current item."
  7961. (interactive)
  7962. (org-priority 'up))
  7963. (defun org-priority-down ()
  7964. "Decrease the priority of the current item."
  7965. (interactive)
  7966. (org-priority 'down))
  7967. (defun org-priority (&optional action)
  7968. "Change the priority of an item by ARG.
  7969. ACTION can be `set', `up', `down', or a character."
  7970. (interactive)
  7971. (setq action (or action 'set))
  7972. (let (current new news have remove)
  7973. (save-excursion
  7974. (org-back-to-heading)
  7975. (if (looking-at org-priority-regexp)
  7976. (setq current (string-to-char (match-string 2))
  7977. have t)
  7978. (setq current org-default-priority))
  7979. (cond
  7980. ((or (eq action 'set)
  7981. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7982. (if (not (eq action 'set))
  7983. (setq new action)
  7984. (message "Priority %c-%c, SPC to remove: "
  7985. org-highest-priority org-lowest-priority)
  7986. (setq new (read-char-exclusive)))
  7987. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7988. (= (upcase org-lowest-priority) org-lowest-priority))
  7989. (setq new (upcase new)))
  7990. (cond ((equal new ?\ ) (setq remove t))
  7991. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7992. (error "Priority must be between `%c' and `%c'"
  7993. org-highest-priority org-lowest-priority))))
  7994. ((eq action 'up)
  7995. (if (and (not have) (eq last-command this-command))
  7996. (setq new org-lowest-priority)
  7997. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7998. org-default-priority (1- current)))))
  7999. ((eq action 'down)
  8000. (if (and (not have) (eq last-command this-command))
  8001. (setq new org-highest-priority)
  8002. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8003. org-default-priority (1+ current)))))
  8004. (t (error "Invalid action")))
  8005. (if (or (< (upcase new) org-highest-priority)
  8006. (> (upcase new) org-lowest-priority))
  8007. (setq remove t))
  8008. (setq news (format "%c" new))
  8009. (if have
  8010. (if remove
  8011. (replace-match "" t t nil 1)
  8012. (replace-match news t t nil 2))
  8013. (if remove
  8014. (error "No priority cookie found in line")
  8015. (looking-at org-todo-line-regexp)
  8016. (if (match-end 2)
  8017. (progn
  8018. (goto-char (match-end 2))
  8019. (insert " [#" news "]"))
  8020. (goto-char (match-beginning 3))
  8021. (insert "[#" news "] ")))))
  8022. (org-preserve-lc (org-set-tags nil 'align))
  8023. (if remove
  8024. (message "Priority removed")
  8025. (message "Priority of current item set to %s" news))))
  8026. (defun org-get-priority (s)
  8027. "Find priority cookie and return priority."
  8028. (save-match-data
  8029. (if (not (string-match org-priority-regexp s))
  8030. (* 1000 (- org-lowest-priority org-default-priority))
  8031. (* 1000 (- org-lowest-priority
  8032. (string-to-char (match-string 2 s)))))))
  8033. ;;;; Tags
  8034. (defvar org-agenda-archives-mode)
  8035. (defun org-scan-tags (action matcher &optional todo-only)
  8036. "Scan headline tags with inheritance and produce output ACTION.
  8037. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8038. or `agenda' to produce an entry list for an agenda view. It can also be
  8039. a Lisp form or a function that should be called at each matched headline, in
  8040. this case the return value is a list of all return values from these calls.
  8041. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8042. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8043. only lines with a TODO keyword are included in the output."
  8044. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8045. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8046. (org-re
  8047. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8048. (props (list 'face 'default
  8049. 'done-face 'org-done
  8050. 'undone-face 'default
  8051. 'mouse-face 'highlight
  8052. 'org-not-done-regexp org-not-done-regexp
  8053. 'org-todo-regexp org-todo-regexp
  8054. 'keymap org-agenda-keymap
  8055. 'help-echo
  8056. (format "mouse-2 or RET jump to org file %s"
  8057. (abbreviate-file-name
  8058. (or (buffer-file-name (buffer-base-buffer))
  8059. (buffer-name (buffer-base-buffer)))))))
  8060. (case-fold-search nil)
  8061. lspos tags tags-list
  8062. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8063. (llast 0) rtn rtn1 level category i txt
  8064. todo marker entry priority)
  8065. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8066. (setq action (list 'lambda nil action)))
  8067. (save-excursion
  8068. (goto-char (point-min))
  8069. (when (eq action 'sparse-tree)
  8070. (org-overview)
  8071. (org-remove-occur-highlights))
  8072. (while (re-search-forward re nil t)
  8073. (catch :skip
  8074. (setq todo (if (match-end 1) (match-string 2))
  8075. tags (if (match-end 4) (match-string 4)))
  8076. (goto-char (setq lspos (1+ (match-beginning 0))))
  8077. (setq level (org-reduced-level (funcall outline-level))
  8078. category (org-get-category))
  8079. (setq i llast llast level)
  8080. ;; remove tag lists from same and sublevels
  8081. (while (>= i level)
  8082. (when (setq entry (assoc i tags-alist))
  8083. (setq tags-alist (delete entry tags-alist)))
  8084. (setq i (1- i)))
  8085. ;; add the next tags
  8086. (when tags
  8087. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8088. tags-alist
  8089. (cons (cons level tags) tags-alist)))
  8090. ;; compile tags for current headline
  8091. (setq tags-list
  8092. (if org-use-tag-inheritance
  8093. (apply 'append (mapcar 'cdr tags-alist))
  8094. tags))
  8095. (when (and tags org-use-tag-inheritance
  8096. (not (eq t org-use-tag-inheritance)))
  8097. ;; selective inheritance, remove uninherited ones
  8098. (setcdr (car tags-alist)
  8099. (org-remove-uniherited-tags (cdar tags-alist))))
  8100. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8101. (let ((case-fold-search t)) (eval matcher))
  8102. (or
  8103. (not (member org-archive-tag tags-list))
  8104. ;; we have an archive tag, should we use this anyway?
  8105. (or (not org-agenda-skip-archived-trees)
  8106. (and (eq action 'agenda) org-agenda-archives-mode))))
  8107. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8108. ;; select this headline
  8109. (cond
  8110. ((eq action 'sparse-tree)
  8111. (and org-highlight-sparse-tree-matches
  8112. (org-get-heading) (match-end 0)
  8113. (org-highlight-new-match
  8114. (match-beginning 0) (match-beginning 1)))
  8115. (org-show-context 'tags-tree))
  8116. ((eq action 'agenda)
  8117. (setq txt (org-format-agenda-item
  8118. ""
  8119. (concat
  8120. (if org-tags-match-list-sublevels
  8121. (make-string (1- level) ?.) "")
  8122. (org-get-heading))
  8123. category tags-list)
  8124. priority (org-get-priority txt))
  8125. (goto-char lspos)
  8126. (setq marker (org-agenda-new-marker))
  8127. (org-add-props txt props
  8128. 'org-marker marker 'org-hd-marker marker 'org-category category
  8129. 'priority priority 'type "tagsmatch")
  8130. (push txt rtn))
  8131. ((functionp action)
  8132. (save-excursion
  8133. (setq rtn1 (funcall action))
  8134. (push rtn1 rtn))
  8135. (goto-char (point-at-eol)))
  8136. (t (error "Invalid action")))
  8137. ;; if we are to skip sublevels, jump to end of subtree
  8138. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8139. (when (and (eq action 'sparse-tree)
  8140. (not org-sparse-tree-open-archived-trees))
  8141. (org-hide-archived-subtrees (point-min) (point-max)))
  8142. (nreverse rtn)))
  8143. (defun org-remove-uniherited-tags (tags)
  8144. "Remove all tags that are not inherited from the list TAGS."
  8145. (cond
  8146. ((eq org-use-tag-inheritance t) tags)
  8147. ((not org-use-tag-inheritance) nil)
  8148. ((stringp org-use-tag-inheritance)
  8149. (delq nil (mapcar
  8150. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8151. tags)))
  8152. ((listp org-use-tag-inheritance)
  8153. (delq nil (mapcar
  8154. (lambda (x) (if (member x org-use-tag-inheritance) x nil))
  8155. tags)))))
  8156. (defvar todo-only) ;; dynamically scoped
  8157. (defun org-tags-sparse-tree (&optional todo-only match)
  8158. "Create a sparse tree according to tags string MATCH.
  8159. MATCH can contain positive and negative selection of tags, like
  8160. \"+WORK+URGENT-WITHBOSS\".
  8161. If optional argument TODO_ONLY is non-nil, only select lines that are
  8162. also TODO lines."
  8163. (interactive "P")
  8164. (org-prepare-agenda-buffers (list (current-buffer)))
  8165. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8166. (defvar org-cached-props nil)
  8167. (defun org-cached-entry-get (pom property)
  8168. (if (or (eq t org-use-property-inheritance)
  8169. (and (stringp org-use-property-inheritance)
  8170. (string-match org-use-property-inheritance property))
  8171. (and (listp org-use-property-inheritance)
  8172. (member property org-use-property-inheritance)))
  8173. ;; Caching is not possible, check it directly
  8174. (org-entry-get pom property 'inherit)
  8175. ;; Get all properties, so that we can do complicated checks easily
  8176. (cdr (assoc property (or org-cached-props
  8177. (setq org-cached-props
  8178. (org-entry-properties pom)))))))
  8179. (defun org-global-tags-completion-table (&optional files)
  8180. "Return the list of all tags in all agenda buffer/files."
  8181. (save-excursion
  8182. (org-uniquify
  8183. (delq nil
  8184. (apply 'append
  8185. (mapcar
  8186. (lambda (file)
  8187. (set-buffer (find-file-noselect file))
  8188. (append (org-get-buffer-tags)
  8189. (mapcar (lambda (x) (if (stringp (car-safe x))
  8190. (list (car-safe x)) nil))
  8191. org-tag-alist)))
  8192. (if (and files (car files))
  8193. files
  8194. (org-agenda-files))))))))
  8195. (defun org-make-tags-matcher (match)
  8196. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8197. ;; todo-only is scoped dynamically into this function, and the function
  8198. ;; may change it it the matcher asksk for it.
  8199. (unless match
  8200. ;; Get a new match request, with completion
  8201. (let ((org-last-tags-completion-table
  8202. (org-global-tags-completion-table)))
  8203. (setq match (completing-read
  8204. "Match: " 'org-tags-completion-function nil nil nil
  8205. 'org-tags-history))))
  8206. ;; Parse the string and create a lisp form
  8207. (let ((match0 match)
  8208. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8209. minus tag mm
  8210. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8211. orterms term orlist re-p str-p level-p level-op time-p
  8212. prop-p pn pv po cat-p gv rest)
  8213. (if (string-match "/+" match)
  8214. ;; match contains also a todo-matching request
  8215. (progn
  8216. (setq tagsmatch (substring match 0 (match-beginning 0))
  8217. todomatch (substring match (match-end 0)))
  8218. (if (string-match "^!" todomatch)
  8219. (setq todo-only t todomatch (substring todomatch 1)))
  8220. (if (string-match "^\\s-*$" todomatch)
  8221. (setq todomatch nil)))
  8222. ;; only matching tags
  8223. (setq tagsmatch match todomatch nil))
  8224. ;; Make the tags matcher
  8225. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8226. (setq tagsmatcher t)
  8227. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8228. (while (setq term (pop orterms))
  8229. (while (and (equal (substring term -1) "\\") orterms)
  8230. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8231. (while (string-match re term)
  8232. (setq rest (substring term (match-end 0))
  8233. minus (and (match-end 1)
  8234. (equal (match-string 1 term) "-"))
  8235. tag (match-string 2 term)
  8236. re-p (equal (string-to-char tag) ?{)
  8237. level-p (match-end 4)
  8238. prop-p (match-end 5)
  8239. mm (cond
  8240. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8241. (level-p
  8242. (setq level-op (org-op-to-function (match-string 3 term)))
  8243. `(,level-op level ,(string-to-number
  8244. (match-string 4 term))))
  8245. (prop-p
  8246. (setq pn (match-string 5 term)
  8247. po (match-string 6 term)
  8248. pv (match-string 7 term)
  8249. cat-p (equal pn "CATEGORY")
  8250. re-p (equal (string-to-char pv) ?{)
  8251. str-p (equal (string-to-char pv) ?\")
  8252. time-p (save-match-data
  8253. (string-match "^\"[[<].*[]>]\"$" pv))
  8254. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8255. (if time-p (setq pv (org-matcher-time pv)))
  8256. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8257. (cond
  8258. ((equal pn "CATEGORY")
  8259. (setq gv '(get-text-property (point) 'org-category)))
  8260. ((equal pn "TODO")
  8261. (setq gv 'todo))
  8262. (t
  8263. (setq gv `(org-cached-entry-get nil ,pn))))
  8264. (if re-p
  8265. (if (eq po 'org<>)
  8266. `(not (string-match ,pv (or ,gv "")))
  8267. `(string-match ,pv (or ,gv "")))
  8268. (if str-p
  8269. `(,po (or ,gv "") ,pv)
  8270. `(,po (string-to-number (or ,gv ""))
  8271. ,(string-to-number pv) ))))
  8272. (t `(member ,(downcase tag) tags-list)))
  8273. mm (if minus (list 'not mm) mm)
  8274. term rest)
  8275. (push mm tagsmatcher))
  8276. (push (if (> (length tagsmatcher) 1)
  8277. (cons 'and tagsmatcher)
  8278. (car tagsmatcher))
  8279. orlist)
  8280. (setq tagsmatcher nil))
  8281. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8282. (setq tagsmatcher
  8283. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8284. ;; Make the todo matcher
  8285. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8286. (setq todomatcher t)
  8287. (setq orterms (org-split-string todomatch "|") orlist nil)
  8288. (while (setq term (pop orterms))
  8289. (while (string-match re term)
  8290. (setq minus (and (match-end 1)
  8291. (equal (match-string 1 term) "-"))
  8292. kwd (match-string 2 term)
  8293. re-p (equal (string-to-char kwd) ?{)
  8294. term (substring term (match-end 0))
  8295. mm (if re-p
  8296. `(string-match ,(substring kwd 1 -1) todo)
  8297. (list 'equal 'todo kwd))
  8298. mm (if minus (list 'not mm) mm))
  8299. (push mm todomatcher))
  8300. (push (if (> (length todomatcher) 1)
  8301. (cons 'and todomatcher)
  8302. (car todomatcher))
  8303. orlist)
  8304. (setq todomatcher nil))
  8305. (setq todomatcher (if (> (length orlist) 1)
  8306. (cons 'or orlist) (car orlist))))
  8307. ;; Return the string and lisp forms of the matcher
  8308. (setq matcher (if todomatcher
  8309. (list 'and tagsmatcher todomatcher)
  8310. tagsmatcher))
  8311. (cons match0 matcher)))
  8312. (defun org-op-to-function (op &optional stringp)
  8313. "Turn an operator into the appropriate function."
  8314. (setq op
  8315. (cond
  8316. ((equal op "<" ) '(< string< org-time<))
  8317. ((equal op ">" ) '(> org-string> org-time>))
  8318. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8319. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8320. ((member op '("=" "==")) '(= string= org-time=))
  8321. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8322. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8323. (defun org<> (a b) (not (= a b)))
  8324. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8325. (defun org-string>= (a b) (not (string< a b)))
  8326. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8327. (defun org-string<> (a b) (not (string= a b)))
  8328. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8329. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8330. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8331. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8332. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8333. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8334. (defun org-2ft (s)
  8335. "Convert S to a floating point time.
  8336. If S is already a number, just return it. If it is a string, parse
  8337. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8338. (cond
  8339. ((numberp s) s)
  8340. ((stringp s)
  8341. (condition-case nil
  8342. (float-time (apply 'encode-time (org-parse-time-string s)))
  8343. (error 0.)))
  8344. (t 0.)))
  8345. (defun org-matcher-time (s)
  8346. (cond
  8347. ((equal s "<now>") (float-time))
  8348. ((equal s "<today>")
  8349. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8350. (t (org-2ft s))))
  8351. (defun org-match-any-p (re list)
  8352. "Does re match any element of list?"
  8353. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8354. (delq nil list))
  8355. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8356. (defvar org-tags-overlay (org-make-overlay 1 1))
  8357. (org-detach-overlay org-tags-overlay)
  8358. (defun org-get-local-tags-at (&optional pos)
  8359. "Get a list of tags defined in the current headline."
  8360. (org-get-tags-at pos 'local))
  8361. (defun org-get-local-tags ()
  8362. "Get a list of tags defined in the current headline."
  8363. (org-get-tags-at nil 'local))
  8364. (defun org-get-tags-at (&optional pos local)
  8365. "Get a list of all headline tags applicable at POS.
  8366. POS defaults to point. If tags are inherited, the list contains
  8367. the targets in the same sequence as the headlines appear, i.e.
  8368. the tags of the current headline come last.
  8369. When LOCAL is non-nil, only return tags from the current headline,
  8370. ignore inherited ones."
  8371. (interactive)
  8372. (let (tags ltags lastpos parent)
  8373. (save-excursion
  8374. (save-restriction
  8375. (widen)
  8376. (goto-char (or pos (point)))
  8377. (save-match-data
  8378. (catch 'done
  8379. (condition-case nil
  8380. (progn
  8381. (org-back-to-heading t)
  8382. (while (not (equal lastpos (point)))
  8383. (setq lastpos (point))
  8384. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8385. (setq ltags (org-split-string
  8386. (org-match-string-no-properties 1) ":"))
  8387. (setq tags (append
  8388. (if parent
  8389. (org-remove-uniherited-tags ltags)
  8390. ltags)
  8391. tags)))
  8392. (or org-use-tag-inheritance (throw 'done t))
  8393. (if local (throw 'done t))
  8394. (org-up-heading-all 1)
  8395. (setq parent t)))
  8396. (error nil)))))
  8397. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8398. (defun org-toggle-tag (tag &optional onoff)
  8399. "Toggle the tag TAG for the current line.
  8400. If ONOFF is `on' or `off', don't toggle but set to this state."
  8401. (unless (org-on-heading-p t) (error "Not on headling"))
  8402. (let (res current)
  8403. (save-excursion
  8404. (beginning-of-line)
  8405. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8406. (point-at-eol) t)
  8407. (progn
  8408. (setq current (match-string 1))
  8409. (replace-match ""))
  8410. (setq current ""))
  8411. (setq current (nreverse (org-split-string current ":")))
  8412. (cond
  8413. ((eq onoff 'on)
  8414. (setq res t)
  8415. (or (member tag current) (push tag current)))
  8416. ((eq onoff 'off)
  8417. (or (not (member tag current)) (setq current (delete tag current))))
  8418. (t (if (member tag current)
  8419. (setq current (delete tag current))
  8420. (setq res t)
  8421. (push tag current))))
  8422. (end-of-line 1)
  8423. (if current
  8424. (progn
  8425. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8426. (org-set-tags nil t))
  8427. (delete-horizontal-space))
  8428. (run-hooks 'org-after-tags-change-hook))
  8429. res))
  8430. (defun org-align-tags-here (to-col)
  8431. ;; Assumes that this is a headline
  8432. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8433. (beginning-of-line 1)
  8434. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8435. (< pos (match-beginning 2)))
  8436. (progn
  8437. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8438. (goto-char (match-beginning 1))
  8439. (insert " ")
  8440. (delete-region (point) (1+ (match-beginning 2)))
  8441. (setq ncol (max (1+ (current-column))
  8442. (1+ col)
  8443. (if (> to-col 0)
  8444. to-col
  8445. (- (abs to-col) tags-l))))
  8446. (setq p (point))
  8447. (insert (make-string (- ncol (current-column)) ?\ ))
  8448. (setq ncol (current-column))
  8449. (when indent-tabs-mode (tabify p (point-at-eol)))
  8450. (org-move-to-column (min ncol col) t))
  8451. (goto-char pos))))
  8452. (defun org-set-tags-command (&optional arg just-align)
  8453. "Call the set-tags command for the current entry."
  8454. (interactive "P")
  8455. (if (org-on-heading-p)
  8456. (org-set-tags arg just-align)
  8457. (save-excursion
  8458. (org-back-to-heading t)
  8459. (org-set-tags arg just-align))))
  8460. (defun org-set-tags (&optional arg just-align)
  8461. "Set the tags for the current headline.
  8462. With prefix ARG, realign all tags in headings in the current buffer."
  8463. (interactive "P")
  8464. (let* ((re (concat "^" outline-regexp))
  8465. (current (org-get-tags-string))
  8466. (col (current-column))
  8467. (org-setting-tags t)
  8468. table current-tags inherited-tags ; computed below when needed
  8469. tags p0 c0 c1 rpl)
  8470. (if arg
  8471. (save-excursion
  8472. (goto-char (point-min))
  8473. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8474. (while (re-search-forward re nil t)
  8475. (org-set-tags nil t)
  8476. (end-of-line 1)))
  8477. (message "All tags realigned to column %d" org-tags-column))
  8478. (if just-align
  8479. (setq tags current)
  8480. ;; Get a new set of tags from the user
  8481. (save-excursion
  8482. (setq table (or org-tag-alist (org-get-buffer-tags))
  8483. org-last-tags-completion-table table
  8484. current-tags (org-split-string current ":")
  8485. inherited-tags (nreverse
  8486. (nthcdr (length current-tags)
  8487. (nreverse (org-get-tags-at))))
  8488. tags
  8489. (if (or (eq t org-use-fast-tag-selection)
  8490. (and org-use-fast-tag-selection
  8491. (delq nil (mapcar 'cdr table))))
  8492. (org-fast-tag-selection
  8493. current-tags inherited-tags table
  8494. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8495. (let ((org-add-colon-after-tag-completion t))
  8496. (org-trim
  8497. (org-without-partial-completion
  8498. (completing-read "Tags: " 'org-tags-completion-function
  8499. nil nil current 'org-tags-history)))))))
  8500. (while (string-match "[-+&]+" tags)
  8501. ;; No boolean logic, just a list
  8502. (setq tags (replace-match ":" t t tags))))
  8503. (if (string-match "\\`[\t ]*\\'" tags)
  8504. (setq tags "")
  8505. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8506. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8507. ;; Insert new tags at the correct column
  8508. (beginning-of-line 1)
  8509. (cond
  8510. ((and (equal current "") (equal tags "")))
  8511. ((re-search-forward
  8512. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8513. (point-at-eol) t)
  8514. (if (equal tags "")
  8515. (setq rpl "")
  8516. (goto-char (match-beginning 0))
  8517. (setq c0 (current-column) p0 (point)
  8518. c1 (max (1+ c0) (if (> org-tags-column 0)
  8519. org-tags-column
  8520. (- (- org-tags-column) (length tags))))
  8521. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8522. (replace-match rpl t t)
  8523. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8524. tags)
  8525. (t (error "Tags alignment failed")))
  8526. (org-move-to-column col)
  8527. (unless just-align
  8528. (run-hooks 'org-after-tags-change-hook)))))
  8529. (defun org-change-tag-in-region (beg end tag off)
  8530. "Add or remove TAG for each entry in the region.
  8531. This works in the agenda, and also in an org-mode buffer."
  8532. (interactive
  8533. (list (region-beginning) (region-end)
  8534. (let ((org-last-tags-completion-table
  8535. (if (org-mode-p)
  8536. (org-get-buffer-tags)
  8537. (org-global-tags-completion-table))))
  8538. (completing-read
  8539. "Tag: " 'org-tags-completion-function nil nil nil
  8540. 'org-tags-history))
  8541. (progn
  8542. (message "[s]et or [r]emove? ")
  8543. (equal (read-char-exclusive) ?r))))
  8544. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8545. (let ((agendap (equal major-mode 'org-agenda-mode))
  8546. l1 l2 m buf pos newhead (cnt 0))
  8547. (goto-char end)
  8548. (setq l2 (1- (org-current-line)))
  8549. (goto-char beg)
  8550. (setq l1 (org-current-line))
  8551. (loop for l from l1 to l2 do
  8552. (goto-line l)
  8553. (setq m (get-text-property (point) 'org-hd-marker))
  8554. (when (or (and (org-mode-p) (org-on-heading-p))
  8555. (and agendap m))
  8556. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8557. pos (if agendap m (point)))
  8558. (with-current-buffer buf
  8559. (save-excursion
  8560. (save-restriction
  8561. (goto-char pos)
  8562. (setq cnt (1+ cnt))
  8563. (org-toggle-tag tag (if off 'off 'on))
  8564. (setq newhead (org-get-heading)))))
  8565. (and agendap (org-agenda-change-all-lines newhead m))))
  8566. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8567. (defun org-tags-completion-function (string predicate &optional flag)
  8568. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8569. (confirm (lambda (x) (stringp (car x)))))
  8570. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8571. (setq s1 (match-string 1 string)
  8572. s2 (match-string 2 string))
  8573. (setq s1 "" s2 string))
  8574. (cond
  8575. ((eq flag nil)
  8576. ;; try completion
  8577. (setq rtn (try-completion s2 ctable confirm))
  8578. (if (stringp rtn)
  8579. (setq rtn
  8580. (concat s1 s2 (substring rtn (length s2))
  8581. (if (and org-add-colon-after-tag-completion
  8582. (assoc rtn ctable))
  8583. ":" ""))))
  8584. rtn)
  8585. ((eq flag t)
  8586. ;; all-completions
  8587. (all-completions s2 ctable confirm)
  8588. )
  8589. ((eq flag 'lambda)
  8590. ;; exact match?
  8591. (assoc s2 ctable)))
  8592. ))
  8593. (defun org-fast-tag-insert (kwd tags face &optional end)
  8594. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8595. (insert (format "%-12s" (concat kwd ":"))
  8596. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8597. (or end "")))
  8598. (defun org-fast-tag-show-exit (flag)
  8599. (save-excursion
  8600. (goto-line 3)
  8601. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8602. (replace-match ""))
  8603. (when flag
  8604. (end-of-line 1)
  8605. (org-move-to-column (- (window-width) 19) t)
  8606. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8607. (defun org-set-current-tags-overlay (current prefix)
  8608. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8609. (if (featurep 'xemacs)
  8610. (org-overlay-display org-tags-overlay (concat prefix s)
  8611. 'secondary-selection)
  8612. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8613. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8614. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8615. "Fast tag selection with single keys.
  8616. CURRENT is the current list of tags in the headline, INHERITED is the
  8617. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8618. possibly with grouping information. TODO-TABLE is a similar table with
  8619. TODO keywords, should these have keys assigned to them.
  8620. If the keys are nil, a-z are automatically assigned.
  8621. Returns the new tags string, or nil to not change the current settings."
  8622. (let* ((fulltable (append table todo-table))
  8623. (maxlen (apply 'max (mapcar
  8624. (lambda (x)
  8625. (if (stringp (car x)) (string-width (car x)) 0))
  8626. fulltable)))
  8627. (buf (current-buffer))
  8628. (expert (eq org-fast-tag-selection-single-key 'expert))
  8629. (buffer-tags nil)
  8630. (fwidth (+ maxlen 3 1 3))
  8631. (ncol (/ (- (window-width) 4) fwidth))
  8632. (i-face 'org-done)
  8633. (c-face 'org-todo)
  8634. tg cnt e c char c1 c2 ntable tbl rtn
  8635. ov-start ov-end ov-prefix
  8636. (exit-after-next org-fast-tag-selection-single-key)
  8637. (done-keywords org-done-keywords)
  8638. groups ingroup)
  8639. (save-excursion
  8640. (beginning-of-line 1)
  8641. (if (looking-at
  8642. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8643. (setq ov-start (match-beginning 1)
  8644. ov-end (match-end 1)
  8645. ov-prefix "")
  8646. (setq ov-start (1- (point-at-eol))
  8647. ov-end (1+ ov-start))
  8648. (skip-chars-forward "^\n\r")
  8649. (setq ov-prefix
  8650. (concat
  8651. (buffer-substring (1- (point)) (point))
  8652. (if (> (current-column) org-tags-column)
  8653. " "
  8654. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8655. (org-move-overlay org-tags-overlay ov-start ov-end)
  8656. (save-window-excursion
  8657. (if expert
  8658. (set-buffer (get-buffer-create " *Org tags*"))
  8659. (delete-other-windows)
  8660. (split-window-vertically)
  8661. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8662. (erase-buffer)
  8663. (org-set-local 'org-done-keywords done-keywords)
  8664. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8665. (org-fast-tag-insert "Current" current c-face "\n\n")
  8666. (org-fast-tag-show-exit exit-after-next)
  8667. (org-set-current-tags-overlay current ov-prefix)
  8668. (setq tbl fulltable char ?a cnt 0)
  8669. (while (setq e (pop tbl))
  8670. (cond
  8671. ((equal e '(:startgroup))
  8672. (push '() groups) (setq ingroup t)
  8673. (when (not (= cnt 0))
  8674. (setq cnt 0)
  8675. (insert "\n"))
  8676. (insert "{ "))
  8677. ((equal e '(:endgroup))
  8678. (setq ingroup nil cnt 0)
  8679. (insert "}\n"))
  8680. (t
  8681. (setq tg (car e) c2 nil)
  8682. (if (cdr e)
  8683. (setq c (cdr e))
  8684. ;; automatically assign a character.
  8685. (setq c1 (string-to-char
  8686. (downcase (substring
  8687. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8688. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8689. (while (or (rassoc char ntable) (rassoc char table))
  8690. (setq char (1+ char)))
  8691. (setq c2 c1))
  8692. (setq c (or c2 char)))
  8693. (if ingroup (push tg (car groups)))
  8694. (setq tg (org-add-props tg nil 'face
  8695. (cond
  8696. ((not (assoc tg table))
  8697. (org-get-todo-face tg))
  8698. ((member tg current) c-face)
  8699. ((member tg inherited) i-face)
  8700. (t nil))))
  8701. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8702. (insert "[" c "] " tg (make-string
  8703. (- fwidth 4 (length tg)) ?\ ))
  8704. (push (cons tg c) ntable)
  8705. (when (= (setq cnt (1+ cnt)) ncol)
  8706. (insert "\n")
  8707. (if ingroup (insert " "))
  8708. (setq cnt 0)))))
  8709. (setq ntable (nreverse ntable))
  8710. (insert "\n")
  8711. (goto-char (point-min))
  8712. (if (not expert) (org-fit-window-to-buffer))
  8713. (setq rtn
  8714. (catch 'exit
  8715. (while t
  8716. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8717. (if groups " [!] no groups" " [!]groups")
  8718. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8719. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8720. (cond
  8721. ((= c ?\r) (throw 'exit t))
  8722. ((= c ?!)
  8723. (setq groups (not groups))
  8724. (goto-char (point-min))
  8725. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8726. ((= c ?\C-c)
  8727. (if (not expert)
  8728. (org-fast-tag-show-exit
  8729. (setq exit-after-next (not exit-after-next)))
  8730. (setq expert nil)
  8731. (delete-other-windows)
  8732. (split-window-vertically)
  8733. (org-switch-to-buffer-other-window " *Org tags*")
  8734. (org-fit-window-to-buffer)))
  8735. ((or (= c ?\C-g)
  8736. (and (= c ?q) (not (rassoc c ntable))))
  8737. (org-detach-overlay org-tags-overlay)
  8738. (setq quit-flag t))
  8739. ((= c ?\ )
  8740. (setq current nil)
  8741. (if exit-after-next (setq exit-after-next 'now)))
  8742. ((= c ?\t)
  8743. (condition-case nil
  8744. (setq tg (completing-read
  8745. "Tag: "
  8746. (or buffer-tags
  8747. (with-current-buffer buf
  8748. (org-get-buffer-tags)))))
  8749. (quit (setq tg "")))
  8750. (when (string-match "\\S-" tg)
  8751. (add-to-list 'buffer-tags (list tg))
  8752. (if (member tg current)
  8753. (setq current (delete tg current))
  8754. (push tg current)))
  8755. (if exit-after-next (setq exit-after-next 'now)))
  8756. ((setq e (rassoc c todo-table) tg (car e))
  8757. (with-current-buffer buf
  8758. (save-excursion (org-todo tg)))
  8759. (if exit-after-next (setq exit-after-next 'now)))
  8760. ((setq e (rassoc c ntable) tg (car e))
  8761. (if (member tg current)
  8762. (setq current (delete tg current))
  8763. (loop for g in groups do
  8764. (if (member tg g)
  8765. (mapc (lambda (x)
  8766. (setq current (delete x current)))
  8767. g)))
  8768. (push tg current))
  8769. (if exit-after-next (setq exit-after-next 'now))))
  8770. ;; Create a sorted list
  8771. (setq current
  8772. (sort current
  8773. (lambda (a b)
  8774. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8775. (if (eq exit-after-next 'now) (throw 'exit t))
  8776. (goto-char (point-min))
  8777. (beginning-of-line 2)
  8778. (delete-region (point) (point-at-eol))
  8779. (org-fast-tag-insert "Current" current c-face)
  8780. (org-set-current-tags-overlay current ov-prefix)
  8781. (while (re-search-forward
  8782. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8783. (setq tg (match-string 1))
  8784. (add-text-properties
  8785. (match-beginning 1) (match-end 1)
  8786. (list 'face
  8787. (cond
  8788. ((member tg current) c-face)
  8789. ((member tg inherited) i-face)
  8790. (t (get-text-property (match-beginning 1) 'face))))))
  8791. (goto-char (point-min)))))
  8792. (org-detach-overlay org-tags-overlay)
  8793. (if rtn
  8794. (mapconcat 'identity current ":")
  8795. nil))))
  8796. (defun org-get-tags-string ()
  8797. "Get the TAGS string in the current headline."
  8798. (unless (org-on-heading-p t)
  8799. (error "Not on a heading"))
  8800. (save-excursion
  8801. (beginning-of-line 1)
  8802. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8803. (org-match-string-no-properties 1)
  8804. "")))
  8805. (defun org-get-tags ()
  8806. "Get the list of tags specified in the current headline."
  8807. (org-split-string (org-get-tags-string) ":"))
  8808. (defun org-get-buffer-tags ()
  8809. "Get a table of all tags used in the buffer, for completion."
  8810. (let (tags)
  8811. (save-excursion
  8812. (goto-char (point-min))
  8813. (while (re-search-forward
  8814. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8815. (when (equal (char-after (point-at-bol 0)) ?*)
  8816. (mapc (lambda (x) (add-to-list 'tags x))
  8817. (org-split-string (org-match-string-no-properties 1) ":")))))
  8818. (mapcar 'list tags)))
  8819. ;;;; The mapping API
  8820. ;;;###autoload
  8821. (defun org-map-entries (func &optional match scope &rest skip)
  8822. "Call FUNC at each headline selected by MATCH in SCOPE.
  8823. FUNC is a function or a lisp form. The function will be called without
  8824. arguments, with the cursor positioned at the beginning of the headline.
  8825. The return values of all calls to the function will be collected and
  8826. returned as a list.
  8827. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8828. Only headlines that are matched by this query will be considered during
  8829. the iteration. When MATCH is nil or t, all headlines will be
  8830. visited by the iteration.
  8831. SCOPE determines the scope of this command. It can be any of:
  8832. nil The current buffer, respecting the restriction if any
  8833. tree The subtree started with the entry at point
  8834. file The current buffer, without restriction
  8835. file-with-archives
  8836. The current buffer, and any archives associated with it
  8837. agenda All agenda files
  8838. agenda-with-archives
  8839. All agenda files with any archive files associated with them
  8840. \(file1 file2 ...)
  8841. If this is a list, all files in the list will be scanned
  8842. The remaining args are treated as settings for the skipping facilities of
  8843. the scanner. The following items can be given here:
  8844. archive skip trees with the archive tag.
  8845. comment skip trees with the COMMENT keyword
  8846. function or Emacs Lisp form:
  8847. will be used as value for `org-agenda-skip-function', so whenever
  8848. the the function returns t, FUNC will not be called for that
  8849. entry and search will continue from the point where the
  8850. function leaves it."
  8851. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8852. (org-agenda-skip-archived-trees (memq 'archive skip))
  8853. (org-agenda-skip-comment-trees (memq 'comment skip))
  8854. (org-agenda-skip-function
  8855. (car (org-delete-all '(comment archive) skip)))
  8856. (org-tags-match-list-sublevels t)
  8857. matcher pos file
  8858. org-todo-keywords-for-agenda
  8859. org-done-keywords-for-agenda
  8860. org-todo-keyword-alist-for-agenda
  8861. org-tag-alist-for-agenda)
  8862. (cond
  8863. ((eq match t) (setq matcher t))
  8864. ((eq match nil) (setq matcher t))
  8865. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  8866. (when (eq scope 'tree)
  8867. (org-back-to-heading t)
  8868. (org-narrow-to-subtree)
  8869. (setq scope nil))
  8870. (if (not scope)
  8871. (progn
  8872. (org-prepare-agenda-buffers
  8873. (list (buffer-file-name (current-buffer))))
  8874. (org-scan-tags func matcher))
  8875. ;; Get the right scope
  8876. (setq pos (point))
  8877. (cond
  8878. ((and scope (listp scope) (symbolp (car scope)))
  8879. (setq scope (eval scope)))
  8880. ((eq scope 'agenda)
  8881. (setq scope (org-agenda-files t)))
  8882. ((eq scope 'agenda-with-archives)
  8883. (setq scope (org-agenda-files t))
  8884. (setq scope (org-add-archive-files scope)))
  8885. ((eq scope 'file)
  8886. (setq scope (list (buffer-file-name))))
  8887. ((eq scope 'file-with-archives)
  8888. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  8889. (org-prepare-agenda-buffers scope)
  8890. (while (setq file (pop scope))
  8891. (with-current-buffer (org-find-base-buffer-visiting file)
  8892. (save-excursion
  8893. (save-restriction
  8894. (widen)
  8895. (goto-char (point-min))
  8896. (org-scan-tags func matcher))))))))
  8897. ;;;; Properties
  8898. ;;; Setting and retrieving properties
  8899. (defconst org-special-properties
  8900. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  8901. "TIMESTAMP" "TIMESTAMP_IA")
  8902. "The special properties valid in Org-mode.
  8903. These are properties that are not defined in the property drawer,
  8904. but in some other way.")
  8905. (defconst org-default-properties
  8906. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8907. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8908. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8909. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8910. "Some properties that are used by Org-mode for various purposes.
  8911. Being in this list makes sure that they are offered for completion.")
  8912. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8913. "Regular expression matching the first line of a property drawer.")
  8914. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8915. "Regular expression matching the first line of a property drawer.")
  8916. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  8917. "Regular expression matching the first line of a property drawer.")
  8918. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  8919. "Regular expression matching the first line of a property drawer.")
  8920. (defconst org-property-drawer-re
  8921. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  8922. org-property-end-re "\\)\n?")
  8923. "Matches an entire property drawer.")
  8924. (defconst org-clock-drawer-re
  8925. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  8926. org-property-end-re "\\)\n?")
  8927. "Matches an entire clock drawer.")
  8928. (defun org-property-action ()
  8929. "Do an action on properties."
  8930. (interactive)
  8931. (let (c)
  8932. (org-at-property-p)
  8933. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8934. (setq c (read-char-exclusive))
  8935. (cond
  8936. ((equal c ?s)
  8937. (call-interactively 'org-set-property))
  8938. ((equal c ?d)
  8939. (call-interactively 'org-delete-property))
  8940. ((equal c ?D)
  8941. (call-interactively 'org-delete-property-globally))
  8942. ((equal c ?c)
  8943. (call-interactively 'org-compute-property-at-point))
  8944. (t (error "No such property action %c" c)))))
  8945. (defun org-at-property-p ()
  8946. "Is the cursor in a property line?"
  8947. ;; FIXME: Does not check if we are actually in the drawer.
  8948. ;; FIXME: also returns true on any drawers.....
  8949. ;; This is used by C-c C-c for property action.
  8950. (save-excursion
  8951. (beginning-of-line 1)
  8952. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8953. (defun org-get-property-block (&optional beg end force)
  8954. "Return the (beg . end) range of the body of the property drawer.
  8955. BEG and END can be beginning and end of subtree, if not given
  8956. they will be found.
  8957. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8958. (catch 'exit
  8959. (save-excursion
  8960. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8961. (end (or end (progn (outline-next-heading) (point)))))
  8962. (goto-char beg)
  8963. (if (re-search-forward org-property-start-re end t)
  8964. (setq beg (1+ (match-end 0)))
  8965. (if force
  8966. (save-excursion
  8967. (org-insert-property-drawer)
  8968. (setq end (progn (outline-next-heading) (point))))
  8969. (throw 'exit nil))
  8970. (goto-char beg)
  8971. (if (re-search-forward org-property-start-re end t)
  8972. (setq beg (1+ (match-end 0)))))
  8973. (if (re-search-forward org-property-end-re end t)
  8974. (setq end (match-beginning 0))
  8975. (or force (throw 'exit nil))
  8976. (goto-char beg)
  8977. (setq end beg)
  8978. (org-indent-line-function)
  8979. (insert ":END:\n"))
  8980. (cons beg end)))))
  8981. (defun org-entry-properties (&optional pom which)
  8982. "Get all properties of the entry at point-or-marker POM.
  8983. This includes the TODO keyword, the tags, time strings for deadline,
  8984. scheduled, and clocking, and any additional properties defined in the
  8985. entry. The return value is an alist, keys may occur multiple times
  8986. if the property key was used several times.
  8987. POM may also be nil, in which case the current entry is used.
  8988. If WHICH is nil or `all', get all properties. If WHICH is
  8989. `special' or `standard', only get that subclass."
  8990. (setq which (or which 'all))
  8991. (org-with-point-at pom
  8992. (let ((clockstr (substring org-clock-string 0 -1))
  8993. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8994. beg end range props sum-props key value string clocksum)
  8995. (save-excursion
  8996. (when (condition-case nil (org-back-to-heading t) (error nil))
  8997. (setq beg (point))
  8998. (setq sum-props (get-text-property (point) 'org-summaries))
  8999. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9000. (outline-next-heading)
  9001. (setq end (point))
  9002. (when (memq which '(all special))
  9003. ;; Get the special properties, like TODO and tags
  9004. (goto-char beg)
  9005. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9006. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9007. (when (looking-at org-priority-regexp)
  9008. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9009. (when (and (setq value (org-get-tags-string))
  9010. (string-match "\\S-" value))
  9011. (push (cons "TAGS" value) props))
  9012. (when (setq value (org-get-tags-at))
  9013. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9014. props))
  9015. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9016. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9017. string (if (equal key clockstr)
  9018. (org-no-properties
  9019. (org-trim
  9020. (buffer-substring
  9021. (match-beginning 3) (goto-char (point-at-eol)))))
  9022. (substring (org-match-string-no-properties 3) 1 -1)))
  9023. (unless key
  9024. (if (= (char-after (match-beginning 3)) ?\[)
  9025. (setq key "TIMESTAMP_IA")
  9026. (setq key "TIMESTAMP")))
  9027. (when (or (equal key clockstr) (not (assoc key props)))
  9028. (push (cons key string) props)))
  9029. )
  9030. (when (memq which '(all standard))
  9031. ;; Get the standard properties, like :PORP: ...
  9032. (setq range (org-get-property-block beg end))
  9033. (when range
  9034. (goto-char (car range))
  9035. (while (re-search-forward
  9036. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9037. (cdr range) t)
  9038. (setq key (org-match-string-no-properties 1)
  9039. value (org-trim (or (org-match-string-no-properties 2) "")))
  9040. (unless (member key excluded)
  9041. (push (cons key (or value "")) props)))))
  9042. (if clocksum
  9043. (push (cons "CLOCKSUM"
  9044. (org-columns-number-to-string (/ (float clocksum) 60.)
  9045. 'add_times))
  9046. props))
  9047. (unless (assoc "CATEGORY" props)
  9048. (setq value (or (org-get-category)
  9049. (progn (org-refresh-category-properties)
  9050. (org-get-category))))
  9051. (push (cons "CATEGORY" value) props))
  9052. (append sum-props (nreverse props)))))))
  9053. (defun org-entry-get (pom property &optional inherit)
  9054. "Get value of PROPERTY for entry at point-or-marker POM.
  9055. If INHERIT is non-nil and the entry does not have the property,
  9056. then also check higher levels of the hierarchy.
  9057. If INHERIT is the symbol `selective', use inheritance only if the setting
  9058. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9059. If the property is present but empty, the return value is the empty string.
  9060. If the property is not present at all, nil is returned."
  9061. (org-with-point-at pom
  9062. (if (and inherit (if (eq inherit 'selective)
  9063. (org-property-inherit-p property)
  9064. t))
  9065. (org-entry-get-with-inheritance property)
  9066. (if (member property org-special-properties)
  9067. ;; We need a special property. Use brute force, get all properties.
  9068. (cdr (assoc property (org-entry-properties nil 'special)))
  9069. (let ((range (org-get-property-block)))
  9070. (if (and range
  9071. (goto-char (car range))
  9072. (re-search-forward
  9073. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9074. (cdr range) t))
  9075. ;; Found the property, return it.
  9076. (if (match-end 1)
  9077. (org-match-string-no-properties 1)
  9078. "")))))))
  9079. (defun org-property-or-variable-value (var &optional inherit)
  9080. "Check if there is a property fixing the value of VAR.
  9081. If yes, return this value. If not, return the current value of the variable."
  9082. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9083. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9084. (read prop)
  9085. (symbol-value var))))
  9086. (defun org-entry-delete (pom property)
  9087. "Delete the property PROPERTY from entry at point-or-marker POM."
  9088. (org-with-point-at pom
  9089. (if (member property org-special-properties)
  9090. nil ; cannot delete these properties.
  9091. (let ((range (org-get-property-block)))
  9092. (if (and range
  9093. (goto-char (car range))
  9094. (re-search-forward
  9095. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9096. (cdr range) t))
  9097. (progn
  9098. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9099. t)
  9100. nil)))))
  9101. ;; Multi-values properties are properties that contain multiple values
  9102. ;; These values are assumed to be single words, separated by whitespace.
  9103. (defun org-entry-add-to-multivalued-property (pom property value)
  9104. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9105. (let* ((old (org-entry-get pom property))
  9106. (values (and old (org-split-string old "[ \t]"))))
  9107. (setq value (org-entry-protect-space value))
  9108. (unless (member value values)
  9109. (setq values (cons value values))
  9110. (org-entry-put pom property
  9111. (mapconcat 'identity values " ")))))
  9112. (defun org-entry-remove-from-multivalued-property (pom property value)
  9113. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9114. (let* ((old (org-entry-get pom property))
  9115. (values (and old (org-split-string old "[ \t]"))))
  9116. (setq value (org-entry-protect-space value))
  9117. (when (member value values)
  9118. (setq values (delete value values))
  9119. (org-entry-put pom property
  9120. (mapconcat 'identity values " ")))))
  9121. (defun org-entry-member-in-multivalued-property (pom property value)
  9122. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9123. (let* ((old (org-entry-get pom property))
  9124. (values (and old (org-split-string old "[ \t]"))))
  9125. (setq value (org-entry-protect-space value))
  9126. (member value values)))
  9127. (defun org-entry-get-multivalued-property (pom property)
  9128. "Return a list of values in a multivalued property."
  9129. (let* ((value (org-entry-get pom property))
  9130. (values (and value (org-split-string value "[ \t]"))))
  9131. (mapcar 'org-entry-restore-space values)))
  9132. (defun org-entry-put-multivalued-property (pom property &rest values)
  9133. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9134. VALUES should be a list of strings. Spaces will be protected."
  9135. (org-entry-put pom property
  9136. (mapconcat 'org-entry-protect-space values " "))
  9137. (let* ((value (org-entry-get pom property))
  9138. (values (and value (org-split-string value "[ \t]"))))
  9139. (mapcar 'org-entry-restore-space values)))
  9140. (defun org-entry-protect-space (s)
  9141. "Protect spaces and newline in string S."
  9142. (while (string-match " " s)
  9143. (setq s (replace-match "%20" t t s)))
  9144. (while (string-match "\n" s)
  9145. (setq s (replace-match "%0A" t t s)))
  9146. s)
  9147. (defun org-entry-restore-space (s)
  9148. "Restore spaces and newline in string S."
  9149. (while (string-match "%20" s)
  9150. (setq s (replace-match " " t t s)))
  9151. (while (string-match "%0A" s)
  9152. (setq s (replace-match "\n" t t s)))
  9153. s)
  9154. (defvar org-entry-property-inherited-from (make-marker)
  9155. "Marker pointing to the entry from where a proerty was inherited.
  9156. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9157. location of the entry where the inheriance search matched. If there was
  9158. no match, the marker will point nowhere.
  9159. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9160. is set.")
  9161. (defun org-entry-get-with-inheritance (property)
  9162. "Get entry property, and search higher levels if not present."
  9163. (move-marker org-entry-property-inherited-from nil)
  9164. (let (tmp)
  9165. (save-excursion
  9166. (save-restriction
  9167. (widen)
  9168. (catch 'ex
  9169. (while t
  9170. (when (setq tmp (org-entry-get nil property))
  9171. (org-back-to-heading t)
  9172. (move-marker org-entry-property-inherited-from (point))
  9173. (throw 'ex tmp))
  9174. (or (org-up-heading-safe) (throw 'ex nil)))))
  9175. (or tmp
  9176. (cdr (assoc property org-file-properties))
  9177. (cdr (assoc property org-global-properties))
  9178. (cdr (assoc property org-global-properties-fixed))))))
  9179. (defun org-entry-put (pom property value)
  9180. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9181. (org-with-point-at pom
  9182. (org-back-to-heading t)
  9183. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9184. range)
  9185. (cond
  9186. ((equal property "TODO")
  9187. (when (and (stringp value) (string-match "\\S-" value)
  9188. (not (member value org-todo-keywords-1)))
  9189. (error "\"%s\" is not a valid TODO state" value))
  9190. (if (or (not value)
  9191. (not (string-match "\\S-" value)))
  9192. (setq value 'none))
  9193. (org-todo value)
  9194. (org-set-tags nil 'align))
  9195. ((equal property "PRIORITY")
  9196. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9197. (string-to-char value) ?\ ))
  9198. (org-set-tags nil 'align))
  9199. ((equal property "SCHEDULED")
  9200. (if (re-search-forward org-scheduled-time-regexp end t)
  9201. (cond
  9202. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9203. ((eq value 'later) (org-timestamp-change 1 'day))
  9204. (t (call-interactively 'org-schedule)))
  9205. (call-interactively 'org-schedule)))
  9206. ((equal property "DEADLINE")
  9207. (if (re-search-forward org-deadline-time-regexp end t)
  9208. (cond
  9209. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9210. ((eq value 'later) (org-timestamp-change 1 'day))
  9211. (t (call-interactively 'org-deadline)))
  9212. (call-interactively 'org-deadline)))
  9213. ((member property org-special-properties)
  9214. (error "The %s property can not yet be set with `org-entry-put'"
  9215. property))
  9216. (t ; a non-special property
  9217. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9218. (setq range (org-get-property-block beg end 'force))
  9219. (goto-char (car range))
  9220. (if (re-search-forward
  9221. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9222. (progn
  9223. (delete-region (match-beginning 1) (match-end 1))
  9224. (goto-char (match-beginning 1)))
  9225. (goto-char (cdr range))
  9226. (insert "\n")
  9227. (backward-char 1)
  9228. (org-indent-line-function)
  9229. (insert ":" property ":"))
  9230. (and value (insert " " value))
  9231. (org-indent-line-function)))))))
  9232. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9233. "Get all property keys in the current buffer.
  9234. With INCLUDE-SPECIALS, also list the special properties that relect things
  9235. like tags and TODO state.
  9236. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9237. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9238. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9239. formats in the current buffer."
  9240. (let (rtn range cfmt cols s p)
  9241. (save-excursion
  9242. (save-restriction
  9243. (widen)
  9244. (goto-char (point-min))
  9245. (while (re-search-forward org-property-start-re nil t)
  9246. (setq range (org-get-property-block))
  9247. (goto-char (car range))
  9248. (while (re-search-forward
  9249. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9250. (cdr range) t)
  9251. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9252. (outline-next-heading))))
  9253. (when include-specials
  9254. (setq rtn (append org-special-properties rtn)))
  9255. (when include-defaults
  9256. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9257. (when include-columns
  9258. (save-excursion
  9259. (save-restriction
  9260. (widen)
  9261. (goto-char (point-min))
  9262. (while (re-search-forward
  9263. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9264. nil t)
  9265. (setq cfmt (match-string 2) s 0)
  9266. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9267. cfmt s)
  9268. (setq s (match-end 0)
  9269. p (match-string 1 cfmt))
  9270. (unless (or (equal p "ITEM")
  9271. (member p org-special-properties))
  9272. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9273. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9274. (defun org-property-values (key)
  9275. "Return a list of all values of property KEY."
  9276. (save-excursion
  9277. (save-restriction
  9278. (widen)
  9279. (goto-char (point-min))
  9280. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9281. values)
  9282. (while (re-search-forward re nil t)
  9283. (add-to-list 'values (org-trim (match-string 1))))
  9284. (delete "" values)))))
  9285. (defun org-insert-property-drawer ()
  9286. "Insert a property drawer into the current entry."
  9287. (interactive)
  9288. (org-back-to-heading t)
  9289. (looking-at outline-regexp)
  9290. (let ((indent (- (match-end 0)(match-beginning 0)))
  9291. (beg (point))
  9292. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9293. end hiddenp)
  9294. (outline-next-heading)
  9295. (setq end (point))
  9296. (goto-char beg)
  9297. (while (re-search-forward re end t))
  9298. (setq hiddenp (org-invisible-p))
  9299. (end-of-line 1)
  9300. (and (equal (char-after) ?\n) (forward-char 1))
  9301. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9302. (beginning-of-line 2))
  9303. (org-skip-over-state-notes)
  9304. (skip-chars-backward " \t\n\r")
  9305. (if (eq (char-before) ?*) (forward-char 1))
  9306. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9307. (beginning-of-line 0)
  9308. (org-indent-to-column indent)
  9309. (beginning-of-line 2)
  9310. (org-indent-to-column indent)
  9311. (beginning-of-line 0)
  9312. (if hiddenp
  9313. (save-excursion
  9314. (org-back-to-heading t)
  9315. (hide-entry))
  9316. (org-flag-drawer t))))
  9317. (defun org-set-property (property value)
  9318. "In the current entry, set PROPERTY to VALUE.
  9319. When called interactively, this will prompt for a property name, offering
  9320. completion on existing and default properties. And then it will prompt
  9321. for a value, offering competion either on allowed values (via an inherited
  9322. xxx_ALL property) or on existing values in other instances of this property
  9323. in the current file."
  9324. (interactive
  9325. (let* ((completion-ignore-case t)
  9326. (keys (org-buffer-property-keys nil t t))
  9327. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9328. (prop (if (member prop0 keys)
  9329. prop0
  9330. (or (cdr (assoc (downcase prop0)
  9331. (mapcar (lambda (x) (cons (downcase x) x))
  9332. keys)))
  9333. prop0)))
  9334. (cur (org-entry-get nil prop))
  9335. (allowed (org-property-get-allowed-values nil prop 'table))
  9336. (existing (mapcar 'list (org-property-values prop)))
  9337. (val (if allowed
  9338. (org-completing-read "Value: " allowed nil 'req-match)
  9339. (org-completing-read
  9340. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9341. (concat "[" cur "]") "")
  9342. ": ")
  9343. existing nil nil "" nil cur))))
  9344. (list prop (if (equal val "") cur val))))
  9345. (unless (equal (org-entry-get nil property) value)
  9346. (org-entry-put nil property value)))
  9347. (defun org-delete-property (property)
  9348. "In the current entry, delete PROPERTY."
  9349. (interactive
  9350. (let* ((completion-ignore-case t)
  9351. (prop (completing-read
  9352. "Property: " (org-entry-properties nil 'standard))))
  9353. (list prop)))
  9354. (message "Property %s %s" property
  9355. (if (org-entry-delete nil property)
  9356. "deleted"
  9357. "was not present in the entry")))
  9358. (defun org-delete-property-globally (property)
  9359. "Remove PROPERTY globally, from all entries."
  9360. (interactive
  9361. (let* ((completion-ignore-case t)
  9362. (prop (completing-read
  9363. "Globally remove property: "
  9364. (mapcar 'list (org-buffer-property-keys)))))
  9365. (list prop)))
  9366. (save-excursion
  9367. (save-restriction
  9368. (widen)
  9369. (goto-char (point-min))
  9370. (let ((cnt 0))
  9371. (while (re-search-forward
  9372. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9373. nil t)
  9374. (setq cnt (1+ cnt))
  9375. (replace-match ""))
  9376. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9377. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9378. (defun org-compute-property-at-point ()
  9379. "Compute the property at point.
  9380. This looks for an enclosing column format, extracts the operator and
  9381. then applies it to the proerty in the column format's scope."
  9382. (interactive)
  9383. (unless (org-at-property-p)
  9384. (error "Not at a property"))
  9385. (let ((prop (org-match-string-no-properties 2)))
  9386. (org-columns-get-format-and-top-level)
  9387. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9388. (error "No operator defined for property %s" prop))
  9389. (org-columns-compute prop)))
  9390. (defun org-property-get-allowed-values (pom property &optional table)
  9391. "Get allowed values for the property PROPERTY.
  9392. When TABLE is non-nil, return an alist that can directly be used for
  9393. completion."
  9394. (let (vals)
  9395. (cond
  9396. ((equal property "TODO")
  9397. (setq vals (org-with-point-at pom
  9398. (append org-todo-keywords-1 '("")))))
  9399. ((equal property "PRIORITY")
  9400. (let ((n org-lowest-priority))
  9401. (while (>= n org-highest-priority)
  9402. (push (char-to-string n) vals)
  9403. (setq n (1- n)))))
  9404. ((member property org-special-properties))
  9405. (t
  9406. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9407. (when (and vals (string-match "\\S-" vals))
  9408. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9409. (setq vals (mapcar (lambda (x)
  9410. (cond ((stringp x) x)
  9411. ((numberp x) (number-to-string x))
  9412. ((symbolp x) (symbol-name x))
  9413. (t "???")))
  9414. vals)))))
  9415. (if table (mapcar 'list vals) vals)))
  9416. (defun org-property-previous-allowed-value (&optional previous)
  9417. "Switch to the next allowed value for this property."
  9418. (interactive)
  9419. (org-property-next-allowed-value t))
  9420. (defun org-property-next-allowed-value (&optional previous)
  9421. "Switch to the next allowed value for this property."
  9422. (interactive)
  9423. (unless (org-at-property-p)
  9424. (error "Not at a property"))
  9425. (let* ((key (match-string 2))
  9426. (value (match-string 3))
  9427. (allowed (or (org-property-get-allowed-values (point) key)
  9428. (and (member value '("[ ]" "[-]" "[X]"))
  9429. '("[ ]" "[X]"))))
  9430. nval)
  9431. (unless allowed
  9432. (error "Allowed values for this property have not been defined"))
  9433. (if previous (setq allowed (reverse allowed)))
  9434. (if (member value allowed)
  9435. (setq nval (car (cdr (member value allowed)))))
  9436. (setq nval (or nval (car allowed)))
  9437. (if (equal nval value)
  9438. (error "Only one allowed value for this property"))
  9439. (org-at-property-p)
  9440. (replace-match (concat " :" key ": " nval) t t)
  9441. (org-indent-line-function)
  9442. (beginning-of-line 1)
  9443. (skip-chars-forward " \t")))
  9444. (defun org-find-entry-with-id (ident)
  9445. "Locate the entry that contains the ID property with exact value IDENT.
  9446. IDENT can be a string, a symbol or a number, this function will search for
  9447. the string representation of it.
  9448. Return the position where this entry starts, or nil if there is no such entry."
  9449. (let ((id (cond
  9450. ((stringp ident) ident)
  9451. ((symbol-name ident) (symbol-name ident))
  9452. ((numberp ident) (number-to-string ident))
  9453. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9454. (case-fold-search nil))
  9455. (save-excursion
  9456. (save-restriction
  9457. (widen)
  9458. (goto-char (point-min))
  9459. (when (re-search-forward
  9460. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9461. nil t)
  9462. (org-back-to-heading)
  9463. (point))))))
  9464. ;;;; Timestamps
  9465. (defvar org-last-changed-timestamp nil)
  9466. (defvar org-last-inserted-timestamp nil
  9467. "The last time stamp inserted with `org-insert-time-stamp'.")
  9468. (defvar org-time-was-given) ; dynamically scoped parameter
  9469. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9470. (defvar org-ts-what) ; dynamically scoped parameter
  9471. (defun org-time-stamp (arg &optional inactive)
  9472. "Prompt for a date/time and insert a time stamp.
  9473. If the user specifies a time like HH:MM, or if this command is called
  9474. with a prefix argument, the time stamp will contain date and time.
  9475. Otherwise, only the date will be included. All parts of a date not
  9476. specified by the user will be filled in from the current date/time.
  9477. So if you press just return without typing anything, the time stamp
  9478. will represent the current date/time. If there is already a timestamp
  9479. at the cursor, it will be modified."
  9480. (interactive "P")
  9481. (let* ((ts nil)
  9482. (default-time
  9483. ;; Default time is either today, or, when entering a range,
  9484. ;; the range start.
  9485. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9486. (save-excursion
  9487. (re-search-backward
  9488. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9489. (- (point) 20) t)))
  9490. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9491. (current-time)))
  9492. (default-input (and ts (org-get-compact-tod ts)))
  9493. org-time-was-given org-end-time-was-given time)
  9494. (cond
  9495. ((and (org-at-timestamp-p t)
  9496. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9497. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9498. (insert "--")
  9499. (setq time (let ((this-command this-command))
  9500. (org-read-date arg 'totime nil nil
  9501. default-time default-input)))
  9502. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9503. ((org-at-timestamp-p t)
  9504. (setq time (let ((this-command this-command))
  9505. (org-read-date arg 'totime nil nil default-time default-input)))
  9506. (when (org-at-timestamp-p t) ; just to get the match data
  9507. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9508. (replace-match "")
  9509. (setq org-last-changed-timestamp
  9510. (org-insert-time-stamp
  9511. time (or org-time-was-given arg)
  9512. inactive nil nil (list org-end-time-was-given))))
  9513. (message "Timestamp updated"))
  9514. (t
  9515. (setq time (let ((this-command this-command))
  9516. (org-read-date arg 'totime nil nil default-time default-input)))
  9517. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9518. nil nil (list org-end-time-was-given))))))
  9519. ;; FIXME: can we use this for something else, like computing time differences?
  9520. (defun org-get-compact-tod (s)
  9521. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9522. (let* ((t1 (match-string 1 s))
  9523. (h1 (string-to-number (match-string 2 s)))
  9524. (m1 (string-to-number (match-string 3 s)))
  9525. (t2 (and (match-end 4) (match-string 5 s)))
  9526. (h2 (and t2 (string-to-number (match-string 6 s))))
  9527. (m2 (and t2 (string-to-number (match-string 7 s))))
  9528. dh dm)
  9529. (if (not t2)
  9530. t1
  9531. (setq dh (- h2 h1) dm (- m2 m1))
  9532. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9533. (concat t1 "+" (number-to-string dh)
  9534. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9535. (defun org-time-stamp-inactive (&optional arg)
  9536. "Insert an inactive time stamp.
  9537. An inactive time stamp is enclosed in square brackets instead of angle
  9538. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9539. does not link to the calendar and cannot be changed with the S-cursor keys.
  9540. So these are more for recording a certain time/date."
  9541. (interactive "P")
  9542. (org-time-stamp arg 'inactive))
  9543. (defvar org-date-ovl (org-make-overlay 1 1))
  9544. (org-overlay-put org-date-ovl 'face 'org-warning)
  9545. (org-detach-overlay org-date-ovl)
  9546. (defvar org-ans1) ; dynamically scoped parameter
  9547. (defvar org-ans2) ; dynamically scoped parameter
  9548. (defvar org-plain-time-of-day-regexp) ; defined below
  9549. (defvar org-overriding-default-time nil) ; dynamically scoped
  9550. (defvar org-read-date-overlay nil)
  9551. (defvar org-dcst nil) ; dynamically scoped
  9552. (defun org-read-date (&optional with-time to-time from-string prompt
  9553. default-time default-input)
  9554. "Read a date, possibly a time, and make things smooth for the user.
  9555. The prompt will suggest to enter an ISO date, but you can also enter anything
  9556. which will at least partially be understood by `parse-time-string'.
  9557. Unrecognized parts of the date will default to the current day, month, year,
  9558. hour and minute. If this command is called to replace a timestamp at point,
  9559. of to enter the second timestamp of a range, the default time is taken from the
  9560. existing stamp. For example,
  9561. 3-2-5 --> 2003-02-05
  9562. feb 15 --> currentyear-02-15
  9563. sep 12 9 --> 2009-09-12
  9564. 12:45 --> today 12:45
  9565. 22 sept 0:34 --> currentyear-09-22 0:34
  9566. 12 --> currentyear-currentmonth-12
  9567. Fri --> nearest Friday (today or later)
  9568. etc.
  9569. Furthermore you can specify a relative date by giving, as the *first* thing
  9570. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9571. change in days weeks, months, years.
  9572. With a single plus or minus, the date is relative to today. With a double
  9573. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9574. +4d --> four days from today
  9575. +4 --> same as above
  9576. +2w --> two weeks from today
  9577. ++5 --> five days from default date
  9578. The function understands only English month and weekday abbreviations,
  9579. but this can be configured with the variables `parse-time-months' and
  9580. `parse-time-weekdays'.
  9581. While prompting, a calendar is popped up - you can also select the
  9582. date with the mouse (button 1). The calendar shows a period of three
  9583. months. To scroll it to other months, use the keys `>' and `<'.
  9584. If you don't like the calendar, turn it off with
  9585. \(setq org-read-date-popup-calendar nil)
  9586. With optional argument TO-TIME, the date will immediately be converted
  9587. to an internal time.
  9588. With an optional argument WITH-TIME, the prompt will suggest to also
  9589. insert a time. Note that when WITH-TIME is not set, you can still
  9590. enter a time, and this function will inform the calling routine about
  9591. this change. The calling routine may then choose to change the format
  9592. used to insert the time stamp into the buffer to include the time.
  9593. With optional argument FROM-STRING, read from this string instead from
  9594. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9595. the time/date that is used for everything that is not specified by the
  9596. user."
  9597. (require 'parse-time)
  9598. (let* ((org-time-stamp-rounding-minutes
  9599. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9600. (org-dcst org-display-custom-times)
  9601. (ct (org-current-time))
  9602. (def (or org-overriding-default-time default-time ct))
  9603. (defdecode (decode-time def))
  9604. (dummy (progn
  9605. (when (< (nth 2 defdecode) org-extend-today-until)
  9606. (setcar (nthcdr 2 defdecode) -1)
  9607. (setcar (nthcdr 1 defdecode) 59)
  9608. (setq def (apply 'encode-time defdecode)
  9609. defdecode (decode-time def)))))
  9610. (calendar-move-hook nil)
  9611. (calendar-view-diary-initially-flag nil)
  9612. (view-diary-entries-initially nil)
  9613. (calendar-view-holidays-initially-flag nil)
  9614. (view-calendar-holidays-initially nil)
  9615. (timestr (format-time-string
  9616. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9617. (prompt (concat (if prompt (concat prompt " ") "")
  9618. (format "Date+time [%s]: " timestr)))
  9619. ans (org-ans0 "") org-ans1 org-ans2 final)
  9620. (cond
  9621. (from-string (setq ans from-string))
  9622. (org-read-date-popup-calendar
  9623. (save-excursion
  9624. (save-window-excursion
  9625. (calendar)
  9626. (calendar-forward-day (- (time-to-days def)
  9627. (calendar-absolute-from-gregorian
  9628. (calendar-current-date))))
  9629. (org-eval-in-calendar nil t)
  9630. (let* ((old-map (current-local-map))
  9631. (map (copy-keymap calendar-mode-map))
  9632. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9633. (org-defkey map (kbd "RET") 'org-calendar-select)
  9634. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9635. 'org-calendar-select-mouse)
  9636. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9637. 'org-calendar-select-mouse)
  9638. (org-defkey minibuffer-local-map [(meta shift left)]
  9639. (lambda () (interactive)
  9640. (org-eval-in-calendar '(calendar-backward-month 1))))
  9641. (org-defkey minibuffer-local-map [(meta shift right)]
  9642. (lambda () (interactive)
  9643. (org-eval-in-calendar '(calendar-forward-month 1))))
  9644. (org-defkey minibuffer-local-map [(meta shift up)]
  9645. (lambda () (interactive)
  9646. (org-eval-in-calendar '(calendar-backward-year 1))))
  9647. (org-defkey minibuffer-local-map [(meta shift down)]
  9648. (lambda () (interactive)
  9649. (org-eval-in-calendar '(calendar-forward-year 1))))
  9650. (org-defkey minibuffer-local-map [(shift up)]
  9651. (lambda () (interactive)
  9652. (org-eval-in-calendar '(calendar-backward-week 1))))
  9653. (org-defkey minibuffer-local-map [(shift down)]
  9654. (lambda () (interactive)
  9655. (org-eval-in-calendar '(calendar-forward-week 1))))
  9656. (org-defkey minibuffer-local-map [(shift left)]
  9657. (lambda () (interactive)
  9658. (org-eval-in-calendar '(calendar-backward-day 1))))
  9659. (org-defkey minibuffer-local-map [(shift right)]
  9660. (lambda () (interactive)
  9661. (org-eval-in-calendar '(calendar-forward-day 1))))
  9662. (org-defkey minibuffer-local-map ">"
  9663. (lambda () (interactive)
  9664. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9665. (org-defkey minibuffer-local-map "<"
  9666. (lambda () (interactive)
  9667. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9668. (unwind-protect
  9669. (progn
  9670. (use-local-map map)
  9671. (add-hook 'post-command-hook 'org-read-date-display)
  9672. (setq org-ans0 (read-string prompt default-input nil nil))
  9673. ;; org-ans0: from prompt
  9674. ;; org-ans1: from mouse click
  9675. ;; org-ans2: from calendar motion
  9676. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9677. (remove-hook 'post-command-hook 'org-read-date-display)
  9678. (use-local-map old-map)
  9679. (when org-read-date-overlay
  9680. (org-delete-overlay org-read-date-overlay)
  9681. (setq org-read-date-overlay nil)))))))
  9682. (t ; Naked prompt only
  9683. (unwind-protect
  9684. (setq ans (read-string prompt default-input nil timestr))
  9685. (when org-read-date-overlay
  9686. (org-delete-overlay org-read-date-overlay)
  9687. (setq org-read-date-overlay nil)))))
  9688. (setq final (org-read-date-analyze ans def defdecode))
  9689. (if to-time
  9690. (apply 'encode-time final)
  9691. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9692. (format "%04d-%02d-%02d %02d:%02d"
  9693. (nth 5 final) (nth 4 final) (nth 3 final)
  9694. (nth 2 final) (nth 1 final))
  9695. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9696. (defvar def)
  9697. (defvar defdecode)
  9698. (defvar with-time)
  9699. (defun org-read-date-display ()
  9700. "Display the currrent date prompt interpretation in the minibuffer."
  9701. (when org-read-date-display-live
  9702. (when org-read-date-overlay
  9703. (org-delete-overlay org-read-date-overlay))
  9704. (let ((p (point)))
  9705. (end-of-line 1)
  9706. (while (not (equal (buffer-substring
  9707. (max (point-min) (- (point) 4)) (point))
  9708. " "))
  9709. (insert " "))
  9710. (goto-char p))
  9711. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9712. " " (or org-ans1 org-ans2)))
  9713. (org-end-time-was-given nil)
  9714. (f (org-read-date-analyze ans def defdecode))
  9715. (fmts (if org-dcst
  9716. org-time-stamp-custom-formats
  9717. org-time-stamp-formats))
  9718. (fmt (if (or with-time
  9719. (and (boundp 'org-time-was-given) org-time-was-given))
  9720. (cdr fmts)
  9721. (car fmts)))
  9722. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9723. (when (and org-end-time-was-given
  9724. (string-match org-plain-time-of-day-regexp txt))
  9725. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9726. org-end-time-was-given
  9727. (substring txt (match-end 0)))))
  9728. (setq org-read-date-overlay
  9729. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9730. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9731. (defun org-read-date-analyze (ans def defdecode)
  9732. "Analyze the combined answer of the date prompt."
  9733. ;; FIXME: cleanup and comment
  9734. (let (delta deltan deltaw deltadef year month day
  9735. hour minute second wday pm h2 m2 tl wday1
  9736. iso-year iso-weekday iso-week iso-year iso-date)
  9737. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9738. (setq ans "+0"))
  9739. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9740. (setq ans (replace-match "" t t ans)
  9741. deltan (car delta)
  9742. deltaw (nth 1 delta)
  9743. deltadef (nth 2 delta)))
  9744. ;; Check if there is an iso week date in there
  9745. ;; If yes, sore the info and ostpone interpreting it until the rest
  9746. ;; of the parsing is done
  9747. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9748. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9749. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9750. iso-week (string-to-number (match-string 2 ans)))
  9751. (setq ans (replace-match "" t t ans)))
  9752. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9753. (when (string-match
  9754. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9755. (setq year (if (match-end 2)
  9756. (string-to-number (match-string 2 ans))
  9757. (string-to-number (format-time-string "%Y")))
  9758. month (string-to-number (match-string 3 ans))
  9759. day (string-to-number (match-string 4 ans)))
  9760. (if (< year 100) (setq year (+ 2000 year)))
  9761. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9762. t nil ans)))
  9763. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9764. ;; If there is a time with am/pm, and *no* time without it, we convert
  9765. ;; so that matching will be successful.
  9766. (loop for i from 1 to 2 do ; twice, for end time as well
  9767. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9768. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9769. (setq hour (string-to-number (match-string 1 ans))
  9770. minute (if (match-end 3)
  9771. (string-to-number (match-string 3 ans))
  9772. 0)
  9773. pm (equal ?p
  9774. (string-to-char (downcase (match-string 4 ans)))))
  9775. (if (and (= hour 12) (not pm))
  9776. (setq hour 0)
  9777. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9778. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9779. t t ans))))
  9780. ;; Check if a time range is given as a duration
  9781. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9782. (setq hour (string-to-number (match-string 1 ans))
  9783. h2 (+ hour (string-to-number (match-string 3 ans)))
  9784. minute (string-to-number (match-string 2 ans))
  9785. m2 (+ minute (if (match-end 5) (string-to-number
  9786. (match-string 5 ans))0)))
  9787. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9788. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9789. t t ans)))
  9790. ;; Check if there is a time range
  9791. (when (boundp 'org-end-time-was-given)
  9792. (setq org-time-was-given nil)
  9793. (when (and (string-match org-plain-time-of-day-regexp ans)
  9794. (match-end 8))
  9795. (setq org-end-time-was-given (match-string 8 ans))
  9796. (setq ans (concat (substring ans 0 (match-beginning 7))
  9797. (substring ans (match-end 7))))))
  9798. (setq tl (parse-time-string ans)
  9799. day (or (nth 3 tl) (nth 3 defdecode))
  9800. month (or (nth 4 tl)
  9801. (if (and org-read-date-prefer-future
  9802. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9803. (1+ (nth 4 defdecode))
  9804. (nth 4 defdecode)))
  9805. year (or (nth 5 tl)
  9806. (if (and org-read-date-prefer-future
  9807. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9808. (1+ (nth 5 defdecode))
  9809. (nth 5 defdecode)))
  9810. hour (or (nth 2 tl) (nth 2 defdecode))
  9811. minute (or (nth 1 tl) (nth 1 defdecode))
  9812. second (or (nth 0 tl) 0)
  9813. wday (nth 6 tl))
  9814. ;; Special date definitions below
  9815. (cond
  9816. (iso-week
  9817. ;; There was an iso week
  9818. (setq year (or iso-year year)
  9819. day (or iso-weekday wday 1)
  9820. wday nil ; to make sure that the trigger below does not match
  9821. iso-date (calendar-gregorian-from-absolute
  9822. (calendar-absolute-from-iso
  9823. (list iso-week day year))))
  9824. ; FIXME: Should we also push ISO weeks into the future?
  9825. ; (when (and org-read-date-prefer-future
  9826. ; (not iso-year)
  9827. ; (< (calendar-absolute-from-gregorian iso-date)
  9828. ; (time-to-days (current-time))))
  9829. ; (setq year (1+ year)
  9830. ; iso-date (calendar-gregorian-from-absolute
  9831. ; (calendar-absolute-from-iso
  9832. ; (list iso-week day year)))))
  9833. (setq month (car iso-date)
  9834. year (nth 2 iso-date)
  9835. day (nth 1 iso-date)))
  9836. (deltan
  9837. (unless deltadef
  9838. (let ((now (decode-time (current-time))))
  9839. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9840. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9841. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9842. ((equal deltaw "m") (setq month (+ month deltan)))
  9843. ((equal deltaw "y") (setq year (+ year deltan)))))
  9844. ((and wday (not (nth 3 tl)))
  9845. ;; Weekday was given, but no day, so pick that day in the week
  9846. ;; on or after the derived date.
  9847. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9848. (unless (equal wday wday1)
  9849. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9850. (if (and (boundp 'org-time-was-given)
  9851. (nth 2 tl))
  9852. (setq org-time-was-given t))
  9853. (if (< year 100) (setq year (+ 2000 year)))
  9854. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9855. (list second minute hour day month year)))
  9856. (defvar parse-time-weekdays)
  9857. (defun org-read-date-get-relative (s today default)
  9858. "Check string S for special relative date string.
  9859. TODAY and DEFAULT are internal times, for today and for a default.
  9860. Return shift list (N what def-flag)
  9861. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9862. N is the number of WHATs to shift.
  9863. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9864. the DEFAULT date rather than TODAY."
  9865. (when (and
  9866. (string-match
  9867. (concat
  9868. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9869. "\\([0-9]+\\)?"
  9870. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9871. "\\([ \t]\\|$\\)") s)
  9872. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9873. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9874. (string-to-char (substring (match-string 1 s) -1))
  9875. ?+))
  9876. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9877. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9878. (what (if (match-end 3) (match-string 3 s) "d"))
  9879. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9880. (date (if rel default today))
  9881. (wday (nth 6 (decode-time date)))
  9882. delta)
  9883. (if wday1
  9884. (progn
  9885. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9886. (if (= dir ?-) (setq delta (- delta 7)))
  9887. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9888. (list delta "d" rel))
  9889. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9890. (defun org-eval-in-calendar (form &optional keepdate)
  9891. "Eval FORM in the calendar window and return to current window.
  9892. Also, store the cursor date in variable org-ans2."
  9893. (let ((sw (selected-window)))
  9894. (select-window (get-buffer-window "*Calendar*"))
  9895. (eval form)
  9896. (when (and (not keepdate) (calendar-cursor-to-date))
  9897. (let* ((date (calendar-cursor-to-date))
  9898. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9899. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9900. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9901. (select-window sw)))
  9902. (defun org-calendar-select ()
  9903. "Return to `org-read-date' with the date currently selected.
  9904. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9905. (interactive)
  9906. (when (calendar-cursor-to-date)
  9907. (let* ((date (calendar-cursor-to-date))
  9908. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9909. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9910. (if (active-minibuffer-window) (exit-minibuffer))))
  9911. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9912. "Insert a date stamp for the date given by the internal TIME.
  9913. WITH-HM means, use the stamp format that includes the time of the day.
  9914. INACTIVE means use square brackets instead of angular ones, so that the
  9915. stamp will not contribute to the agenda.
  9916. PRE and POST are optional strings to be inserted before and after the
  9917. stamp.
  9918. The command returns the inserted time stamp."
  9919. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9920. stamp)
  9921. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9922. (insert-before-markers (or pre ""))
  9923. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9924. (when (listp extra)
  9925. (setq extra (car extra))
  9926. (if (and (stringp extra)
  9927. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9928. (setq extra (format "-%02d:%02d"
  9929. (string-to-number (match-string 1 extra))
  9930. (string-to-number (match-string 2 extra))))
  9931. (setq extra nil)))
  9932. (when extra
  9933. (backward-char 1)
  9934. (insert-before-markers extra)
  9935. (forward-char 1))
  9936. (insert-before-markers (or post ""))
  9937. (setq org-last-inserted-timestamp stamp)))
  9938. (defun org-toggle-time-stamp-overlays ()
  9939. "Toggle the use of custom time stamp formats."
  9940. (interactive)
  9941. (setq org-display-custom-times (not org-display-custom-times))
  9942. (unless org-display-custom-times
  9943. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9944. (while (setq p (next-single-property-change p 'display))
  9945. (if (and (get-text-property p 'display)
  9946. (eq (get-text-property p 'face) 'org-date))
  9947. (remove-text-properties
  9948. p (setq p (next-single-property-change p 'display))
  9949. '(display t))))
  9950. (set-buffer-modified-p bmp)))
  9951. (if (featurep 'xemacs)
  9952. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9953. (org-restart-font-lock)
  9954. (setq org-table-may-need-update t)
  9955. (if org-display-custom-times
  9956. (message "Time stamps are overlayed with custom format")
  9957. (message "Time stamp overlays removed")))
  9958. (defun org-display-custom-time (beg end)
  9959. "Overlay modified time stamp format over timestamp between BEG and END."
  9960. (let* ((ts (buffer-substring beg end))
  9961. t1 w1 with-hm tf time str w2 (off 0))
  9962. (save-match-data
  9963. (setq t1 (org-parse-time-string ts t))
  9964. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9965. (setq off (- (match-end 0) (match-beginning 0)))))
  9966. (setq end (- end off))
  9967. (setq w1 (- end beg)
  9968. with-hm (and (nth 1 t1) (nth 2 t1))
  9969. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9970. time (org-fix-decoded-time t1)
  9971. str (org-add-props
  9972. (format-time-string
  9973. (substring tf 1 -1) (apply 'encode-time time))
  9974. nil 'mouse-face 'highlight)
  9975. w2 (length str))
  9976. (if (not (= w2 w1))
  9977. (add-text-properties (1+ beg) (+ 2 beg)
  9978. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9979. (if (featurep 'xemacs)
  9980. (progn
  9981. (put-text-property beg end 'invisible t)
  9982. (put-text-property beg end 'end-glyph (make-glyph str)))
  9983. (put-text-property beg end 'display str))))
  9984. (defun org-translate-time (string)
  9985. "Translate all timestamps in STRING to custom format.
  9986. But do this only if the variable `org-display-custom-times' is set."
  9987. (when org-display-custom-times
  9988. (save-match-data
  9989. (let* ((start 0)
  9990. (re org-ts-regexp-both)
  9991. t1 with-hm inactive tf time str beg end)
  9992. (while (setq start (string-match re string start))
  9993. (setq beg (match-beginning 0)
  9994. end (match-end 0)
  9995. t1 (save-match-data
  9996. (org-parse-time-string (substring string beg end) t))
  9997. with-hm (and (nth 1 t1) (nth 2 t1))
  9998. inactive (equal (substring string beg (1+ beg)) "[")
  9999. tf (funcall (if with-hm 'cdr 'car)
  10000. org-time-stamp-custom-formats)
  10001. time (org-fix-decoded-time t1)
  10002. str (format-time-string
  10003. (concat
  10004. (if inactive "[" "<") (substring tf 1 -1)
  10005. (if inactive "]" ">"))
  10006. (apply 'encode-time time))
  10007. string (replace-match str t t string)
  10008. start (+ start (length str)))))))
  10009. string)
  10010. (defun org-fix-decoded-time (time)
  10011. "Set 0 instead of nil for the first 6 elements of time.
  10012. Don't touch the rest."
  10013. (let ((n 0))
  10014. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10015. (defun org-days-to-time (timestamp-string)
  10016. "Difference between TIMESTAMP-STRING and now in days."
  10017. (- (time-to-days (org-time-string-to-time timestamp-string))
  10018. (time-to-days (current-time))))
  10019. (defun org-deadline-close (timestamp-string &optional ndays)
  10020. "Is the time in TIMESTAMP-STRING close to the current date?"
  10021. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10022. (and (< (org-days-to-time timestamp-string) ndays)
  10023. (not (org-entry-is-done-p))))
  10024. (defun org-get-wdays (ts)
  10025. "Get the deadline lead time appropriate for timestring TS."
  10026. (cond
  10027. ((<= org-deadline-warning-days 0)
  10028. ;; 0 or negative, enforce this value no matter what
  10029. (- org-deadline-warning-days))
  10030. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10031. ;; lead time is specified.
  10032. (floor (* (string-to-number (match-string 1 ts))
  10033. (cdr (assoc (match-string 2 ts)
  10034. '(("d" . 1) ("w" . 7)
  10035. ("m" . 30.4) ("y" . 365.25)))))))
  10036. ;; go for the default.
  10037. (t org-deadline-warning-days)))
  10038. (defun org-calendar-select-mouse (ev)
  10039. "Return to `org-read-date' with the date currently selected.
  10040. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10041. (interactive "e")
  10042. (mouse-set-point ev)
  10043. (when (calendar-cursor-to-date)
  10044. (let* ((date (calendar-cursor-to-date))
  10045. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10046. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10047. (if (active-minibuffer-window) (exit-minibuffer))))
  10048. (defun org-check-deadlines (ndays)
  10049. "Check if there are any deadlines due or past due.
  10050. A deadline is considered due if it happens within `org-deadline-warning-days'
  10051. days from today's date. If the deadline appears in an entry marked DONE,
  10052. it is not shown. The prefix arg NDAYS can be used to test that many
  10053. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10054. (interactive "P")
  10055. (let* ((org-warn-days
  10056. (cond
  10057. ((equal ndays '(4)) 100000)
  10058. (ndays (prefix-numeric-value ndays))
  10059. (t (abs org-deadline-warning-days))))
  10060. (case-fold-search nil)
  10061. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10062. (callback
  10063. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10064. (message "%d deadlines past-due or due within %d days"
  10065. (org-occur regexp nil callback)
  10066. org-warn-days)))
  10067. (defun org-check-before-date (date)
  10068. "Check if there are deadlines or scheduled entries before DATE."
  10069. (interactive (list (org-read-date)))
  10070. (let ((case-fold-search nil)
  10071. (regexp (concat "\\<\\(" org-deadline-string
  10072. "\\|" org-scheduled-string
  10073. "\\) *<\\([^>]+\\)>"))
  10074. (callback
  10075. (lambda () (time-less-p
  10076. (org-time-string-to-time (match-string 2))
  10077. (org-time-string-to-time date)))))
  10078. (message "%d entries before %s"
  10079. (org-occur regexp nil callback) date)))
  10080. (defun org-evaluate-time-range (&optional to-buffer)
  10081. "Evaluate a time range by computing the difference between start and end.
  10082. Normally the result is just printed in the echo area, but with prefix arg
  10083. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10084. If the time range is actually in a table, the result is inserted into the
  10085. next column.
  10086. For time difference computation, a year is assumed to be exactly 365
  10087. days in order to avoid rounding problems."
  10088. (interactive "P")
  10089. (or
  10090. (org-clock-update-time-maybe)
  10091. (save-excursion
  10092. (unless (org-at-date-range-p t)
  10093. (goto-char (point-at-bol))
  10094. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10095. (if (not (org-at-date-range-p t))
  10096. (error "Not at a time-stamp range, and none found in current line")))
  10097. (let* ((ts1 (match-string 1))
  10098. (ts2 (match-string 2))
  10099. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10100. (match-end (match-end 0))
  10101. (time1 (org-time-string-to-time ts1))
  10102. (time2 (org-time-string-to-time ts2))
  10103. (t1 (time-to-seconds time1))
  10104. (t2 (time-to-seconds time2))
  10105. (diff (abs (- t2 t1)))
  10106. (negative (< (- t2 t1) 0))
  10107. ;; (ys (floor (* 365 24 60 60)))
  10108. (ds (* 24 60 60))
  10109. (hs (* 60 60))
  10110. (fy "%dy %dd %02d:%02d")
  10111. (fy1 "%dy %dd")
  10112. (fd "%dd %02d:%02d")
  10113. (fd1 "%dd")
  10114. (fh "%02d:%02d")
  10115. y d h m align)
  10116. (if havetime
  10117. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10118. y 0
  10119. d (floor (/ diff ds)) diff (mod diff ds)
  10120. h (floor (/ diff hs)) diff (mod diff hs)
  10121. m (floor (/ diff 60)))
  10122. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10123. y 0
  10124. d (floor (+ (/ diff ds) 0.5))
  10125. h 0 m 0))
  10126. (if (not to-buffer)
  10127. (message "%s" (org-make-tdiff-string y d h m))
  10128. (if (org-at-table-p)
  10129. (progn
  10130. (goto-char match-end)
  10131. (setq align t)
  10132. (and (looking-at " *|") (goto-char (match-end 0))))
  10133. (goto-char match-end))
  10134. (if (looking-at
  10135. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10136. (replace-match ""))
  10137. (if negative (insert " -"))
  10138. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10139. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10140. (insert " " (format fh h m))))
  10141. (if align (org-table-align))
  10142. (message "Time difference inserted")))))
  10143. (defun org-make-tdiff-string (y d h m)
  10144. (let ((fmt "")
  10145. (l nil))
  10146. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10147. l (push y l)))
  10148. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10149. l (push d l)))
  10150. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10151. l (push h l)))
  10152. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10153. l (push m l)))
  10154. (apply 'format fmt (nreverse l))))
  10155. (defun org-time-string-to-time (s)
  10156. (apply 'encode-time (org-parse-time-string s)))
  10157. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10158. "Convert a time stamp to an absolute day number.
  10159. If there is a specifyer for a cyclic time stamp, get the closest date to
  10160. DAYNR.
  10161. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10162. (cond
  10163. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10164. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10165. daynr
  10166. (+ daynr 1000)))
  10167. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10168. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10169. (time-to-days (current-time))) (match-string 0 s)
  10170. prefer show-all))
  10171. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10172. (defun org-days-to-iso-week (days)
  10173. "Return the iso week number."
  10174. (require 'cal-iso)
  10175. (car (calendar-iso-from-absolute days)))
  10176. (defun org-small-year-to-year (year)
  10177. "Convert 2-digit years into 4-digit years.
  10178. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10179. The year 2000 cannot be abbreviated. Any year lager than 99
  10180. is retrned unchanged."
  10181. (if (< year 38)
  10182. (setq year (+ 2000 year))
  10183. (if (< year 100)
  10184. (setq year (+ 1900 year))))
  10185. year)
  10186. (defun org-time-from-absolute (d)
  10187. "Return the time corresponding to date D.
  10188. D may be an absolute day number, or a calendar-type list (month day year)."
  10189. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10190. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10191. (defun org-calendar-holiday ()
  10192. "List of holidays, for Diary display in Org-mode."
  10193. (require 'holidays)
  10194. (let ((hl (funcall
  10195. (if (fboundp 'calendar-check-holidays)
  10196. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10197. (if hl (mapconcat 'identity hl "; "))))
  10198. (defun org-diary-sexp-entry (sexp entry date)
  10199. "Process a SEXP diary ENTRY for DATE."
  10200. (require 'diary-lib)
  10201. (let ((result (if calendar-debug-sexp
  10202. (let ((stack-trace-on-error t))
  10203. (eval (car (read-from-string sexp))))
  10204. (condition-case nil
  10205. (eval (car (read-from-string sexp)))
  10206. (error
  10207. (beep)
  10208. (message "Bad sexp at line %d in %s: %s"
  10209. (org-current-line)
  10210. (buffer-file-name) sexp)
  10211. (sleep-for 2))))))
  10212. (cond ((stringp result) result)
  10213. ((and (consp result)
  10214. (stringp (cdr result))) (cdr result))
  10215. (result entry)
  10216. (t nil))))
  10217. (defun org-diary-to-ical-string (frombuf)
  10218. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10219. This uses the icalendar.el library."
  10220. (let* ((tmpdir (if (featurep 'xemacs)
  10221. (temp-directory)
  10222. temporary-file-directory))
  10223. (tmpfile (make-temp-name
  10224. (expand-file-name "orgics" tmpdir)))
  10225. buf rtn b e)
  10226. (save-excursion
  10227. (set-buffer frombuf)
  10228. (icalendar-export-region (point-min) (point-max) tmpfile)
  10229. (setq buf (find-buffer-visiting tmpfile))
  10230. (set-buffer buf)
  10231. (goto-char (point-min))
  10232. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10233. (setq b (match-beginning 0)))
  10234. (goto-char (point-max))
  10235. (if (re-search-backward "^END:VEVENT" nil t)
  10236. (setq e (match-end 0)))
  10237. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10238. (kill-buffer buf)
  10239. (delete-file tmpfile)
  10240. rtn))
  10241. (defun org-closest-date (start current change prefer show-all)
  10242. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10243. When PREFER is `past' return a date that is either CURRENT or past.
  10244. When PREFER is `future', return a date that is either CURRENT or future.
  10245. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10246. ;; Make the proper lists from the dates
  10247. (catch 'exit
  10248. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10249. dn dw sday cday n1 n2
  10250. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10251. (setq start (org-date-to-gregorian start)
  10252. current (org-date-to-gregorian
  10253. (if show-all
  10254. current
  10255. (time-to-days (current-time))))
  10256. sday (calendar-absolute-from-gregorian start)
  10257. cday (calendar-absolute-from-gregorian current))
  10258. (if (<= cday sday) (throw 'exit sday))
  10259. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10260. (setq dn (string-to-number (match-string 1 change))
  10261. dw (cdr (assoc (match-string 2 change) a1)))
  10262. (error "Invalid change specifyer: %s" change))
  10263. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10264. (cond
  10265. ((eq dw 'day)
  10266. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10267. n2 (+ n1 dn)))
  10268. ((eq dw 'year)
  10269. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10270. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10271. (setq date1 (list m d y1)
  10272. n1 (calendar-absolute-from-gregorian date1)
  10273. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10274. n2 (calendar-absolute-from-gregorian date2)))
  10275. ((eq dw 'month)
  10276. ;; approx number of month between the two dates
  10277. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10278. ;; How often does dn fit in there?
  10279. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10280. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10281. m (+ m nm)
  10282. ny (floor (/ m 12))
  10283. y (+ y ny)
  10284. m (- m (* ny 12)))
  10285. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10286. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10287. (setq m2 (+ m dn) y2 y)
  10288. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10289. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10290. (while (<= n2 cday)
  10291. (setq n1 n2 m m2 y y2)
  10292. (setq m2 (+ m dn) y2 y)
  10293. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10294. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10295. (if show-all
  10296. (cond
  10297. ((eq prefer 'past) n1)
  10298. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10299. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10300. (cond
  10301. ((eq prefer 'past) n1)
  10302. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10303. (t (if (= cday n1) n1 n2)))))))
  10304. (defun org-date-to-gregorian (date)
  10305. "Turn any specification of DATE into a gregorian date for the calendar."
  10306. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10307. ((and (listp date) (= (length date) 3)) date)
  10308. ((stringp date)
  10309. (setq date (org-parse-time-string date))
  10310. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10311. ((listp date)
  10312. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10313. (defun org-parse-time-string (s &optional nodefault)
  10314. "Parse the standard Org-mode time string.
  10315. This should be a lot faster than the normal `parse-time-string'.
  10316. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10317. hour and minute fields will be nil if not given."
  10318. (if (string-match org-ts-regexp0 s)
  10319. (list 0
  10320. (if (or (match-beginning 8) (not nodefault))
  10321. (string-to-number (or (match-string 8 s) "0")))
  10322. (if (or (match-beginning 7) (not nodefault))
  10323. (string-to-number (or (match-string 7 s) "0")))
  10324. (string-to-number (match-string 4 s))
  10325. (string-to-number (match-string 3 s))
  10326. (string-to-number (match-string 2 s))
  10327. nil nil nil)
  10328. (make-list 9 0)))
  10329. (defun org-timestamp-up (&optional arg)
  10330. "Increase the date item at the cursor by one.
  10331. If the cursor is on the year, change the year. If it is on the month or
  10332. the day, change that.
  10333. With prefix ARG, change by that many units."
  10334. (interactive "p")
  10335. (org-timestamp-change (prefix-numeric-value arg)))
  10336. (defun org-timestamp-down (&optional arg)
  10337. "Decrease the date item at the cursor by one.
  10338. If the cursor is on the year, change the year. If it is on the month or
  10339. the day, change that.
  10340. With prefix ARG, change by that many units."
  10341. (interactive "p")
  10342. (org-timestamp-change (- (prefix-numeric-value arg))))
  10343. (defun org-timestamp-up-day (&optional arg)
  10344. "Increase the date in the time stamp by one day.
  10345. With prefix ARG, change that many days."
  10346. (interactive "p")
  10347. (if (and (not (org-at-timestamp-p t))
  10348. (org-on-heading-p))
  10349. (org-todo 'up)
  10350. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10351. (defun org-timestamp-down-day (&optional arg)
  10352. "Decrease the date in the time stamp by one day.
  10353. With prefix ARG, change that many days."
  10354. (interactive "p")
  10355. (if (and (not (org-at-timestamp-p t))
  10356. (org-on-heading-p))
  10357. (org-todo 'down)
  10358. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10359. (defun org-at-timestamp-p (&optional inactive-ok)
  10360. "Determine if the cursor is in or at a timestamp."
  10361. (interactive)
  10362. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10363. (pos (point))
  10364. (ans (or (looking-at tsr)
  10365. (save-excursion
  10366. (skip-chars-backward "^[<\n\r\t")
  10367. (if (> (point) (point-min)) (backward-char 1))
  10368. (and (looking-at tsr)
  10369. (> (- (match-end 0) pos) -1))))))
  10370. (and ans
  10371. (boundp 'org-ts-what)
  10372. (setq org-ts-what
  10373. (cond
  10374. ((= pos (match-beginning 0)) 'bracket)
  10375. ((= pos (1- (match-end 0))) 'bracket)
  10376. ((org-pos-in-match-range pos 2) 'year)
  10377. ((org-pos-in-match-range pos 3) 'month)
  10378. ((org-pos-in-match-range pos 7) 'hour)
  10379. ((org-pos-in-match-range pos 8) 'minute)
  10380. ((or (org-pos-in-match-range pos 4)
  10381. (org-pos-in-match-range pos 5)) 'day)
  10382. ((and (> pos (or (match-end 8) (match-end 5)))
  10383. (< pos (match-end 0)))
  10384. (- pos (or (match-end 8) (match-end 5))))
  10385. (t 'day))))
  10386. ans))
  10387. (defun org-toggle-timestamp-type ()
  10388. "Toggle the type (<active> or [inactive]) of a time stamp."
  10389. (interactive)
  10390. (when (org-at-timestamp-p t)
  10391. (let ((beg (match-beginning 0)) (end (match-end 0))
  10392. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10393. (save-excursion
  10394. (goto-char beg)
  10395. (while (re-search-forward "[][<>]" end t)
  10396. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10397. t t)))
  10398. (message "Timestamp is now %sactive"
  10399. (if (equal (char-after beg) ?<) "" "in")))))
  10400. (defun org-timestamp-change (n &optional what)
  10401. "Change the date in the time stamp at point.
  10402. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10403. `year', `minute', `second'. If WHAT is not given, the cursor position
  10404. in the timestamp determines what will be changed."
  10405. (let ((pos (point))
  10406. with-hm inactive
  10407. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10408. org-ts-what
  10409. extra rem
  10410. ts time time0)
  10411. (if (not (org-at-timestamp-p t))
  10412. (error "Not at a timestamp"))
  10413. (if (and (not what) (eq org-ts-what 'bracket))
  10414. (org-toggle-timestamp-type)
  10415. (if (and (not what) (not (eq org-ts-what 'day))
  10416. org-display-custom-times
  10417. (get-text-property (point) 'display)
  10418. (not (get-text-property (1- (point)) 'display)))
  10419. (setq org-ts-what 'day))
  10420. (setq org-ts-what (or what org-ts-what)
  10421. inactive (= (char-after (match-beginning 0)) ?\[)
  10422. ts (match-string 0))
  10423. (replace-match "")
  10424. (if (string-match
  10425. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10426. ts)
  10427. (setq extra (match-string 1 ts)))
  10428. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10429. (setq with-hm t))
  10430. (setq time0 (org-parse-time-string ts))
  10431. (when (and (eq org-ts-what 'minute)
  10432. (eq current-prefix-arg nil))
  10433. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10434. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10435. (setcar (cdr time0) (+ (nth 1 time0)
  10436. (if (> n 0) (- rem) (- dm rem))))))
  10437. (setq time
  10438. (encode-time (or (car time0) 0)
  10439. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10440. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10441. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10442. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10443. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10444. (nthcdr 6 time0)))
  10445. (when (integerp org-ts-what)
  10446. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10447. (if (eq what 'calendar)
  10448. (let ((cal-date (org-get-date-from-calendar)))
  10449. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10450. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10451. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10452. (setcar time0 (or (car time0) 0))
  10453. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10454. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10455. (setq time (apply 'encode-time time0))))
  10456. (setq org-last-changed-timestamp
  10457. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10458. (org-clock-update-time-maybe)
  10459. (goto-char pos)
  10460. ;; Try to recenter the calendar window, if any
  10461. (if (and org-calendar-follow-timestamp-change
  10462. (get-buffer-window "*Calendar*" t)
  10463. (memq org-ts-what '(day month year)))
  10464. (org-recenter-calendar (time-to-days time))))))
  10465. (defun org-modify-ts-extra (s pos n dm)
  10466. "Change the different parts of the lead-time and repeat fields in timestamp."
  10467. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10468. ng h m new rem)
  10469. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10470. (cond
  10471. ((or (org-pos-in-match-range pos 2)
  10472. (org-pos-in-match-range pos 3))
  10473. (setq m (string-to-number (match-string 3 s))
  10474. h (string-to-number (match-string 2 s)))
  10475. (if (org-pos-in-match-range pos 2)
  10476. (setq h (+ h n))
  10477. (setq n (* dm (org-no-warnings (signum n))))
  10478. (when (not (= 0 (setq rem (% m dm))))
  10479. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10480. (setq m (+ m n)))
  10481. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10482. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10483. (setq h (min 24 (max 0 h)))
  10484. (setq ng 1 new (format "-%02d:%02d" h m)))
  10485. ((org-pos-in-match-range pos 6)
  10486. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10487. ((org-pos-in-match-range pos 5)
  10488. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10489. ((org-pos-in-match-range pos 9)
  10490. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10491. ((org-pos-in-match-range pos 8)
  10492. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10493. (when ng
  10494. (setq s (concat
  10495. (substring s 0 (match-beginning ng))
  10496. new
  10497. (substring s (match-end ng))))))
  10498. s))
  10499. (defun org-recenter-calendar (date)
  10500. "If the calendar is visible, recenter it to DATE."
  10501. (let* ((win (selected-window))
  10502. (cwin (get-buffer-window "*Calendar*" t))
  10503. (calendar-move-hook nil))
  10504. (when cwin
  10505. (select-window cwin)
  10506. (calendar-goto-date (if (listp date) date
  10507. (calendar-gregorian-from-absolute date)))
  10508. (select-window win))))
  10509. (defun org-goto-calendar (&optional arg)
  10510. "Go to the Emacs calendar at the current date.
  10511. If there is a time stamp in the current line, go to that date.
  10512. A prefix ARG can be used to force the current date."
  10513. (interactive "P")
  10514. (let ((tsr org-ts-regexp) diff
  10515. (calendar-move-hook nil)
  10516. (calendar-view-holidays-initially-flag nil)
  10517. (view-calendar-holidays-initially nil)
  10518. (calendar-view-diary-initially-flag nil)
  10519. (view-diary-entries-initially nil))
  10520. (if (or (org-at-timestamp-p)
  10521. (save-excursion
  10522. (beginning-of-line 1)
  10523. (looking-at (concat ".*" tsr))))
  10524. (let ((d1 (time-to-days (current-time)))
  10525. (d2 (time-to-days
  10526. (org-time-string-to-time (match-string 1)))))
  10527. (setq diff (- d2 d1))))
  10528. (calendar)
  10529. (calendar-goto-today)
  10530. (if (and diff (not arg)) (calendar-forward-day diff))))
  10531. (defun org-get-date-from-calendar ()
  10532. "Return a list (month day year) of date at point in calendar."
  10533. (with-current-buffer "*Calendar*"
  10534. (save-match-data
  10535. (calendar-cursor-to-date))))
  10536. (defun org-date-from-calendar ()
  10537. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10538. If there is already a time stamp at the cursor position, update it."
  10539. (interactive)
  10540. (if (org-at-timestamp-p t)
  10541. (org-timestamp-change 0 'calendar)
  10542. (let ((cal-date (org-get-date-from-calendar)))
  10543. (org-insert-time-stamp
  10544. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10545. (defun org-minutes-to-hh:mm-string (m)
  10546. "Compute H:MM from a number of minutes."
  10547. (let ((h (/ m 60)))
  10548. (setq m (- m (* 60 h)))
  10549. (format org-time-clocksum-format h m)))
  10550. (defun org-hh:mm-string-to-minutes (s)
  10551. "Convert a string H:MM to a number of minutes."
  10552. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10553. (+ (* (string-to-number (match-string 1 s)) 60)
  10554. (string-to-number (match-string 2 s)))
  10555. 0))
  10556. ;;;; Agenda files
  10557. ;;;###autoload
  10558. (defun org-iswitchb (&optional arg)
  10559. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10560. With a prefix argument, restrict available to files.
  10561. With two prefix arguments, restrict available buffers to agenda files.
  10562. Due to some yet unresolved reason, the global function
  10563. `iswitchb-mode' needs to be active for this function to work."
  10564. (interactive "P")
  10565. (require 'iswitchb)
  10566. (let ((enabled iswitchb-mode) blist)
  10567. (or enabled (iswitchb-mode 1))
  10568. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10569. ((equal arg '(16)) (org-buffer-list 'agenda))
  10570. (t (org-buffer-list))))
  10571. (unwind-protect
  10572. (let ((iswitchb-make-buflist-hook
  10573. (lambda ()
  10574. (setq iswitchb-temp-buflist
  10575. (mapcar 'buffer-name blist)))))
  10576. (switch-to-buffer
  10577. (iswitchb-read-buffer
  10578. "Switch-to: " nil t))
  10579. (or enabled (iswitchb-mode -1))))))
  10580. (defun org-buffer-list (&optional predicate exclude-tmp)
  10581. "Return a list of Org buffers.
  10582. PREDICATE can be `export', `files' or `agenda'.
  10583. export restrict the list to Export buffers.
  10584. files restrict the list to buffers visiting Org files.
  10585. agenda restrict the list to buffers visiting agenda files.
  10586. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10587. (let* ((bfn nil)
  10588. (agenda-files (and (eq predicate 'agenda)
  10589. (mapcar 'file-truename (org-agenda-files t))))
  10590. (filter
  10591. (cond
  10592. ((eq predicate 'files)
  10593. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10594. ((eq predicate 'export)
  10595. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10596. ((eq predicate 'agenda)
  10597. (lambda (b)
  10598. (with-current-buffer b
  10599. (and (eq major-mode 'org-mode)
  10600. (setq bfn (buffer-file-name b))
  10601. (member (file-truename bfn) agenda-files)))))
  10602. (t (lambda (b) (with-current-buffer b
  10603. (or (eq major-mode 'org-mode)
  10604. (string-match "\*Org .*Export"
  10605. (buffer-name b)))))))))
  10606. (delq nil
  10607. (mapcar
  10608. (lambda(b)
  10609. (if (and (funcall filter b)
  10610. (or (not exclude-tmp)
  10611. (not (string-match "tmp" (buffer-name b)))))
  10612. b
  10613. nil))
  10614. (buffer-list)))))
  10615. (defun org-agenda-files (&optional unrestricted archives)
  10616. "Get the list of agenda files.
  10617. Optional UNRESTRICTED means return the full list even if a restriction
  10618. is currently in place.
  10619. When ARCHIVES is t, include all archive files hat are really being
  10620. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10621. `org-agenda-archives-mode' is t."
  10622. (let ((files
  10623. (cond
  10624. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10625. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10626. ((listp org-agenda-files) org-agenda-files)
  10627. (t (error "Invalid value of `org-agenda-files'")))))
  10628. (setq files (apply 'append
  10629. (mapcar (lambda (f)
  10630. (if (file-directory-p f)
  10631. (directory-files
  10632. f t org-agenda-file-regexp)
  10633. (list f)))
  10634. files)))
  10635. (when org-agenda-skip-unavailable-files
  10636. (setq files (delq nil
  10637. (mapcar (function
  10638. (lambda (file)
  10639. (and (file-readable-p file) file)))
  10640. files))))
  10641. (when (or (eq archives t)
  10642. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10643. (setq files (org-add-archive-files files)))
  10644. files))
  10645. (defun org-edit-agenda-file-list ()
  10646. "Edit the list of agenda files.
  10647. Depending on setup, this either uses customize to edit the variable
  10648. `org-agenda-files', or it visits the file that is holding the list. In the
  10649. latter case, the buffer is set up in a way that saving it automatically kills
  10650. the buffer and restores the previous window configuration."
  10651. (interactive)
  10652. (if (stringp org-agenda-files)
  10653. (let ((cw (current-window-configuration)))
  10654. (find-file org-agenda-files)
  10655. (org-set-local 'org-window-configuration cw)
  10656. (org-add-hook 'after-save-hook
  10657. (lambda ()
  10658. (set-window-configuration
  10659. (prog1 org-window-configuration
  10660. (kill-buffer (current-buffer))))
  10661. (org-install-agenda-files-menu)
  10662. (message "New agenda file list installed"))
  10663. nil 'local)
  10664. (message "%s" (substitute-command-keys
  10665. "Edit list and finish with \\[save-buffer]")))
  10666. (customize-variable 'org-agenda-files)))
  10667. (defun org-store-new-agenda-file-list (list)
  10668. "Set new value for the agenda file list and save it correcly."
  10669. (if (stringp org-agenda-files)
  10670. (let ((f org-agenda-files) b)
  10671. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10672. (with-temp-file f
  10673. (insert (mapconcat 'identity list "\n") "\n")))
  10674. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10675. (setq org-agenda-files list)
  10676. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10677. (defun org-read-agenda-file-list ()
  10678. "Read the list of agenda files from a file."
  10679. (when (file-directory-p org-agenda-files)
  10680. (error "`org-agenda-files' cannot be a single directory"))
  10681. (when (stringp org-agenda-files)
  10682. (with-temp-buffer
  10683. (insert-file-contents org-agenda-files)
  10684. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10685. ;;;###autoload
  10686. (defun org-cycle-agenda-files ()
  10687. "Cycle through the files in `org-agenda-files'.
  10688. If the current buffer visits an agenda file, find the next one in the list.
  10689. If the current buffer does not, find the first agenda file."
  10690. (interactive)
  10691. (let* ((fs (org-agenda-files t))
  10692. (files (append fs (list (car fs))))
  10693. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10694. file)
  10695. (unless files (error "No agenda files"))
  10696. (catch 'exit
  10697. (while (setq file (pop files))
  10698. (if (equal (file-truename file) tcf)
  10699. (when (car files)
  10700. (find-file (car files))
  10701. (throw 'exit t))))
  10702. (find-file (car fs)))
  10703. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10704. (defun org-agenda-file-to-front (&optional to-end)
  10705. "Move/add the current file to the top of the agenda file list.
  10706. If the file is not present in the list, it is added to the front. If it is
  10707. present, it is moved there. With optional argument TO-END, add/move to the
  10708. end of the list."
  10709. (interactive "P")
  10710. (let ((org-agenda-skip-unavailable-files nil)
  10711. (file-alist (mapcar (lambda (x)
  10712. (cons (file-truename x) x))
  10713. (org-agenda-files t)))
  10714. (ctf (file-truename buffer-file-name))
  10715. x had)
  10716. (setq x (assoc ctf file-alist) had x)
  10717. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10718. (if to-end
  10719. (setq file-alist (append (delq x file-alist) (list x)))
  10720. (setq file-alist (cons x (delq x file-alist))))
  10721. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10722. (org-install-agenda-files-menu)
  10723. (message "File %s to %s of agenda file list"
  10724. (if had "moved" "added") (if to-end "end" "front"))))
  10725. (defun org-remove-file (&optional file)
  10726. "Remove current file from the list of files in variable `org-agenda-files'.
  10727. These are the files which are being checked for agenda entries.
  10728. Optional argument FILE means, use this file instead of the current."
  10729. (interactive)
  10730. (let* ((org-agenda-skip-unavailable-files nil)
  10731. (file (or file buffer-file-name))
  10732. (true-file (file-truename file))
  10733. (afile (abbreviate-file-name file))
  10734. (files (delq nil (mapcar
  10735. (lambda (x)
  10736. (if (equal true-file
  10737. (file-truename x))
  10738. nil x))
  10739. (org-agenda-files t)))))
  10740. (if (not (= (length files) (length (org-agenda-files t))))
  10741. (progn
  10742. (org-store-new-agenda-file-list files)
  10743. (org-install-agenda-files-menu)
  10744. (message "Removed file: %s" afile))
  10745. (message "File was not in list: %s (not removed)" afile))))
  10746. (defun org-file-menu-entry (file)
  10747. (vector file (list 'find-file file) t))
  10748. (defun org-check-agenda-file (file)
  10749. "Make sure FILE exists. If not, ask user what to do."
  10750. (when (not (file-exists-p file))
  10751. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10752. (abbreviate-file-name file))
  10753. (let ((r (downcase (read-char-exclusive))))
  10754. (cond
  10755. ((equal r ?r)
  10756. (org-remove-file file)
  10757. (throw 'nextfile t))
  10758. (t (error "Abort"))))))
  10759. (defun org-get-agenda-file-buffer (file)
  10760. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10761. it to the list of buffers which might be released later."
  10762. (let ((buf (org-find-base-buffer-visiting file)))
  10763. (if buf
  10764. buf ; just return it
  10765. ;; Make a new buffer and remember it
  10766. (setq buf (find-file-noselect file))
  10767. (if buf (push buf org-agenda-new-buffers))
  10768. buf)))
  10769. (defun org-release-buffers (blist)
  10770. "Release all buffers in list, asking the user for confirmation when needed.
  10771. When a buffer is unmodified, it is just killed. When modified, it is saved
  10772. \(if the user agrees) and then killed."
  10773. (let (buf file)
  10774. (while (setq buf (pop blist))
  10775. (setq file (buffer-file-name buf))
  10776. (when (and (buffer-modified-p buf)
  10777. file
  10778. (y-or-n-p (format "Save file %s? " file)))
  10779. (with-current-buffer buf (save-buffer)))
  10780. (kill-buffer buf))))
  10781. (defun org-prepare-agenda-buffers (files)
  10782. "Create buffers for all agenda files, protect archived trees and comments."
  10783. (interactive)
  10784. (let ((pa '(:org-archived t))
  10785. (pc '(:org-comment t))
  10786. (pall '(:org-archived t :org-comment t))
  10787. (inhibit-read-only t)
  10788. (rea (concat ":" org-archive-tag ":"))
  10789. bmp file re)
  10790. (save-excursion
  10791. (save-restriction
  10792. (while (setq file (pop files))
  10793. (if (bufferp file)
  10794. (set-buffer file)
  10795. (org-check-agenda-file file)
  10796. (set-buffer (org-get-agenda-file-buffer file)))
  10797. (widen)
  10798. (setq bmp (buffer-modified-p))
  10799. (org-refresh-category-properties)
  10800. (setq org-todo-keywords-for-agenda
  10801. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10802. (setq org-done-keywords-for-agenda
  10803. (append org-done-keywords-for-agenda org-done-keywords))
  10804. (setq org-todo-keyword-alist-for-agenda
  10805. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10806. (setq org-tag-alist-for-agenda
  10807. (append org-tag-alist-for-agenda org-tag-alist))
  10808. (save-excursion
  10809. (remove-text-properties (point-min) (point-max) pall)
  10810. (when org-agenda-skip-archived-trees
  10811. (goto-char (point-min))
  10812. (while (re-search-forward rea nil t)
  10813. (if (org-on-heading-p t)
  10814. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10815. (goto-char (point-min))
  10816. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10817. (while (re-search-forward re nil t)
  10818. (add-text-properties
  10819. (match-beginning 0) (org-end-of-subtree t) pc)))
  10820. (set-buffer-modified-p bmp))))
  10821. (setq org-todo-keyword-alist-for-agenda
  10822. (org-uniquify org-todo-keyword-alist-for-agenda)
  10823. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10824. ;;;; Embedded LaTeX
  10825. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10826. "Keymap for the minor `org-cdlatex-mode'.")
  10827. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10828. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10829. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10830. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10831. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10832. (defvar org-cdlatex-texmathp-advice-is-done nil
  10833. "Flag remembering if we have applied the advice to texmathp already.")
  10834. (define-minor-mode org-cdlatex-mode
  10835. "Toggle the minor `org-cdlatex-mode'.
  10836. This mode supports entering LaTeX environment and math in LaTeX fragments
  10837. in Org-mode.
  10838. \\{org-cdlatex-mode-map}"
  10839. nil " OCDL" nil
  10840. (when org-cdlatex-mode (require 'cdlatex))
  10841. (unless org-cdlatex-texmathp-advice-is-done
  10842. (setq org-cdlatex-texmathp-advice-is-done t)
  10843. (defadvice texmathp (around org-math-always-on activate)
  10844. "Always return t in org-mode buffers.
  10845. This is because we want to insert math symbols without dollars even outside
  10846. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10847. en embedded LaTeX fragement, let texmathp do its job.
  10848. \\[org-cdlatex-mode-map]"
  10849. (interactive)
  10850. (let (p)
  10851. (cond
  10852. ((not (org-mode-p)) ad-do-it)
  10853. ((eq this-command 'cdlatex-math-symbol)
  10854. (setq ad-return-value t
  10855. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10856. (t
  10857. (let ((p (org-inside-LaTeX-fragment-p)))
  10858. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10859. (setq ad-return-value t
  10860. texmathp-why '("Org-mode embedded math" . 0))
  10861. (if p ad-do-it)))))))))
  10862. (defun turn-on-org-cdlatex ()
  10863. "Unconditionally turn on `org-cdlatex-mode'."
  10864. (org-cdlatex-mode 1))
  10865. (defun org-inside-LaTeX-fragment-p ()
  10866. "Test if point is inside a LaTeX fragment.
  10867. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10868. sequence appearing also before point.
  10869. Even though the matchers for math are configurable, this function assumes
  10870. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10871. delimiters are skipped when they have been removed by customization.
  10872. The return value is nil, or a cons cell with the delimiter and
  10873. and the position of this delimiter.
  10874. This function does a reasonably good job, but can locally be fooled by
  10875. for example currency specifications. For example it will assume being in
  10876. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10877. fragments that are properly closed, but during editing, we have to live
  10878. with the uncertainty caused by missing closing delimiters. This function
  10879. looks only before point, not after."
  10880. (catch 'exit
  10881. (let ((pos (point))
  10882. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10883. (lim (progn
  10884. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10885. (point)))
  10886. dd-on str (start 0) m re)
  10887. (goto-char pos)
  10888. (when dodollar
  10889. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10890. re (nth 1 (assoc "$" org-latex-regexps)))
  10891. (while (string-match re str start)
  10892. (cond
  10893. ((= (match-end 0) (length str))
  10894. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10895. ((= (match-end 0) (- (length str) 5))
  10896. (throw 'exit nil))
  10897. (t (setq start (match-end 0))))))
  10898. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10899. (goto-char pos)
  10900. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10901. (and (match-beginning 2) (throw 'exit nil))
  10902. ;; count $$
  10903. (while (re-search-backward "\\$\\$" lim t)
  10904. (setq dd-on (not dd-on)))
  10905. (goto-char pos)
  10906. (if dd-on (cons "$$" m))))))
  10907. (defun org-try-cdlatex-tab ()
  10908. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10909. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10910. - inside a LaTeX fragment, or
  10911. - after the first word in a line, where an abbreviation expansion could
  10912. insert a LaTeX environment."
  10913. (when org-cdlatex-mode
  10914. (cond
  10915. ((save-excursion
  10916. (skip-chars-backward "a-zA-Z0-9*")
  10917. (skip-chars-backward " \t")
  10918. (bolp))
  10919. (cdlatex-tab) t)
  10920. ((org-inside-LaTeX-fragment-p)
  10921. (cdlatex-tab) t)
  10922. (t nil))))
  10923. (defun org-cdlatex-underscore-caret (&optional arg)
  10924. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10925. Revert to the normal definition outside of these fragments."
  10926. (interactive "P")
  10927. (if (org-inside-LaTeX-fragment-p)
  10928. (call-interactively 'cdlatex-sub-superscript)
  10929. (let (org-cdlatex-mode)
  10930. (call-interactively (key-binding (vector last-input-event))))))
  10931. (defun org-cdlatex-math-modify (&optional arg)
  10932. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10933. Revert to the normal definition outside of these fragments."
  10934. (interactive "P")
  10935. (if (org-inside-LaTeX-fragment-p)
  10936. (call-interactively 'cdlatex-math-modify)
  10937. (let (org-cdlatex-mode)
  10938. (call-interactively (key-binding (vector last-input-event))))))
  10939. (defvar org-latex-fragment-image-overlays nil
  10940. "List of overlays carrying the images of latex fragments.")
  10941. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10942. (defun org-remove-latex-fragment-image-overlays ()
  10943. "Remove all overlays with LaTeX fragment images in current buffer."
  10944. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10945. (setq org-latex-fragment-image-overlays nil))
  10946. (defun org-preview-latex-fragment (&optional subtree)
  10947. "Preview the LaTeX fragment at point, or all locally or globally.
  10948. If the cursor is in a LaTeX fragment, create the image and overlay
  10949. it over the source code. If there is no fragment at point, display
  10950. all fragments in the current text, from one headline to the next. With
  10951. prefix SUBTREE, display all fragments in the current subtree. With a
  10952. double prefix `C-u C-u', or when the cursor is before the first headline,
  10953. display all fragments in the buffer.
  10954. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10955. (interactive "P")
  10956. (org-remove-latex-fragment-image-overlays)
  10957. (save-excursion
  10958. (save-restriction
  10959. (let (beg end at msg)
  10960. (cond
  10961. ((or (equal subtree '(16))
  10962. (not (save-excursion
  10963. (re-search-backward (concat "^" outline-regexp) nil t))))
  10964. (setq beg (point-min) end (point-max)
  10965. msg "Creating images for buffer...%s"))
  10966. ((equal subtree '(4))
  10967. (org-back-to-heading)
  10968. (setq beg (point) end (org-end-of-subtree t)
  10969. msg "Creating images for subtree...%s"))
  10970. (t
  10971. (if (setq at (org-inside-LaTeX-fragment-p))
  10972. (goto-char (max (point-min) (- (cdr at) 2)))
  10973. (org-back-to-heading))
  10974. (setq beg (point) end (progn (outline-next-heading) (point))
  10975. msg (if at "Creating image...%s"
  10976. "Creating images for entry...%s"))))
  10977. (message msg "")
  10978. (narrow-to-region beg end)
  10979. (goto-char beg)
  10980. (org-format-latex
  10981. (concat "ltxpng/" (file-name-sans-extension
  10982. (file-name-nondirectory
  10983. buffer-file-name)))
  10984. default-directory 'overlays msg at 'forbuffer)
  10985. (message msg "done. Use `C-c C-c' to remove images.")))))
  10986. (defvar org-latex-regexps
  10987. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10988. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10989. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10990. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10991. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10992. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10993. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10994. "Regular expressions for matching embedded LaTeX.")
  10995. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10996. "Replace LaTeX fragments with links to an image, and produce images."
  10997. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10998. (let* ((prefixnodir (file-name-nondirectory prefix))
  10999. (absprefix (expand-file-name prefix dir))
  11000. (todir (file-name-directory absprefix))
  11001. (opt org-format-latex-options)
  11002. (matchers (plist-get opt :matchers))
  11003. (re-list org-latex-regexps)
  11004. (cnt 0) txt link beg end re e checkdir
  11005. m n block linkfile movefile ov)
  11006. ;; Check if there are old images files with this prefix, and remove them
  11007. (when (file-directory-p todir)
  11008. (mapc 'delete-file
  11009. (directory-files
  11010. todir 'full
  11011. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11012. ;; Check the different regular expressions
  11013. (while (setq e (pop re-list))
  11014. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11015. block (if (nth 3 e) "\n\n" ""))
  11016. (when (member m matchers)
  11017. (goto-char (point-min))
  11018. (while (re-search-forward re nil t)
  11019. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11020. (setq txt (match-string n)
  11021. beg (match-beginning n) end (match-end n)
  11022. cnt (1+ cnt)
  11023. linkfile (format "%s_%04d.png" prefix cnt)
  11024. movefile (format "%s_%04d.png" absprefix cnt)
  11025. link (concat block "[[file:" linkfile "]]" block))
  11026. (if msg (message msg cnt))
  11027. (goto-char beg)
  11028. (unless checkdir ; make sure the directory exists
  11029. (setq checkdir t)
  11030. (or (file-directory-p todir) (make-directory todir)))
  11031. (org-create-formula-image
  11032. txt movefile opt forbuffer)
  11033. (if overlays
  11034. (progn
  11035. (setq ov (org-make-overlay beg end))
  11036. (if (featurep 'xemacs)
  11037. (progn
  11038. (org-overlay-put ov 'invisible t)
  11039. (org-overlay-put
  11040. ov 'end-glyph
  11041. (make-glyph (vector 'png :file movefile))))
  11042. (org-overlay-put
  11043. ov 'display
  11044. (list 'image :type 'png :file movefile :ascent 'center)))
  11045. (push ov org-latex-fragment-image-overlays)
  11046. (goto-char end))
  11047. (delete-region beg end)
  11048. (insert link))))))))
  11049. ;; This function borrows from Ganesh Swami's latex2png.el
  11050. (defun org-create-formula-image (string tofile options buffer)
  11051. (let* ((tmpdir (if (featurep 'xemacs)
  11052. (temp-directory)
  11053. temporary-file-directory))
  11054. (texfilebase (make-temp-name
  11055. (expand-file-name "orgtex" tmpdir)))
  11056. (texfile (concat texfilebase ".tex"))
  11057. (dvifile (concat texfilebase ".dvi"))
  11058. (pngfile (concat texfilebase ".png"))
  11059. (fnh (if (featurep 'xemacs)
  11060. (font-height (get-face-font 'default))
  11061. (face-attribute 'default :height nil)))
  11062. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11063. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11064. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11065. "Black"))
  11066. (bg (or (plist-get options (if buffer :background :html-background))
  11067. "Transparent")))
  11068. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11069. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11070. (with-temp-file texfile
  11071. (insert org-format-latex-header
  11072. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11073. (let ((dir default-directory))
  11074. (condition-case nil
  11075. (progn
  11076. (cd tmpdir)
  11077. (call-process "latex" nil nil nil texfile))
  11078. (error nil))
  11079. (cd dir))
  11080. (if (not (file-exists-p dvifile))
  11081. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11082. (condition-case nil
  11083. (call-process "dvipng" nil nil nil
  11084. "-E" "-fg" fg "-bg" bg
  11085. "-D" dpi
  11086. ;;"-x" scale "-y" scale
  11087. "-T" "tight"
  11088. "-o" pngfile
  11089. dvifile)
  11090. (error nil))
  11091. (if (not (file-exists-p pngfile))
  11092. (progn (message "Failed to create png file from %s" texfile) nil)
  11093. ;; Use the requested file name and clean up
  11094. (copy-file pngfile tofile 'replace)
  11095. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11096. (delete-file (concat texfilebase e)))
  11097. pngfile))))
  11098. (defun org-dvipng-color (attr)
  11099. "Return an rgb color specification for dvipng."
  11100. (apply 'format "rgb %s %s %s"
  11101. (mapcar 'org-normalize-color
  11102. (color-values (face-attribute 'default attr nil)))))
  11103. (defun org-normalize-color (value)
  11104. "Return string to be used as color value for an RGB component."
  11105. (format "%g" (/ value 65535.0)))
  11106. ;;;; Key bindings
  11107. ;; Make `C-c C-x' a prefix key
  11108. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11109. ;; TAB key with modifiers
  11110. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11111. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11112. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11113. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11114. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11115. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11116. ;; The following line is necessary under Suse GNU/Linux
  11117. (unless (featurep 'xemacs)
  11118. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11119. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11120. (define-key org-mode-map [backtab] 'org-shifttab)
  11121. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11122. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11123. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11124. ;; Cursor keys with modifiers
  11125. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11126. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11127. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11128. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11129. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11130. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11131. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11132. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11133. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11134. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11135. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11136. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11137. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11138. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11139. ;;; Extra keys for tty access.
  11140. ;; We only set them when really needed because otherwise the
  11141. ;; menus don't show the simple keys
  11142. (when (or org-use-extra-keys
  11143. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11144. (not window-system))
  11145. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11146. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11147. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11148. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11149. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11150. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11151. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11152. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11153. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11154. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11155. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11156. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11157. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11158. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11159. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11160. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11161. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11162. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11163. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11164. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11165. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11166. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11167. ;; All the other keys
  11168. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11169. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11170. (if (boundp 'narrow-map)
  11171. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11172. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11173. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11174. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11175. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11176. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11177. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11178. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11179. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11180. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11181. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11182. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11183. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11184. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11185. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11186. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11187. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11188. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11189. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11190. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11191. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11192. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11193. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11194. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11195. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11196. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11197. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11198. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11199. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11200. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11201. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11202. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11203. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11204. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11205. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11206. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11207. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11208. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11209. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11210. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11211. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11212. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11213. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11214. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11215. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11216. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11217. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11218. (org-defkey org-mode-map "\C-m" 'org-return)
  11219. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11220. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11221. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11222. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11223. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11224. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11225. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11226. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11227. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11228. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11229. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11230. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11231. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11232. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11233. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11234. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11235. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11236. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11237. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11238. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11239. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11240. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11241. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11242. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11243. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11244. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11245. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11246. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11247. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11248. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11249. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11250. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11251. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11252. (when (featurep 'xemacs)
  11253. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11254. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11255. (defun org-self-insert-command (N)
  11256. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11257. If the cursor is in a table looking at whitespace, the whitespace is
  11258. overwritten, and the table is not marked as requiring realignment."
  11259. (interactive "p")
  11260. (if (and (org-table-p)
  11261. (progn
  11262. ;; check if we blank the field, and if that triggers align
  11263. (and (featurep 'org-table) org-table-auto-blank-field
  11264. (member last-command
  11265. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11266. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11267. ;; got extra space, this field does not determine column width
  11268. (let (org-table-may-need-update) (org-table-blank-field))
  11269. ;; no extra space, this field may determine column width
  11270. (org-table-blank-field)))
  11271. t)
  11272. (eq N 1)
  11273. (looking-at "[^|\n]* |"))
  11274. (let (org-table-may-need-update)
  11275. (goto-char (1- (match-end 0)))
  11276. (delete-backward-char 1)
  11277. (goto-char (match-beginning 0))
  11278. (self-insert-command N))
  11279. (setq org-table-may-need-update t)
  11280. (self-insert-command N)
  11281. (org-fix-tags-on-the-fly)))
  11282. (defun org-fix-tags-on-the-fly ()
  11283. (when (and (equal (char-after (point-at-bol)) ?*)
  11284. (org-on-heading-p))
  11285. (org-align-tags-here org-tags-column)))
  11286. (defun org-delete-backward-char (N)
  11287. "Like `delete-backward-char', insert whitespace at field end in tables.
  11288. When deleting backwards, in tables this function will insert whitespace in
  11289. front of the next \"|\" separator, to keep the table aligned. The table will
  11290. still be marked for re-alignment if the field did fill the entire column,
  11291. because, in this case the deletion might narrow the column."
  11292. (interactive "p")
  11293. (if (and (org-table-p)
  11294. (eq N 1)
  11295. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11296. (looking-at ".*?|"))
  11297. (let ((pos (point))
  11298. (noalign (looking-at "[^|\n\r]* |"))
  11299. (c org-table-may-need-update))
  11300. (backward-delete-char N)
  11301. (skip-chars-forward "^|")
  11302. (insert " ")
  11303. (goto-char (1- pos))
  11304. ;; noalign: if there were two spaces at the end, this field
  11305. ;; does not determine the width of the column.
  11306. (if noalign (setq org-table-may-need-update c)))
  11307. (backward-delete-char N)
  11308. (org-fix-tags-on-the-fly)))
  11309. (defun org-delete-char (N)
  11310. "Like `delete-char', but insert whitespace at field end in tables.
  11311. When deleting characters, in tables this function will insert whitespace in
  11312. front of the next \"|\" separator, to keep the table aligned. The table will
  11313. still be marked for re-alignment if the field did fill the entire column,
  11314. because, in this case the deletion might narrow the column."
  11315. (interactive "p")
  11316. (if (and (org-table-p)
  11317. (not (bolp))
  11318. (not (= (char-after) ?|))
  11319. (eq N 1))
  11320. (if (looking-at ".*?|")
  11321. (let ((pos (point))
  11322. (noalign (looking-at "[^|\n\r]* |"))
  11323. (c org-table-may-need-update))
  11324. (replace-match (concat
  11325. (substring (match-string 0) 1 -1)
  11326. " |"))
  11327. (goto-char pos)
  11328. ;; noalign: if there were two spaces at the end, this field
  11329. ;; does not determine the width of the column.
  11330. (if noalign (setq org-table-may-need-update c)))
  11331. (delete-char N))
  11332. (delete-char N)
  11333. (org-fix-tags-on-the-fly)))
  11334. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11335. (put 'org-self-insert-command 'delete-selection t)
  11336. (put 'orgtbl-self-insert-command 'delete-selection t)
  11337. (put 'org-delete-char 'delete-selection 'supersede)
  11338. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11339. ;; Make `flyspell-mode' delay after some commands
  11340. (put 'org-self-insert-command 'flyspell-delayed t)
  11341. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11342. (put 'org-delete-char 'flyspell-delayed t)
  11343. (put 'org-delete-backward-char 'flyspell-delayed t)
  11344. ;; Make pabbrev-mode expand after org-mode commands
  11345. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11346. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11347. ;; How to do this: Measure non-white length of current string
  11348. ;; If equal to column width, we should realign.
  11349. (defun org-remap (map &rest commands)
  11350. "In MAP, remap the functions given in COMMANDS.
  11351. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11352. (let (new old)
  11353. (while commands
  11354. (setq old (pop commands) new (pop commands))
  11355. (if (fboundp 'command-remapping)
  11356. (org-defkey map (vector 'remap old) new)
  11357. (substitute-key-definition old new map global-map)))))
  11358. (when (eq org-enable-table-editor 'optimized)
  11359. ;; If the user wants maximum table support, we need to hijack
  11360. ;; some standard editing functions
  11361. (org-remap org-mode-map
  11362. 'self-insert-command 'org-self-insert-command
  11363. 'delete-char 'org-delete-char
  11364. 'delete-backward-char 'org-delete-backward-char)
  11365. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11366. (defun org-shiftcursor-error ()
  11367. "Throw an error because Shift-Cursor command was applied in wrong context."
  11368. (error "This command is active in special context like tables, headlines or timestamps"))
  11369. (defun org-shifttab (&optional arg)
  11370. "Global visibility cycling or move to previous table field.
  11371. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11372. on context.
  11373. See the individual commands for more information."
  11374. (interactive "P")
  11375. (cond
  11376. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11377. ((integerp arg)
  11378. (message "Content view to level: %d" arg)
  11379. (org-content (prefix-numeric-value arg))
  11380. (setq org-cycle-global-status 'overview))
  11381. (t (call-interactively 'org-global-cycle))))
  11382. (defun org-shiftmetaleft ()
  11383. "Promote subtree or delete table column.
  11384. Calls `org-promote-subtree', `org-outdent-item',
  11385. or `org-table-delete-column', depending on context.
  11386. See the individual commands for more information."
  11387. (interactive)
  11388. (cond
  11389. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11390. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11391. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11392. (t (org-shiftcursor-error))))
  11393. (defun org-shiftmetaright ()
  11394. "Demote subtree or insert table column.
  11395. Calls `org-demote-subtree', `org-indent-item',
  11396. or `org-table-insert-column', depending on context.
  11397. See the individual commands for more information."
  11398. (interactive)
  11399. (cond
  11400. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11401. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11402. ((org-at-item-p) (call-interactively 'org-indent-item))
  11403. (t (org-shiftcursor-error))))
  11404. (defun org-shiftmetaup (&optional arg)
  11405. "Move subtree up or kill table row.
  11406. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11407. `org-move-item-up' depending on context. See the individual commands
  11408. for more information."
  11409. (interactive "P")
  11410. (cond
  11411. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11412. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11413. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11414. (t (org-shiftcursor-error))))
  11415. (defun org-shiftmetadown (&optional arg)
  11416. "Move subtree down or insert table row.
  11417. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11418. `org-move-item-down', depending on context. See the individual
  11419. commands for more information."
  11420. (interactive "P")
  11421. (cond
  11422. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11423. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11424. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11425. (t (org-shiftcursor-error))))
  11426. (defun org-metaleft (&optional arg)
  11427. "Promote heading or move table column to left.
  11428. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11429. With no specific context, calls the Emacs default `backward-word'.
  11430. See the individual commands for more information."
  11431. (interactive "P")
  11432. (cond
  11433. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11434. ((or (org-on-heading-p) (org-region-active-p))
  11435. (call-interactively 'org-do-promote))
  11436. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11437. (t (call-interactively 'backward-word))))
  11438. (defun org-metaright (&optional arg)
  11439. "Demote subtree or move table column to right.
  11440. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11441. With no specific context, calls the Emacs default `forward-word'.
  11442. See the individual commands for more information."
  11443. (interactive "P")
  11444. (cond
  11445. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11446. ((or (org-on-heading-p) (org-region-active-p))
  11447. (call-interactively 'org-do-demote))
  11448. ((org-at-item-p) (call-interactively 'org-indent-item))
  11449. (t (call-interactively 'forward-word))))
  11450. (defun org-metaup (&optional arg)
  11451. "Move subtree up or move table row up.
  11452. Calls `org-move-subtree-up' or `org-table-move-row' or
  11453. `org-move-item-up', depending on context. See the individual commands
  11454. for more information."
  11455. (interactive "P")
  11456. (cond
  11457. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11458. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11459. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11460. (t (transpose-lines 1) (beginning-of-line -1))))
  11461. (defun org-metadown (&optional arg)
  11462. "Move subtree down or move table row down.
  11463. Calls `org-move-subtree-down' or `org-table-move-row' or
  11464. `org-move-item-down', depending on context. See the individual
  11465. commands for more information."
  11466. (interactive "P")
  11467. (cond
  11468. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11469. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11470. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11471. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11472. (defun org-shiftup (&optional arg)
  11473. "Increase item in timestamp or increase priority of current headline.
  11474. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11475. depending on context. See the individual commands for more information."
  11476. (interactive "P")
  11477. (cond
  11478. ((org-at-timestamp-p t)
  11479. (call-interactively (if org-edit-timestamp-down-means-later
  11480. 'org-timestamp-down 'org-timestamp-up)))
  11481. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11482. ((org-at-item-p) (call-interactively 'org-previous-item))
  11483. ((org-clocktable-try-shift 'up arg))
  11484. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11485. (defun org-shiftdown (&optional arg)
  11486. "Decrease item in timestamp or decrease priority of current headline.
  11487. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11488. depending on context. See the individual commands for more information."
  11489. (interactive "P")
  11490. (cond
  11491. ((org-at-timestamp-p t)
  11492. (call-interactively (if org-edit-timestamp-down-means-later
  11493. 'org-timestamp-up 'org-timestamp-down)))
  11494. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11495. ((org-clocktable-try-shift 'down arg))
  11496. (t (call-interactively 'org-next-item))))
  11497. (defun org-shiftright (&optional arg)
  11498. "Next TODO keyword or timestamp one day later, depending on context."
  11499. (interactive "P")
  11500. (cond
  11501. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11502. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11503. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11504. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11505. ((org-clocktable-try-shift 'right arg))
  11506. (t (org-shiftcursor-error))))
  11507. (defun org-shiftleft (&optional arg)
  11508. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11509. (interactive "P")
  11510. (cond
  11511. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11512. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11513. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11514. ((org-at-property-p)
  11515. (call-interactively 'org-property-previous-allowed-value))
  11516. ((org-clocktable-try-shift 'left arg))
  11517. (t (org-shiftcursor-error))))
  11518. (defun org-shiftcontrolright ()
  11519. "Switch to next TODO set."
  11520. (interactive)
  11521. (cond
  11522. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11523. (t (org-shiftcursor-error))))
  11524. (defun org-shiftcontrolleft ()
  11525. "Switch to previous TODO set."
  11526. (interactive)
  11527. (cond
  11528. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11529. (t (org-shiftcursor-error))))
  11530. (defun org-ctrl-c-ret ()
  11531. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11532. (interactive)
  11533. (cond
  11534. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11535. (t (call-interactively 'org-insert-heading))))
  11536. (defun org-copy-special ()
  11537. "Copy region in table or copy current subtree.
  11538. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11539. See the individual commands for more information."
  11540. (interactive)
  11541. (call-interactively
  11542. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11543. (defun org-cut-special ()
  11544. "Cut region in table or cut current subtree.
  11545. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11546. See the individual commands for more information."
  11547. (interactive)
  11548. (call-interactively
  11549. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11550. (defun org-paste-special (arg)
  11551. "Paste rectangular region into table, or past subtree relative to level.
  11552. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11553. See the individual commands for more information."
  11554. (interactive "P")
  11555. (if (org-at-table-p)
  11556. (org-table-paste-rectangle)
  11557. (org-paste-subtree arg)))
  11558. (defun org-edit-special ()
  11559. "Call a special editor for the stuff at point.
  11560. When at a table, call the formula editor with `org-table-edit-formulas'.
  11561. When at the first line of an src example, call `org-edit-src-code'.
  11562. When in an #+include line, visit the include file. Otherwise call
  11563. `ffap' to visit the file at point."
  11564. (interactive)
  11565. (cond
  11566. ((org-at-table-p)
  11567. (call-interactively 'org-table-edit-formulas))
  11568. ((save-excursion
  11569. (beginning-of-line 1)
  11570. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11571. (find-file (org-trim (match-string 1))))
  11572. ((org-edit-src-code))
  11573. ((org-edit-fixed-width-region))
  11574. (t (call-interactively 'ffap))))
  11575. (defun org-ctrl-c-ctrl-c (&optional arg)
  11576. "Set tags in headline, or update according to changed information at point.
  11577. This command does many different things, depending on context:
  11578. - If the cursor is in a headline, prompt for tags and insert them
  11579. into the current line, aligned to `org-tags-column'. When called
  11580. with prefix arg, realign all tags in the current buffer.
  11581. - If the cursor is in one of the special #+KEYWORD lines, this
  11582. triggers scanning the buffer for these lines and updating the
  11583. information.
  11584. - If the cursor is inside a table, realign the table. This command
  11585. works even if the automatic table editor has been turned off.
  11586. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11587. the entire table.
  11588. - If the cursor is a the beginning of a dynamic block, update it.
  11589. - If the cursor is inside a table created by the table.el package,
  11590. activate that table.
  11591. - If the current buffer is a remember buffer, close note and file
  11592. it. A prefix argument of 1 files to the default location
  11593. without further interaction. A prefix argument of 2 files to
  11594. the currently clocking task.
  11595. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11596. links in this buffer.
  11597. - If the cursor is on a numbered item in a plain list, renumber the
  11598. ordered list.
  11599. - If the cursor is on a checkbox, toggle it."
  11600. (interactive "P")
  11601. (let ((org-enable-table-editor t))
  11602. (cond
  11603. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11604. org-occur-highlights
  11605. org-latex-fragment-image-overlays)
  11606. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11607. (org-remove-occur-highlights)
  11608. (org-remove-latex-fragment-image-overlays)
  11609. (message "Temporary highlights/overlays removed from current buffer"))
  11610. ((and (local-variable-p 'org-finish-function (current-buffer))
  11611. (fboundp org-finish-function))
  11612. (funcall org-finish-function))
  11613. ((org-at-property-p)
  11614. (call-interactively 'org-property-action))
  11615. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11616. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11617. ((org-at-table.el-p)
  11618. (require 'table)
  11619. (beginning-of-line 1)
  11620. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11621. (call-interactively 'table-recognize-table))
  11622. ((org-at-table-p)
  11623. (org-table-maybe-eval-formula)
  11624. (if arg
  11625. (call-interactively 'org-table-recalculate)
  11626. (org-table-maybe-recalculate-line))
  11627. (call-interactively 'org-table-align))
  11628. ((org-at-item-checkbox-p)
  11629. (call-interactively 'org-toggle-checkbox))
  11630. ((org-at-item-p)
  11631. (call-interactively 'org-maybe-renumber-ordered-list))
  11632. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11633. ;; Dynamic block
  11634. (beginning-of-line 1)
  11635. (save-excursion (org-update-dblock)))
  11636. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11637. (cond
  11638. ((equal (match-string 1) "TBLFM")
  11639. ;; Recalculate the table before this line
  11640. (save-excursion
  11641. (beginning-of-line 1)
  11642. (skip-chars-backward " \r\n\t")
  11643. (if (org-at-table-p)
  11644. (org-call-with-arg 'org-table-recalculate t))))
  11645. (t
  11646. ; (org-set-regexps-and-options)
  11647. ; (org-restart-font-lock)
  11648. (let ((org-inhibit-startup t)) (org-mode-restart))
  11649. (message "Local setup has been refreshed"))))
  11650. (t (error "C-c C-c can do nothing useful at this location.")))))
  11651. (defun org-mode-restart ()
  11652. "Restart Org-mode, to scan again for special lines.
  11653. Also updates the keyword regular expressions."
  11654. (interactive)
  11655. (org-mode)
  11656. (message "Org-mode restarted"))
  11657. (defun org-kill-note-or-show-branches ()
  11658. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11659. (interactive)
  11660. (if (not org-finish-function)
  11661. (call-interactively 'show-branches)
  11662. (let ((org-note-abort t))
  11663. (funcall org-finish-function))))
  11664. (defun org-return (&optional indent)
  11665. "Goto next table row or insert a newline.
  11666. Calls `org-table-next-row' or `newline', depending on context.
  11667. See the individual commands for more information."
  11668. (interactive)
  11669. (cond
  11670. ((bobp) (if indent (newline-and-indent) (newline)))
  11671. ((and (org-at-heading-p)
  11672. (looking-at
  11673. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11674. (org-show-entry)
  11675. (end-of-line 1)
  11676. (newline))
  11677. ((org-at-table-p)
  11678. (org-table-justify-field-maybe)
  11679. (call-interactively 'org-table-next-row))
  11680. (t (if indent (newline-and-indent) (newline)))))
  11681. (defun org-return-indent ()
  11682. "Goto next table row or insert a newline and indent.
  11683. Calls `org-table-next-row' or `newline-and-indent', depending on
  11684. context. See the individual commands for more information."
  11685. (interactive)
  11686. (org-return t))
  11687. (defun org-ctrl-c-star ()
  11688. "Compute table, or change heading status of lines.
  11689. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11690. depending on context. This will also turn a plain list item or a normal
  11691. line into a subheading."
  11692. (interactive)
  11693. (cond
  11694. ((org-at-table-p)
  11695. (call-interactively 'org-table-recalculate))
  11696. ((org-region-active-p)
  11697. ;; Convert all lines in region to list items
  11698. (call-interactively 'org-toggle-region-headings))
  11699. ((org-on-heading-p)
  11700. (org-toggle-region-headings (point-at-bol)
  11701. (min (1+ (point-at-eol)) (point-max))))
  11702. ((org-at-item-p)
  11703. ;; Convert to heading
  11704. (let ((level (save-match-data
  11705. (save-excursion
  11706. (condition-case nil
  11707. (progn
  11708. (org-back-to-heading t)
  11709. (funcall outline-level))
  11710. (error 0))))))
  11711. (replace-match
  11712. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11713. (t (org-toggle-region-headings (point-at-bol)
  11714. (min (1+ (point-at-eol)) (point-max))))))
  11715. (defun org-ctrl-c-minus ()
  11716. "Insert separator line in table or modify bullet status of line.
  11717. Also turns a plain line or a region of lines into list items.
  11718. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11719. `org-cycle-list-bullet', depending on context."
  11720. (interactive)
  11721. (cond
  11722. ((org-at-table-p)
  11723. (call-interactively 'org-table-insert-hline))
  11724. ((org-on-heading-p)
  11725. ;; Convert to item
  11726. (save-excursion
  11727. (beginning-of-line 1)
  11728. (if (looking-at "\\*+ ")
  11729. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11730. ((org-region-active-p)
  11731. ;; Convert all lines in region to list items
  11732. (call-interactively 'org-toggle-region-items))
  11733. ((org-in-item-p)
  11734. (call-interactively 'org-cycle-list-bullet))
  11735. (t (org-toggle-region-items (point-at-bol)
  11736. (min (1+ (point-at-eol)) (point-max))))))
  11737. (defun org-toggle-region-items (beg end)
  11738. "Convert all lines in region to list items.
  11739. If the first line is already an item, convert all list items in the region
  11740. to normal lines."
  11741. (interactive "r")
  11742. (let (l2 l)
  11743. (save-excursion
  11744. (goto-char end)
  11745. (setq l2 (org-current-line))
  11746. (goto-char beg)
  11747. (beginning-of-line 1)
  11748. (setq l (1- (org-current-line)))
  11749. (if (org-at-item-p)
  11750. ;; We already have items, de-itemize
  11751. (while (< (setq l (1+ l)) l2)
  11752. (when (org-at-item-p)
  11753. (goto-char (match-beginning 2))
  11754. (delete-region (match-beginning 2) (match-end 2))
  11755. (and (looking-at "[ \t]+") (replace-match "")))
  11756. (beginning-of-line 2))
  11757. (while (< (setq l (1+ l)) l2)
  11758. (unless (org-at-item-p)
  11759. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11760. (replace-match "\\1- \\2")))
  11761. (beginning-of-line 2))))))
  11762. (defun org-toggle-region-headings (beg end)
  11763. "Convert all lines in region to list items.
  11764. If the first line is already an item, convert all list items in the region
  11765. to normal lines."
  11766. (interactive "r")
  11767. (let (l2 l)
  11768. (save-excursion
  11769. (goto-char end)
  11770. (setq l2 (org-current-line))
  11771. (goto-char beg)
  11772. (beginning-of-line 1)
  11773. (setq l (1- (org-current-line)))
  11774. (if (org-on-heading-p)
  11775. ;; We already have headlines, de-star them
  11776. (while (< (setq l (1+ l)) l2)
  11777. (when (org-on-heading-p t)
  11778. (and (looking-at outline-regexp) (replace-match "")))
  11779. (beginning-of-line 2))
  11780. (let* ((stars (save-excursion
  11781. (re-search-backward org-complex-heading-regexp nil t)
  11782. (or (match-string 1) "*")))
  11783. (add-stars (if org-odd-levels-only "**" "*"))
  11784. (rpl (concat stars add-stars " \\2")))
  11785. (while (< (setq l (1+ l)) l2)
  11786. (unless (org-on-heading-p)
  11787. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11788. (replace-match rpl)))
  11789. (beginning-of-line 2)))))))
  11790. (defun org-meta-return (&optional arg)
  11791. "Insert a new heading or wrap a region in a table.
  11792. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11793. See the individual commands for more information."
  11794. (interactive "P")
  11795. (cond
  11796. ((org-at-table-p)
  11797. (call-interactively 'org-table-wrap-region))
  11798. (t (call-interactively 'org-insert-heading))))
  11799. ;;; Menu entries
  11800. ;; Define the Org-mode menus
  11801. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11802. '("Tbl"
  11803. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11804. ["Next Field" org-cycle (org-at-table-p)]
  11805. ["Previous Field" org-shifttab (org-at-table-p)]
  11806. ["Next Row" org-return (org-at-table-p)]
  11807. "--"
  11808. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11809. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11810. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11811. "--"
  11812. ("Column"
  11813. ["Move Column Left" org-metaleft (org-at-table-p)]
  11814. ["Move Column Right" org-metaright (org-at-table-p)]
  11815. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11816. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11817. ("Row"
  11818. ["Move Row Up" org-metaup (org-at-table-p)]
  11819. ["Move Row Down" org-metadown (org-at-table-p)]
  11820. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11821. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11822. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11823. "--"
  11824. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11825. ("Rectangle"
  11826. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11827. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11828. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11829. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11830. "--"
  11831. ("Calculate"
  11832. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11833. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11834. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11835. "--"
  11836. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11837. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11838. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11839. "--"
  11840. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11841. "--"
  11842. ["Sum Column/Rectangle" org-table-sum
  11843. (or (org-at-table-p) (org-region-active-p))]
  11844. ["Which Column?" org-table-current-column (org-at-table-p)])
  11845. ["Debug Formulas"
  11846. org-table-toggle-formula-debugger
  11847. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11848. ["Show Col/Row Numbers"
  11849. org-table-toggle-coordinate-overlays
  11850. :style toggle
  11851. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11852. "--"
  11853. ["Create" org-table-create (and (not (org-at-table-p))
  11854. org-enable-table-editor)]
  11855. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11856. ["Import from File" org-table-import (not (org-at-table-p))]
  11857. ["Export to File" org-table-export (org-at-table-p)]
  11858. "--"
  11859. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11860. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11861. '("Org"
  11862. ("Show/Hide"
  11863. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11864. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11865. ["Sparse Tree..." org-sparse-tree t]
  11866. ["Reveal Context" org-reveal t]
  11867. ["Show All" show-all t]
  11868. "--"
  11869. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11870. "--"
  11871. ["New Heading" org-insert-heading t]
  11872. ("Navigate Headings"
  11873. ["Up" outline-up-heading t]
  11874. ["Next" outline-next-visible-heading t]
  11875. ["Previous" outline-previous-visible-heading t]
  11876. ["Next Same Level" outline-forward-same-level t]
  11877. ["Previous Same Level" outline-backward-same-level t]
  11878. "--"
  11879. ["Jump" org-goto t])
  11880. ("Edit Structure"
  11881. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11882. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11883. "--"
  11884. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11885. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11886. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11887. "--"
  11888. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11889. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11890. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11891. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11892. "--"
  11893. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11894. "--"
  11895. ["Convert to odd levels" org-convert-to-odd-levels t]
  11896. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11897. ("Editing"
  11898. ["Emphasis..." org-emphasize t]
  11899. ["Edit Source Example" org-edit-special t])
  11900. ("Archive"
  11901. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11902. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11903. ; :active t :keys "C-u C-c C-x C-a"]
  11904. ["Sparse trees open ARCHIVE trees"
  11905. (setq org-sparse-tree-open-archived-trees
  11906. (not org-sparse-tree-open-archived-trees))
  11907. :style toggle :selected org-sparse-tree-open-archived-trees]
  11908. ["Cycling opens ARCHIVE trees"
  11909. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11910. :style toggle :selected org-cycle-open-archived-trees]
  11911. "--"
  11912. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  11913. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11914. ; ["Check and Move Children" (org-archive-subtree '(4))
  11915. ; :active t :keys "C-u C-c C-x C-s"]
  11916. )
  11917. "--"
  11918. ("TODO Lists"
  11919. ["TODO/DONE/-" org-todo t]
  11920. ("Select keyword"
  11921. ["Next keyword" org-shiftright (org-on-heading-p)]
  11922. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11923. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11924. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11925. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11926. ["Show TODO Tree" org-show-todo-tree t]
  11927. ["Global TODO list" org-todo-list t]
  11928. "--"
  11929. ["Set Priority" org-priority t]
  11930. ["Priority Up" org-shiftup t]
  11931. ["Priority Down" org-shiftdown t])
  11932. ("TAGS and Properties"
  11933. ["Set Tags" 'org-set-tags-command t]
  11934. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11935. "--"
  11936. ["Set property" 'org-set-property t]
  11937. ["Column view of properties" org-columns t]
  11938. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11939. ("Dates and Scheduling"
  11940. ["Timestamp" org-time-stamp t]
  11941. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11942. ("Change Date"
  11943. ["1 Day Later" org-shiftright t]
  11944. ["1 Day Earlier" org-shiftleft t]
  11945. ["1 ... Later" org-shiftup t]
  11946. ["1 ... Earlier" org-shiftdown t])
  11947. ["Compute Time Range" org-evaluate-time-range t]
  11948. ["Schedule Item" org-schedule t]
  11949. ["Deadline" org-deadline t]
  11950. "--"
  11951. ["Custom time format" org-toggle-time-stamp-overlays
  11952. :style radio :selected org-display-custom-times]
  11953. "--"
  11954. ["Goto Calendar" org-goto-calendar t]
  11955. ["Date from Calendar" org-date-from-calendar t])
  11956. ("Logging work"
  11957. ["Clock in" org-clock-in t]
  11958. ["Clock out" org-clock-out t]
  11959. ["Clock cancel" org-clock-cancel t]
  11960. ["Goto running clock" org-clock-goto t]
  11961. ["Display times" org-clock-display t]
  11962. ["Create clock table" org-clock-report t]
  11963. "--"
  11964. ["Record DONE time"
  11965. (progn (setq org-log-done (not org-log-done))
  11966. (message "Switching to %s will %s record a timestamp"
  11967. (car org-done-keywords)
  11968. (if org-log-done "automatically" "not")))
  11969. :style toggle :selected org-log-done])
  11970. "--"
  11971. ["Agenda Command..." org-agenda t]
  11972. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11973. ("File List for Agenda")
  11974. ("Special views current file"
  11975. ["TODO Tree" org-show-todo-tree t]
  11976. ["Check Deadlines" org-check-deadlines t]
  11977. ["Timeline" org-timeline t]
  11978. ["Tags Tree" org-tags-sparse-tree t])
  11979. "--"
  11980. ("Hyperlinks"
  11981. ["Store Link (Global)" org-store-link t]
  11982. ["Insert Link" org-insert-link t]
  11983. ["Follow Link" org-open-at-point t]
  11984. "--"
  11985. ["Next link" org-next-link t]
  11986. ["Previous link" org-previous-link t]
  11987. "--"
  11988. ["Descriptive Links"
  11989. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11990. :style radio
  11991. :selected (member '(org-link) buffer-invisibility-spec)]
  11992. ["Literal Links"
  11993. (progn
  11994. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11995. :style radio
  11996. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11997. "--"
  11998. ["Export/Publish..." org-export t]
  11999. ("LaTeX"
  12000. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12001. :selected org-cdlatex-mode]
  12002. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12003. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12004. ["Modify math symbol" org-cdlatex-math-modify
  12005. (org-inside-LaTeX-fragment-p)]
  12006. ["Export LaTeX fragments as images"
  12007. (if (featurep 'org-exp)
  12008. (setq org-export-with-LaTeX-fragments
  12009. (not org-export-with-LaTeX-fragments))
  12010. (require 'org-exp))
  12011. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12012. org-export-with-LaTeX-fragments)])
  12013. "--"
  12014. ("Documentation"
  12015. ["Show Version" org-version t]
  12016. ["Info Documentation" org-info t])
  12017. ("Customize"
  12018. ["Browse Org Group" org-customize t]
  12019. "--"
  12020. ["Expand This Menu" org-create-customize-menu
  12021. (fboundp 'customize-menu-create)])
  12022. "--"
  12023. ["Refresh setup" org-mode-restart t]
  12024. ))
  12025. (defun org-info (&optional node)
  12026. "Read documentation for Org-mode in the info system.
  12027. With optional NODE, go directly to that node."
  12028. (interactive)
  12029. (info (format "(org)%s" (or node ""))))
  12030. (defun org-install-agenda-files-menu ()
  12031. (let ((bl (buffer-list)))
  12032. (save-excursion
  12033. (while bl
  12034. (set-buffer (pop bl))
  12035. (if (org-mode-p) (setq bl nil)))
  12036. (when (org-mode-p)
  12037. (easy-menu-change
  12038. '("Org") "File List for Agenda"
  12039. (append
  12040. (list
  12041. ["Edit File List" (org-edit-agenda-file-list) t]
  12042. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12043. ["Remove Current File from List" org-remove-file t]
  12044. ["Cycle through agenda files" org-cycle-agenda-files t]
  12045. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12046. "--")
  12047. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12048. ;;;; Documentation
  12049. ;;;###autoload
  12050. (defun org-require-autoloaded-modules ()
  12051. (interactive)
  12052. (mapc 'require
  12053. '(org-agenda org-archive org-clock org-colview
  12054. org-exp org-id org-export-latex org-publish
  12055. org-remember org-table)))
  12056. ;;;###autoload
  12057. (defun org-customize ()
  12058. "Call the customize function with org as argument."
  12059. (interactive)
  12060. (org-load-modules-maybe)
  12061. (org-require-autoloaded-modules)
  12062. (customize-browse 'org))
  12063. (defun org-create-customize-menu ()
  12064. "Create a full customization menu for Org-mode, insert it into the menu."
  12065. (interactive)
  12066. (org-load-modules-maybe)
  12067. (org-require-autoloaded-modules)
  12068. (if (fboundp 'customize-menu-create)
  12069. (progn
  12070. (easy-menu-change
  12071. '("Org") "Customize"
  12072. `(["Browse Org group" org-customize t]
  12073. "--"
  12074. ,(customize-menu-create 'org)
  12075. ["Set" Custom-set t]
  12076. ["Save" Custom-save t]
  12077. ["Reset to Current" Custom-reset-current t]
  12078. ["Reset to Saved" Custom-reset-saved t]
  12079. ["Reset to Standard Settings" Custom-reset-standard t]))
  12080. (message "\"Org\"-menu now contains full customization menu"))
  12081. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12082. ;;;; Miscellaneous stuff
  12083. ;;; Generally useful functions
  12084. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12085. "Display the given MESSAGE as a warning."
  12086. (if (fboundp 'display-warning)
  12087. (display-warning 'org message
  12088. (if (featurep 'xemacs)
  12089. 'warning
  12090. :warning))
  12091. (let ((buf (get-buffer-create "*Org warnings*")))
  12092. (with-current-buffer buf
  12093. (goto-char (point-max))
  12094. (insert "Warning (Org): " message)
  12095. (unless (bolp)
  12096. (newline)))
  12097. (display-buffer buf)
  12098. (sit-for 0))))
  12099. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12100. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12101. (if (and marker (marker-buffer marker)
  12102. (buffer-live-p (marker-buffer marker)))
  12103. (progn
  12104. (switch-to-buffer (marker-buffer marker))
  12105. (if (or (> marker (point-max)) (< marker (point-min)))
  12106. (widen))
  12107. (goto-char marker))
  12108. (if bookmark
  12109. (bookmark-jump bookmark)
  12110. (error "Cannot find location"))))
  12111. (defun org-quote-csv-field (s)
  12112. "Quote field for inclusion in CSV material."
  12113. (if (string-match "[\",]" s)
  12114. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12115. s))
  12116. (defun org-plist-delete (plist property)
  12117. "Delete PROPERTY from PLIST.
  12118. This is in contrast to merely setting it to 0."
  12119. (let (p)
  12120. (while plist
  12121. (if (not (eq property (car plist)))
  12122. (setq p (plist-put p (car plist) (nth 1 plist))))
  12123. (setq plist (cddr plist)))
  12124. p))
  12125. (defun org-force-self-insert (N)
  12126. "Needed to enforce self-insert under remapping."
  12127. (interactive "p")
  12128. (self-insert-command N))
  12129. (defun org-string-width (s)
  12130. "Compute width of string, ignoring invisible characters.
  12131. This ignores character with invisibility property `org-link', and also
  12132. characters with property `org-cwidth', because these will become invisible
  12133. upon the next fontification round."
  12134. (let (b l)
  12135. (when (or (eq t buffer-invisibility-spec)
  12136. (assq 'org-link buffer-invisibility-spec))
  12137. (while (setq b (text-property-any 0 (length s)
  12138. 'invisible 'org-link s))
  12139. (setq s (concat (substring s 0 b)
  12140. (substring s (or (next-single-property-change
  12141. b 'invisible s) (length s)))))))
  12142. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12143. (setq s (concat (substring s 0 b)
  12144. (substring s (or (next-single-property-change
  12145. b 'org-cwidth s) (length s))))))
  12146. (setq l (string-width s) b -1)
  12147. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12148. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12149. l))
  12150. (defun org-get-indentation (&optional line)
  12151. "Get the indentation of the current line, interpreting tabs.
  12152. When LINE is given, assume it represents a line and compute its indentation."
  12153. (if line
  12154. (if (string-match "^ *" (org-remove-tabs line))
  12155. (match-end 0))
  12156. (save-excursion
  12157. (beginning-of-line 1)
  12158. (skip-chars-forward " \t")
  12159. (current-column))))
  12160. (defun org-remove-tabs (s &optional width)
  12161. "Replace tabulators in S with spaces.
  12162. Assumes that s is a single line, starting in column 0."
  12163. (setq width (or width tab-width))
  12164. (while (string-match "\t" s)
  12165. (setq s (replace-match
  12166. (make-string
  12167. (- (* width (/ (+ (match-beginning 0) width) width))
  12168. (match-beginning 0)) ?\ )
  12169. t t s)))
  12170. s)
  12171. (defun org-fix-indentation (line ind)
  12172. "Fix indentation in LINE.
  12173. IND is a cons cell with target and minimum indentation.
  12174. If the current indenation in LINE is smaller than the minimum,
  12175. leave it alone. If it is larger than ind, set it to the target."
  12176. (let* ((l (org-remove-tabs line))
  12177. (i (org-get-indentation l))
  12178. (i1 (car ind)) (i2 (cdr ind)))
  12179. (if (>= i i2) (setq l (substring line i2)))
  12180. (if (> i1 0)
  12181. (concat (make-string i1 ?\ ) l)
  12182. l)))
  12183. (defun org-base-buffer (buffer)
  12184. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12185. (if (not buffer)
  12186. buffer
  12187. (or (buffer-base-buffer buffer)
  12188. buffer)))
  12189. (defun org-trim (s)
  12190. "Remove whitespace at beginning and end of string."
  12191. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12192. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12193. s)
  12194. (defun org-wrap (string &optional width lines)
  12195. "Wrap string to either a number of lines, or a width in characters.
  12196. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12197. that costs. If there is a word longer than WIDTH, the text is actually
  12198. wrapped to the length of that word.
  12199. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12200. many lines, whatever width that takes.
  12201. The return value is a list of lines, without newlines at the end."
  12202. (let* ((words (org-split-string string "[ \t\n]+"))
  12203. (maxword (apply 'max (mapcar 'org-string-width words)))
  12204. w ll)
  12205. (cond (width
  12206. (org-do-wrap words (max maxword width)))
  12207. (lines
  12208. (setq w maxword)
  12209. (setq ll (org-do-wrap words maxword))
  12210. (if (<= (length ll) lines)
  12211. ll
  12212. (setq ll words)
  12213. (while (> (length ll) lines)
  12214. (setq w (1+ w))
  12215. (setq ll (org-do-wrap words w)))
  12216. ll))
  12217. (t (error "Cannot wrap this")))))
  12218. (defun org-do-wrap (words width)
  12219. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12220. (let (lines line)
  12221. (while words
  12222. (setq line (pop words))
  12223. (while (and words (< (+ (length line) (length (car words))) width))
  12224. (setq line (concat line " " (pop words))))
  12225. (setq lines (push line lines)))
  12226. (nreverse lines)))
  12227. (defun org-split-string (string &optional separators)
  12228. "Splits STRING into substrings at SEPARATORS.
  12229. No empty strings are returned if there are matches at the beginning
  12230. and end of string."
  12231. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12232. (start 0)
  12233. notfirst
  12234. (list nil))
  12235. (while (and (string-match rexp string
  12236. (if (and notfirst
  12237. (= start (match-beginning 0))
  12238. (< start (length string)))
  12239. (1+ start) start))
  12240. (< (match-beginning 0) (length string)))
  12241. (setq notfirst t)
  12242. (or (eq (match-beginning 0) 0)
  12243. (and (eq (match-beginning 0) (match-end 0))
  12244. (eq (match-beginning 0) start))
  12245. (setq list
  12246. (cons (substring string start (match-beginning 0))
  12247. list)))
  12248. (setq start (match-end 0)))
  12249. (or (eq start (length string))
  12250. (setq list
  12251. (cons (substring string start)
  12252. list)))
  12253. (nreverse list)))
  12254. (defun org-context ()
  12255. "Return a list of contexts of the current cursor position.
  12256. If several contexts apply, all are returned.
  12257. Each context entry is a list with a symbol naming the context, and
  12258. two positions indicating start and end of the context. Possible
  12259. contexts are:
  12260. :headline anywhere in a headline
  12261. :headline-stars on the leading stars in a headline
  12262. :todo-keyword on a TODO keyword (including DONE) in a headline
  12263. :tags on the TAGS in a headline
  12264. :priority on the priority cookie in a headline
  12265. :item on the first line of a plain list item
  12266. :item-bullet on the bullet/number of a plain list item
  12267. :checkbox on the checkbox in a plain list item
  12268. :table in an org-mode table
  12269. :table-special on a special filed in a table
  12270. :table-table in a table.el table
  12271. :link on a hyperlink
  12272. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12273. :target on a <<target>>
  12274. :radio-target on a <<<radio-target>>>
  12275. :latex-fragment on a LaTeX fragment
  12276. :latex-preview on a LaTeX fragment with overlayed preview image
  12277. This function expects the position to be visible because it uses font-lock
  12278. faces as a help to recognize the following contexts: :table-special, :link,
  12279. and :keyword."
  12280. (let* ((f (get-text-property (point) 'face))
  12281. (faces (if (listp f) f (list f)))
  12282. (p (point)) clist o)
  12283. ;; First the large context
  12284. (cond
  12285. ((org-on-heading-p t)
  12286. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12287. (when (progn
  12288. (beginning-of-line 1)
  12289. (looking-at org-todo-line-tags-regexp))
  12290. (push (org-point-in-group p 1 :headline-stars) clist)
  12291. (push (org-point-in-group p 2 :todo-keyword) clist)
  12292. (push (org-point-in-group p 4 :tags) clist))
  12293. (goto-char p)
  12294. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12295. (if (looking-at "\\[#[A-Z0-9]\\]")
  12296. (push (org-point-in-group p 0 :priority) clist)))
  12297. ((org-at-item-p)
  12298. (push (org-point-in-group p 2 :item-bullet) clist)
  12299. (push (list :item (point-at-bol)
  12300. (save-excursion (org-end-of-item) (point)))
  12301. clist)
  12302. (and (org-at-item-checkbox-p)
  12303. (push (org-point-in-group p 0 :checkbox) clist)))
  12304. ((org-at-table-p)
  12305. (push (list :table (org-table-begin) (org-table-end)) clist)
  12306. (if (memq 'org-formula faces)
  12307. (push (list :table-special
  12308. (previous-single-property-change p 'face)
  12309. (next-single-property-change p 'face)) clist)))
  12310. ((org-at-table-p 'any)
  12311. (push (list :table-table) clist)))
  12312. (goto-char p)
  12313. ;; Now the small context
  12314. (cond
  12315. ((org-at-timestamp-p)
  12316. (push (org-point-in-group p 0 :timestamp) clist))
  12317. ((memq 'org-link faces)
  12318. (push (list :link
  12319. (previous-single-property-change p 'face)
  12320. (next-single-property-change p 'face)) clist))
  12321. ((memq 'org-special-keyword faces)
  12322. (push (list :keyword
  12323. (previous-single-property-change p 'face)
  12324. (next-single-property-change p 'face)) clist))
  12325. ((org-on-target-p)
  12326. (push (org-point-in-group p 0 :target) clist)
  12327. (goto-char (1- (match-beginning 0)))
  12328. (if (looking-at org-radio-target-regexp)
  12329. (push (org-point-in-group p 0 :radio-target) clist))
  12330. (goto-char p))
  12331. ((setq o (car (delq nil
  12332. (mapcar
  12333. (lambda (x)
  12334. (if (memq x org-latex-fragment-image-overlays) x))
  12335. (org-overlays-at (point))))))
  12336. (push (list :latex-fragment
  12337. (org-overlay-start o) (org-overlay-end o)) clist)
  12338. (push (list :latex-preview
  12339. (org-overlay-start o) (org-overlay-end o)) clist))
  12340. ((org-inside-LaTeX-fragment-p)
  12341. ;; FIXME: positions wrong.
  12342. (push (list :latex-fragment (point) (point)) clist)))
  12343. (setq clist (nreverse (delq nil clist)))
  12344. clist))
  12345. ;; FIXME: Compare with at-regexp-p Do we need both?
  12346. (defun org-in-regexp (re &optional nlines visually)
  12347. "Check if point is inside a match of regexp.
  12348. Normally only the current line is checked, but you can include NLINES extra
  12349. lines both before and after point into the search.
  12350. If VISUALLY is set, require that the cursor is not after the match but
  12351. really on, so that the block visually is on the match."
  12352. (catch 'exit
  12353. (let ((pos (point))
  12354. (eol (point-at-eol (+ 1 (or nlines 0))))
  12355. (inc (if visually 1 0)))
  12356. (save-excursion
  12357. (beginning-of-line (- 1 (or nlines 0)))
  12358. (while (re-search-forward re eol t)
  12359. (if (and (<= (match-beginning 0) pos)
  12360. (>= (+ inc (match-end 0)) pos))
  12361. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12362. (defun org-at-regexp-p (regexp)
  12363. "Is point inside a match of REGEXP in the current line?"
  12364. (catch 'exit
  12365. (save-excursion
  12366. (let ((pos (point)) (end (point-at-eol)))
  12367. (beginning-of-line 1)
  12368. (while (re-search-forward regexp end t)
  12369. (if (and (<= (match-beginning 0) pos)
  12370. (>= (match-end 0) pos))
  12371. (throw 'exit t)))
  12372. nil))))
  12373. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12374. "Call `multi-occur' with buffers for all agenda files."
  12375. (interactive "sOrg-files matching: \np")
  12376. (let* ((files (org-agenda-files))
  12377. (tnames (mapcar 'file-truename files))
  12378. (extra org-agenda-text-search-extra-files)
  12379. f)
  12380. (when (eq (car extra) 'agenda-archives)
  12381. (setq extra (cdr extra))
  12382. (setq files (org-add-archive-files files)))
  12383. (while (setq f (pop extra))
  12384. (unless (member (file-truename f) tnames)
  12385. (add-to-list 'files f 'append)
  12386. (add-to-list 'tnames (file-truename f) 'append)))
  12387. (multi-occur
  12388. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12389. regexp)))
  12390. (if (boundp 'occur-mode-find-occurrence-hook)
  12391. ;; Emacs 23
  12392. (add-hook 'occur-mode-find-occurrence-hook
  12393. (lambda ()
  12394. (when (org-mode-p)
  12395. (org-reveal))))
  12396. ;; Emacs 22
  12397. (defadvice occur-mode-goto-occurrence
  12398. (after org-occur-reveal activate)
  12399. (and (org-mode-p) (org-reveal)))
  12400. (defadvice occur-mode-goto-occurrence-other-window
  12401. (after org-occur-reveal activate)
  12402. (and (org-mode-p) (org-reveal)))
  12403. (defadvice occur-mode-display-occurrence
  12404. (after org-occur-reveal activate)
  12405. (when (org-mode-p)
  12406. (let ((pos (occur-mode-find-occurrence)))
  12407. (with-current-buffer (marker-buffer pos)
  12408. (save-excursion
  12409. (goto-char pos)
  12410. (org-reveal)))))))
  12411. (defun org-uniquify (list)
  12412. "Remove duplicate elements from LIST."
  12413. (let (res)
  12414. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12415. res))
  12416. (defun org-delete-all (elts list)
  12417. "Remove all elements in ELTS from LIST."
  12418. (while elts
  12419. (setq list (delete (pop elts) list)))
  12420. list)
  12421. (defun org-back-over-empty-lines ()
  12422. "Move backwards over witespace, to the beginning of the first empty line.
  12423. Returns the number of empty lines passed."
  12424. (let ((pos (point)))
  12425. (skip-chars-backward " \t\n\r")
  12426. (beginning-of-line 2)
  12427. (goto-char (min (point) pos))
  12428. (count-lines (point) pos)))
  12429. (defun org-skip-whitespace ()
  12430. (skip-chars-forward " \t\n\r"))
  12431. (defun org-point-in-group (point group &optional context)
  12432. "Check if POINT is in match-group GROUP.
  12433. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12434. match. If the match group does ot exist or point is not inside it,
  12435. return nil."
  12436. (and (match-beginning group)
  12437. (>= point (match-beginning group))
  12438. (<= point (match-end group))
  12439. (if context
  12440. (list context (match-beginning group) (match-end group))
  12441. t)))
  12442. (defun org-switch-to-buffer-other-window (&rest args)
  12443. "Switch to buffer in a second window on the current frame.
  12444. In particular, do not allow pop-up frames."
  12445. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12446. special-display-function)
  12447. (apply 'switch-to-buffer-other-window args)))
  12448. (defun org-combine-plists (&rest plists)
  12449. "Create a single property list from all plists in PLISTS.
  12450. The process starts by copying the first list, and then setting properties
  12451. from the other lists. Settings in the last list are the most significant
  12452. ones and overrule settings in the other lists."
  12453. (let ((rtn (copy-sequence (pop plists)))
  12454. p v ls)
  12455. (while plists
  12456. (setq ls (pop plists))
  12457. (while ls
  12458. (setq p (pop ls) v (pop ls))
  12459. (setq rtn (plist-put rtn p v))))
  12460. rtn))
  12461. (defun org-move-line-down (arg)
  12462. "Move the current line down. With prefix argument, move it past ARG lines."
  12463. (interactive "p")
  12464. (let ((col (current-column))
  12465. beg end pos)
  12466. (beginning-of-line 1) (setq beg (point))
  12467. (beginning-of-line 2) (setq end (point))
  12468. (beginning-of-line (+ 1 arg))
  12469. (setq pos (move-marker (make-marker) (point)))
  12470. (insert (delete-and-extract-region beg end))
  12471. (goto-char pos)
  12472. (org-move-to-column col)))
  12473. (defun org-move-line-up (arg)
  12474. "Move the current line up. With prefix argument, move it past ARG lines."
  12475. (interactive "p")
  12476. (let ((col (current-column))
  12477. beg end pos)
  12478. (beginning-of-line 1) (setq beg (point))
  12479. (beginning-of-line 2) (setq end (point))
  12480. (beginning-of-line (- arg))
  12481. (setq pos (move-marker (make-marker) (point)))
  12482. (insert (delete-and-extract-region beg end))
  12483. (goto-char pos)
  12484. (org-move-to-column col)))
  12485. (defun org-replace-escapes (string table)
  12486. "Replace %-escapes in STRING with values in TABLE.
  12487. TABLE is an association list with keys like \"%a\" and string values.
  12488. The sequences in STRING may contain normal field width and padding information,
  12489. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12490. so values can contain further %-escapes if they are define later in TABLE."
  12491. (let ((case-fold-search nil)
  12492. e re rpl)
  12493. (while (setq e (pop table))
  12494. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12495. (while (string-match re string)
  12496. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12497. (cdr e)))
  12498. (setq string (replace-match rpl t t string))))
  12499. string))
  12500. (defun org-sublist (list start end)
  12501. "Return a section of LIST, from START to END.
  12502. Counting starts at 1."
  12503. (let (rtn (c start))
  12504. (setq list (nthcdr (1- start) list))
  12505. (while (and list (<= c end))
  12506. (push (pop list) rtn)
  12507. (setq c (1+ c)))
  12508. (nreverse rtn)))
  12509. (defun org-find-base-buffer-visiting (file)
  12510. "Like `find-buffer-visiting' but alway return the base buffer and
  12511. not an indirect buffer."
  12512. (let ((buf (find-buffer-visiting file)))
  12513. (if buf
  12514. (or (buffer-base-buffer buf) buf)
  12515. nil)))
  12516. (defun org-image-file-name-regexp ()
  12517. "Return regexp matching the file names of images."
  12518. (if (fboundp 'image-file-name-regexp)
  12519. (image-file-name-regexp)
  12520. (let ((image-file-name-extensions
  12521. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12522. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12523. (concat "\\."
  12524. (regexp-opt (nconc (mapcar 'upcase
  12525. image-file-name-extensions)
  12526. image-file-name-extensions)
  12527. t)
  12528. "\\'"))))
  12529. (defun org-file-image-p (file)
  12530. "Return non-nil if FILE is an image."
  12531. (save-match-data
  12532. (string-match (org-image-file-name-regexp) file)))
  12533. (defun org-get-cursor-date ()
  12534. "Return the date at cursor in as a time.
  12535. This works in the calendar and in the agenda, anywhere else it just
  12536. returns the current time."
  12537. (let (date day defd)
  12538. (cond
  12539. ((eq major-mode 'calendar-mode)
  12540. (setq date (calendar-cursor-to-date)
  12541. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12542. ((eq major-mode 'org-agenda-mode)
  12543. (setq day (get-text-property (point) 'day))
  12544. (if day
  12545. (setq date (calendar-gregorian-from-absolute day)
  12546. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12547. (nth 2 date))))))
  12548. (or defd (current-time))))
  12549. (defvar org-agenda-action-marker (make-marker)
  12550. "Marker pointing to the entry for the next agenda action.")
  12551. (defun org-mark-entry-for-agenda-action ()
  12552. "Mark the current entry as target of an agenda action.
  12553. Agenda actions are actions executed from the agenda with the key `k',
  12554. which make use of the date at the cursor."
  12555. (interactive)
  12556. (move-marker org-agenda-action-marker
  12557. (save-excursion (org-back-to-heading t) (point))
  12558. (current-buffer))
  12559. (message
  12560. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12561. ;;; Paragraph filling stuff.
  12562. ;; We want this to be just right, so use the full arsenal.
  12563. (defun org-indent-line-function ()
  12564. "Indent line like previous, but further if previous was headline or item."
  12565. (interactive)
  12566. (let* ((pos (point))
  12567. (itemp (org-at-item-p))
  12568. column bpos bcol tpos tcol bullet btype bullet-type)
  12569. ;; Find the previous relevant line
  12570. (beginning-of-line 1)
  12571. (cond
  12572. ((looking-at "#") (setq column 0))
  12573. ((looking-at "\\*+ ") (setq column 0))
  12574. (t
  12575. (beginning-of-line 0)
  12576. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12577. (beginning-of-line 0))
  12578. (cond
  12579. ((looking-at "\\*+[ \t]+")
  12580. (if (not org-adapt-indentation)
  12581. (setq column 0)
  12582. (goto-char (match-end 0))
  12583. (setq column (current-column))))
  12584. ((org-in-item-p)
  12585. (org-beginning-of-item)
  12586. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12587. (setq bpos (match-beginning 1) tpos (match-end 0)
  12588. bcol (progn (goto-char bpos) (current-column))
  12589. tcol (progn (goto-char tpos) (current-column))
  12590. bullet (match-string 1)
  12591. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12592. (if (> tcol (+ bcol org-description-max-indent))
  12593. (setq tcol (+ bcol 5)))
  12594. (if (not itemp)
  12595. (setq column tcol)
  12596. (goto-char pos)
  12597. (beginning-of-line 1)
  12598. (if (looking-at "\\S-")
  12599. (progn
  12600. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12601. (setq bullet (match-string 1)
  12602. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12603. (setq column (if (equal btype bullet-type) bcol tcol)))
  12604. (setq column (org-get-indentation)))))
  12605. (t (setq column (org-get-indentation))))))
  12606. (goto-char pos)
  12607. (if (<= (current-column) (current-indentation))
  12608. (org-indent-line-to column)
  12609. (save-excursion (org-indent-line-to column)))
  12610. (setq column (current-column))
  12611. (beginning-of-line 1)
  12612. (if (looking-at
  12613. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12614. (replace-match (concat "\\1" (format org-property-format
  12615. (match-string 2) (match-string 3)))
  12616. t nil))
  12617. (org-move-to-column column)))
  12618. (defun org-set-autofill-regexps ()
  12619. (interactive)
  12620. ;; In the paragraph separator we include headlines, because filling
  12621. ;; text in a line directly attached to a headline would otherwise
  12622. ;; fill the headline as well.
  12623. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12624. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12625. ;; The paragraph starter includes hand-formatted lists.
  12626. (org-set-local 'paragraph-start
  12627. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12628. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12629. ;; But only if the user has not turned off tables or fixed-width regions
  12630. (org-set-local
  12631. 'auto-fill-inhibit-regexp
  12632. (concat "\\*+ \\|#\\+"
  12633. "\\|[ \t]*" org-keyword-time-regexp
  12634. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12635. (concat
  12636. "\\|[ \t]*["
  12637. (if org-enable-table-editor "|" "")
  12638. (if org-enable-fixed-width-editor ":" "")
  12639. "]"))))
  12640. ;; We use our own fill-paragraph function, to make sure that tables
  12641. ;; and fixed-width regions are not wrapped. That function will pass
  12642. ;; through to `fill-paragraph' when appropriate.
  12643. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12644. ; Adaptive filling: To get full control, first make sure that
  12645. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12646. (org-set-local 'adaptive-fill-regexp "\000")
  12647. (org-set-local 'adaptive-fill-function
  12648. 'org-adaptive-fill-function)
  12649. (org-set-local
  12650. 'align-mode-rules-list
  12651. '((org-in-buffer-settings
  12652. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12653. (modes . '(org-mode))))))
  12654. (defun org-fill-paragraph (&optional justify)
  12655. "Re-align a table, pass through to fill-paragraph if no table."
  12656. (let ((table-p (org-at-table-p))
  12657. (table.el-p (org-at-table.el-p)))
  12658. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12659. (save-excursion (goto-char (point-at-bol))
  12660. (looking-at outline-regexp)))
  12661. t) ; skip headlines
  12662. (table.el-p t) ; skip table.el tables
  12663. (table-p (org-table-align) t) ; align org-mode tables
  12664. (t nil)))) ; call paragraph-fill
  12665. ;; For reference, this is the default value of adaptive-fill-regexp
  12666. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12667. (defun org-adaptive-fill-function ()
  12668. "Return a fill prefix for org-mode files.
  12669. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12670. work correctly."
  12671. (cond ((looking-at "#[ \t]+")
  12672. (match-string 0))
  12673. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12674. (save-excursion
  12675. (if (> (match-end 1) (+ (match-beginning 1)
  12676. org-description-max-indent))
  12677. (goto-char (+ (match-beginning 1) 5))
  12678. (goto-char (match-end 0)))
  12679. (make-string (current-column) ?\ )))
  12680. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12681. (save-excursion
  12682. (goto-char (match-end 0))
  12683. (make-string (current-column) ?\ )))
  12684. (t nil)))
  12685. ;;; Other stuff.
  12686. (defun org-toggle-fixed-width-section (arg)
  12687. "Toggle the fixed-width export.
  12688. If there is no active region, the QUOTE keyword at the current headline is
  12689. inserted or removed. When present, it causes the text between this headline
  12690. and the next to be exported as fixed-width text, and unmodified.
  12691. If there is an active region, this command adds or removes a colon as the
  12692. first character of this line. If the first character of a line is a colon,
  12693. this line is also exported in fixed-width font."
  12694. (interactive "P")
  12695. (let* ((cc 0)
  12696. (regionp (org-region-active-p))
  12697. (beg (if regionp (region-beginning) (point)))
  12698. (end (if regionp (region-end)))
  12699. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12700. (case-fold-search nil)
  12701. (re "[ \t]*\\(:\\)")
  12702. off)
  12703. (if regionp
  12704. (save-excursion
  12705. (goto-char beg)
  12706. (setq cc (current-column))
  12707. (beginning-of-line 1)
  12708. (setq off (looking-at re))
  12709. (while (> nlines 0)
  12710. (setq nlines (1- nlines))
  12711. (beginning-of-line 1)
  12712. (cond
  12713. (arg
  12714. (org-move-to-column cc t)
  12715. (insert ":\n")
  12716. (forward-line -1))
  12717. ((and off (looking-at re))
  12718. (replace-match "" t t nil 1))
  12719. ((not off) (org-move-to-column cc t) (insert ":")))
  12720. (forward-line 1)))
  12721. (save-excursion
  12722. (org-back-to-heading)
  12723. (if (looking-at (concat outline-regexp
  12724. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12725. (replace-match "" t t nil 1)
  12726. (if (looking-at outline-regexp)
  12727. (progn
  12728. (goto-char (match-end 0))
  12729. (insert org-quote-string " "))))))))
  12730. ;;;; Functions extending outline functionality
  12731. (defun org-beginning-of-line (&optional arg)
  12732. "Go to the beginning of the current line. If that is invisible, continue
  12733. to a visible line beginning. This makes the function of C-a more intuitive.
  12734. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12735. first attempt, and only move to after the tags when the cursor is already
  12736. beyond the end of the headline."
  12737. (interactive "P")
  12738. (let ((pos (point)) refpos)
  12739. (beginning-of-line 1)
  12740. (if (bobp)
  12741. nil
  12742. (backward-char 1)
  12743. (if (org-invisible-p)
  12744. (while (and (not (bobp)) (org-invisible-p))
  12745. (backward-char 1)
  12746. (beginning-of-line 1))
  12747. (forward-char 1)))
  12748. (when org-special-ctrl-a/e
  12749. (cond
  12750. ((and (looking-at org-complex-heading-regexp)
  12751. (= (char-after (match-end 1)) ?\ ))
  12752. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12753. (point-at-eol)))
  12754. (goto-char
  12755. (if (eq org-special-ctrl-a/e t)
  12756. (cond ((> pos refpos) refpos)
  12757. ((= pos (point)) refpos)
  12758. (t (point)))
  12759. (cond ((> pos (point)) (point))
  12760. ((not (eq last-command this-command)) (point))
  12761. (t refpos)))))
  12762. ((org-at-item-p)
  12763. (goto-char
  12764. (if (eq org-special-ctrl-a/e t)
  12765. (cond ((> pos (match-end 4)) (match-end 4))
  12766. ((= pos (point)) (match-end 4))
  12767. (t (point)))
  12768. (cond ((> pos (point)) (point))
  12769. ((not (eq last-command this-command)) (point))
  12770. (t (match-end 4))))))))
  12771. (org-no-warnings
  12772. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12773. (defun org-end-of-line (&optional arg)
  12774. "Go to the end of the line.
  12775. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12776. first attempt, and only move to after the tags when the cursor is already
  12777. beyond the end of the headline."
  12778. (interactive "P")
  12779. (if (or (not org-special-ctrl-a/e)
  12780. (not (org-on-heading-p)))
  12781. (end-of-line arg)
  12782. (let ((pos (point)))
  12783. (beginning-of-line 1)
  12784. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12785. (if (eq org-special-ctrl-a/e t)
  12786. (if (or (< pos (match-beginning 1))
  12787. (= pos (match-end 0)))
  12788. (goto-char (match-beginning 1))
  12789. (goto-char (match-end 0)))
  12790. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12791. (goto-char (match-end 0))
  12792. (goto-char (match-beginning 1))))
  12793. (end-of-line arg))))
  12794. (org-no-warnings
  12795. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12796. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12797. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12798. (defun org-kill-line (&optional arg)
  12799. "Kill line, to tags or end of line."
  12800. (interactive "P")
  12801. (cond
  12802. ((or (not org-special-ctrl-k)
  12803. (bolp)
  12804. (not (org-on-heading-p)))
  12805. (call-interactively 'kill-line))
  12806. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12807. (kill-region (point) (match-beginning 1))
  12808. (org-set-tags nil t))
  12809. (t (kill-region (point) (point-at-eol)))))
  12810. (define-key org-mode-map "\C-k" 'org-kill-line)
  12811. (defun org-yank ()
  12812. "Yank. If the kill is a subtree, treat it specially.
  12813. This command will look at the current kill and check it is a single
  12814. subtree, or a series of subtrees[1]. If it passes the test, it is
  12815. treated specially, depending on the value of the following variables, both
  12816. set by default.
  12817. org-yank-folded-subtrees
  12818. When set, the subree(s) wiil be folded after insertion.
  12819. org-yank-adjusted-subtrees
  12820. When set, the subtree will be promoted or demoted in order to
  12821. fit into the local outline tree structure.
  12822. \[1] Basically, the test checks if the first non-white line is a heading
  12823. and if there are no other headings with fewer stars."
  12824. (interactive)
  12825. (let ((subtreep (org-kill-is-subtree-p)))
  12826. (if org-yank-folded-subtrees
  12827. (let ((beg (point))
  12828. end)
  12829. (if (and subtreep org-yank-adjusted-subtrees)
  12830. (org-paste-subtree nil nil 'for-yank)
  12831. (call-interactively 'yank))
  12832. (setq end (point))
  12833. (goto-char beg)
  12834. (when (and (bolp) subtreep)
  12835. (or (looking-at outline-regexp)
  12836. (re-search-forward (concat "^" outline-regexp) end t))
  12837. (while (and (< (point) end) (looking-at outline-regexp))
  12838. (hide-subtree)
  12839. (org-cycle-show-empty-lines 'folded)
  12840. (condition-case nil
  12841. (outline-forward-same-level 1)
  12842. (error (goto-char end)))))
  12843. (goto-char end)
  12844. (skip-chars-forward " \t\n\r"))
  12845. (if (and subtreep org-yank-adjusted-subtrees)
  12846. (org-paste-subtree nil nil 'for-yank)
  12847. (call-interactively 'yank)))))
  12848. (define-key org-mode-map "\C-y" 'org-yank)
  12849. (defun org-invisible-p ()
  12850. "Check if point is at a character currently not visible."
  12851. ;; Early versions of noutline don't have `outline-invisible-p'.
  12852. (if (fboundp 'outline-invisible-p)
  12853. (outline-invisible-p)
  12854. (get-char-property (point) 'invisible)))
  12855. (defun org-invisible-p2 ()
  12856. "Check if point is at a character currently not visible."
  12857. (save-excursion
  12858. (if (and (eolp) (not (bobp))) (backward-char 1))
  12859. ;; Early versions of noutline don't have `outline-invisible-p'.
  12860. (if (fboundp 'outline-invisible-p)
  12861. (outline-invisible-p)
  12862. (get-char-property (point) 'invisible))))
  12863. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12864. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12865. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12866. (defun org-at-heading-or-item-p ()
  12867. (or (org-on-heading-p) (org-at-item-p)))
  12868. (defun org-on-target-p ()
  12869. (or (org-in-regexp org-radio-target-regexp)
  12870. (org-in-regexp org-target-regexp)))
  12871. (defun org-up-heading-all (arg)
  12872. "Move to the heading line of which the present line is a subheading.
  12873. This function considers both visible and invisible heading lines.
  12874. With argument, move up ARG levels."
  12875. (if (fboundp 'outline-up-heading-all)
  12876. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12877. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12878. (defun org-up-heading-safe ()
  12879. "Move to the heading line of which the present line is a subheading.
  12880. This version will not throw an error. It will return the level of the
  12881. headline found, or nil if no higher level is found."
  12882. (let ((pos (point)) start-level level
  12883. (re (concat "^" outline-regexp)))
  12884. (catch 'exit
  12885. (outline-back-to-heading t)
  12886. (setq start-level (funcall outline-level))
  12887. (if (equal start-level 1) (throw 'exit nil))
  12888. (while (re-search-backward re nil t)
  12889. (setq level (funcall outline-level))
  12890. (if (< level start-level) (throw 'exit level)))
  12891. nil)))
  12892. (defun org-first-sibling-p ()
  12893. "Is this heading the first child of its parents?"
  12894. (interactive)
  12895. (let ((re (concat "^" outline-regexp))
  12896. level l)
  12897. (unless (org-at-heading-p t)
  12898. (error "Not at a heading"))
  12899. (setq level (funcall outline-level))
  12900. (save-excursion
  12901. (if (not (re-search-backward re nil t))
  12902. t
  12903. (setq l (funcall outline-level))
  12904. (< l level)))))
  12905. (defun org-goto-sibling (&optional previous)
  12906. "Goto the next sibling, even if it is invisible.
  12907. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12908. when a sibling was found. When none is found, return nil and don't
  12909. move point."
  12910. (let ((fun (if previous 're-search-backward 're-search-forward))
  12911. (pos (point))
  12912. (re (concat "^" outline-regexp))
  12913. level l)
  12914. (when (condition-case nil (org-back-to-heading t) (error nil))
  12915. (setq level (funcall outline-level))
  12916. (catch 'exit
  12917. (or previous (forward-char 1))
  12918. (while (funcall fun re nil t)
  12919. (setq l (funcall outline-level))
  12920. (when (< l level) (goto-char pos) (throw 'exit nil))
  12921. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12922. (goto-char pos)
  12923. nil))))
  12924. (defun org-show-siblings ()
  12925. "Show all siblings of the current headline."
  12926. (save-excursion
  12927. (while (org-goto-sibling) (org-flag-heading nil)))
  12928. (save-excursion
  12929. (while (org-goto-sibling 'previous)
  12930. (org-flag-heading nil))))
  12931. (defun org-show-hidden-entry ()
  12932. "Show an entry where even the heading is hidden."
  12933. (save-excursion
  12934. (org-show-entry)))
  12935. (defun org-flag-heading (flag &optional entry)
  12936. "Flag the current heading. FLAG non-nil means make invisible.
  12937. When ENTRY is non-nil, show the entire entry."
  12938. (save-excursion
  12939. (org-back-to-heading t)
  12940. ;; Check if we should show the entire entry
  12941. (if entry
  12942. (progn
  12943. (org-show-entry)
  12944. (save-excursion
  12945. (and (outline-next-heading)
  12946. (org-flag-heading nil))))
  12947. (outline-flag-region (max (point-min) (1- (point)))
  12948. (save-excursion (outline-end-of-heading) (point))
  12949. flag))))
  12950. (defun org-forward-same-level (arg)
  12951. "Move forward to the ARG'th subheading at same level as this one.
  12952. Stop at the first and last subheadings of a superior heading.
  12953. This is like outline-forward-same-level, but invisible headings are ok."
  12954. (interactive "p")
  12955. (outline-back-to-heading t)
  12956. (while (> arg 0)
  12957. (let ((point-to-move-to (save-excursion
  12958. (org-get-next-sibling))))
  12959. (if point-to-move-to
  12960. (progn
  12961. (goto-char point-to-move-to)
  12962. (setq arg (1- arg)))
  12963. (progn
  12964. (setq arg 0)
  12965. (error "No following same-level heading"))))))
  12966. (defun org-get-next-sibling ()
  12967. "Move to next heading of the same level, and return point.
  12968. If there is no such heading, return nil.
  12969. This is like outline-next-sibling, but invisible headings are ok."
  12970. (let ((level (funcall outline-level)))
  12971. (outline-next-heading)
  12972. (while (and (not (eobp)) (> (funcall outline-level) level))
  12973. (outline-next-heading))
  12974. (if (or (eobp) (< (funcall outline-level) level))
  12975. nil
  12976. (point))))
  12977. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12978. ;; This is an exact copy of the original function, but it uses
  12979. ;; `org-back-to-heading', to make it work also in invisible
  12980. ;; trees. And is uses an invisible-OK argument.
  12981. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12982. (org-back-to-heading invisible-OK)
  12983. (let ((first t)
  12984. (level (funcall outline-level)))
  12985. (while (and (not (eobp))
  12986. (or first (> (funcall outline-level) level)))
  12987. (setq first nil)
  12988. (outline-next-heading))
  12989. (unless to-heading
  12990. (if (memq (preceding-char) '(?\n ?\^M))
  12991. (progn
  12992. ;; Go to end of line before heading
  12993. (forward-char -1)
  12994. (if (memq (preceding-char) '(?\n ?\^M))
  12995. ;; leave blank line before heading
  12996. (forward-char -1))))))
  12997. (point))
  12998. (defun org-show-subtree ()
  12999. "Show everything after this heading at deeper levels."
  13000. (outline-flag-region
  13001. (point)
  13002. (save-excursion
  13003. (outline-end-of-subtree) (outline-next-heading) (point))
  13004. nil))
  13005. (defun org-show-entry ()
  13006. "Show the body directly following this heading.
  13007. Show the heading too, if it is currently invisible."
  13008. (interactive)
  13009. (save-excursion
  13010. (condition-case nil
  13011. (progn
  13012. (org-back-to-heading t)
  13013. (outline-flag-region
  13014. (max (point-min) (1- (point)))
  13015. (save-excursion
  13016. (re-search-forward
  13017. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13018. (or (match-beginning 1) (point-max)))
  13019. nil))
  13020. (error nil))))
  13021. (defun org-make-options-regexp (kwds)
  13022. "Make a regular expression for keyword lines."
  13023. (concat
  13024. "^"
  13025. "#?[ \t]*\\+\\("
  13026. (mapconcat 'regexp-quote kwds "\\|")
  13027. "\\):[ \t]*"
  13028. "\\(.+\\)"))
  13029. ;; Make isearch reveal the necessary context
  13030. (defun org-isearch-end ()
  13031. "Reveal context after isearch exits."
  13032. (when isearch-success ; only if search was successful
  13033. (if (featurep 'xemacs)
  13034. ;; Under XEmacs, the hook is run in the correct place,
  13035. ;; we directly show the context.
  13036. (org-show-context 'isearch)
  13037. ;; In Emacs the hook runs *before* restoring the overlays.
  13038. ;; So we have to use a one-time post-command-hook to do this.
  13039. ;; (Emacs 22 has a special variable, see function `org-mode')
  13040. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13041. isearch-mode-end-hook-quit)
  13042. ;; Only when the isearch was not quitted.
  13043. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13044. 'append 'local)))))
  13045. (defun org-isearch-post-command ()
  13046. "Remove self from hook, and show context."
  13047. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13048. (org-show-context 'isearch))
  13049. ;;;; Integration with and fixes for other packages
  13050. ;;; Imenu support
  13051. (defvar org-imenu-markers nil
  13052. "All markers currently used by Imenu.")
  13053. (make-variable-buffer-local 'org-imenu-markers)
  13054. (defun org-imenu-new-marker (&optional pos)
  13055. "Return a new marker for use by Imenu, and remember the marker."
  13056. (let ((m (make-marker)))
  13057. (move-marker m (or pos (point)))
  13058. (push m org-imenu-markers)
  13059. m))
  13060. (defun org-imenu-get-tree ()
  13061. "Produce the index for Imenu."
  13062. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13063. (setq org-imenu-markers nil)
  13064. (let* ((n org-imenu-depth)
  13065. (re (concat "^" outline-regexp))
  13066. (subs (make-vector (1+ n) nil))
  13067. (last-level 0)
  13068. m tree level head)
  13069. (save-excursion
  13070. (save-restriction
  13071. (widen)
  13072. (goto-char (point-max))
  13073. (while (re-search-backward re nil t)
  13074. (setq level (org-reduced-level (funcall outline-level)))
  13075. (when (<= level n)
  13076. (looking-at org-complex-heading-regexp)
  13077. (setq head (org-link-display-format
  13078. (org-match-string-no-properties 4))
  13079. m (org-imenu-new-marker))
  13080. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13081. (if (>= level last-level)
  13082. (push (cons head m) (aref subs level))
  13083. (push (cons head (aref subs (1+ level))) (aref subs level))
  13084. (loop for i from (1+ level) to n do (aset subs i nil)))
  13085. (setq last-level level)))))
  13086. (aref subs 1)))
  13087. (eval-after-load "imenu"
  13088. '(progn
  13089. (add-hook 'imenu-after-jump-hook
  13090. (lambda ()
  13091. (if (eq major-mode 'org-mode)
  13092. (org-show-context 'org-goto))))))
  13093. (defun org-link-display-format (link)
  13094. "Replace a link with either the description, or the link target
  13095. if no description is present"
  13096. (save-match-data
  13097. (if (string-match org-bracket-link-analytic-regexp link)
  13098. (replace-match (or (match-string 5 link)
  13099. (concat (match-string 1 link)
  13100. (match-string 3 link)))
  13101. nil nil link)
  13102. link)))
  13103. ;; Speedbar support
  13104. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13105. "Overlay marking the agenda restriction line in speedbar.")
  13106. (org-overlay-put org-speedbar-restriction-lock-overlay
  13107. 'face 'org-agenda-restriction-lock)
  13108. (org-overlay-put org-speedbar-restriction-lock-overlay
  13109. 'help-echo "Agendas are currently limited to this item.")
  13110. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13111. (defun org-speedbar-set-agenda-restriction ()
  13112. "Restrict future agenda commands to the location at point in speedbar.
  13113. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13114. (interactive)
  13115. (require 'org-agenda)
  13116. (let (p m tp np dir txt w)
  13117. (cond
  13118. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13119. 'org-imenu t))
  13120. (setq m (get-text-property p 'org-imenu-marker))
  13121. (save-excursion
  13122. (save-restriction
  13123. (set-buffer (marker-buffer m))
  13124. (goto-char m)
  13125. (org-agenda-set-restriction-lock 'subtree))))
  13126. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13127. 'speedbar-function 'speedbar-find-file))
  13128. (setq tp (previous-single-property-change
  13129. (1+ p) 'speedbar-function)
  13130. np (next-single-property-change
  13131. tp 'speedbar-function)
  13132. dir (speedbar-line-directory)
  13133. txt (buffer-substring-no-properties (or tp (point-min))
  13134. (or np (point-max))))
  13135. (save-excursion
  13136. (save-restriction
  13137. (set-buffer (find-file-noselect
  13138. (let ((default-directory dir))
  13139. (expand-file-name txt))))
  13140. (unless (org-mode-p)
  13141. (error "Cannot restrict to non-Org-mode file"))
  13142. (org-agenda-set-restriction-lock 'file))))
  13143. (t (error "Don't know how to restrict Org-mode's agenda")))
  13144. (org-move-overlay org-speedbar-restriction-lock-overlay
  13145. (point-at-bol) (point-at-eol))
  13146. (setq current-prefix-arg nil)
  13147. (org-agenda-maybe-redo)))
  13148. (eval-after-load "speedbar"
  13149. '(progn
  13150. (speedbar-add-supported-extension ".org")
  13151. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13152. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13153. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13154. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13155. (add-hook 'speedbar-visiting-tag-hook
  13156. (lambda () (org-show-context 'org-goto)))))
  13157. ;;; Fixes and Hacks for problems with other packages
  13158. ;; Make flyspell not check words in links, to not mess up our keymap
  13159. (defun org-mode-flyspell-verify ()
  13160. "Don't let flyspell put overlays at active buttons."
  13161. (not (get-text-property (point) 'keymap)))
  13162. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13163. (eval-after-load "bookmark"
  13164. '(if (boundp 'bookmark-after-jump-hook)
  13165. ;; We can use the hook
  13166. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13167. ;; Hook not available, use advice
  13168. (defadvice bookmark-jump (after org-make-visible activate)
  13169. "Make the position visible."
  13170. (org-bookmark-jump-unhide))))
  13171. ;; Make sure saveplace show the location if it was hidden
  13172. (eval-after-load "saveplace"
  13173. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13174. "Make the position visible."
  13175. (org-bookmark-jump-unhide)))
  13176. (defun org-bookmark-jump-unhide ()
  13177. "Unhide the current position, to show the bookmark location."
  13178. (and (org-mode-p)
  13179. (or (org-invisible-p)
  13180. (save-excursion (goto-char (max (point-min) (1- (point))))
  13181. (org-invisible-p)))
  13182. (org-show-context 'bookmark-jump)))
  13183. ;; Make session.el ignore our circular variable
  13184. (eval-after-load "session"
  13185. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13186. ;;;; Experimental code
  13187. (defun org-closed-in-range ()
  13188. "Sparse tree of items closed in a certain time range.
  13189. Still experimental, may disappear in the future."
  13190. (interactive)
  13191. ;; Get the time interval from the user.
  13192. (let* ((time1 (time-to-seconds
  13193. (org-read-date nil 'to-time nil "Starting date: ")))
  13194. (time2 (time-to-seconds
  13195. (org-read-date nil 'to-time nil "End date:")))
  13196. ;; callback function
  13197. (callback (lambda ()
  13198. (let ((time
  13199. (time-to-seconds
  13200. (apply 'encode-time
  13201. (org-parse-time-string
  13202. (match-string 1))))))
  13203. ;; check if time in interval
  13204. (and (>= time time1) (<= time time2))))))
  13205. ;; make tree, check each match with the callback
  13206. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13207. ;;;; Finish up
  13208. (provide 'org)
  13209. (run-hooks 'org-load-hook)
  13210. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13211. ;;; org.el ends here