org.el 529 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296
  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.09a
  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.09a"
  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-M-RET-may-split-line '((default . t))
  567. "Non-nil means, M-RET will split the line at the cursor position.
  568. When nil, it will go to the end of the line before making a
  569. new line.
  570. You may also set this option in a different way for different
  571. contexts. Valid contexts are:
  572. headline when creating a new headline
  573. item when creating a new item
  574. table in a table field
  575. default the value to be used for all contexts not explicitly
  576. customized"
  577. :group 'org-structure
  578. :group 'org-table
  579. :type '(choice
  580. (const :tag "Always" t)
  581. (const :tag "Never" nil)
  582. (repeat :greedy t :tag "Individual contexts"
  583. (cons
  584. (choice :tag "Context"
  585. (const headline)
  586. (const item)
  587. (const table)
  588. (const default))
  589. (boolean)))))
  590. (defcustom org-insert-heading-respect-content nil
  591. "Non-nil means, insert new headings after the current subtree.
  592. When nil, the new heading is created directly after the current line.
  593. The commands \\[org-insert-heading-respect-content] and
  594. \\[org-insert-todo-heading-respect-content] turn this variable on
  595. for the duration of the command."
  596. :group 'org-structure
  597. :type 'boolean)
  598. (defcustom org-blank-before-new-entry '((heading . nil)
  599. (plain-list-item . nil))
  600. "Should `org-insert-heading' leave a blank line before new heading/item?
  601. The value is an alist, with `heading' and `plain-list-item' as car,
  602. and a boolean flag as cdr."
  603. :group 'org-edit-structure
  604. :type '(list
  605. (cons (const heading) (boolean))
  606. (cons (const plain-list-item) (boolean))))
  607. (defcustom org-insert-heading-hook nil
  608. "Hook being run after inserting a new heading."
  609. :group 'org-edit-structure
  610. :type 'hook)
  611. (defcustom org-enable-fixed-width-editor t
  612. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  613. This currently only means, they are never auto-wrapped.
  614. When nil, such lines will be treated like ordinary lines.
  615. See also the QUOTE keyword."
  616. :group 'org-edit-structure
  617. :type 'boolean)
  618. (defcustom org-edit-src-region-extra nil
  619. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  620. For examples see the function `org-edit-src-find-region-and-lang'.
  621. The regular expression identifying the begin marker should end with a newline,
  622. and the regexp marking the end line should start with a newline, to make sure
  623. there are kept outside the narrowed region."
  624. :group 'org-edit-structure
  625. :type '(repeat
  626. (list
  627. (regexp :tag "begin regexp")
  628. (regexp :tag "end regexp")
  629. (choice :tag "language"
  630. (string :tag "specify")
  631. (integer :tag "from match group")
  632. (const :tag "from `lang' element")
  633. (const :tag "from `style' element")))))
  634. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  635. "The mode that should be used to edit fixed-width regions.
  636. These are the regions where each line starts with a colon."
  637. :group 'org-edit-structure
  638. :type '(choice
  639. (const artist-mode)
  640. (const picture-mode)
  641. (const fundamental-mode)
  642. (function :tag "Other (specify)")))
  643. (defcustom org-goto-auto-isearch t
  644. "Non-nil means, typing characters in org-goto starts incremental search."
  645. :group 'org-edit-structure
  646. :type 'boolean)
  647. (defgroup org-sparse-trees nil
  648. "Options concerning sparse trees in Org-mode."
  649. :tag "Org Sparse Trees"
  650. :group 'org-structure)
  651. (defcustom org-highlight-sparse-tree-matches t
  652. "Non-nil means, highlight all matches that define a sparse tree.
  653. The highlights will automatically disappear the next time the buffer is
  654. changed by an edit command."
  655. :group 'org-sparse-trees
  656. :type 'boolean)
  657. (defcustom org-remove-highlights-with-change t
  658. "Non-nil means, any change to the buffer will remove temporary highlights.
  659. Such highlights are created by `org-occur' and `org-clock-display'.
  660. When nil, `C-c C-c needs to be used to get rid of the highlights.
  661. The highlights created by `org-preview-latex-fragment' always need
  662. `C-c C-c' to be removed."
  663. :group 'org-sparse-trees
  664. :group 'org-time
  665. :type 'boolean)
  666. (defcustom org-occur-hook '(org-first-headline-recenter)
  667. "Hook that is run after `org-occur' has constructed a sparse tree.
  668. This can be used to recenter the window to show as much of the structure
  669. as possible."
  670. :group 'org-sparse-trees
  671. :type 'hook)
  672. (defgroup org-imenu-and-speedbar nil
  673. "Options concerning imenu and speedbar in Org-mode."
  674. :tag "Org Imenu and Speedbar"
  675. :group 'org-structure)
  676. (defcustom org-imenu-depth 2
  677. "The maximum level for Imenu access to Org-mode headlines.
  678. This also applied for speedbar access."
  679. :group 'org-imenu-and-speedbar
  680. :type 'number)
  681. (defgroup org-table nil
  682. "Options concerning tables in Org-mode."
  683. :tag "Org Table"
  684. :group 'org)
  685. (defcustom org-enable-table-editor 'optimized
  686. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  687. When nil, such lines will be treated like ordinary lines.
  688. When equal to the symbol `optimized', the table editor will be optimized to
  689. do the following:
  690. - Automatic overwrite mode in front of whitespace in table fields.
  691. This makes the structure of the table stay in tact as long as the edited
  692. field does not exceed the column width.
  693. - Minimize the number of realigns. Normally, the table is aligned each time
  694. TAB or RET are pressed to move to another field. With optimization this
  695. happens only if changes to a field might have changed the column width.
  696. Optimization requires replacing the functions `self-insert-command',
  697. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  698. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  699. very good at guessing when a re-align will be necessary, but you can always
  700. force one with \\[org-ctrl-c-ctrl-c].
  701. If you would like to use the optimized version in Org-mode, but the
  702. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  703. This variable can be used to turn on and off the table editor during a session,
  704. but in order to toggle optimization, a restart is required.
  705. See also the variable `org-table-auto-blank-field'."
  706. :group 'org-table
  707. :type '(choice
  708. (const :tag "off" nil)
  709. (const :tag "on" t)
  710. (const :tag "on, optimized" optimized)))
  711. (defcustom org-table-tab-recognizes-table.el t
  712. "Non-nil means, TAB will automatically notice a table.el table.
  713. When it sees such a table, it moves point into it and - if necessary -
  714. calls `table-recognize-table'."
  715. :group 'org-table-editing
  716. :type 'boolean)
  717. (defgroup org-link nil
  718. "Options concerning links in Org-mode."
  719. :tag "Org Link"
  720. :group 'org)
  721. (defvar org-link-abbrev-alist-local nil
  722. "Buffer-local version of `org-link-abbrev-alist', which see.
  723. The value of this is taken from the #+LINK lines.")
  724. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  725. (defcustom org-link-abbrev-alist nil
  726. "Alist of link abbreviations.
  727. The car of each element is a string, to be replaced at the start of a link.
  728. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  729. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  730. [[linkkey:tag][description]]
  731. If REPLACE is a string, the tag will simply be appended to create the link.
  732. If the string contains \"%s\", the tag will be inserted there.
  733. REPLACE may also be a function that will be called with the tag as the
  734. only argument to create the link, which should be returned as a string.
  735. See the manual for examples."
  736. :group 'org-link
  737. :type 'alist)
  738. (defcustom org-descriptive-links t
  739. "Non-nil means, hide link part and only show description of bracket links.
  740. Bracket links are like [[link][descritpion]]. This variable sets the initial
  741. state in new org-mode buffers. The setting can then be toggled on a
  742. per-buffer basis from the Org->Hyperlinks menu."
  743. :group 'org-link
  744. :type 'boolean)
  745. (defcustom org-link-file-path-type 'adaptive
  746. "How the path name in file links should be stored.
  747. Valid values are:
  748. relative Relative to the current directory, i.e. the directory of the file
  749. into which the link is being inserted.
  750. absolute Absolute path, if possible with ~ for home directory.
  751. noabbrev Absolute path, no abbreviation of home directory.
  752. adaptive Use relative path for files in the current directory and sub-
  753. directories of it. For other files, use an absolute path."
  754. :group 'org-link
  755. :type '(choice
  756. (const relative)
  757. (const absolute)
  758. (const noabbrev)
  759. (const adaptive)))
  760. (defcustom org-activate-links '(bracket angle plain radio tag date)
  761. "Types of links that should be activated in Org-mode files.
  762. This is a list of symbols, each leading to the activation of a certain link
  763. type. In principle, it does not hurt to turn on most link types - there may
  764. be a small gain when turning off unused link types. The types are:
  765. bracket The recommended [[link][description]] or [[link]] links with hiding.
  766. angular Links in angular brackes that may contain whitespace like
  767. <bbdb:Carsten Dominik>.
  768. plain Plain links in normal text, no whitespace, like http://google.com.
  769. radio Text that is matched by a radio target, see manual for details.
  770. tag Tag settings in a headline (link to tag search).
  771. date Time stamps (link to calendar).
  772. Changing this variable requires a restart of Emacs to become effective."
  773. :group 'org-link
  774. :type '(set (const :tag "Double bracket links (new style)" bracket)
  775. (const :tag "Angular bracket links (old style)" angular)
  776. (const :tag "Plain text links" plain)
  777. (const :tag "Radio target matches" radio)
  778. (const :tag "Tags" tag)
  779. (const :tag "Timestamps" date)))
  780. (defcustom org-make-link-description-function nil
  781. "Function to use to generate link descriptions from links. If
  782. nil the link location will be used. This function must take two
  783. parameters; the first is the link and the second the description
  784. org-insert-link has generated, and should return the description
  785. to use."
  786. :group 'org-link
  787. :type 'function)
  788. (defgroup org-link-store nil
  789. "Options concerning storing links in Org-mode."
  790. :tag "Org Store Link"
  791. :group 'org-link)
  792. (defcustom org-email-link-description-format "Email %c: %.30s"
  793. "Format of the description part of a link to an email or usenet message.
  794. The following %-excapes will be replaced by corresponding information:
  795. %F full \"From\" field
  796. %f name, taken from \"From\" field, address if no name
  797. %T full \"To\" field
  798. %t first name in \"To\" field, address if no name
  799. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  800. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  801. %s subject
  802. %m message-id.
  803. You may use normal field width specification between the % and the letter.
  804. This is for example useful to limit the length of the subject.
  805. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  806. :group 'org-link-store
  807. :type 'string)
  808. (defcustom org-from-is-user-regexp
  809. (let (r1 r2)
  810. (when (and user-mail-address (not (string= user-mail-address "")))
  811. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  812. (when (and user-full-name (not (string= user-full-name "")))
  813. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  814. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  815. "Regexp mached against the \"From:\" header of an email or usenet message.
  816. It should match if the message is from the user him/herself."
  817. :group 'org-link-store
  818. :type 'regexp)
  819. (defcustom org-context-in-file-links t
  820. "Non-nil means, file links from `org-store-link' contain context.
  821. A search string will be added to the file name with :: as separator and
  822. used to find the context when the link is activated by the command
  823. `org-open-at-point'.
  824. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  825. negates this setting for the duration of the command."
  826. :group 'org-link-store
  827. :type 'boolean)
  828. (defcustom org-keep-stored-link-after-insertion nil
  829. "Non-nil means, keep link in list for entire session.
  830. The command `org-store-link' adds a link pointing to the current
  831. location to an internal list. These links accumulate during a session.
  832. The command `org-insert-link' can be used to insert links into any
  833. Org-mode file (offering completion for all stored links). When this
  834. option is nil, every link which has been inserted once using \\[org-insert-link]
  835. will be removed from the list, to make completing the unused links
  836. more efficient."
  837. :group 'org-link-store
  838. :type 'boolean)
  839. (defgroup org-link-follow nil
  840. "Options concerning following links in Org-mode."
  841. :tag "Org Follow Link"
  842. :group 'org-link)
  843. (defcustom org-follow-link-hook nil
  844. "Hook that is run after a link has been followed."
  845. :group 'org-link-follow
  846. :type 'hook)
  847. (defcustom org-tab-follows-link nil
  848. "Non-nil means, on links TAB will follow the link.
  849. Needs to be set before org.el is loaded."
  850. :group 'org-link-follow
  851. :type 'boolean)
  852. (defcustom org-return-follows-link nil
  853. "Non-nil means, on links RET will follow the link.
  854. Needs to be set before org.el is loaded."
  855. :group 'org-link-follow
  856. :type 'boolean)
  857. (defcustom org-mouse-1-follows-link
  858. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  859. "Non-nil means, mouse-1 on a link will follow the link.
  860. A longer mouse click will still set point. Does not work on XEmacs.
  861. Needs to be set before org.el is loaded."
  862. :group 'org-link-follow
  863. :type 'boolean)
  864. (defcustom org-mark-ring-length 4
  865. "Number of different positions to be recorded in the ring
  866. Changing this requires a restart of Emacs to work correctly."
  867. :group 'org-link-follow
  868. :type 'interger)
  869. (defcustom org-link-frame-setup
  870. '((vm . vm-visit-folder-other-frame)
  871. (gnus . gnus-other-frame)
  872. (file . find-file-other-window))
  873. "Setup the frame configuration for following links.
  874. When following a link with Emacs, it may often be useful to display
  875. this link in another window or frame. This variable can be used to
  876. set this up for the different types of links.
  877. For VM, use any of
  878. `vm-visit-folder'
  879. `vm-visit-folder-other-frame'
  880. For Gnus, use any of
  881. `gnus'
  882. `gnus-other-frame'
  883. For FILE, use any of
  884. `find-file'
  885. `find-file-other-window'
  886. `find-file-other-frame'
  887. For the calendar, use the variable `calendar-setup'.
  888. For BBDB, it is currently only possible to display the matches in
  889. another window."
  890. :group 'org-link-follow
  891. :type '(list
  892. (cons (const vm)
  893. (choice
  894. (const vm-visit-folder)
  895. (const vm-visit-folder-other-window)
  896. (const vm-visit-folder-other-frame)))
  897. (cons (const gnus)
  898. (choice
  899. (const gnus)
  900. (const gnus-other-frame)))
  901. (cons (const file)
  902. (choice
  903. (const find-file)
  904. (const find-file-other-window)
  905. (const find-file-other-frame)))))
  906. (defcustom org-display-internal-link-with-indirect-buffer nil
  907. "Non-nil means, use indirect buffer to display infile links.
  908. Activating internal links (from one location in a file to another location
  909. in the same file) normally just jumps to the location. When the link is
  910. activated with a C-u prefix (or with mouse-3), the link is displayed in
  911. another window. When this option is set, the other window actually displays
  912. an indirect buffer clone of the current buffer, to avoid any visibility
  913. changes to the current buffer."
  914. :group 'org-link-follow
  915. :type 'boolean)
  916. (defcustom org-open-non-existing-files nil
  917. "Non-nil means, `org-open-file' will open non-existing files.
  918. When nil, an error will be generated."
  919. :group 'org-link-follow
  920. :type 'boolean)
  921. (defcustom org-open-directory-means-index-dot-org nil
  922. "Non-nil means, a link to a directory really means to index.org.
  923. When nil, following a directory link will run dired or open a finder/explorer
  924. window on that directory."
  925. :group 'org-link-follow
  926. :type 'boolean)
  927. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  928. "Function and arguments to call for following mailto links.
  929. This is a list with the first element being a lisp function, and the
  930. remaining elements being arguments to the function. In string arguments,
  931. %a will be replaced by the address, and %s will be replaced by the subject
  932. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  933. :group 'org-link-follow
  934. :type '(choice
  935. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  936. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  937. (const :tag "message-mail" (message-mail "%a" "%s"))
  938. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  939. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  940. "Non-nil means, ask for confirmation before executing shell links.
  941. Shell links can be dangerous: just think about a link
  942. [[shell:rm -rf ~/*][Google Search]]
  943. This link would show up in your Org-mode document as \"Google Search\",
  944. but really it would remove your entire home directory.
  945. Therefore we advise against setting this variable to nil.
  946. Just change it to `y-or-n-p' of you want to confirm with a
  947. single keystroke rather than having to type \"yes\"."
  948. :group 'org-link-follow
  949. :type '(choice
  950. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  951. (const :tag "with y-or-n (faster)" y-or-n-p)
  952. (const :tag "no confirmation (dangerous)" nil)))
  953. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  954. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  955. Elisp links can be dangerous: just think about a link
  956. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  957. This link would show up in your Org-mode document as \"Google Search\",
  958. but really it would remove your entire home directory.
  959. Therefore we advise against setting this variable to nil.
  960. Just change it to `y-or-n-p' of you want to confirm with a
  961. single keystroke rather than having to type \"yes\"."
  962. :group 'org-link-follow
  963. :type '(choice
  964. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  965. (const :tag "with y-or-n (faster)" y-or-n-p)
  966. (const :tag "no confirmation (dangerous)" nil)))
  967. (defconst org-file-apps-defaults-gnu
  968. '((remote . emacs)
  969. (t . mailcap))
  970. "Default file applications on a UNIX or GNU/Linux system.
  971. See `org-file-apps'.")
  972. (defconst org-file-apps-defaults-macosx
  973. '((remote . emacs)
  974. (t . "open %s")
  975. ("ps.gz" . "gv %s")
  976. ("eps.gz" . "gv %s")
  977. ("dvi" . "xdvi %s")
  978. ("fig" . "xfig %s"))
  979. "Default file applications on a MacOS X system.
  980. The system \"open\" is known as a default, but we use X11 applications
  981. for some files for which the OS does not have a good default.
  982. See `org-file-apps'.")
  983. (defconst org-file-apps-defaults-windowsnt
  984. (list
  985. '(remote . emacs)
  986. (cons t
  987. (list (if (featurep 'xemacs)
  988. 'mswindows-shell-execute
  989. 'w32-shell-execute)
  990. "open" 'file)))
  991. "Default file applications on a Windows NT system.
  992. The system \"open\" is used for most files.
  993. See `org-file-apps'.")
  994. (defcustom org-file-apps
  995. '(
  996. (auto-mode . emacs)
  997. ("\\.x?html?\\'" . default)
  998. ("\\.pdf\\'" . default)
  999. )
  1000. "External applications for opening `file:path' items in a document.
  1001. Org-mode uses system defaults for different file types, but
  1002. you can use this variable to set the application for a given file
  1003. extension. The entries in this list are cons cells where the car identifies
  1004. files and the cdr the corresponding command. Possible values for the
  1005. file identifier are
  1006. \"regex\" Regular expression matched against the file name. For backward
  1007. compatibility, this can also be a string with only alphanumeric
  1008. characters, which is then interpreted as an extension.
  1009. `directory' Matches a directory
  1010. `remote' Matches a remote file, accessible through tramp or efs.
  1011. Remote files most likely should be visited through Emacs
  1012. because external applications cannot handle such paths.
  1013. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1014. so all files Emacs knows how to handle. Useing this with
  1015. command `emacs' will open most files in Emacs. Beware that this
  1016. will also open html files insite Emacs, unless you add
  1017. (\"html\" . default) to the list as well.
  1018. t Default for files not matched by any of the other options.
  1019. Possible values for the command are:
  1020. `emacs' The file will be visited by the current Emacs process.
  1021. `default' Use the default application for this file type, which is the
  1022. association for t in the list, most likely in the system-specific
  1023. part.
  1024. This can be used to overrule an unwanted seting in the
  1025. system-specific variable.
  1026. string A command to be executed by a shell; %s will be replaced
  1027. by the path to the file.
  1028. sexp A Lisp form which will be evaluated. The file path will
  1029. be available in the Lisp variable `file'.
  1030. For more examples, see the system specific constants
  1031. `org-file-apps-defaults-macosx'
  1032. `org-file-apps-defaults-windowsnt'
  1033. `org-file-apps-defaults-gnu'."
  1034. :group 'org-link-follow
  1035. :type '(repeat
  1036. (cons (choice :value ""
  1037. (string :tag "Extension")
  1038. (const :tag "Default for unrecognized files" t)
  1039. (const :tag "Remote file" remote)
  1040. (const :tag "Links to a directory" directory)
  1041. (const :tag "Any files that have Emacs modes"
  1042. auto-mode))
  1043. (choice :value ""
  1044. (const :tag "Visit with Emacs" emacs)
  1045. (const :tag "Use system default" default)
  1046. (string :tag "Command")
  1047. (sexp :tag "Lisp form")))))
  1048. (defgroup org-refile nil
  1049. "Options concerning refiling entries in Org-mode."
  1050. :tag "Org Remember"
  1051. :group 'org)
  1052. (defcustom org-directory "~/org"
  1053. "Directory with org files.
  1054. This directory will be used as default to prompt for org files.
  1055. Used by the hooks for remember.el."
  1056. :group 'org-refile
  1057. :group 'org-remember
  1058. :type 'directory)
  1059. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1060. "Default target for storing notes.
  1061. Used by the hooks for remember.el. This can be a string, or nil to mean
  1062. the value of `remember-data-file'.
  1063. You can set this on a per-template basis with the variable
  1064. `org-remember-templates'."
  1065. :group 'org-refile
  1066. :group 'org-remember
  1067. :type '(choice
  1068. (const :tag "Default from remember-data-file" nil)
  1069. file))
  1070. (defcustom org-goto-interface 'outline
  1071. "The default interface to be used for `org-goto'.
  1072. Allowed vaues are:
  1073. outline The interface shows an outline of the relevant file
  1074. and the correct heading is found by moving through
  1075. the outline or by searching with incremental search.
  1076. outline-path-completion Headlines in the current buffer are offered via
  1077. completion."
  1078. :group 'org-refile
  1079. :type '(choice
  1080. (const :tag "Outline" outline)
  1081. (const :tag "Outline-path-completion" outline-path-completion)))
  1082. (defcustom org-reverse-note-order nil
  1083. "Non-nil means, store new notes at the beginning of a file or entry.
  1084. When nil, new notes will be filed to the end of a file or entry.
  1085. This can also be a list with cons cells of regular expressions that
  1086. are matched against file names, and values."
  1087. :group 'org-remember
  1088. :type '(choice
  1089. (const :tag "Reverse always" t)
  1090. (const :tag "Reverse never" nil)
  1091. (repeat :tag "By file name regexp"
  1092. (cons regexp boolean))))
  1093. (defcustom org-refile-targets nil
  1094. "Targets for refiling entries with \\[org-refile].
  1095. This is list of cons cells. Each cell contains:
  1096. - a specification of the files to be considered, either a list of files,
  1097. or a symbol whose function or variable value will be used to retrieve
  1098. a file name or a list of file names. Nil means, refile to a different
  1099. heading in the current buffer.
  1100. - A specification of how to find candidate refile targets. This may be
  1101. any of
  1102. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1103. This tag has to be present in all target headlines, inheritance will
  1104. not be considered.
  1105. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1106. todo keyword.
  1107. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1108. headlines that are refiling targets.
  1109. - a cons cell (:level . N). Any headline of level N is considered a target.
  1110. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1111. When this variable is nil, all top-level headlines in the current buffer
  1112. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1113. :group 'org-remember
  1114. :type '(repeat
  1115. (cons
  1116. (choice :value org-agenda-files
  1117. (const :tag "All agenda files" org-agenda-files)
  1118. (const :tag "Current buffer" nil)
  1119. (function) (variable) (file))
  1120. (choice :tag "Identify target headline by"
  1121. (cons :tag "Specific tag" (const :tag) (string))
  1122. (cons :tag "TODO keyword" (const :todo) (string))
  1123. (cons :tag "Regular expression" (const :regexp) (regexp))
  1124. (cons :tag "Level number" (const :level) (integer))
  1125. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1126. (defcustom org-refile-use-outline-path nil
  1127. "Non-nil means, provide refile targets as paths.
  1128. So a level 3 headline will be available as level1/level2/level3.
  1129. When the value is `file', also include the file name (without directory)
  1130. into the path. When `full-file-path', include the full file path."
  1131. :group 'org-remember
  1132. :type '(choice
  1133. (const :tag "Not" nil)
  1134. (const :tag "Yes" t)
  1135. (const :tag "Start with file name" file)
  1136. (const :tag "Start with full file path" full-file-path)))
  1137. (defgroup org-todo nil
  1138. "Options concerning TODO items in Org-mode."
  1139. :tag "Org TODO"
  1140. :group 'org)
  1141. (defgroup org-progress nil
  1142. "Options concerning Progress logging in Org-mode."
  1143. :tag "Org Progress"
  1144. :group 'org-time)
  1145. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1146. "List of TODO entry keyword sequences and their interpretation.
  1147. \\<org-mode-map>This is a list of sequences.
  1148. Each sequence starts with a symbol, either `sequence' or `type',
  1149. indicating if the keywords should be interpreted as a sequence of
  1150. action steps, or as different types of TODO items. The first
  1151. keywords are states requiring action - these states will select a headline
  1152. for inclusion into the global TODO list Org-mode produces. If one of
  1153. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1154. signify that no further action is necessary. If \"|\" is not found,
  1155. the last keyword is treated as the only DONE state of the sequence.
  1156. The command \\[org-todo] cycles an entry through these states, and one
  1157. additional state where no keyword is present. For details about this
  1158. cycling, see the manual.
  1159. TODO keywords and interpretation can also be set on a per-file basis with
  1160. the special #+SEQ_TODO and #+TYP_TODO lines.
  1161. Each keyword can optionally specify a character for fast state selection
  1162. \(in combination with the variable `org-use-fast-todo-selection')
  1163. and specifiers for state change logging, using the same syntax
  1164. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1165. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1166. indicates to record a time stamp each time this state is selected.
  1167. Each keyword may also specify if a timestamp or a note should be
  1168. recorded when entering or leaving the state, by adding additional
  1169. characters in the parenthesis after the keyword. This looks like this:
  1170. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1171. record only the time of the state change. With X and Y being either
  1172. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1173. Y when leaving the state if and only if the *target* state does not
  1174. define X. You may omit any of the fast-selection key or X or /Y,
  1175. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1176. For backward compatibility, this variable may also be just a list
  1177. of keywords - in this case the interptetation (sequence or type) will be
  1178. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1179. :group 'org-todo
  1180. :group 'org-keywords
  1181. :type '(choice
  1182. (repeat :tag "Old syntax, just keywords"
  1183. (string :tag "Keyword"))
  1184. (repeat :tag "New syntax"
  1185. (cons
  1186. (choice
  1187. :tag "Interpretation"
  1188. (const :tag "Sequence (cycling hits every state)" sequence)
  1189. (const :tag "Type (cycling directly to DONE)" type))
  1190. (repeat
  1191. (string :tag "Keyword"))))))
  1192. (defvar org-todo-keywords-1 nil
  1193. "All TODO and DONE keywords active in a buffer.")
  1194. (make-variable-buffer-local 'org-todo-keywords-1)
  1195. (defvar org-todo-keywords-for-agenda nil)
  1196. (defvar org-done-keywords-for-agenda nil)
  1197. (defvar org-todo-keyword-alist-for-agenda nil)
  1198. (defvar org-tag-alist-for-agenda nil)
  1199. (defvar org-agenda-contributing-files nil)
  1200. (defvar org-not-done-keywords nil)
  1201. (make-variable-buffer-local 'org-not-done-keywords)
  1202. (defvar org-done-keywords nil)
  1203. (make-variable-buffer-local 'org-done-keywords)
  1204. (defvar org-todo-heads nil)
  1205. (make-variable-buffer-local 'org-todo-heads)
  1206. (defvar org-todo-sets nil)
  1207. (make-variable-buffer-local 'org-todo-sets)
  1208. (defvar org-todo-log-states nil)
  1209. (make-variable-buffer-local 'org-todo-log-states)
  1210. (defvar org-todo-kwd-alist nil)
  1211. (make-variable-buffer-local 'org-todo-kwd-alist)
  1212. (defvar org-todo-key-alist nil)
  1213. (make-variable-buffer-local 'org-todo-key-alist)
  1214. (defvar org-todo-key-trigger nil)
  1215. (make-variable-buffer-local 'org-todo-key-trigger)
  1216. (defcustom org-todo-interpretation 'sequence
  1217. "Controls how TODO keywords are interpreted.
  1218. This variable is in principle obsolete and is only used for
  1219. backward compatibility, if the interpretation of todo keywords is
  1220. not given already in `org-todo-keywords'. See that variable for
  1221. more information."
  1222. :group 'org-todo
  1223. :group 'org-keywords
  1224. :type '(choice (const sequence)
  1225. (const type)))
  1226. (defcustom org-use-fast-todo-selection 'prefix
  1227. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1228. This variable describes if and under what circumstances the cycling
  1229. mechanism for TODO keywords will be replaced by a single-key, direct
  1230. selection scheme.
  1231. When nil, fast selection is never used.
  1232. When the symbol `prefix', it will be used when `org-todo' is called with
  1233. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1234. in an agenda buffer.
  1235. When t, fast selection is used by default. In this case, the prefix
  1236. argument forces cycling instead.
  1237. In all cases, the special interface is only used if access keys have actually
  1238. been assigned by the user, i.e. if keywords in the configuration are followed
  1239. by a letter in parenthesis, like TODO(t)."
  1240. :group 'org-todo
  1241. :type '(choice
  1242. (const :tag "Never" nil)
  1243. (const :tag "By default" t)
  1244. (const :tag "Only with C-u C-c C-t" prefix)))
  1245. (defcustom org-provide-todo-statistics t
  1246. "Non-nil means, update todo statistics after insert and toggle.
  1247. When this is set, todo statistics is updated in the parent of the current
  1248. entry each time a todo state is changed."
  1249. :group 'org-todo
  1250. :type 'boolean)
  1251. (defcustom org-after-todo-state-change-hook nil
  1252. "Hook which is run after the state of a TODO item was changed.
  1253. The new state (a string with a TODO keyword, or nil) is available in the
  1254. Lisp variable `state'."
  1255. :group 'org-todo
  1256. :type 'hook)
  1257. (defcustom org-todo-state-tags-triggers nil
  1258. "Tag changes that should be triggered by TODO state changes.
  1259. This is a list. Each entry is
  1260. (state-change (tag . flag) .......)
  1261. State-change can be a string with a state, and empty string to indicate the
  1262. state that has no TODO keyword, or it can be one of the symbols `todo'
  1263. or `done', meaning any not-done or done state, respectively."
  1264. :group 'org-todo
  1265. :group 'org-tags
  1266. :type '(repeat
  1267. (cons (choice :tag "When changing to"
  1268. (const :tag "Not-done state" todo)
  1269. (const :tag "Done state" done)
  1270. (string :tag "State"))
  1271. (repeat
  1272. (cons :tag "Tag action"
  1273. (string :tag "Tag")
  1274. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1275. (defcustom org-log-done nil
  1276. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1277. When equal to the list (done), also prompt for a closing note.
  1278. This can also be configured on a per-file basis by adding one of
  1279. the following lines anywhere in the buffer:
  1280. #+STARTUP: logdone
  1281. #+STARTUP: lognotedone
  1282. #+STARTUP: nologdone"
  1283. :group 'org-todo
  1284. :group 'org-progress
  1285. :type '(choice
  1286. (const :tag "No logging" nil)
  1287. (const :tag "Record CLOSED timestamp" time)
  1288. (const :tag "Record CLOSED timestamp with closing note." note)))
  1289. ;; Normalize old uses of org-log-done.
  1290. (cond
  1291. ((eq org-log-done t) (setq org-log-done 'time))
  1292. ((and (listp org-log-done) (memq 'done org-log-done))
  1293. (setq org-log-done 'note)))
  1294. (defcustom org-log-note-clock-out nil
  1295. "Non-nil means, record a note when clocking out of an item.
  1296. This can also be configured on a per-file basis by adding one of
  1297. the following lines anywhere in the buffer:
  1298. #+STARTUP: lognoteclock-out
  1299. #+STARTUP: nolognoteclock-out"
  1300. :group 'org-todo
  1301. :group 'org-progress
  1302. :type 'boolean)
  1303. (defcustom org-log-done-with-time t
  1304. "Non-nil means, the CLOSED time stamp will contain date and time.
  1305. When nil, only the date will be recorded."
  1306. :group 'org-progress
  1307. :type 'boolean)
  1308. (defcustom org-log-note-headings
  1309. '((done . "CLOSING NOTE %t")
  1310. (state . "State %-12s %t")
  1311. (note . "Note taken on %t")
  1312. (clock-out . ""))
  1313. "Headings for notes added to entries.
  1314. The value is an alist, with the car being a symbol indicating the note
  1315. context, and the cdr is the heading to be used. The heading may also be the
  1316. empty string.
  1317. %t in the heading will be replaced by a time stamp.
  1318. %s will be replaced by the new TODO state, in double quotes.
  1319. %u will be replaced by the user name.
  1320. %U will be replaced by the full user name."
  1321. :group 'org-todo
  1322. :group 'org-progress
  1323. :type '(list :greedy t
  1324. (cons (const :tag "Heading when closing an item" done) string)
  1325. (cons (const :tag
  1326. "Heading when changing todo state (todo sequence only)"
  1327. state) string)
  1328. (cons (const :tag "Heading when just taking a note" note) string)
  1329. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1330. (unless (assq 'note org-log-note-headings)
  1331. (push '(note . "%t") org-log-note-headings))
  1332. (defcustom org-log-state-notes-insert-after-drawers nil
  1333. "Non-nil means, insert state change notes after any drawers in entry.
  1334. Only the drawers that *immediately* follow the headline and the
  1335. deadline/scheduled line are skipped.
  1336. When nil, insert notes right after the heading and perhaps the line
  1337. with deadline/scheduling if present."
  1338. :group 'org-todo
  1339. :group 'org-progress
  1340. :type 'boolean)
  1341. (defcustom org-log-states-order-reversed t
  1342. "Non-nil means, the latest state change note will be directly after heading.
  1343. When nil, the notes will be orderer according to time."
  1344. :group 'org-todo
  1345. :group 'org-progress
  1346. :type 'boolean)
  1347. (defcustom org-log-repeat 'time
  1348. "Non-nil means, record moving through the DONE state when triggering repeat.
  1349. An auto-repeating tasks is immediately switched back to TODO when marked
  1350. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1351. the TODO keyword definition, or recording a closing note by setting
  1352. `org-log-done', there will be no record of the task moving through DONE.
  1353. This variable forces taking a note anyway. Possible values are:
  1354. nil Don't force a record
  1355. time Record a time stamp
  1356. note Record a note
  1357. This option can also be set with on a per-file-basis with
  1358. #+STARTUP: logrepeat
  1359. #+STARTUP: lognoterepeat
  1360. #+STARTUP: nologrepeat
  1361. You can have local logging settings for a subtree by setting the LOGGING
  1362. property to one or more of these keywords."
  1363. :group 'org-todo
  1364. :group 'org-progress
  1365. :type '(choice
  1366. (const :tag "Don't force a record" nil)
  1367. (const :tag "Force recording the DONE state" time)
  1368. (const :tag "Force recording a note with the DONE state" note)))
  1369. (defgroup org-priorities nil
  1370. "Priorities in Org-mode."
  1371. :tag "Org Priorities"
  1372. :group 'org-todo)
  1373. (defcustom org-highest-priority ?A
  1374. "The highest priority of TODO items. A character like ?A, ?B etc.
  1375. Must have a smaller ASCII number than `org-lowest-priority'."
  1376. :group 'org-priorities
  1377. :type 'character)
  1378. (defcustom org-lowest-priority ?C
  1379. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1380. Must have a larger ASCII number than `org-highest-priority'."
  1381. :group 'org-priorities
  1382. :type 'character)
  1383. (defcustom org-default-priority ?B
  1384. "The default priority of TODO items.
  1385. This is the priority an item get if no explicit priority is given."
  1386. :group 'org-priorities
  1387. :type 'character)
  1388. (defcustom org-priority-start-cycle-with-default t
  1389. "Non-nil means, start with default priority when starting to cycle.
  1390. When this is nil, the first step in the cycle will be (depending on the
  1391. command used) one higher or lower that the default priority."
  1392. :group 'org-priorities
  1393. :type 'boolean)
  1394. (defgroup org-time nil
  1395. "Options concerning time stamps and deadlines in Org-mode."
  1396. :tag "Org Time"
  1397. :group 'org)
  1398. (defcustom org-insert-labeled-timestamps-at-point nil
  1399. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1400. When nil, these labeled time stamps are forces into the second line of an
  1401. entry, just after the headline. When scheduling from the global TODO list,
  1402. the time stamp will always be forced into the second line."
  1403. :group 'org-time
  1404. :type 'boolean)
  1405. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1406. "Formats for `format-time-string' which are used for time stamps.
  1407. It is not recommended to change this constant.")
  1408. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1409. "Number of minutes to round time stamps to.
  1410. These are two values, the first applies when first creating a time stamp.
  1411. The second applies when changing it with the commands `S-up' and `S-down'.
  1412. When changing the time stamp, this means that it will change in steps
  1413. of N minutes, as given by the second value.
  1414. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1415. numbers should be factors of 60, so for example 5, 10, 15.
  1416. When this is larger than 1, you can still force an exact time-stamp by using
  1417. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1418. and by using a prefix arg to `S-up/down' to specify the exact number
  1419. of minutes to shift."
  1420. :group 'org-time
  1421. :get '(lambda (var) ; Make sure all entries have 5 elements
  1422. (if (integerp (default-value var))
  1423. (list (default-value var) 5)
  1424. (default-value var)))
  1425. :type '(list
  1426. (integer :tag "when inserting times")
  1427. (integer :tag "when modifying times")))
  1428. ;; Normalize old customizations of this variable.
  1429. (when (integerp org-time-stamp-rounding-minutes)
  1430. (setq org-time-stamp-rounding-minutes
  1431. (list org-time-stamp-rounding-minutes
  1432. org-time-stamp-rounding-minutes)))
  1433. (defcustom org-display-custom-times nil
  1434. "Non-nil means, overlay custom formats over all time stamps.
  1435. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1436. To turn this on on a per-file basis, insert anywhere in the file:
  1437. #+STARTUP: customtime"
  1438. :group 'org-time
  1439. :set 'set-default
  1440. :type 'sexp)
  1441. (make-variable-buffer-local 'org-display-custom-times)
  1442. (defcustom org-time-stamp-custom-formats
  1443. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1444. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1445. These are overlayed over the default ISO format if the variable
  1446. `org-display-custom-times' is set. Time like %H:%M should be at the
  1447. end of the second format."
  1448. :group 'org-time
  1449. :type 'sexp)
  1450. (defun org-time-stamp-format (&optional long inactive)
  1451. "Get the right format for a time string."
  1452. (let ((f (if long (cdr org-time-stamp-formats)
  1453. (car org-time-stamp-formats))))
  1454. (if inactive
  1455. (concat "[" (substring f 1 -1) "]")
  1456. f)))
  1457. (defcustom org-time-clocksum-format "%d:%02d"
  1458. "The format string used when creating CLOCKSUM lines, or when
  1459. org-mode generates a time duration."
  1460. :group 'org-time
  1461. :type 'string)
  1462. (defcustom org-deadline-warning-days 14
  1463. "No. of days before expiration during which a deadline becomes active.
  1464. This variable governs the display in sparse trees and in the agenda.
  1465. When 0 or negative, it means use this number (the absolute value of it)
  1466. even if a deadline has a different individual lead time specified."
  1467. :group 'org-time
  1468. :group 'org-agenda-daily/weekly
  1469. :type 'number)
  1470. (defcustom org-read-date-prefer-future t
  1471. "Non-nil means, assume future for incomplete date input from user.
  1472. This affects the following situations:
  1473. 1. The user gives a day, but no month.
  1474. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1475. read this as the third of *next* month. However, if you enter \"17\",
  1476. it will be considered as *this* month.
  1477. 2. The user gives a month but not a year.
  1478. For example, if it is april and you enter \"feb 2\", this will be read
  1479. as feb 2, *next* year. \"May 5\", however, will be this year.
  1480. Currently this does not work for ISO week specifications.
  1481. When this option is nil, the current month and year will always be used
  1482. as defaults."
  1483. :group 'org-time
  1484. :type 'boolean)
  1485. (defcustom org-read-date-display-live t
  1486. "Non-nil means, display current interpretation of date prompt live.
  1487. This display will be in an overlay, in the minibuffer."
  1488. :group 'org-time
  1489. :type 'boolean)
  1490. (defcustom org-read-date-popup-calendar t
  1491. "Non-nil means, pop up a calendar when prompting for a date.
  1492. In the calendar, the date can be selected with mouse-1. However, the
  1493. minibuffer will also be active, and you can simply enter the date as well.
  1494. When nil, only the minibuffer will be available."
  1495. :group 'org-time
  1496. :type 'boolean)
  1497. (if (fboundp 'defvaralias)
  1498. (defvaralias 'org-popup-calendar-for-date-prompt
  1499. 'org-read-date-popup-calendar))
  1500. (defcustom org-extend-today-until 0
  1501. "The hour when your day really ends. Must be an integer.
  1502. This has influence for the following applications:
  1503. - When switching the agenda to \"today\". It it is still earlier than
  1504. the time given here, the day recognized as TODAY is actually yesterday.
  1505. - When a date is read from the user and it is still before the time given
  1506. here, the current date and time will be assumed to be yesterday, 23:59.
  1507. Also, timestamps inserted in remember templates follow this rule.
  1508. IMPORTANT: This is a feature whose implementation is and likely will
  1509. remain incomplete. Really, it is only here because past midnight seems to
  1510. ne the favorite working time of John Wiegley :-)"
  1511. :group 'org-time
  1512. :type 'number)
  1513. (defcustom org-edit-timestamp-down-means-later nil
  1514. "Non-nil means, S-down will increase the time in a time stamp.
  1515. When nil, S-up will increase."
  1516. :group 'org-time
  1517. :type 'boolean)
  1518. (defcustom org-calendar-follow-timestamp-change t
  1519. "Non-nil means, make the calendar window follow timestamp changes.
  1520. When a timestamp is modified and the calendar window is visible, it will be
  1521. moved to the new date."
  1522. :group 'org-time
  1523. :type 'boolean)
  1524. (defgroup org-tags nil
  1525. "Options concerning tags in Org-mode."
  1526. :tag "Org Tags"
  1527. :group 'org)
  1528. (defcustom org-tag-alist nil
  1529. "List of tags allowed in Org-mode files.
  1530. When this list is nil, Org-mode will base TAG input on what is already in the
  1531. buffer.
  1532. The value of this variable is an alist, the car of each entry must be a
  1533. keyword as a string, the cdr may be a character that is used to select
  1534. that tag through the fast-tag-selection interface.
  1535. See the manual for details."
  1536. :group 'org-tags
  1537. :type '(repeat
  1538. (choice
  1539. (cons (string :tag "Tag name")
  1540. (character :tag "Access char"))
  1541. (const :tag "Start radio group" (:startgroup))
  1542. (const :tag "End radio group" (:endgroup)))))
  1543. (defvar org-file-tags nil
  1544. "List of tags that can be inherited by all entries in the file.
  1545. The tags will be inherited if the variable `org-use-tag-inheritance'
  1546. says they should be.
  1547. This variable is populated from #+TAG lines.")
  1548. (defcustom org-use-fast-tag-selection 'auto
  1549. "Non-nil means, use fast tag selection scheme.
  1550. This is a special interface to select and deselect tags with single keys.
  1551. When nil, fast selection is never used.
  1552. When the symbol `auto', fast selection is used if and only if selection
  1553. characters for tags have been configured, either through the variable
  1554. `org-tag-alist' or through a #+TAGS line in the buffer.
  1555. When t, fast selection is always used and selection keys are assigned
  1556. automatically if necessary."
  1557. :group 'org-tags
  1558. :type '(choice
  1559. (const :tag "Always" t)
  1560. (const :tag "Never" nil)
  1561. (const :tag "When selection characters are configured" 'auto)))
  1562. (defcustom org-fast-tag-selection-single-key nil
  1563. "Non-nil means, fast tag selection exits after first change.
  1564. When nil, you have to press RET to exit it.
  1565. During fast tag selection, you can toggle this flag with `C-c'.
  1566. This variable can also have the value `expert'. In this case, the window
  1567. displaying the tags menu is not even shown, until you press C-c again."
  1568. :group 'org-tags
  1569. :type '(choice
  1570. (const :tag "No" nil)
  1571. (const :tag "Yes" t)
  1572. (const :tag "Expert" expert)))
  1573. (defvar org-fast-tag-selection-include-todo nil
  1574. "Non-nil means, fast tags selection interface will also offer TODO states.
  1575. This is an undocumented feature, you should not rely on it.")
  1576. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1577. "The column to which tags should be indented in a headline.
  1578. If this number is positive, it specifies the column. If it is negative,
  1579. it means that the tags should be flushright to that column. For example,
  1580. -80 works well for a normal 80 character screen."
  1581. :group 'org-tags
  1582. :type 'integer)
  1583. (defcustom org-auto-align-tags t
  1584. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1585. These operations change the length of a headline and therefore shift
  1586. the tags around. With this options turned on, after each such operation
  1587. the tags are again aligned to `org-tags-column'."
  1588. :group 'org-tags
  1589. :type 'boolean)
  1590. (defcustom org-use-tag-inheritance t
  1591. "Non-nil means, tags in levels apply also for sublevels.
  1592. When nil, only the tags directly given in a specific line apply there.
  1593. If this option is t, a match early-on in a tree can lead to a large
  1594. number of matches in the subtree. If you only want to see the first
  1595. match in a tree during a search, check out the variable
  1596. `org-tags-match-list-sublevels'.
  1597. This may also be a list of tags that should be inherited, or a regexp that
  1598. matches tags that should be inherited."
  1599. :group 'org-tags
  1600. :type '(choice
  1601. (const :tag "Not" nil)
  1602. (const :tag "Always" t)
  1603. (repeat :tag "Specific tags" (string :tag "Tag"))
  1604. (regexp :tag "Tags matched by regexp")))
  1605. (defun org-tag-inherit-p (tag)
  1606. "Check if TAG is one that should be inherited."
  1607. (cond
  1608. ((eq org-use-tag-inheritance t) t)
  1609. ((not org-use-tag-inheritance) nil)
  1610. ((stringp org-use-tag-inheritance)
  1611. (string-match org-use-tag-inheritance tag))
  1612. ((listp org-use-tag-inheritance)
  1613. (member tag org-use-tag-inheritance))
  1614. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1615. (defcustom org-tags-match-list-sublevels t
  1616. "Non-nil means list also sublevels of headlines matching tag search.
  1617. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1618. the sublevels of a headline matching a tag search often also match
  1619. the same search. Listing all of them can create very long lists.
  1620. Setting this variable to nil causes subtrees of a match to be skipped.
  1621. This option is off by default, because inheritance in on. If you turn
  1622. inheritance off, you very likely want to turn this option on.
  1623. As a special case, if the tag search is restricted to TODO items, the
  1624. value of this variable is ignored and sublevels are always checked, to
  1625. make sure all corresponding TODO items find their way into the list."
  1626. :group 'org-tags
  1627. :type 'boolean)
  1628. (defvar org-tags-history nil
  1629. "History of minibuffer reads for tags.")
  1630. (defvar org-last-tags-completion-table nil
  1631. "The last used completion table for tags.")
  1632. (defvar org-after-tags-change-hook nil
  1633. "Hook that is run after the tags in a line have changed.")
  1634. (defgroup org-properties nil
  1635. "Options concerning properties in Org-mode."
  1636. :tag "Org Properties"
  1637. :group 'org)
  1638. (defcustom org-property-format "%-10s %s"
  1639. "How property key/value pairs should be formatted by `indent-line'.
  1640. When `indent-line' hits a property definition, it will format the line
  1641. according to this format, mainly to make sure that the values are
  1642. lined-up with respect to each other."
  1643. :group 'org-properties
  1644. :type 'string)
  1645. (defcustom org-use-property-inheritance nil
  1646. "Non-nil means, properties apply also for sublevels.
  1647. This setting is chiefly used during property searches. Turning it on can
  1648. cause significant overhead when doing a search, which is why it is not
  1649. on by default.
  1650. When nil, only the properties directly given in the current entry count.
  1651. When t, every property is inherited. The value may also be a list of
  1652. properties that should have inheritance, or a regular expression matching
  1653. properties that should be inherited.
  1654. However, note that some special properties use inheritance under special
  1655. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1656. and the properties ending in \"_ALL\" when they are used as descriptor
  1657. for valid values of a property.
  1658. Note for programmers:
  1659. When querying an entry with `org-entry-get', you can control if inheritance
  1660. should be used. By default, `org-entry-get' looks only at the local
  1661. properties. You can request inheritance by setting the inherit argument
  1662. to t (to force inheritance) or to `selective' (to respect the setting
  1663. in this variable)."
  1664. :group 'org-properties
  1665. :type '(choice
  1666. (const :tag "Not" nil)
  1667. (const :tag "Always" t)
  1668. (repeat :tag "Specific properties" (string :tag "Property"))
  1669. (regexp :tag "Properties matched by regexp")))
  1670. (defun org-property-inherit-p (property)
  1671. "Check if PROPERTY is one that should be inherited."
  1672. (cond
  1673. ((eq org-use-property-inheritance t) t)
  1674. ((not org-use-property-inheritance) nil)
  1675. ((stringp org-use-property-inheritance)
  1676. (string-match org-use-property-inheritance property))
  1677. ((listp org-use-property-inheritance)
  1678. (member property org-use-property-inheritance))
  1679. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1680. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1681. "The default column format, if no other format has been defined.
  1682. This variable can be set on the per-file basis by inserting a line
  1683. #+COLUMNS: %25ITEM ....."
  1684. :group 'org-properties
  1685. :type 'string)
  1686. (defcustom org-columns-ellipses ".."
  1687. "The ellipses to be used when a field in column view is truncated.
  1688. When this is the empty string, as many characters as possible are shown,
  1689. but then there will be no visual indication that the field has been truncated.
  1690. When this is a string of length N, the last N characters of a truncated
  1691. field are replaced by this string. If the column is narrower than the
  1692. ellipses string, only part of the ellipses string will be shown."
  1693. :group 'org-properties
  1694. :type 'string)
  1695. (defcustom org-columns-modify-value-for-display-function nil
  1696. "Function that modifies values for display in column view.
  1697. For example, it can be used to cut out a certain part from a time stamp.
  1698. The function must take 2 argments:
  1699. column-title The tite of the column (*not* the property name)
  1700. value The value that should be modified.
  1701. The function should return the value that should be displayed,
  1702. or nil if the normal value should be used."
  1703. :group 'org-properties
  1704. :type 'function)
  1705. (defcustom org-effort-property "Effort"
  1706. "The property that is being used to keep track of effort estimates.
  1707. Effort estimates given in this property need to have the format H:MM."
  1708. :group 'org-properties
  1709. :group 'org-progress
  1710. :type '(string :tag "Property"))
  1711. (defconst org-global-properties-fixed
  1712. '(("VISIBILITY_ALL" . "folded children content all"))
  1713. "List of property/value pairs that can be inherited by any entry.
  1714. These are fixed values, for the preset properties.")
  1715. (defcustom org-global-properties nil
  1716. "List of property/value pairs that can be inherited by any entry.
  1717. You can set buffer-local values for this by adding lines like
  1718. #+PROPERTY: NAME VALUE"
  1719. :group 'org-properties
  1720. :type '(repeat
  1721. (cons (string :tag "Property")
  1722. (string :tag "Value"))))
  1723. (defvar org-file-properties nil
  1724. "List of property/value pairs that can be inherited by any entry.
  1725. Valid for the current buffer.
  1726. This variable is populated from #+PROPERTY lines.")
  1727. (make-variable-buffer-local 'org-file-properties)
  1728. (defgroup org-agenda nil
  1729. "Options concerning agenda views in Org-mode."
  1730. :tag "Org Agenda"
  1731. :group 'org)
  1732. (defvar org-category nil
  1733. "Variable used by org files to set a category for agenda display.
  1734. Such files should use a file variable to set it, for example
  1735. # -*- mode: org; org-category: \"ELisp\"
  1736. or contain a special line
  1737. #+CATEGORY: ELisp
  1738. If the file does not specify a category, then file's base name
  1739. is used instead.")
  1740. (make-variable-buffer-local 'org-category)
  1741. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1742. (defcustom org-agenda-files nil
  1743. "The files to be used for agenda display.
  1744. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1745. \\[org-remove-file]. You can also use customize to edit the list.
  1746. If an entry is a directory, all files in that directory that are matched by
  1747. `org-agenda-file-regexp' will be part of the file list.
  1748. If the value of the variable is not a list but a single file name, then
  1749. the list of agenda files is actually stored and maintained in that file, one
  1750. agenda file per line."
  1751. :group 'org-agenda
  1752. :type '(choice
  1753. (repeat :tag "List of files and directories" file)
  1754. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1755. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1756. "Regular expression to match files for `org-agenda-files'.
  1757. If any element in the list in that variable contains a directory instead
  1758. of a normal file, all files in that directory that are matched by this
  1759. regular expression will be included."
  1760. :group 'org-agenda
  1761. :type 'regexp)
  1762. (defcustom org-agenda-text-search-extra-files nil
  1763. "List of extra files to be searched by text search commands.
  1764. These files will be search in addition to the agenda files by the
  1765. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1766. Note that these files will only be searched for text search commands,
  1767. not for the other agenda views like todo lists, tag searches or the weekly
  1768. agenda. This variable is intended to list notes and possibly archive files
  1769. that should also be searched by these two commands.
  1770. In fact, if the first element in the list is the symbol `agenda-archives',
  1771. than all archive files of all agenda files will be added to the search
  1772. scope."
  1773. :group 'org-agenda
  1774. :type '(set :greedy t
  1775. (const :tag "Agenda Archives" agenda-archives)
  1776. (repeat :inline t (file))))
  1777. (if (fboundp 'defvaralias)
  1778. (defvaralias 'org-agenda-multi-occur-extra-files
  1779. 'org-agenda-text-search-extra-files))
  1780. (defcustom org-agenda-skip-unavailable-files nil
  1781. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1782. A nil value means to remove them, after a query, from the list."
  1783. :group 'org-agenda
  1784. :type 'boolean)
  1785. (defcustom org-calendar-to-agenda-key [?c]
  1786. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1787. The command `org-calendar-goto-agenda' will be bound to this key. The
  1788. default is the character `c' because then `c' can be used to switch back and
  1789. forth between agenda and calendar."
  1790. :group 'org-agenda
  1791. :type 'sexp)
  1792. (defcustom org-calendar-agenda-action-key [?k]
  1793. "The key to be installed in `calendar-mode-map' for agenda-action.
  1794. The command `org-agenda-action' will be bound to this key. The
  1795. default is the character `k' because we use the same key in the agenda."
  1796. :group 'org-agenda
  1797. :type 'sexp)
  1798. (eval-after-load "calendar"
  1799. '(progn
  1800. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1801. 'org-calendar-goto-agenda)
  1802. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1803. 'org-agenda-action)))
  1804. (defgroup org-latex nil
  1805. "Options for embedding LaTeX code into Org-mode."
  1806. :tag "Org LaTeX"
  1807. :group 'org)
  1808. (defcustom org-format-latex-options
  1809. '(:foreground default :background default :scale 1.0
  1810. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1811. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1812. "Options for creating images from LaTeX fragments.
  1813. This is a property list with the following properties:
  1814. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1815. `default' means use the foreground of the default face.
  1816. :background the background color, or \"Transparent\".
  1817. `default' means use the background of the default face.
  1818. :scale a scaling factor for the size of the images.
  1819. :html-foreground, :html-background, :html-scale
  1820. the same numbers for HTML export.
  1821. :matchers a list indicating which matchers should be used to
  1822. find LaTeX fragments. Valid members of this list are:
  1823. \"begin\" find environments
  1824. \"$\" find math expressions surrounded by $...$
  1825. \"$$\" find math expressions surrounded by $$....$$
  1826. \"\\(\" find math expressions surrounded by \\(...\\)
  1827. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1828. :group 'org-latex
  1829. :type 'plist)
  1830. (defcustom org-format-latex-header "\\documentclass{article}
  1831. \\usepackage{fullpage} % do not remove
  1832. \\usepackage{amssymb}
  1833. \\usepackage[usenames]{color}
  1834. \\usepackage{amsmath}
  1835. \\usepackage{latexsym}
  1836. \\usepackage[mathscr]{eucal}
  1837. \\pagestyle{empty} % do not remove"
  1838. "The document header used for processing LaTeX fragments."
  1839. :group 'org-latex
  1840. :type 'string)
  1841. (defgroup org-font-lock nil
  1842. "Font-lock settings for highlighting in Org-mode."
  1843. :tag "Org Font Lock"
  1844. :group 'org)
  1845. (defcustom org-level-color-stars-only nil
  1846. "Non-nil means fontify only the stars in each headline.
  1847. When nil, the entire headline is fontified.
  1848. Changing it requires restart of `font-lock-mode' to become effective
  1849. also in regions already fontified."
  1850. :group 'org-font-lock
  1851. :type 'boolean)
  1852. (defcustom org-hide-leading-stars nil
  1853. "Non-nil means, hide the first N-1 stars in a headline.
  1854. This works by using the face `org-hide' for these stars. This
  1855. face is white for a light background, and black for a dark
  1856. background. You may have to customize the face `org-hide' to
  1857. make this work.
  1858. Changing it requires restart of `font-lock-mode' to become effective
  1859. also in regions already fontified.
  1860. You may also set this on a per-file basis by adding one of the following
  1861. lines to the buffer:
  1862. #+STARTUP: hidestars
  1863. #+STARTUP: showstars"
  1864. :group 'org-font-lock
  1865. :type 'boolean)
  1866. (defcustom org-fontify-done-headline nil
  1867. "Non-nil means, change the face of a headline if it is marked DONE.
  1868. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1869. When this is non-nil, the headline after the keyword is set to the
  1870. `org-headline-done' as an additional indication."
  1871. :group 'org-font-lock
  1872. :type 'boolean)
  1873. (defcustom org-fontify-emphasized-text t
  1874. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1875. Changing this variable requires a restart of Emacs to take effect."
  1876. :group 'org-font-lock
  1877. :type 'boolean)
  1878. (defcustom org-highlight-latex-fragments-and-specials nil
  1879. "Non-nil means, fontify what is treated specially by the exporters."
  1880. :group 'org-font-lock
  1881. :type 'boolean)
  1882. (defcustom org-hide-emphasis-markers nil
  1883. "Non-nil mean font-lock should hide the emphasis marker characters."
  1884. :group 'org-font-lock
  1885. :type 'boolean)
  1886. (defvar org-emph-re nil
  1887. "Regular expression for matching emphasis.")
  1888. (defvar org-verbatim-re nil
  1889. "Regular expression for matching verbatim text.")
  1890. (defvar org-emphasis-regexp-components) ; defined just below
  1891. (defvar org-emphasis-alist) ; defined just below
  1892. (defun org-set-emph-re (var val)
  1893. "Set variable and compute the emphasis regular expression."
  1894. (set var val)
  1895. (when (and (boundp 'org-emphasis-alist)
  1896. (boundp 'org-emphasis-regexp-components)
  1897. org-emphasis-alist org-emphasis-regexp-components)
  1898. (let* ((e org-emphasis-regexp-components)
  1899. (pre (car e))
  1900. (post (nth 1 e))
  1901. (border (nth 2 e))
  1902. (body (nth 3 e))
  1903. (nl (nth 4 e))
  1904. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1905. (body1 (concat body "*?"))
  1906. (markers (mapconcat 'car org-emphasis-alist ""))
  1907. (vmarkers (mapconcat
  1908. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1909. org-emphasis-alist "")))
  1910. ;; make sure special characters appear at the right position in the class
  1911. (if (string-match "\\^" markers)
  1912. (setq markers (concat (replace-match "" t t markers) "^")))
  1913. (if (string-match "-" markers)
  1914. (setq markers (concat (replace-match "" t t markers) "-")))
  1915. (if (string-match "\\^" vmarkers)
  1916. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1917. (if (string-match "-" vmarkers)
  1918. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1919. (if (> nl 0)
  1920. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1921. (int-to-string nl) "\\}")))
  1922. ;; Make the regexp
  1923. (setq org-emph-re
  1924. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1925. "\\("
  1926. "\\([" markers "]\\)"
  1927. "\\("
  1928. "[^" border "]\\|"
  1929. "[^" border (if (and nil stacked) markers) "]"
  1930. body1
  1931. "[^" border (if (and nil stacked) markers) "]"
  1932. "\\)"
  1933. "\\3\\)"
  1934. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1935. (setq org-verbatim-re
  1936. (concat "\\([" pre "]\\|^\\)"
  1937. "\\("
  1938. "\\([" vmarkers "]\\)"
  1939. "\\("
  1940. "[^" border "]\\|"
  1941. "[^" border "]"
  1942. body1
  1943. "[^" border "]"
  1944. "\\)"
  1945. "\\3\\)"
  1946. "\\([" post "]\\|$\\)")))))
  1947. (defcustom org-emphasis-regexp-components
  1948. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1949. "Components used to build the regular expression for emphasis.
  1950. This is a list with 6 entries. Terminology: In an emphasis string
  1951. like \" *strong word* \", we call the initial space PREMATCH, the final
  1952. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1953. and \"trong wor\" is the body. The different components in this variable
  1954. specify what is allowed/forbidden in each part:
  1955. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1956. post Chars allowed as postmatch. End of line will be allowed too.
  1957. border The chars *forbidden* as border characters.
  1958. body-regexp A regexp like \".\" to match a body character. Don't use
  1959. non-shy groups here, and don't allow newline here.
  1960. newline The maximum number of newlines allowed in an emphasis exp.
  1961. Use customize to modify this, or restart Emacs after changing it."
  1962. :group 'org-font-lock
  1963. :set 'org-set-emph-re
  1964. :type '(list
  1965. (sexp :tag "Allowed chars in pre ")
  1966. (sexp :tag "Allowed chars in post ")
  1967. (sexp :tag "Forbidden chars in border ")
  1968. (sexp :tag "Regexp for body ")
  1969. (integer :tag "number of newlines allowed")
  1970. (option (boolean :tag "Please ignore this button"))))
  1971. (defcustom org-emphasis-alist
  1972. `(("*" bold "<b>" "</b>")
  1973. ("/" italic "<i>" "</i>")
  1974. ("_" underline "<u>" "</u>")
  1975. ("=" org-code "<code>" "</code>" verbatim)
  1976. ("~" org-verbatim "" "" verbatim)
  1977. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1978. "<del>" "</del>")
  1979. )
  1980. "Special syntax for emphasized text.
  1981. Text starting and ending with a special character will be emphasized, for
  1982. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1983. characters, the face to be used by font-lock for highlighting in Org-mode
  1984. Emacs buffers, and the HTML tags to be used for this.
  1985. Use customize to modify this, or restart Emacs after changing it."
  1986. :group 'org-font-lock
  1987. :set 'org-set-emph-re
  1988. :type '(repeat
  1989. (list
  1990. (string :tag "Marker character")
  1991. (choice
  1992. (face :tag "Font-lock-face")
  1993. (plist :tag "Face property list"))
  1994. (string :tag "HTML start tag")
  1995. (string :tag "HTML end tag")
  1996. (option (const verbatim)))))
  1997. ;;; Miscellaneous options
  1998. (defgroup org-completion nil
  1999. "Completion in Org-mode."
  2000. :tag "Org Completion"
  2001. :group 'org)
  2002. (defcustom org-completion-fallback-command 'hippie-expand
  2003. "The expansion command called by \\[org-complete] in normal context.
  2004. Normal means, no org-mode-specific context."
  2005. :group 'org-completion
  2006. :type 'function)
  2007. ;;; Functions and variables from ther packages
  2008. ;; Declared here to avoid compiler warnings
  2009. ;; XEmacs only
  2010. (defvar outline-mode-menu-heading)
  2011. (defvar outline-mode-menu-show)
  2012. (defvar outline-mode-menu-hide)
  2013. (defvar zmacs-regions) ; XEmacs regions
  2014. ;; Emacs only
  2015. (defvar mark-active)
  2016. ;; Various packages
  2017. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2018. (declare-function calendar-forward-day "cal-move" (arg))
  2019. (declare-function calendar-goto-date "cal-move" (date))
  2020. (declare-function calendar-goto-today "cal-move" ())
  2021. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2022. (defvar calc-embedded-close-formula)
  2023. (defvar calc-embedded-open-formula)
  2024. (declare-function cdlatex-tab "ext:cdlatex" ())
  2025. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2026. (defvar font-lock-unfontify-region-function)
  2027. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2028. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2029. (defvar iswitchb-temp-buflist)
  2030. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2031. (declare-function org-agenda-skip "org-agenda" ())
  2032. (declare-function org-format-agenda-item "org-agenda"
  2033. (extra txt &optional category tags dotime noprefix remove-re))
  2034. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2035. (declare-function org-agenda-change-all-lines "org-agenda"
  2036. (newhead hdmarker &optional fixface))
  2037. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2038. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2039. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2040. (beg end))
  2041. (declare-function parse-time-string "parse-time" (string))
  2042. (declare-function remember "remember" (&optional initial))
  2043. (declare-function remember-buffer-desc "remember" ())
  2044. (declare-function remember-finalize "remember" ())
  2045. (defvar remember-save-after-remembering)
  2046. (defvar remember-data-file)
  2047. (defvar remember-register)
  2048. (defvar remember-buffer)
  2049. (defvar remember-handler-functions)
  2050. (defvar remember-annotation-functions)
  2051. (defvar texmathp-why)
  2052. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2053. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2054. (defvar w3m-current-url)
  2055. (defvar w3m-current-title)
  2056. (defvar org-latex-regexps)
  2057. ;;; Autoload and prepare some org modules
  2058. ;; Some table stuff that needs to be defined here, because it is used
  2059. ;; by the functions setting up org-mode or checking for table context.
  2060. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2061. "Detects an org-type or table-type table.")
  2062. (defconst org-table-line-regexp "^[ \t]*|"
  2063. "Detects an org-type table line.")
  2064. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2065. "Detects an org-type table line.")
  2066. (defconst org-table-hline-regexp "^[ \t]*|-"
  2067. "Detects an org-type table hline.")
  2068. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2069. "Detects a table-type table hline.")
  2070. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2071. "Searching from within a table (any type) this finds the first line
  2072. outside the table.")
  2073. ;; Autoload the functions in org-table.el that are needed by functions here.
  2074. (eval-and-compile
  2075. (org-autoload "org-table"
  2076. '(org-table-align org-table-begin org-table-blank-field
  2077. org-table-convert org-table-convert-region org-table-copy-down
  2078. org-table-copy-region org-table-create
  2079. org-table-create-or-convert-from-region
  2080. org-table-create-with-table.el org-table-current-dline
  2081. org-table-cut-region org-table-delete-column org-table-edit-field
  2082. org-table-edit-formulas org-table-end org-table-eval-formula
  2083. org-table-export org-table-field-info
  2084. org-table-get-stored-formulas org-table-goto-column
  2085. org-table-hline-and-move org-table-import org-table-insert-column
  2086. org-table-insert-hline org-table-insert-row org-table-iterate
  2087. org-table-justify-field-maybe org-table-kill-row
  2088. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2089. org-table-move-column org-table-move-column-left
  2090. org-table-move-column-right org-table-move-row
  2091. org-table-move-row-down org-table-move-row-up
  2092. org-table-next-field org-table-next-row org-table-paste-rectangle
  2093. org-table-previous-field org-table-recalculate
  2094. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2095. org-table-toggle-coordinate-overlays
  2096. org-table-toggle-formula-debugger org-table-wrap-region
  2097. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2098. (defun org-at-table-p (&optional table-type)
  2099. "Return t if the cursor is inside an org-type table.
  2100. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2101. (if org-enable-table-editor
  2102. (save-excursion
  2103. (beginning-of-line 1)
  2104. (looking-at (if table-type org-table-any-line-regexp
  2105. org-table-line-regexp)))
  2106. nil))
  2107. (defsubst org-table-p () (org-at-table-p))
  2108. (defun org-at-table.el-p ()
  2109. "Return t if and only if we are at a table.el table."
  2110. (and (org-at-table-p 'any)
  2111. (save-excursion
  2112. (goto-char (org-table-begin 'any))
  2113. (looking-at org-table1-hline-regexp))))
  2114. (defun org-table-recognize-table.el ()
  2115. "If there is a table.el table nearby, recognize it and move into it."
  2116. (if org-table-tab-recognizes-table.el
  2117. (if (org-at-table.el-p)
  2118. (progn
  2119. (beginning-of-line 1)
  2120. (if (looking-at org-table-dataline-regexp)
  2121. nil
  2122. (if (looking-at org-table1-hline-regexp)
  2123. (progn
  2124. (beginning-of-line 2)
  2125. (if (looking-at org-table-any-border-regexp)
  2126. (beginning-of-line -1)))))
  2127. (if (re-search-forward "|" (org-table-end t) t)
  2128. (progn
  2129. (require 'table)
  2130. (if (table--at-cell-p (point))
  2131. t
  2132. (message "recognizing table.el table...")
  2133. (table-recognize-table)
  2134. (message "recognizing table.el table...done")))
  2135. (error "This should not happen..."))
  2136. t)
  2137. nil)
  2138. nil))
  2139. (defun org-at-table-hline-p ()
  2140. "Return t if the cursor is inside a hline in a table."
  2141. (if org-enable-table-editor
  2142. (save-excursion
  2143. (beginning-of-line 1)
  2144. (looking-at org-table-hline-regexp))
  2145. nil))
  2146. (defvar org-table-clean-did-remove-column nil)
  2147. (defun org-table-map-tables (function)
  2148. "Apply FUNCTION to the start of all tables in the buffer."
  2149. (save-excursion
  2150. (save-restriction
  2151. (widen)
  2152. (goto-char (point-min))
  2153. (while (re-search-forward org-table-any-line-regexp nil t)
  2154. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2155. (beginning-of-line 1)
  2156. (if (looking-at org-table-line-regexp)
  2157. (save-excursion (funcall function)))
  2158. (re-search-forward org-table-any-border-regexp nil 1))))
  2159. (message "Mapping tables: done"))
  2160. ;; Declare and autoload functions from org-exp.el
  2161. (declare-function org-default-export-plist "org-exp")
  2162. (declare-function org-infile-export-plist "org-exp")
  2163. (declare-function org-get-current-options "org-exp")
  2164. (eval-and-compile
  2165. (org-autoload "org-exp"
  2166. '(org-export org-export-as-ascii org-export-visible
  2167. org-insert-export-options-template org-export-as-html-and-open
  2168. org-export-as-html-batch org-export-as-html-to-buffer
  2169. org-replace-region-by-html org-export-region-as-html
  2170. org-export-as-html org-export-icalendar-this-file
  2171. org-export-icalendar-all-agenda-files
  2172. org-table-clean-before-export
  2173. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2174. ;; Declare and autoload functions from org-agenda.el
  2175. (eval-and-compile
  2176. (org-autoload "org-agenda"
  2177. '(org-agenda org-agenda-list org-search-view
  2178. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2179. org-diary org-agenda-to-appt)))
  2180. ;; Autoload org-remember
  2181. (eval-and-compile
  2182. (org-autoload "org-remember"
  2183. '(org-remember-insinuate org-remember-annotation
  2184. org-remember-apply-template org-remember org-remember-handler)))
  2185. ;; Autoload org-clock.el
  2186. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2187. (beg end))
  2188. (declare-function org-update-mode-line "org-clock" ())
  2189. (defvar org-clock-start-time)
  2190. (defvar org-clock-marker (make-marker)
  2191. "Marker recording the last clock-in.")
  2192. (eval-and-compile
  2193. (org-autoload
  2194. "org-clock"
  2195. '(org-clock-in org-clock-out org-clock-cancel
  2196. org-clock-goto org-clock-sum org-clock-display
  2197. org-remove-clock-overlays org-clock-report
  2198. org-clocktable-shift org-dblock-write:clocktable
  2199. org-get-clocktable)))
  2200. (defun org-clock-update-time-maybe ()
  2201. "If this is a CLOCK line, update it and return t.
  2202. Otherwise, return nil."
  2203. (interactive)
  2204. (save-excursion
  2205. (beginning-of-line 1)
  2206. (skip-chars-forward " \t")
  2207. (when (looking-at org-clock-string)
  2208. (let ((re (concat "[ \t]*" org-clock-string
  2209. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2210. "\\([ \t]*=>.*\\)?\\)?"))
  2211. ts te h m s neg)
  2212. (cond
  2213. ((not (looking-at re))
  2214. nil)
  2215. ((not (match-end 2))
  2216. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2217. (> org-clock-marker (point))
  2218. (<= org-clock-marker (point-at-eol)))
  2219. ;; The clock is running here
  2220. (setq org-clock-start-time
  2221. (apply 'encode-time
  2222. (org-parse-time-string (match-string 1))))
  2223. (org-update-mode-line)))
  2224. (t
  2225. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2226. (end-of-line 1)
  2227. (setq ts (match-string 1)
  2228. te (match-string 3))
  2229. (setq s (- (time-to-seconds
  2230. (apply 'encode-time (org-parse-time-string te)))
  2231. (time-to-seconds
  2232. (apply 'encode-time (org-parse-time-string ts))))
  2233. neg (< s 0)
  2234. s (abs s)
  2235. h (floor (/ s 3600))
  2236. s (- s (* 3600 h))
  2237. m (floor (/ s 60))
  2238. s (- s (* 60 s)))
  2239. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2240. t))))))
  2241. (defun org-check-running-clock ()
  2242. "Check if the current buffer contains the running clock.
  2243. If yes, offer to stop it and to save the buffer with the changes."
  2244. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2245. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2246. (buffer-name))))
  2247. (org-clock-out)
  2248. (when (y-or-n-p "Save changed buffer?")
  2249. (save-buffer))))
  2250. (defun org-clocktable-try-shift (dir n)
  2251. "Check if this line starts a clock table, if yes, shift the time block."
  2252. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2253. (org-clocktable-shift dir n)))
  2254. ;; Autoload archiving code
  2255. ;; The stuff that is needed for cycling and tags has to be defined here.
  2256. (defgroup org-archive nil
  2257. "Options concerning archiving in Org-mode."
  2258. :tag "Org Archive"
  2259. :group 'org-structure)
  2260. (defcustom org-archive-location "%s_archive::"
  2261. "The location where subtrees should be archived.
  2262. Otherwise, the value of this variable is a string, consisting of two
  2263. parts, separated by a double-colon.
  2264. The first part is a file name - when omitted, archiving happens in the same
  2265. file. %s will be replaced by the current file name (without directory part).
  2266. Archiving to a different file is useful to keep archived entries from
  2267. contributing to the Org-mode Agenda.
  2268. The part after the double colon is a headline. The archived entries will be
  2269. filed under that headline. When omitted, the subtrees are simply filed away
  2270. at the end of the file, as top-level entries.
  2271. Here are a few examples:
  2272. \"%s_archive::\"
  2273. If the current file is Projects.org, archive in file
  2274. Projects.org_archive, as top-level trees. This is the default.
  2275. \"::* Archived Tasks\"
  2276. Archive in the current file, under the top-level headline
  2277. \"* Archived Tasks\".
  2278. \"~/org/archive.org::\"
  2279. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2280. \"basement::** Finished Tasks\"
  2281. Archive in file ./basement (relative path), as level 3 trees
  2282. below the level 2 heading \"** Finished Tasks\".
  2283. You may set this option on a per-file basis by adding to the buffer a
  2284. line like
  2285. #+ARCHIVE: basement::** Finished Tasks
  2286. You may also define it locally for a subtree by setting an ARCHIVE property
  2287. in the entry. If such a property is found in an entry, or anywhere up
  2288. the hierarchy, it will be used."
  2289. :group 'org-archive
  2290. :type 'string)
  2291. (defcustom org-archive-tag "ARCHIVE"
  2292. "The tag that marks a subtree as archived.
  2293. An archived subtree does not open during visibility cycling, and does
  2294. not contribute to the agenda listings.
  2295. After changing this, font-lock must be restarted in the relevant buffers to
  2296. get the proper fontification."
  2297. :group 'org-archive
  2298. :group 'org-keywords
  2299. :type 'string)
  2300. (defcustom org-agenda-skip-archived-trees t
  2301. "Non-nil means, the agenda will skip any items located in archived trees.
  2302. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2303. variable is no longer recommended, you should leave it at the value t.
  2304. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2305. :group 'org-archive
  2306. :group 'org-agenda-skip
  2307. :type 'boolean)
  2308. (defcustom org-cycle-open-archived-trees nil
  2309. "Non-nil means, `org-cycle' will open archived trees.
  2310. An archived tree is a tree marked with the tag ARCHIVE.
  2311. When nil, archived trees will stay folded. You can still open them with
  2312. normal outline commands like `show-all', but not with the cycling commands."
  2313. :group 'org-archive
  2314. :group 'org-cycle
  2315. :type 'boolean)
  2316. (defcustom org-sparse-tree-open-archived-trees nil
  2317. "Non-nil means sparse tree construction shows matches in archived trees.
  2318. When nil, matches in these trees are highlighted, but the trees are kept in
  2319. collapsed state."
  2320. :group 'org-archive
  2321. :group 'org-sparse-trees
  2322. :type 'boolean)
  2323. (defun org-cycle-hide-archived-subtrees (state)
  2324. "Re-hide all archived subtrees after a visibility state change."
  2325. (when (and (not org-cycle-open-archived-trees)
  2326. (not (memq state '(overview folded))))
  2327. (save-excursion
  2328. (let* ((globalp (memq state '(contents all)))
  2329. (beg (if globalp (point-min) (point)))
  2330. (end (if globalp (point-max) (org-end-of-subtree t))))
  2331. (org-hide-archived-subtrees beg end)
  2332. (goto-char beg)
  2333. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2334. (message "%s" (substitute-command-keys
  2335. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2336. (defun org-force-cycle-archived ()
  2337. "Cycle subtree even if it is archived."
  2338. (interactive)
  2339. (setq this-command 'org-cycle)
  2340. (let ((org-cycle-open-archived-trees t))
  2341. (call-interactively 'org-cycle)))
  2342. (defun org-hide-archived-subtrees (beg end)
  2343. "Re-hide all archived subtrees after a visibility state change."
  2344. (save-excursion
  2345. (let* ((re (concat ":" org-archive-tag ":")))
  2346. (goto-char beg)
  2347. (while (re-search-forward re end t)
  2348. (and (org-on-heading-p) (hide-subtree))
  2349. (org-end-of-subtree t)))))
  2350. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2351. (eval-and-compile
  2352. (org-autoload "org-archive"
  2353. '(org-add-archive-files org-archive-subtree
  2354. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2355. ;; Autoload Column View Code
  2356. (declare-function org-columns-number-to-string "org-colview")
  2357. (declare-function org-columns-get-format-and-top-level "org-colview")
  2358. (declare-function org-columns-compute "org-colview")
  2359. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2360. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2361. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2362. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2363. ;; Autoload ID code
  2364. (org-autoload "org-id"
  2365. '(org-id-get-create org-id-new org-id-copy org-id-get
  2366. org-id-get-with-outline-path-completion
  2367. org-id-get-with-outline-drilling
  2368. org-id-goto org-id-find))
  2369. ;;; Variables for pre-computed regular expressions, all buffer local
  2370. (defvar org-drawer-regexp nil
  2371. "Matches first line of a hidden block.")
  2372. (make-variable-buffer-local 'org-drawer-regexp)
  2373. (defvar org-todo-regexp nil
  2374. "Matches any of the TODO state keywords.")
  2375. (make-variable-buffer-local 'org-todo-regexp)
  2376. (defvar org-not-done-regexp nil
  2377. "Matches any of the TODO state keywords except the last one.")
  2378. (make-variable-buffer-local 'org-not-done-regexp)
  2379. (defvar org-todo-line-regexp nil
  2380. "Matches a headline and puts TODO state into group 2 if present.")
  2381. (make-variable-buffer-local 'org-todo-line-regexp)
  2382. (defvar org-complex-heading-regexp nil
  2383. "Matches a headline and puts everything into groups:
  2384. group 1: the stars
  2385. group 2: The todo keyword, maybe
  2386. group 3: Priority cookie
  2387. group 4: True headline
  2388. group 5: Tags")
  2389. (make-variable-buffer-local 'org-complex-heading-regexp)
  2390. (defvar org-todo-line-tags-regexp nil
  2391. "Matches a headline and puts TODO state into group 2 if present.
  2392. Also put tags into group 4 if tags are present.")
  2393. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2394. (defvar org-nl-done-regexp nil
  2395. "Matches newline followed by a headline with the DONE keyword.")
  2396. (make-variable-buffer-local 'org-nl-done-regexp)
  2397. (defvar org-looking-at-done-regexp nil
  2398. "Matches the DONE keyword a point.")
  2399. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2400. (defvar org-ds-keyword-length 12
  2401. "Maximum length of the Deadline and SCHEDULED keywords.")
  2402. (make-variable-buffer-local 'org-ds-keyword-length)
  2403. (defvar org-deadline-regexp nil
  2404. "Matches the DEADLINE keyword.")
  2405. (make-variable-buffer-local 'org-deadline-regexp)
  2406. (defvar org-deadline-time-regexp nil
  2407. "Matches the DEADLINE keyword together with a time stamp.")
  2408. (make-variable-buffer-local 'org-deadline-time-regexp)
  2409. (defvar org-deadline-line-regexp nil
  2410. "Matches the DEADLINE keyword and the rest of the line.")
  2411. (make-variable-buffer-local 'org-deadline-line-regexp)
  2412. (defvar org-scheduled-regexp nil
  2413. "Matches the SCHEDULED keyword.")
  2414. (make-variable-buffer-local 'org-scheduled-regexp)
  2415. (defvar org-scheduled-time-regexp nil
  2416. "Matches the SCHEDULED keyword together with a time stamp.")
  2417. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2418. (defvar org-closed-time-regexp nil
  2419. "Matches the CLOSED keyword together with a time stamp.")
  2420. (make-variable-buffer-local 'org-closed-time-regexp)
  2421. (defvar org-keyword-time-regexp nil
  2422. "Matches any of the 4 keywords, together with the time stamp.")
  2423. (make-variable-buffer-local 'org-keyword-time-regexp)
  2424. (defvar org-keyword-time-not-clock-regexp nil
  2425. "Matches any of the 3 keywords, together with the time stamp.")
  2426. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2427. (defvar org-maybe-keyword-time-regexp nil
  2428. "Matches a timestamp, possibly preceeded by a keyword.")
  2429. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2430. (defvar org-planning-or-clock-line-re nil
  2431. "Matches a line with planning or clock info.")
  2432. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2433. (defconst org-plain-time-of-day-regexp
  2434. (concat
  2435. "\\(\\<[012]?[0-9]"
  2436. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2437. "\\(--?"
  2438. "\\(\\<[012]?[0-9]"
  2439. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2440. "\\)?")
  2441. "Regular expression to match a plain time or time range.
  2442. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2443. groups carry important information:
  2444. 0 the full match
  2445. 1 the first time, range or not
  2446. 8 the second time, if it is a range.")
  2447. (defconst org-plain-time-extension-regexp
  2448. (concat
  2449. "\\(\\<[012]?[0-9]"
  2450. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2451. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2452. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2453. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2454. groups carry important information:
  2455. 0 the full match
  2456. 7 hours of duration
  2457. 9 minutes of duration")
  2458. (defconst org-stamp-time-of-day-regexp
  2459. (concat
  2460. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2461. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2462. "\\(--?"
  2463. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2464. "Regular expression to match a timestamp time or time range.
  2465. After a match, the following groups carry important information:
  2466. 0 the full match
  2467. 1 date plus weekday, for backreferencing to make sure both times on same day
  2468. 2 the first time, range or not
  2469. 4 the second time, if it is a range.")
  2470. (defconst org-startup-options
  2471. '(("fold" org-startup-folded t)
  2472. ("overview" org-startup-folded t)
  2473. ("nofold" org-startup-folded nil)
  2474. ("showall" org-startup-folded nil)
  2475. ("content" org-startup-folded content)
  2476. ("hidestars" org-hide-leading-stars t)
  2477. ("showstars" org-hide-leading-stars nil)
  2478. ("odd" org-odd-levels-only t)
  2479. ("oddeven" org-odd-levels-only nil)
  2480. ("align" org-startup-align-all-tables t)
  2481. ("noalign" org-startup-align-all-tables nil)
  2482. ("customtime" org-display-custom-times t)
  2483. ("logdone" org-log-done time)
  2484. ("lognotedone" org-log-done note)
  2485. ("nologdone" org-log-done nil)
  2486. ("lognoteclock-out" org-log-note-clock-out t)
  2487. ("nolognoteclock-out" org-log-note-clock-out nil)
  2488. ("logrepeat" org-log-repeat state)
  2489. ("lognoterepeat" org-log-repeat note)
  2490. ("nologrepeat" org-log-repeat nil)
  2491. ("constcgs" constants-unit-system cgs)
  2492. ("constSI" constants-unit-system SI))
  2493. "Variable associated with STARTUP options for org-mode.
  2494. Each element is a list of three items: The startup options as written
  2495. in the #+STARTUP line, the corresponding variable, and the value to
  2496. set this variable to if the option is found. An optional forth element PUSH
  2497. means to push this value onto the list in the variable.")
  2498. (defun org-set-regexps-and-options ()
  2499. "Precompute regular expressions for current buffer."
  2500. (when (org-mode-p)
  2501. (org-set-local 'org-todo-kwd-alist nil)
  2502. (org-set-local 'org-todo-key-alist nil)
  2503. (org-set-local 'org-todo-key-trigger nil)
  2504. (org-set-local 'org-todo-keywords-1 nil)
  2505. (org-set-local 'org-done-keywords nil)
  2506. (org-set-local 'org-todo-heads nil)
  2507. (org-set-local 'org-todo-sets nil)
  2508. (org-set-local 'org-todo-log-states nil)
  2509. (org-set-local 'org-file-properties nil)
  2510. (org-set-local 'org-file-tags nil)
  2511. (let ((re (org-make-options-regexp
  2512. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2513. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2514. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2515. (splitre "[ \t]+")
  2516. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2517. tail sep kws1 prio props ftags drawers
  2518. ext-setup-or-nil setup-contents (start 0))
  2519. (save-excursion
  2520. (save-restriction
  2521. (widen)
  2522. (goto-char (point-min))
  2523. (while (or (and ext-setup-or-nil
  2524. (string-match re ext-setup-or-nil start)
  2525. (setq start (match-end 0)))
  2526. (and (setq ext-setup-or-nil nil start 0)
  2527. (re-search-forward re nil t)))
  2528. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2529. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2530. (cond
  2531. ((equal key "CATEGORY")
  2532. (if (string-match "[ \t]+$" value)
  2533. (setq value (replace-match "" t t value)))
  2534. (setq cat value))
  2535. ((member key '("SEQ_TODO" "TODO"))
  2536. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2537. ((equal key "TYP_TODO")
  2538. (push (cons 'type (org-split-string value splitre)) kwds))
  2539. ((equal key "TAGS")
  2540. (setq tags (append tags (org-split-string value splitre))))
  2541. ((equal key "COLUMNS")
  2542. (org-set-local 'org-columns-default-format value))
  2543. ((equal key "LINK")
  2544. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2545. (push (cons (match-string 1 value)
  2546. (org-trim (match-string 2 value)))
  2547. links)))
  2548. ((equal key "PRIORITIES")
  2549. (setq prio (org-split-string value " +")))
  2550. ((equal key "PROPERTY")
  2551. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2552. (push (cons (match-string 1 value) (match-string 2 value))
  2553. props)))
  2554. ((equal key "FILETAGS")
  2555. (when (string-match "\\S-" value)
  2556. (setq ftags
  2557. (append
  2558. ftags
  2559. (apply 'append
  2560. (mapcar (lambda (x) (org-split-string x ":"))
  2561. (org-split-string value)))))))
  2562. ((equal key "DRAWERS")
  2563. (setq drawers (org-split-string value splitre)))
  2564. ((equal key "CONSTANTS")
  2565. (setq const (append const (org-split-string value splitre))))
  2566. ((equal key "STARTUP")
  2567. (let ((opts (org-split-string value splitre))
  2568. l var val)
  2569. (while (setq l (pop opts))
  2570. (when (setq l (assoc l org-startup-options))
  2571. (setq var (nth 1 l) val (nth 2 l))
  2572. (if (not (nth 3 l))
  2573. (set (make-local-variable var) val)
  2574. (if (not (listp (symbol-value var)))
  2575. (set (make-local-variable var) nil))
  2576. (set (make-local-variable var) (symbol-value var))
  2577. (add-to-list var val))))))
  2578. ((equal key "ARCHIVE")
  2579. (string-match " *$" value)
  2580. (setq arch (replace-match "" t t value))
  2581. (remove-text-properties 0 (length arch)
  2582. '(face t fontified t) arch))
  2583. ((equal key "SETUPFILE")
  2584. (setq setup-contents (org-file-contents
  2585. (expand-file-name
  2586. (org-remove-double-quotes value))
  2587. 'noerror))
  2588. (if (not ext-setup-or-nil)
  2589. (setq ext-setup-or-nil setup-contents start 0)
  2590. (setq ext-setup-or-nil
  2591. (concat (substring ext-setup-or-nil 0 start)
  2592. "\n" setup-contents "\n"
  2593. (substring ext-setup-or-nil start)))))
  2594. ))))
  2595. (when cat
  2596. (org-set-local 'org-category (intern cat))
  2597. (push (cons "CATEGORY" cat) props))
  2598. (when prio
  2599. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2600. (setq prio (mapcar 'string-to-char prio))
  2601. (org-set-local 'org-highest-priority (nth 0 prio))
  2602. (org-set-local 'org-lowest-priority (nth 1 prio))
  2603. (org-set-local 'org-default-priority (nth 2 prio)))
  2604. (and props (org-set-local 'org-file-properties (nreverse props)))
  2605. (and ftags (org-set-local 'org-file-tags ftags))
  2606. (and drawers (org-set-local 'org-drawers drawers))
  2607. (and arch (org-set-local 'org-archive-location arch))
  2608. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2609. ;; Process the TODO keywords
  2610. (unless kwds
  2611. ;; Use the global values as if they had been given locally.
  2612. (setq kwds (default-value 'org-todo-keywords))
  2613. (if (stringp (car kwds))
  2614. (setq kwds (list (cons org-todo-interpretation
  2615. (default-value 'org-todo-keywords)))))
  2616. (setq kwds (reverse kwds)))
  2617. (setq kwds (nreverse kwds))
  2618. (let (inter kws kw)
  2619. (while (setq kws (pop kwds))
  2620. (setq inter (pop kws) sep (member "|" kws)
  2621. kws0 (delete "|" (copy-sequence kws))
  2622. kwsa nil
  2623. kws1 (mapcar
  2624. (lambda (x)
  2625. ;; 1 2
  2626. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2627. (progn
  2628. (setq kw (match-string 1 x)
  2629. key (and (match-end 2) (match-string 2 x))
  2630. log (org-extract-log-state-settings x))
  2631. (push (cons kw (and key (string-to-char key))) kwsa)
  2632. (and log (push log org-todo-log-states))
  2633. kw)
  2634. (error "Invalid TODO keyword %s" x)))
  2635. kws0)
  2636. kwsa (if kwsa (append '((:startgroup))
  2637. (nreverse kwsa)
  2638. '((:endgroup))))
  2639. hw (car kws1)
  2640. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2641. tail (list inter hw (car dws) (org-last dws)))
  2642. (add-to-list 'org-todo-heads hw 'append)
  2643. (push kws1 org-todo-sets)
  2644. (setq org-done-keywords (append org-done-keywords dws nil))
  2645. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2646. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2647. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2648. (setq org-todo-sets (nreverse org-todo-sets)
  2649. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2650. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2651. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2652. ;; Process the constants
  2653. (when const
  2654. (let (e cst)
  2655. (while (setq e (pop const))
  2656. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2657. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2658. (setq org-table-formula-constants-local cst)))
  2659. ;; Process the tags.
  2660. (when tags
  2661. (let (e tgs)
  2662. (while (setq e (pop tags))
  2663. (cond
  2664. ((equal e "{") (push '(:startgroup) tgs))
  2665. ((equal e "}") (push '(:endgroup) tgs))
  2666. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2667. (push (cons (match-string 1 e)
  2668. (string-to-char (match-string 2 e)))
  2669. tgs))
  2670. (t (push (list e) tgs))))
  2671. (org-set-local 'org-tag-alist nil)
  2672. (while (setq e (pop tgs))
  2673. (or (and (stringp (car e))
  2674. (assoc (car e) org-tag-alist))
  2675. (push e org-tag-alist)))))
  2676. ;; Compute the regular expressions and other local variables
  2677. (if (not org-done-keywords)
  2678. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2679. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2680. (length org-scheduled-string)
  2681. (length org-clock-string)
  2682. (length org-closed-string)))
  2683. org-drawer-regexp
  2684. (concat "^[ \t]*:\\("
  2685. (mapconcat 'regexp-quote org-drawers "\\|")
  2686. "\\):[ \t]*$")
  2687. org-not-done-keywords
  2688. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2689. org-todo-regexp
  2690. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2691. "\\|") "\\)\\>")
  2692. org-not-done-regexp
  2693. (concat "\\<\\("
  2694. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2695. "\\)\\>")
  2696. org-todo-line-regexp
  2697. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2698. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2699. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2700. org-complex-heading-regexp
  2701. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2702. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2703. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2704. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2705. org-nl-done-regexp
  2706. (concat "\n\\*+[ \t]+"
  2707. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2708. "\\)" "\\>")
  2709. org-todo-line-tags-regexp
  2710. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2711. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2712. (org-re
  2713. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2714. org-looking-at-done-regexp
  2715. (concat "^" "\\(?:"
  2716. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2717. "\\>")
  2718. org-deadline-regexp (concat "\\<" org-deadline-string)
  2719. org-deadline-time-regexp
  2720. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2721. org-deadline-line-regexp
  2722. (concat "\\<\\(" org-deadline-string "\\).*")
  2723. org-scheduled-regexp
  2724. (concat "\\<" org-scheduled-string)
  2725. org-scheduled-time-regexp
  2726. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2727. org-closed-time-regexp
  2728. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2729. org-keyword-time-regexp
  2730. (concat "\\<\\(" org-scheduled-string
  2731. "\\|" org-deadline-string
  2732. "\\|" org-closed-string
  2733. "\\|" org-clock-string "\\)"
  2734. " *[[<]\\([^]>]+\\)[]>]")
  2735. org-keyword-time-not-clock-regexp
  2736. (concat "\\<\\(" org-scheduled-string
  2737. "\\|" org-deadline-string
  2738. "\\|" org-closed-string
  2739. "\\)"
  2740. " *[[<]\\([^]>]+\\)[]>]")
  2741. org-maybe-keyword-time-regexp
  2742. (concat "\\(\\<\\(" org-scheduled-string
  2743. "\\|" org-deadline-string
  2744. "\\|" org-closed-string
  2745. "\\|" org-clock-string "\\)\\)?"
  2746. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2747. org-planning-or-clock-line-re
  2748. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2749. "\\|" org-deadline-string
  2750. "\\|" org-closed-string "\\|" org-clock-string
  2751. "\\)\\>\\)")
  2752. )
  2753. (org-compute-latex-and-specials-regexp)
  2754. (org-set-font-lock-defaults))))
  2755. (defun org-file-contents (file &optional noerror)
  2756. "Return the contents of FILE, as a string."
  2757. (if (or (not file)
  2758. (not (file-readable-p file)))
  2759. (if noerror
  2760. (progn
  2761. (message "Cannot read file %s" file)
  2762. (ding) (sit-for 2)
  2763. "")
  2764. (error "Cannot read file %s" file))
  2765. (with-temp-buffer
  2766. (insert-file-contents file)
  2767. (buffer-string))))
  2768. (defun org-extract-log-state-settings (x)
  2769. "Extract the log state setting from a TODO keyword string.
  2770. This will extract info from a string like \"WAIT(w@/!)\"."
  2771. (let (kw key log1 log2)
  2772. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2773. (setq kw (match-string 1 x)
  2774. key (and (match-end 2) (match-string 2 x))
  2775. log1 (and (match-end 3) (match-string 3 x))
  2776. log2 (and (match-end 4) (match-string 4 x)))
  2777. (and (or log1 log2)
  2778. (list kw
  2779. (and log1 (if (equal log1 "!") 'time 'note))
  2780. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2781. (defun org-remove-keyword-keys (list)
  2782. "Remove a pair of parenthesis at the end of each string in LIST."
  2783. (mapcar (lambda (x)
  2784. (if (string-match "(.*)$" x)
  2785. (substring x 0 (match-beginning 0))
  2786. x))
  2787. list))
  2788. ;; FIXME: this could be done much better, using second characters etc.
  2789. (defun org-assign-fast-keys (alist)
  2790. "Assign fast keys to a keyword-key alist.
  2791. Respect keys that are already there."
  2792. (let (new e k c c1 c2 (char ?a))
  2793. (while (setq e (pop alist))
  2794. (cond
  2795. ((equal e '(:startgroup)) (push e new))
  2796. ((equal e '(:endgroup)) (push e new))
  2797. (t
  2798. (setq k (car e) c2 nil)
  2799. (if (cdr e)
  2800. (setq c (cdr e))
  2801. ;; automatically assign a character.
  2802. (setq c1 (string-to-char
  2803. (downcase (substring
  2804. k (if (= (string-to-char k) ?@) 1 0)))))
  2805. (if (or (rassoc c1 new) (rassoc c1 alist))
  2806. (while (or (rassoc char new) (rassoc char alist))
  2807. (setq char (1+ char)))
  2808. (setq c2 c1))
  2809. (setq c (or c2 char)))
  2810. (push (cons k c) new))))
  2811. (nreverse new)))
  2812. ;;; Some variables used in various places
  2813. (defvar org-window-configuration nil
  2814. "Used in various places to store a window configuration.")
  2815. (defvar org-finish-function nil
  2816. "Function to be called when `C-c C-c' is used.
  2817. This is for getting out of special buffers like remember.")
  2818. ;; FIXME: Occasionally check by commenting these, to make sure
  2819. ;; no other functions uses these, forgetting to let-bind them.
  2820. (defvar entry)
  2821. (defvar state)
  2822. (defvar last-state)
  2823. (defvar date)
  2824. (defvar description)
  2825. ;; Defined somewhere in this file, but used before definition.
  2826. (defvar org-html-entities)
  2827. (defvar org-struct-menu)
  2828. (defvar org-org-menu)
  2829. (defvar org-tbl-menu)
  2830. (defvar org-agenda-keymap)
  2831. ;;;; Define the Org-mode
  2832. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2833. (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."))
  2834. ;; We use a before-change function to check if a table might need
  2835. ;; an update.
  2836. (defvar org-table-may-need-update t
  2837. "Indicates that a table might need an update.
  2838. This variable is set by `org-before-change-function'.
  2839. `org-table-align' sets it back to nil.")
  2840. (defun org-before-change-function (beg end)
  2841. "Every change indicates that a table might need an update."
  2842. (setq org-table-may-need-update t))
  2843. (defvar org-mode-map)
  2844. (defvar org-mode-hook nil)
  2845. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2846. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2847. (defvar org-table-buffer-is-an nil)
  2848. (defconst org-outline-regexp "\\*+ ")
  2849. ;;;###autoload
  2850. (define-derived-mode org-mode outline-mode "Org"
  2851. "Outline-based notes management and organizer, alias
  2852. \"Carsten's outline-mode for keeping track of everything.\"
  2853. Org-mode develops organizational tasks around a NOTES file which
  2854. contains information about projects as plain text. Org-mode is
  2855. implemented on top of outline-mode, which is ideal to keep the content
  2856. of large files well structured. It supports ToDo items, deadlines and
  2857. time stamps, which magically appear in the diary listing of the Emacs
  2858. calendar. Tables are easily created with a built-in table editor.
  2859. Plain text URL-like links connect to websites, emails (VM), Usenet
  2860. messages (Gnus), BBDB entries, and any files related to the project.
  2861. For printing and sharing of notes, an Org-mode file (or a part of it)
  2862. can be exported as a structured ASCII or HTML file.
  2863. The following commands are available:
  2864. \\{org-mode-map}"
  2865. ;; Get rid of Outline menus, they are not needed
  2866. ;; Need to do this here because define-derived-mode sets up
  2867. ;; the keymap so late. Still, it is a waste to call this each time
  2868. ;; we switch another buffer into org-mode.
  2869. (if (featurep 'xemacs)
  2870. (when (boundp 'outline-mode-menu-heading)
  2871. ;; Assume this is Greg's port, it used easymenu
  2872. (easy-menu-remove outline-mode-menu-heading)
  2873. (easy-menu-remove outline-mode-menu-show)
  2874. (easy-menu-remove outline-mode-menu-hide))
  2875. (define-key org-mode-map [menu-bar headings] 'undefined)
  2876. (define-key org-mode-map [menu-bar hide] 'undefined)
  2877. (define-key org-mode-map [menu-bar show] 'undefined))
  2878. (org-load-modules-maybe)
  2879. (easy-menu-add org-org-menu)
  2880. (easy-menu-add org-tbl-menu)
  2881. (org-install-agenda-files-menu)
  2882. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2883. (org-add-to-invisibility-spec '(org-cwidth))
  2884. (when (featurep 'xemacs)
  2885. (org-set-local 'line-move-ignore-invisible t))
  2886. (org-set-local 'outline-regexp org-outline-regexp)
  2887. (org-set-local 'outline-level 'org-outline-level)
  2888. (when (and org-ellipsis
  2889. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2890. (fboundp 'make-glyph-code))
  2891. (unless org-display-table
  2892. (setq org-display-table (make-display-table)))
  2893. (set-display-table-slot
  2894. org-display-table 4
  2895. (vconcat (mapcar
  2896. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2897. org-ellipsis)))
  2898. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2899. (setq buffer-display-table org-display-table))
  2900. (org-set-regexps-and-options)
  2901. ;; Calc embedded
  2902. (org-set-local 'calc-embedded-open-mode "# ")
  2903. (modify-syntax-entry ?# "<")
  2904. (modify-syntax-entry ?@ "w")
  2905. (if org-startup-truncated (setq truncate-lines t))
  2906. (org-set-local 'font-lock-unfontify-region-function
  2907. 'org-unfontify-region)
  2908. ;; Activate before-change-function
  2909. (org-set-local 'org-table-may-need-update t)
  2910. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2911. 'local)
  2912. ;; Check for running clock before killing a buffer
  2913. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2914. ;; Paragraphs and auto-filling
  2915. (org-set-autofill-regexps)
  2916. (setq indent-line-function 'org-indent-line-function)
  2917. (org-update-radio-target-regexp)
  2918. ;; Comment characters
  2919. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2920. (org-set-local 'comment-padding " ")
  2921. ;; Align options lines
  2922. (org-set-local
  2923. 'align-mode-rules-list
  2924. '((org-in-buffer-settings
  2925. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2926. (modes . '(org-mode)))))
  2927. ;; Imenu
  2928. (org-set-local 'imenu-create-index-function
  2929. 'org-imenu-get-tree)
  2930. ;; Make isearch reveal context
  2931. (if (or (featurep 'xemacs)
  2932. (not (boundp 'outline-isearch-open-invisible-function)))
  2933. ;; Emacs 21 and XEmacs make use of the hook
  2934. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2935. ;; Emacs 22 deals with this through a special variable
  2936. (org-set-local 'outline-isearch-open-invisible-function
  2937. (lambda (&rest ignore) (org-show-context 'isearch))))
  2938. ;; If empty file that did not turn on org-mode automatically, make it to.
  2939. (if (and org-insert-mode-line-in-empty-file
  2940. (interactive-p)
  2941. (= (point-min) (point-max)))
  2942. (insert "# -*- mode: org -*-\n\n"))
  2943. (unless org-inhibit-startup
  2944. (when org-startup-align-all-tables
  2945. (let ((bmp (buffer-modified-p)))
  2946. (org-table-map-tables 'org-table-align)
  2947. (set-buffer-modified-p bmp)))
  2948. (org-set-startup-visibility)))
  2949. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2950. (defun org-current-time ()
  2951. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2952. (if (> (car org-time-stamp-rounding-minutes) 1)
  2953. (let ((r (car org-time-stamp-rounding-minutes))
  2954. (time (decode-time)))
  2955. (apply 'encode-time
  2956. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2957. (nthcdr 2 time))))
  2958. (current-time)))
  2959. ;;;; Font-Lock stuff, including the activators
  2960. (defvar org-mouse-map (make-sparse-keymap))
  2961. (org-defkey org-mouse-map
  2962. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2963. (org-defkey org-mouse-map
  2964. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2965. (when org-mouse-1-follows-link
  2966. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2967. (when org-tab-follows-link
  2968. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2969. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2970. (when org-return-follows-link
  2971. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2972. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2973. (require 'font-lock)
  2974. (defconst org-non-link-chars "]\t\n\r<>")
  2975. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2976. "shell" "elisp"))
  2977. (defvar org-link-types-re nil
  2978. "Matches a link that has a url-like prefix like \"http:\"")
  2979. (defvar org-link-re-with-space nil
  2980. "Matches a link with spaces, optional angular brackets around it.")
  2981. (defvar org-link-re-with-space2 nil
  2982. "Matches a link with spaces, optional angular brackets around it.")
  2983. (defvar org-angle-link-re nil
  2984. "Matches link with angular brackets, spaces are allowed.")
  2985. (defvar org-plain-link-re nil
  2986. "Matches plain link, without spaces.")
  2987. (defvar org-bracket-link-regexp nil
  2988. "Matches a link in double brackets.")
  2989. (defvar org-bracket-link-analytic-regexp nil
  2990. "Regular expression used to analyze links.
  2991. Here is what the match groups contain after a match:
  2992. 1: http:
  2993. 2: http
  2994. 3: path
  2995. 4: [desc]
  2996. 5: desc")
  2997. (defvar org-any-link-re nil
  2998. "Regular expression matching any link.")
  2999. (defun org-make-link-regexps ()
  3000. "Update the link regular expressions.
  3001. This should be called after the variable `org-link-types' has changed."
  3002. (setq org-link-types-re
  3003. (concat
  3004. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3005. org-link-re-with-space
  3006. (concat
  3007. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3008. "\\([^" org-non-link-chars " ]"
  3009. "[^" org-non-link-chars "]*"
  3010. "[^" org-non-link-chars " ]\\)>?")
  3011. org-link-re-with-space2
  3012. (concat
  3013. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3014. "\\([^" org-non-link-chars " ]"
  3015. "[^]\t\n\r]*"
  3016. "[^" org-non-link-chars " ]\\)>?")
  3017. org-angle-link-re
  3018. (concat
  3019. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3020. "\\([^" org-non-link-chars " ]"
  3021. "[^" org-non-link-chars "]*"
  3022. "\\)>")
  3023. org-plain-link-re
  3024. (concat
  3025. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3026. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3027. org-bracket-link-regexp
  3028. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3029. org-bracket-link-analytic-regexp
  3030. (concat
  3031. "\\[\\["
  3032. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3033. "\\([^]]+\\)"
  3034. "\\]"
  3035. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3036. "\\]")
  3037. org-any-link-re
  3038. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3039. org-angle-link-re "\\)\\|\\("
  3040. org-plain-link-re "\\)")))
  3041. (org-make-link-regexps)
  3042. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3043. "Regular expression for fast time stamp matching.")
  3044. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3045. "Regular expression for fast time stamp matching.")
  3046. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3047. "Regular expression matching time strings for analysis.
  3048. This one does not require the space after the date, so it can be used
  3049. on a string that terminates immediately after the date.")
  3050. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3051. "Regular expression matching time strings for analysis.")
  3052. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3053. "Regular expression matching time stamps, with groups.")
  3054. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3055. "Regular expression matching time stamps (also [..]), with groups.")
  3056. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3057. "Regular expression matching a time stamp range.")
  3058. (defconst org-tr-regexp-both
  3059. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3060. "Regular expression matching a time stamp range.")
  3061. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3062. org-ts-regexp "\\)?")
  3063. "Regular expression matching a time stamp or time stamp range.")
  3064. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3065. org-ts-regexp-both "\\)?")
  3066. "Regular expression matching a time stamp or time stamp range.
  3067. The time stamps may be either active or inactive.")
  3068. (defvar org-emph-face nil)
  3069. (defun org-do-emphasis-faces (limit)
  3070. "Run through the buffer and add overlays to links."
  3071. (let (rtn)
  3072. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3073. (if (not (= (char-after (match-beginning 3))
  3074. (char-after (match-beginning 4))))
  3075. (progn
  3076. (setq rtn t)
  3077. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3078. 'face
  3079. (nth 1 (assoc (match-string 3)
  3080. org-emphasis-alist)))
  3081. (add-text-properties (match-beginning 2) (match-end 2)
  3082. '(font-lock-multiline t))
  3083. (when org-hide-emphasis-markers
  3084. (add-text-properties (match-end 4) (match-beginning 5)
  3085. '(invisible org-link))
  3086. (add-text-properties (match-beginning 3) (match-end 3)
  3087. '(invisible org-link)))))
  3088. (backward-char 1))
  3089. rtn))
  3090. (defun org-emphasize (&optional char)
  3091. "Insert or change an emphasis, i.e. a font like bold or italic.
  3092. If there is an active region, change that region to a new emphasis.
  3093. If there is no region, just insert the marker characters and position
  3094. the cursor between them.
  3095. CHAR should be either the marker character, or the first character of the
  3096. HTML tag associated with that emphasis. If CHAR is a space, the means
  3097. to remove the emphasis of the selected region.
  3098. If char is not given (for example in an interactive call) it
  3099. will be prompted for."
  3100. (interactive)
  3101. (let ((eal org-emphasis-alist) e det
  3102. (erc org-emphasis-regexp-components)
  3103. (prompt "")
  3104. (string "") beg end move tag c s)
  3105. (if (org-region-active-p)
  3106. (setq beg (region-beginning) end (region-end)
  3107. string (buffer-substring beg end))
  3108. (setq move t))
  3109. (while (setq e (pop eal))
  3110. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3111. c (aref tag 0))
  3112. (push (cons c (string-to-char (car e))) det)
  3113. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3114. (substring tag 1)))))
  3115. (unless char
  3116. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3117. (setq char (read-char-exclusive)))
  3118. (setq char (or (cdr (assoc char det)) char))
  3119. (if (equal char ?\ )
  3120. (setq s "" move nil)
  3121. (unless (assoc (char-to-string char) org-emphasis-alist)
  3122. (error "No such emphasis marker: \"%c\"" char))
  3123. (setq s (char-to-string char)))
  3124. (while (and (> (length string) 1)
  3125. (equal (substring string 0 1) (substring string -1))
  3126. (assoc (substring string 0 1) org-emphasis-alist))
  3127. (setq string (substring string 1 -1)))
  3128. (setq string (concat s string s))
  3129. (if beg (delete-region beg end))
  3130. (unless (or (bolp)
  3131. (string-match (concat "[" (nth 0 erc) "\n]")
  3132. (char-to-string (char-before (point)))))
  3133. (insert " "))
  3134. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3135. (char-to-string (char-after (point))))
  3136. (insert " ") (backward-char 1))
  3137. (insert string)
  3138. (and move (backward-char 1))))
  3139. (defconst org-nonsticky-props
  3140. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3141. (defun org-activate-plain-links (limit)
  3142. "Run through the buffer and add overlays to links."
  3143. (catch 'exit
  3144. (let (f)
  3145. (while (re-search-forward org-plain-link-re limit t)
  3146. (setq f (get-text-property (match-beginning 0) 'face))
  3147. (if (or (eq f 'org-tag)
  3148. (and (listp f) (memq 'org-tag f)))
  3149. nil
  3150. (add-text-properties (match-beginning 0) (match-end 0)
  3151. (list 'mouse-face 'highlight
  3152. 'rear-nonsticky org-nonsticky-props
  3153. 'keymap org-mouse-map
  3154. ))
  3155. (throw 'exit t))))))
  3156. (defun org-activate-code (limit)
  3157. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3158. (progn
  3159. (remove-text-properties (match-beginning 0) (match-end 0)
  3160. '(display t invisible t intangible t))
  3161. t)))
  3162. (defun org-activate-angle-links (limit)
  3163. "Run through the buffer and add overlays to links."
  3164. (if (re-search-forward org-angle-link-re limit t)
  3165. (progn
  3166. (add-text-properties (match-beginning 0) (match-end 0)
  3167. (list 'mouse-face 'highlight
  3168. 'rear-nonsticky org-nonsticky-props
  3169. 'keymap org-mouse-map
  3170. ))
  3171. t)))
  3172. (defun org-activate-bracket-links (limit)
  3173. "Run through the buffer and add overlays to bracketed links."
  3174. (if (re-search-forward org-bracket-link-regexp limit t)
  3175. (let* ((help (concat "LINK: "
  3176. (org-match-string-no-properties 1)))
  3177. ;; FIXME: above we should remove the escapes.
  3178. ;; but that requires another match, protecting match data,
  3179. ;; a lot of overhead for font-lock.
  3180. (ip (org-maybe-intangible
  3181. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3182. 'keymap org-mouse-map 'mouse-face 'highlight
  3183. 'font-lock-multiline t 'help-echo help)))
  3184. (vp (list 'rear-nonsticky org-nonsticky-props
  3185. 'keymap org-mouse-map 'mouse-face 'highlight
  3186. ' font-lock-multiline t 'help-echo help)))
  3187. ;; We need to remove the invisible property here. Table narrowing
  3188. ;; may have made some of this invisible.
  3189. (remove-text-properties (match-beginning 0) (match-end 0)
  3190. '(invisible nil))
  3191. (if (match-end 3)
  3192. (progn
  3193. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3194. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3195. (add-text-properties (match-end 3) (match-end 0) ip))
  3196. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3197. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3198. (add-text-properties (match-end 1) (match-end 0) ip))
  3199. t)))
  3200. (defun org-activate-dates (limit)
  3201. "Run through the buffer and add overlays to dates."
  3202. (if (re-search-forward org-tsr-regexp-both limit t)
  3203. (progn
  3204. (add-text-properties (match-beginning 0) (match-end 0)
  3205. (list 'mouse-face 'highlight
  3206. 'rear-nonsticky org-nonsticky-props
  3207. 'keymap org-mouse-map))
  3208. (when org-display-custom-times
  3209. (if (match-end 3)
  3210. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3211. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3212. t)))
  3213. (defvar org-target-link-regexp nil
  3214. "Regular expression matching radio targets in plain text.")
  3215. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3216. "Regular expression matching a link target.")
  3217. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3218. "Regular expression matching a radio target.")
  3219. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3220. "Regular expression matching any target.")
  3221. (defun org-activate-target-links (limit)
  3222. "Run through the buffer and add overlays to target matches."
  3223. (when org-target-link-regexp
  3224. (let ((case-fold-search t))
  3225. (if (re-search-forward org-target-link-regexp limit t)
  3226. (progn
  3227. (add-text-properties (match-beginning 0) (match-end 0)
  3228. (list 'mouse-face 'highlight
  3229. 'rear-nonsticky org-nonsticky-props
  3230. 'keymap org-mouse-map
  3231. 'help-echo "Radio target link"
  3232. 'org-linked-text t))
  3233. t)))))
  3234. (defun org-update-radio-target-regexp ()
  3235. "Find all radio targets in this file and update the regular expression."
  3236. (interactive)
  3237. (when (memq 'radio org-activate-links)
  3238. (setq org-target-link-regexp
  3239. (org-make-target-link-regexp (org-all-targets 'radio)))
  3240. (org-restart-font-lock)))
  3241. (defun org-hide-wide-columns (limit)
  3242. (let (s e)
  3243. (setq s (text-property-any (point) (or limit (point-max))
  3244. 'org-cwidth t))
  3245. (when s
  3246. (setq e (next-single-property-change s 'org-cwidth))
  3247. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3248. (goto-char e)
  3249. t)))
  3250. (defvar org-latex-and-specials-regexp nil
  3251. "Regular expression for highlighting export special stuff.")
  3252. (defvar org-match-substring-regexp)
  3253. (defvar org-match-substring-with-braces-regexp)
  3254. (defvar org-export-html-special-string-regexps)
  3255. (defun org-compute-latex-and-specials-regexp ()
  3256. "Compute regular expression for stuff treated specially by exporters."
  3257. (if (not org-highlight-latex-fragments-and-specials)
  3258. (org-set-local 'org-latex-and-specials-regexp nil)
  3259. (require 'org-exp)
  3260. (let*
  3261. ((matchers (plist-get org-format-latex-options :matchers))
  3262. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3263. org-latex-regexps)))
  3264. (options (org-combine-plists (org-default-export-plist)
  3265. (org-infile-export-plist)))
  3266. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3267. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3268. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3269. (org-export-html-expand (plist-get options :expand-quoted-html))
  3270. (org-export-with-special-strings (plist-get options :special-strings))
  3271. (re-sub
  3272. (cond
  3273. ((equal org-export-with-sub-superscripts '{})
  3274. (list org-match-substring-with-braces-regexp))
  3275. (org-export-with-sub-superscripts
  3276. (list org-match-substring-regexp))
  3277. (t nil)))
  3278. (re-latex
  3279. (if org-export-with-LaTeX-fragments
  3280. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3281. (re-macros
  3282. (if org-export-with-TeX-macros
  3283. (list (concat "\\\\"
  3284. (regexp-opt
  3285. (append (mapcar 'car org-html-entities)
  3286. (if (boundp 'org-latex-entities)
  3287. org-latex-entities nil))
  3288. 'words))) ; FIXME
  3289. ))
  3290. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3291. (re-special (if org-export-with-special-strings
  3292. (mapcar (lambda (x) (car x))
  3293. org-export-html-special-string-regexps)))
  3294. (re-rest
  3295. (delq nil
  3296. (list
  3297. (if org-export-html-expand "@<[^>\n]+>")
  3298. ))))
  3299. (org-set-local
  3300. 'org-latex-and-specials-regexp
  3301. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3302. re-rest) "\\|")))))
  3303. (defun org-do-latex-and-special-faces (limit)
  3304. "Run through the buffer and add overlays to links."
  3305. (when org-latex-and-specials-regexp
  3306. (let (rtn d)
  3307. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3308. limit t))
  3309. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3310. 'face))
  3311. '(org-code org-verbatim underline)))
  3312. (progn
  3313. (setq rtn t
  3314. d (cond ((member (char-after (1+ (match-beginning 0)))
  3315. '(?_ ?^)) 1)
  3316. (t 0)))
  3317. (font-lock-prepend-text-property
  3318. (+ d (match-beginning 0)) (match-end 0)
  3319. 'face 'org-latex-and-export-specials)
  3320. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3321. '(font-lock-multiline t)))))
  3322. rtn)))
  3323. (defun org-restart-font-lock ()
  3324. "Restart font-lock-mode, to force refontification."
  3325. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3326. (font-lock-mode -1)
  3327. (font-lock-mode 1)))
  3328. (defun org-all-targets (&optional radio)
  3329. "Return a list of all targets in this file.
  3330. With optional argument RADIO, only find radio targets."
  3331. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3332. rtn)
  3333. (save-excursion
  3334. (goto-char (point-min))
  3335. (while (re-search-forward re nil t)
  3336. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3337. rtn)))
  3338. (defun org-make-target-link-regexp (targets)
  3339. "Make regular expression matching all strings in TARGETS.
  3340. The regular expression finds the targets also if there is a line break
  3341. between words."
  3342. (and targets
  3343. (concat
  3344. "\\<\\("
  3345. (mapconcat
  3346. (lambda (x)
  3347. (while (string-match " +" x)
  3348. (setq x (replace-match "\\s-+" t t x)))
  3349. x)
  3350. targets
  3351. "\\|")
  3352. "\\)\\>")))
  3353. (defun org-activate-tags (limit)
  3354. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3355. (progn
  3356. (add-text-properties (match-beginning 1) (match-end 1)
  3357. (list 'mouse-face 'highlight
  3358. 'rear-nonsticky org-nonsticky-props
  3359. 'keymap org-mouse-map))
  3360. t)))
  3361. (defun org-outline-level ()
  3362. (save-excursion
  3363. (looking-at outline-regexp)
  3364. (if (match-beginning 1)
  3365. (+ (org-get-string-indentation (match-string 1)) 1000)
  3366. (1- (- (match-end 0) (match-beginning 0))))))
  3367. (defvar org-font-lock-keywords nil)
  3368. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3369. "Regular expression matching a property line.")
  3370. (defvar org-font-lock-hook nil
  3371. "Functions to be called for special font lock stuff.")
  3372. (defun org-font-lock-hook (limit)
  3373. (run-hook-with-args 'org-font-lock-hook limit))
  3374. (defun org-set-font-lock-defaults ()
  3375. (let* ((em org-fontify-emphasized-text)
  3376. (lk org-activate-links)
  3377. (org-font-lock-extra-keywords
  3378. (list
  3379. ;; Call the hook
  3380. '(org-font-lock-hook)
  3381. ;; Headlines
  3382. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3383. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3384. ;; Table lines
  3385. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3386. (1 'org-table t))
  3387. ;; Table internals
  3388. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3389. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3390. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3391. ;; Drawers
  3392. (list org-drawer-regexp '(0 'org-special-keyword t))
  3393. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3394. ;; Properties
  3395. (list org-property-re
  3396. '(1 'org-special-keyword t)
  3397. '(3 'org-property-value t))
  3398. (if org-format-transports-properties-p
  3399. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3400. ;; Links
  3401. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3402. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3403. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3404. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3405. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3406. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3407. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3408. '(org-hide-wide-columns (0 nil append))
  3409. ;; TODO lines
  3410. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3411. '(1 (org-get-todo-face 1) t))
  3412. ;; DONE
  3413. (if org-fontify-done-headline
  3414. (list (concat "^[*]+ +\\<\\("
  3415. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3416. "\\)\\(.*\\)")
  3417. '(2 'org-headline-done t))
  3418. nil)
  3419. ;; Priorities
  3420. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3421. ;; Special keywords
  3422. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3423. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3424. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3425. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3426. ;; Emphasis
  3427. (if em
  3428. (if (featurep 'xemacs)
  3429. '(org-do-emphasis-faces (0 nil append))
  3430. '(org-do-emphasis-faces)))
  3431. ;; Checkboxes
  3432. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3433. 2 'bold prepend)
  3434. (if org-provide-checkbox-statistics
  3435. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3436. (0 (org-get-checkbox-statistics-face) t)))
  3437. ;; Description list items
  3438. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3439. 2 'bold prepend)
  3440. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3441. '(1 'org-archived prepend))
  3442. ;; Specials
  3443. '(org-do-latex-and-special-faces)
  3444. ;; Code
  3445. '(org-activate-code (1 'org-code t))
  3446. ;; COMMENT
  3447. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3448. "\\|" org-quote-string "\\)\\>")
  3449. '(1 'org-special-keyword t))
  3450. '("^#.*" (0 'font-lock-comment-face t))
  3451. )))
  3452. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3453. ;; Now set the full font-lock-keywords
  3454. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3455. (org-set-local 'font-lock-defaults
  3456. '(org-font-lock-keywords t nil nil backward-paragraph))
  3457. (kill-local-variable 'font-lock-keywords) nil))
  3458. (defvar org-m nil)
  3459. (defvar org-l nil)
  3460. (defvar org-f nil)
  3461. (defun org-get-level-face (n)
  3462. "Get the right face for match N in font-lock matching of healdines."
  3463. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3464. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3465. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3466. (cond
  3467. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3468. ((eq n 2) org-f)
  3469. (t (if org-level-color-stars-only nil org-f))))
  3470. (defun org-get-todo-face (kwd)
  3471. "Get the right face for a TODO keyword KWD.
  3472. If KWD is a number, get the corresponding match group."
  3473. (if (numberp kwd) (setq kwd (match-string kwd)))
  3474. (or (cdr (assoc kwd org-todo-keyword-faces))
  3475. (and (member kwd org-done-keywords) 'org-done)
  3476. 'org-todo))
  3477. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3478. "Remove fontification and activation overlays from links."
  3479. (font-lock-default-unfontify-region beg end)
  3480. (let* ((buffer-undo-list t)
  3481. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3482. (inhibit-modification-hooks t)
  3483. deactivate-mark buffer-file-name buffer-file-truename)
  3484. (remove-text-properties beg end
  3485. '(mouse-face t keymap t org-linked-text t
  3486. invisible t intangible t))))
  3487. ;;;; Visibility cycling, including org-goto and indirect buffer
  3488. ;;; Cycling
  3489. (defvar org-cycle-global-status nil)
  3490. (make-variable-buffer-local 'org-cycle-global-status)
  3491. (defvar org-cycle-subtree-status nil)
  3492. (make-variable-buffer-local 'org-cycle-subtree-status)
  3493. ;;;###autoload
  3494. (defun org-cycle (&optional arg)
  3495. "Visibility cycling for Org-mode.
  3496. - When this function is called with a prefix argument, rotate the entire
  3497. buffer through 3 states (global cycling)
  3498. 1. OVERVIEW: Show only top-level headlines.
  3499. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3500. 3. SHOW ALL: Show everything.
  3501. When called with two C-u C-u prefixes, switch to the startup visibility,
  3502. determined by the variable `org-startup-folded', and by any VISIBILITY
  3503. properties in the buffer.
  3504. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3505. including drawers.
  3506. - When point is at the beginning of a headline, rotate the subtree started
  3507. by this line through 3 different states (local cycling)
  3508. 1. FOLDED: Only the main headline is shown.
  3509. 2. CHILDREN: The main headline and the direct children are shown.
  3510. From this state, you can move to one of the children
  3511. and zoom in further.
  3512. 3. SUBTREE: Show the entire subtree, including body text.
  3513. - When there is a numeric prefix, go up to a heading with level ARG, do
  3514. a `show-subtree' and return to the previous cursor position. If ARG
  3515. is negative, go up that many levels.
  3516. - When point is not at the beginning of a headline, execute the global
  3517. binding for TAB, which is re-indenting the line. See the option
  3518. `org-cycle-emulate-tab' for details.
  3519. - Special case: if point is at the beginning of the buffer and there is
  3520. no headline in line 1, this function will act as if called with prefix arg.
  3521. But only if also the variable `org-cycle-global-at-bob' is t."
  3522. (interactive "P")
  3523. (org-load-modules-maybe)
  3524. (let* ((outline-regexp
  3525. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3526. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3527. outline-regexp))
  3528. (bob-special (and org-cycle-global-at-bob (bobp)
  3529. (not (looking-at outline-regexp))))
  3530. (org-cycle-hook
  3531. (if bob-special
  3532. (delq 'org-optimize-window-after-visibility-change
  3533. (copy-sequence org-cycle-hook))
  3534. org-cycle-hook))
  3535. (pos (point)))
  3536. (if (or bob-special (equal arg '(4)))
  3537. ;; special case: use global cycling
  3538. (setq arg t))
  3539. (cond
  3540. ((equal arg '(16))
  3541. (org-set-startup-visibility)
  3542. (message "Startup visibility, plus VISIBILITY properties"))
  3543. ((equal arg '(64))
  3544. (show-all)
  3545. (message "Entire buffer visible, including drawers"))
  3546. ((org-at-table-p 'any)
  3547. ;; Enter the table or move to the next field in the table
  3548. (or (org-table-recognize-table.el)
  3549. (progn
  3550. (if arg (org-table-edit-field t)
  3551. (org-table-justify-field-maybe)
  3552. (call-interactively 'org-table-next-field)))))
  3553. ((eq arg t) ;; Global cycling
  3554. (cond
  3555. ((and (eq last-command this-command)
  3556. (eq org-cycle-global-status 'overview))
  3557. ;; We just created the overview - now do table of contents
  3558. ;; This can be slow in very large buffers, so indicate action
  3559. (message "CONTENTS...")
  3560. (org-content)
  3561. (message "CONTENTS...done")
  3562. (setq org-cycle-global-status 'contents)
  3563. (run-hook-with-args 'org-cycle-hook 'contents))
  3564. ((and (eq last-command this-command)
  3565. (eq org-cycle-global-status 'contents))
  3566. ;; We just showed the table of contents - now show everything
  3567. (show-all)
  3568. (message "SHOW ALL")
  3569. (setq org-cycle-global-status 'all)
  3570. (run-hook-with-args 'org-cycle-hook 'all))
  3571. (t
  3572. ;; Default action: go to overview
  3573. (org-overview)
  3574. (message "OVERVIEW")
  3575. (setq org-cycle-global-status 'overview)
  3576. (run-hook-with-args 'org-cycle-hook 'overview))))
  3577. ((and org-drawers org-drawer-regexp
  3578. (save-excursion
  3579. (beginning-of-line 1)
  3580. (looking-at org-drawer-regexp)))
  3581. ;; Toggle block visibility
  3582. (org-flag-drawer
  3583. (not (get-char-property (match-end 0) 'invisible))))
  3584. ((integerp arg)
  3585. ;; Show-subtree, ARG levels up from here.
  3586. (save-excursion
  3587. (org-back-to-heading)
  3588. (outline-up-heading (if (< arg 0) (- arg)
  3589. (- (funcall outline-level) arg)))
  3590. (org-show-subtree)))
  3591. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3592. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3593. ;; At a heading: rotate between three different views
  3594. (org-back-to-heading)
  3595. (let ((goal-column 0) eoh eol eos)
  3596. ;; First, some boundaries
  3597. (save-excursion
  3598. (org-back-to-heading)
  3599. (save-excursion
  3600. (beginning-of-line 2)
  3601. (while (and (not (eobp)) ;; this is like `next-line'
  3602. (get-char-property (1- (point)) 'invisible))
  3603. (beginning-of-line 2)) (setq eol (point)))
  3604. (outline-end-of-heading) (setq eoh (point))
  3605. (org-end-of-subtree t)
  3606. (unless (eobp)
  3607. (skip-chars-forward " \t\n")
  3608. (beginning-of-line 1) ; in case this is an item
  3609. )
  3610. (setq eos (1- (point))))
  3611. ;; Find out what to do next and set `this-command'
  3612. (cond
  3613. ((= eos eoh)
  3614. ;; Nothing is hidden behind this heading
  3615. (message "EMPTY ENTRY")
  3616. (setq org-cycle-subtree-status nil)
  3617. (save-excursion
  3618. (goto-char eos)
  3619. (outline-next-heading)
  3620. (if (org-invisible-p) (org-flag-heading nil))))
  3621. ((or (>= eol eos)
  3622. (not (string-match "\\S-" (buffer-substring eol eos))))
  3623. ;; Entire subtree is hidden in one line: open it
  3624. (org-show-entry)
  3625. (show-children)
  3626. (message "CHILDREN")
  3627. (save-excursion
  3628. (goto-char eos)
  3629. (outline-next-heading)
  3630. (if (org-invisible-p) (org-flag-heading nil)))
  3631. (setq org-cycle-subtree-status 'children)
  3632. (run-hook-with-args 'org-cycle-hook 'children))
  3633. ((and (eq last-command this-command)
  3634. (eq org-cycle-subtree-status 'children))
  3635. ;; We just showed the children, now show everything.
  3636. (org-show-subtree)
  3637. (message "SUBTREE")
  3638. (setq org-cycle-subtree-status 'subtree)
  3639. (run-hook-with-args 'org-cycle-hook 'subtree))
  3640. (t
  3641. ;; Default action: hide the subtree.
  3642. (hide-subtree)
  3643. (message "FOLDED")
  3644. (setq org-cycle-subtree-status 'folded)
  3645. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3646. ;; TAB emulation and template completion
  3647. (buffer-read-only (org-back-to-heading))
  3648. ((org-try-structure-completion))
  3649. ((org-try-cdlatex-tab))
  3650. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3651. (or (not (bolp))
  3652. (not (looking-at outline-regexp))))
  3653. (call-interactively (global-key-binding "\t")))
  3654. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3655. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3656. (or (and (eq org-cycle-emulate-tab 'white)
  3657. (= (match-end 0) (point-at-eol)))
  3658. (and (eq org-cycle-emulate-tab 'whitestart)
  3659. (>= (match-end 0) pos))))
  3660. t
  3661. (eq org-cycle-emulate-tab t))
  3662. (call-interactively (global-key-binding "\t")))
  3663. (t (save-excursion
  3664. (org-back-to-heading)
  3665. (org-cycle))))))
  3666. ;;;###autoload
  3667. (defun org-global-cycle (&optional arg)
  3668. "Cycle the global visibility. For details see `org-cycle'.
  3669. With C-u prefix arg, switch to startup visibility.
  3670. With a numeric prefix, show all headlines up to that level."
  3671. (interactive "P")
  3672. (let ((org-cycle-include-plain-lists
  3673. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3674. (cond
  3675. ((integerp arg)
  3676. (show-all)
  3677. (hide-sublevels arg)
  3678. (setq org-cycle-global-status 'contents))
  3679. ((equal arg '(4))
  3680. (org-set-startup-visibility)
  3681. (message "Startup visibility, plus VISIBILITY properties."))
  3682. (t
  3683. (org-cycle '(4))))))
  3684. (defun org-set-startup-visibility ()
  3685. "Set the visibility required by startup options and properties."
  3686. (cond
  3687. ((eq org-startup-folded t)
  3688. (org-cycle '(4)))
  3689. ((eq org-startup-folded 'content)
  3690. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3691. (org-cycle '(4)) (org-cycle '(4)))))
  3692. (org-set-visibility-according-to-property 'no-cleanup)
  3693. (org-cycle-hide-archived-subtrees 'all)
  3694. (org-cycle-hide-drawers 'all)
  3695. (org-cycle-show-empty-lines 'all))
  3696. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3697. "Switch subtree visibilities according to :VISIBILITY: property."
  3698. (interactive)
  3699. (let (state)
  3700. (save-excursion
  3701. (goto-char (point-min))
  3702. (while (re-search-forward
  3703. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3704. nil t)
  3705. (setq state (match-string 1))
  3706. (save-excursion
  3707. (org-back-to-heading t)
  3708. (hide-subtree)
  3709. (org-reveal)
  3710. (cond
  3711. ((equal state '("fold" "folded"))
  3712. (hide-subtree))
  3713. ((equal state "children")
  3714. (org-show-hidden-entry)
  3715. (show-children))
  3716. ((equal state "content")
  3717. (save-excursion
  3718. (save-restriction
  3719. (org-narrow-to-subtree)
  3720. (org-content))))
  3721. ((member state '("all" "showall"))
  3722. (show-subtree)))))
  3723. (unless no-cleanup
  3724. (org-cycle-hide-archived-subtrees 'all)
  3725. (org-cycle-hide-drawers 'all)
  3726. (org-cycle-show-empty-lines 'all)))))
  3727. (defun org-overview ()
  3728. "Switch to overview mode, shoing only top-level headlines.
  3729. Really, this shows all headlines with level equal or greater than the level
  3730. of the first headline in the buffer. This is important, because if the
  3731. first headline is not level one, then (hide-sublevels 1) gives confusing
  3732. results."
  3733. (interactive)
  3734. (let ((level (save-excursion
  3735. (goto-char (point-min))
  3736. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3737. (progn
  3738. (goto-char (match-beginning 0))
  3739. (funcall outline-level))))))
  3740. (and level (hide-sublevels level))))
  3741. (defun org-content (&optional arg)
  3742. "Show all headlines in the buffer, like a table of contents.
  3743. With numerical argument N, show content up to level N."
  3744. (interactive "P")
  3745. (save-excursion
  3746. ;; Visit all headings and show their offspring
  3747. (and (integerp arg) (org-overview))
  3748. (goto-char (point-max))
  3749. (catch 'exit
  3750. (while (and (progn (condition-case nil
  3751. (outline-previous-visible-heading 1)
  3752. (error (goto-char (point-min))))
  3753. t)
  3754. (looking-at outline-regexp))
  3755. (if (integerp arg)
  3756. (show-children (1- arg))
  3757. (show-branches))
  3758. (if (bobp) (throw 'exit nil))))))
  3759. (defun org-optimize-window-after-visibility-change (state)
  3760. "Adjust the window after a change in outline visibility.
  3761. This function is the default value of the hook `org-cycle-hook'."
  3762. (when (get-buffer-window (current-buffer))
  3763. (cond
  3764. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3765. ; ((eq state 'overview) (org-beginning-of-line))
  3766. ((eq state 'content) nil)
  3767. ((eq state 'all) nil)
  3768. ((eq state 'folded) nil)
  3769. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3770. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3771. (defun org-compact-display-after-subtree-move ()
  3772. (let (beg end)
  3773. (save-excursion
  3774. (if (org-up-heading-safe)
  3775. (progn
  3776. (hide-subtree)
  3777. (show-entry)
  3778. (show-children)
  3779. (org-cycle-show-empty-lines 'children)
  3780. (org-cycle-hide-drawers 'children))
  3781. (org-overview)))))
  3782. (defun org-cycle-show-empty-lines (state)
  3783. "Show empty lines above all visible headlines.
  3784. The region to be covered depends on STATE when called through
  3785. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3786. entire buffer covered. Note that an empty line is only shown if there
  3787. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3788. (when (> org-cycle-separator-lines 0)
  3789. (save-excursion
  3790. (let* ((n org-cycle-separator-lines)
  3791. (re (cond
  3792. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3793. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3794. (t (let ((ns (number-to-string (- n 2))))
  3795. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3796. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3797. beg end)
  3798. (cond
  3799. ((memq state '(overview contents t))
  3800. (setq beg (point-min) end (point-max)))
  3801. ((memq state '(children folded))
  3802. (setq beg (point) end (progn (org-end-of-subtree t t)
  3803. (beginning-of-line 2)
  3804. (point)))))
  3805. (when beg
  3806. (goto-char beg)
  3807. (while (re-search-forward re end t)
  3808. (if (not (get-char-property (match-end 1) 'invisible))
  3809. (outline-flag-region
  3810. (match-beginning 1) (match-end 1) nil)))))))
  3811. ;; Never hide empty lines at the end of the file.
  3812. (save-excursion
  3813. (goto-char (point-max))
  3814. (outline-previous-heading)
  3815. (outline-end-of-heading)
  3816. (if (and (looking-at "[ \t\n]+")
  3817. (= (match-end 0) (point-max)))
  3818. (outline-flag-region (point) (match-end 0) nil))))
  3819. (defun org-show-empty-lines-in-parent ()
  3820. "Move to the parent and re-show empty lines before visible headlines."
  3821. (save-excursion
  3822. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3823. (org-cycle-show-empty-lines context))))
  3824. (defun org-cycle-hide-drawers (state)
  3825. "Re-hide all drawers after a visibility state change."
  3826. (when (and (org-mode-p)
  3827. (not (memq state '(overview folded))))
  3828. (save-excursion
  3829. (let* ((globalp (memq state '(contents all)))
  3830. (beg (if globalp (point-min) (point)))
  3831. (end (if globalp (point-max) (org-end-of-subtree t))))
  3832. (goto-char beg)
  3833. (while (re-search-forward org-drawer-regexp end t)
  3834. (org-flag-drawer t))))))
  3835. (defun org-flag-drawer (flag)
  3836. (save-excursion
  3837. (beginning-of-line 1)
  3838. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3839. (let ((b (match-end 0))
  3840. (outline-regexp org-outline-regexp))
  3841. (if (re-search-forward
  3842. "^[ \t]*:END:"
  3843. (save-excursion (outline-next-heading) (point)) t)
  3844. (outline-flag-region b (point-at-eol) flag)
  3845. (error ":END: line missing"))))))
  3846. (defun org-subtree-end-visible-p ()
  3847. "Is the end of the current subtree visible?"
  3848. (pos-visible-in-window-p
  3849. (save-excursion (org-end-of-subtree t) (point))))
  3850. (defun org-first-headline-recenter (&optional N)
  3851. "Move cursor to the first headline and recenter the headline.
  3852. Optional argument N means, put the headline into the Nth line of the window."
  3853. (goto-char (point-min))
  3854. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3855. (beginning-of-line)
  3856. (recenter (prefix-numeric-value N))))
  3857. ;;; Org-goto
  3858. (defvar org-goto-window-configuration nil)
  3859. (defvar org-goto-marker nil)
  3860. (defvar org-goto-map
  3861. (let ((map (make-sparse-keymap)))
  3862. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3863. (while (setq cmd (pop cmds))
  3864. (substitute-key-definition cmd cmd map global-map)))
  3865. (suppress-keymap map)
  3866. (org-defkey map "\C-m" 'org-goto-ret)
  3867. (org-defkey map [(return)] 'org-goto-ret)
  3868. (org-defkey map [(left)] 'org-goto-left)
  3869. (org-defkey map [(right)] 'org-goto-right)
  3870. (org-defkey map [(control ?g)] 'org-goto-quit)
  3871. (org-defkey map "\C-i" 'org-cycle)
  3872. (org-defkey map [(tab)] 'org-cycle)
  3873. (org-defkey map [(down)] 'outline-next-visible-heading)
  3874. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3875. (if org-goto-auto-isearch
  3876. (if (fboundp 'define-key-after)
  3877. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3878. nil)
  3879. (org-defkey map "q" 'org-goto-quit)
  3880. (org-defkey map "n" 'outline-next-visible-heading)
  3881. (org-defkey map "p" 'outline-previous-visible-heading)
  3882. (org-defkey map "f" 'outline-forward-same-level)
  3883. (org-defkey map "b" 'outline-backward-same-level)
  3884. (org-defkey map "u" 'outline-up-heading))
  3885. (org-defkey map "/" 'org-occur)
  3886. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3887. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3888. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3889. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3890. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3891. map))
  3892. (defconst org-goto-help
  3893. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3894. RET=jump to location [Q]uit and return to previous location
  3895. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3896. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3897. ;; FIXME: Docstring doe not mention both interfaces
  3898. (defun org-goto (&optional alternative-interface)
  3899. "Look up a different location in the current file, keeping current visibility.
  3900. When you want look-up or go to a different location in a document, the
  3901. fastest way is often to fold the entire buffer and then dive into the tree.
  3902. This method has the disadvantage, that the previous location will be folded,
  3903. which may not be what you want.
  3904. This command works around this by showing a copy of the current buffer
  3905. in an indirect buffer, in overview mode. You can dive into the tree in
  3906. that copy, use org-occur and incremental search to find a location.
  3907. When pressing RET or `Q', the command returns to the original buffer in
  3908. which the visibility is still unchanged. After RET is will also jump to
  3909. the location selected in the indirect buffer and expose the
  3910. the headline hierarchy above."
  3911. (interactive "P")
  3912. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3913. (org-refile-use-outline-path t)
  3914. (interface
  3915. (if (not alternative-interface)
  3916. org-goto-interface
  3917. (if (eq org-goto-interface 'outline)
  3918. 'outline-path-completion
  3919. 'outline)))
  3920. (org-goto-start-pos (point))
  3921. (selected-point
  3922. (if (eq interface 'outline)
  3923. (car (org-get-location (current-buffer) org-goto-help))
  3924. (nth 3 (org-refile-get-location "Goto: ")))))
  3925. (if selected-point
  3926. (progn
  3927. (org-mark-ring-push org-goto-start-pos)
  3928. (goto-char selected-point)
  3929. (if (or (org-invisible-p) (org-invisible-p2))
  3930. (org-show-context 'org-goto)))
  3931. (message "Quit"))))
  3932. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3933. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3934. (defvar org-goto-local-auto-isearch-map) ; defined below
  3935. (defun org-get-location (buf help)
  3936. "Let the user select a location in the Org-mode buffer BUF.
  3937. This function uses a recursive edit. It returns the selected position
  3938. or nil."
  3939. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3940. (isearch-hide-immediately nil)
  3941. (isearch-search-fun-function
  3942. (lambda () 'org-goto-local-search-headings))
  3943. (org-goto-selected-point org-goto-exit-command))
  3944. (save-excursion
  3945. (save-window-excursion
  3946. (delete-other-windows)
  3947. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3948. (switch-to-buffer
  3949. (condition-case nil
  3950. (make-indirect-buffer (current-buffer) "*org-goto*")
  3951. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3952. (with-output-to-temp-buffer "*Help*"
  3953. (princ help))
  3954. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3955. (setq buffer-read-only nil)
  3956. (let ((org-startup-truncated t)
  3957. (org-startup-folded nil)
  3958. (org-startup-align-all-tables nil))
  3959. (org-mode)
  3960. (org-overview))
  3961. (setq buffer-read-only t)
  3962. (if (and (boundp 'org-goto-start-pos)
  3963. (integer-or-marker-p org-goto-start-pos))
  3964. (let ((org-show-hierarchy-above t)
  3965. (org-show-siblings t)
  3966. (org-show-following-heading t))
  3967. (goto-char org-goto-start-pos)
  3968. (and (org-invisible-p) (org-show-context)))
  3969. (goto-char (point-min)))
  3970. (org-beginning-of-line)
  3971. (message "Select location and press RET")
  3972. (use-local-map org-goto-map)
  3973. (recursive-edit)
  3974. ))
  3975. (kill-buffer "*org-goto*")
  3976. (cons org-goto-selected-point org-goto-exit-command)))
  3977. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3978. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3979. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3980. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3981. (defun org-goto-local-search-headings (string bound noerror)
  3982. "Search and make sure that any matches are in headlines."
  3983. (catch 'return
  3984. (while (if isearch-forward
  3985. (search-forward string bound noerror)
  3986. (search-backward string bound noerror))
  3987. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3988. (and (member :headline context)
  3989. (not (member :tags context))))
  3990. (throw 'return (point))))))
  3991. (defun org-goto-local-auto-isearch ()
  3992. "Start isearch."
  3993. (interactive)
  3994. (goto-char (point-min))
  3995. (let ((keys (this-command-keys)))
  3996. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3997. (isearch-mode t)
  3998. (isearch-process-search-char (string-to-char keys)))))
  3999. (defun org-goto-ret (&optional arg)
  4000. "Finish `org-goto' by going to the new location."
  4001. (interactive "P")
  4002. (setq org-goto-selected-point (point)
  4003. org-goto-exit-command 'return)
  4004. (throw 'exit nil))
  4005. (defun org-goto-left ()
  4006. "Finish `org-goto' by going to the new location."
  4007. (interactive)
  4008. (if (org-on-heading-p)
  4009. (progn
  4010. (beginning-of-line 1)
  4011. (setq org-goto-selected-point (point)
  4012. org-goto-exit-command 'left)
  4013. (throw 'exit nil))
  4014. (error "Not on a heading")))
  4015. (defun org-goto-right ()
  4016. "Finish `org-goto' by going to the new location."
  4017. (interactive)
  4018. (if (org-on-heading-p)
  4019. (progn
  4020. (setq org-goto-selected-point (point)
  4021. org-goto-exit-command 'right)
  4022. (throw 'exit nil))
  4023. (error "Not on a heading")))
  4024. (defun org-goto-quit ()
  4025. "Finish `org-goto' without cursor motion."
  4026. (interactive)
  4027. (setq org-goto-selected-point nil)
  4028. (setq org-goto-exit-command 'quit)
  4029. (throw 'exit nil))
  4030. ;;; Indirect buffer display of subtrees
  4031. (defvar org-indirect-dedicated-frame nil
  4032. "This is the frame being used for indirect tree display.")
  4033. (defvar org-last-indirect-buffer nil)
  4034. (defun org-tree-to-indirect-buffer (&optional arg)
  4035. "Create indirect buffer and narrow it to current subtree.
  4036. With numerical prefix ARG, go up to this level and then take that tree.
  4037. If ARG is negative, go up that many levels.
  4038. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4039. indirect buffer previously made with this command, to avoid proliferation of
  4040. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4041. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4042. is kept so that you can work with several indirect buffers at the same time.
  4043. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4044. requests that a new frame be made for the new buffer, so that the dedicated
  4045. frame is not changed."
  4046. (interactive "P")
  4047. (let ((cbuf (current-buffer))
  4048. (cwin (selected-window))
  4049. (pos (point))
  4050. beg end level heading ibuf)
  4051. (save-excursion
  4052. (org-back-to-heading t)
  4053. (when (numberp arg)
  4054. (setq level (org-outline-level))
  4055. (if (< arg 0) (setq arg (+ level arg)))
  4056. (while (> (setq level (org-outline-level)) arg)
  4057. (outline-up-heading 1 t)))
  4058. (setq beg (point)
  4059. heading (org-get-heading))
  4060. (org-end-of-subtree t) (setq end (point)))
  4061. (if (and (buffer-live-p org-last-indirect-buffer)
  4062. (not (eq org-indirect-buffer-display 'new-frame))
  4063. (not arg))
  4064. (kill-buffer org-last-indirect-buffer))
  4065. (setq ibuf (org-get-indirect-buffer cbuf)
  4066. org-last-indirect-buffer ibuf)
  4067. (cond
  4068. ((or (eq org-indirect-buffer-display 'new-frame)
  4069. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4070. (select-frame (make-frame))
  4071. (delete-other-windows)
  4072. (switch-to-buffer ibuf)
  4073. (org-set-frame-title heading))
  4074. ((eq org-indirect-buffer-display 'dedicated-frame)
  4075. (raise-frame
  4076. (select-frame (or (and org-indirect-dedicated-frame
  4077. (frame-live-p org-indirect-dedicated-frame)
  4078. org-indirect-dedicated-frame)
  4079. (setq org-indirect-dedicated-frame (make-frame)))))
  4080. (delete-other-windows)
  4081. (switch-to-buffer ibuf)
  4082. (org-set-frame-title (concat "Indirect: " heading)))
  4083. ((eq org-indirect-buffer-display 'current-window)
  4084. (switch-to-buffer ibuf))
  4085. ((eq org-indirect-buffer-display 'other-window)
  4086. (pop-to-buffer ibuf))
  4087. (t (error "Invalid value.")))
  4088. (if (featurep 'xemacs)
  4089. (save-excursion (org-mode) (turn-on-font-lock)))
  4090. (narrow-to-region beg end)
  4091. (show-all)
  4092. (goto-char pos)
  4093. (and (window-live-p cwin) (select-window cwin))))
  4094. (defun org-get-indirect-buffer (&optional buffer)
  4095. (setq buffer (or buffer (current-buffer)))
  4096. (let ((n 1) (base (buffer-name buffer)) bname)
  4097. (while (buffer-live-p
  4098. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4099. (setq n (1+ n)))
  4100. (condition-case nil
  4101. (make-indirect-buffer buffer bname 'clone)
  4102. (error (make-indirect-buffer buffer bname)))))
  4103. (defun org-set-frame-title (title)
  4104. "Set the title of the current frame to the string TITLE."
  4105. ;; FIXME: how to name a single frame in XEmacs???
  4106. (unless (featurep 'xemacs)
  4107. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4108. ;;;; Structure editing
  4109. ;;; Inserting headlines
  4110. (defun org-insert-heading (&optional force-heading)
  4111. "Insert a new heading or item with same depth at point.
  4112. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4113. If point is at the beginning of a headline, insert a sibling before the
  4114. current headline. If point is not at the beginning, do not split the line,
  4115. but create the new hedline after the current line."
  4116. (interactive "P")
  4117. (if (= (buffer-size) 0)
  4118. (insert "\n* ")
  4119. (when (or force-heading (not (org-insert-item)))
  4120. (let* ((head (save-excursion
  4121. (condition-case nil
  4122. (progn
  4123. (org-back-to-heading)
  4124. (match-string 0))
  4125. (error "*"))))
  4126. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4127. pos)
  4128. (cond
  4129. ((and (org-on-heading-p) (bolp)
  4130. (or (bobp)
  4131. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4132. ;; insert before the current line
  4133. (open-line (if blank 2 1)))
  4134. ((and (bolp)
  4135. (or (bobp)
  4136. (save-excursion
  4137. (backward-char 1) (not (org-invisible-p)))))
  4138. ;; insert right here
  4139. nil)
  4140. (t
  4141. ;; in the middle of the line
  4142. (org-show-entry)
  4143. (let ((split
  4144. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4145. tags pos)
  4146. (cond
  4147. (org-insert-heading-respect-content
  4148. (org-end-of-subtree nil t)
  4149. (open-line 1))
  4150. ((org-on-heading-p)
  4151. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4152. (setq tags (and (match-end 2) (match-string 2)))
  4153. (and (match-end 1)
  4154. (delete-region (match-beginning 1) (match-end 1)))
  4155. (setq pos (point-at-bol))
  4156. (or split (end-of-line 1))
  4157. (delete-horizontal-space)
  4158. (newline (if blank 2 1))
  4159. (when tags
  4160. (save-excursion
  4161. (goto-char pos)
  4162. (end-of-line 1)
  4163. (insert " " tags)
  4164. (org-set-tags nil 'align))))
  4165. (t
  4166. (or split (end-of-line 1))
  4167. (newline (if blank 2 1)))))))
  4168. (insert head) (just-one-space)
  4169. (setq pos (point))
  4170. (end-of-line 1)
  4171. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4172. (run-hooks 'org-insert-heading-hook)))))
  4173. (defun org-get-heading (&optional no-tags)
  4174. "Return the heading of the current entry, without the stars."
  4175. (save-excursion
  4176. (org-back-to-heading t)
  4177. (if (looking-at
  4178. (if no-tags
  4179. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4180. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4181. (match-string 1) "")))
  4182. (defun org-insert-heading-after-current ()
  4183. "Insert a new heading with same level as current, after current subtree."
  4184. (interactive)
  4185. (org-back-to-heading)
  4186. (org-insert-heading)
  4187. (org-move-subtree-down)
  4188. (end-of-line 1))
  4189. (defun org-insert-heading-respect-content ()
  4190. (interactive)
  4191. (let ((org-insert-heading-respect-content t))
  4192. (call-interactively 'org-insert-heading)))
  4193. (defun org-insert-todo-heading-respect-content ()
  4194. (interactive)
  4195. (let ((org-insert-heading-respect-content t))
  4196. (call-interactively 'org-insert-todo-heading)))
  4197. (defun org-insert-todo-heading (arg)
  4198. "Insert a new heading with the same level and TODO state as current heading.
  4199. If the heading has no TODO state, or if the state is DONE, use the first
  4200. state (TODO by default). Also with prefix arg, force first state."
  4201. (interactive "P")
  4202. (when (not (org-insert-item 'checkbox))
  4203. (org-insert-heading)
  4204. (save-excursion
  4205. (org-back-to-heading)
  4206. (outline-previous-heading)
  4207. (looking-at org-todo-line-regexp))
  4208. (if (or arg
  4209. (not (match-beginning 2))
  4210. (member (match-string 2) org-done-keywords))
  4211. (insert (car org-todo-keywords-1) " ")
  4212. (insert (match-string 2) " "))
  4213. (when org-provide-todo-statistics
  4214. (org-update-parent-todo-statistics))))
  4215. (defun org-insert-subheading (arg)
  4216. "Insert a new subheading and demote it.
  4217. Works for outline headings and for plain lists alike."
  4218. (interactive "P")
  4219. (org-insert-heading arg)
  4220. (cond
  4221. ((org-on-heading-p) (org-do-demote))
  4222. ((org-at-item-p) (org-indent-item 1))))
  4223. (defun org-insert-todo-subheading (arg)
  4224. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4225. Works for outline headings and for plain lists alike."
  4226. (interactive "P")
  4227. (org-insert-todo-heading arg)
  4228. (cond
  4229. ((org-on-heading-p) (org-do-demote))
  4230. ((org-at-item-p) (org-indent-item 1))))
  4231. ;;; Promotion and Demotion
  4232. (defun org-promote-subtree ()
  4233. "Promote the entire subtree.
  4234. See also `org-promote'."
  4235. (interactive)
  4236. (save-excursion
  4237. (org-map-tree 'org-promote))
  4238. (org-fix-position-after-promote))
  4239. (defun org-demote-subtree ()
  4240. "Demote the entire subtree. See `org-demote'.
  4241. See also `org-promote'."
  4242. (interactive)
  4243. (save-excursion
  4244. (org-map-tree 'org-demote))
  4245. (org-fix-position-after-promote))
  4246. (defun org-do-promote ()
  4247. "Promote the current heading higher up the tree.
  4248. If the region is active in `transient-mark-mode', promote all headings
  4249. in the region."
  4250. (interactive)
  4251. (save-excursion
  4252. (if (org-region-active-p)
  4253. (org-map-region 'org-promote (region-beginning) (region-end))
  4254. (org-promote)))
  4255. (org-fix-position-after-promote))
  4256. (defun org-do-demote ()
  4257. "Demote the current heading lower down the tree.
  4258. If the region is active in `transient-mark-mode', demote all headings
  4259. in the region."
  4260. (interactive)
  4261. (save-excursion
  4262. (if (org-region-active-p)
  4263. (org-map-region 'org-demote (region-beginning) (region-end))
  4264. (org-demote)))
  4265. (org-fix-position-after-promote))
  4266. (defun org-fix-position-after-promote ()
  4267. "Make sure that after pro/demotion cursor position is right."
  4268. (let ((pos (point)))
  4269. (when (save-excursion
  4270. (beginning-of-line 1)
  4271. (looking-at org-todo-line-regexp)
  4272. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4273. (cond ((eobp) (insert " "))
  4274. ((eolp) (insert " "))
  4275. ((equal (char-after) ?\ ) (forward-char 1))))))
  4276. (defun org-reduced-level (l)
  4277. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4278. (defun org-get-valid-level (level &optional change)
  4279. "Rectify a level change under the influence of `org-odd-levels-only'
  4280. LEVEL is a current level, CHANGE is by how much the level should be
  4281. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4282. even level numbers will become the next higher odd number."
  4283. (if org-odd-levels-only
  4284. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4285. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4286. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4287. (max 1 (+ level change))))
  4288. (if (boundp 'define-obsolete-function-alias)
  4289. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4290. (define-obsolete-function-alias 'org-get-legal-level
  4291. 'org-get-valid-level)
  4292. (define-obsolete-function-alias 'org-get-legal-level
  4293. 'org-get-valid-level "23.1")))
  4294. (defun org-promote ()
  4295. "Promote the current heading higher up the tree.
  4296. If the region is active in `transient-mark-mode', promote all headings
  4297. in the region."
  4298. (org-back-to-heading t)
  4299. (let* ((level (save-match-data (funcall outline-level)))
  4300. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4301. (diff (abs (- level (length up-head) -1))))
  4302. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4303. (replace-match up-head nil t)
  4304. ;; Fixup tag positioning
  4305. (and org-auto-align-tags (org-set-tags nil t))
  4306. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4307. (defun org-demote ()
  4308. "Demote the current heading lower down the tree.
  4309. If the region is active in `transient-mark-mode', demote all headings
  4310. in the region."
  4311. (org-back-to-heading t)
  4312. (let* ((level (save-match-data (funcall outline-level)))
  4313. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4314. (diff (abs (- level (length down-head) -1))))
  4315. (replace-match down-head nil t)
  4316. ;; Fixup tag positioning
  4317. (and org-auto-align-tags (org-set-tags nil t))
  4318. (if org-adapt-indentation (org-fixup-indentation diff))))
  4319. (defun org-map-tree (fun)
  4320. "Call FUN for every heading underneath the current one."
  4321. (org-back-to-heading)
  4322. (let ((level (funcall outline-level)))
  4323. (save-excursion
  4324. (funcall fun)
  4325. (while (and (progn
  4326. (outline-next-heading)
  4327. (> (funcall outline-level) level))
  4328. (not (eobp)))
  4329. (funcall fun)))))
  4330. (defun org-map-region (fun beg end)
  4331. "Call FUN for every heading between BEG and END."
  4332. (let ((org-ignore-region t))
  4333. (save-excursion
  4334. (setq end (copy-marker end))
  4335. (goto-char beg)
  4336. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4337. (< (point) end))
  4338. (funcall fun))
  4339. (while (and (progn
  4340. (outline-next-heading)
  4341. (< (point) end))
  4342. (not (eobp)))
  4343. (funcall fun)))))
  4344. (defun org-fixup-indentation (diff)
  4345. "Change the indentation in the current entry by DIFF
  4346. However, if any line in the current entry has no indentation, or if it
  4347. would end up with no indentation after the change, nothing at all is done."
  4348. (save-excursion
  4349. (let ((end (save-excursion (outline-next-heading)
  4350. (point-marker)))
  4351. (prohibit (if (> diff 0)
  4352. "^\\S-"
  4353. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4354. col)
  4355. (unless (save-excursion (end-of-line 1)
  4356. (re-search-forward prohibit end t))
  4357. (while (and (< (point) end)
  4358. (re-search-forward "^[ \t]+" end t))
  4359. (goto-char (match-end 0))
  4360. (setq col (current-column))
  4361. (if (< diff 0) (replace-match ""))
  4362. (indent-to (+ diff col))))
  4363. (move-marker end nil))))
  4364. (defun org-convert-to-odd-levels ()
  4365. "Convert an org-mode file with all levels allowed to one with odd levels.
  4366. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4367. level 5 etc."
  4368. (interactive)
  4369. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4370. (let ((org-odd-levels-only nil) n)
  4371. (save-excursion
  4372. (goto-char (point-min))
  4373. (while (re-search-forward "^\\*\\*+ " nil t)
  4374. (setq n (- (length (match-string 0)) 2))
  4375. (while (>= (setq n (1- n)) 0)
  4376. (org-demote))
  4377. (end-of-line 1))))))
  4378. (defun org-convert-to-oddeven-levels ()
  4379. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4380. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4381. section with an even level, conversion would destroy the structure of the file. An error
  4382. is signaled in this case."
  4383. (interactive)
  4384. (goto-char (point-min))
  4385. ;; First check if there are no even levels
  4386. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4387. (org-show-context t)
  4388. (error "Not all levels are odd in this file. Conversion not possible."))
  4389. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4390. (let ((org-odd-levels-only nil) n)
  4391. (save-excursion
  4392. (goto-char (point-min))
  4393. (while (re-search-forward "^\\*\\*+ " nil t)
  4394. (setq n (/ (1- (length (match-string 0))) 2))
  4395. (while (>= (setq n (1- n)) 0)
  4396. (org-promote))
  4397. (end-of-line 1))))))
  4398. (defun org-tr-level (n)
  4399. "Make N odd if required."
  4400. (if org-odd-levels-only (1+ (/ n 2)) n))
  4401. ;;; Vertical tree motion, cutting and pasting of subtrees
  4402. (defun org-move-subtree-up (&optional arg)
  4403. "Move the current subtree up past ARG headlines of the same level."
  4404. (interactive "p")
  4405. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4406. (defun org-move-subtree-down (&optional arg)
  4407. "Move the current subtree down past ARG headlines of the same level."
  4408. (interactive "p")
  4409. (setq arg (prefix-numeric-value arg))
  4410. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4411. 'outline-get-last-sibling))
  4412. (ins-point (make-marker))
  4413. (cnt (abs arg))
  4414. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4415. ;; Select the tree
  4416. (org-back-to-heading)
  4417. (setq beg0 (point))
  4418. (save-excursion
  4419. (setq ne-beg (org-back-over-empty-lines))
  4420. (setq beg (point)))
  4421. (save-match-data
  4422. (save-excursion (outline-end-of-heading)
  4423. (setq folded (org-invisible-p)))
  4424. (outline-end-of-subtree))
  4425. (outline-next-heading)
  4426. (setq ne-end (org-back-over-empty-lines))
  4427. (setq end (point))
  4428. (goto-char beg0)
  4429. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4430. ;; include less whitespace
  4431. (save-excursion
  4432. (goto-char beg)
  4433. (forward-line (- ne-beg ne-end))
  4434. (setq beg (point))))
  4435. ;; Find insertion point, with error handling
  4436. (while (> cnt 0)
  4437. (or (and (funcall movfunc) (looking-at outline-regexp))
  4438. (progn (goto-char beg0)
  4439. (error "Cannot move past superior level or buffer limit")))
  4440. (setq cnt (1- cnt)))
  4441. (if (> arg 0)
  4442. ;; Moving forward - still need to move over subtree
  4443. (progn (org-end-of-subtree t t)
  4444. (save-excursion
  4445. (org-back-over-empty-lines)
  4446. (or (bolp) (newline)))))
  4447. (setq ne-ins (org-back-over-empty-lines))
  4448. (move-marker ins-point (point))
  4449. (setq txt (buffer-substring beg end))
  4450. (org-save-markers-in-region beg end)
  4451. (delete-region beg end)
  4452. (outline-flag-region (1- beg) beg nil)
  4453. (outline-flag-region (1- (point)) (point) nil)
  4454. (let ((bbb (point)))
  4455. (insert-before-markers txt)
  4456. (org-reinstall-markers-in-region bbb)
  4457. (move-marker ins-point bbb))
  4458. (or (bolp) (insert "\n"))
  4459. (setq ins-end (point))
  4460. (goto-char ins-point)
  4461. (org-skip-whitespace)
  4462. (when (and (< arg 0)
  4463. (org-first-sibling-p)
  4464. (> ne-ins ne-beg))
  4465. ;; Move whitespace back to beginning
  4466. (save-excursion
  4467. (goto-char ins-end)
  4468. (let ((kill-whole-line t))
  4469. (kill-line (- ne-ins ne-beg)) (point)))
  4470. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4471. (move-marker ins-point nil)
  4472. (org-compact-display-after-subtree-move)
  4473. (org-show-empty-lines-in-parent)
  4474. (unless folded
  4475. (org-show-entry)
  4476. (show-children)
  4477. (org-cycle-hide-drawers 'children))))
  4478. (defvar org-subtree-clip ""
  4479. "Clipboard for cut and paste of subtrees.
  4480. This is actually only a copy of the kill, because we use the normal kill
  4481. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4482. (defvar org-subtree-clip-folded nil
  4483. "Was the last copied subtree folded?
  4484. This is used to fold the tree back after pasting.")
  4485. (defun org-cut-subtree (&optional n)
  4486. "Cut the current subtree into the clipboard.
  4487. With prefix arg N, cut this many sequential subtrees.
  4488. This is a short-hand for marking the subtree and then cutting it."
  4489. (interactive "p")
  4490. (org-copy-subtree n 'cut))
  4491. (defun org-copy-subtree (&optional n cut force-store-markers)
  4492. "Cut the current subtree into the clipboard.
  4493. With prefix arg N, cut this many sequential subtrees.
  4494. This is a short-hand for marking the subtree and then copying it.
  4495. If CUT is non-nil, actually cut the subtree.
  4496. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4497. of some markers in the region, even if CUT is non-nil. This is
  4498. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4499. (interactive "p")
  4500. (let (beg end folded (beg0 (point)))
  4501. (if (interactive-p)
  4502. (org-back-to-heading nil) ; take what looks like a subtree
  4503. (org-back-to-heading t)) ; take what is really there
  4504. (org-back-over-empty-lines)
  4505. (setq beg (point))
  4506. (skip-chars-forward " \t\r\n")
  4507. (save-match-data
  4508. (save-excursion (outline-end-of-heading)
  4509. (setq folded (org-invisible-p)))
  4510. (condition-case nil
  4511. (outline-forward-same-level (1- n))
  4512. (error nil))
  4513. (org-end-of-subtree t t))
  4514. (org-back-over-empty-lines)
  4515. (setq end (point))
  4516. (goto-char beg0)
  4517. (when (> end beg)
  4518. (setq org-subtree-clip-folded folded)
  4519. (when (or cut force-store-markers)
  4520. (org-save-markers-in-region beg end))
  4521. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4522. (setq org-subtree-clip (current-kill 0))
  4523. (message "%s: Subtree(s) with %d characters"
  4524. (if cut "Cut" "Copied")
  4525. (length org-subtree-clip)))))
  4526. (defun org-paste-subtree (&optional level tree)
  4527. "Paste the clipboard as a subtree, with modification of headline level.
  4528. The entire subtree is promoted or demoted in order to match a new headline
  4529. level. By default, the new level is derived from the visible headings
  4530. before and after the insertion point, and taken to be the inferior headline
  4531. level of the two. So if the previous visible heading is level 3 and the
  4532. next is level 4 (or vice versa), level 4 will be used for insertion.
  4533. This makes sure that the subtree remains an independent subtree and does
  4534. not swallow low level entries.
  4535. You can also force a different level, either by using a numeric prefix
  4536. argument, or by inserting the heading marker by hand. For example, if the
  4537. cursor is after \"*****\", then the tree will be shifted to level 5.
  4538. If you want to insert the tree as is, just use \\[yank].
  4539. If optional TREE is given, use this text instead of the kill ring."
  4540. (interactive "P")
  4541. (unless (org-kill-is-subtree-p tree)
  4542. (error "%s"
  4543. (substitute-command-keys
  4544. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4545. (let* ((visp (not (org-invisible-p)))
  4546. (txt (or tree (and kill-ring (current-kill 0))))
  4547. (^re (concat "^\\(" outline-regexp "\\)"))
  4548. (re (concat "\\(" outline-regexp "\\)"))
  4549. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4550. (old-level (if (string-match ^re txt)
  4551. (- (match-end 0) (match-beginning 0) 1)
  4552. -1))
  4553. (force-level (cond (level (prefix-numeric-value level))
  4554. ((string-match
  4555. ^re_ (buffer-substring (point-at-bol) (point)))
  4556. (- (match-end 1) (match-beginning 1)))
  4557. (t nil)))
  4558. (previous-level (save-excursion
  4559. (condition-case nil
  4560. (progn
  4561. (outline-previous-visible-heading 1)
  4562. (if (looking-at re)
  4563. (- (match-end 0) (match-beginning 0) 1)
  4564. 1))
  4565. (error 1))))
  4566. (next-level (save-excursion
  4567. (condition-case nil
  4568. (progn
  4569. (or (looking-at outline-regexp)
  4570. (outline-next-visible-heading 1))
  4571. (if (looking-at re)
  4572. (- (match-end 0) (match-beginning 0) 1)
  4573. 1))
  4574. (error 1))))
  4575. (new-level (or force-level (max previous-level next-level)))
  4576. (shift (if (or (= old-level -1)
  4577. (= new-level -1)
  4578. (= old-level new-level))
  4579. 0
  4580. (- new-level old-level)))
  4581. (delta (if (> shift 0) -1 1))
  4582. (func (if (> shift 0) 'org-demote 'org-promote))
  4583. (org-odd-levels-only nil)
  4584. beg end)
  4585. ;; Remove the forced level indicator
  4586. (if force-level
  4587. (delete-region (point-at-bol) (point)))
  4588. ;; Paste
  4589. (beginning-of-line 1)
  4590. (org-back-over-empty-lines)
  4591. (setq beg (point))
  4592. (insert-before-markers txt)
  4593. (unless (string-match "\n\\'" txt) (insert "\n"))
  4594. (org-reinstall-markers-in-region beg)
  4595. (setq end (point))
  4596. (goto-char beg)
  4597. (skip-chars-forward " \t\n\r")
  4598. (setq beg (point))
  4599. (if (and (org-invisible-p) visp)
  4600. (save-excursion (outline-show-heading)))
  4601. ;; Shift if necessary
  4602. (unless (= shift 0)
  4603. (save-restriction
  4604. (narrow-to-region beg end)
  4605. (while (not (= shift 0))
  4606. (org-map-region func (point-min) (point-max))
  4607. (setq shift (+ delta shift)))
  4608. (goto-char (point-min))))
  4609. (when (interactive-p)
  4610. (message "Clipboard pasted as level %d subtree" new-level))
  4611. (if (and kill-ring
  4612. (eq org-subtree-clip (current-kill 0))
  4613. org-subtree-clip-folded)
  4614. ;; The tree was folded before it was killed/copied
  4615. (hide-subtree))))
  4616. (defun org-kill-is-subtree-p (&optional txt)
  4617. "Check if the current kill is an outline subtree, or a set of trees.
  4618. Returns nil if kill does not start with a headline, or if the first
  4619. headline level is not the largest headline level in the tree.
  4620. So this will actually accept several entries of equal levels as well,
  4621. which is OK for `org-paste-subtree'.
  4622. If optional TXT is given, check this string instead of the current kill."
  4623. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4624. (start-level (and kill
  4625. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4626. org-outline-regexp "\\)")
  4627. kill)
  4628. (- (match-end 2) (match-beginning 2) 1)))
  4629. (re (concat "^" org-outline-regexp))
  4630. (start (1+ (or (match-beginning 2) -1))))
  4631. (if (not start-level)
  4632. (progn
  4633. nil) ;; does not even start with a heading
  4634. (catch 'exit
  4635. (while (setq start (string-match re kill (1+ start)))
  4636. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4637. (throw 'exit nil)))
  4638. t))))
  4639. (defvar org-markers-to-move nil
  4640. "Markers that should be moved with a cut-and-paste operation.
  4641. Those markers are stored together with their positions relative to
  4642. the start of the region.")
  4643. (defun org-save-markers-in-region (beg end)
  4644. "Check markers in region.
  4645. If these markers are between BEG and END, record their position relative
  4646. to BEG, so that after moving the block of text, we can put the markers back
  4647. into place.
  4648. This function gets called just before an entry or tree gets cut from the
  4649. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4650. called immediately, to move the markers with the entries."
  4651. (setq org-markers-to-move nil)
  4652. (when (featurep 'org-clock)
  4653. (org-clock-save-markers-for-cut-and-paste beg end))
  4654. (when (featurep 'org-agenda)
  4655. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4656. (defun org-check-and-save-marker (marker beg end)
  4657. "Check if MARKER is between BEG and END.
  4658. If yes, remember the marker and the distance to BEG."
  4659. (when (and (marker-buffer marker)
  4660. (equal (marker-buffer marker) (current-buffer)))
  4661. (if (and (>= marker beg) (< marker end))
  4662. (push (cons marker (- marker beg)) org-markers-to-move))))
  4663. (defun org-reinstall-markers-in-region (beg)
  4664. "Move all remembered markers to their position relative to BEG."
  4665. (mapc (lambda (x)
  4666. (move-marker (car x) (+ beg (cdr x))))
  4667. org-markers-to-move)
  4668. (setq org-markers-to-move nil))
  4669. (defun org-narrow-to-subtree ()
  4670. "Narrow buffer to the current subtree."
  4671. (interactive)
  4672. (save-excursion
  4673. (save-match-data
  4674. (narrow-to-region
  4675. (progn (org-back-to-heading) (point))
  4676. (progn (org-end-of-subtree t) (point))))))
  4677. ;;; Outline Sorting
  4678. (defun org-sort (with-case)
  4679. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4680. Optional argument WITH-CASE means sort case-sensitively."
  4681. (interactive "P")
  4682. (if (org-at-table-p)
  4683. (org-call-with-arg 'org-table-sort-lines with-case)
  4684. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4685. (defun org-sort-remove-invisible (s)
  4686. (remove-text-properties 0 (length s) org-rm-props s)
  4687. (while (string-match org-bracket-link-regexp s)
  4688. (setq s (replace-match (if (match-end 2)
  4689. (match-string 3 s)
  4690. (match-string 1 s)) t t s)))
  4691. s)
  4692. (defvar org-priority-regexp) ; defined later in the file
  4693. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4694. "Sort entries on a certain level of an outline tree.
  4695. If there is an active region, the entries in the region are sorted.
  4696. Else, if the cursor is before the first entry, sort the top-level items.
  4697. Else, the children of the entry at point are sorted.
  4698. Sorting can be alphabetically, numerically, and by date/time as given by
  4699. the first time stamp in the entry. The command prompts for the sorting
  4700. type unless it has been given to the function through the SORTING-TYPE
  4701. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4702. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4703. called with point at the beginning of the record. It must return either
  4704. a string or a number that should serve as the sorting key for that record.
  4705. Comparing entries ignores case by default. However, with an optional argument
  4706. WITH-CASE, the sorting considers case as well."
  4707. (interactive "P")
  4708. (let ((case-func (if with-case 'identity 'downcase))
  4709. start beg end stars re re2
  4710. txt what tmp plain-list-p)
  4711. ;; Find beginning and end of region to sort
  4712. (cond
  4713. ((org-region-active-p)
  4714. ;; we will sort the region
  4715. (setq end (region-end)
  4716. what "region")
  4717. (goto-char (region-beginning))
  4718. (if (not (org-on-heading-p)) (outline-next-heading))
  4719. (setq start (point)))
  4720. ((org-at-item-p)
  4721. ;; we will sort this plain list
  4722. (org-beginning-of-item-list) (setq start (point))
  4723. (org-end-of-item-list) (setq end (point))
  4724. (goto-char start)
  4725. (setq plain-list-p t
  4726. what "plain list"))
  4727. ((or (org-on-heading-p)
  4728. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4729. ;; we will sort the children of the current headline
  4730. (org-back-to-heading)
  4731. (setq start (point)
  4732. end (progn (org-end-of-subtree t t)
  4733. (org-back-over-empty-lines)
  4734. (point))
  4735. what "children")
  4736. (goto-char start)
  4737. (show-subtree)
  4738. (outline-next-heading))
  4739. (t
  4740. ;; we will sort the top-level entries in this file
  4741. (goto-char (point-min))
  4742. (or (org-on-heading-p) (outline-next-heading))
  4743. (setq start (point) end (point-max) what "top-level")
  4744. (goto-char start)
  4745. (show-all)))
  4746. (setq beg (point))
  4747. (if (>= beg end) (error "Nothing to sort"))
  4748. (unless plain-list-p
  4749. (looking-at "\\(\\*+\\)")
  4750. (setq stars (match-string 1)
  4751. re (concat "^" (regexp-quote stars) " +")
  4752. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4753. txt (buffer-substring beg end))
  4754. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4755. (if (and (not (equal stars "*")) (string-match re2 txt))
  4756. (error "Region to sort contains a level above the first entry")))
  4757. (unless sorting-type
  4758. (message
  4759. (if plain-list-p
  4760. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4761. "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:")
  4762. what)
  4763. (setq sorting-type (read-char-exclusive))
  4764. (and (= (downcase sorting-type) ?f)
  4765. (setq getkey-func
  4766. (completing-read "Sort using function: "
  4767. obarray 'fboundp t nil nil))
  4768. (setq getkey-func (intern getkey-func)))
  4769. (and (= (downcase sorting-type) ?r)
  4770. (setq property
  4771. (completing-read "Property: "
  4772. (mapcar 'list (org-buffer-property-keys t))
  4773. nil t))))
  4774. (message "Sorting entries...")
  4775. (save-restriction
  4776. (narrow-to-region start end)
  4777. (let ((dcst (downcase sorting-type))
  4778. (now (current-time)))
  4779. (sort-subr
  4780. (/= dcst sorting-type)
  4781. ;; This function moves to the beginning character of the "record" to
  4782. ;; be sorted.
  4783. (if plain-list-p
  4784. (lambda nil
  4785. (if (org-at-item-p) t (goto-char (point-max))))
  4786. (lambda nil
  4787. (if (re-search-forward re nil t)
  4788. (goto-char (match-beginning 0))
  4789. (goto-char (point-max)))))
  4790. ;; This function moves to the last character of the "record" being
  4791. ;; sorted.
  4792. (if plain-list-p
  4793. 'org-end-of-item
  4794. (lambda nil
  4795. (save-match-data
  4796. (condition-case nil
  4797. (outline-forward-same-level 1)
  4798. (error
  4799. (goto-char (point-max)))))))
  4800. ;; This function returns the value that gets sorted against.
  4801. (if plain-list-p
  4802. (lambda nil
  4803. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4804. (cond
  4805. ((= dcst ?n)
  4806. (string-to-number (buffer-substring (match-end 0)
  4807. (point-at-eol))))
  4808. ((= dcst ?a)
  4809. (buffer-substring (match-end 0) (point-at-eol)))
  4810. ((= dcst ?t)
  4811. (if (re-search-forward org-ts-regexp
  4812. (point-at-eol) t)
  4813. (org-time-string-to-time (match-string 0))
  4814. now))
  4815. ((= dcst ?f)
  4816. (if getkey-func
  4817. (progn
  4818. (setq tmp (funcall getkey-func))
  4819. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4820. tmp)
  4821. (error "Invalid key function `%s'" getkey-func)))
  4822. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4823. (lambda nil
  4824. (cond
  4825. ((= dcst ?n)
  4826. (if (looking-at org-complex-heading-regexp)
  4827. (string-to-number (match-string 4))
  4828. nil))
  4829. ((= dcst ?a)
  4830. (if (looking-at org-complex-heading-regexp)
  4831. (funcall case-func (match-string 4))
  4832. nil))
  4833. ((= dcst ?t)
  4834. (if (re-search-forward org-ts-regexp
  4835. (save-excursion
  4836. (forward-line 2)
  4837. (point)) t)
  4838. (org-time-string-to-time (match-string 0))
  4839. now))
  4840. ((= dcst ?p)
  4841. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4842. (string-to-char (match-string 2))
  4843. org-default-priority))
  4844. ((= dcst ?r)
  4845. (or (org-entry-get nil property) ""))
  4846. ((= dcst ?o)
  4847. (if (looking-at org-complex-heading-regexp)
  4848. (- 9999 (length (member (match-string 2)
  4849. org-todo-keywords-1)))))
  4850. ((= dcst ?f)
  4851. (if getkey-func
  4852. (progn
  4853. (setq tmp (funcall getkey-func))
  4854. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4855. tmp)
  4856. (error "Invalid key function `%s'" getkey-func)))
  4857. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4858. nil
  4859. (cond
  4860. ((= dcst ?a) 'string<)
  4861. ((= dcst ?t) 'time-less-p)
  4862. (t nil)))))
  4863. (message "Sorting entries...done")))
  4864. (defun org-do-sort (table what &optional with-case sorting-type)
  4865. "Sort TABLE of WHAT according to SORTING-TYPE.
  4866. The user will be prompted for the SORTING-TYPE if the call to this
  4867. function does not specify it. WHAT is only for the prompt, to indicate
  4868. what is being sorted. The sorting key will be extracted from
  4869. the car of the elements of the table.
  4870. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4871. (unless sorting-type
  4872. (message
  4873. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4874. what)
  4875. (setq sorting-type (read-char-exclusive)))
  4876. (let ((dcst (downcase sorting-type))
  4877. extractfun comparefun)
  4878. ;; Define the appropriate functions
  4879. (cond
  4880. ((= dcst ?n)
  4881. (setq extractfun 'string-to-number
  4882. comparefun (if (= dcst sorting-type) '< '>)))
  4883. ((= dcst ?a)
  4884. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4885. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4886. comparefun (if (= dcst sorting-type)
  4887. 'string<
  4888. (lambda (a b) (and (not (string< a b))
  4889. (not (string= a b)))))))
  4890. ((= dcst ?t)
  4891. (setq extractfun
  4892. (lambda (x)
  4893. (if (string-match org-ts-regexp x)
  4894. (time-to-seconds
  4895. (org-time-string-to-time (match-string 0 x)))
  4896. 0))
  4897. comparefun (if (= dcst sorting-type) '< '>)))
  4898. (t (error "Invalid sorting type `%c'" sorting-type)))
  4899. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4900. table)
  4901. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4902. ;;; Editing source examples
  4903. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4904. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4905. (defvar org-edit-src-force-single-line nil)
  4906. (defvar org-edit-src-from-org-mode nil)
  4907. (defvar org-edit-src-picture nil)
  4908. (define-minor-mode org-exit-edit-mode
  4909. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4910. (defun org-edit-src-code ()
  4911. "Edit the source code example at point.
  4912. An indirect buffer is created, and that buffer is then narrowed to the
  4913. example at point and switched to the correct language mode. When done,
  4914. exit by killing the buffer with \\[org-edit-src-exit]."
  4915. (interactive)
  4916. (let ((line (org-current-line))
  4917. (case-fold-search t)
  4918. (msg (substitute-command-keys
  4919. "Edit, then exit with C-c ' (C-c and single quote)"))
  4920. (info (org-edit-src-find-region-and-lang))
  4921. (org-mode-p (eq major-mode 'org-mode))
  4922. beg end lang lang-f single)
  4923. (if (not info)
  4924. nil
  4925. (setq beg (nth 0 info)
  4926. end (nth 1 info)
  4927. lang (nth 2 info)
  4928. single (nth 3 info)
  4929. lang-f (intern (concat lang "-mode")))
  4930. (unless (functionp lang-f)
  4931. (error "No such language mode: %s" lang-f))
  4932. (goto-line line)
  4933. (if (get-buffer "*Org Edit Src Example*")
  4934. (kill-buffer "*Org Edit Src Example*"))
  4935. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4936. "*Org Edit Src Example*"))
  4937. (narrow-to-region beg end)
  4938. (remove-text-properties beg end '(display nil invisible nil
  4939. intangible nil))
  4940. (let ((org-inhibit-startup t))
  4941. (funcall lang-f))
  4942. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4943. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4944. (when org-mode-p
  4945. (goto-char (point-min))
  4946. (while (re-search-forward "^," nil t)
  4947. (replace-match "")))
  4948. (goto-line line)
  4949. (org-exit-edit-mode)
  4950. (org-set-local 'header-line-format msg)
  4951. (message "%s" msg)
  4952. t)))
  4953. (defun org-edit-fixed-width-region ()
  4954. "Edit the fixed-width ascii drawing at point.
  4955. This must be a region where each line starts with ca colon followed by
  4956. a space character.
  4957. An indirect buffer is created, and that buffer is then narrowed to the
  4958. example at point and switched to artist-mode. When done,
  4959. exit by killing the buffer with \\[org-edit-src-exit]."
  4960. (interactive)
  4961. (let ((line (org-current-line))
  4962. (case-fold-search t)
  4963. (msg (substitute-command-keys
  4964. "Edit, then exit with C-c ' (C-c and single quote)"))
  4965. (org-mode-p (eq major-mode 'org-mode))
  4966. beg end lang lang-f)
  4967. (beginning-of-line 1)
  4968. (if (looking-at "[ \t]*[^:\n \t]")
  4969. nil
  4970. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  4971. (setq beg (point) end (match-end 0))
  4972. (save-excursion
  4973. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  4974. (setq beg (point-at-bol 2))
  4975. (setq beg (point))))
  4976. (save-excursion
  4977. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  4978. (setq end (1- (match-beginning 0)))
  4979. (setq end (point))))
  4980. (goto-line line)
  4981. (if (get-buffer "*Org Edit Picture*")
  4982. (kill-buffer "*Org Edit Picture*"))
  4983. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4984. "*Org Edit Picture*"))
  4985. (narrow-to-region beg end)
  4986. (remove-text-properties beg end '(display nil invisible nil
  4987. intangible nil))
  4988. (when (fboundp 'font-lock-unfontify-region)
  4989. (font-lock-unfontify-region (point-min) (point-max)))
  4990. (cond
  4991. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  4992. (fundamental-mode)
  4993. (artist-mode 1))
  4994. (t (funcall org-edit-fixed-width-region-mode)))
  4995. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  4996. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4997. (set (make-local-variable 'org-edit-src-picture) t)
  4998. (goto-char (point-min))
  4999. (while (re-search-forward "^[ \t]*: " nil t)
  5000. (replace-match ""))
  5001. (goto-line line)
  5002. (org-exit-edit-mode)
  5003. (org-set-local 'header-line-format msg)
  5004. (message "%s" msg)
  5005. t))))
  5006. (defun org-edit-src-find-region-and-lang ()
  5007. "Find the region and language for a local edit.
  5008. Return a list with beginning and end of the region, a string representing
  5009. the language, a switch telling of the content should be in a single line."
  5010. (let ((re-list
  5011. (append
  5012. org-edit-src-region-extra
  5013. '(
  5014. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5015. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5016. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5017. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5018. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5019. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5020. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5021. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5022. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5023. ("^#\\+html:" "\n" "html" single-line)
  5024. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5025. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5026. ("^#\\+latex:" "\n" "latex" single-line)
  5027. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5028. ("^#\\+ascii:" "\n" "ascii" single-line)
  5029. )))
  5030. (pos (point))
  5031. re re1 re2 single beg end lang)
  5032. (catch 'exit
  5033. (while (setq entry (pop re-list))
  5034. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5035. single (nth 3 entry))
  5036. (save-excursion
  5037. (if (or (looking-at re1)
  5038. (re-search-backward re1 nil t))
  5039. (progn
  5040. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5041. (if (and (re-search-forward re2 nil t)
  5042. (>= (match-end 0) pos))
  5043. (throw 'exit (list beg (match-beginning 0) lang single))))
  5044. (if (or (looking-at re2)
  5045. (re-search-forward re2 nil t))
  5046. (progn
  5047. (setq end (match-beginning 0))
  5048. (if (and (re-search-backward re1 nil t)
  5049. (<= (match-beginning 0) pos))
  5050. (throw 'exit
  5051. (list (match-end 0) end
  5052. (org-edit-src-get-lang lang) single)))))))))))
  5053. (defun org-edit-src-get-lang (lang)
  5054. "Extract the src language."
  5055. (let ((m (match-string 0)))
  5056. (cond
  5057. ((stringp lang) lang)
  5058. ((integerp lang) (match-string lang))
  5059. ((and (eq lang 'lang)
  5060. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5061. (match-string 1 m))
  5062. ((and (eq lang 'style)
  5063. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5064. (match-string 1 m))
  5065. (t "fundamental"))))
  5066. (defun org-edit-src-exit ()
  5067. "Exit special edit and protect problematic lines."
  5068. (interactive)
  5069. (unless (buffer-base-buffer (current-buffer))
  5070. (error "This is not an indirect buffer, something is wrong..."))
  5071. (unless (> (point-min) 1)
  5072. (error "This buffer is not narrowed, something is wrong..."))
  5073. (goto-char (point-min))
  5074. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5075. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5076. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5077. (goto-char (point-min))
  5078. (while (re-search-forward "\n" nil t)
  5079. (replace-match " "))
  5080. (goto-char (point-min))
  5081. (if (looking-at "\\s-*") (replace-match " "))
  5082. (if (re-search-forward "\\s-+\\'" nil t)
  5083. (replace-match "")))
  5084. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5085. (goto-char (point-min))
  5086. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5087. (replace-match ",\\1"))
  5088. (when font-lock-mode
  5089. (font-lock-unfontify-region (point-min) (point-max)))
  5090. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5091. (when (org-bound-and-true-p org-edit-src-picture)
  5092. (goto-char (point-min))
  5093. (while (re-search-forward "^" nil t)
  5094. (replace-match ": "))
  5095. (when font-lock-mode
  5096. (font-lock-unfontify-region (point-min) (point-max)))
  5097. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5098. (kill-buffer (current-buffer))
  5099. (and (org-mode-p) (org-restart-font-lock)))
  5100. ;;; The orgstruct minor mode
  5101. ;; Define a minor mode which can be used in other modes in order to
  5102. ;; integrate the org-mode structure editing commands.
  5103. ;; This is really a hack, because the org-mode structure commands use
  5104. ;; keys which normally belong to the major mode. Here is how it
  5105. ;; works: The minor mode defines all the keys necessary to operate the
  5106. ;; structure commands, but wraps the commands into a function which
  5107. ;; tests if the cursor is currently at a headline or a plain list
  5108. ;; item. If that is the case, the structure command is used,
  5109. ;; temporarily setting many Org-mode variables like regular
  5110. ;; expressions for filling etc. However, when any of those keys is
  5111. ;; used at a different location, function uses `key-binding' to look
  5112. ;; up if the key has an associated command in another currently active
  5113. ;; keymap (minor modes, major mode, global), and executes that
  5114. ;; command. There might be problems if any of the keys is otherwise
  5115. ;; used as a prefix key.
  5116. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5117. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5118. ;; addresses this by checking explicitly for both bindings.
  5119. (defvar orgstruct-mode-map (make-sparse-keymap)
  5120. "Keymap for the minor `orgstruct-mode'.")
  5121. (defvar org-local-vars nil
  5122. "List of local variables, for use by `orgstruct-mode'")
  5123. ;;;###autoload
  5124. (define-minor-mode orgstruct-mode
  5125. "Toggle the minor more `orgstruct-mode'.
  5126. This mode is for using Org-mode structure commands in other modes.
  5127. The following key behave as if Org-mode was active, if the cursor
  5128. is on a headline, or on a plain list item (both in the definition
  5129. of Org-mode).
  5130. M-up Move entry/item up
  5131. M-down Move entry/item down
  5132. M-left Promote
  5133. M-right Demote
  5134. M-S-up Move entry/item up
  5135. M-S-down Move entry/item down
  5136. M-S-left Promote subtree
  5137. M-S-right Demote subtree
  5138. M-q Fill paragraph and items like in Org-mode
  5139. C-c ^ Sort entries
  5140. C-c - Cycle list bullet
  5141. TAB Cycle item visibility
  5142. M-RET Insert new heading/item
  5143. S-M-RET Insert new TODO heading / Chekbox item
  5144. C-c C-c Set tags / toggle checkbox"
  5145. nil " OrgStruct" nil
  5146. (org-load-modules-maybe)
  5147. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5148. ;;;###autoload
  5149. (defun turn-on-orgstruct ()
  5150. "Unconditionally turn on `orgstruct-mode'."
  5151. (orgstruct-mode 1))
  5152. ;;;###autoload
  5153. (defun turn-on-orgstruct++ ()
  5154. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5155. In addition to setting orgstruct-mode, this also exports all indentation and
  5156. autofilling variables from org-mode into the buffer. Note that turning
  5157. off orgstruct-mode will *not* remove these additional settings."
  5158. (orgstruct-mode 1)
  5159. (let (var val)
  5160. (mapc
  5161. (lambda (x)
  5162. (when (string-match
  5163. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5164. (symbol-name (car x)))
  5165. (setq var (car x) val (nth 1 x))
  5166. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5167. org-local-vars)))
  5168. (defun orgstruct-error ()
  5169. "Error when there is no default binding for a structure key."
  5170. (interactive)
  5171. (error "This key has no function outside structure elements"))
  5172. (defun orgstruct-setup ()
  5173. "Setup orgstruct keymaps."
  5174. (let ((nfunc 0)
  5175. (bindings
  5176. (list
  5177. '([(meta up)] org-metaup)
  5178. '([(meta down)] org-metadown)
  5179. '([(meta left)] org-metaleft)
  5180. '([(meta right)] org-metaright)
  5181. '([(meta shift up)] org-shiftmetaup)
  5182. '([(meta shift down)] org-shiftmetadown)
  5183. '([(meta shift left)] org-shiftmetaleft)
  5184. '([(meta shift right)] org-shiftmetaright)
  5185. '([(shift up)] org-shiftup)
  5186. '([(shift down)] org-shiftdown)
  5187. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5188. '("\M-q" fill-paragraph)
  5189. '("\C-c^" org-sort)
  5190. '("\C-c-" org-cycle-list-bullet)))
  5191. elt key fun cmd)
  5192. (while (setq elt (pop bindings))
  5193. (setq nfunc (1+ nfunc))
  5194. (setq key (org-key (car elt))
  5195. fun (nth 1 elt)
  5196. cmd (orgstruct-make-binding fun nfunc key))
  5197. (org-defkey orgstruct-mode-map key cmd))
  5198. ;; Special treatment needed for TAB and RET
  5199. (org-defkey orgstruct-mode-map [(tab)]
  5200. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5201. (org-defkey orgstruct-mode-map "\C-i"
  5202. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5203. (org-defkey orgstruct-mode-map "\M-\C-m"
  5204. (orgstruct-make-binding 'org-insert-heading 105
  5205. "\M-\C-m" [(meta return)]))
  5206. (org-defkey orgstruct-mode-map [(meta return)]
  5207. (orgstruct-make-binding 'org-insert-heading 106
  5208. [(meta return)] "\M-\C-m"))
  5209. (org-defkey orgstruct-mode-map [(shift meta return)]
  5210. (orgstruct-make-binding 'org-insert-todo-heading 107
  5211. [(meta return)] "\M-\C-m"))
  5212. (unless org-local-vars
  5213. (setq org-local-vars (org-get-local-variables)))
  5214. t))
  5215. (defun orgstruct-make-binding (fun n &rest keys)
  5216. "Create a function for binding in the structure minor mode.
  5217. FUN is the command to call inside a table. N is used to create a unique
  5218. command name. KEYS are keys that should be checked in for a command
  5219. to execute outside of tables."
  5220. (eval
  5221. (list 'defun
  5222. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5223. '(arg)
  5224. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5225. "Outside of structure, run the binding of `"
  5226. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5227. "'.")
  5228. '(interactive "p")
  5229. (list 'if
  5230. '(org-context-p 'headline 'item)
  5231. (list 'org-run-like-in-org-mode (list 'quote fun))
  5232. (list 'let '(orgstruct-mode)
  5233. (list 'call-interactively
  5234. (append '(or)
  5235. (mapcar (lambda (k)
  5236. (list 'key-binding k))
  5237. keys)
  5238. '('orgstruct-error))))))))
  5239. (defun org-context-p (&rest contexts)
  5240. "Check if local context is any of CONTEXTS.
  5241. Possible values in the list of contexts are `table', `headline', and `item'."
  5242. (let ((pos (point)))
  5243. (goto-char (point-at-bol))
  5244. (prog1 (or (and (memq 'table contexts)
  5245. (looking-at "[ \t]*|"))
  5246. (and (memq 'headline contexts)
  5247. ;;????????? (looking-at "\\*+"))
  5248. (looking-at outline-regexp))
  5249. (and (memq 'item contexts)
  5250. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5251. (goto-char pos))))
  5252. (defun org-get-local-variables ()
  5253. "Return a list of all local variables in an org-mode buffer."
  5254. (let (varlist)
  5255. (with-current-buffer (get-buffer-create "*Org tmp*")
  5256. (erase-buffer)
  5257. (org-mode)
  5258. (setq varlist (buffer-local-variables)))
  5259. (kill-buffer "*Org tmp*")
  5260. (delq nil
  5261. (mapcar
  5262. (lambda (x)
  5263. (setq x
  5264. (if (symbolp x)
  5265. (list x)
  5266. (list (car x) (list 'quote (cdr x)))))
  5267. (if (string-match
  5268. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5269. (symbol-name (car x)))
  5270. x nil))
  5271. varlist))))
  5272. ;;;###autoload
  5273. (defun org-run-like-in-org-mode (cmd)
  5274. (org-load-modules-maybe)
  5275. (unless org-local-vars
  5276. (setq org-local-vars (org-get-local-variables)))
  5277. (eval (list 'let org-local-vars
  5278. (list 'call-interactively (list 'quote cmd)))))
  5279. ;;;; Archiving
  5280. (defun org-get-category (&optional pos)
  5281. "Get the category applying to position POS."
  5282. (get-text-property (or pos (point)) 'org-category))
  5283. (defun org-refresh-category-properties ()
  5284. "Refresh category text properties in the buffer."
  5285. (let ((def-cat (cond
  5286. ((null org-category)
  5287. (if buffer-file-name
  5288. (file-name-sans-extension
  5289. (file-name-nondirectory buffer-file-name))
  5290. "???"))
  5291. ((symbolp org-category) (symbol-name org-category))
  5292. (t org-category)))
  5293. beg end cat pos optionp)
  5294. (org-unmodified
  5295. (save-excursion
  5296. (save-restriction
  5297. (widen)
  5298. (goto-char (point-min))
  5299. (put-text-property (point) (point-max) 'org-category def-cat)
  5300. (while (re-search-forward
  5301. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5302. (setq pos (match-end 0)
  5303. optionp (equal (char-after (match-beginning 0)) ?#)
  5304. cat (org-trim (match-string 2)))
  5305. (if optionp
  5306. (setq beg (point-at-bol) end (point-max))
  5307. (org-back-to-heading t)
  5308. (setq beg (point) end (org-end-of-subtree t t)))
  5309. (put-text-property beg end 'org-category cat)
  5310. (goto-char pos)))))))
  5311. ;;;; Link Stuff
  5312. ;;; Link abbreviations
  5313. (defun org-link-expand-abbrev (link)
  5314. "Apply replacements as defined in `org-link-abbrev-alist."
  5315. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5316. (let* ((key (match-string 1 link))
  5317. (as (or (assoc key org-link-abbrev-alist-local)
  5318. (assoc key org-link-abbrev-alist)))
  5319. (tag (and (match-end 2) (match-string 3 link)))
  5320. rpl)
  5321. (if (not as)
  5322. link
  5323. (setq rpl (cdr as))
  5324. (cond
  5325. ((symbolp rpl) (funcall rpl tag))
  5326. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5327. (t (concat rpl tag)))))
  5328. link))
  5329. ;;; Storing and inserting links
  5330. (defvar org-insert-link-history nil
  5331. "Minibuffer history for links inserted with `org-insert-link'.")
  5332. (defvar org-stored-links nil
  5333. "Contains the links stored with `org-store-link'.")
  5334. (defvar org-store-link-plist nil
  5335. "Plist with info about the most recently link created with `org-store-link'.")
  5336. (defvar org-link-protocols nil
  5337. "Link protocols added to Org-mode using `org-add-link-type'.")
  5338. (defvar org-store-link-functions nil
  5339. "List of functions that are called to create and store a link.
  5340. Each function will be called in turn until one returns a non-nil
  5341. value. Each function should check if it is responsible for creating
  5342. this link (for example by looking at the major mode).
  5343. If not, it must exit and return nil.
  5344. If yes, it should return a non-nil value after a calling
  5345. `org-store-link-props' with a list of properties and values.
  5346. Special properties are:
  5347. :type The link prefix. like \"http\". This must be given.
  5348. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5349. This is obligatory as well.
  5350. :description Optional default description for the second pair
  5351. of brackets in an Org-mode link. The user can still change
  5352. this when inserting this link into an Org-mode buffer.
  5353. In addition to these, any additional properties can be specified
  5354. and then used in remember templates.")
  5355. (defun org-add-link-type (type &optional follow export)
  5356. "Add TYPE to the list of `org-link-types'.
  5357. Re-compute all regular expressions depending on `org-link-types'
  5358. FOLLOW and EXPORT are two functions.
  5359. FOLLOW should take the link path as the single argument and do whatever
  5360. is necessary to follow the link, for example find a file or display
  5361. a mail message.
  5362. EXPORT should format the link path for export to one of the export formats.
  5363. It should be a function accepting three arguments:
  5364. path the path of the link, the text after the prefix (like \"http:\")
  5365. desc the description of the link, if any, nil if there was no descripton
  5366. format the export format, a symbol like `html' or `latex'.
  5367. The function may use the FORMAT information to return different values
  5368. depending on the format. The return value will be put literally into
  5369. the exported file.
  5370. Org-mode has a built-in default for exporting links. If you are happy with
  5371. this default, there is no need to define an export function for the link
  5372. type. For a simple example of an export function, see `org-bbdb.el'."
  5373. (add-to-list 'org-link-types type t)
  5374. (org-make-link-regexps)
  5375. (if (assoc type org-link-protocols)
  5376. (setcdr (assoc type org-link-protocols) (list follow export))
  5377. (push (list type follow export) org-link-protocols)))
  5378. ;;;###autoload
  5379. (defun org-store-link (arg)
  5380. "\\<org-mode-map>Store an org-link to the current location.
  5381. This link is added to `org-stored-links' and can later be inserted
  5382. into an org-buffer with \\[org-insert-link].
  5383. For some link types, a prefix arg is interpreted:
  5384. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5385. For file links, arg negates `org-context-in-file-links'."
  5386. (interactive "P")
  5387. (org-load-modules-maybe)
  5388. (setq org-store-link-plist nil) ; reset
  5389. (let (link cpltxt desc description search txt)
  5390. (cond
  5391. ((run-hook-with-args-until-success 'org-store-link-functions)
  5392. (setq link (plist-get org-store-link-plist :link)
  5393. desc (or (plist-get org-store-link-plist :description) link)))
  5394. ((eq major-mode 'calendar-mode)
  5395. (let ((cd (calendar-cursor-to-date)))
  5396. (setq link
  5397. (format-time-string
  5398. (car org-time-stamp-formats)
  5399. (apply 'encode-time
  5400. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5401. nil nil nil))))
  5402. (org-store-link-props :type "calendar" :date cd)))
  5403. ((eq major-mode 'w3-mode)
  5404. (setq cpltxt (url-view-url t)
  5405. link (org-make-link cpltxt))
  5406. (org-store-link-props :type "w3" :url (url-view-url t)))
  5407. ((eq major-mode 'w3m-mode)
  5408. (setq cpltxt (or w3m-current-title w3m-current-url)
  5409. link (org-make-link w3m-current-url))
  5410. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5411. ((setq search (run-hook-with-args-until-success
  5412. 'org-create-file-search-functions))
  5413. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5414. "::" search))
  5415. (setq cpltxt (or description link)))
  5416. ((eq major-mode 'image-mode)
  5417. (setq cpltxt (concat "file:"
  5418. (abbreviate-file-name buffer-file-name))
  5419. link (org-make-link cpltxt))
  5420. (org-store-link-props :type "image" :file buffer-file-name))
  5421. ((eq major-mode 'dired-mode)
  5422. ;; link to the file in the current line
  5423. (setq cpltxt (concat "file:"
  5424. (abbreviate-file-name
  5425. (expand-file-name
  5426. (dired-get-filename nil t))))
  5427. link (org-make-link cpltxt)))
  5428. ((and buffer-file-name (org-mode-p))
  5429. ;; Just link to current headline
  5430. (setq cpltxt (concat "file:"
  5431. (abbreviate-file-name buffer-file-name)))
  5432. ;; Add a context search string
  5433. (when (org-xor org-context-in-file-links arg)
  5434. ;; Check if we are on a target
  5435. (if (org-in-regexp "<<\\(.*?\\)>>")
  5436. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5437. (setq txt (cond
  5438. ((org-on-heading-p) nil)
  5439. ((org-region-active-p)
  5440. (buffer-substring (region-beginning) (region-end)))
  5441. (t nil)))
  5442. (when (or (null txt) (string-match "\\S-" txt))
  5443. (setq cpltxt
  5444. (concat cpltxt "::"
  5445. (condition-case nil
  5446. (org-make-org-heading-search-string txt)
  5447. (error "")))
  5448. desc "NONE"))))
  5449. (if (string-match "::\\'" cpltxt)
  5450. (setq cpltxt (substring cpltxt 0 -2)))
  5451. (setq link (org-make-link cpltxt)))
  5452. ((buffer-file-name (buffer-base-buffer))
  5453. ;; Just link to this file here.
  5454. (setq cpltxt (concat "file:"
  5455. (abbreviate-file-name
  5456. (buffer-file-name (buffer-base-buffer)))))
  5457. ;; Add a context string
  5458. (when (org-xor org-context-in-file-links arg)
  5459. (setq txt (if (org-region-active-p)
  5460. (buffer-substring (region-beginning) (region-end))
  5461. (buffer-substring (point-at-bol) (point-at-eol))))
  5462. ;; Only use search option if there is some text.
  5463. (when (string-match "\\S-" txt)
  5464. (setq cpltxt
  5465. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5466. desc "NONE")))
  5467. (setq link (org-make-link cpltxt)))
  5468. ((interactive-p)
  5469. (error "Cannot link to a buffer which is not visiting a file"))
  5470. (t (setq link nil)))
  5471. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5472. (setq link (or link cpltxt)
  5473. desc (or desc cpltxt))
  5474. (if (equal desc "NONE") (setq desc nil))
  5475. (if (and (interactive-p) link)
  5476. (progn
  5477. (setq org-stored-links
  5478. (cons (list link desc) org-stored-links))
  5479. (message "Stored: %s" (or desc link)))
  5480. (and link (org-make-link-string link desc)))))
  5481. (defun org-store-link-props (&rest plist)
  5482. "Store link properties, extract names and addresses."
  5483. (let (x adr)
  5484. (when (setq x (plist-get plist :from))
  5485. (setq adr (mail-extract-address-components x))
  5486. (plist-put plist :fromname (car adr))
  5487. (plist-put plist :fromaddress (nth 1 adr)))
  5488. (when (setq x (plist-get plist :to))
  5489. (setq adr (mail-extract-address-components x))
  5490. (plist-put plist :toname (car adr))
  5491. (plist-put plist :toaddress (nth 1 adr))))
  5492. (let ((from (plist-get plist :from))
  5493. (to (plist-get plist :to)))
  5494. (when (and from to org-from-is-user-regexp)
  5495. (plist-put plist :fromto
  5496. (if (string-match org-from-is-user-regexp from)
  5497. (concat "to %t")
  5498. (concat "from %f")))))
  5499. (setq org-store-link-plist plist))
  5500. (defun org-add-link-props (&rest plist)
  5501. "Add these properties to the link property list."
  5502. (let (key value)
  5503. (while plist
  5504. (setq key (pop plist) value (pop plist))
  5505. (setq org-store-link-plist
  5506. (plist-put org-store-link-plist key value)))))
  5507. (defun org-email-link-description (&optional fmt)
  5508. "Return the description part of an email link.
  5509. This takes information from `org-store-link-plist' and formats it
  5510. according to FMT (default from `org-email-link-description-format')."
  5511. (setq fmt (or fmt org-email-link-description-format))
  5512. (let* ((p org-store-link-plist)
  5513. (to (plist-get p :toaddress))
  5514. (from (plist-get p :fromaddress))
  5515. (table
  5516. (list
  5517. (cons "%c" (plist-get p :fromto))
  5518. (cons "%F" (plist-get p :from))
  5519. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5520. (cons "%T" (plist-get p :to))
  5521. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5522. (cons "%s" (plist-get p :subject))
  5523. (cons "%m" (plist-get p :message-id)))))
  5524. (when (string-match "%c" fmt)
  5525. ;; Check if the user wrote this message
  5526. (if (and org-from-is-user-regexp from to
  5527. (save-match-data (string-match org-from-is-user-regexp from)))
  5528. (setq fmt (replace-match "to %t" t t fmt))
  5529. (setq fmt (replace-match "from %f" t t fmt))))
  5530. (org-replace-escapes fmt table)))
  5531. (defun org-make-org-heading-search-string (&optional string heading)
  5532. "Make search string for STRING or current headline."
  5533. (interactive)
  5534. (let ((s (or string (org-get-heading))))
  5535. (unless (and string (not heading))
  5536. ;; We are using a headline, clean up garbage in there.
  5537. (if (string-match org-todo-regexp s)
  5538. (setq s (replace-match "" t t s)))
  5539. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5540. (setq s (replace-match "" t t s)))
  5541. (setq s (org-trim s))
  5542. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5543. org-comment-string "\\)") s)
  5544. (setq s (replace-match "" t t s)))
  5545. (while (string-match org-ts-regexp s)
  5546. (setq s (replace-match "" t t s))))
  5547. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5548. (setq s (replace-match " " t t s)))
  5549. (or string (setq s (concat "*" s))) ; Add * for headlines
  5550. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5551. (defun org-make-link (&rest strings)
  5552. "Concatenate STRINGS."
  5553. (apply 'concat strings))
  5554. (defun org-make-link-string (link &optional description)
  5555. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5556. (unless (string-match "\\S-" link)
  5557. (error "Empty link"))
  5558. (when (stringp description)
  5559. ;; Remove brackets from the description, they are fatal.
  5560. (while (string-match "\\[" description)
  5561. (setq description (replace-match "{" t t description)))
  5562. (while (string-match "\\]" description)
  5563. (setq description (replace-match "}" t t description))))
  5564. (when (equal (org-link-escape link) description)
  5565. ;; No description needed, it is identical
  5566. (setq description nil))
  5567. (when (and (not description)
  5568. (not (equal link (org-link-escape link))))
  5569. (setq description (org-extract-attributes link)))
  5570. (concat "[[" (org-link-escape link) "]"
  5571. (if description (concat "[" description "]") "")
  5572. "]"))
  5573. (defconst org-link-escape-chars
  5574. '((?\ . "%20")
  5575. (?\[ . "%5B")
  5576. (?\] . "%5D")
  5577. (?\340 . "%E0") ; `a
  5578. (?\342 . "%E2") ; ^a
  5579. (?\347 . "%E7") ; ,c
  5580. (?\350 . "%E8") ; `e
  5581. (?\351 . "%E9") ; 'e
  5582. (?\352 . "%EA") ; ^e
  5583. (?\356 . "%EE") ; ^i
  5584. (?\364 . "%F4") ; ^o
  5585. (?\371 . "%F9") ; `u
  5586. (?\373 . "%FB") ; ^u
  5587. (?\; . "%3B")
  5588. (?? . "%3F")
  5589. (?= . "%3D")
  5590. (?+ . "%2B")
  5591. )
  5592. "Association list of escapes for some characters problematic in links.
  5593. This is the list that is used for internal purposes.")
  5594. (defconst org-link-escape-chars-browser
  5595. '((?\ . "%20")) ; 32 for the SPC char
  5596. "Association list of escapes for some characters problematic in links.
  5597. This is the list that is used before handing over to the browser.")
  5598. (defun org-link-escape (text &optional table)
  5599. "Escape charaters in TEXT that are problematic for links."
  5600. (setq table (or table org-link-escape-chars))
  5601. (when text
  5602. (let ((re (mapconcat (lambda (x) (regexp-quote
  5603. (char-to-string (car x))))
  5604. table "\\|")))
  5605. (while (string-match re text)
  5606. (setq text
  5607. (replace-match
  5608. (cdr (assoc (string-to-char (match-string 0 text))
  5609. table))
  5610. t t text)))
  5611. text)))
  5612. (defun org-link-unescape (text &optional table)
  5613. "Reverse the action of `org-link-escape'."
  5614. (setq table (or table org-link-escape-chars))
  5615. (when text
  5616. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5617. table "\\|")))
  5618. (while (string-match re text)
  5619. (setq text
  5620. (replace-match
  5621. (char-to-string (car (rassoc (match-string 0 text) table)))
  5622. t t text)))
  5623. text)))
  5624. (defun org-xor (a b)
  5625. "Exclusive or."
  5626. (if a (not b) b))
  5627. (defun org-get-header (header)
  5628. "Find a header field in the current buffer."
  5629. (save-excursion
  5630. (goto-char (point-min))
  5631. (let ((case-fold-search t) s)
  5632. (cond
  5633. ((eq header 'from)
  5634. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5635. (setq s (match-string 1)))
  5636. (while (string-match "\"" s)
  5637. (setq s (replace-match "" t t s)))
  5638. (if (string-match "[<(].*" s)
  5639. (setq s (replace-match "" t t s))))
  5640. ((eq header 'message-id)
  5641. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5642. (setq s (match-string 1))))
  5643. ((eq header 'subject)
  5644. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5645. (setq s (match-string 1)))))
  5646. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5647. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5648. s)))
  5649. (defun org-fixup-message-id-for-http (s)
  5650. "Replace special characters in a message id, so it can be used in an http query."
  5651. (while (string-match "<" s)
  5652. (setq s (replace-match "%3C" t t s)))
  5653. (while (string-match ">" s)
  5654. (setq s (replace-match "%3E" t t s)))
  5655. (while (string-match "@" s)
  5656. (setq s (replace-match "%40" t t s)))
  5657. s)
  5658. ;;;###autoload
  5659. (defun org-insert-link-global ()
  5660. "Insert a link like Org-mode does.
  5661. This command can be called in any mode to insert a link in Org-mode syntax."
  5662. (interactive)
  5663. (org-load-modules-maybe)
  5664. (org-run-like-in-org-mode 'org-insert-link))
  5665. (defun org-insert-link (&optional complete-file link-location)
  5666. "Insert a link. At the prompt, enter the link.
  5667. Completion can be used to select a link previously stored with
  5668. `org-store-link'. When the empty string is entered (i.e. if you just
  5669. press RET at the prompt), the link defaults to the most recently
  5670. stored link. As SPC triggers completion in the minibuffer, you need to
  5671. use M-SPC or C-q SPC to force the insertion of a space character.
  5672. You will also be prompted for a description, and if one is given, it will
  5673. be displayed in the buffer instead of the link.
  5674. If there is already a link at point, this command will allow you to edit link
  5675. and description parts.
  5676. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5677. be selected using completion. The path to the file will be relative to the
  5678. current directory if the file is in the current directory or a subdirectory.
  5679. Otherwise, the link will be the absolute path as completed in the minibuffer
  5680. \(i.e. normally ~/path/to/file).
  5681. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5682. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5683. of `org-keep-stored-link-after-insertion'.
  5684. If `org-make-link-description-function' is non-nil, this function will be
  5685. called with the link target, and the result will be the default
  5686. link description.
  5687. If the LINK-LOCATION parameter is non-nil, this value will be
  5688. used as the link location instead of reading one interactively."
  5689. (interactive "P")
  5690. (let* ((wcf (current-window-configuration))
  5691. (region (if (org-region-active-p)
  5692. (buffer-substring (region-beginning) (region-end))))
  5693. (remove (and region (list (region-beginning) (region-end))))
  5694. (desc region)
  5695. tmphist ; byte-compile incorrectly complains about this
  5696. (link link-location)
  5697. entry file)
  5698. (cond
  5699. (link-location) ; specified by arg, just use it.
  5700. ((org-in-regexp org-bracket-link-regexp 1)
  5701. ;; We do have a link at point, and we are going to edit it.
  5702. (setq remove (list (match-beginning 0) (match-end 0)))
  5703. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5704. (setq link (read-string "Link: "
  5705. (org-link-unescape
  5706. (org-match-string-no-properties 1)))))
  5707. ((or (org-in-regexp org-angle-link-re)
  5708. (org-in-regexp org-plain-link-re))
  5709. ;; Convert to bracket link
  5710. (setq remove (list (match-beginning 0) (match-end 0))
  5711. link (read-string "Link: "
  5712. (org-remove-angle-brackets (match-string 0)))))
  5713. ((equal complete-file '(4))
  5714. ;; Completing read for file names.
  5715. (setq file (read-file-name "File: "))
  5716. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5717. (pwd1 (file-name-as-directory (abbreviate-file-name
  5718. (expand-file-name ".")))))
  5719. (cond
  5720. ((equal complete-file '(16))
  5721. (setq link (org-make-link
  5722. "file:"
  5723. (abbreviate-file-name (expand-file-name file)))))
  5724. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5725. (setq link (org-make-link "file:" (match-string 1 file))))
  5726. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5727. (expand-file-name file))
  5728. (setq link (org-make-link
  5729. "file:" (match-string 1 (expand-file-name file)))))
  5730. (t (setq link (org-make-link "file:" file))))))
  5731. (t
  5732. ;; Read link, with completion for stored links.
  5733. (with-output-to-temp-buffer "*Org Links*"
  5734. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5735. (when org-stored-links
  5736. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5737. (princ (mapconcat
  5738. (lambda (x)
  5739. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5740. (reverse org-stored-links) "\n"))))
  5741. (let ((cw (selected-window)))
  5742. (select-window (get-buffer-window "*Org Links*"))
  5743. (shrink-window-if-larger-than-buffer)
  5744. (setq truncate-lines t)
  5745. (select-window cw))
  5746. ;; Fake a link history, containing the stored links.
  5747. (setq tmphist (append (mapcar 'car org-stored-links)
  5748. org-insert-link-history))
  5749. (unwind-protect
  5750. (setq link (org-completing-read
  5751. "Link: "
  5752. (append
  5753. (mapcar (lambda (x) (list (concat (car x) ":")))
  5754. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5755. (mapcar (lambda (x) (list (concat x ":")))
  5756. org-link-types))
  5757. nil nil nil
  5758. 'tmphist
  5759. (or (car (car org-stored-links)))))
  5760. (set-window-configuration wcf)
  5761. (kill-buffer "*Org Links*"))
  5762. (setq entry (assoc link org-stored-links))
  5763. (or entry (push link org-insert-link-history))
  5764. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5765. (not org-keep-stored-link-after-insertion))
  5766. (setq org-stored-links (delq (assoc link org-stored-links)
  5767. org-stored-links)))
  5768. (setq desc (or desc (nth 1 entry)))))
  5769. (if (string-match org-plain-link-re link)
  5770. ;; URL-like link, normalize the use of angular brackets.
  5771. (setq link (org-make-link (org-remove-angle-brackets link))))
  5772. ;; Check if we are linking to the current file with a search option
  5773. ;; If yes, simplify the link by using only the search option.
  5774. (when (and buffer-file-name
  5775. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5776. (let* ((path (match-string 1 link))
  5777. (case-fold-search nil)
  5778. (search (match-string 2 link)))
  5779. (save-match-data
  5780. (if (equal (file-truename buffer-file-name) (file-truename path))
  5781. ;; We are linking to this same file, with a search option
  5782. (setq link search)))))
  5783. ;; Check if we can/should use a relative path. If yes, simplify the link
  5784. (when (string-match "\\<file:\\(.*\\)" link)
  5785. (let* ((path (match-string 1 link))
  5786. (origpath path)
  5787. (case-fold-search nil))
  5788. (cond
  5789. ((eq org-link-file-path-type 'absolute)
  5790. (setq path (abbreviate-file-name (expand-file-name path))))
  5791. ((eq org-link-file-path-type 'noabbrev)
  5792. (setq path (expand-file-name path)))
  5793. ((eq org-link-file-path-type 'relative)
  5794. (setq path (file-relative-name path)))
  5795. (t
  5796. (save-match-data
  5797. (if (string-match (concat "^" (regexp-quote
  5798. (file-name-as-directory
  5799. (expand-file-name "."))))
  5800. (expand-file-name path))
  5801. ;; We are linking a file with relative path name.
  5802. (setq path (substring (expand-file-name path)
  5803. (match-end 0)))))))
  5804. (setq link (concat "file:" path))
  5805. (if (equal desc origpath)
  5806. (setq desc path))))
  5807. (if org-make-link-description-function
  5808. (setq desc (funcall org-make-link-description-function link desc)))
  5809. (setq desc (read-string "Description: " desc))
  5810. (unless (string-match "\\S-" desc) (setq desc nil))
  5811. (if remove (apply 'delete-region remove))
  5812. (insert (org-make-link-string link desc))))
  5813. (defun org-completing-read (&rest args)
  5814. (let ((minibuffer-local-completion-map
  5815. (copy-keymap minibuffer-local-completion-map)))
  5816. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5817. (apply 'completing-read args)))
  5818. (defun org-extract-attributes (s)
  5819. "Extract the attributes cookie from a string and set as text property."
  5820. (let (a attr (start 0) key value)
  5821. (save-match-data
  5822. (when (string-match "{{\\([^}]+\\)}}$" s)
  5823. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5824. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5825. (setq key (match-string 1 a) value (match-string 2 a)
  5826. start (match-end 0)
  5827. attr (plist-put attr (intern key) value))))
  5828. (org-add-props s nil 'org-attributes attr))
  5829. s))
  5830. (defun org-attributes-to-string (plist)
  5831. "Format a property list into an HTML attribute list."
  5832. (let ((s "") key value)
  5833. (while plist
  5834. (setq key (pop plist) value (pop plist))
  5835. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5836. s))
  5837. ;;; Opening/following a link
  5838. (defvar org-link-search-failed nil)
  5839. (defun org-next-link ()
  5840. "Move forward to the next link.
  5841. If the link is in hidden text, expose it."
  5842. (interactive)
  5843. (when (and org-link-search-failed (eq this-command last-command))
  5844. (goto-char (point-min))
  5845. (message "Link search wrapped back to beginning of buffer"))
  5846. (setq org-link-search-failed nil)
  5847. (let* ((pos (point))
  5848. (ct (org-context))
  5849. (a (assoc :link ct)))
  5850. (if a (goto-char (nth 2 a)))
  5851. (if (re-search-forward org-any-link-re nil t)
  5852. (progn
  5853. (goto-char (match-beginning 0))
  5854. (if (org-invisible-p) (org-show-context)))
  5855. (goto-char pos)
  5856. (setq org-link-search-failed t)
  5857. (error "No further link found"))))
  5858. (defun org-previous-link ()
  5859. "Move backward to the previous link.
  5860. If the link is in hidden text, expose it."
  5861. (interactive)
  5862. (when (and org-link-search-failed (eq this-command last-command))
  5863. (goto-char (point-max))
  5864. (message "Link search wrapped back to end of buffer"))
  5865. (setq org-link-search-failed nil)
  5866. (let* ((pos (point))
  5867. (ct (org-context))
  5868. (a (assoc :link ct)))
  5869. (if a (goto-char (nth 1 a)))
  5870. (if (re-search-backward org-any-link-re nil t)
  5871. (progn
  5872. (goto-char (match-beginning 0))
  5873. (if (org-invisible-p) (org-show-context)))
  5874. (goto-char pos)
  5875. (setq org-link-search-failed t)
  5876. (error "No further link found"))))
  5877. (defun org-find-file-at-mouse (ev)
  5878. "Open file link or URL at mouse."
  5879. (interactive "e")
  5880. (mouse-set-point ev)
  5881. (org-open-at-point 'in-emacs))
  5882. (defun org-open-at-mouse (ev)
  5883. "Open file link or URL at mouse."
  5884. (interactive "e")
  5885. (mouse-set-point ev)
  5886. (org-open-at-point))
  5887. (defvar org-window-config-before-follow-link nil
  5888. "The window configuration before following a link.
  5889. This is saved in case the need arises to restore it.")
  5890. (defvar org-open-link-marker (make-marker)
  5891. "Marker pointing to the location where `org-open-at-point; was called.")
  5892. ;;;###autoload
  5893. (defun org-open-at-point-global ()
  5894. "Follow a link like Org-mode does.
  5895. This command can be called in any mode to follow a link that has
  5896. Org-mode syntax."
  5897. (interactive)
  5898. (org-run-like-in-org-mode 'org-open-at-point))
  5899. ;;;###autoload
  5900. (defun org-open-link-from-string (s &optional arg)
  5901. "Open a link in the string S, as if it was in Org-mode."
  5902. (interactive "sLink: \nP")
  5903. (with-temp-buffer
  5904. (let ((org-inhibit-startup t))
  5905. (org-mode)
  5906. (insert s)
  5907. (goto-char (point-min))
  5908. (org-open-at-point arg))))
  5909. (defun org-open-at-point (&optional in-emacs)
  5910. "Open link at or after point.
  5911. If there is no link at point, this function will search forward up to
  5912. the end of the current subtree.
  5913. Normally, files will be opened by an appropriate application. If the
  5914. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  5915. (interactive "P")
  5916. (org-load-modules-maybe)
  5917. (move-marker org-open-link-marker (point))
  5918. (setq org-window-config-before-follow-link (current-window-configuration))
  5919. (org-remove-occur-highlights nil nil t)
  5920. (if (org-at-timestamp-p t)
  5921. (org-follow-timestamp-link)
  5922. (let (type path link line search (pos (point)))
  5923. (catch 'match
  5924. (save-excursion
  5925. (skip-chars-forward "^]\n\r")
  5926. (when (org-in-regexp org-bracket-link-regexp)
  5927. (setq link (org-extract-attributes
  5928. (org-link-unescape (org-match-string-no-properties 1))))
  5929. (while (string-match " *\n *" link)
  5930. (setq link (replace-match " " t t link)))
  5931. (setq link (org-link-expand-abbrev link))
  5932. (cond
  5933. ((or (file-name-absolute-p link)
  5934. (string-match "^\\.\\.?/" link))
  5935. (setq type "file" path link))
  5936. ((string-match org-link-re-with-space2 link)
  5937. (setq type (match-string 1 link) path (match-string 2 link)))
  5938. (t (setq type "thisfile" path link)))
  5939. (throw 'match t)))
  5940. (when (get-text-property (point) 'org-linked-text)
  5941. (setq type "thisfile"
  5942. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  5943. (1+ (point)) (point))
  5944. path (buffer-substring
  5945. (previous-single-property-change pos 'org-linked-text)
  5946. (next-single-property-change pos 'org-linked-text)))
  5947. (throw 'match t))
  5948. (save-excursion
  5949. (when (or (org-in-regexp org-angle-link-re)
  5950. (org-in-regexp org-plain-link-re))
  5951. (setq type (match-string 1) path (match-string 2))
  5952. (throw 'match t)))
  5953. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  5954. (setq type "tree-match"
  5955. path (match-string 1))
  5956. (throw 'match t))
  5957. (save-excursion
  5958. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  5959. (setq type "tags"
  5960. path (match-string 1))
  5961. (while (string-match ":" path)
  5962. (setq path (replace-match "+" t t path)))
  5963. (throw 'match t))))
  5964. (unless path
  5965. (error "No link found"))
  5966. ;; Remove any trailing spaces in path
  5967. (if (string-match " +\\'" path)
  5968. (setq path (replace-match "" t t path)))
  5969. (cond
  5970. ((assoc type org-link-protocols)
  5971. (funcall (nth 1 (assoc type org-link-protocols)) path))
  5972. ((equal type "mailto")
  5973. (let ((cmd (car org-link-mailto-program))
  5974. (args (cdr org-link-mailto-program)) args1
  5975. (address path) (subject "") a)
  5976. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  5977. (setq address (match-string 1 path)
  5978. subject (org-link-escape (match-string 2 path))))
  5979. (while args
  5980. (cond
  5981. ((not (stringp (car args))) (push (pop args) args1))
  5982. (t (setq a (pop args))
  5983. (if (string-match "%a" a)
  5984. (setq a (replace-match address t t a)))
  5985. (if (string-match "%s" a)
  5986. (setq a (replace-match subject t t a)))
  5987. (push a args1))))
  5988. (apply cmd (nreverse args1))))
  5989. ((member type '("http" "https" "ftp" "news"))
  5990. (browse-url (concat type ":" (org-link-escape
  5991. path org-link-escape-chars-browser))))
  5992. ((member type '("message"))
  5993. (browse-url (concat type ":" path)))
  5994. ((string= type "tags")
  5995. (org-tags-view in-emacs path))
  5996. ((string= type "thisfile")
  5997. (if in-emacs
  5998. (switch-to-buffer-other-window
  5999. (org-get-buffer-for-internal-link (current-buffer)))
  6000. (org-mark-ring-push))
  6001. (let ((cmd `(org-link-search
  6002. ,path
  6003. ,(cond ((equal in-emacs '(4)) 'occur)
  6004. ((equal in-emacs '(16)) 'org-occur)
  6005. (t nil))
  6006. ,pos)))
  6007. (condition-case nil (eval cmd)
  6008. (error (progn (widen) (eval cmd))))))
  6009. ((string= type "tree-match")
  6010. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6011. ((string= type "file")
  6012. (if (string-match "::\\([0-9]+\\)\\'" path)
  6013. (setq line (string-to-number (match-string 1 path))
  6014. path (substring path 0 (match-beginning 0)))
  6015. (if (string-match "::\\(.+\\)\\'" path)
  6016. (setq search (match-string 1 path)
  6017. path (substring path 0 (match-beginning 0)))))
  6018. (if (string-match "[*?{]" (file-name-nondirectory path))
  6019. (dired path)
  6020. (org-open-file path in-emacs line search)))
  6021. ((string= type "news")
  6022. (require 'org-gnus)
  6023. (org-gnus-follow-link path))
  6024. ((string= type "shell")
  6025. (let ((cmd path))
  6026. (if (or (not org-confirm-shell-link-function)
  6027. (funcall org-confirm-shell-link-function
  6028. (format "Execute \"%s\" in shell? "
  6029. (org-add-props cmd nil
  6030. 'face 'org-warning))))
  6031. (progn
  6032. (message "Executing %s" cmd)
  6033. (shell-command cmd))
  6034. (error "Abort"))))
  6035. ((string= type "elisp")
  6036. (let ((cmd path))
  6037. (if (or (not org-confirm-elisp-link-function)
  6038. (funcall org-confirm-elisp-link-function
  6039. (format "Execute \"%s\" as elisp? "
  6040. (org-add-props cmd nil
  6041. 'face 'org-warning))))
  6042. (message "%s => %s" cmd (eval (read cmd)))
  6043. (error "Abort"))))
  6044. (t
  6045. (browse-url-at-point)))))
  6046. (move-marker org-open-link-marker nil)
  6047. (run-hook-with-args 'org-follow-link-hook))
  6048. ;;;; Time estimates
  6049. (defun org-get-effort (&optional pom)
  6050. "Get the effort estimate for the current entry."
  6051. (org-entry-get pom org-effort-property))
  6052. ;;; File search
  6053. (defvar org-create-file-search-functions nil
  6054. "List of functions to construct the right search string for a file link.
  6055. These functions are called in turn with point at the location to
  6056. which the link should point.
  6057. A function in the hook should first test if it would like to
  6058. handle this file type, for example by checking the major-mode or
  6059. the file extension. If it decides not to handle this file, it
  6060. should just return nil to give other functions a chance. If it
  6061. does handle the file, it must return the search string to be used
  6062. when following the link. The search string will be part of the
  6063. file link, given after a double colon, and `org-open-at-point'
  6064. will automatically search for it. If special measures must be
  6065. taken to make the search successful, another function should be
  6066. added to the companion hook `org-execute-file-search-functions',
  6067. which see.
  6068. A function in this hook may also use `setq' to set the variable
  6069. `description' to provide a suggestion for the descriptive text to
  6070. be used for this link when it gets inserted into an Org-mode
  6071. buffer with \\[org-insert-link].")
  6072. (defvar org-execute-file-search-functions nil
  6073. "List of functions to execute a file search triggered by a link.
  6074. Functions added to this hook must accept a single argument, the
  6075. search string that was part of the file link, the part after the
  6076. double colon. The function must first check if it would like to
  6077. handle this search, for example by checking the major-mode or the
  6078. file extension. If it decides not to handle this search, it
  6079. should just return nil to give other functions a chance. If it
  6080. does handle the search, it must return a non-nil value to keep
  6081. other functions from trying.
  6082. Each function can access the current prefix argument through the
  6083. variable `current-prefix-argument'. Note that a single prefix is
  6084. used to force opening a link in Emacs, so it may be good to only
  6085. use a numeric or double prefix to guide the search function.
  6086. In case this is needed, a function in this hook can also restore
  6087. the window configuration before `org-open-at-point' was called using:
  6088. (set-window-configuration org-window-config-before-follow-link)")
  6089. (defun org-link-search (s &optional type avoid-pos)
  6090. "Search for a link search option.
  6091. If S is surrounded by forward slashes, it is interpreted as a
  6092. regular expression. In org-mode files, this will create an `org-occur'
  6093. sparse tree. In ordinary files, `occur' will be used to list matches.
  6094. If the current buffer is in `dired-mode', grep will be used to search
  6095. in all files. If AVOID-POS is given, ignore matches near that position."
  6096. (let ((case-fold-search t)
  6097. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6098. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6099. (append '(("") (" ") ("\t") ("\n"))
  6100. org-emphasis-alist)
  6101. "\\|") "\\)"))
  6102. (pos (point))
  6103. (pre nil) (post nil)
  6104. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6105. (cond
  6106. ;; First check if there are any special
  6107. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6108. ;; Now try the builtin stuff
  6109. ((save-excursion
  6110. (goto-char (point-min))
  6111. (and
  6112. (re-search-forward
  6113. (concat "<<" (regexp-quote s0) ">>") nil t)
  6114. (setq type 'dedicated
  6115. pos (match-beginning 0))))
  6116. ;; There is an exact target for this
  6117. (goto-char pos))
  6118. ((string-match "^/\\(.*\\)/$" s)
  6119. ;; A regular expression
  6120. (cond
  6121. ((org-mode-p)
  6122. (org-occur (match-string 1 s)))
  6123. ;;((eq major-mode 'dired-mode)
  6124. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6125. (t (org-do-occur (match-string 1 s)))))
  6126. (t
  6127. ;; A normal search strings
  6128. (when (equal (string-to-char s) ?*)
  6129. ;; Anchor on headlines, post may include tags.
  6130. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6131. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6132. s (substring s 1)))
  6133. (remove-text-properties
  6134. 0 (length s)
  6135. '(face nil mouse-face nil keymap nil fontified nil) s)
  6136. ;; Make a series of regular expressions to find a match
  6137. (setq words (org-split-string s "[ \n\r\t]+")
  6138. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6139. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6140. "\\)" markers)
  6141. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6142. re2a (concat "[ \t\r\n]" re2a_)
  6143. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6144. re4 (concat "[^a-zA-Z_]" re4_)
  6145. re1 (concat pre re2 post)
  6146. re3 (concat pre (if pre re4_ re4) post)
  6147. re5 (concat pre ".*" re4)
  6148. re2 (concat pre re2)
  6149. re2a (concat pre (if pre re2a_ re2a))
  6150. re4 (concat pre (if pre re4_ re4))
  6151. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6152. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6153. re5 "\\)"
  6154. ))
  6155. (cond
  6156. ((eq type 'org-occur) (org-occur reall))
  6157. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6158. (t (goto-char (point-min))
  6159. (setq type 'fuzzy)
  6160. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6161. (org-search-not-self 1 re1 nil t)
  6162. (org-search-not-self 1 re2 nil t)
  6163. (org-search-not-self 1 re2a nil t)
  6164. (org-search-not-self 1 re3 nil t)
  6165. (org-search-not-self 1 re4 nil t)
  6166. (org-search-not-self 1 re5 nil t)
  6167. )
  6168. (goto-char (match-beginning 1))
  6169. (goto-char pos)
  6170. (error "No match")))))
  6171. (t
  6172. ;; Normal string-search
  6173. (goto-char (point-min))
  6174. (if (search-forward s nil t)
  6175. (goto-char (match-beginning 0))
  6176. (error "No match"))))
  6177. (and (org-mode-p) (org-show-context 'link-search))
  6178. type))
  6179. (defun org-search-not-self (group &rest args)
  6180. "Execute `re-search-forward', but only accept matches that do not
  6181. enclose the position of `org-open-link-marker'."
  6182. (let ((m org-open-link-marker))
  6183. (catch 'exit
  6184. (while (apply 're-search-forward args)
  6185. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6186. (goto-char (match-end group))
  6187. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6188. (> (match-beginning 0) (marker-position m))
  6189. (< (match-end 0) (marker-position m)))
  6190. (save-match-data
  6191. (or (not (org-in-regexp
  6192. org-bracket-link-analytic-regexp 1))
  6193. (not (match-end 4)) ; no description
  6194. (and (<= (match-beginning 4) (point))
  6195. (>= (match-end 4) (point))))))
  6196. (throw 'exit (point))))))))
  6197. (defun org-get-buffer-for-internal-link (buffer)
  6198. "Return a buffer to be used for displaying the link target of internal links."
  6199. (cond
  6200. ((not org-display-internal-link-with-indirect-buffer)
  6201. buffer)
  6202. ((string-match "(Clone)$" (buffer-name buffer))
  6203. (message "Buffer is already a clone, not making another one")
  6204. ;; we also do not modify visibility in this case
  6205. buffer)
  6206. (t ; make a new indirect buffer for displaying the link
  6207. (let* ((bn (buffer-name buffer))
  6208. (ibn (concat bn "(Clone)"))
  6209. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6210. (with-current-buffer ib (org-overview))
  6211. ib))))
  6212. (defun org-do-occur (regexp &optional cleanup)
  6213. "Call the Emacs command `occur'.
  6214. If CLEANUP is non-nil, remove the printout of the regular expression
  6215. in the *Occur* buffer. This is useful if the regex is long and not useful
  6216. to read."
  6217. (occur regexp)
  6218. (when cleanup
  6219. (let ((cwin (selected-window)) win beg end)
  6220. (when (setq win (get-buffer-window "*Occur*"))
  6221. (select-window win))
  6222. (goto-char (point-min))
  6223. (when (re-search-forward "match[a-z]+" nil t)
  6224. (setq beg (match-end 0))
  6225. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6226. (setq end (1- (match-beginning 0)))))
  6227. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6228. (goto-char (point-min))
  6229. (select-window cwin))))
  6230. ;;; The mark ring for links jumps
  6231. (defvar org-mark-ring nil
  6232. "Mark ring for positions before jumps in Org-mode.")
  6233. (defvar org-mark-ring-last-goto nil
  6234. "Last position in the mark ring used to go back.")
  6235. ;; Fill and close the ring
  6236. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6237. (loop for i from 1 to org-mark-ring-length do
  6238. (push (make-marker) org-mark-ring))
  6239. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6240. org-mark-ring)
  6241. (defun org-mark-ring-push (&optional pos buffer)
  6242. "Put the current position or POS into the mark ring and rotate it."
  6243. (interactive)
  6244. (setq pos (or pos (point)))
  6245. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6246. (move-marker (car org-mark-ring)
  6247. (or pos (point))
  6248. (or buffer (current-buffer)))
  6249. (message "%s"
  6250. (substitute-command-keys
  6251. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6252. (defun org-mark-ring-goto (&optional n)
  6253. "Jump to the previous position in the mark ring.
  6254. With prefix arg N, jump back that many stored positions. When
  6255. called several times in succession, walk through the entire ring.
  6256. Org-mode commands jumping to a different position in the current file,
  6257. or to another Org-mode file, automatically push the old position
  6258. onto the ring."
  6259. (interactive "p")
  6260. (let (p m)
  6261. (if (eq last-command this-command)
  6262. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6263. (setq p org-mark-ring))
  6264. (setq org-mark-ring-last-goto p)
  6265. (setq m (car p))
  6266. (switch-to-buffer (marker-buffer m))
  6267. (goto-char m)
  6268. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6269. (defun org-remove-angle-brackets (s)
  6270. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6271. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6272. s)
  6273. (defun org-add-angle-brackets (s)
  6274. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6275. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6276. s)
  6277. (defun org-remove-double-quotes (s)
  6278. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6279. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6280. s)
  6281. ;;; Following specific links
  6282. (defun org-follow-timestamp-link ()
  6283. (cond
  6284. ((org-at-date-range-p t)
  6285. (let ((org-agenda-start-on-weekday)
  6286. (t1 (match-string 1))
  6287. (t2 (match-string 2)))
  6288. (setq t1 (time-to-days (org-time-string-to-time t1))
  6289. t2 (time-to-days (org-time-string-to-time t2)))
  6290. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6291. ((org-at-timestamp-p t)
  6292. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6293. (substring (match-string 1) 0 10)))
  6294. 1))
  6295. (t (error "This should not happen"))))
  6296. ;;; Following file links
  6297. (defvar org-wait nil)
  6298. (defun org-open-file (path &optional in-emacs line search)
  6299. "Open the file at PATH.
  6300. First, this expands any special file name abbreviations. Then the
  6301. configuration variable `org-file-apps' is checked if it contains an
  6302. entry for this file type, and if yes, the corresponding command is launched.
  6303. If no application is found, Emacs simply visits the file.
  6304. With optional argument IN-EMACS, Emacs will visit the file.
  6305. Optional LINE specifies a line to go to, optional SEARCH a string to
  6306. search for. If LINE or SEARCH is given, the file will always be
  6307. opened in Emacs.
  6308. If the file does not exist, an error is thrown."
  6309. (setq in-emacs (or in-emacs line search))
  6310. (let* ((file (if (equal path "")
  6311. buffer-file-name
  6312. (substitute-in-file-name (expand-file-name path))))
  6313. (apps (append org-file-apps (org-default-apps)))
  6314. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6315. (dirp (if remp nil (file-directory-p file)))
  6316. (file (if (and dirp org-open-directory-means-index-dot-org)
  6317. (concat (file-name-as-directory file) "index.org")
  6318. file))
  6319. (a-m-a-p (assq 'auto-mode apps))
  6320. (dfile (downcase file))
  6321. (old-buffer (current-buffer))
  6322. (old-pos (point))
  6323. (old-mode major-mode)
  6324. ext cmd)
  6325. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6326. (setq ext (match-string 1 dfile))
  6327. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6328. (setq ext (match-string 1 dfile))))
  6329. (if in-emacs
  6330. (setq cmd 'emacs)
  6331. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6332. (and dirp (cdr (assoc 'directory apps)))
  6333. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6334. 'string-match)
  6335. (cdr (assoc ext apps))
  6336. (cdr (assoc t apps)))))
  6337. (when (eq cmd 'default)
  6338. (setq cmd (cdr (assoc t apps))))
  6339. (when (eq cmd 'mailcap)
  6340. (require 'mailcap)
  6341. (mailcap-parse-mailcaps)
  6342. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6343. (command (mailcap-mime-info mime-type)))
  6344. (if (stringp command)
  6345. (setq cmd command)
  6346. (setq cmd 'emacs))))
  6347. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6348. (not (file-exists-p file))
  6349. (not org-open-non-existing-files))
  6350. (error "No such file: %s" file))
  6351. (cond
  6352. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6353. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6354. (while (string-match "['\"]%s['\"]" cmd)
  6355. (setq cmd (replace-match "%s" t t cmd)))
  6356. (while (string-match "%s" cmd)
  6357. (setq cmd (replace-match
  6358. (save-match-data
  6359. (shell-quote-argument
  6360. (convert-standard-filename file)))
  6361. t t cmd)))
  6362. (save-window-excursion
  6363. (start-process-shell-command cmd nil cmd)
  6364. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6365. ))
  6366. ((or (stringp cmd)
  6367. (eq cmd 'emacs))
  6368. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6369. (widen)
  6370. (if line (goto-line line)
  6371. (if search (org-link-search search))))
  6372. ((consp cmd)
  6373. (let ((file (convert-standard-filename file)))
  6374. (eval cmd)))
  6375. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6376. (and (org-mode-p) (eq old-mode 'org-mode)
  6377. (or (not (equal old-buffer (current-buffer)))
  6378. (not (equal old-pos (point))))
  6379. (org-mark-ring-push old-pos old-buffer))))
  6380. (defun org-default-apps ()
  6381. "Return the default applications for this operating system."
  6382. (cond
  6383. ((eq system-type 'darwin)
  6384. org-file-apps-defaults-macosx)
  6385. ((eq system-type 'windows-nt)
  6386. org-file-apps-defaults-windowsnt)
  6387. (t org-file-apps-defaults-gnu)))
  6388. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6389. "Convert extensions to regular expressions in the cars of LIST.
  6390. Also, weed out any non-string entries, because the return value is used
  6391. only for regexp matching.
  6392. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6393. point to the symbol `emacs', indicating that the file should
  6394. be opened in Emacs."
  6395. (append
  6396. (delq nil
  6397. (mapcar (lambda (x)
  6398. (if (not (stringp (car x)))
  6399. nil
  6400. (if (string-match "\\W" (car x))
  6401. x
  6402. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6403. list))
  6404. (if add-auto-mode
  6405. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6406. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6407. (defun org-file-remote-p (file)
  6408. "Test whether FILE specifies a location on a remote system.
  6409. Return non-nil if the location is indeed remote.
  6410. For example, the filename \"/user@host:/foo\" specifies a location
  6411. on the system \"/user@host:\"."
  6412. (cond ((fboundp 'file-remote-p)
  6413. (file-remote-p file))
  6414. ((fboundp 'tramp-handle-file-remote-p)
  6415. (tramp-handle-file-remote-p file))
  6416. ((and (boundp 'ange-ftp-name-format)
  6417. (string-match (car ange-ftp-name-format) file))
  6418. t)
  6419. (t nil)))
  6420. ;;;; Refiling
  6421. (defun org-get-org-file ()
  6422. "Read a filename, with default directory `org-directory'."
  6423. (let ((default (or org-default-notes-file remember-data-file)))
  6424. (read-file-name (format "File name [%s]: " default)
  6425. (file-name-as-directory org-directory)
  6426. default)))
  6427. (defun org-notes-order-reversed-p ()
  6428. "Check if the current file should receive notes in reversed order."
  6429. (cond
  6430. ((not org-reverse-note-order) nil)
  6431. ((eq t org-reverse-note-order) t)
  6432. ((not (listp org-reverse-note-order)) nil)
  6433. (t (catch 'exit
  6434. (let ((all org-reverse-note-order)
  6435. entry)
  6436. (while (setq entry (pop all))
  6437. (if (string-match (car entry) buffer-file-name)
  6438. (throw 'exit (cdr entry))))
  6439. nil)))))
  6440. (defvar org-refile-target-table nil
  6441. "The list of refile targets, created by `org-refile'.")
  6442. (defvar org-agenda-new-buffers nil
  6443. "Buffers created to visit agenda files.")
  6444. (defun org-get-refile-targets (&optional default-buffer)
  6445. "Produce a table with refile targets."
  6446. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6447. targets txt re files f desc descre)
  6448. (with-current-buffer (or default-buffer (current-buffer))
  6449. (while (setq entry (pop entries))
  6450. (setq files (car entry) desc (cdr entry))
  6451. (cond
  6452. ((null files) (setq files (list (current-buffer))))
  6453. ((eq files 'org-agenda-files)
  6454. (setq files (org-agenda-files 'unrestricted)))
  6455. ((and (symbolp files) (fboundp files))
  6456. (setq files (funcall files)))
  6457. ((and (symbolp files) (boundp files))
  6458. (setq files (symbol-value files))))
  6459. (if (stringp files) (setq files (list files)))
  6460. (cond
  6461. ((eq (car desc) :tag)
  6462. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6463. ((eq (car desc) :todo)
  6464. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6465. ((eq (car desc) :regexp)
  6466. (setq descre (cdr desc)))
  6467. ((eq (car desc) :level)
  6468. (setq descre (concat "^\\*\\{" (number-to-string
  6469. (if org-odd-levels-only
  6470. (1- (* 2 (cdr desc)))
  6471. (cdr desc)))
  6472. "\\}[ \t]")))
  6473. ((eq (car desc) :maxlevel)
  6474. (setq descre (concat "^\\*\\{1," (number-to-string
  6475. (if org-odd-levels-only
  6476. (1- (* 2 (cdr desc)))
  6477. (cdr desc)))
  6478. "\\}[ \t]")))
  6479. (t (error "Bad refiling target description %s" desc)))
  6480. (while (setq f (pop files))
  6481. (save-excursion
  6482. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6483. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6484. (save-excursion
  6485. (save-restriction
  6486. (widen)
  6487. (goto-char (point-min))
  6488. (while (re-search-forward descre nil t)
  6489. (goto-char (point-at-bol))
  6490. (when (looking-at org-complex-heading-regexp)
  6491. (setq txt (org-link-display-format (match-string 4))
  6492. re (concat "^" (regexp-quote
  6493. (buffer-substring (match-beginning 1)
  6494. (match-end 4)))))
  6495. (if (match-end 5) (setq re (concat re "[ \t]+"
  6496. (regexp-quote
  6497. (match-string 5)))))
  6498. (setq re (concat re "[ \t]*$"))
  6499. (when org-refile-use-outline-path
  6500. (setq txt (mapconcat 'org-protect-slash
  6501. (append
  6502. (if (eq org-refile-use-outline-path 'file)
  6503. (list (file-name-nondirectory
  6504. (buffer-file-name (buffer-base-buffer))))
  6505. (if (eq org-refile-use-outline-path 'full-file-path)
  6506. (list (buffer-file-name (buffer-base-buffer)))))
  6507. (org-get-outline-path)
  6508. (list txt))
  6509. "/")))
  6510. (push (list txt f re (point)) targets))
  6511. (goto-char (point-at-eol))))))))
  6512. (nreverse targets))))
  6513. (defun org-protect-slash (s)
  6514. (while (string-match "/" s)
  6515. (setq s (replace-match "\\" t t s)))
  6516. s)
  6517. (defun org-get-outline-path ()
  6518. "Return the outline path to the current entry, as a list."
  6519. (let (rtn)
  6520. (save-excursion
  6521. (while (org-up-heading-safe)
  6522. (when (looking-at org-complex-heading-regexp)
  6523. (push (org-match-string-no-properties 4) rtn)))
  6524. rtn)))
  6525. (defvar org-refile-history nil
  6526. "History for refiling operations.")
  6527. (defun org-refile (&optional goto default-buffer)
  6528. "Move the entry at point to another heading.
  6529. The list of target headings is compiled using the information in
  6530. `org-refile-targets', which see. This list is created before each use
  6531. and will therefore always be up-to-date.
  6532. At the target location, the entry is filed as a subitem of the target heading.
  6533. Depending on `org-reverse-note-order', the new subitem will either be the
  6534. first of the last subitem.
  6535. With prefix arg GOTO, the command will only visit the target location,
  6536. not actually move anything.
  6537. With a double prefix `C-u C-u', go to the location where the last refiling
  6538. operation has put the subtree."
  6539. (interactive "P")
  6540. (let* ((cbuf (current-buffer))
  6541. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6542. pos it nbuf file re level reversed)
  6543. (if (equal goto '(16))
  6544. (org-refile-goto-last-stored)
  6545. (when (setq it (org-refile-get-location
  6546. (if goto "Goto: " "Refile to: ") default-buffer))
  6547. (setq file (nth 1 it)
  6548. re (nth 2 it)
  6549. pos (nth 3 it))
  6550. (setq nbuf (or (find-buffer-visiting file)
  6551. (find-file-noselect file)))
  6552. (if goto
  6553. (progn
  6554. (switch-to-buffer nbuf)
  6555. (goto-char pos)
  6556. (org-show-context 'org-goto))
  6557. (org-copy-subtree 1 nil t)
  6558. (save-excursion
  6559. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6560. (find-file-noselect file))))
  6561. (setq reversed (org-notes-order-reversed-p))
  6562. (save-excursion
  6563. (save-restriction
  6564. (widen)
  6565. (goto-char pos)
  6566. (looking-at outline-regexp)
  6567. (setq level (org-get-valid-level (funcall outline-level) 1))
  6568. (goto-char
  6569. (if reversed
  6570. (or (outline-next-heading) (point-max))
  6571. (or (save-excursion (outline-get-next-sibling))
  6572. (org-end-of-subtree t t)
  6573. (point-max))))
  6574. (if (not (bolp)) (newline))
  6575. (bookmark-set "org-refile-last-stored")
  6576. (org-paste-subtree level))))
  6577. (org-cut-subtree)
  6578. (setq org-markers-to-move nil)
  6579. (message "Entry refiled to \"%s\"" (car it)))))))
  6580. (defun org-refile-goto-last-stored ()
  6581. "Go to the location where the last refile was stored."
  6582. (interactive)
  6583. (bookmark-jump "org-refile-last-stored")
  6584. (message "This is the location of the last refile"))
  6585. (defun org-refile-get-location (&optional prompt default-buffer)
  6586. "Prompt the user for a refile location, using PROMPT."
  6587. (let ((org-refile-targets org-refile-targets)
  6588. (org-refile-use-outline-path org-refile-use-outline-path))
  6589. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6590. (unless org-refile-target-table
  6591. (error "No refile targets"))
  6592. (let* ((cbuf (current-buffer))
  6593. (cfunc (if org-refile-use-outline-path
  6594. 'org-olpath-completing-read
  6595. 'completing-read))
  6596. (extra (if org-refile-use-outline-path "/" ""))
  6597. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6598. (fname (and filename (file-truename filename)))
  6599. (tbl (mapcar
  6600. (lambda (x)
  6601. (if (not (equal fname (file-truename (nth 1 x))))
  6602. (cons (concat (car x) extra " ("
  6603. (file-name-nondirectory (nth 1 x)) ")")
  6604. (cdr x))
  6605. (cons (concat (car x) extra) (cdr x))))
  6606. org-refile-target-table))
  6607. (completion-ignore-case t))
  6608. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6609. tbl)))
  6610. (defun org-olpath-completing-read (prompt collection &rest args)
  6611. "Read an outline path like a file name."
  6612. (let ((thetable collection))
  6613. (apply
  6614. 'completing-read prompt
  6615. (lambda (string predicate &optional flag)
  6616. (let (rtn r s f (l (length string)))
  6617. (cond
  6618. ((eq flag nil)
  6619. ;; try completion
  6620. (try-completion string thetable))
  6621. ((eq flag t)
  6622. ;; all-completions
  6623. (setq rtn (all-completions string thetable predicate))
  6624. (mapcar
  6625. (lambda (x)
  6626. (setq r (substring x l))
  6627. (if (string-match " ([^)]*)$" x)
  6628. (setq f (match-string 0 x))
  6629. (setq f ""))
  6630. (if (string-match "/" r)
  6631. (concat string (substring r 0 (match-end 0)) f)
  6632. x))
  6633. rtn))
  6634. ((eq flag 'lambda)
  6635. ;; exact match?
  6636. (assoc string thetable)))
  6637. ))
  6638. args)))
  6639. ;;;; Dynamic blocks
  6640. (defun org-find-dblock (name)
  6641. "Find the first dynamic block with name NAME in the buffer.
  6642. If not found, stay at current position and return nil."
  6643. (let (pos)
  6644. (save-excursion
  6645. (goto-char (point-min))
  6646. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6647. nil t)
  6648. (match-beginning 0))))
  6649. (if pos (goto-char pos))
  6650. pos))
  6651. (defconst org-dblock-start-re
  6652. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6653. "Matches the startline of a dynamic block, with parameters.")
  6654. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6655. "Matches the end of a dyhamic block.")
  6656. (defun org-create-dblock (plist)
  6657. "Create a dynamic block section, with parameters taken from PLIST.
  6658. PLIST must containe a :name entry which is used as name of the block."
  6659. (unless (bolp) (newline))
  6660. (let ((name (plist-get plist :name)))
  6661. (insert "#+BEGIN: " name)
  6662. (while plist
  6663. (if (eq (car plist) :name)
  6664. (setq plist (cddr plist))
  6665. (insert " " (prin1-to-string (pop plist)))))
  6666. (insert "\n\n#+END:\n")
  6667. (beginning-of-line -2)))
  6668. (defun org-prepare-dblock ()
  6669. "Prepare dynamic block for refresh.
  6670. This empties the block, puts the cursor at the insert position and returns
  6671. the property list including an extra property :name with the block name."
  6672. (unless (looking-at org-dblock-start-re)
  6673. (error "Not at a dynamic block"))
  6674. (let* ((begdel (1+ (match-end 0)))
  6675. (name (org-no-properties (match-string 1)))
  6676. (params (append (list :name name)
  6677. (read (concat "(" (match-string 3) ")")))))
  6678. (unless (re-search-forward org-dblock-end-re nil t)
  6679. (error "Dynamic block not terminated"))
  6680. (setq params
  6681. (append params
  6682. (list :content (buffer-substring
  6683. begdel (match-beginning 0)))))
  6684. (delete-region begdel (match-beginning 0))
  6685. (goto-char begdel)
  6686. (open-line 1)
  6687. params))
  6688. (defun org-map-dblocks (&optional command)
  6689. "Apply COMMAND to all dynamic blocks in the current buffer.
  6690. If COMMAND is not given, use `org-update-dblock'."
  6691. (let ((cmd (or command 'org-update-dblock))
  6692. pos)
  6693. (save-excursion
  6694. (goto-char (point-min))
  6695. (while (re-search-forward org-dblock-start-re nil t)
  6696. (goto-char (setq pos (match-beginning 0)))
  6697. (condition-case nil
  6698. (funcall cmd)
  6699. (error (message "Error during update of dynamic block")))
  6700. (goto-char pos)
  6701. (unless (re-search-forward org-dblock-end-re nil t)
  6702. (error "Dynamic block not terminated"))))))
  6703. (defun org-dblock-update (&optional arg)
  6704. "User command for updating dynamic blocks.
  6705. Update the dynamic block at point. With prefix ARG, update all dynamic
  6706. blocks in the buffer."
  6707. (interactive "P")
  6708. (if arg
  6709. (org-update-all-dblocks)
  6710. (or (looking-at org-dblock-start-re)
  6711. (org-beginning-of-dblock))
  6712. (org-update-dblock)))
  6713. (defun org-update-dblock ()
  6714. "Update the dynamic block at point
  6715. This means to empty the block, parse for parameters and then call
  6716. the correct writing function."
  6717. (save-window-excursion
  6718. (let* ((pos (point))
  6719. (line (org-current-line))
  6720. (params (org-prepare-dblock))
  6721. (name (plist-get params :name))
  6722. (cmd (intern (concat "org-dblock-write:" name))))
  6723. (message "Updating dynamic block `%s' at line %d..." name line)
  6724. (funcall cmd params)
  6725. (message "Updating dynamic block `%s' at line %d...done" name line)
  6726. (goto-char pos))))
  6727. (defun org-beginning-of-dblock ()
  6728. "Find the beginning of the dynamic block at point.
  6729. Error if there is no scuh block at point."
  6730. (let ((pos (point))
  6731. beg)
  6732. (end-of-line 1)
  6733. (if (and (re-search-backward org-dblock-start-re nil t)
  6734. (setq beg (match-beginning 0))
  6735. (re-search-forward org-dblock-end-re nil t)
  6736. (> (match-end 0) pos))
  6737. (goto-char beg)
  6738. (goto-char pos)
  6739. (error "Not in a dynamic block"))))
  6740. (defun org-update-all-dblocks ()
  6741. "Update all dynamic blocks in the buffer.
  6742. This function can be used in a hook."
  6743. (when (org-mode-p)
  6744. (org-map-dblocks 'org-update-dblock)))
  6745. ;;;; Completion
  6746. (defconst org-additional-option-like-keywords
  6747. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6748. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6749. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6750. "BEGIN_QUOTE" "END_QUOTE"
  6751. "BEGIN_VERSE" "END_VERSE"
  6752. "BEGIN_SRC" "END_SRC"))
  6753. (defcustom org-structure-template-alist
  6754. '(
  6755. ("s" "#+begin_src ?\n\n#+end_src"
  6756. "<src lang=\"?\">\n\n</src>")
  6757. ("e" "#+begin_example\n?\n#+end_example"
  6758. "<example>\n?\n</example>")
  6759. ("q" "#+begin_quote\n?\n#+end_quote"
  6760. "<quote>\n?\n</quote>")
  6761. ("v" "#+begin_verse\n?\n#+end_verse"
  6762. "<verse>\n?\n/verse>")
  6763. ("l" "#+begin_latex\n?\n#+end_latex"
  6764. "<literal style=\"latex\">\n?\n</literal>")
  6765. ("L" "#+latex: "
  6766. "<literal style=\"latex\">?</literal>")
  6767. ("h" "#+begin_html\n?\n#+end_html"
  6768. "<literal style=\"html\">\n?\n</literal>")
  6769. ("H" "#+html: "
  6770. "<literal style=\"html\">?</literal>")
  6771. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6772. ("A" "#+ascii: ")
  6773. ("i" "#+include %file ?"
  6774. "<include file=%file markup=\"?\">")
  6775. )
  6776. "Structure completion elements.
  6777. This is a list of abbreviation keys and values. The value gets inserted
  6778. it you type @samp{.} followed by the key and then the completion key,
  6779. usually `M-TAB'. %file will be replaced by a file name after prompting
  6780. for the file uning completion.
  6781. There are two templates for each key, the first uses the original Org syntax,
  6782. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6783. the default when the /org-mtags.el/ module has been loaded. See also the
  6784. variable `org-mtags-prefere-muse-templates'.
  6785. This is an experimental feature, it is undecided if it is going to stay in."
  6786. :group 'org-completion
  6787. :type '(repeat
  6788. (string :tag "Key")
  6789. (string :tag "Template")
  6790. (string :tag "Muse Template")))
  6791. (defun org-try-structure-completion ()
  6792. "Try to complete a structure template before point.
  6793. This looks for strings like \"<e\" on an otherwise empty line and
  6794. expands them."
  6795. (let ((l (buffer-substring (point-at-bol) (point)))
  6796. a)
  6797. (when (and (looking-at "[ \t]*$")
  6798. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  6799. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  6800. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  6801. (match-beginning 1)) a)
  6802. t)))
  6803. (defun org-complete-expand-structure-template (start cell)
  6804. "Expand a structure template."
  6805. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  6806. (rpl (nth (if musep 2 1) cell)))
  6807. (delete-region start (point))
  6808. (when (string-match "\\`#\\+" rpl)
  6809. (cond
  6810. ((bolp))
  6811. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6812. (delete-region (point-at-bol) (point)))
  6813. (t (newline))))
  6814. (setq start (point))
  6815. (if (string-match "%file" rpl)
  6816. (setq rpl (replace-match
  6817. (concat
  6818. "\""
  6819. (save-match-data
  6820. (abbreviate-file-name (read-file-name "Include file: ")))
  6821. "\"")
  6822. t t rpl)))
  6823. (insert rpl)
  6824. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6825. (defun org-complete (&optional arg)
  6826. "Perform completion on word at point.
  6827. At the beginning of a headline, this completes TODO keywords as given in
  6828. `org-todo-keywords'.
  6829. If the current word is preceded by a backslash, completes the TeX symbols
  6830. that are supported for HTML support.
  6831. If the current word is preceded by \"#+\", completes special words for
  6832. setting file options.
  6833. In the line after \"#+STARTUP:, complete valid keywords.\"
  6834. At all other locations, this simply calls the value of
  6835. `org-completion-fallback-command'."
  6836. (interactive "P")
  6837. (org-without-partial-completion
  6838. (catch 'exit
  6839. (let* ((a nil)
  6840. (end (point))
  6841. (beg1 (save-excursion
  6842. (skip-chars-backward (org-re "[:alnum:]_@"))
  6843. (point)))
  6844. (beg (save-excursion
  6845. (skip-chars-backward "a-zA-Z0-9_:$")
  6846. (point)))
  6847. (confirm (lambda (x) (stringp (car x))))
  6848. (searchhead (equal (char-before beg) ?*))
  6849. (struct
  6850. (when (and (member (char-before beg1) '(?. ?<))
  6851. (setq a (assoc (buffer-substring beg1 (point))
  6852. org-structure-template-alist)))
  6853. (org-complete-expand-structure-template (1- beg1) a)
  6854. (throw 'exit t)))
  6855. (tag (and (equal (char-before beg1) ?:)
  6856. (equal (char-after (point-at-bol)) ?*)))
  6857. (prop (and (equal (char-before beg1) ?:)
  6858. (not (equal (char-after (point-at-bol)) ?*))))
  6859. (texp (equal (char-before beg) ?\\))
  6860. (link (equal (char-before beg) ?\[))
  6861. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6862. beg)
  6863. "#+"))
  6864. (startup (string-match "^#\\+STARTUP:.*"
  6865. (buffer-substring (point-at-bol) (point))))
  6866. (completion-ignore-case opt)
  6867. (type nil)
  6868. (tbl nil)
  6869. (table (cond
  6870. (opt
  6871. (setq type :opt)
  6872. (require 'org-exp)
  6873. (append
  6874. (mapcar
  6875. (lambda (x)
  6876. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6877. (cons (match-string 2 x) (match-string 1 x)))
  6878. (org-split-string (org-get-current-options) "\n"))
  6879. (mapcar 'list org-additional-option-like-keywords)))
  6880. (startup
  6881. (setq type :startup)
  6882. org-startup-options)
  6883. (link (append org-link-abbrev-alist-local
  6884. org-link-abbrev-alist))
  6885. (texp
  6886. (setq type :tex)
  6887. org-html-entities)
  6888. ((string-match "\\`\\*+[ \t]+\\'"
  6889. (buffer-substring (point-at-bol) beg))
  6890. (setq type :todo)
  6891. (mapcar 'list org-todo-keywords-1))
  6892. (searchhead
  6893. (setq type :searchhead)
  6894. (save-excursion
  6895. (goto-char (point-min))
  6896. (while (re-search-forward org-todo-line-regexp nil t)
  6897. (push (list
  6898. (org-make-org-heading-search-string
  6899. (match-string 3) t))
  6900. tbl)))
  6901. tbl)
  6902. (tag (setq type :tag beg beg1)
  6903. (or org-tag-alist (org-get-buffer-tags)))
  6904. (prop (setq type :prop beg beg1)
  6905. (mapcar 'list (org-buffer-property-keys nil t t)))
  6906. (t (progn
  6907. (call-interactively org-completion-fallback-command)
  6908. (throw 'exit nil)))))
  6909. (pattern (buffer-substring-no-properties beg end))
  6910. (completion (try-completion pattern table confirm)))
  6911. (cond ((eq completion t)
  6912. (if (not (assoc (upcase pattern) table))
  6913. (message "Already complete")
  6914. (if (and (equal type :opt)
  6915. (not (member (car (assoc (upcase pattern) table))
  6916. org-additional-option-like-keywords)))
  6917. (insert (substring (cdr (assoc (upcase pattern) table))
  6918. (length pattern)))
  6919. (if (memq type '(:tag :prop)) (insert ":")))))
  6920. ((null completion)
  6921. (message "Can't find completion for \"%s\"" pattern)
  6922. (ding))
  6923. ((not (string= pattern completion))
  6924. (delete-region beg end)
  6925. (if (string-match " +$" completion)
  6926. (setq completion (replace-match "" t t completion)))
  6927. (insert completion)
  6928. (if (get-buffer-window "*Completions*")
  6929. (delete-window (get-buffer-window "*Completions*")))
  6930. (if (assoc completion table)
  6931. (if (eq type :todo) (insert " ")
  6932. (if (memq type '(:tag :prop)) (insert ":"))))
  6933. (if (and (equal type :opt) (assoc completion table))
  6934. (message "%s" (substitute-command-keys
  6935. "Press \\[org-complete] again to insert example settings"))))
  6936. (t
  6937. (message "Making completion list...")
  6938. (let ((list (sort (all-completions pattern table confirm)
  6939. 'string<)))
  6940. (with-output-to-temp-buffer "*Completions*"
  6941. (condition-case nil
  6942. ;; Protection needed for XEmacs and emacs 21
  6943. (display-completion-list list pattern)
  6944. (error (display-completion-list list)))))
  6945. (message "Making completion list...%s" "done")))))))
  6946. ;;;; TODO, DEADLINE, Comments
  6947. (defun org-toggle-comment ()
  6948. "Change the COMMENT state of an entry."
  6949. (interactive)
  6950. (save-excursion
  6951. (org-back-to-heading)
  6952. (let (case-fold-search)
  6953. (if (looking-at (concat outline-regexp
  6954. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6955. (replace-match "" t t nil 1)
  6956. (if (looking-at outline-regexp)
  6957. (progn
  6958. (goto-char (match-end 0))
  6959. (insert org-comment-string " ")))))))
  6960. (defvar org-last-todo-state-is-todo nil
  6961. "This is non-nil when the last TODO state change led to a TODO state.
  6962. If the last change removed the TODO tag or switched to DONE, then
  6963. this is nil.")
  6964. (defvar org-setting-tags nil) ; dynamically skiped
  6965. (defun org-parse-local-options (string var)
  6966. "Parse STRING for startup setting relevant for variable VAR."
  6967. (let ((rtn (symbol-value var))
  6968. e opts)
  6969. (save-match-data
  6970. (if (or (not string) (not (string-match "\\S-" string)))
  6971. rtn
  6972. (setq opts (delq nil (mapcar (lambda (x)
  6973. (setq e (assoc x org-startup-options))
  6974. (if (eq (nth 1 e) var) e nil))
  6975. (org-split-string string "[ \t]+"))))
  6976. (if (not opts)
  6977. rtn
  6978. (setq rtn nil)
  6979. (while (setq e (pop opts))
  6980. (if (not (nth 3 e))
  6981. (setq rtn (nth 2 e))
  6982. (if (not (listp rtn)) (setq rtn nil))
  6983. (push (nth 2 e) rtn)))
  6984. rtn)))))
  6985. (defvar org-blocker-hook nil
  6986. "Hook for functions that are allowed to block a state change.
  6987. Each function gets as its single argument a property list, see
  6988. `org-trigger-hook' for more information about this list.
  6989. If any of the functions in this hook returns nil, the state change
  6990. is blocked.")
  6991. (defvar org-trigger-hook nil
  6992. "Hook for functions that are triggered by a state change.
  6993. Each function gets as its single argument a property list with at least
  6994. the following elements:
  6995. (:type type-of-change :position pos-at-entry-start
  6996. :from old-state :to new-state)
  6997. Depending on the type, more properties may be present.
  6998. This mechanism is currently implemented for:
  6999. TODO state changes
  7000. ------------------
  7001. :type todo-state-change
  7002. :from previous state (keyword as a string), or nil
  7003. :to new state (keyword as a string), or nil")
  7004. (defun org-todo (&optional arg)
  7005. "Change the TODO state of an item.
  7006. The state of an item is given by a keyword at the start of the heading,
  7007. like
  7008. *** TODO Write paper
  7009. *** DONE Call mom
  7010. The different keywords are specified in the variable `org-todo-keywords'.
  7011. By default the available states are \"TODO\" and \"DONE\".
  7012. So for this example: when the item starts with TODO, it is changed to DONE.
  7013. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7014. DONE are present, add TODO at the beginning of the heading.
  7015. With C-u prefix arg, use completion to determine the new state.
  7016. With numeric prefix arg, switch to that state.
  7017. For calling through lisp, arg is also interpreted in the following way:
  7018. 'none -> empty state
  7019. \"\"(empty string) -> switch to empty state
  7020. 'done -> switch to DONE
  7021. 'nextset -> switch to the next set of keywords
  7022. 'previousset -> switch to the previous set of keywords
  7023. \"WAITING\" -> switch to the specified keyword, but only if it
  7024. really is a member of `org-todo-keywords'."
  7025. (interactive "P")
  7026. (save-excursion
  7027. (catch 'exit
  7028. (org-back-to-heading)
  7029. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7030. (or (looking-at (concat " +" org-todo-regexp " *"))
  7031. (looking-at " *"))
  7032. (let* ((match-data (match-data))
  7033. (startpos (point-at-bol))
  7034. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7035. (org-log-done org-log-done)
  7036. (org-log-repeat org-log-repeat)
  7037. (org-todo-log-states org-todo-log-states)
  7038. (this (match-string 1))
  7039. (hl-pos (match-beginning 0))
  7040. (head (org-get-todo-sequence-head this))
  7041. (ass (assoc head org-todo-kwd-alist))
  7042. (interpret (nth 1 ass))
  7043. (done-word (nth 3 ass))
  7044. (final-done-word (nth 4 ass))
  7045. (last-state (or this ""))
  7046. (completion-ignore-case t)
  7047. (member (member this org-todo-keywords-1))
  7048. (tail (cdr member))
  7049. (state (cond
  7050. ((and org-todo-key-trigger
  7051. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7052. (and (not arg) org-use-fast-todo-selection
  7053. (not (eq org-use-fast-todo-selection 'prefix)))))
  7054. ;; Use fast selection
  7055. (org-fast-todo-selection))
  7056. ((and (equal arg '(4))
  7057. (or (not org-use-fast-todo-selection)
  7058. (not org-todo-key-trigger)))
  7059. ;; Read a state with completion
  7060. (completing-read "State: " (mapcar (lambda(x) (list x))
  7061. org-todo-keywords-1)
  7062. nil t))
  7063. ((eq arg 'right)
  7064. (if this
  7065. (if tail (car tail) nil)
  7066. (car org-todo-keywords-1)))
  7067. ((eq arg 'left)
  7068. (if (equal member org-todo-keywords-1)
  7069. nil
  7070. (if this
  7071. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7072. org-todo-keywords-1)
  7073. (org-last org-todo-keywords-1))))
  7074. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7075. (setq arg nil))) ; hack to fall back to cycling
  7076. (arg
  7077. ;; user or caller requests a specific state
  7078. (cond
  7079. ((equal arg "") nil)
  7080. ((eq arg 'none) nil)
  7081. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7082. ((eq arg 'nextset)
  7083. (or (car (cdr (member head org-todo-heads)))
  7084. (car org-todo-heads)))
  7085. ((eq arg 'previousset)
  7086. (let ((org-todo-heads (reverse org-todo-heads)))
  7087. (or (car (cdr (member head org-todo-heads)))
  7088. (car org-todo-heads))))
  7089. ((car (member arg org-todo-keywords-1)))
  7090. ((nth (1- (prefix-numeric-value arg))
  7091. org-todo-keywords-1))))
  7092. ((null member) (or head (car org-todo-keywords-1)))
  7093. ((equal this final-done-word) nil) ;; -> make empty
  7094. ((null tail) nil) ;; -> first entry
  7095. ((eq interpret 'sequence)
  7096. (car tail))
  7097. ((memq interpret '(type priority))
  7098. (if (eq this-command last-command)
  7099. (car tail)
  7100. (if (> (length tail) 0)
  7101. (or done-word (car org-done-keywords))
  7102. nil)))
  7103. (t nil)))
  7104. (next (if state (concat " " state " ") " "))
  7105. (change-plist (list :type 'todo-state-change :from this :to state
  7106. :position startpos))
  7107. dolog now-done-p)
  7108. (when org-blocker-hook
  7109. (unless (save-excursion
  7110. (save-match-data
  7111. (run-hook-with-args-until-failure
  7112. 'org-blocker-hook change-plist)))
  7113. (if (interactive-p)
  7114. (error "TODO state change from %s to %s blocked" this state)
  7115. ;; fail silently
  7116. (message "TODO state change from %s to %s blocked" this state)
  7117. (throw 'exit nil))))
  7118. (store-match-data match-data)
  7119. (replace-match next t t)
  7120. (unless (pos-visible-in-window-p hl-pos)
  7121. (message "TODO state changed to %s" (org-trim next)))
  7122. (unless head
  7123. (setq head (org-get-todo-sequence-head state)
  7124. ass (assoc head org-todo-kwd-alist)
  7125. interpret (nth 1 ass)
  7126. done-word (nth 3 ass)
  7127. final-done-word (nth 4 ass)))
  7128. (when (memq arg '(nextset previousset))
  7129. (message "Keyword-Set %d/%d: %s"
  7130. (- (length org-todo-sets) -1
  7131. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7132. (length org-todo-sets)
  7133. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7134. (setq org-last-todo-state-is-todo
  7135. (not (member state org-done-keywords)))
  7136. (setq now-done-p (and (member state org-done-keywords)
  7137. (not (member this org-done-keywords))))
  7138. (and logging (org-local-logging logging))
  7139. (when (and (or org-todo-log-states org-log-done)
  7140. (not (memq arg '(nextset previousset))))
  7141. ;; we need to look at recording a time and note
  7142. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7143. (nth 2 (assoc this org-todo-log-states))))
  7144. (when (and state
  7145. (member state org-not-done-keywords)
  7146. (not (member this org-not-done-keywords)))
  7147. ;; This is now a todo state and was not one before
  7148. ;; If there was a CLOSED time stamp, get rid of it.
  7149. (org-add-planning-info nil nil 'closed))
  7150. (when (and now-done-p org-log-done)
  7151. ;; It is now done, and it was not done before
  7152. (org-add-planning-info 'closed (org-current-time))
  7153. (if (and (not dolog) (eq 'note org-log-done))
  7154. (org-add-log-setup 'done state 'findpos 'note)))
  7155. (when (and state dolog)
  7156. ;; This is a non-nil state, and we need to log it
  7157. (org-add-log-setup 'state state 'findpos dolog)))
  7158. ;; Fixup tag positioning
  7159. (org-todo-trigger-tag-changes state)
  7160. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7161. (when org-provide-todo-statistics
  7162. (org-update-parent-todo-statistics))
  7163. (run-hooks 'org-after-todo-state-change-hook)
  7164. (if (and arg (not (member state org-done-keywords)))
  7165. (setq head (org-get-todo-sequence-head state)))
  7166. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7167. ;; Do we need to trigger a repeat?
  7168. (when now-done-p (org-auto-repeat-maybe state))
  7169. ;; Fixup cursor location if close to the keyword
  7170. (if (and (outline-on-heading-p)
  7171. (not (bolp))
  7172. (save-excursion (beginning-of-line 1)
  7173. (looking-at org-todo-line-regexp))
  7174. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7175. (progn
  7176. (goto-char (or (match-end 2) (match-end 1)))
  7177. (just-one-space)))
  7178. (when org-trigger-hook
  7179. (save-excursion
  7180. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7181. (defun org-update-parent-todo-statistics ()
  7182. "Update any statistics cookie in the parent of the current headline."
  7183. (interactive)
  7184. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7185. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7186. (catch 'exit
  7187. (save-excursion
  7188. (setq level (org-up-heading-safe))
  7189. (unless (and level
  7190. (re-search-forward box-re (point-at-eol) t))
  7191. (throw 'exit nil))
  7192. (setq is-percent (match-end 2))
  7193. (save-match-data
  7194. (unless (outline-next-heading) (throw 'exit nil))
  7195. (while (looking-at org-todo-line-regexp)
  7196. (setq kwd (match-string 2))
  7197. (and kwd (setq cnt-all (1+ cnt-all)))
  7198. (and (member kwd org-done-keywords)
  7199. (setq cnt-done (1+ cnt-done)))
  7200. (condition-case nil
  7201. (org-forward-same-level 1)
  7202. (error (end-of-line 1)))))
  7203. (replace-match
  7204. (if is-percent
  7205. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7206. (format "[%d/%d]" cnt-done cnt-all)))
  7207. (run-hook-with-args 'org-after-todo-statistics-hook
  7208. cnt-done (- cnt-all cnt-done))))))
  7209. (defvar org-after-todo-statistics-hook nil
  7210. "Hook that is called after a TODO statistics cookie has been updated.
  7211. Each function is called with two arguments: the number of not-done entries
  7212. and the number of done entries.
  7213. For example, the following function, when added to this hook, will switch
  7214. an entry to DONE when all children are done, and back to TODO when new
  7215. entries are set to a TODO status. Note that this hook is only called
  7216. when there is a statistics cookie in the headline!
  7217. (defun org-summary-todo (n-done n-not-done)
  7218. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7219. (let (org-log-done org-log-states) ; turn off logging
  7220. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7221. ")
  7222. (defun org-todo-trigger-tag-changes (state)
  7223. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7224. (let ((l org-todo-state-tags-triggers)
  7225. changes)
  7226. (when (or (not state) (equal state ""))
  7227. (setq changes (append changes (cdr (assoc "" l)))))
  7228. (when (and (stringp state) (> (length state) 0))
  7229. (setq changes (append changes (cdr (assoc state l)))))
  7230. (when (member state org-not-done-keywords)
  7231. (setq changes (append changes (cdr (assoc 'todo l)))))
  7232. (when (member state org-done-keywords)
  7233. (setq changes (append changes (cdr (assoc 'done l)))))
  7234. (dolist (c changes)
  7235. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7236. (defun org-local-logging (value)
  7237. "Get logging settings from a property VALUE."
  7238. (let* (words w a)
  7239. ;; directly set the variables, they are already local.
  7240. (setq org-log-done nil
  7241. org-log-repeat nil
  7242. org-todo-log-states nil)
  7243. (setq words (org-split-string value))
  7244. (while (setq w (pop words))
  7245. (cond
  7246. ((setq a (assoc w org-startup-options))
  7247. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7248. (set (nth 1 a) (nth 2 a))))
  7249. ((setq a (org-extract-log-state-settings w))
  7250. (and (member (car a) org-todo-keywords-1)
  7251. (push a org-todo-log-states)))))))
  7252. (defun org-get-todo-sequence-head (kwd)
  7253. "Return the head of the TODO sequence to which KWD belongs.
  7254. If KWD is not set, check if there is a text property remembering the
  7255. right sequence."
  7256. (let (p)
  7257. (cond
  7258. ((not kwd)
  7259. (or (get-text-property (point-at-bol) 'org-todo-head)
  7260. (progn
  7261. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7262. nil (point-at-eol)))
  7263. (get-text-property p 'org-todo-head))))
  7264. ((not (member kwd org-todo-keywords-1))
  7265. (car org-todo-keywords-1))
  7266. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7267. (defun org-fast-todo-selection ()
  7268. "Fast TODO keyword selection with single keys.
  7269. Returns the new TODO keyword, or nil if no state change should occur."
  7270. (let* ((fulltable org-todo-key-alist)
  7271. (done-keywords org-done-keywords) ;; needed for the faces.
  7272. (maxlen (apply 'max (mapcar
  7273. (lambda (x)
  7274. (if (stringp (car x)) (string-width (car x)) 0))
  7275. fulltable)))
  7276. (expert nil)
  7277. (fwidth (+ maxlen 3 1 3))
  7278. (ncol (/ (- (window-width) 4) fwidth))
  7279. tg cnt e c tbl
  7280. groups ingroup)
  7281. (save-window-excursion
  7282. (if expert
  7283. (set-buffer (get-buffer-create " *Org todo*"))
  7284. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7285. (erase-buffer)
  7286. (org-set-local 'org-done-keywords done-keywords)
  7287. (setq tbl fulltable cnt 0)
  7288. (while (setq e (pop tbl))
  7289. (cond
  7290. ((equal e '(:startgroup))
  7291. (push '() groups) (setq ingroup t)
  7292. (when (not (= cnt 0))
  7293. (setq cnt 0)
  7294. (insert "\n"))
  7295. (insert "{ "))
  7296. ((equal e '(:endgroup))
  7297. (setq ingroup nil cnt 0)
  7298. (insert "}\n"))
  7299. (t
  7300. (setq tg (car e) c (cdr e))
  7301. (if ingroup (push tg (car groups)))
  7302. (setq tg (org-add-props tg nil 'face
  7303. (org-get-todo-face tg)))
  7304. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7305. (insert "[" c "] " tg (make-string
  7306. (- fwidth 4 (length tg)) ?\ ))
  7307. (when (= (setq cnt (1+ cnt)) ncol)
  7308. (insert "\n")
  7309. (if ingroup (insert " "))
  7310. (setq cnt 0)))))
  7311. (insert "\n")
  7312. (goto-char (point-min))
  7313. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7314. (fit-window-to-buffer))
  7315. (message "[a-z..]:Set [SPC]:clear")
  7316. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7317. (cond
  7318. ((or (= c ?\C-g)
  7319. (and (= c ?q) (not (rassoc c fulltable))))
  7320. (setq quit-flag t))
  7321. ((= c ?\ ) nil)
  7322. ((setq e (rassoc c fulltable) tg (car e))
  7323. tg)
  7324. (t (setq quit-flag t))))))
  7325. (defun org-entry-is-todo-p ()
  7326. (member (org-get-todo-state) org-not-done-keywords))
  7327. (defun org-entry-is-done-p ()
  7328. (member (org-get-todo-state) org-done-keywords))
  7329. (defun org-get-todo-state ()
  7330. (save-excursion
  7331. (org-back-to-heading t)
  7332. (and (looking-at org-todo-line-regexp)
  7333. (match-end 2)
  7334. (match-string 2))))
  7335. (defun org-at-date-range-p (&optional inactive-ok)
  7336. "Is the cursor inside a date range?"
  7337. (interactive)
  7338. (save-excursion
  7339. (catch 'exit
  7340. (let ((pos (point)))
  7341. (skip-chars-backward "^[<\r\n")
  7342. (skip-chars-backward "<[")
  7343. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7344. (>= (match-end 0) pos)
  7345. (throw 'exit t))
  7346. (skip-chars-backward "^<[\r\n")
  7347. (skip-chars-backward "<[")
  7348. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7349. (>= (match-end 0) pos)
  7350. (throw 'exit t)))
  7351. nil)))
  7352. (defun org-get-repeat ()
  7353. "Check if there is a deadline/schedule with repeater in this entry."
  7354. (save-match-data
  7355. (save-excursion
  7356. (org-back-to-heading t)
  7357. (if (re-search-forward
  7358. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7359. (match-string 1)))))
  7360. (defvar org-last-changed-timestamp)
  7361. (defvar org-last-inserted-timestamp)
  7362. (defvar org-log-post-message)
  7363. (defvar org-log-note-purpose)
  7364. (defvar org-log-note-how)
  7365. (defvar org-log-note-extra)
  7366. (defun org-auto-repeat-maybe (done-word)
  7367. "Check if the current headline contains a repeated deadline/schedule.
  7368. If yes, set TODO state back to what it was and change the base date
  7369. of repeating deadline/scheduled time stamps to new date.
  7370. This function is run automatically after each state change to a DONE state."
  7371. ;; last-state is dynamically scoped into this function
  7372. (let* ((repeat (org-get-repeat))
  7373. (aa (assoc last-state org-todo-kwd-alist))
  7374. (interpret (nth 1 aa))
  7375. (head (nth 2 aa))
  7376. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7377. (msg "Entry repeats: ")
  7378. (org-log-done nil)
  7379. (org-todo-log-states nil)
  7380. (nshiftmax 10) (nshift 0)
  7381. re type n what ts mb0 time)
  7382. (when repeat
  7383. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7384. (org-todo (if (eq interpret 'type) last-state head))
  7385. (when org-log-repeat
  7386. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7387. (memq 'org-add-log-note post-command-hook))
  7388. ;; OK, we are already setup for some record
  7389. (if (eq org-log-repeat 'note)
  7390. ;; make sure we take a note, not only a time stamp
  7391. (setq org-log-note-how 'note))
  7392. ;; Set up for taking a record
  7393. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7394. 'findpos org-log-repeat)))
  7395. (org-back-to-heading t)
  7396. (org-add-planning-info nil nil 'closed)
  7397. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7398. org-deadline-time-regexp "\\)\\|\\("
  7399. org-ts-regexp "\\)"))
  7400. (while (re-search-forward
  7401. re (save-excursion (outline-next-heading) (point)) t)
  7402. (setq type (if (match-end 1) org-scheduled-string
  7403. (if (match-end 3) org-deadline-string "Plain:"))
  7404. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7405. mb0 (match-beginning 0))
  7406. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7407. (setq n (string-to-number (match-string 2 ts))
  7408. what (match-string 3 ts))
  7409. (if (equal what "w") (setq n (* n 7) what "d"))
  7410. ;; Preparation, see if we need to modify the start date for the change
  7411. (when (match-end 1)
  7412. (setq time (save-match-data (org-time-string-to-time ts)))
  7413. (cond
  7414. ((equal (match-string 1 ts) ".")
  7415. ;; Shift starting date to today
  7416. (org-timestamp-change
  7417. (- (time-to-days (current-time)) (time-to-days time))
  7418. 'day))
  7419. ((equal (match-string 1 ts) "+")
  7420. (while (or (= nshift 0)
  7421. (<= (time-to-days time) (time-to-days (current-time))))
  7422. (when (= (incf nshift) nshiftmax)
  7423. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7424. (error "Abort")))
  7425. (org-timestamp-change n (cdr (assoc what whata)))
  7426. (org-at-timestamp-p t)
  7427. (setq ts (match-string 1))
  7428. (setq time (save-match-data (org-time-string-to-time ts))))
  7429. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7430. ;; rematch, so that we have everything in place for the real shift
  7431. (org-at-timestamp-p t)
  7432. (setq ts (match-string 1))
  7433. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7434. (org-timestamp-change n (cdr (assoc what whata)))
  7435. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7436. (setq org-log-post-message msg)
  7437. (message "%s" msg))))
  7438. (defun org-show-todo-tree (arg)
  7439. "Make a compact tree which shows all headlines marked with TODO.
  7440. The tree will show the lines where the regexp matches, and all higher
  7441. headlines above the match.
  7442. With a \\[universal-argument] prefix, also show the DONE entries.
  7443. With a numeric prefix N, construct a sparse tree for the Nth element
  7444. of `org-todo-keywords-1'."
  7445. (interactive "P")
  7446. (let ((case-fold-search nil)
  7447. (kwd-re
  7448. (cond ((null arg) org-not-done-regexp)
  7449. ((equal arg '(4))
  7450. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7451. (mapcar 'list org-todo-keywords-1))))
  7452. (concat "\\("
  7453. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7454. "\\)\\>")))
  7455. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7456. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7457. org-todo-keywords-1)))
  7458. (t (error "Invalid prefix argument: %s" arg)))))
  7459. (message "%d TODO entries found"
  7460. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7461. (defun org-deadline (&optional remove time)
  7462. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7463. With argument REMOVE, remove any deadline from the item.
  7464. When TIME is set, it should be an internal time specification, and the
  7465. scheduling will use the corresponding date."
  7466. (interactive "P")
  7467. (if remove
  7468. (progn
  7469. (org-remove-timestamp-with-keyword org-deadline-string)
  7470. (message "Item no longer has a deadline."))
  7471. (if (org-get-repeat)
  7472. (error "Cannot change deadline on task with repeater, please do that by hand")
  7473. (org-add-planning-info 'deadline time 'closed)
  7474. (message "Deadline on %s" org-last-inserted-timestamp))))
  7475. (defun org-schedule (&optional remove time)
  7476. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7477. With argument REMOVE, remove any scheduling date from the item.
  7478. When TIME is set, it should be an internal time specification, and the
  7479. scheduling will use the corresponding date."
  7480. (interactive "P")
  7481. (if remove
  7482. (progn
  7483. (org-remove-timestamp-with-keyword org-scheduled-string)
  7484. (message "Item is no longer scheduled."))
  7485. (if (org-get-repeat)
  7486. (error "Cannot reschedule task with repeater, please do that by hand")
  7487. (org-add-planning-info 'scheduled time 'closed)
  7488. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7489. (defun org-remove-timestamp-with-keyword (keyword)
  7490. "Remove all time stamps with KEYWORD in the current entry."
  7491. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7492. beg)
  7493. (save-excursion
  7494. (org-back-to-heading t)
  7495. (setq beg (point))
  7496. (org-end-of-subtree t t)
  7497. (while (re-search-backward re beg t)
  7498. (replace-match "")
  7499. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7500. (equal (char-before) ?\ ))
  7501. (backward-delete-char 1)
  7502. (if (string-match "^[ \t]*$" (buffer-substring
  7503. (point-at-bol) (point-at-eol)))
  7504. (delete-region (point-at-bol)
  7505. (min (point-max) (1+ (point-at-eol))))))))))
  7506. (defun org-add-planning-info (what &optional time &rest remove)
  7507. "Insert new timestamp with keyword in the line directly after the headline.
  7508. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7509. If non is given, the user is prompted for a date.
  7510. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7511. be removed."
  7512. (interactive)
  7513. (let (org-time-was-given org-end-time-was-given ts
  7514. end default-time default-input)
  7515. (when (and (not time) (memq what '(scheduled deadline)))
  7516. ;; Try to get a default date/time from existing timestamp
  7517. (save-excursion
  7518. (org-back-to-heading t)
  7519. (setq end (save-excursion (outline-next-heading) (point)))
  7520. (when (re-search-forward (if (eq what 'scheduled)
  7521. org-scheduled-time-regexp
  7522. org-deadline-time-regexp)
  7523. end t)
  7524. (setq ts (match-string 1)
  7525. default-time
  7526. (apply 'encode-time (org-parse-time-string ts))
  7527. default-input (and ts (org-get-compact-tod ts))))))
  7528. (when what
  7529. ;; If necessary, get the time from the user
  7530. (setq time (or time (org-read-date nil 'to-time nil nil
  7531. default-time default-input))))
  7532. (when (and org-insert-labeled-timestamps-at-point
  7533. (member what '(scheduled deadline)))
  7534. (insert
  7535. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7536. (org-insert-time-stamp time org-time-was-given
  7537. nil nil nil (list org-end-time-was-given))
  7538. (setq what nil))
  7539. (save-excursion
  7540. (save-restriction
  7541. (let (col list elt ts buffer-invisibility-spec)
  7542. (org-back-to-heading t)
  7543. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7544. (goto-char (match-end 1))
  7545. (setq col (current-column))
  7546. (goto-char (match-end 0))
  7547. (if (eobp) (insert "\n") (forward-char 1))
  7548. (if (and (not (looking-at outline-regexp))
  7549. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7550. "[^\r\n]*"))
  7551. (not (equal (match-string 1) org-clock-string)))
  7552. (narrow-to-region (match-beginning 0) (match-end 0))
  7553. (insert-before-markers "\n")
  7554. (backward-char 1)
  7555. (narrow-to-region (point) (point))
  7556. (and org-adapt-indentation (org-indent-to-column col)))
  7557. ;; Check if we have to remove something.
  7558. (setq list (cons what remove))
  7559. (while list
  7560. (setq elt (pop list))
  7561. (goto-char (point-min))
  7562. (when (or (and (eq elt 'scheduled)
  7563. (re-search-forward org-scheduled-time-regexp nil t))
  7564. (and (eq elt 'deadline)
  7565. (re-search-forward org-deadline-time-regexp nil t))
  7566. (and (eq elt 'closed)
  7567. (re-search-forward org-closed-time-regexp nil t)))
  7568. (replace-match "")
  7569. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7570. (if (looking-at " +") (replace-match ""))))
  7571. (goto-char (point-max))
  7572. (when what
  7573. (insert
  7574. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7575. (cond ((eq what 'scheduled) org-scheduled-string)
  7576. ((eq what 'deadline) org-deadline-string)
  7577. ((eq what 'closed) org-closed-string))
  7578. " ")
  7579. (setq ts (org-insert-time-stamp
  7580. time
  7581. (or org-time-was-given
  7582. (and (eq what 'closed) org-log-done-with-time))
  7583. (eq what 'closed)
  7584. nil nil (list org-end-time-was-given)))
  7585. (end-of-line 1))
  7586. (goto-char (point-min))
  7587. (widen)
  7588. (if (and (looking-at "[ \t]+\n")
  7589. (equal (char-before) ?\n))
  7590. (delete-region (1- (point)) (point-at-eol)))
  7591. ts)))))
  7592. (defvar org-log-note-marker (make-marker))
  7593. (defvar org-log-note-purpose nil)
  7594. (defvar org-log-note-state nil)
  7595. (defvar org-log-note-how nil)
  7596. (defvar org-log-note-extra nil)
  7597. (defvar org-log-note-window-configuration nil)
  7598. (defvar org-log-note-return-to (make-marker))
  7599. (defvar org-log-post-message nil
  7600. "Message to be displayed after a log note has been stored.
  7601. The auto-repeater uses this.")
  7602. (defun org-add-note ()
  7603. "Add a note to the current entry.
  7604. This is done in the same way as adding a state change note."
  7605. (interactive)
  7606. (org-add-log-setup 'note nil 'findpos nil))
  7607. (defvar org-property-end-re)
  7608. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7609. "Set up the post command hook to take a note.
  7610. If this is about to TODO state change, the new state is expected in STATE.
  7611. When FINDPOS is non-nil, find the correct position for the note in
  7612. the current entry. If not, assume that it can be inserted at point.
  7613. HOW is an indicator what kind of note should be created.
  7614. EXTRA is additional text that will be inserted into the notes buffer."
  7615. (save-restriction
  7616. (save-excursion
  7617. (when findpos
  7618. (org-back-to-heading t)
  7619. (narrow-to-region (point) (save-excursion
  7620. (outline-next-heading) (point)))
  7621. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7622. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7623. "[^\r\n]*\\)?"))
  7624. (goto-char (match-end 0))
  7625. (when (and org-log-state-notes-insert-after-drawers
  7626. (save-excursion
  7627. (forward-line) (looking-at org-drawer-regexp)))
  7628. (progn (forward-line)
  7629. (while (looking-at org-drawer-regexp)
  7630. (goto-char (match-end 0))
  7631. (re-search-forward org-property-end-re (point-max) t)
  7632. (forward-line))
  7633. (forward-line -1)))
  7634. (unless org-log-states-order-reversed
  7635. (and (= (char-after) ?\n) (forward-char 1))
  7636. (org-skip-over-state-notes)
  7637. (skip-chars-backward " \t\n\r")))
  7638. (move-marker org-log-note-marker (point))
  7639. (setq org-log-note-purpose purpose
  7640. org-log-note-state state
  7641. org-log-note-how how
  7642. org-log-note-extra extra)
  7643. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7644. (defun org-skip-over-state-notes ()
  7645. "Skip past the list of State notes in an entry."
  7646. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7647. (while (looking-at "[ \t]*- State")
  7648. (condition-case nil
  7649. (org-next-item)
  7650. (error (org-end-of-item)))))
  7651. (defun org-add-log-note (&optional purpose)
  7652. "Pop up a window for taking a note, and add this note later at point."
  7653. (remove-hook 'post-command-hook 'org-add-log-note)
  7654. (setq org-log-note-window-configuration (current-window-configuration))
  7655. (delete-other-windows)
  7656. (move-marker org-log-note-return-to (point))
  7657. (switch-to-buffer (marker-buffer org-log-note-marker))
  7658. (goto-char org-log-note-marker)
  7659. (org-switch-to-buffer-other-window "*Org Note*")
  7660. (erase-buffer)
  7661. (if (memq org-log-note-how '(time state))
  7662. (org-store-log-note)
  7663. (let ((org-inhibit-startup t)) (org-mode))
  7664. (insert (format "# Insert note for %s.
  7665. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7666. (cond
  7667. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7668. ((eq org-log-note-purpose 'done) "closed todo item")
  7669. ((eq org-log-note-purpose 'state)
  7670. (format "state change to \"%s\"" org-log-note-state))
  7671. ((eq org-log-note-purpose 'note)
  7672. "this entry")
  7673. (t (error "This should not happen")))))
  7674. (if org-log-note-extra (insert org-log-note-extra))
  7675. (org-set-local 'org-finish-function 'org-store-log-note)))
  7676. (defvar org-note-abort nil) ; dynamically scoped
  7677. (defun org-store-log-note ()
  7678. "Finish taking a log note, and insert it to where it belongs."
  7679. (let ((txt (buffer-string))
  7680. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7681. lines ind)
  7682. (kill-buffer (current-buffer))
  7683. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7684. (setq txt (replace-match "" t t txt)))
  7685. (if (string-match "\\s-+\\'" txt)
  7686. (setq txt (replace-match "" t t txt)))
  7687. (setq lines (org-split-string txt "\n"))
  7688. (when (and note (string-match "\\S-" note))
  7689. (setq note
  7690. (org-replace-escapes
  7691. note
  7692. (list (cons "%u" (user-login-name))
  7693. (cons "%U" user-full-name)
  7694. (cons "%t" (format-time-string
  7695. (org-time-stamp-format 'long 'inactive)
  7696. (current-time)))
  7697. (cons "%s" (if org-log-note-state
  7698. (concat "\"" org-log-note-state "\"")
  7699. "")))))
  7700. (if lines (setq note (concat note " \\\\")))
  7701. (push note lines))
  7702. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7703. (when lines
  7704. (save-excursion
  7705. (set-buffer (marker-buffer org-log-note-marker))
  7706. (save-excursion
  7707. (goto-char org-log-note-marker)
  7708. (move-marker org-log-note-marker nil)
  7709. (end-of-line 1)
  7710. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7711. (indent-relative nil)
  7712. (insert "- " (pop lines))
  7713. (org-indent-line-function)
  7714. (beginning-of-line 1)
  7715. (looking-at "[ \t]*")
  7716. (setq ind (concat (match-string 0) " "))
  7717. (end-of-line 1)
  7718. (while lines (insert "\n" ind (pop lines)))))))
  7719. (set-window-configuration org-log-note-window-configuration)
  7720. (with-current-buffer (marker-buffer org-log-note-return-to)
  7721. (goto-char org-log-note-return-to))
  7722. (move-marker org-log-note-return-to nil)
  7723. (and org-log-post-message (message "%s" org-log-post-message)))
  7724. (defun org-sparse-tree (&optional arg)
  7725. "Create a sparse tree, prompt for the details.
  7726. This command can create sparse trees. You first need to select the type
  7727. of match used to create the tree:
  7728. t Show entries with a specific TODO keyword.
  7729. T Show entries selected by a tags match.
  7730. p Enter a property name and its value (both with completion on existing
  7731. names/values) and show entries with that property.
  7732. r Show entries matching a regular expression
  7733. d Show deadlines due within `org-deadline-warning-days'."
  7734. (interactive "P")
  7735. (let (ans kwd value)
  7736. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7737. (setq ans (read-char-exclusive))
  7738. (cond
  7739. ((equal ans ?d)
  7740. (call-interactively 'org-check-deadlines))
  7741. ((equal ans ?b)
  7742. (call-interactively 'org-check-before-date))
  7743. ((equal ans ?t)
  7744. (org-show-todo-tree '(4)))
  7745. ((equal ans ?T)
  7746. (call-interactively 'org-tags-sparse-tree))
  7747. ((member ans '(?p ?P))
  7748. (setq kwd (completing-read "Property: "
  7749. (mapcar 'list (org-buffer-property-keys))))
  7750. (setq value (completing-read "Value: "
  7751. (mapcar 'list (org-property-values kwd))))
  7752. (unless (string-match "\\`{.*}\\'" value)
  7753. (setq value (concat "\"" value "\"")))
  7754. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7755. ((member ans '(?r ?R ?/))
  7756. (call-interactively 'org-occur))
  7757. (t (error "No such sparse tree command \"%c\"" ans)))))
  7758. (defvar org-occur-highlights nil
  7759. "List of overlays used for occur matches.")
  7760. (make-variable-buffer-local 'org-occur-highlights)
  7761. (defvar org-occur-parameters nil
  7762. "Parameters of the active org-occur calls.
  7763. This is a list, each call to org-occur pushes as cons cell,
  7764. containing the regular expression and the callback, onto the list.
  7765. The list can contain several entries if `org-occur' has been called
  7766. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7767. will only contain one set of parameters. When the highlights are
  7768. removed (for example with `C-c C-c', or with the next edit (depending
  7769. on `org-remove-highlights-with-change'), this variable is emptied
  7770. as well.")
  7771. (make-variable-buffer-local 'org-occur-parameters)
  7772. (defun org-occur (regexp &optional keep-previous callback)
  7773. "Make a compact tree which shows all matches of REGEXP.
  7774. The tree will show the lines where the regexp matches, and all higher
  7775. headlines above the match. It will also show the heading after the match,
  7776. to make sure editing the matching entry is easy.
  7777. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7778. call to `org-occur' will be kept, to allow stacking of calls to this
  7779. command.
  7780. If CALLBACK is non-nil, it is a function which is called to confirm
  7781. that the match should indeed be shown."
  7782. (interactive "sRegexp: \nP")
  7783. (unless keep-previous
  7784. (org-remove-occur-highlights nil nil t))
  7785. (push (cons regexp callback) org-occur-parameters)
  7786. (let ((cnt 0))
  7787. (save-excursion
  7788. (goto-char (point-min))
  7789. (if (or (not keep-previous) ; do not want to keep
  7790. (not org-occur-highlights)) ; no previous matches
  7791. ;; hide everything
  7792. (org-overview))
  7793. (while (re-search-forward regexp nil t)
  7794. (when (or (not callback)
  7795. (save-match-data (funcall callback)))
  7796. (setq cnt (1+ cnt))
  7797. (when org-highlight-sparse-tree-matches
  7798. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7799. (org-show-context 'occur-tree))))
  7800. (when org-remove-highlights-with-change
  7801. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7802. nil 'local))
  7803. (unless org-sparse-tree-open-archived-trees
  7804. (org-hide-archived-subtrees (point-min) (point-max)))
  7805. (run-hooks 'org-occur-hook)
  7806. (if (interactive-p)
  7807. (message "%d match(es) for regexp %s" cnt regexp))
  7808. cnt))
  7809. (defun org-show-context (&optional key)
  7810. "Make sure point and context and visible.
  7811. How much context is shown depends upon the variables
  7812. `org-show-hierarchy-above', `org-show-following-heading'. and
  7813. `org-show-siblings'."
  7814. (let ((heading-p (org-on-heading-p t))
  7815. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7816. (following-p (org-get-alist-option org-show-following-heading key))
  7817. (entry-p (org-get-alist-option org-show-entry-below key))
  7818. (siblings-p (org-get-alist-option org-show-siblings key)))
  7819. (catch 'exit
  7820. ;; Show heading or entry text
  7821. (if (and heading-p (not entry-p))
  7822. (org-flag-heading nil) ; only show the heading
  7823. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7824. (org-show-hidden-entry))) ; show entire entry
  7825. (when following-p
  7826. ;; Show next sibling, or heading below text
  7827. (save-excursion
  7828. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7829. (org-flag-heading nil))))
  7830. (when siblings-p (org-show-siblings))
  7831. (when hierarchy-p
  7832. ;; show all higher headings, possibly with siblings
  7833. (save-excursion
  7834. (while (and (condition-case nil
  7835. (progn (org-up-heading-all 1) t)
  7836. (error nil))
  7837. (not (bobp)))
  7838. (org-flag-heading nil)
  7839. (when siblings-p (org-show-siblings))))))))
  7840. (defun org-reveal (&optional siblings)
  7841. "Show current entry, hierarchy above it, and the following headline.
  7842. This can be used to show a consistent set of context around locations
  7843. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7844. not t for the search context.
  7845. With optional argument SIBLINGS, on each level of the hierarchy all
  7846. siblings are shown. This repairs the tree structure to what it would
  7847. look like when opened with hierarchical calls to `org-cycle'."
  7848. (interactive "P")
  7849. (let ((org-show-hierarchy-above t)
  7850. (org-show-following-heading t)
  7851. (org-show-siblings (if siblings t org-show-siblings)))
  7852. (org-show-context nil)))
  7853. (defun org-highlight-new-match (beg end)
  7854. "Highlight from BEG to END and mark the highlight is an occur headline."
  7855. (let ((ov (org-make-overlay beg end)))
  7856. (org-overlay-put ov 'face 'secondary-selection)
  7857. (push ov org-occur-highlights)))
  7858. (defun org-remove-occur-highlights (&optional beg end noremove)
  7859. "Remove the occur highlights from the buffer.
  7860. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7861. from the `before-change-functions' in the current buffer."
  7862. (interactive)
  7863. (unless org-inhibit-highlight-removal
  7864. (mapc 'org-delete-overlay org-occur-highlights)
  7865. (setq org-occur-highlights nil)
  7866. (setq org-occur-parameters nil)
  7867. (unless noremove
  7868. (remove-hook 'before-change-functions
  7869. 'org-remove-occur-highlights 'local))))
  7870. ;;;; Priorities
  7871. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7872. "Regular expression matching the priority indicator.")
  7873. (defvar org-remove-priority-next-time nil)
  7874. (defun org-priority-up ()
  7875. "Increase the priority of the current item."
  7876. (interactive)
  7877. (org-priority 'up))
  7878. (defun org-priority-down ()
  7879. "Decrease the priority of the current item."
  7880. (interactive)
  7881. (org-priority 'down))
  7882. (defun org-priority (&optional action)
  7883. "Change the priority of an item by ARG.
  7884. ACTION can be `set', `up', `down', or a character."
  7885. (interactive)
  7886. (setq action (or action 'set))
  7887. (let (current new news have remove)
  7888. (save-excursion
  7889. (org-back-to-heading)
  7890. (if (looking-at org-priority-regexp)
  7891. (setq current (string-to-char (match-string 2))
  7892. have t)
  7893. (setq current org-default-priority))
  7894. (cond
  7895. ((or (eq action 'set)
  7896. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7897. (if (not (eq action 'set))
  7898. (setq new action)
  7899. (message "Priority %c-%c, SPC to remove: "
  7900. org-highest-priority org-lowest-priority)
  7901. (setq new (read-char-exclusive)))
  7902. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7903. (= (upcase org-lowest-priority) org-lowest-priority))
  7904. (setq new (upcase new)))
  7905. (cond ((equal new ?\ ) (setq remove t))
  7906. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7907. (error "Priority must be between `%c' and `%c'"
  7908. org-highest-priority org-lowest-priority))))
  7909. ((eq action 'up)
  7910. (if (and (not have) (eq last-command this-command))
  7911. (setq new org-lowest-priority)
  7912. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7913. org-default-priority (1- current)))))
  7914. ((eq action 'down)
  7915. (if (and (not have) (eq last-command this-command))
  7916. (setq new org-highest-priority)
  7917. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7918. org-default-priority (1+ current)))))
  7919. (t (error "Invalid action")))
  7920. (if (or (< (upcase new) org-highest-priority)
  7921. (> (upcase new) org-lowest-priority))
  7922. (setq remove t))
  7923. (setq news (format "%c" new))
  7924. (if have
  7925. (if remove
  7926. (replace-match "" t t nil 1)
  7927. (replace-match news t t nil 2))
  7928. (if remove
  7929. (error "No priority cookie found in line")
  7930. (looking-at org-todo-line-regexp)
  7931. (if (match-end 2)
  7932. (progn
  7933. (goto-char (match-end 2))
  7934. (insert " [#" news "]"))
  7935. (goto-char (match-beginning 3))
  7936. (insert "[#" news "] ")))))
  7937. (org-preserve-lc (org-set-tags nil 'align))
  7938. (if remove
  7939. (message "Priority removed")
  7940. (message "Priority of current item set to %s" news))))
  7941. (defun org-get-priority (s)
  7942. "Find priority cookie and return priority."
  7943. (save-match-data
  7944. (if (not (string-match org-priority-regexp s))
  7945. (* 1000 (- org-lowest-priority org-default-priority))
  7946. (* 1000 (- org-lowest-priority
  7947. (string-to-char (match-string 2 s)))))))
  7948. ;;;; Tags
  7949. (defvar org-agenda-archives-mode)
  7950. (defun org-scan-tags (action matcher &optional todo-only)
  7951. "Scan headline tags with inheritance and produce output ACTION.
  7952. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  7953. or `agenda' to produce an entry list for an agenda view. It can also be
  7954. a Lisp form or a function that should be called at each matched headline, in
  7955. this case the return value is a list of all return values from these calls.
  7956. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  7957. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  7958. only lines with a TODO keyword are included in the output."
  7959. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7960. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7961. (org-re
  7962. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7963. (props (list 'face 'default
  7964. 'done-face 'org-done
  7965. 'undone-face 'default
  7966. 'mouse-face 'highlight
  7967. 'org-not-done-regexp org-not-done-regexp
  7968. 'org-todo-regexp org-todo-regexp
  7969. 'keymap org-agenda-keymap
  7970. 'help-echo
  7971. (format "mouse-2 or RET jump to org file %s"
  7972. (abbreviate-file-name
  7973. (or (buffer-file-name (buffer-base-buffer))
  7974. (buffer-name (buffer-base-buffer)))))))
  7975. (case-fold-search nil)
  7976. lspos tags tags-list
  7977. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  7978. (llast 0) rtn rtn1 level category i txt
  7979. todo marker entry priority)
  7980. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  7981. (setq action (list 'lambda nil action)))
  7982. (save-excursion
  7983. (goto-char (point-min))
  7984. (when (eq action 'sparse-tree)
  7985. (org-overview)
  7986. (org-remove-occur-highlights))
  7987. (while (re-search-forward re nil t)
  7988. (catch :skip
  7989. (setq todo (if (match-end 1) (match-string 2))
  7990. tags (if (match-end 4) (match-string 4)))
  7991. (goto-char (setq lspos (1+ (match-beginning 0))))
  7992. (setq level (org-reduced-level (funcall outline-level))
  7993. category (org-get-category))
  7994. (setq i llast llast level)
  7995. ;; remove tag lists from same and sublevels
  7996. (while (>= i level)
  7997. (when (setq entry (assoc i tags-alist))
  7998. (setq tags-alist (delete entry tags-alist)))
  7999. (setq i (1- i)))
  8000. ;; add the next tags
  8001. (when tags
  8002. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8003. tags-alist
  8004. (cons (cons level tags) tags-alist)))
  8005. ;; compile tags for current headline
  8006. (setq tags-list
  8007. (if org-use-tag-inheritance
  8008. (apply 'append (mapcar 'cdr tags-alist))
  8009. tags))
  8010. (when (and tags org-use-tag-inheritance
  8011. (not (eq t org-use-tag-inheritance)))
  8012. ;; selective inheritance, remove uninherited ones
  8013. (setcdr (car tags-alist)
  8014. (org-remove-uniherited-tags (cdar tags-alist))))
  8015. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8016. (let ((case-fold-search t)) (eval matcher))
  8017. (or
  8018. (not (member org-archive-tag tags-list))
  8019. ;; we have an archive tag, should we use this anyway?
  8020. (or (not org-agenda-skip-archived-trees)
  8021. (and (eq action 'agenda) org-agenda-archives-mode))))
  8022. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8023. ;; select this headline
  8024. (cond
  8025. ((eq action 'sparse-tree)
  8026. (and org-highlight-sparse-tree-matches
  8027. (org-get-heading) (match-end 0)
  8028. (org-highlight-new-match
  8029. (match-beginning 0) (match-beginning 1)))
  8030. (org-show-context 'tags-tree))
  8031. ((eq action 'agenda)
  8032. (setq txt (org-format-agenda-item
  8033. ""
  8034. (concat
  8035. (if org-tags-match-list-sublevels
  8036. (make-string (1- level) ?.) "")
  8037. (org-get-heading))
  8038. category tags-list)
  8039. priority (org-get-priority txt))
  8040. (goto-char lspos)
  8041. (setq marker (org-agenda-new-marker))
  8042. (org-add-props txt props
  8043. 'org-marker marker 'org-hd-marker marker 'org-category category
  8044. 'priority priority 'type "tagsmatch")
  8045. (push txt rtn))
  8046. ((functionp action)
  8047. (save-excursion
  8048. (setq rtn1 (funcall action))
  8049. (push rtn1 rtn))
  8050. (goto-char (point-at-eol)))
  8051. (t (error "Invalid action")))
  8052. ;; if we are to skip sublevels, jump to end of subtree
  8053. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8054. (when (and (eq action 'sparse-tree)
  8055. (not org-sparse-tree-open-archived-trees))
  8056. (org-hide-archived-subtrees (point-min) (point-max)))
  8057. (nreverse rtn)))
  8058. (defun org-remove-uniherited-tags (tags)
  8059. "Remove all tags that are not inherited from the list TAGS."
  8060. (cond
  8061. ((eq org-use-tag-inheritance t) tags)
  8062. ((not org-use-tag-inheritance) nil)
  8063. ((stringp org-use-tag-inheritance)
  8064. (delq nil (mapcar
  8065. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8066. tags)))
  8067. ((listp org-use-tag-inheritance)
  8068. (delq nil (mapcar
  8069. (lambda (x) (if (member x org-use-tag-inheritance) x nil))
  8070. tags)))))
  8071. (defvar todo-only) ;; dynamically scoped
  8072. (defun org-tags-sparse-tree (&optional todo-only match)
  8073. "Create a sparse tree according to tags string MATCH.
  8074. MATCH can contain positive and negative selection of tags, like
  8075. \"+WORK+URGENT-WITHBOSS\".
  8076. If optional argument TODO_ONLY is non-nil, only select lines that are
  8077. also TODO lines."
  8078. (interactive "P")
  8079. (org-prepare-agenda-buffers (list (current-buffer)))
  8080. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8081. (defvar org-cached-props nil)
  8082. (defun org-cached-entry-get (pom property)
  8083. (if (or (eq t org-use-property-inheritance)
  8084. (and (stringp org-use-property-inheritance)
  8085. (string-match org-use-property-inheritance property))
  8086. (and (listp org-use-property-inheritance)
  8087. (member property org-use-property-inheritance)))
  8088. ;; Caching is not possible, check it directly
  8089. (org-entry-get pom property 'inherit)
  8090. ;; Get all properties, so that we can do complicated checks easily
  8091. (cdr (assoc property (or org-cached-props
  8092. (setq org-cached-props
  8093. (org-entry-properties pom)))))))
  8094. (defun org-global-tags-completion-table (&optional files)
  8095. "Return the list of all tags in all agenda buffer/files."
  8096. (save-excursion
  8097. (org-uniquify
  8098. (delq nil
  8099. (apply 'append
  8100. (mapcar
  8101. (lambda (file)
  8102. (set-buffer (find-file-noselect file))
  8103. (append (org-get-buffer-tags)
  8104. (mapcar (lambda (x) (if (stringp (car-safe x))
  8105. (list (car-safe x)) nil))
  8106. org-tag-alist)))
  8107. (if (and files (car files))
  8108. files
  8109. (org-agenda-files))))))))
  8110. (defun org-make-tags-matcher (match)
  8111. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8112. ;; todo-only is scoped dynamically into this function, and the function
  8113. ;; may change it it the matcher asksk for it.
  8114. (unless match
  8115. ;; Get a new match request, with completion
  8116. (let ((org-last-tags-completion-table
  8117. (org-global-tags-completion-table)))
  8118. (setq match (completing-read
  8119. "Match: " 'org-tags-completion-function nil nil nil
  8120. 'org-tags-history))))
  8121. ;; Parse the string and create a lisp form
  8122. (let ((match0 match)
  8123. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8124. minus tag mm
  8125. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8126. orterms term orlist re-p str-p level-p level-op time-p
  8127. prop-p pn pv po cat-p gv)
  8128. (if (string-match "/+" match)
  8129. ;; match contains also a todo-matching request
  8130. (progn
  8131. (setq tagsmatch (substring match 0 (match-beginning 0))
  8132. todomatch (substring match (match-end 0)))
  8133. (if (string-match "^!" todomatch)
  8134. (setq todo-only t todomatch (substring todomatch 1)))
  8135. (if (string-match "^\\s-*$" todomatch)
  8136. (setq todomatch nil)))
  8137. ;; only matching tags
  8138. (setq tagsmatch match todomatch nil))
  8139. ;; Make the tags matcher
  8140. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8141. (setq tagsmatcher t)
  8142. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8143. (while (setq term (pop orterms))
  8144. (while (and (equal (substring term -1) "\\") orterms)
  8145. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8146. (while (string-match re term)
  8147. (setq minus (and (match-end 1)
  8148. (equal (match-string 1 term) "-"))
  8149. tag (match-string 2 term)
  8150. re-p (equal (string-to-char tag) ?{)
  8151. level-p (match-end 4)
  8152. prop-p (match-end 5)
  8153. mm (cond
  8154. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8155. (level-p
  8156. (setq level-op (org-op-to-function (match-string 3 term)))
  8157. `(,level-op level ,(string-to-number
  8158. (match-string 4 term))))
  8159. (prop-p
  8160. (setq pn (match-string 5 term)
  8161. po (match-string 6 term)
  8162. pv (match-string 7 term)
  8163. cat-p (equal pn "CATEGORY")
  8164. re-p (equal (string-to-char pv) ?{)
  8165. str-p (equal (string-to-char pv) ?\")
  8166. time-p (save-match-data (string-match "^\"<.*>\"$" pv))
  8167. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8168. (if time-p (setq pv (org-matcher-time pv)))
  8169. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8170. (if (equal pn "CATEGORY")
  8171. (setq gv '(get-text-property (point) 'org-category))
  8172. (setq gv `(org-cached-entry-get nil ,pn)))
  8173. (if re-p
  8174. (if (eq po 'org<>)
  8175. `(not (string-match ,pv (or ,gv "")))
  8176. `(string-match ,pv (or ,gv "")))
  8177. (if str-p
  8178. `(,po (or ,gv "") ,pv)
  8179. `(,po (string-to-number (or ,gv ""))
  8180. ,(string-to-number pv) ))))
  8181. (t `(member ,(downcase tag) tags-list)))
  8182. mm (if minus (list 'not mm) mm)
  8183. term (substring term (match-end 0)))
  8184. (push mm tagsmatcher))
  8185. (push (if (> (length tagsmatcher) 1)
  8186. (cons 'and tagsmatcher)
  8187. (car tagsmatcher))
  8188. orlist)
  8189. (setq tagsmatcher nil))
  8190. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8191. (setq tagsmatcher
  8192. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8193. ;; Make the todo matcher
  8194. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8195. (setq todomatcher t)
  8196. (setq orterms (org-split-string todomatch "|") orlist nil)
  8197. (while (setq term (pop orterms))
  8198. (while (string-match re term)
  8199. (setq minus (and (match-end 1)
  8200. (equal (match-string 1 term) "-"))
  8201. kwd (match-string 2 term)
  8202. re-p (equal (string-to-char kwd) ?{)
  8203. term (substring term (match-end 0))
  8204. mm (if re-p
  8205. `(string-match ,(substring kwd 1 -1) todo)
  8206. (list 'equal 'todo kwd))
  8207. mm (if minus (list 'not mm) mm))
  8208. (push mm todomatcher))
  8209. (push (if (> (length todomatcher) 1)
  8210. (cons 'and todomatcher)
  8211. (car todomatcher))
  8212. orlist)
  8213. (setq todomatcher nil))
  8214. (setq todomatcher (if (> (length orlist) 1)
  8215. (cons 'or orlist) (car orlist))))
  8216. ;; Return the string and lisp forms of the matcher
  8217. (setq matcher (if todomatcher
  8218. (list 'and tagsmatcher todomatcher)
  8219. tagsmatcher))
  8220. (cons match0 matcher)))
  8221. (defun org-op-to-function (op &optional stringp)
  8222. "Turn an operator into the appropriate function."
  8223. (setq op
  8224. (cond
  8225. ((equal op "<" ) '(< string< org-time<))
  8226. ((equal op ">" ) '(> org-string> org-time>))
  8227. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8228. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8229. ((member op '("=" "==")) '(= string= org-time=))
  8230. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8231. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8232. (defun org<> (a b) (not (= a b)))
  8233. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8234. (defun org-string>= (a b) (not (string< a b)))
  8235. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8236. (defun org-string<> (a b) (not (string= a b)))
  8237. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8238. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8239. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8240. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8241. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8242. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8243. (defun org-2ft (s)
  8244. "Convert S to a floating point time.
  8245. If S is already a number, just return it. If it is a string, parse
  8246. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8247. (cond
  8248. ((numberp s) s)
  8249. ((stringp s)
  8250. (condition-case nil
  8251. (float-time (apply 'encode-time (org-parse-time-string s)))
  8252. (error 0.)))
  8253. (t 0.)))
  8254. (defun org-matcher-time (s)
  8255. (cond
  8256. ((equal s "<now>") (float-time))
  8257. ((equal s "<today>")
  8258. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8259. (t (org-2ft s))))
  8260. (defun org-match-any-p (re list)
  8261. "Does re match any element of list?"
  8262. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8263. (delq nil list))
  8264. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8265. (defvar org-tags-overlay (org-make-overlay 1 1))
  8266. (org-detach-overlay org-tags-overlay)
  8267. (defun org-get-local-tags-at (&optional pos)
  8268. "Get a list of tags defined in the current headline."
  8269. (org-get-tags-at pos 'local))
  8270. (defun org-get-local-tags ()
  8271. "Get a list of tags defined in the current headline."
  8272. (org-get-tags-at nil 'local))
  8273. (defun org-get-tags-at (&optional pos local)
  8274. "Get a list of all headline tags applicable at POS.
  8275. POS defaults to point. If tags are inherited, the list contains
  8276. the targets in the same sequence as the headlines appear, i.e.
  8277. the tags of the current headline come last.
  8278. When LOCAL is non-nil, only return tags from the current headline,
  8279. ignore inherited ones."
  8280. (interactive)
  8281. (let (tags ltags lastpos parent)
  8282. (save-excursion
  8283. (save-restriction
  8284. (widen)
  8285. (goto-char (or pos (point)))
  8286. (save-match-data
  8287. (catch 'done
  8288. (condition-case nil
  8289. (progn
  8290. (org-back-to-heading t)
  8291. (while (not (equal lastpos (point)))
  8292. (setq lastpos (point))
  8293. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8294. (setq ltags (org-split-string
  8295. (org-match-string-no-properties 1) ":"))
  8296. (setq tags (append
  8297. (if parent
  8298. (org-remove-uniherited-tags ltags)
  8299. ltags)
  8300. tags)))
  8301. (or org-use-tag-inheritance (throw 'done t))
  8302. (if local (throw 'done t))
  8303. (org-up-heading-all 1)
  8304. (setq parent t)))
  8305. (error nil)))))
  8306. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8307. (defun org-toggle-tag (tag &optional onoff)
  8308. "Toggle the tag TAG for the current line.
  8309. If ONOFF is `on' or `off', don't toggle but set to this state."
  8310. (unless (org-on-heading-p t) (error "Not on headling"))
  8311. (let (res current)
  8312. (save-excursion
  8313. (beginning-of-line)
  8314. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8315. (point-at-eol) t)
  8316. (progn
  8317. (setq current (match-string 1))
  8318. (replace-match ""))
  8319. (setq current ""))
  8320. (setq current (nreverse (org-split-string current ":")))
  8321. (cond
  8322. ((eq onoff 'on)
  8323. (setq res t)
  8324. (or (member tag current) (push tag current)))
  8325. ((eq onoff 'off)
  8326. (or (not (member tag current)) (setq current (delete tag current))))
  8327. (t (if (member tag current)
  8328. (setq current (delete tag current))
  8329. (setq res t)
  8330. (push tag current))))
  8331. (end-of-line 1)
  8332. (if current
  8333. (progn
  8334. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8335. (org-set-tags nil t))
  8336. (delete-horizontal-space))
  8337. (run-hooks 'org-after-tags-change-hook))
  8338. res))
  8339. (defun org-align-tags-here (to-col)
  8340. ;; Assumes that this is a headline
  8341. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8342. (beginning-of-line 1)
  8343. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8344. (< pos (match-beginning 2)))
  8345. (progn
  8346. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8347. (goto-char (match-beginning 1))
  8348. (insert " ")
  8349. (delete-region (point) (1+ (match-beginning 2)))
  8350. (setq ncol (max (1+ (current-column))
  8351. (1+ col)
  8352. (if (> to-col 0)
  8353. to-col
  8354. (- (abs to-col) tags-l))))
  8355. (setq p (point))
  8356. (insert (make-string (- ncol (current-column)) ?\ ))
  8357. (setq ncol (current-column))
  8358. (when indent-tabs-mode (tabify p (point-at-eol)))
  8359. (org-move-to-column (min ncol col) t))
  8360. (goto-char pos))))
  8361. (defun org-set-tags-command (&optional arg just-align)
  8362. "Call the set-tags command for the current entry."
  8363. (interactive "P")
  8364. (if (org-on-heading-p)
  8365. (org-set-tags arg just-align)
  8366. (save-excursion
  8367. (org-back-to-heading t)
  8368. (org-set-tags arg just-align))))
  8369. (defun org-set-tags (&optional arg just-align)
  8370. "Set the tags for the current headline.
  8371. With prefix ARG, realign all tags in headings in the current buffer."
  8372. (interactive "P")
  8373. (let* ((re (concat "^" outline-regexp))
  8374. (current (org-get-tags-string))
  8375. (col (current-column))
  8376. (org-setting-tags t)
  8377. table current-tags inherited-tags ; computed below when needed
  8378. tags p0 c0 c1 rpl)
  8379. (if arg
  8380. (save-excursion
  8381. (goto-char (point-min))
  8382. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8383. (while (re-search-forward re nil t)
  8384. (org-set-tags nil t)
  8385. (end-of-line 1)))
  8386. (message "All tags realigned to column %d" org-tags-column))
  8387. (if just-align
  8388. (setq tags current)
  8389. ;; Get a new set of tags from the user
  8390. (save-excursion
  8391. (setq table (or org-tag-alist (org-get-buffer-tags))
  8392. org-last-tags-completion-table table
  8393. current-tags (org-split-string current ":")
  8394. inherited-tags (nreverse
  8395. (nthcdr (length current-tags)
  8396. (nreverse (org-get-tags-at))))
  8397. tags
  8398. (if (or (eq t org-use-fast-tag-selection)
  8399. (and org-use-fast-tag-selection
  8400. (delq nil (mapcar 'cdr table))))
  8401. (org-fast-tag-selection
  8402. current-tags inherited-tags table
  8403. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8404. (let ((org-add-colon-after-tag-completion t))
  8405. (org-trim
  8406. (org-without-partial-completion
  8407. (completing-read "Tags: " 'org-tags-completion-function
  8408. nil nil current 'org-tags-history)))))))
  8409. (while (string-match "[-+&]+" tags)
  8410. ;; No boolean logic, just a list
  8411. (setq tags (replace-match ":" t t tags))))
  8412. (if (string-match "\\`[\t ]*\\'" tags)
  8413. (setq tags "")
  8414. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8415. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8416. ;; Insert new tags at the correct column
  8417. (beginning-of-line 1)
  8418. (cond
  8419. ((and (equal current "") (equal tags "")))
  8420. ((re-search-forward
  8421. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8422. (point-at-eol) t)
  8423. (if (equal tags "")
  8424. (setq rpl "")
  8425. (goto-char (match-beginning 0))
  8426. (setq c0 (current-column) p0 (point)
  8427. c1 (max (1+ c0) (if (> org-tags-column 0)
  8428. org-tags-column
  8429. (- (- org-tags-column) (length tags))))
  8430. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8431. (replace-match rpl t t)
  8432. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8433. tags)
  8434. (t (error "Tags alignment failed")))
  8435. (org-move-to-column col)
  8436. (unless just-align
  8437. (run-hooks 'org-after-tags-change-hook)))))
  8438. (defun org-change-tag-in-region (beg end tag off)
  8439. "Add or remove TAG for each entry in the region.
  8440. This works in the agenda, and also in an org-mode buffer."
  8441. (interactive
  8442. (list (region-beginning) (region-end)
  8443. (let ((org-last-tags-completion-table
  8444. (if (org-mode-p)
  8445. (org-get-buffer-tags)
  8446. (org-global-tags-completion-table))))
  8447. (completing-read
  8448. "Tag: " 'org-tags-completion-function nil nil nil
  8449. 'org-tags-history))
  8450. (progn
  8451. (message "[s]et or [r]emove? ")
  8452. (equal (read-char-exclusive) ?r))))
  8453. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8454. (let ((agendap (equal major-mode 'org-agenda-mode))
  8455. l1 l2 m buf pos newhead (cnt 0))
  8456. (goto-char end)
  8457. (setq l2 (1- (org-current-line)))
  8458. (goto-char beg)
  8459. (setq l1 (org-current-line))
  8460. (loop for l from l1 to l2 do
  8461. (goto-line l)
  8462. (setq m (get-text-property (point) 'org-hd-marker))
  8463. (when (or (and (org-mode-p) (org-on-heading-p))
  8464. (and agendap m))
  8465. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8466. pos (if agendap m (point)))
  8467. (with-current-buffer buf
  8468. (save-excursion
  8469. (save-restriction
  8470. (goto-char pos)
  8471. (setq cnt (1+ cnt))
  8472. (org-toggle-tag tag (if off 'off 'on))
  8473. (setq newhead (org-get-heading)))))
  8474. (and agendap (org-agenda-change-all-lines newhead m))))
  8475. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8476. (defun org-tags-completion-function (string predicate &optional flag)
  8477. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8478. (confirm (lambda (x) (stringp (car x)))))
  8479. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8480. (setq s1 (match-string 1 string)
  8481. s2 (match-string 2 string))
  8482. (setq s1 "" s2 string))
  8483. (cond
  8484. ((eq flag nil)
  8485. ;; try completion
  8486. (setq rtn (try-completion s2 ctable confirm))
  8487. (if (stringp rtn)
  8488. (setq rtn
  8489. (concat s1 s2 (substring rtn (length s2))
  8490. (if (and org-add-colon-after-tag-completion
  8491. (assoc rtn ctable))
  8492. ":" ""))))
  8493. rtn)
  8494. ((eq flag t)
  8495. ;; all-completions
  8496. (all-completions s2 ctable confirm)
  8497. )
  8498. ((eq flag 'lambda)
  8499. ;; exact match?
  8500. (assoc s2 ctable)))
  8501. ))
  8502. (defun org-fast-tag-insert (kwd tags face &optional end)
  8503. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8504. (insert (format "%-12s" (concat kwd ":"))
  8505. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8506. (or end "")))
  8507. (defun org-fast-tag-show-exit (flag)
  8508. (save-excursion
  8509. (goto-line 3)
  8510. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8511. (replace-match ""))
  8512. (when flag
  8513. (end-of-line 1)
  8514. (org-move-to-column (- (window-width) 19) t)
  8515. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8516. (defun org-set-current-tags-overlay (current prefix)
  8517. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8518. (if (featurep 'xemacs)
  8519. (org-overlay-display org-tags-overlay (concat prefix s)
  8520. 'secondary-selection)
  8521. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8522. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8523. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8524. "Fast tag selection with single keys.
  8525. CURRENT is the current list of tags in the headline, INHERITED is the
  8526. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8527. possibly with grouping information. TODO-TABLE is a similar table with
  8528. TODO keywords, should these have keys assigned to them.
  8529. If the keys are nil, a-z are automatically assigned.
  8530. Returns the new tags string, or nil to not change the current settings."
  8531. (let* ((fulltable (append table todo-table))
  8532. (maxlen (apply 'max (mapcar
  8533. (lambda (x)
  8534. (if (stringp (car x)) (string-width (car x)) 0))
  8535. fulltable)))
  8536. (buf (current-buffer))
  8537. (expert (eq org-fast-tag-selection-single-key 'expert))
  8538. (buffer-tags nil)
  8539. (fwidth (+ maxlen 3 1 3))
  8540. (ncol (/ (- (window-width) 4) fwidth))
  8541. (i-face 'org-done)
  8542. (c-face 'org-todo)
  8543. tg cnt e c char c1 c2 ntable tbl rtn
  8544. ov-start ov-end ov-prefix
  8545. (exit-after-next org-fast-tag-selection-single-key)
  8546. (done-keywords org-done-keywords)
  8547. groups ingroup)
  8548. (save-excursion
  8549. (beginning-of-line 1)
  8550. (if (looking-at
  8551. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8552. (setq ov-start (match-beginning 1)
  8553. ov-end (match-end 1)
  8554. ov-prefix "")
  8555. (setq ov-start (1- (point-at-eol))
  8556. ov-end (1+ ov-start))
  8557. (skip-chars-forward "^\n\r")
  8558. (setq ov-prefix
  8559. (concat
  8560. (buffer-substring (1- (point)) (point))
  8561. (if (> (current-column) org-tags-column)
  8562. " "
  8563. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8564. (org-move-overlay org-tags-overlay ov-start ov-end)
  8565. (save-window-excursion
  8566. (if expert
  8567. (set-buffer (get-buffer-create " *Org tags*"))
  8568. (delete-other-windows)
  8569. (split-window-vertically)
  8570. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8571. (erase-buffer)
  8572. (org-set-local 'org-done-keywords done-keywords)
  8573. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8574. (org-fast-tag-insert "Current" current c-face "\n\n")
  8575. (org-fast-tag-show-exit exit-after-next)
  8576. (org-set-current-tags-overlay current ov-prefix)
  8577. (setq tbl fulltable char ?a cnt 0)
  8578. (while (setq e (pop tbl))
  8579. (cond
  8580. ((equal e '(:startgroup))
  8581. (push '() groups) (setq ingroup t)
  8582. (when (not (= cnt 0))
  8583. (setq cnt 0)
  8584. (insert "\n"))
  8585. (insert "{ "))
  8586. ((equal e '(:endgroup))
  8587. (setq ingroup nil cnt 0)
  8588. (insert "}\n"))
  8589. (t
  8590. (setq tg (car e) c2 nil)
  8591. (if (cdr e)
  8592. (setq c (cdr e))
  8593. ;; automatically assign a character.
  8594. (setq c1 (string-to-char
  8595. (downcase (substring
  8596. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8597. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8598. (while (or (rassoc char ntable) (rassoc char table))
  8599. (setq char (1+ char)))
  8600. (setq c2 c1))
  8601. (setq c (or c2 char)))
  8602. (if ingroup (push tg (car groups)))
  8603. (setq tg (org-add-props tg nil 'face
  8604. (cond
  8605. ((not (assoc tg table))
  8606. (org-get-todo-face tg))
  8607. ((member tg current) c-face)
  8608. ((member tg inherited) i-face)
  8609. (t nil))))
  8610. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8611. (insert "[" c "] " tg (make-string
  8612. (- fwidth 4 (length tg)) ?\ ))
  8613. (push (cons tg c) ntable)
  8614. (when (= (setq cnt (1+ cnt)) ncol)
  8615. (insert "\n")
  8616. (if ingroup (insert " "))
  8617. (setq cnt 0)))))
  8618. (setq ntable (nreverse ntable))
  8619. (insert "\n")
  8620. (goto-char (point-min))
  8621. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8622. (fit-window-to-buffer))
  8623. (setq rtn
  8624. (catch 'exit
  8625. (while t
  8626. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8627. (if groups " [!] no groups" " [!]groups")
  8628. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8629. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8630. (cond
  8631. ((= c ?\r) (throw 'exit t))
  8632. ((= c ?!)
  8633. (setq groups (not groups))
  8634. (goto-char (point-min))
  8635. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8636. ((= c ?\C-c)
  8637. (if (not expert)
  8638. (org-fast-tag-show-exit
  8639. (setq exit-after-next (not exit-after-next)))
  8640. (setq expert nil)
  8641. (delete-other-windows)
  8642. (split-window-vertically)
  8643. (org-switch-to-buffer-other-window " *Org tags*")
  8644. (and (fboundp 'fit-window-to-buffer)
  8645. (fit-window-to-buffer))))
  8646. ((or (= c ?\C-g)
  8647. (and (= c ?q) (not (rassoc c ntable))))
  8648. (org-detach-overlay org-tags-overlay)
  8649. (setq quit-flag t))
  8650. ((= c ?\ )
  8651. (setq current nil)
  8652. (if exit-after-next (setq exit-after-next 'now)))
  8653. ((= c ?\t)
  8654. (condition-case nil
  8655. (setq tg (completing-read
  8656. "Tag: "
  8657. (or buffer-tags
  8658. (with-current-buffer buf
  8659. (org-get-buffer-tags)))))
  8660. (quit (setq tg "")))
  8661. (when (string-match "\\S-" tg)
  8662. (add-to-list 'buffer-tags (list tg))
  8663. (if (member tg current)
  8664. (setq current (delete tg current))
  8665. (push tg current)))
  8666. (if exit-after-next (setq exit-after-next 'now)))
  8667. ((setq e (rassoc c todo-table) tg (car e))
  8668. (with-current-buffer buf
  8669. (save-excursion (org-todo tg)))
  8670. (if exit-after-next (setq exit-after-next 'now)))
  8671. ((setq e (rassoc c ntable) tg (car e))
  8672. (if (member tg current)
  8673. (setq current (delete tg current))
  8674. (loop for g in groups do
  8675. (if (member tg g)
  8676. (mapc (lambda (x)
  8677. (setq current (delete x current)))
  8678. g)))
  8679. (push tg current))
  8680. (if exit-after-next (setq exit-after-next 'now))))
  8681. ;; Create a sorted list
  8682. (setq current
  8683. (sort current
  8684. (lambda (a b)
  8685. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8686. (if (eq exit-after-next 'now) (throw 'exit t))
  8687. (goto-char (point-min))
  8688. (beginning-of-line 2)
  8689. (delete-region (point) (point-at-eol))
  8690. (org-fast-tag-insert "Current" current c-face)
  8691. (org-set-current-tags-overlay current ov-prefix)
  8692. (while (re-search-forward
  8693. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8694. (setq tg (match-string 1))
  8695. (add-text-properties
  8696. (match-beginning 1) (match-end 1)
  8697. (list 'face
  8698. (cond
  8699. ((member tg current) c-face)
  8700. ((member tg inherited) i-face)
  8701. (t (get-text-property (match-beginning 1) 'face))))))
  8702. (goto-char (point-min)))))
  8703. (org-detach-overlay org-tags-overlay)
  8704. (if rtn
  8705. (mapconcat 'identity current ":")
  8706. nil))))
  8707. (defun org-get-tags-string ()
  8708. "Get the TAGS string in the current headline."
  8709. (unless (org-on-heading-p t)
  8710. (error "Not on a heading"))
  8711. (save-excursion
  8712. (beginning-of-line 1)
  8713. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8714. (org-match-string-no-properties 1)
  8715. "")))
  8716. (defun org-get-tags ()
  8717. "Get the list of tags specified in the current headline."
  8718. (org-split-string (org-get-tags-string) ":"))
  8719. (defun org-get-buffer-tags ()
  8720. "Get a table of all tags used in the buffer, for completion."
  8721. (let (tags)
  8722. (save-excursion
  8723. (goto-char (point-min))
  8724. (while (re-search-forward
  8725. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8726. (when (equal (char-after (point-at-bol 0)) ?*)
  8727. (mapc (lambda (x) (add-to-list 'tags x))
  8728. (org-split-string (org-match-string-no-properties 1) ":")))))
  8729. (mapcar 'list tags)))
  8730. ;;;; The mapping API
  8731. ;;;###autoload
  8732. (defun org-map-entries (func &optional match scope &rest skip)
  8733. "Call FUNC at each headline selected by MATCH in SCOPE.
  8734. FUNC is a function or a lisp form. The function will be called without
  8735. arguments, with the cursor positioned at the beginning of the headline.
  8736. The return values of all calls to the function will be collected and
  8737. returned as a list.
  8738. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8739. Only headlines that are matched by this query will be considered during
  8740. the iteration. When MATCH is nil or t, all headlines will be
  8741. visited by the iteration.
  8742. SCOPE determines the scope of this command. It can be any of:
  8743. nil The current buffer, respecting the restriction if any
  8744. tree The subtree started with the entry at point
  8745. file The current buffer, without restriction
  8746. file-with-archives
  8747. The current buffer, and any archives associated with it
  8748. agenda All agenda files
  8749. agenda-with-archives
  8750. All agenda files with any archive files associated with them
  8751. \(file1 file2 ...)
  8752. If this is a list, all files in the list will be scanned
  8753. The remaining args are treated as settings for the skipping facilities of
  8754. the scanner. The following items can be given here:
  8755. archive skip trees with the archive tag.
  8756. comment skip trees with the COMMENT keyword
  8757. function or Emacs Lisp form:
  8758. will be used as value for `org-agenda-skip-function', so whenever
  8759. the the function returns t, FUNC will not be called for that
  8760. entry and search will continue from the point where the
  8761. function leaves it."
  8762. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8763. (org-agenda-skip-archived-trees (memq 'archive skip))
  8764. (org-agenda-skip-comment-trees (memq 'comment skip))
  8765. (org-agenda-skip-function
  8766. (car (org-delete-all '(comment archive) skip)))
  8767. (org-tags-match-list-sublevels t)
  8768. matcher pos file
  8769. org-todo-keywords-for-agenda
  8770. org-done-keywords-for-agenda
  8771. org-todo-keyword-alist-for-agenda
  8772. org-tag-alist-for-agenda)
  8773. (cond
  8774. ((eq match t) (setq matcher t))
  8775. ((eq match nil) (setq matcher t))
  8776. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  8777. (when (eq scope 'tree)
  8778. (org-back-to-heading t)
  8779. (org-narrow-to-subtree)
  8780. (setq scope nil))
  8781. (if (not scope)
  8782. (progn
  8783. (org-prepare-agenda-buffers
  8784. (list (buffer-file-name (current-buffer))))
  8785. (org-scan-tags func matcher))
  8786. ;; Get the right scope
  8787. (setq pos (point))
  8788. (cond
  8789. ((and scope (listp scope) (symbolp (car scope)))
  8790. (setq scope (eval scope)))
  8791. ((eq scope 'agenda)
  8792. (setq scope (org-agenda-files t)))
  8793. ((eq scope 'agenda-with-archives)
  8794. (setq scope (org-agenda-files t))
  8795. (setq scope (org-add-archive-files scope)))
  8796. ((eq scope 'file)
  8797. (setq scope (list (buffer-file-name))))
  8798. ((eq scope 'file-with-archives)
  8799. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  8800. (org-prepare-agenda-buffers scope)
  8801. (while (setq file (pop scope))
  8802. (with-current-buffer (org-find-base-buffer-visiting file)
  8803. (save-excursion
  8804. (save-restriction
  8805. (widen)
  8806. (goto-char (point-min))
  8807. (org-scan-tags func matcher))))))))
  8808. ;;;; Properties
  8809. ;;; Setting and retrieving properties
  8810. (defconst org-special-properties
  8811. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8812. "TIMESTAMP" "TIMESTAMP_IA")
  8813. "The special properties valid in Org-mode.
  8814. These are properties that are not defined in the property drawer,
  8815. but in some other way.")
  8816. (defconst org-default-properties
  8817. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8818. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8819. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  8820. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  8821. "Some properties that are used by Org-mode for various purposes.
  8822. Being in this list makes sure that they are offered for completion.")
  8823. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8824. "Regular expression matching the first line of a property drawer.")
  8825. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8826. "Regular expression matching the first line of a property drawer.")
  8827. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  8828. "Regular expression matching the first line of a property drawer.")
  8829. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  8830. "Regular expression matching the first line of a property drawer.")
  8831. (defconst org-property-drawer-re
  8832. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  8833. org-property-end-re "\\)\n?")
  8834. "Matches an entire property drawer.")
  8835. (defconst org-clock-drawer-re
  8836. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  8837. org-property-end-re "\\)\n?")
  8838. "Matches an entire clock drawer.")
  8839. (defun org-property-action ()
  8840. "Do an action on properties."
  8841. (interactive)
  8842. (let (c)
  8843. (org-at-property-p)
  8844. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8845. (setq c (read-char-exclusive))
  8846. (cond
  8847. ((equal c ?s)
  8848. (call-interactively 'org-set-property))
  8849. ((equal c ?d)
  8850. (call-interactively 'org-delete-property))
  8851. ((equal c ?D)
  8852. (call-interactively 'org-delete-property-globally))
  8853. ((equal c ?c)
  8854. (call-interactively 'org-compute-property-at-point))
  8855. (t (error "No such property action %c" c)))))
  8856. (defun org-at-property-p ()
  8857. "Is the cursor in a property line?"
  8858. ;; FIXME: Does not check if we are actually in the drawer.
  8859. ;; FIXME: also returns true on any drawers.....
  8860. ;; This is used by C-c C-c for property action.
  8861. (save-excursion
  8862. (beginning-of-line 1)
  8863. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8864. (defun org-get-property-block (&optional beg end force)
  8865. "Return the (beg . end) range of the body of the property drawer.
  8866. BEG and END can be beginning and end of subtree, if not given
  8867. they will be found.
  8868. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8869. (catch 'exit
  8870. (save-excursion
  8871. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8872. (end (or end (progn (outline-next-heading) (point)))))
  8873. (goto-char beg)
  8874. (if (re-search-forward org-property-start-re end t)
  8875. (setq beg (1+ (match-end 0)))
  8876. (if force
  8877. (save-excursion
  8878. (org-insert-property-drawer)
  8879. (setq end (progn (outline-next-heading) (point))))
  8880. (throw 'exit nil))
  8881. (goto-char beg)
  8882. (if (re-search-forward org-property-start-re end t)
  8883. (setq beg (1+ (match-end 0)))))
  8884. (if (re-search-forward org-property-end-re end t)
  8885. (setq end (match-beginning 0))
  8886. (or force (throw 'exit nil))
  8887. (goto-char beg)
  8888. (setq end beg)
  8889. (org-indent-line-function)
  8890. (insert ":END:\n"))
  8891. (cons beg end)))))
  8892. (defun org-entry-properties (&optional pom which)
  8893. "Get all properties of the entry at point-or-marker POM.
  8894. This includes the TODO keyword, the tags, time strings for deadline,
  8895. scheduled, and clocking, and any additional properties defined in the
  8896. entry. The return value is an alist, keys may occur multiple times
  8897. if the property key was used several times.
  8898. POM may also be nil, in which case the current entry is used.
  8899. If WHICH is nil or `all', get all properties. If WHICH is
  8900. `special' or `standard', only get that subclass."
  8901. (setq which (or which 'all))
  8902. (org-with-point-at pom
  8903. (let ((clockstr (substring org-clock-string 0 -1))
  8904. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8905. beg end range props sum-props key value string clocksum)
  8906. (save-excursion
  8907. (when (condition-case nil (org-back-to-heading t) (error nil))
  8908. (setq beg (point))
  8909. (setq sum-props (get-text-property (point) 'org-summaries))
  8910. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8911. (outline-next-heading)
  8912. (setq end (point))
  8913. (when (memq which '(all special))
  8914. ;; Get the special properties, like TODO and tags
  8915. (goto-char beg)
  8916. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8917. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8918. (when (looking-at org-priority-regexp)
  8919. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8920. (when (and (setq value (org-get-tags-string))
  8921. (string-match "\\S-" value))
  8922. (push (cons "TAGS" value) props))
  8923. (when (setq value (org-get-tags-at))
  8924. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8925. props))
  8926. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8927. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8928. string (if (equal key clockstr)
  8929. (org-no-properties
  8930. (org-trim
  8931. (buffer-substring
  8932. (match-beginning 3) (goto-char (point-at-eol)))))
  8933. (substring (org-match-string-no-properties 3) 1 -1)))
  8934. (unless key
  8935. (if (= (char-after (match-beginning 3)) ?\[)
  8936. (setq key "TIMESTAMP_IA")
  8937. (setq key "TIMESTAMP")))
  8938. (when (or (equal key clockstr) (not (assoc key props)))
  8939. (push (cons key string) props)))
  8940. )
  8941. (when (memq which '(all standard))
  8942. ;; Get the standard properties, like :PORP: ...
  8943. (setq range (org-get-property-block beg end))
  8944. (when range
  8945. (goto-char (car range))
  8946. (while (re-search-forward
  8947. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8948. (cdr range) t)
  8949. (setq key (org-match-string-no-properties 1)
  8950. value (org-trim (or (org-match-string-no-properties 2) "")))
  8951. (unless (member key excluded)
  8952. (push (cons key (or value "")) props)))))
  8953. (if clocksum
  8954. (push (cons "CLOCKSUM"
  8955. (org-columns-number-to-string (/ (float clocksum) 60.)
  8956. 'add_times))
  8957. props))
  8958. (unless (assoc "CATEGORY" props)
  8959. (setq value (or (org-get-category)
  8960. (progn (org-refresh-category-properties)
  8961. (org-get-category))))
  8962. (push (cons "CATEGORY" value) props))
  8963. (append sum-props (nreverse props)))))))
  8964. (defun org-entry-get (pom property &optional inherit)
  8965. "Get value of PROPERTY for entry at point-or-marker POM.
  8966. If INHERIT is non-nil and the entry does not have the property,
  8967. then also check higher levels of the hierarchy.
  8968. If INHERIT is the symbol `selective', use inheritance only if the setting
  8969. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8970. If the property is present but empty, the return value is the empty string.
  8971. If the property is not present at all, nil is returned."
  8972. (org-with-point-at pom
  8973. (if (and inherit (if (eq inherit 'selective)
  8974. (org-property-inherit-p property)
  8975. t))
  8976. (org-entry-get-with-inheritance property)
  8977. (if (member property org-special-properties)
  8978. ;; We need a special property. Use brute force, get all properties.
  8979. (cdr (assoc property (org-entry-properties nil 'special)))
  8980. (let ((range (org-get-property-block)))
  8981. (if (and range
  8982. (goto-char (car range))
  8983. (re-search-forward
  8984. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8985. (cdr range) t))
  8986. ;; Found the property, return it.
  8987. (if (match-end 1)
  8988. (org-match-string-no-properties 1)
  8989. "")))))))
  8990. (defun org-property-or-variable-value (var &optional inherit)
  8991. "Check if there is a property fixing the value of VAR.
  8992. If yes, return this value. If not, return the current value of the variable."
  8993. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8994. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8995. (read prop)
  8996. (symbol-value var))))
  8997. (defun org-entry-delete (pom property)
  8998. "Delete the property PROPERTY from entry at point-or-marker POM."
  8999. (org-with-point-at pom
  9000. (if (member property org-special-properties)
  9001. nil ; cannot delete these properties.
  9002. (let ((range (org-get-property-block)))
  9003. (if (and range
  9004. (goto-char (car range))
  9005. (re-search-forward
  9006. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9007. (cdr range) t))
  9008. (progn
  9009. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9010. t)
  9011. nil)))))
  9012. ;; Multi-values properties are properties that contain multiple values
  9013. ;; These values are assumed to be single words, separated by whitespace.
  9014. (defun org-entry-add-to-multivalued-property (pom property value)
  9015. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9016. (let* ((old (org-entry-get pom property))
  9017. (values (and old (org-split-string old "[ \t]"))))
  9018. (setq value (org-entry-protect-space value))
  9019. (unless (member value values)
  9020. (setq values (cons value values))
  9021. (org-entry-put pom property
  9022. (mapconcat 'identity values " ")))))
  9023. (defun org-entry-remove-from-multivalued-property (pom property value)
  9024. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9025. (let* ((old (org-entry-get pom property))
  9026. (values (and old (org-split-string old "[ \t]"))))
  9027. (setq value (org-entry-protect-space value))
  9028. (when (member value values)
  9029. (setq values (delete value values))
  9030. (org-entry-put pom property
  9031. (mapconcat 'identity values " ")))))
  9032. (defun org-entry-member-in-multivalued-property (pom property value)
  9033. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9034. (let* ((old (org-entry-get pom property))
  9035. (values (and old (org-split-string old "[ \t]"))))
  9036. (setq value (org-entry-protect-space value))
  9037. (member value values)))
  9038. (defun org-entry-get-multivalued-property (pom property)
  9039. "Return a list of values in a multivalued property."
  9040. (let* ((value (org-entry-get pom property))
  9041. (values (and value (org-split-string value "[ \t]"))))
  9042. (mapcar 'org-entry-restore-space values)))
  9043. (defun org-entry-put-multivalued-property (pom property &rest values)
  9044. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9045. VALUES should be a list of strings. Spaces will be protected."
  9046. (org-entry-put pom property
  9047. (mapconcat 'org-entry-protect-space values " "))
  9048. (let* ((value (org-entry-get pom property))
  9049. (values (and value (org-split-string value "[ \t]"))))
  9050. (mapcar 'org-entry-restore-space values)))
  9051. (defun org-entry-protect-space (s)
  9052. "Protect spaces and newline in string S."
  9053. (while (string-match " " s)
  9054. (setq s (replace-match "%20" t t s)))
  9055. (while (string-match "\n" s)
  9056. (setq s (replace-match "%0A" t t s)))
  9057. s)
  9058. (defun org-entry-restore-space (s)
  9059. "Restore spaces and newline in string S."
  9060. (while (string-match "%20" s)
  9061. (setq s (replace-match " " t t s)))
  9062. (while (string-match "%0A" s)
  9063. (setq s (replace-match "\n" t t s)))
  9064. s)
  9065. (defvar org-entry-property-inherited-from (make-marker)
  9066. "Marker pointing to the entry from where a proerty was inherited.
  9067. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9068. location of the entry where the inheriance search matched. If there was
  9069. no match, the marker will point nowhere.
  9070. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9071. is set.")
  9072. (defun org-entry-get-with-inheritance (property)
  9073. "Get entry property, and search higher levels if not present."
  9074. (move-marker org-entry-property-inherited-from nil)
  9075. (let (tmp)
  9076. (save-excursion
  9077. (save-restriction
  9078. (widen)
  9079. (catch 'ex
  9080. (while t
  9081. (when (setq tmp (org-entry-get nil property))
  9082. (org-back-to-heading t)
  9083. (move-marker org-entry-property-inherited-from (point))
  9084. (throw 'ex tmp))
  9085. (or (org-up-heading-safe) (throw 'ex nil)))))
  9086. (or tmp
  9087. (cdr (assoc property org-file-properties))
  9088. (cdr (assoc property org-global-properties))
  9089. (cdr (assoc property org-global-properties-fixed))))))
  9090. (defun org-entry-put (pom property value)
  9091. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9092. (org-with-point-at pom
  9093. (org-back-to-heading t)
  9094. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9095. range)
  9096. (cond
  9097. ((equal property "TODO")
  9098. (when (and (stringp value) (string-match "\\S-" value)
  9099. (not (member value org-todo-keywords-1)))
  9100. (error "\"%s\" is not a valid TODO state" value))
  9101. (if (or (not value)
  9102. (not (string-match "\\S-" value)))
  9103. (setq value 'none))
  9104. (org-todo value)
  9105. (org-set-tags nil 'align))
  9106. ((equal property "PRIORITY")
  9107. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9108. (string-to-char value) ?\ ))
  9109. (org-set-tags nil 'align))
  9110. ((equal property "SCHEDULED")
  9111. (if (re-search-forward org-scheduled-time-regexp end t)
  9112. (cond
  9113. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9114. ((eq value 'later) (org-timestamp-change 1 'day))
  9115. (t (call-interactively 'org-schedule)))
  9116. (call-interactively 'org-schedule)))
  9117. ((equal property "DEADLINE")
  9118. (if (re-search-forward org-deadline-time-regexp end t)
  9119. (cond
  9120. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9121. ((eq value 'later) (org-timestamp-change 1 'day))
  9122. (t (call-interactively 'org-deadline)))
  9123. (call-interactively 'org-deadline)))
  9124. ((member property org-special-properties)
  9125. (error "The %s property can not yet be set with `org-entry-put'"
  9126. property))
  9127. (t ; a non-special property
  9128. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9129. (setq range (org-get-property-block beg end 'force))
  9130. (goto-char (car range))
  9131. (if (re-search-forward
  9132. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9133. (progn
  9134. (delete-region (match-beginning 1) (match-end 1))
  9135. (goto-char (match-beginning 1)))
  9136. (goto-char (cdr range))
  9137. (insert "\n")
  9138. (backward-char 1)
  9139. (org-indent-line-function)
  9140. (insert ":" property ":"))
  9141. (and value (insert " " value))
  9142. (org-indent-line-function)))))))
  9143. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9144. "Get all property keys in the current buffer.
  9145. With INCLUDE-SPECIALS, also list the special properties that relect things
  9146. like tags and TODO state.
  9147. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9148. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9149. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9150. formats in the current buffer."
  9151. (let (rtn range cfmt cols s p)
  9152. (save-excursion
  9153. (save-restriction
  9154. (widen)
  9155. (goto-char (point-min))
  9156. (while (re-search-forward org-property-start-re nil t)
  9157. (setq range (org-get-property-block))
  9158. (goto-char (car range))
  9159. (while (re-search-forward
  9160. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9161. (cdr range) t)
  9162. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9163. (outline-next-heading))))
  9164. (when include-specials
  9165. (setq rtn (append org-special-properties rtn)))
  9166. (when include-defaults
  9167. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9168. (when include-columns
  9169. (save-excursion
  9170. (save-restriction
  9171. (widen)
  9172. (goto-char (point-min))
  9173. (while (re-search-forward
  9174. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9175. nil t)
  9176. (setq cfmt (match-string 2) s 0)
  9177. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9178. cfmt s)
  9179. (setq s (match-end 0)
  9180. p (match-string 1 cfmt))
  9181. (unless (or (equal p "ITEM")
  9182. (member p org-special-properties))
  9183. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9184. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9185. (defun org-property-values (key)
  9186. "Return a list of all values of property KEY."
  9187. (save-excursion
  9188. (save-restriction
  9189. (widen)
  9190. (goto-char (point-min))
  9191. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9192. values)
  9193. (while (re-search-forward re nil t)
  9194. (add-to-list 'values (org-trim (match-string 1))))
  9195. (delete "" values)))))
  9196. (defun org-insert-property-drawer ()
  9197. "Insert a property drawer into the current entry."
  9198. (interactive)
  9199. (org-back-to-heading t)
  9200. (looking-at outline-regexp)
  9201. (let ((indent (- (match-end 0)(match-beginning 0)))
  9202. (beg (point))
  9203. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9204. end hiddenp)
  9205. (outline-next-heading)
  9206. (setq end (point))
  9207. (goto-char beg)
  9208. (while (re-search-forward re end t))
  9209. (setq hiddenp (org-invisible-p))
  9210. (end-of-line 1)
  9211. (and (equal (char-after) ?\n) (forward-char 1))
  9212. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9213. (beginning-of-line 2))
  9214. (org-skip-over-state-notes)
  9215. (skip-chars-backward " \t\n\r")
  9216. (if (eq (char-before) ?*) (forward-char 1))
  9217. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9218. (beginning-of-line 0)
  9219. (org-indent-to-column indent)
  9220. (beginning-of-line 2)
  9221. (org-indent-to-column indent)
  9222. (beginning-of-line 0)
  9223. (if hiddenp
  9224. (save-excursion
  9225. (org-back-to-heading t)
  9226. (hide-entry))
  9227. (org-flag-drawer t))))
  9228. (defun org-set-property (property value)
  9229. "In the current entry, set PROPERTY to VALUE.
  9230. When called interactively, this will prompt for a property name, offering
  9231. completion on existing and default properties. And then it will prompt
  9232. for a value, offering competion either on allowed values (via an inherited
  9233. xxx_ALL property) or on existing values in other instances of this property
  9234. in the current file."
  9235. (interactive
  9236. (let* ((completion-ignore-case t)
  9237. (keys (org-buffer-property-keys nil t t))
  9238. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9239. (prop (if (member prop0 keys)
  9240. prop0
  9241. (or (cdr (assoc (downcase prop0)
  9242. (mapcar (lambda (x) (cons (downcase x) x))
  9243. keys)))
  9244. prop0)))
  9245. (cur (org-entry-get nil prop))
  9246. (allowed (org-property-get-allowed-values nil prop 'table))
  9247. (existing (mapcar 'list (org-property-values prop)))
  9248. (val (if allowed
  9249. (org-completing-read "Value: " allowed nil 'req-match)
  9250. (org-completing-read
  9251. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9252. (concat "[" cur "]") "")
  9253. ": ")
  9254. existing nil nil "" nil cur))))
  9255. (list prop (if (equal val "") cur val))))
  9256. (unless (equal (org-entry-get nil property) value)
  9257. (org-entry-put nil property value)))
  9258. (defun org-delete-property (property)
  9259. "In the current entry, delete PROPERTY."
  9260. (interactive
  9261. (let* ((completion-ignore-case t)
  9262. (prop (completing-read
  9263. "Property: " (org-entry-properties nil 'standard))))
  9264. (list prop)))
  9265. (message "Property %s %s" property
  9266. (if (org-entry-delete nil property)
  9267. "deleted"
  9268. "was not present in the entry")))
  9269. (defun org-delete-property-globally (property)
  9270. "Remove PROPERTY globally, from all entries."
  9271. (interactive
  9272. (let* ((completion-ignore-case t)
  9273. (prop (completing-read
  9274. "Globally remove property: "
  9275. (mapcar 'list (org-buffer-property-keys)))))
  9276. (list prop)))
  9277. (save-excursion
  9278. (save-restriction
  9279. (widen)
  9280. (goto-char (point-min))
  9281. (let ((cnt 0))
  9282. (while (re-search-forward
  9283. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9284. nil t)
  9285. (setq cnt (1+ cnt))
  9286. (replace-match ""))
  9287. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9288. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9289. (defun org-compute-property-at-point ()
  9290. "Compute the property at point.
  9291. This looks for an enclosing column format, extracts the operator and
  9292. then applies it to the proerty in the column format's scope."
  9293. (interactive)
  9294. (unless (org-at-property-p)
  9295. (error "Not at a property"))
  9296. (let ((prop (org-match-string-no-properties 2)))
  9297. (org-columns-get-format-and-top-level)
  9298. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9299. (error "No operator defined for property %s" prop))
  9300. (org-columns-compute prop)))
  9301. (defun org-property-get-allowed-values (pom property &optional table)
  9302. "Get allowed values for the property PROPERTY.
  9303. When TABLE is non-nil, return an alist that can directly be used for
  9304. completion."
  9305. (let (vals)
  9306. (cond
  9307. ((equal property "TODO")
  9308. (setq vals (org-with-point-at pom
  9309. (append org-todo-keywords-1 '("")))))
  9310. ((equal property "PRIORITY")
  9311. (let ((n org-lowest-priority))
  9312. (while (>= n org-highest-priority)
  9313. (push (char-to-string n) vals)
  9314. (setq n (1- n)))))
  9315. ((member property org-special-properties))
  9316. (t
  9317. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9318. (when (and vals (string-match "\\S-" vals))
  9319. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9320. (setq vals (mapcar (lambda (x)
  9321. (cond ((stringp x) x)
  9322. ((numberp x) (number-to-string x))
  9323. ((symbolp x) (symbol-name x))
  9324. (t "???")))
  9325. vals)))))
  9326. (if table (mapcar 'list vals) vals)))
  9327. (defun org-property-previous-allowed-value (&optional previous)
  9328. "Switch to the next allowed value for this property."
  9329. (interactive)
  9330. (org-property-next-allowed-value t))
  9331. (defun org-property-next-allowed-value (&optional previous)
  9332. "Switch to the next allowed value for this property."
  9333. (interactive)
  9334. (unless (org-at-property-p)
  9335. (error "Not at a property"))
  9336. (let* ((key (match-string 2))
  9337. (value (match-string 3))
  9338. (allowed (or (org-property-get-allowed-values (point) key)
  9339. (and (member value '("[ ]" "[-]" "[X]"))
  9340. '("[ ]" "[X]"))))
  9341. nval)
  9342. (unless allowed
  9343. (error "Allowed values for this property have not been defined"))
  9344. (if previous (setq allowed (reverse allowed)))
  9345. (if (member value allowed)
  9346. (setq nval (car (cdr (member value allowed)))))
  9347. (setq nval (or nval (car allowed)))
  9348. (if (equal nval value)
  9349. (error "Only one allowed value for this property"))
  9350. (org-at-property-p)
  9351. (replace-match (concat " :" key ": " nval) t t)
  9352. (org-indent-line-function)
  9353. (beginning-of-line 1)
  9354. (skip-chars-forward " \t")))
  9355. (defun org-find-entry-with-id (ident)
  9356. "Locate the entry that contains the ID property with exact value IDENT.
  9357. IDENT can be a string, a symbol or a number, this function will search for
  9358. the string representation of it.
  9359. Return the position where this entry starts, or nil if there is no such entry."
  9360. (let ((id (cond
  9361. ((stringp ident) ident)
  9362. ((symbol-name ident) (symbol-name ident))
  9363. ((numberp ident) (number-to-string ident))
  9364. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9365. (case-fold-search nil))
  9366. (save-excursion
  9367. (save-restriction
  9368. (widen)
  9369. (goto-char (point-min))
  9370. (when (re-search-forward
  9371. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9372. nil t)
  9373. (org-back-to-heading)
  9374. (point))))))
  9375. ;;;; Timestamps
  9376. (defvar org-last-changed-timestamp nil)
  9377. (defvar org-last-inserted-timestamp nil
  9378. "The last time stamp inserted with `org-insert-time-stamp'.")
  9379. (defvar org-time-was-given) ; dynamically scoped parameter
  9380. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9381. (defvar org-ts-what) ; dynamically scoped parameter
  9382. (defun org-time-stamp (arg &optional inactive)
  9383. "Prompt for a date/time and insert a time stamp.
  9384. If the user specifies a time like HH:MM, or if this command is called
  9385. with a prefix argument, the time stamp will contain date and time.
  9386. Otherwise, only the date will be included. All parts of a date not
  9387. specified by the user will be filled in from the current date/time.
  9388. So if you press just return without typing anything, the time stamp
  9389. will represent the current date/time. If there is already a timestamp
  9390. at the cursor, it will be modified."
  9391. (interactive "P")
  9392. (let* ((ts nil)
  9393. (default-time
  9394. ;; Default time is either today, or, when entering a range,
  9395. ;; the range start.
  9396. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9397. (save-excursion
  9398. (re-search-backward
  9399. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9400. (- (point) 20) t)))
  9401. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9402. (current-time)))
  9403. (default-input (and ts (org-get-compact-tod ts)))
  9404. org-time-was-given org-end-time-was-given time)
  9405. (cond
  9406. ((and (org-at-timestamp-p t)
  9407. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9408. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9409. (insert "--")
  9410. (setq time (let ((this-command this-command))
  9411. (org-read-date arg 'totime nil nil
  9412. default-time default-input)))
  9413. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9414. ((org-at-timestamp-p t)
  9415. (setq time (let ((this-command this-command))
  9416. (org-read-date arg 'totime nil nil default-time default-input)))
  9417. (when (org-at-timestamp-p t) ; just to get the match data
  9418. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9419. (replace-match "")
  9420. (setq org-last-changed-timestamp
  9421. (org-insert-time-stamp
  9422. time (or org-time-was-given arg)
  9423. inactive nil nil (list org-end-time-was-given))))
  9424. (message "Timestamp updated"))
  9425. (t
  9426. (setq time (let ((this-command this-command))
  9427. (org-read-date arg 'totime nil nil default-time default-input)))
  9428. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9429. nil nil (list org-end-time-was-given))))))
  9430. ;; FIXME: can we use this for something else, like computing time differences?
  9431. (defun org-get-compact-tod (s)
  9432. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9433. (let* ((t1 (match-string 1 s))
  9434. (h1 (string-to-number (match-string 2 s)))
  9435. (m1 (string-to-number (match-string 3 s)))
  9436. (t2 (and (match-end 4) (match-string 5 s)))
  9437. (h2 (and t2 (string-to-number (match-string 6 s))))
  9438. (m2 (and t2 (string-to-number (match-string 7 s))))
  9439. dh dm)
  9440. (if (not t2)
  9441. t1
  9442. (setq dh (- h2 h1) dm (- m2 m1))
  9443. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9444. (concat t1 "+" (number-to-string dh)
  9445. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9446. (defun org-time-stamp-inactive (&optional arg)
  9447. "Insert an inactive time stamp.
  9448. An inactive time stamp is enclosed in square brackets instead of angle
  9449. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9450. does not link to the calendar and cannot be changed with the S-cursor keys.
  9451. So these are more for recording a certain time/date."
  9452. (interactive "P")
  9453. (org-time-stamp arg 'inactive))
  9454. (defvar org-date-ovl (org-make-overlay 1 1))
  9455. (org-overlay-put org-date-ovl 'face 'org-warning)
  9456. (org-detach-overlay org-date-ovl)
  9457. (defvar org-ans1) ; dynamically scoped parameter
  9458. (defvar org-ans2) ; dynamically scoped parameter
  9459. (defvar org-plain-time-of-day-regexp) ; defined below
  9460. (defvar org-overriding-default-time nil) ; dynamically scoped
  9461. (defvar org-read-date-overlay nil)
  9462. (defvar org-dcst nil) ; dynamically scoped
  9463. (defun org-read-date (&optional with-time to-time from-string prompt
  9464. default-time default-input)
  9465. "Read a date, possibly a time, and make things smooth for the user.
  9466. The prompt will suggest to enter an ISO date, but you can also enter anything
  9467. which will at least partially be understood by `parse-time-string'.
  9468. Unrecognized parts of the date will default to the current day, month, year,
  9469. hour and minute. If this command is called to replace a timestamp at point,
  9470. of to enter the second timestamp of a range, the default time is taken from the
  9471. existing stamp. For example,
  9472. 3-2-5 --> 2003-02-05
  9473. feb 15 --> currentyear-02-15
  9474. sep 12 9 --> 2009-09-12
  9475. 12:45 --> today 12:45
  9476. 22 sept 0:34 --> currentyear-09-22 0:34
  9477. 12 --> currentyear-currentmonth-12
  9478. Fri --> nearest Friday (today or later)
  9479. etc.
  9480. Furthermore you can specify a relative date by giving, as the *first* thing
  9481. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9482. change in days weeks, months, years.
  9483. With a single plus or minus, the date is relative to today. With a double
  9484. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9485. +4d --> four days from today
  9486. +4 --> same as above
  9487. +2w --> two weeks from today
  9488. ++5 --> five days from default date
  9489. The function understands only English month and weekday abbreviations,
  9490. but this can be configured with the variables `parse-time-months' and
  9491. `parse-time-weekdays'.
  9492. While prompting, a calendar is popped up - you can also select the
  9493. date with the mouse (button 1). The calendar shows a period of three
  9494. months. To scroll it to other months, use the keys `>' and `<'.
  9495. If you don't like the calendar, turn it off with
  9496. \(setq org-read-date-popup-calendar nil)
  9497. With optional argument TO-TIME, the date will immediately be converted
  9498. to an internal time.
  9499. With an optional argument WITH-TIME, the prompt will suggest to also
  9500. insert a time. Note that when WITH-TIME is not set, you can still
  9501. enter a time, and this function will inform the calling routine about
  9502. this change. The calling routine may then choose to change the format
  9503. used to insert the time stamp into the buffer to include the time.
  9504. With optional argument FROM-STRING, read from this string instead from
  9505. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9506. the time/date that is used for everything that is not specified by the
  9507. user."
  9508. (require 'parse-time)
  9509. (let* ((org-time-stamp-rounding-minutes
  9510. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9511. (org-dcst org-display-custom-times)
  9512. (ct (org-current-time))
  9513. (def (or org-overriding-default-time default-time ct))
  9514. (defdecode (decode-time def))
  9515. (dummy (progn
  9516. (when (< (nth 2 defdecode) org-extend-today-until)
  9517. (setcar (nthcdr 2 defdecode) -1)
  9518. (setcar (nthcdr 1 defdecode) 59)
  9519. (setq def (apply 'encode-time defdecode)
  9520. defdecode (decode-time def)))))
  9521. (calendar-move-hook nil)
  9522. (calendar-view-diary-initially-flag nil)
  9523. (view-diary-entries-initially nil)
  9524. (calendar-view-holidays-initially-flag nil)
  9525. (view-calendar-holidays-initially nil)
  9526. (timestr (format-time-string
  9527. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9528. (prompt (concat (if prompt (concat prompt " ") "")
  9529. (format "Date+time [%s]: " timestr)))
  9530. ans (org-ans0 "") org-ans1 org-ans2 final)
  9531. (cond
  9532. (from-string (setq ans from-string))
  9533. (org-read-date-popup-calendar
  9534. (save-excursion
  9535. (save-window-excursion
  9536. (calendar)
  9537. (calendar-forward-day (- (time-to-days def)
  9538. (calendar-absolute-from-gregorian
  9539. (calendar-current-date))))
  9540. (org-eval-in-calendar nil t)
  9541. (let* ((old-map (current-local-map))
  9542. (map (copy-keymap calendar-mode-map))
  9543. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9544. (org-defkey map (kbd "RET") 'org-calendar-select)
  9545. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9546. 'org-calendar-select-mouse)
  9547. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9548. 'org-calendar-select-mouse)
  9549. (org-defkey minibuffer-local-map [(meta shift left)]
  9550. (lambda () (interactive)
  9551. (org-eval-in-calendar '(calendar-backward-month 1))))
  9552. (org-defkey minibuffer-local-map [(meta shift right)]
  9553. (lambda () (interactive)
  9554. (org-eval-in-calendar '(calendar-forward-month 1))))
  9555. (org-defkey minibuffer-local-map [(meta shift up)]
  9556. (lambda () (interactive)
  9557. (org-eval-in-calendar '(calendar-backward-year 1))))
  9558. (org-defkey minibuffer-local-map [(meta shift down)]
  9559. (lambda () (interactive)
  9560. (org-eval-in-calendar '(calendar-forward-year 1))))
  9561. (org-defkey minibuffer-local-map [(shift up)]
  9562. (lambda () (interactive)
  9563. (org-eval-in-calendar '(calendar-backward-week 1))))
  9564. (org-defkey minibuffer-local-map [(shift down)]
  9565. (lambda () (interactive)
  9566. (org-eval-in-calendar '(calendar-forward-week 1))))
  9567. (org-defkey minibuffer-local-map [(shift left)]
  9568. (lambda () (interactive)
  9569. (org-eval-in-calendar '(calendar-backward-day 1))))
  9570. (org-defkey minibuffer-local-map [(shift right)]
  9571. (lambda () (interactive)
  9572. (org-eval-in-calendar '(calendar-forward-day 1))))
  9573. (org-defkey minibuffer-local-map ">"
  9574. (lambda () (interactive)
  9575. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9576. (org-defkey minibuffer-local-map "<"
  9577. (lambda () (interactive)
  9578. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9579. (unwind-protect
  9580. (progn
  9581. (use-local-map map)
  9582. (add-hook 'post-command-hook 'org-read-date-display)
  9583. (setq org-ans0 (read-string prompt default-input nil nil))
  9584. ;; org-ans0: from prompt
  9585. ;; org-ans1: from mouse click
  9586. ;; org-ans2: from calendar motion
  9587. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9588. (remove-hook 'post-command-hook 'org-read-date-display)
  9589. (use-local-map old-map)
  9590. (when org-read-date-overlay
  9591. (org-delete-overlay org-read-date-overlay)
  9592. (setq org-read-date-overlay nil)))))))
  9593. (t ; Naked prompt only
  9594. (unwind-protect
  9595. (setq ans (read-string prompt default-input nil timestr))
  9596. (when org-read-date-overlay
  9597. (org-delete-overlay org-read-date-overlay)
  9598. (setq org-read-date-overlay nil)))))
  9599. (setq final (org-read-date-analyze ans def defdecode))
  9600. (if to-time
  9601. (apply 'encode-time final)
  9602. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9603. (format "%04d-%02d-%02d %02d:%02d"
  9604. (nth 5 final) (nth 4 final) (nth 3 final)
  9605. (nth 2 final) (nth 1 final))
  9606. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9607. (defvar def)
  9608. (defvar defdecode)
  9609. (defvar with-time)
  9610. (defun org-read-date-display ()
  9611. "Display the currrent date prompt interpretation in the minibuffer."
  9612. (when org-read-date-display-live
  9613. (when org-read-date-overlay
  9614. (org-delete-overlay org-read-date-overlay))
  9615. (let ((p (point)))
  9616. (end-of-line 1)
  9617. (while (not (equal (buffer-substring
  9618. (max (point-min) (- (point) 4)) (point))
  9619. " "))
  9620. (insert " "))
  9621. (goto-char p))
  9622. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9623. " " (or org-ans1 org-ans2)))
  9624. (org-end-time-was-given nil)
  9625. (f (org-read-date-analyze ans def defdecode))
  9626. (fmts (if org-dcst
  9627. org-time-stamp-custom-formats
  9628. org-time-stamp-formats))
  9629. (fmt (if (or with-time
  9630. (and (boundp 'org-time-was-given) org-time-was-given))
  9631. (cdr fmts)
  9632. (car fmts)))
  9633. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9634. (when (and org-end-time-was-given
  9635. (string-match org-plain-time-of-day-regexp txt))
  9636. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9637. org-end-time-was-given
  9638. (substring txt (match-end 0)))))
  9639. (setq org-read-date-overlay
  9640. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9641. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9642. (defun org-read-date-analyze (ans def defdecode)
  9643. "Analyze the combined answer of the date prompt."
  9644. ;; FIXME: cleanup and comment
  9645. (let (delta deltan deltaw deltadef year month day
  9646. hour minute second wday pm h2 m2 tl wday1
  9647. iso-year iso-weekday iso-week iso-year iso-date)
  9648. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9649. (setq ans "+0"))
  9650. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9651. (setq ans (replace-match "" t t ans)
  9652. deltan (car delta)
  9653. deltaw (nth 1 delta)
  9654. deltadef (nth 2 delta)))
  9655. ;; Check if there is an iso week date in there
  9656. ;; If yes, sore the info and ostpone interpreting it until the rest
  9657. ;; of the parsing is done
  9658. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9659. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9660. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9661. iso-week (string-to-number (match-string 2 ans)))
  9662. (setq ans (replace-match "" t t ans)))
  9663. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9664. (when (string-match
  9665. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9666. (setq year (if (match-end 2)
  9667. (string-to-number (match-string 2 ans))
  9668. (string-to-number (format-time-string "%Y")))
  9669. month (string-to-number (match-string 3 ans))
  9670. day (string-to-number (match-string 4 ans)))
  9671. (if (< year 100) (setq year (+ 2000 year)))
  9672. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9673. t nil ans)))
  9674. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9675. ;; If there is a time with am/pm, and *no* time without it, we convert
  9676. ;; so that matching will be successful.
  9677. (loop for i from 1 to 2 do ; twice, for end time as well
  9678. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9679. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9680. (setq hour (string-to-number (match-string 1 ans))
  9681. minute (if (match-end 3)
  9682. (string-to-number (match-string 3 ans))
  9683. 0)
  9684. pm (equal ?p
  9685. (string-to-char (downcase (match-string 4 ans)))))
  9686. (if (and (= hour 12) (not pm))
  9687. (setq hour 0)
  9688. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9689. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9690. t t ans))))
  9691. ;; Check if a time range is given as a duration
  9692. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9693. (setq hour (string-to-number (match-string 1 ans))
  9694. h2 (+ hour (string-to-number (match-string 3 ans)))
  9695. minute (string-to-number (match-string 2 ans))
  9696. m2 (+ minute (if (match-end 5) (string-to-number
  9697. (match-string 5 ans))0)))
  9698. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9699. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9700. t t ans)))
  9701. ;; Check if there is a time range
  9702. (when (boundp 'org-end-time-was-given)
  9703. (setq org-time-was-given nil)
  9704. (when (and (string-match org-plain-time-of-day-regexp ans)
  9705. (match-end 8))
  9706. (setq org-end-time-was-given (match-string 8 ans))
  9707. (setq ans (concat (substring ans 0 (match-beginning 7))
  9708. (substring ans (match-end 7))))))
  9709. (setq tl (parse-time-string ans)
  9710. day (or (nth 3 tl) (nth 3 defdecode))
  9711. month (or (nth 4 tl)
  9712. (if (and org-read-date-prefer-future
  9713. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9714. (1+ (nth 4 defdecode))
  9715. (nth 4 defdecode)))
  9716. year (or (nth 5 tl)
  9717. (if (and org-read-date-prefer-future
  9718. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9719. (1+ (nth 5 defdecode))
  9720. (nth 5 defdecode)))
  9721. hour (or (nth 2 tl) (nth 2 defdecode))
  9722. minute (or (nth 1 tl) (nth 1 defdecode))
  9723. second (or (nth 0 tl) 0)
  9724. wday (nth 6 tl))
  9725. ;; Special date definitions below
  9726. (cond
  9727. (iso-week
  9728. ;; There was an iso week
  9729. (setq year (or iso-year year)
  9730. day (or iso-weekday wday 1)
  9731. wday nil ; to make sure that the trigger below does not match
  9732. iso-date (calendar-gregorian-from-absolute
  9733. (calendar-absolute-from-iso
  9734. (list iso-week day year))))
  9735. ; FIXME: Should we also push ISO weeks into the future?
  9736. ; (when (and org-read-date-prefer-future
  9737. ; (not iso-year)
  9738. ; (< (calendar-absolute-from-gregorian iso-date)
  9739. ; (time-to-days (current-time))))
  9740. ; (setq year (1+ year)
  9741. ; iso-date (calendar-gregorian-from-absolute
  9742. ; (calendar-absolute-from-iso
  9743. ; (list iso-week day year)))))
  9744. (setq month (car iso-date)
  9745. year (nth 2 iso-date)
  9746. day (nth 1 iso-date)))
  9747. (deltan
  9748. (unless deltadef
  9749. (let ((now (decode-time (current-time))))
  9750. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9751. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9752. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9753. ((equal deltaw "m") (setq month (+ month deltan)))
  9754. ((equal deltaw "y") (setq year (+ year deltan)))))
  9755. ((and wday (not (nth 3 tl)))
  9756. ;; Weekday was given, but no day, so pick that day in the week
  9757. ;; on or after the derived date.
  9758. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9759. (unless (equal wday wday1)
  9760. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9761. (if (and (boundp 'org-time-was-given)
  9762. (nth 2 tl))
  9763. (setq org-time-was-given t))
  9764. (if (< year 100) (setq year (+ 2000 year)))
  9765. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9766. (list second minute hour day month year)))
  9767. (defvar parse-time-weekdays)
  9768. (defun org-read-date-get-relative (s today default)
  9769. "Check string S for special relative date string.
  9770. TODAY and DEFAULT are internal times, for today and for a default.
  9771. Return shift list (N what def-flag)
  9772. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9773. N is the number of WHATs to shift.
  9774. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9775. the DEFAULT date rather than TODAY."
  9776. (when (and
  9777. (string-match
  9778. (concat
  9779. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9780. "\\([0-9]+\\)?"
  9781. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9782. "\\([ \t]\\|$\\)") s)
  9783. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9784. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9785. (string-to-char (substring (match-string 1 s) -1))
  9786. ?+))
  9787. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9788. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9789. (what (if (match-end 3) (match-string 3 s) "d"))
  9790. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9791. (date (if rel default today))
  9792. (wday (nth 6 (decode-time date)))
  9793. delta)
  9794. (if wday1
  9795. (progn
  9796. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9797. (if (= dir ?-) (setq delta (- delta 7)))
  9798. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9799. (list delta "d" rel))
  9800. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9801. (defun org-eval-in-calendar (form &optional keepdate)
  9802. "Eval FORM in the calendar window and return to current window.
  9803. Also, store the cursor date in variable org-ans2."
  9804. (let ((sw (selected-window)))
  9805. (select-window (get-buffer-window "*Calendar*"))
  9806. (eval form)
  9807. (when (and (not keepdate) (calendar-cursor-to-date))
  9808. (let* ((date (calendar-cursor-to-date))
  9809. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9810. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9811. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9812. (select-window sw)))
  9813. (defun org-calendar-select ()
  9814. "Return to `org-read-date' with the date currently selected.
  9815. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9816. (interactive)
  9817. (when (calendar-cursor-to-date)
  9818. (let* ((date (calendar-cursor-to-date))
  9819. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9820. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9821. (if (active-minibuffer-window) (exit-minibuffer))))
  9822. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9823. "Insert a date stamp for the date given by the internal TIME.
  9824. WITH-HM means, use the stamp format that includes the time of the day.
  9825. INACTIVE means use square brackets instead of angular ones, so that the
  9826. stamp will not contribute to the agenda.
  9827. PRE and POST are optional strings to be inserted before and after the
  9828. stamp.
  9829. The command returns the inserted time stamp."
  9830. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9831. stamp)
  9832. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9833. (insert-before-markers (or pre ""))
  9834. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9835. (when (listp extra)
  9836. (setq extra (car extra))
  9837. (if (and (stringp extra)
  9838. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9839. (setq extra (format "-%02d:%02d"
  9840. (string-to-number (match-string 1 extra))
  9841. (string-to-number (match-string 2 extra))))
  9842. (setq extra nil)))
  9843. (when extra
  9844. (backward-char 1)
  9845. (insert-before-markers extra)
  9846. (forward-char 1))
  9847. (insert-before-markers (or post ""))
  9848. (setq org-last-inserted-timestamp stamp)))
  9849. (defun org-toggle-time-stamp-overlays ()
  9850. "Toggle the use of custom time stamp formats."
  9851. (interactive)
  9852. (setq org-display-custom-times (not org-display-custom-times))
  9853. (unless org-display-custom-times
  9854. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9855. (while (setq p (next-single-property-change p 'display))
  9856. (if (and (get-text-property p 'display)
  9857. (eq (get-text-property p 'face) 'org-date))
  9858. (remove-text-properties
  9859. p (setq p (next-single-property-change p 'display))
  9860. '(display t))))
  9861. (set-buffer-modified-p bmp)))
  9862. (if (featurep 'xemacs)
  9863. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9864. (org-restart-font-lock)
  9865. (setq org-table-may-need-update t)
  9866. (if org-display-custom-times
  9867. (message "Time stamps are overlayed with custom format")
  9868. (message "Time stamp overlays removed")))
  9869. (defun org-display-custom-time (beg end)
  9870. "Overlay modified time stamp format over timestamp between BEG and END."
  9871. (let* ((ts (buffer-substring beg end))
  9872. t1 w1 with-hm tf time str w2 (off 0))
  9873. (save-match-data
  9874. (setq t1 (org-parse-time-string ts t))
  9875. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9876. (setq off (- (match-end 0) (match-beginning 0)))))
  9877. (setq end (- end off))
  9878. (setq w1 (- end beg)
  9879. with-hm (and (nth 1 t1) (nth 2 t1))
  9880. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9881. time (org-fix-decoded-time t1)
  9882. str (org-add-props
  9883. (format-time-string
  9884. (substring tf 1 -1) (apply 'encode-time time))
  9885. nil 'mouse-face 'highlight)
  9886. w2 (length str))
  9887. (if (not (= w2 w1))
  9888. (add-text-properties (1+ beg) (+ 2 beg)
  9889. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9890. (if (featurep 'xemacs)
  9891. (progn
  9892. (put-text-property beg end 'invisible t)
  9893. (put-text-property beg end 'end-glyph (make-glyph str)))
  9894. (put-text-property beg end 'display str))))
  9895. (defun org-translate-time (string)
  9896. "Translate all timestamps in STRING to custom format.
  9897. But do this only if the variable `org-display-custom-times' is set."
  9898. (when org-display-custom-times
  9899. (save-match-data
  9900. (let* ((start 0)
  9901. (re org-ts-regexp-both)
  9902. t1 with-hm inactive tf time str beg end)
  9903. (while (setq start (string-match re string start))
  9904. (setq beg (match-beginning 0)
  9905. end (match-end 0)
  9906. t1 (save-match-data
  9907. (org-parse-time-string (substring string beg end) t))
  9908. with-hm (and (nth 1 t1) (nth 2 t1))
  9909. inactive (equal (substring string beg (1+ beg)) "[")
  9910. tf (funcall (if with-hm 'cdr 'car)
  9911. org-time-stamp-custom-formats)
  9912. time (org-fix-decoded-time t1)
  9913. str (format-time-string
  9914. (concat
  9915. (if inactive "[" "<") (substring tf 1 -1)
  9916. (if inactive "]" ">"))
  9917. (apply 'encode-time time))
  9918. string (replace-match str t t string)
  9919. start (+ start (length str)))))))
  9920. string)
  9921. (defun org-fix-decoded-time (time)
  9922. "Set 0 instead of nil for the first 6 elements of time.
  9923. Don't touch the rest."
  9924. (let ((n 0))
  9925. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9926. (defun org-days-to-time (timestamp-string)
  9927. "Difference between TIMESTAMP-STRING and now in days."
  9928. (- (time-to-days (org-time-string-to-time timestamp-string))
  9929. (time-to-days (current-time))))
  9930. (defun org-deadline-close (timestamp-string &optional ndays)
  9931. "Is the time in TIMESTAMP-STRING close to the current date?"
  9932. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9933. (and (< (org-days-to-time timestamp-string) ndays)
  9934. (not (org-entry-is-done-p))))
  9935. (defun org-get-wdays (ts)
  9936. "Get the deadline lead time appropriate for timestring TS."
  9937. (cond
  9938. ((<= org-deadline-warning-days 0)
  9939. ;; 0 or negative, enforce this value no matter what
  9940. (- org-deadline-warning-days))
  9941. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9942. ;; lead time is specified.
  9943. (floor (* (string-to-number (match-string 1 ts))
  9944. (cdr (assoc (match-string 2 ts)
  9945. '(("d" . 1) ("w" . 7)
  9946. ("m" . 30.4) ("y" . 365.25)))))))
  9947. ;; go for the default.
  9948. (t org-deadline-warning-days)))
  9949. (defun org-calendar-select-mouse (ev)
  9950. "Return to `org-read-date' with the date currently selected.
  9951. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9952. (interactive "e")
  9953. (mouse-set-point ev)
  9954. (when (calendar-cursor-to-date)
  9955. (let* ((date (calendar-cursor-to-date))
  9956. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9957. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9958. (if (active-minibuffer-window) (exit-minibuffer))))
  9959. (defun org-check-deadlines (ndays)
  9960. "Check if there are any deadlines due or past due.
  9961. A deadline is considered due if it happens within `org-deadline-warning-days'
  9962. days from today's date. If the deadline appears in an entry marked DONE,
  9963. it is not shown. The prefix arg NDAYS can be used to test that many
  9964. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9965. (interactive "P")
  9966. (let* ((org-warn-days
  9967. (cond
  9968. ((equal ndays '(4)) 100000)
  9969. (ndays (prefix-numeric-value ndays))
  9970. (t (abs org-deadline-warning-days))))
  9971. (case-fold-search nil)
  9972. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9973. (callback
  9974. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9975. (message "%d deadlines past-due or due within %d days"
  9976. (org-occur regexp nil callback)
  9977. org-warn-days)))
  9978. (defun org-check-before-date (date)
  9979. "Check if there are deadlines or scheduled entries before DATE."
  9980. (interactive (list (org-read-date)))
  9981. (let ((case-fold-search nil)
  9982. (regexp (concat "\\<\\(" org-deadline-string
  9983. "\\|" org-scheduled-string
  9984. "\\) *<\\([^>]+\\)>"))
  9985. (callback
  9986. (lambda () (time-less-p
  9987. (org-time-string-to-time (match-string 2))
  9988. (org-time-string-to-time date)))))
  9989. (message "%d entries before %s"
  9990. (org-occur regexp nil callback) date)))
  9991. (defun org-evaluate-time-range (&optional to-buffer)
  9992. "Evaluate a time range by computing the difference between start and end.
  9993. Normally the result is just printed in the echo area, but with prefix arg
  9994. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9995. If the time range is actually in a table, the result is inserted into the
  9996. next column.
  9997. For time difference computation, a year is assumed to be exactly 365
  9998. days in order to avoid rounding problems."
  9999. (interactive "P")
  10000. (or
  10001. (org-clock-update-time-maybe)
  10002. (save-excursion
  10003. (unless (org-at-date-range-p t)
  10004. (goto-char (point-at-bol))
  10005. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10006. (if (not (org-at-date-range-p t))
  10007. (error "Not at a time-stamp range, and none found in current line")))
  10008. (let* ((ts1 (match-string 1))
  10009. (ts2 (match-string 2))
  10010. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10011. (match-end (match-end 0))
  10012. (time1 (org-time-string-to-time ts1))
  10013. (time2 (org-time-string-to-time ts2))
  10014. (t1 (time-to-seconds time1))
  10015. (t2 (time-to-seconds time2))
  10016. (diff (abs (- t2 t1)))
  10017. (negative (< (- t2 t1) 0))
  10018. ;; (ys (floor (* 365 24 60 60)))
  10019. (ds (* 24 60 60))
  10020. (hs (* 60 60))
  10021. (fy "%dy %dd %02d:%02d")
  10022. (fy1 "%dy %dd")
  10023. (fd "%dd %02d:%02d")
  10024. (fd1 "%dd")
  10025. (fh "%02d:%02d")
  10026. y d h m align)
  10027. (if havetime
  10028. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10029. y 0
  10030. d (floor (/ diff ds)) diff (mod diff ds)
  10031. h (floor (/ diff hs)) diff (mod diff hs)
  10032. m (floor (/ diff 60)))
  10033. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10034. y 0
  10035. d (floor (+ (/ diff ds) 0.5))
  10036. h 0 m 0))
  10037. (if (not to-buffer)
  10038. (message "%s" (org-make-tdiff-string y d h m))
  10039. (if (org-at-table-p)
  10040. (progn
  10041. (goto-char match-end)
  10042. (setq align t)
  10043. (and (looking-at " *|") (goto-char (match-end 0))))
  10044. (goto-char match-end))
  10045. (if (looking-at
  10046. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10047. (replace-match ""))
  10048. (if negative (insert " -"))
  10049. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10050. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10051. (insert " " (format fh h m))))
  10052. (if align (org-table-align))
  10053. (message "Time difference inserted")))))
  10054. (defun org-make-tdiff-string (y d h m)
  10055. (let ((fmt "")
  10056. (l nil))
  10057. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10058. l (push y l)))
  10059. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10060. l (push d l)))
  10061. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10062. l (push h l)))
  10063. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10064. l (push m l)))
  10065. (apply 'format fmt (nreverse l))))
  10066. (defun org-time-string-to-time (s)
  10067. (apply 'encode-time (org-parse-time-string s)))
  10068. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10069. "Convert a time stamp to an absolute day number.
  10070. If there is a specifyer for a cyclic time stamp, get the closest date to
  10071. DAYNR.
  10072. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10073. (cond
  10074. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10075. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10076. daynr
  10077. (+ daynr 1000)))
  10078. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10079. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10080. (time-to-days (current-time))) (match-string 0 s)
  10081. prefer show-all))
  10082. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10083. (defun org-days-to-iso-week (days)
  10084. "Return the iso week number."
  10085. (require 'cal-iso)
  10086. (car (calendar-iso-from-absolute days)))
  10087. (defun org-small-year-to-year (year)
  10088. "Convert 2-digit years into 4-digit years.
  10089. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10090. The year 2000 cannot be abbreviated. Any year lager than 99
  10091. is retrned unchanged."
  10092. (if (< year 38)
  10093. (setq year (+ 2000 year))
  10094. (if (< year 100)
  10095. (setq year (+ 1900 year))))
  10096. year)
  10097. (defun org-time-from-absolute (d)
  10098. "Return the time corresponding to date D.
  10099. D may be an absolute day number, or a calendar-type list (month day year)."
  10100. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10101. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10102. (defun org-calendar-holiday ()
  10103. "List of holidays, for Diary display in Org-mode."
  10104. (require 'holidays)
  10105. (let ((hl (funcall
  10106. (if (fboundp 'calendar-check-holidays)
  10107. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10108. (if hl (mapconcat 'identity hl "; "))))
  10109. (defun org-diary-sexp-entry (sexp entry date)
  10110. "Process a SEXP diary ENTRY for DATE."
  10111. (require 'diary-lib)
  10112. (let ((result (if calendar-debug-sexp
  10113. (let ((stack-trace-on-error t))
  10114. (eval (car (read-from-string sexp))))
  10115. (condition-case nil
  10116. (eval (car (read-from-string sexp)))
  10117. (error
  10118. (beep)
  10119. (message "Bad sexp at line %d in %s: %s"
  10120. (org-current-line)
  10121. (buffer-file-name) sexp)
  10122. (sleep-for 2))))))
  10123. (cond ((stringp result) result)
  10124. ((and (consp result)
  10125. (stringp (cdr result))) (cdr result))
  10126. (result entry)
  10127. (t nil))))
  10128. (defun org-diary-to-ical-string (frombuf)
  10129. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10130. This uses the icalendar.el library."
  10131. (let* ((tmpdir (if (featurep 'xemacs)
  10132. (temp-directory)
  10133. temporary-file-directory))
  10134. (tmpfile (make-temp-name
  10135. (expand-file-name "orgics" tmpdir)))
  10136. buf rtn b e)
  10137. (save-excursion
  10138. (set-buffer frombuf)
  10139. (icalendar-export-region (point-min) (point-max) tmpfile)
  10140. (setq buf (find-buffer-visiting tmpfile))
  10141. (set-buffer buf)
  10142. (goto-char (point-min))
  10143. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10144. (setq b (match-beginning 0)))
  10145. (goto-char (point-max))
  10146. (if (re-search-backward "^END:VEVENT" nil t)
  10147. (setq e (match-end 0)))
  10148. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10149. (kill-buffer buf)
  10150. (delete-file tmpfile)
  10151. rtn))
  10152. (defun org-closest-date (start current change prefer show-all)
  10153. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10154. When PREFER is `past' return a date that is either CURRENT or past.
  10155. When PREFER is `future', return a date that is either CURRENT or future.
  10156. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10157. ;; Make the proper lists from the dates
  10158. (catch 'exit
  10159. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10160. dn dw sday cday n1 n2
  10161. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10162. (setq start (org-date-to-gregorian start)
  10163. current (org-date-to-gregorian
  10164. (if show-all
  10165. current
  10166. (time-to-days (current-time))))
  10167. sday (calendar-absolute-from-gregorian start)
  10168. cday (calendar-absolute-from-gregorian current))
  10169. (if (<= cday sday) (throw 'exit sday))
  10170. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10171. (setq dn (string-to-number (match-string 1 change))
  10172. dw (cdr (assoc (match-string 2 change) a1)))
  10173. (error "Invalid change specifyer: %s" change))
  10174. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10175. (cond
  10176. ((eq dw 'day)
  10177. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10178. n2 (+ n1 dn)))
  10179. ((eq dw 'year)
  10180. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10181. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10182. (setq date1 (list m d y1)
  10183. n1 (calendar-absolute-from-gregorian date1)
  10184. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10185. n2 (calendar-absolute-from-gregorian date2)))
  10186. ((eq dw 'month)
  10187. ;; approx number of month between the two dates
  10188. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10189. ;; How often does dn fit in there?
  10190. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10191. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10192. m (+ m nm)
  10193. ny (floor (/ m 12))
  10194. y (+ y ny)
  10195. m (- m (* ny 12)))
  10196. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10197. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10198. (setq m2 (+ m dn) y2 y)
  10199. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10200. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10201. (while (<= n2 cday)
  10202. (setq n1 n2 m m2 y y2)
  10203. (setq m2 (+ m dn) y2 y)
  10204. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10205. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10206. (if show-all
  10207. (cond
  10208. ((eq prefer 'past) n1)
  10209. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10210. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10211. (cond
  10212. ((eq prefer 'past) n1)
  10213. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10214. (t (if (= cday n1) n1 n2)))))))
  10215. (defun org-date-to-gregorian (date)
  10216. "Turn any specification of DATE into a gregorian date for the calendar."
  10217. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10218. ((and (listp date) (= (length date) 3)) date)
  10219. ((stringp date)
  10220. (setq date (org-parse-time-string date))
  10221. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10222. ((listp date)
  10223. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10224. (defun org-parse-time-string (s &optional nodefault)
  10225. "Parse the standard Org-mode time string.
  10226. This should be a lot faster than the normal `parse-time-string'.
  10227. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10228. hour and minute fields will be nil if not given."
  10229. (if (string-match org-ts-regexp0 s)
  10230. (list 0
  10231. (if (or (match-beginning 8) (not nodefault))
  10232. (string-to-number (or (match-string 8 s) "0")))
  10233. (if (or (match-beginning 7) (not nodefault))
  10234. (string-to-number (or (match-string 7 s) "0")))
  10235. (string-to-number (match-string 4 s))
  10236. (string-to-number (match-string 3 s))
  10237. (string-to-number (match-string 2 s))
  10238. nil nil nil)
  10239. (make-list 9 0)))
  10240. (defun org-timestamp-up (&optional arg)
  10241. "Increase the date item at the cursor by one.
  10242. If the cursor is on the year, change the year. If it is on the month or
  10243. the day, change that.
  10244. With prefix ARG, change by that many units."
  10245. (interactive "p")
  10246. (org-timestamp-change (prefix-numeric-value arg)))
  10247. (defun org-timestamp-down (&optional arg)
  10248. "Decrease the date item at the cursor by one.
  10249. If the cursor is on the year, change the year. If it is on the month or
  10250. the day, change that.
  10251. With prefix ARG, change by that many units."
  10252. (interactive "p")
  10253. (org-timestamp-change (- (prefix-numeric-value arg))))
  10254. (defun org-timestamp-up-day (&optional arg)
  10255. "Increase the date in the time stamp by one day.
  10256. With prefix ARG, change that many days."
  10257. (interactive "p")
  10258. (if (and (not (org-at-timestamp-p t))
  10259. (org-on-heading-p))
  10260. (org-todo 'up)
  10261. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10262. (defun org-timestamp-down-day (&optional arg)
  10263. "Decrease the date in the time stamp by one day.
  10264. With prefix ARG, change that many days."
  10265. (interactive "p")
  10266. (if (and (not (org-at-timestamp-p t))
  10267. (org-on-heading-p))
  10268. (org-todo 'down)
  10269. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10270. (defun org-at-timestamp-p (&optional inactive-ok)
  10271. "Determine if the cursor is in or at a timestamp."
  10272. (interactive)
  10273. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10274. (pos (point))
  10275. (ans (or (looking-at tsr)
  10276. (save-excursion
  10277. (skip-chars-backward "^[<\n\r\t")
  10278. (if (> (point) (point-min)) (backward-char 1))
  10279. (and (looking-at tsr)
  10280. (> (- (match-end 0) pos) -1))))))
  10281. (and ans
  10282. (boundp 'org-ts-what)
  10283. (setq org-ts-what
  10284. (cond
  10285. ((= pos (match-beginning 0)) 'bracket)
  10286. ((= pos (1- (match-end 0))) 'bracket)
  10287. ((org-pos-in-match-range pos 2) 'year)
  10288. ((org-pos-in-match-range pos 3) 'month)
  10289. ((org-pos-in-match-range pos 7) 'hour)
  10290. ((org-pos-in-match-range pos 8) 'minute)
  10291. ((or (org-pos-in-match-range pos 4)
  10292. (org-pos-in-match-range pos 5)) 'day)
  10293. ((and (> pos (or (match-end 8) (match-end 5)))
  10294. (< pos (match-end 0)))
  10295. (- pos (or (match-end 8) (match-end 5))))
  10296. (t 'day))))
  10297. ans))
  10298. (defun org-toggle-timestamp-type ()
  10299. "Toggle the type (<active> or [inactive]) of a time stamp."
  10300. (interactive)
  10301. (when (org-at-timestamp-p t)
  10302. (save-excursion
  10303. (goto-char (match-beginning 0))
  10304. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10305. (goto-char (1- (match-end 0)))
  10306. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10307. (message "Timestamp is now %sactive"
  10308. (if (equal (char-before) ?>) "in" ""))))
  10309. (defun org-timestamp-change (n &optional what)
  10310. "Change the date in the time stamp at point.
  10311. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10312. `year', `minute', `second'. If WHAT is not given, the cursor position
  10313. in the timestamp determines what will be changed."
  10314. (let ((pos (point))
  10315. with-hm inactive
  10316. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10317. org-ts-what
  10318. extra rem
  10319. ts time time0)
  10320. (if (not (org-at-timestamp-p t))
  10321. (error "Not at a timestamp"))
  10322. (if (and (not what) (eq org-ts-what 'bracket))
  10323. (org-toggle-timestamp-type)
  10324. (if (and (not what) (not (eq org-ts-what 'day))
  10325. org-display-custom-times
  10326. (get-text-property (point) 'display)
  10327. (not (get-text-property (1- (point)) 'display)))
  10328. (setq org-ts-what 'day))
  10329. (setq org-ts-what (or what org-ts-what)
  10330. inactive (= (char-after (match-beginning 0)) ?\[)
  10331. ts (match-string 0))
  10332. (replace-match "")
  10333. (if (string-match
  10334. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10335. ts)
  10336. (setq extra (match-string 1 ts)))
  10337. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10338. (setq with-hm t))
  10339. (setq time0 (org-parse-time-string ts))
  10340. (when (and (eq org-ts-what 'minute)
  10341. (eq current-prefix-arg nil))
  10342. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10343. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10344. (setcar (cdr time0) (+ (nth 1 time0)
  10345. (if (> n 0) (- rem) (- dm rem))))))
  10346. (setq time
  10347. (encode-time (or (car time0) 0)
  10348. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10349. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10350. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10351. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10352. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10353. (nthcdr 6 time0)))
  10354. (when (integerp org-ts-what)
  10355. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10356. (if (eq what 'calendar)
  10357. (let ((cal-date (org-get-date-from-calendar)))
  10358. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10359. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10360. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10361. (setcar time0 (or (car time0) 0))
  10362. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10363. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10364. (setq time (apply 'encode-time time0))))
  10365. (setq org-last-changed-timestamp
  10366. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10367. (org-clock-update-time-maybe)
  10368. (goto-char pos)
  10369. ;; Try to recenter the calendar window, if any
  10370. (if (and org-calendar-follow-timestamp-change
  10371. (get-buffer-window "*Calendar*" t)
  10372. (memq org-ts-what '(day month year)))
  10373. (org-recenter-calendar (time-to-days time))))))
  10374. (defun org-modify-ts-extra (s pos n dm)
  10375. "Change the different parts of the lead-time and repeat fields in timestamp."
  10376. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10377. ng h m new rem)
  10378. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10379. (cond
  10380. ((or (org-pos-in-match-range pos 2)
  10381. (org-pos-in-match-range pos 3))
  10382. (setq m (string-to-number (match-string 3 s))
  10383. h (string-to-number (match-string 2 s)))
  10384. (if (org-pos-in-match-range pos 2)
  10385. (setq h (+ h n))
  10386. (setq n (* dm (org-no-warnings (signum n))))
  10387. (when (not (= 0 (setq rem (% m dm))))
  10388. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10389. (setq m (+ m n)))
  10390. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10391. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10392. (setq h (min 24 (max 0 h)))
  10393. (setq ng 1 new (format "-%02d:%02d" h m)))
  10394. ((org-pos-in-match-range pos 6)
  10395. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10396. ((org-pos-in-match-range pos 5)
  10397. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10398. ((org-pos-in-match-range pos 9)
  10399. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10400. ((org-pos-in-match-range pos 8)
  10401. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10402. (when ng
  10403. (setq s (concat
  10404. (substring s 0 (match-beginning ng))
  10405. new
  10406. (substring s (match-end ng))))))
  10407. s))
  10408. (defun org-recenter-calendar (date)
  10409. "If the calendar is visible, recenter it to DATE."
  10410. (let* ((win (selected-window))
  10411. (cwin (get-buffer-window "*Calendar*" t))
  10412. (calendar-move-hook nil))
  10413. (when cwin
  10414. (select-window cwin)
  10415. (calendar-goto-date (if (listp date) date
  10416. (calendar-gregorian-from-absolute date)))
  10417. (select-window win))))
  10418. (defun org-goto-calendar (&optional arg)
  10419. "Go to the Emacs calendar at the current date.
  10420. If there is a time stamp in the current line, go to that date.
  10421. A prefix ARG can be used to force the current date."
  10422. (interactive "P")
  10423. (let ((tsr org-ts-regexp) diff
  10424. (calendar-move-hook nil)
  10425. (calendar-view-holidays-initially-flag nil)
  10426. (view-calendar-holidays-initially nil)
  10427. (calendar-view-diary-initially-flag nil)
  10428. (view-diary-entries-initially nil))
  10429. (if (or (org-at-timestamp-p)
  10430. (save-excursion
  10431. (beginning-of-line 1)
  10432. (looking-at (concat ".*" tsr))))
  10433. (let ((d1 (time-to-days (current-time)))
  10434. (d2 (time-to-days
  10435. (org-time-string-to-time (match-string 1)))))
  10436. (setq diff (- d2 d1))))
  10437. (calendar)
  10438. (calendar-goto-today)
  10439. (if (and diff (not arg)) (calendar-forward-day diff))))
  10440. (defun org-get-date-from-calendar ()
  10441. "Return a list (month day year) of date at point in calendar."
  10442. (with-current-buffer "*Calendar*"
  10443. (save-match-data
  10444. (calendar-cursor-to-date))))
  10445. (defun org-date-from-calendar ()
  10446. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10447. If there is already a time stamp at the cursor position, update it."
  10448. (interactive)
  10449. (if (org-at-timestamp-p t)
  10450. (org-timestamp-change 0 'calendar)
  10451. (let ((cal-date (org-get-date-from-calendar)))
  10452. (org-insert-time-stamp
  10453. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10454. (defun org-minutes-to-hh:mm-string (m)
  10455. "Compute H:MM from a number of minutes."
  10456. (let ((h (/ m 60)))
  10457. (setq m (- m (* 60 h)))
  10458. (format org-time-clocksum-format h m)))
  10459. (defun org-hh:mm-string-to-minutes (s)
  10460. "Convert a string H:MM to a number of minutes."
  10461. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10462. (+ (* (string-to-number (match-string 1 s)) 60)
  10463. (string-to-number (match-string 2 s)))
  10464. 0))
  10465. ;;;; Agenda files
  10466. ;;;###autoload
  10467. (defun org-iswitchb (&optional arg)
  10468. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10469. With a prefix argument, restrict available to files.
  10470. With two prefix arguments, restrict available buffers to agenda files.
  10471. Due to some yet unresolved reason, the global function
  10472. `iswitchb-mode' needs to be active for this function to work."
  10473. (interactive "P")
  10474. (require 'iswitchb)
  10475. (let ((enabled iswitchb-mode) blist)
  10476. (or enabled (iswitchb-mode 1))
  10477. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10478. ((equal arg '(16)) (org-buffer-list 'agenda))
  10479. (t (org-buffer-list))))
  10480. (unwind-protect
  10481. (let ((iswitchb-make-buflist-hook
  10482. (lambda ()
  10483. (setq iswitchb-temp-buflist
  10484. (mapcar 'buffer-name blist)))))
  10485. (switch-to-buffer
  10486. (iswitchb-read-buffer
  10487. "Switch-to: " nil t))
  10488. (or enabled (iswitchb-mode -1))))))
  10489. (defun org-buffer-list (&optional predicate exclude-tmp)
  10490. "Return a list of Org buffers.
  10491. PREDICATE can be `export', `files' or `agenda'.
  10492. export restrict the list to Export buffers.
  10493. files restrict the list to buffers visiting Org files.
  10494. agenda restrict the list to buffers visiting agenda files.
  10495. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10496. (let* ((bfn nil)
  10497. (agenda-files (and (eq predicate 'agenda)
  10498. (mapcar 'file-truename (org-agenda-files t))))
  10499. (filter
  10500. (cond
  10501. ((eq predicate 'files)
  10502. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10503. ((eq predicate 'export)
  10504. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10505. ((eq predicate 'agenda)
  10506. (lambda (b)
  10507. (with-current-buffer b
  10508. (and (eq major-mode 'org-mode)
  10509. (setq bfn (buffer-file-name b))
  10510. (member (file-truename bfn) agenda-files)))))
  10511. (t (lambda (b) (with-current-buffer b
  10512. (or (eq major-mode 'org-mode)
  10513. (string-match "\*Org .*Export"
  10514. (buffer-name b)))))))))
  10515. (delq nil
  10516. (mapcar
  10517. (lambda(b)
  10518. (if (and (funcall filter b)
  10519. (or (not exclude-tmp)
  10520. (not (string-match "tmp" (buffer-name b)))))
  10521. b
  10522. nil))
  10523. (buffer-list)))))
  10524. (defun org-agenda-files (&optional unrestricted archives)
  10525. "Get the list of agenda files.
  10526. Optional UNRESTRICTED means return the full list even if a restriction
  10527. is currently in place.
  10528. When ARCHIVES is t, include all archive files hat are really being
  10529. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10530. `org-agenda-archives-mode' is t."
  10531. (let ((files
  10532. (cond
  10533. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10534. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10535. ((listp org-agenda-files) org-agenda-files)
  10536. (t (error "Invalid value of `org-agenda-files'")))))
  10537. (setq files (apply 'append
  10538. (mapcar (lambda (f)
  10539. (if (file-directory-p f)
  10540. (directory-files
  10541. f t org-agenda-file-regexp)
  10542. (list f)))
  10543. files)))
  10544. (when org-agenda-skip-unavailable-files
  10545. (setq files (delq nil
  10546. (mapcar (function
  10547. (lambda (file)
  10548. (and (file-readable-p file) file)))
  10549. files))))
  10550. (when (or (eq archives t)
  10551. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10552. (setq files (org-add-archive-files files)))
  10553. files))
  10554. (defun org-edit-agenda-file-list ()
  10555. "Edit the list of agenda files.
  10556. Depending on setup, this either uses customize to edit the variable
  10557. `org-agenda-files', or it visits the file that is holding the list. In the
  10558. latter case, the buffer is set up in a way that saving it automatically kills
  10559. the buffer and restores the previous window configuration."
  10560. (interactive)
  10561. (if (stringp org-agenda-files)
  10562. (let ((cw (current-window-configuration)))
  10563. (find-file org-agenda-files)
  10564. (org-set-local 'org-window-configuration cw)
  10565. (org-add-hook 'after-save-hook
  10566. (lambda ()
  10567. (set-window-configuration
  10568. (prog1 org-window-configuration
  10569. (kill-buffer (current-buffer))))
  10570. (org-install-agenda-files-menu)
  10571. (message "New agenda file list installed"))
  10572. nil 'local)
  10573. (message "%s" (substitute-command-keys
  10574. "Edit list and finish with \\[save-buffer]")))
  10575. (customize-variable 'org-agenda-files)))
  10576. (defun org-store-new-agenda-file-list (list)
  10577. "Set new value for the agenda file list and save it correcly."
  10578. (if (stringp org-agenda-files)
  10579. (let ((f org-agenda-files) b)
  10580. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10581. (with-temp-file f
  10582. (insert (mapconcat 'identity list "\n") "\n")))
  10583. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10584. (setq org-agenda-files list)
  10585. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10586. (defun org-read-agenda-file-list ()
  10587. "Read the list of agenda files from a file."
  10588. (when (file-directory-p org-agenda-files)
  10589. (error "`org-agenda-files' cannot be a single directory"))
  10590. (when (stringp org-agenda-files)
  10591. (with-temp-buffer
  10592. (insert-file-contents org-agenda-files)
  10593. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10594. ;;;###autoload
  10595. (defun org-cycle-agenda-files ()
  10596. "Cycle through the files in `org-agenda-files'.
  10597. If the current buffer visits an agenda file, find the next one in the list.
  10598. If the current buffer does not, find the first agenda file."
  10599. (interactive)
  10600. (let* ((fs (org-agenda-files t))
  10601. (files (append fs (list (car fs))))
  10602. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10603. file)
  10604. (unless files (error "No agenda files"))
  10605. (catch 'exit
  10606. (while (setq file (pop files))
  10607. (if (equal (file-truename file) tcf)
  10608. (when (car files)
  10609. (find-file (car files))
  10610. (throw 'exit t))))
  10611. (find-file (car fs)))
  10612. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10613. (defun org-agenda-file-to-front (&optional to-end)
  10614. "Move/add the current file to the top of the agenda file list.
  10615. If the file is not present in the list, it is added to the front. If it is
  10616. present, it is moved there. With optional argument TO-END, add/move to the
  10617. end of the list."
  10618. (interactive "P")
  10619. (let ((org-agenda-skip-unavailable-files nil)
  10620. (file-alist (mapcar (lambda (x)
  10621. (cons (file-truename x) x))
  10622. (org-agenda-files t)))
  10623. (ctf (file-truename buffer-file-name))
  10624. x had)
  10625. (setq x (assoc ctf file-alist) had x)
  10626. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10627. (if to-end
  10628. (setq file-alist (append (delq x file-alist) (list x)))
  10629. (setq file-alist (cons x (delq x file-alist))))
  10630. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10631. (org-install-agenda-files-menu)
  10632. (message "File %s to %s of agenda file list"
  10633. (if had "moved" "added") (if to-end "end" "front"))))
  10634. (defun org-remove-file (&optional file)
  10635. "Remove current file from the list of files in variable `org-agenda-files'.
  10636. These are the files which are being checked for agenda entries.
  10637. Optional argument FILE means, use this file instead of the current."
  10638. (interactive)
  10639. (let* ((org-agenda-skip-unavailable-files nil)
  10640. (file (or file buffer-file-name))
  10641. (true-file (file-truename file))
  10642. (afile (abbreviate-file-name file))
  10643. (files (delq nil (mapcar
  10644. (lambda (x)
  10645. (if (equal true-file
  10646. (file-truename x))
  10647. nil x))
  10648. (org-agenda-files t)))))
  10649. (if (not (= (length files) (length (org-agenda-files t))))
  10650. (progn
  10651. (org-store-new-agenda-file-list files)
  10652. (org-install-agenda-files-menu)
  10653. (message "Removed file: %s" afile))
  10654. (message "File was not in list: %s (not removed)" afile))))
  10655. (defun org-file-menu-entry (file)
  10656. (vector file (list 'find-file file) t))
  10657. (defun org-check-agenda-file (file)
  10658. "Make sure FILE exists. If not, ask user what to do."
  10659. (when (not (file-exists-p file))
  10660. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10661. (abbreviate-file-name file))
  10662. (let ((r (downcase (read-char-exclusive))))
  10663. (cond
  10664. ((equal r ?r)
  10665. (org-remove-file file)
  10666. (throw 'nextfile t))
  10667. (t (error "Abort"))))))
  10668. (defun org-get-agenda-file-buffer (file)
  10669. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10670. it to the list of buffers which might be released later."
  10671. (let ((buf (org-find-base-buffer-visiting file)))
  10672. (if buf
  10673. buf ; just return it
  10674. ;; Make a new buffer and remember it
  10675. (setq buf (find-file-noselect file))
  10676. (if buf (push buf org-agenda-new-buffers))
  10677. buf)))
  10678. (defun org-release-buffers (blist)
  10679. "Release all buffers in list, asking the user for confirmation when needed.
  10680. When a buffer is unmodified, it is just killed. When modified, it is saved
  10681. \(if the user agrees) and then killed."
  10682. (let (buf file)
  10683. (while (setq buf (pop blist))
  10684. (setq file (buffer-file-name buf))
  10685. (when (and (buffer-modified-p buf)
  10686. file
  10687. (y-or-n-p (format "Save file %s? " file)))
  10688. (with-current-buffer buf (save-buffer)))
  10689. (kill-buffer buf))))
  10690. (defun org-prepare-agenda-buffers (files)
  10691. "Create buffers for all agenda files, protect archived trees and comments."
  10692. (interactive)
  10693. (let ((pa '(:org-archived t))
  10694. (pc '(:org-comment t))
  10695. (pall '(:org-archived t :org-comment t))
  10696. (inhibit-read-only t)
  10697. (rea (concat ":" org-archive-tag ":"))
  10698. bmp file re)
  10699. (save-excursion
  10700. (save-restriction
  10701. (while (setq file (pop files))
  10702. (if (bufferp file)
  10703. (set-buffer file)
  10704. (org-check-agenda-file file)
  10705. (set-buffer (org-get-agenda-file-buffer file)))
  10706. (widen)
  10707. (setq bmp (buffer-modified-p))
  10708. (org-refresh-category-properties)
  10709. (setq org-todo-keywords-for-agenda
  10710. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10711. (setq org-done-keywords-for-agenda
  10712. (append org-done-keywords-for-agenda org-done-keywords))
  10713. (setq org-todo-keyword-alist-for-agenda
  10714. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10715. (setq org-tag-alist-for-agenda
  10716. (append org-tag-alist-for-agenda org-tag-alist))
  10717. (save-excursion
  10718. (remove-text-properties (point-min) (point-max) pall)
  10719. (when org-agenda-skip-archived-trees
  10720. (goto-char (point-min))
  10721. (while (re-search-forward rea nil t)
  10722. (if (org-on-heading-p t)
  10723. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10724. (goto-char (point-min))
  10725. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10726. (while (re-search-forward re nil t)
  10727. (add-text-properties
  10728. (match-beginning 0) (org-end-of-subtree t) pc)))
  10729. (set-buffer-modified-p bmp))))
  10730. (setq org-todo-keyword-alist-for-agenda
  10731. (org-uniquify org-todo-keyword-alist-for-agenda)
  10732. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10733. ;;;; Embedded LaTeX
  10734. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10735. "Keymap for the minor `org-cdlatex-mode'.")
  10736. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10737. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10738. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10739. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10740. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10741. (defvar org-cdlatex-texmathp-advice-is-done nil
  10742. "Flag remembering if we have applied the advice to texmathp already.")
  10743. (define-minor-mode org-cdlatex-mode
  10744. "Toggle the minor `org-cdlatex-mode'.
  10745. This mode supports entering LaTeX environment and math in LaTeX fragments
  10746. in Org-mode.
  10747. \\{org-cdlatex-mode-map}"
  10748. nil " OCDL" nil
  10749. (when org-cdlatex-mode (require 'cdlatex))
  10750. (unless org-cdlatex-texmathp-advice-is-done
  10751. (setq org-cdlatex-texmathp-advice-is-done t)
  10752. (defadvice texmathp (around org-math-always-on activate)
  10753. "Always return t in org-mode buffers.
  10754. This is because we want to insert math symbols without dollars even outside
  10755. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10756. en embedded LaTeX fragement, let texmathp do its job.
  10757. \\[org-cdlatex-mode-map]"
  10758. (interactive)
  10759. (let (p)
  10760. (cond
  10761. ((not (org-mode-p)) ad-do-it)
  10762. ((eq this-command 'cdlatex-math-symbol)
  10763. (setq ad-return-value t
  10764. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10765. (t
  10766. (let ((p (org-inside-LaTeX-fragment-p)))
  10767. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10768. (setq ad-return-value t
  10769. texmathp-why '("Org-mode embedded math" . 0))
  10770. (if p ad-do-it)))))))))
  10771. (defun turn-on-org-cdlatex ()
  10772. "Unconditionally turn on `org-cdlatex-mode'."
  10773. (org-cdlatex-mode 1))
  10774. (defun org-inside-LaTeX-fragment-p ()
  10775. "Test if point is inside a LaTeX fragment.
  10776. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10777. sequence appearing also before point.
  10778. Even though the matchers for math are configurable, this function assumes
  10779. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10780. delimiters are skipped when they have been removed by customization.
  10781. The return value is nil, or a cons cell with the delimiter and
  10782. and the position of this delimiter.
  10783. This function does a reasonably good job, but can locally be fooled by
  10784. for example currency specifications. For example it will assume being in
  10785. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10786. fragments that are properly closed, but during editing, we have to live
  10787. with the uncertainty caused by missing closing delimiters. This function
  10788. looks only before point, not after."
  10789. (catch 'exit
  10790. (let ((pos (point))
  10791. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10792. (lim (progn
  10793. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10794. (point)))
  10795. dd-on str (start 0) m re)
  10796. (goto-char pos)
  10797. (when dodollar
  10798. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10799. re (nth 1 (assoc "$" org-latex-regexps)))
  10800. (while (string-match re str start)
  10801. (cond
  10802. ((= (match-end 0) (length str))
  10803. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10804. ((= (match-end 0) (- (length str) 5))
  10805. (throw 'exit nil))
  10806. (t (setq start (match-end 0))))))
  10807. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10808. (goto-char pos)
  10809. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10810. (and (match-beginning 2) (throw 'exit nil))
  10811. ;; count $$
  10812. (while (re-search-backward "\\$\\$" lim t)
  10813. (setq dd-on (not dd-on)))
  10814. (goto-char pos)
  10815. (if dd-on (cons "$$" m))))))
  10816. (defun org-try-cdlatex-tab ()
  10817. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10818. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10819. - inside a LaTeX fragment, or
  10820. - after the first word in a line, where an abbreviation expansion could
  10821. insert a LaTeX environment."
  10822. (when org-cdlatex-mode
  10823. (cond
  10824. ((save-excursion
  10825. (skip-chars-backward "a-zA-Z0-9*")
  10826. (skip-chars-backward " \t")
  10827. (bolp))
  10828. (cdlatex-tab) t)
  10829. ((org-inside-LaTeX-fragment-p)
  10830. (cdlatex-tab) t)
  10831. (t nil))))
  10832. (defun org-cdlatex-underscore-caret (&optional arg)
  10833. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10834. Revert to the normal definition outside of these fragments."
  10835. (interactive "P")
  10836. (if (org-inside-LaTeX-fragment-p)
  10837. (call-interactively 'cdlatex-sub-superscript)
  10838. (let (org-cdlatex-mode)
  10839. (call-interactively (key-binding (vector last-input-event))))))
  10840. (defun org-cdlatex-math-modify (&optional arg)
  10841. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10842. Revert to the normal definition outside of these fragments."
  10843. (interactive "P")
  10844. (if (org-inside-LaTeX-fragment-p)
  10845. (call-interactively 'cdlatex-math-modify)
  10846. (let (org-cdlatex-mode)
  10847. (call-interactively (key-binding (vector last-input-event))))))
  10848. (defvar org-latex-fragment-image-overlays nil
  10849. "List of overlays carrying the images of latex fragments.")
  10850. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10851. (defun org-remove-latex-fragment-image-overlays ()
  10852. "Remove all overlays with LaTeX fragment images in current buffer."
  10853. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10854. (setq org-latex-fragment-image-overlays nil))
  10855. (defun org-preview-latex-fragment (&optional subtree)
  10856. "Preview the LaTeX fragment at point, or all locally or globally.
  10857. If the cursor is in a LaTeX fragment, create the image and overlay
  10858. it over the source code. If there is no fragment at point, display
  10859. all fragments in the current text, from one headline to the next. With
  10860. prefix SUBTREE, display all fragments in the current subtree. With a
  10861. double prefix `C-u C-u', or when the cursor is before the first headline,
  10862. display all fragments in the buffer.
  10863. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10864. (interactive "P")
  10865. (org-remove-latex-fragment-image-overlays)
  10866. (save-excursion
  10867. (save-restriction
  10868. (let (beg end at msg)
  10869. (cond
  10870. ((or (equal subtree '(16))
  10871. (not (save-excursion
  10872. (re-search-backward (concat "^" outline-regexp) nil t))))
  10873. (setq beg (point-min) end (point-max)
  10874. msg "Creating images for buffer...%s"))
  10875. ((equal subtree '(4))
  10876. (org-back-to-heading)
  10877. (setq beg (point) end (org-end-of-subtree t)
  10878. msg "Creating images for subtree...%s"))
  10879. (t
  10880. (if (setq at (org-inside-LaTeX-fragment-p))
  10881. (goto-char (max (point-min) (- (cdr at) 2)))
  10882. (org-back-to-heading))
  10883. (setq beg (point) end (progn (outline-next-heading) (point))
  10884. msg (if at "Creating image...%s"
  10885. "Creating images for entry...%s"))))
  10886. (message msg "")
  10887. (narrow-to-region beg end)
  10888. (goto-char beg)
  10889. (org-format-latex
  10890. (concat "ltxpng/" (file-name-sans-extension
  10891. (file-name-nondirectory
  10892. buffer-file-name)))
  10893. default-directory 'overlays msg at 'forbuffer)
  10894. (message msg "done. Use `C-c C-c' to remove images.")))))
  10895. (defvar org-latex-regexps
  10896. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10897. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10898. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10899. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10900. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10901. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10902. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10903. "Regular expressions for matching embedded LaTeX.")
  10904. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10905. "Replace LaTeX fragments with links to an image, and produce images."
  10906. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10907. (let* ((prefixnodir (file-name-nondirectory prefix))
  10908. (absprefix (expand-file-name prefix dir))
  10909. (todir (file-name-directory absprefix))
  10910. (opt org-format-latex-options)
  10911. (matchers (plist-get opt :matchers))
  10912. (re-list org-latex-regexps)
  10913. (cnt 0) txt link beg end re e checkdir
  10914. m n block linkfile movefile ov)
  10915. ;; Check if there are old images files with this prefix, and remove them
  10916. (when (file-directory-p todir)
  10917. (mapc 'delete-file
  10918. (directory-files
  10919. todir 'full
  10920. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10921. ;; Check the different regular expressions
  10922. (while (setq e (pop re-list))
  10923. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10924. block (if (nth 3 e) "\n\n" ""))
  10925. (when (member m matchers)
  10926. (goto-char (point-min))
  10927. (while (re-search-forward re nil t)
  10928. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10929. (setq txt (match-string n)
  10930. beg (match-beginning n) end (match-end n)
  10931. cnt (1+ cnt)
  10932. linkfile (format "%s_%04d.png" prefix cnt)
  10933. movefile (format "%s_%04d.png" absprefix cnt)
  10934. link (concat block "[[file:" linkfile "]]" block))
  10935. (if msg (message msg cnt))
  10936. (goto-char beg)
  10937. (unless checkdir ; make sure the directory exists
  10938. (setq checkdir t)
  10939. (or (file-directory-p todir) (make-directory todir)))
  10940. (org-create-formula-image
  10941. txt movefile opt forbuffer)
  10942. (if overlays
  10943. (progn
  10944. (setq ov (org-make-overlay beg end))
  10945. (if (featurep 'xemacs)
  10946. (progn
  10947. (org-overlay-put ov 'invisible t)
  10948. (org-overlay-put
  10949. ov 'end-glyph
  10950. (make-glyph (vector 'png :file movefile))))
  10951. (org-overlay-put
  10952. ov 'display
  10953. (list 'image :type 'png :file movefile :ascent 'center)))
  10954. (push ov org-latex-fragment-image-overlays)
  10955. (goto-char end))
  10956. (delete-region beg end)
  10957. (insert link))))))))
  10958. ;; This function borrows from Ganesh Swami's latex2png.el
  10959. (defun org-create-formula-image (string tofile options buffer)
  10960. (let* ((tmpdir (if (featurep 'xemacs)
  10961. (temp-directory)
  10962. temporary-file-directory))
  10963. (texfilebase (make-temp-name
  10964. (expand-file-name "orgtex" tmpdir)))
  10965. (texfile (concat texfilebase ".tex"))
  10966. (dvifile (concat texfilebase ".dvi"))
  10967. (pngfile (concat texfilebase ".png"))
  10968. (fnh (if (featurep 'xemacs)
  10969. (font-height (get-face-font 'default))
  10970. (face-attribute 'default :height nil)))
  10971. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10972. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10973. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10974. "Black"))
  10975. (bg (or (plist-get options (if buffer :background :html-background))
  10976. "Transparent")))
  10977. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10978. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10979. (with-temp-file texfile
  10980. (insert org-format-latex-header
  10981. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10982. (let ((dir default-directory))
  10983. (condition-case nil
  10984. (progn
  10985. (cd tmpdir)
  10986. (call-process "latex" nil nil nil texfile))
  10987. (error nil))
  10988. (cd dir))
  10989. (if (not (file-exists-p dvifile))
  10990. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10991. (condition-case nil
  10992. (call-process "dvipng" nil nil nil
  10993. "-E" "-fg" fg "-bg" bg
  10994. "-D" dpi
  10995. ;;"-x" scale "-y" scale
  10996. "-T" "tight"
  10997. "-o" pngfile
  10998. dvifile)
  10999. (error nil))
  11000. (if (not (file-exists-p pngfile))
  11001. (progn (message "Failed to create png file from %s" texfile) nil)
  11002. ;; Use the requested file name and clean up
  11003. (copy-file pngfile tofile 'replace)
  11004. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11005. (delete-file (concat texfilebase e)))
  11006. pngfile))))
  11007. (defun org-dvipng-color (attr)
  11008. "Return an rgb color specification for dvipng."
  11009. (apply 'format "rgb %s %s %s"
  11010. (mapcar 'org-normalize-color
  11011. (color-values (face-attribute 'default attr nil)))))
  11012. (defun org-normalize-color (value)
  11013. "Return string to be used as color value for an RGB component."
  11014. (format "%g" (/ value 65535.0)))
  11015. ;;;; Key bindings
  11016. ;; Make `C-c C-x' a prefix key
  11017. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11018. ;; TAB key with modifiers
  11019. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11020. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11021. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11022. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11023. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11024. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11025. ;; The following line is necessary under Suse GNU/Linux
  11026. (unless (featurep 'xemacs)
  11027. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11028. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11029. (define-key org-mode-map [backtab] 'org-shifttab)
  11030. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11031. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11032. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11033. ;; Cursor keys with modifiers
  11034. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11035. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11036. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11037. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11038. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11039. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11040. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11041. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11042. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11043. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11044. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11045. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11046. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11047. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11048. ;;; Extra keys for tty access.
  11049. ;; We only set them when really needed because otherwise the
  11050. ;; menus don't show the simple keys
  11051. (when (or org-use-extra-keys
  11052. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11053. (not window-system))
  11054. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11055. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11056. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11057. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11058. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11059. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11060. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11061. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11062. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11063. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11064. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11065. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11066. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11067. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11068. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11069. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11070. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11071. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11072. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11073. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11074. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11075. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11076. ;; All the other keys
  11077. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11078. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11079. (if (boundp 'narrow-map)
  11080. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11081. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11082. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11083. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11084. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11085. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11086. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11087. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11088. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11089. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11090. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11091. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11092. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11093. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11094. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11095. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11096. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11097. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11098. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11099. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11100. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11101. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11102. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11103. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11104. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11105. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11106. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11107. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11108. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11109. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11110. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11111. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11112. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11113. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11114. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11115. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11116. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11117. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11118. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11119. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11120. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11121. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11122. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11123. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11124. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11125. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11126. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11127. (org-defkey org-mode-map "\C-m" 'org-return)
  11128. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11129. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11130. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11131. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11132. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11133. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11134. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11135. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11136. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11137. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11138. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11139. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11140. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11141. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11142. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11143. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11144. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11145. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11146. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11147. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11148. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11149. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11150. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11151. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11152. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11153. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11154. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11155. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11156. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11157. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11158. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11159. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11160. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11161. (when (featurep 'xemacs)
  11162. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11163. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11164. (defun org-self-insert-command (N)
  11165. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11166. If the cursor is in a table looking at whitespace, the whitespace is
  11167. overwritten, and the table is not marked as requiring realignment."
  11168. (interactive "p")
  11169. (if (and (org-table-p)
  11170. (progn
  11171. ;; check if we blank the field, and if that triggers align
  11172. (and (featurep 'org-table) org-table-auto-blank-field
  11173. (member last-command
  11174. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11175. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11176. ;; got extra space, this field does not determine column width
  11177. (let (org-table-may-need-update) (org-table-blank-field))
  11178. ;; no extra space, this field may determine column width
  11179. (org-table-blank-field)))
  11180. t)
  11181. (eq N 1)
  11182. (looking-at "[^|\n]* |"))
  11183. (let (org-table-may-need-update)
  11184. (goto-char (1- (match-end 0)))
  11185. (delete-backward-char 1)
  11186. (goto-char (match-beginning 0))
  11187. (self-insert-command N))
  11188. (setq org-table-may-need-update t)
  11189. (self-insert-command N)
  11190. (org-fix-tags-on-the-fly)))
  11191. (defun org-fix-tags-on-the-fly ()
  11192. (when (and (equal (char-after (point-at-bol)) ?*)
  11193. (org-on-heading-p))
  11194. (org-align-tags-here org-tags-column)))
  11195. (defun org-delete-backward-char (N)
  11196. "Like `delete-backward-char', insert whitespace at field end in tables.
  11197. When deleting backwards, in tables this function will insert whitespace in
  11198. front of the next \"|\" separator, to keep the table aligned. The table will
  11199. still be marked for re-alignment if the field did fill the entire column,
  11200. because, in this case the deletion might narrow the column."
  11201. (interactive "p")
  11202. (if (and (org-table-p)
  11203. (eq N 1)
  11204. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11205. (looking-at ".*?|"))
  11206. (let ((pos (point))
  11207. (noalign (looking-at "[^|\n\r]* |"))
  11208. (c org-table-may-need-update))
  11209. (backward-delete-char N)
  11210. (skip-chars-forward "^|")
  11211. (insert " ")
  11212. (goto-char (1- pos))
  11213. ;; noalign: if there were two spaces at the end, this field
  11214. ;; does not determine the width of the column.
  11215. (if noalign (setq org-table-may-need-update c)))
  11216. (backward-delete-char N)
  11217. (org-fix-tags-on-the-fly)))
  11218. (defun org-delete-char (N)
  11219. "Like `delete-char', but insert whitespace at field end in tables.
  11220. When deleting characters, in tables this function will insert whitespace in
  11221. front of the next \"|\" separator, to keep the table aligned. The table will
  11222. still be marked for re-alignment if the field did fill the entire column,
  11223. because, in this case the deletion might narrow the column."
  11224. (interactive "p")
  11225. (if (and (org-table-p)
  11226. (not (bolp))
  11227. (not (= (char-after) ?|))
  11228. (eq N 1))
  11229. (if (looking-at ".*?|")
  11230. (let ((pos (point))
  11231. (noalign (looking-at "[^|\n\r]* |"))
  11232. (c org-table-may-need-update))
  11233. (replace-match (concat
  11234. (substring (match-string 0) 1 -1)
  11235. " |"))
  11236. (goto-char pos)
  11237. ;; noalign: if there were two spaces at the end, this field
  11238. ;; does not determine the width of the column.
  11239. (if noalign (setq org-table-may-need-update c)))
  11240. (delete-char N))
  11241. (delete-char N)
  11242. (org-fix-tags-on-the-fly)))
  11243. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11244. (put 'org-self-insert-command 'delete-selection t)
  11245. (put 'orgtbl-self-insert-command 'delete-selection t)
  11246. (put 'org-delete-char 'delete-selection 'supersede)
  11247. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11248. ;; Make `flyspell-mode' delay after some commands
  11249. (put 'org-self-insert-command 'flyspell-delayed t)
  11250. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11251. (put 'org-delete-char 'flyspell-delayed t)
  11252. (put 'org-delete-backward-char 'flyspell-delayed t)
  11253. ;; Make pabbrev-mode expand after org-mode commands
  11254. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11255. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11256. ;; How to do this: Measure non-white length of current string
  11257. ;; If equal to column width, we should realign.
  11258. (defun org-remap (map &rest commands)
  11259. "In MAP, remap the functions given in COMMANDS.
  11260. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11261. (let (new old)
  11262. (while commands
  11263. (setq old (pop commands) new (pop commands))
  11264. (if (fboundp 'command-remapping)
  11265. (org-defkey map (vector 'remap old) new)
  11266. (substitute-key-definition old new map global-map)))))
  11267. (when (eq org-enable-table-editor 'optimized)
  11268. ;; If the user wants maximum table support, we need to hijack
  11269. ;; some standard editing functions
  11270. (org-remap org-mode-map
  11271. 'self-insert-command 'org-self-insert-command
  11272. 'delete-char 'org-delete-char
  11273. 'delete-backward-char 'org-delete-backward-char)
  11274. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11275. (defun org-shiftcursor-error ()
  11276. "Throw an error because Shift-Cursor command was applied in wrong context."
  11277. (error "This command is active in special context like tables, headlines or timestamps"))
  11278. (defun org-shifttab (&optional arg)
  11279. "Global visibility cycling or move to previous table field.
  11280. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11281. on context.
  11282. See the individual commands for more information."
  11283. (interactive "P")
  11284. (cond
  11285. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11286. ((integerp arg)
  11287. (message "Content view to level: %d" arg)
  11288. (org-content (prefix-numeric-value arg))
  11289. (setq org-cycle-global-status 'overview))
  11290. (t (call-interactively 'org-global-cycle))))
  11291. (defun org-shiftmetaleft ()
  11292. "Promote subtree or delete table column.
  11293. Calls `org-promote-subtree', `org-outdent-item',
  11294. or `org-table-delete-column', depending on context.
  11295. See the individual commands for more information."
  11296. (interactive)
  11297. (cond
  11298. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11299. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11300. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11301. (t (org-shiftcursor-error))))
  11302. (defun org-shiftmetaright ()
  11303. "Demote subtree or insert table column.
  11304. Calls `org-demote-subtree', `org-indent-item',
  11305. or `org-table-insert-column', depending on context.
  11306. See the individual commands for more information."
  11307. (interactive)
  11308. (cond
  11309. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11310. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11311. ((org-at-item-p) (call-interactively 'org-indent-item))
  11312. (t (org-shiftcursor-error))))
  11313. (defun org-shiftmetaup (&optional arg)
  11314. "Move subtree up or kill table row.
  11315. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11316. `org-move-item-up' depending on context. See the individual commands
  11317. for more information."
  11318. (interactive "P")
  11319. (cond
  11320. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11321. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11322. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11323. (t (org-shiftcursor-error))))
  11324. (defun org-shiftmetadown (&optional arg)
  11325. "Move subtree down or insert table row.
  11326. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11327. `org-move-item-down', depending on context. See the individual
  11328. commands for more information."
  11329. (interactive "P")
  11330. (cond
  11331. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11332. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11333. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11334. (t (org-shiftcursor-error))))
  11335. (defun org-metaleft (&optional arg)
  11336. "Promote heading or move table column to left.
  11337. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11338. With no specific context, calls the Emacs default `backward-word'.
  11339. See the individual commands for more information."
  11340. (interactive "P")
  11341. (cond
  11342. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11343. ((or (org-on-heading-p) (org-region-active-p))
  11344. (call-interactively 'org-do-promote))
  11345. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11346. (t (call-interactively 'backward-word))))
  11347. (defun org-metaright (&optional arg)
  11348. "Demote subtree or move table column to right.
  11349. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11350. With no specific context, calls the Emacs default `forward-word'.
  11351. See the individual commands for more information."
  11352. (interactive "P")
  11353. (cond
  11354. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11355. ((or (org-on-heading-p) (org-region-active-p))
  11356. (call-interactively 'org-do-demote))
  11357. ((org-at-item-p) (call-interactively 'org-indent-item))
  11358. (t (call-interactively 'forward-word))))
  11359. (defun org-metaup (&optional arg)
  11360. "Move subtree up or move table row up.
  11361. Calls `org-move-subtree-up' or `org-table-move-row' or
  11362. `org-move-item-up', depending on context. See the individual commands
  11363. for more information."
  11364. (interactive "P")
  11365. (cond
  11366. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11367. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11368. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11369. (t (transpose-lines 1) (beginning-of-line -1))))
  11370. (defun org-metadown (&optional arg)
  11371. "Move subtree down or move table row down.
  11372. Calls `org-move-subtree-down' or `org-table-move-row' or
  11373. `org-move-item-down', depending on context. See the individual
  11374. commands for more information."
  11375. (interactive "P")
  11376. (cond
  11377. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11378. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11379. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11380. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11381. (defun org-shiftup (&optional arg)
  11382. "Increase item in timestamp or increase priority of current headline.
  11383. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11384. depending on context. See the individual commands for more information."
  11385. (interactive "P")
  11386. (cond
  11387. ((org-at-timestamp-p t)
  11388. (call-interactively (if org-edit-timestamp-down-means-later
  11389. 'org-timestamp-down 'org-timestamp-up)))
  11390. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11391. ((org-at-item-p) (call-interactively 'org-previous-item))
  11392. ((org-clocktable-try-shift 'up arg))
  11393. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11394. (defun org-shiftdown (&optional arg)
  11395. "Decrease item in timestamp or decrease priority of current headline.
  11396. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11397. depending on context. See the individual commands for more information."
  11398. (interactive "P")
  11399. (cond
  11400. ((org-at-timestamp-p t)
  11401. (call-interactively (if org-edit-timestamp-down-means-later
  11402. 'org-timestamp-up 'org-timestamp-down)))
  11403. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11404. ((org-clocktable-try-shift 'down arg))
  11405. (t (call-interactively 'org-next-item))))
  11406. (defun org-shiftright (&optional arg)
  11407. "Next TODO keyword or timestamp one day later, depending on context."
  11408. (interactive "P")
  11409. (cond
  11410. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11411. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11412. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11413. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11414. ((org-clocktable-try-shift 'right arg))
  11415. (t (org-shiftcursor-error))))
  11416. (defun org-shiftleft (&optional arg)
  11417. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11418. (interactive "P")
  11419. (cond
  11420. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11421. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11422. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11423. ((org-at-property-p)
  11424. (call-interactively 'org-property-previous-allowed-value))
  11425. ((org-clocktable-try-shift 'left arg))
  11426. (t (org-shiftcursor-error))))
  11427. (defun org-shiftcontrolright ()
  11428. "Switch to next TODO set."
  11429. (interactive)
  11430. (cond
  11431. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11432. (t (org-shiftcursor-error))))
  11433. (defun org-shiftcontrolleft ()
  11434. "Switch to previous TODO set."
  11435. (interactive)
  11436. (cond
  11437. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11438. (t (org-shiftcursor-error))))
  11439. (defun org-ctrl-c-ret ()
  11440. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11441. (interactive)
  11442. (cond
  11443. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11444. (t (call-interactively 'org-insert-heading))))
  11445. (defun org-copy-special ()
  11446. "Copy region in table or copy current subtree.
  11447. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11448. See the individual commands for more information."
  11449. (interactive)
  11450. (call-interactively
  11451. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11452. (defun org-cut-special ()
  11453. "Cut region in table or cut current subtree.
  11454. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11455. See the individual commands for more information."
  11456. (interactive)
  11457. (call-interactively
  11458. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11459. (defun org-paste-special (arg)
  11460. "Paste rectangular region into table, or past subtree relative to level.
  11461. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11462. See the individual commands for more information."
  11463. (interactive "P")
  11464. (if (org-at-table-p)
  11465. (org-table-paste-rectangle)
  11466. (org-paste-subtree arg)))
  11467. (defun org-edit-special ()
  11468. "Call a special editor for the stuff at point.
  11469. When at a table, call the formula editor with `org-table-edit-formulas'.
  11470. When at the first line of an src example, call `org-edit-src-code'.
  11471. When in an #+include line, visit the include file. Otherwise call
  11472. `ffap' to visit the file at point."
  11473. (interactive)
  11474. (cond
  11475. ((org-at-table-p)
  11476. (call-interactively 'org-table-edit-formulas))
  11477. ((save-excursion
  11478. (beginning-of-line 1)
  11479. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11480. (find-file (org-trim (match-string 1))))
  11481. ((org-edit-src-code))
  11482. ((org-edit-fixed-width-region))
  11483. (t (call-interactively 'ffap))))
  11484. (defun org-ctrl-c-ctrl-c (&optional arg)
  11485. "Set tags in headline, or update according to changed information at point.
  11486. This command does many different things, depending on context:
  11487. - If the cursor is in a headline, prompt for tags and insert them
  11488. into the current line, aligned to `org-tags-column'. When called
  11489. with prefix arg, realign all tags in the current buffer.
  11490. - If the cursor is in one of the special #+KEYWORD lines, this
  11491. triggers scanning the buffer for these lines and updating the
  11492. information.
  11493. - If the cursor is inside a table, realign the table. This command
  11494. works even if the automatic table editor has been turned off.
  11495. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11496. the entire table.
  11497. - If the cursor is a the beginning of a dynamic block, update it.
  11498. - If the cursor is inside a table created by the table.el package,
  11499. activate that table.
  11500. - If the current buffer is a remember buffer, close note and file it.
  11501. with a prefix argument, file it without further interaction to the default
  11502. location.
  11503. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11504. links in this buffer.
  11505. - If the cursor is on a numbered item in a plain list, renumber the
  11506. ordered list.
  11507. - If the cursor is on a checkbox, toggle it."
  11508. (interactive "P")
  11509. (let ((org-enable-table-editor t))
  11510. (cond
  11511. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11512. org-occur-highlights
  11513. org-latex-fragment-image-overlays)
  11514. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11515. (org-remove-occur-highlights)
  11516. (org-remove-latex-fragment-image-overlays)
  11517. (message "Temporary highlights/overlays removed from current buffer"))
  11518. ((and (local-variable-p 'org-finish-function (current-buffer))
  11519. (fboundp org-finish-function))
  11520. (funcall org-finish-function))
  11521. ((org-at-property-p)
  11522. (call-interactively 'org-property-action))
  11523. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11524. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11525. ((org-at-table.el-p)
  11526. (require 'table)
  11527. (beginning-of-line 1)
  11528. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11529. (call-interactively 'table-recognize-table))
  11530. ((org-at-table-p)
  11531. (org-table-maybe-eval-formula)
  11532. (if arg
  11533. (call-interactively 'org-table-recalculate)
  11534. (org-table-maybe-recalculate-line))
  11535. (call-interactively 'org-table-align))
  11536. ((org-at-item-checkbox-p)
  11537. (call-interactively 'org-toggle-checkbox))
  11538. ((org-at-item-p)
  11539. (call-interactively 'org-maybe-renumber-ordered-list))
  11540. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11541. ;; Dynamic block
  11542. (beginning-of-line 1)
  11543. (save-excursion (org-update-dblock)))
  11544. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11545. (cond
  11546. ((equal (match-string 1) "TBLFM")
  11547. ;; Recalculate the table before this line
  11548. (save-excursion
  11549. (beginning-of-line 1)
  11550. (skip-chars-backward " \r\n\t")
  11551. (if (org-at-table-p)
  11552. (org-call-with-arg 'org-table-recalculate t))))
  11553. (t
  11554. ; (org-set-regexps-and-options)
  11555. ; (org-restart-font-lock)
  11556. (let ((org-inhibit-startup t)) (org-mode-restart))
  11557. (message "Local setup has been refreshed"))))
  11558. (t (error "C-c C-c can do nothing useful at this location.")))))
  11559. (defun org-mode-restart ()
  11560. "Restart Org-mode, to scan again for special lines.
  11561. Also updates the keyword regular expressions."
  11562. (interactive)
  11563. (org-mode)
  11564. (message "Org-mode restarted"))
  11565. (defun org-kill-note-or-show-branches ()
  11566. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11567. (interactive)
  11568. (if (not org-finish-function)
  11569. (call-interactively 'show-branches)
  11570. (let ((org-note-abort t))
  11571. (funcall org-finish-function))))
  11572. (defun org-return (&optional indent)
  11573. "Goto next table row or insert a newline.
  11574. Calls `org-table-next-row' or `newline', depending on context.
  11575. See the individual commands for more information."
  11576. (interactive)
  11577. (cond
  11578. ((bobp) (if indent (newline-and-indent) (newline)))
  11579. ((and (org-at-heading-p)
  11580. (looking-at
  11581. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11582. (org-show-entry)
  11583. (end-of-line 1)
  11584. (newline))
  11585. ((org-at-table-p)
  11586. (org-table-justify-field-maybe)
  11587. (call-interactively 'org-table-next-row))
  11588. (t (if indent (newline-and-indent) (newline)))))
  11589. (defun org-return-indent ()
  11590. "Goto next table row or insert a newline and indent.
  11591. Calls `org-table-next-row' or `newline-and-indent', depending on
  11592. context. See the individual commands for more information."
  11593. (interactive)
  11594. (org-return t))
  11595. (defun org-ctrl-c-star ()
  11596. "Compute table, or change heading status of lines.
  11597. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11598. depending on context. This will also turn a plain list item or a normal
  11599. line into a subheading."
  11600. (interactive)
  11601. (cond
  11602. ((org-at-table-p)
  11603. (call-interactively 'org-table-recalculate))
  11604. ((org-region-active-p)
  11605. ;; Convert all lines in region to list items
  11606. (call-interactively 'org-toggle-region-headings))
  11607. ((org-on-heading-p)
  11608. (org-toggle-region-headings (point-at-bol)
  11609. (min (1+ (point-at-eol)) (point-max))))
  11610. ((org-at-item-p)
  11611. ;; Convert to heading
  11612. (let ((level (save-match-data
  11613. (save-excursion
  11614. (condition-case nil
  11615. (progn
  11616. (org-back-to-heading t)
  11617. (funcall outline-level))
  11618. (error 0))))))
  11619. (replace-match
  11620. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11621. (t (org-toggle-region-headings (point-at-bol)
  11622. (min (1+ (point-at-eol)) (point-max))))))
  11623. (defun org-ctrl-c-minus ()
  11624. "Insert separator line in table or modify bullet status of line.
  11625. Also turns a plain line or a region of lines into list items.
  11626. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11627. `org-cycle-list-bullet', depending on context."
  11628. (interactive)
  11629. (cond
  11630. ((org-at-table-p)
  11631. (call-interactively 'org-table-insert-hline))
  11632. ((org-on-heading-p)
  11633. ;; Convert to item
  11634. (save-excursion
  11635. (beginning-of-line 1)
  11636. (if (looking-at "\\*+ ")
  11637. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11638. ((org-region-active-p)
  11639. ;; Convert all lines in region to list items
  11640. (call-interactively 'org-toggle-region-items))
  11641. ((org-in-item-p)
  11642. (call-interactively 'org-cycle-list-bullet))
  11643. (t (org-toggle-region-items (point-at-bol)
  11644. (min (1+ (point-at-eol)) (point-max))))))
  11645. (defun org-toggle-region-items (beg end)
  11646. "Convert all lines in region to list items.
  11647. If the first line is already an item, convert all list items in the region
  11648. to normal lines."
  11649. (interactive "r")
  11650. (let (l2 l)
  11651. (save-excursion
  11652. (goto-char end)
  11653. (setq l2 (org-current-line))
  11654. (goto-char beg)
  11655. (beginning-of-line 1)
  11656. (setq l (1- (org-current-line)))
  11657. (if (org-at-item-p)
  11658. ;; We already have items, de-itemize
  11659. (while (< (setq l (1+ l)) l2)
  11660. (when (org-at-item-p)
  11661. (goto-char (match-beginning 2))
  11662. (delete-region (match-beginning 2) (match-end 2))
  11663. (and (looking-at "[ \t]+") (replace-match "")))
  11664. (beginning-of-line 2))
  11665. (while (< (setq l (1+ l)) l2)
  11666. (unless (org-at-item-p)
  11667. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11668. (replace-match "\\1- \\2")))
  11669. (beginning-of-line 2))))))
  11670. (defun org-toggle-region-headings (beg end)
  11671. "Convert all lines in region to list items.
  11672. If the first line is already an item, convert all list items in the region
  11673. to normal lines."
  11674. (interactive "r")
  11675. (let (l2 l)
  11676. (save-excursion
  11677. (goto-char end)
  11678. (setq l2 (org-current-line))
  11679. (goto-char beg)
  11680. (beginning-of-line 1)
  11681. (setq l (1- (org-current-line)))
  11682. (if (org-on-heading-p)
  11683. ;; We already have headlines, de-star them
  11684. (while (< (setq l (1+ l)) l2)
  11685. (when (org-on-heading-p t)
  11686. (and (looking-at outline-regexp) (replace-match "")))
  11687. (beginning-of-line 2))
  11688. (let* ((stars (save-excursion
  11689. (re-search-backward org-complex-heading-regexp nil t)
  11690. (or (match-string 1) "*")))
  11691. (add-stars (if org-odd-levels-only "**" "*"))
  11692. (rpl (concat stars add-stars " \\2")))
  11693. (while (< (setq l (1+ l)) l2)
  11694. (unless (org-on-heading-p)
  11695. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11696. (replace-match rpl)))
  11697. (beginning-of-line 2)))))))
  11698. (defun org-meta-return (&optional arg)
  11699. "Insert a new heading or wrap a region in a table.
  11700. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11701. See the individual commands for more information."
  11702. (interactive "P")
  11703. (cond
  11704. ((org-at-table-p)
  11705. (call-interactively 'org-table-wrap-region))
  11706. (t (call-interactively 'org-insert-heading))))
  11707. ;;; Menu entries
  11708. ;; Define the Org-mode menus
  11709. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11710. '("Tbl"
  11711. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11712. ["Next Field" org-cycle (org-at-table-p)]
  11713. ["Previous Field" org-shifttab (org-at-table-p)]
  11714. ["Next Row" org-return (org-at-table-p)]
  11715. "--"
  11716. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11717. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11718. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11719. "--"
  11720. ("Column"
  11721. ["Move Column Left" org-metaleft (org-at-table-p)]
  11722. ["Move Column Right" org-metaright (org-at-table-p)]
  11723. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11724. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11725. ("Row"
  11726. ["Move Row Up" org-metaup (org-at-table-p)]
  11727. ["Move Row Down" org-metadown (org-at-table-p)]
  11728. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11729. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11730. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11731. "--"
  11732. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11733. ("Rectangle"
  11734. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11735. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11736. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11737. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11738. "--"
  11739. ("Calculate"
  11740. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11741. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11742. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11743. "--"
  11744. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11745. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11746. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11747. "--"
  11748. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11749. "--"
  11750. ["Sum Column/Rectangle" org-table-sum
  11751. (or (org-at-table-p) (org-region-active-p))]
  11752. ["Which Column?" org-table-current-column (org-at-table-p)])
  11753. ["Debug Formulas"
  11754. org-table-toggle-formula-debugger
  11755. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11756. ["Show Col/Row Numbers"
  11757. org-table-toggle-coordinate-overlays
  11758. :style toggle
  11759. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11760. "--"
  11761. ["Create" org-table-create (and (not (org-at-table-p))
  11762. org-enable-table-editor)]
  11763. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11764. ["Import from File" org-table-import (not (org-at-table-p))]
  11765. ["Export to File" org-table-export (org-at-table-p)]
  11766. "--"
  11767. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11768. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11769. '("Org"
  11770. ("Show/Hide"
  11771. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11772. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11773. ["Sparse Tree..." org-sparse-tree t]
  11774. ["Reveal Context" org-reveal t]
  11775. ["Show All" show-all t]
  11776. "--"
  11777. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11778. "--"
  11779. ["New Heading" org-insert-heading t]
  11780. ("Navigate Headings"
  11781. ["Up" outline-up-heading t]
  11782. ["Next" outline-next-visible-heading t]
  11783. ["Previous" outline-previous-visible-heading t]
  11784. ["Next Same Level" outline-forward-same-level t]
  11785. ["Previous Same Level" outline-backward-same-level t]
  11786. "--"
  11787. ["Jump" org-goto t])
  11788. ("Edit Structure"
  11789. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11790. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11791. "--"
  11792. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11793. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11794. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11795. "--"
  11796. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11797. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11798. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11799. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11800. "--"
  11801. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11802. "--"
  11803. ["Convert to odd levels" org-convert-to-odd-levels t]
  11804. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11805. ("Editing"
  11806. ["Emphasis..." org-emphasize t]
  11807. ["Edit Source Example" org-edit-special t])
  11808. ("Archive"
  11809. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11810. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11811. ; :active t :keys "C-u C-c C-x C-a"]
  11812. ["Sparse trees open ARCHIVE trees"
  11813. (setq org-sparse-tree-open-archived-trees
  11814. (not org-sparse-tree-open-archived-trees))
  11815. :style toggle :selected org-sparse-tree-open-archived-trees]
  11816. ["Cycling opens ARCHIVE trees"
  11817. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11818. :style toggle :selected org-cycle-open-archived-trees]
  11819. "--"
  11820. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  11821. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11822. ; ["Check and Move Children" (org-archive-subtree '(4))
  11823. ; :active t :keys "C-u C-c C-x C-s"]
  11824. )
  11825. "--"
  11826. ("TODO Lists"
  11827. ["TODO/DONE/-" org-todo t]
  11828. ("Select keyword"
  11829. ["Next keyword" org-shiftright (org-on-heading-p)]
  11830. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11831. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11832. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11833. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11834. ["Show TODO Tree" org-show-todo-tree t]
  11835. ["Global TODO list" org-todo-list t]
  11836. "--"
  11837. ["Set Priority" org-priority t]
  11838. ["Priority Up" org-shiftup t]
  11839. ["Priority Down" org-shiftdown t])
  11840. ("TAGS and Properties"
  11841. ["Set Tags" 'org-set-tags-command t]
  11842. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11843. "--"
  11844. ["Set property" 'org-set-property t]
  11845. ["Column view of properties" org-columns t]
  11846. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11847. ("Dates and Scheduling"
  11848. ["Timestamp" org-time-stamp t]
  11849. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11850. ("Change Date"
  11851. ["1 Day Later" org-shiftright t]
  11852. ["1 Day Earlier" org-shiftleft t]
  11853. ["1 ... Later" org-shiftup t]
  11854. ["1 ... Earlier" org-shiftdown t])
  11855. ["Compute Time Range" org-evaluate-time-range t]
  11856. ["Schedule Item" org-schedule t]
  11857. ["Deadline" org-deadline t]
  11858. "--"
  11859. ["Custom time format" org-toggle-time-stamp-overlays
  11860. :style radio :selected org-display-custom-times]
  11861. "--"
  11862. ["Goto Calendar" org-goto-calendar t]
  11863. ["Date from Calendar" org-date-from-calendar t])
  11864. ("Logging work"
  11865. ["Clock in" org-clock-in t]
  11866. ["Clock out" org-clock-out t]
  11867. ["Clock cancel" org-clock-cancel t]
  11868. ["Goto running clock" org-clock-goto t]
  11869. ["Display times" org-clock-display t]
  11870. ["Create clock table" org-clock-report t]
  11871. "--"
  11872. ["Record DONE time"
  11873. (progn (setq org-log-done (not org-log-done))
  11874. (message "Switching to %s will %s record a timestamp"
  11875. (car org-done-keywords)
  11876. (if org-log-done "automatically" "not")))
  11877. :style toggle :selected org-log-done])
  11878. "--"
  11879. ["Agenda Command..." org-agenda t]
  11880. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11881. ("File List for Agenda")
  11882. ("Special views current file"
  11883. ["TODO Tree" org-show-todo-tree t]
  11884. ["Check Deadlines" org-check-deadlines t]
  11885. ["Timeline" org-timeline t]
  11886. ["Tags Tree" org-tags-sparse-tree t])
  11887. "--"
  11888. ("Hyperlinks"
  11889. ["Store Link (Global)" org-store-link t]
  11890. ["Insert Link" org-insert-link t]
  11891. ["Follow Link" org-open-at-point t]
  11892. "--"
  11893. ["Next link" org-next-link t]
  11894. ["Previous link" org-previous-link t]
  11895. "--"
  11896. ["Descriptive Links"
  11897. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11898. :style radio
  11899. :selected (member '(org-link) buffer-invisibility-spec)]
  11900. ["Literal Links"
  11901. (progn
  11902. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11903. :style radio
  11904. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11905. "--"
  11906. ["Export/Publish..." org-export t]
  11907. ("LaTeX"
  11908. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11909. :selected org-cdlatex-mode]
  11910. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11911. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11912. ["Modify math symbol" org-cdlatex-math-modify
  11913. (org-inside-LaTeX-fragment-p)]
  11914. ["Export LaTeX fragments as images"
  11915. (if (featurep 'org-exp)
  11916. (setq org-export-with-LaTeX-fragments
  11917. (not org-export-with-LaTeX-fragments))
  11918. (require 'org-exp))
  11919. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11920. org-export-with-LaTeX-fragments)])
  11921. "--"
  11922. ("Documentation"
  11923. ["Show Version" org-version t]
  11924. ["Info Documentation" org-info t])
  11925. ("Customize"
  11926. ["Browse Org Group" org-customize t]
  11927. "--"
  11928. ["Expand This Menu" org-create-customize-menu
  11929. (fboundp 'customize-menu-create)])
  11930. "--"
  11931. ["Refresh setup" org-mode-restart t]
  11932. ))
  11933. (defun org-info (&optional node)
  11934. "Read documentation for Org-mode in the info system.
  11935. With optional NODE, go directly to that node."
  11936. (interactive)
  11937. (info (format "(org)%s" (or node ""))))
  11938. (defun org-install-agenda-files-menu ()
  11939. (let ((bl (buffer-list)))
  11940. (save-excursion
  11941. (while bl
  11942. (set-buffer (pop bl))
  11943. (if (org-mode-p) (setq bl nil)))
  11944. (when (org-mode-p)
  11945. (easy-menu-change
  11946. '("Org") "File List for Agenda"
  11947. (append
  11948. (list
  11949. ["Edit File List" (org-edit-agenda-file-list) t]
  11950. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11951. ["Remove Current File from List" org-remove-file t]
  11952. ["Cycle through agenda files" org-cycle-agenda-files t]
  11953. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11954. "--")
  11955. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11956. ;;;; Documentation
  11957. ;;;###autoload
  11958. (defun org-require-autoloaded-modules ()
  11959. (interactive)
  11960. (mapc 'require
  11961. '(org-agenda org-archive org-clock org-colview
  11962. org-exp org-id org-export-latex org-publish
  11963. org-remember org-table)))
  11964. ;;;###autoload
  11965. (defun org-customize ()
  11966. "Call the customize function with org as argument."
  11967. (interactive)
  11968. (org-load-modules-maybe)
  11969. (org-require-autoloaded-modules)
  11970. (customize-browse 'org))
  11971. (defun org-create-customize-menu ()
  11972. "Create a full customization menu for Org-mode, insert it into the menu."
  11973. (interactive)
  11974. (org-load-modules-maybe)
  11975. (org-require-autoloaded-modules)
  11976. (if (fboundp 'customize-menu-create)
  11977. (progn
  11978. (easy-menu-change
  11979. '("Org") "Customize"
  11980. `(["Browse Org group" org-customize t]
  11981. "--"
  11982. ,(customize-menu-create 'org)
  11983. ["Set" Custom-set t]
  11984. ["Save" Custom-save t]
  11985. ["Reset to Current" Custom-reset-current t]
  11986. ["Reset to Saved" Custom-reset-saved t]
  11987. ["Reset to Standard Settings" Custom-reset-standard t]))
  11988. (message "\"Org\"-menu now contains full customization menu"))
  11989. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11990. ;;;; Miscellaneous stuff
  11991. ;;; Generally useful functions
  11992. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11993. "Display the given MESSAGE as a warning."
  11994. (if (fboundp 'display-warning)
  11995. (display-warning 'org message
  11996. (if (featurep 'xemacs)
  11997. 'warning
  11998. :warning))
  11999. (let ((buf (get-buffer-create "*Org warnings*")))
  12000. (with-current-buffer buf
  12001. (goto-char (point-max))
  12002. (insert "Warning (Org): " message)
  12003. (unless (bolp)
  12004. (newline)))
  12005. (display-buffer buf)
  12006. (sit-for 0))))
  12007. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12008. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12009. (if (and marker (marker-buffer marker)
  12010. (buffer-live-p (marker-buffer marker)))
  12011. (progn
  12012. (switch-to-buffer (marker-buffer marker))
  12013. (if (or (> marker (point-max)) (< marker (point-min)))
  12014. (widen))
  12015. (goto-char marker))
  12016. (if bookmark
  12017. (bookmark-jump bookmark)
  12018. (error "Cannot find location"))))
  12019. (defun org-quote-csv-field (s)
  12020. "Quote field for inclusion in CSV material."
  12021. (if (string-match "[\",]" s)
  12022. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12023. s))
  12024. (defun org-plist-delete (plist property)
  12025. "Delete PROPERTY from PLIST.
  12026. This is in contrast to merely setting it to 0."
  12027. (let (p)
  12028. (while plist
  12029. (if (not (eq property (car plist)))
  12030. (setq p (plist-put p (car plist) (nth 1 plist))))
  12031. (setq plist (cddr plist)))
  12032. p))
  12033. (defun org-force-self-insert (N)
  12034. "Needed to enforce self-insert under remapping."
  12035. (interactive "p")
  12036. (self-insert-command N))
  12037. (defun org-string-width (s)
  12038. "Compute width of string, ignoring invisible characters.
  12039. This ignores character with invisibility property `org-link', and also
  12040. characters with property `org-cwidth', because these will become invisible
  12041. upon the next fontification round."
  12042. (let (b l)
  12043. (when (or (eq t buffer-invisibility-spec)
  12044. (assq 'org-link buffer-invisibility-spec))
  12045. (while (setq b (text-property-any 0 (length s)
  12046. 'invisible 'org-link s))
  12047. (setq s (concat (substring s 0 b)
  12048. (substring s (or (next-single-property-change
  12049. b 'invisible s) (length s)))))))
  12050. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12051. (setq s (concat (substring s 0 b)
  12052. (substring s (or (next-single-property-change
  12053. b 'org-cwidth s) (length s))))))
  12054. (setq l (string-width s) b -1)
  12055. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12056. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12057. l))
  12058. (defun org-get-indentation (&optional line)
  12059. "Get the indentation of the current line, interpreting tabs.
  12060. When LINE is given, assume it represents a line and compute its indentation."
  12061. (if line
  12062. (if (string-match "^ *" (org-remove-tabs line))
  12063. (match-end 0))
  12064. (save-excursion
  12065. (beginning-of-line 1)
  12066. (skip-chars-forward " \t")
  12067. (current-column))))
  12068. (defun org-remove-tabs (s &optional width)
  12069. "Replace tabulators in S with spaces.
  12070. Assumes that s is a single line, starting in column 0."
  12071. (setq width (or width tab-width))
  12072. (while (string-match "\t" s)
  12073. (setq s (replace-match
  12074. (make-string
  12075. (- (* width (/ (+ (match-beginning 0) width) width))
  12076. (match-beginning 0)) ?\ )
  12077. t t s)))
  12078. s)
  12079. (defun org-fix-indentation (line ind)
  12080. "Fix indentation in LINE.
  12081. IND is a cons cell with target and minimum indentation.
  12082. If the current indenation in LINE is smaller than the minimum,
  12083. leave it alone. If it is larger than ind, set it to the target."
  12084. (let* ((l (org-remove-tabs line))
  12085. (i (org-get-indentation l))
  12086. (i1 (car ind)) (i2 (cdr ind)))
  12087. (if (>= i i2) (setq l (substring line i2)))
  12088. (if (> i1 0)
  12089. (concat (make-string i1 ?\ ) l)
  12090. l)))
  12091. (defun org-base-buffer (buffer)
  12092. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12093. (if (not buffer)
  12094. buffer
  12095. (or (buffer-base-buffer buffer)
  12096. buffer)))
  12097. (defun org-trim (s)
  12098. "Remove whitespace at beginning and end of string."
  12099. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12100. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12101. s)
  12102. (defun org-wrap (string &optional width lines)
  12103. "Wrap string to either a number of lines, or a width in characters.
  12104. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12105. that costs. If there is a word longer than WIDTH, the text is actually
  12106. wrapped to the length of that word.
  12107. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12108. many lines, whatever width that takes.
  12109. The return value is a list of lines, without newlines at the end."
  12110. (let* ((words (org-split-string string "[ \t\n]+"))
  12111. (maxword (apply 'max (mapcar 'org-string-width words)))
  12112. w ll)
  12113. (cond (width
  12114. (org-do-wrap words (max maxword width)))
  12115. (lines
  12116. (setq w maxword)
  12117. (setq ll (org-do-wrap words maxword))
  12118. (if (<= (length ll) lines)
  12119. ll
  12120. (setq ll words)
  12121. (while (> (length ll) lines)
  12122. (setq w (1+ w))
  12123. (setq ll (org-do-wrap words w)))
  12124. ll))
  12125. (t (error "Cannot wrap this")))))
  12126. (defun org-do-wrap (words width)
  12127. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12128. (let (lines line)
  12129. (while words
  12130. (setq line (pop words))
  12131. (while (and words (< (+ (length line) (length (car words))) width))
  12132. (setq line (concat line " " (pop words))))
  12133. (setq lines (push line lines)))
  12134. (nreverse lines)))
  12135. (defun org-split-string (string &optional separators)
  12136. "Splits STRING into substrings at SEPARATORS.
  12137. No empty strings are returned if there are matches at the beginning
  12138. and end of string."
  12139. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12140. (start 0)
  12141. notfirst
  12142. (list nil))
  12143. (while (and (string-match rexp string
  12144. (if (and notfirst
  12145. (= start (match-beginning 0))
  12146. (< start (length string)))
  12147. (1+ start) start))
  12148. (< (match-beginning 0) (length string)))
  12149. (setq notfirst t)
  12150. (or (eq (match-beginning 0) 0)
  12151. (and (eq (match-beginning 0) (match-end 0))
  12152. (eq (match-beginning 0) start))
  12153. (setq list
  12154. (cons (substring string start (match-beginning 0))
  12155. list)))
  12156. (setq start (match-end 0)))
  12157. (or (eq start (length string))
  12158. (setq list
  12159. (cons (substring string start)
  12160. list)))
  12161. (nreverse list)))
  12162. (defun org-context ()
  12163. "Return a list of contexts of the current cursor position.
  12164. If several contexts apply, all are returned.
  12165. Each context entry is a list with a symbol naming the context, and
  12166. two positions indicating start and end of the context. Possible
  12167. contexts are:
  12168. :headline anywhere in a headline
  12169. :headline-stars on the leading stars in a headline
  12170. :todo-keyword on a TODO keyword (including DONE) in a headline
  12171. :tags on the TAGS in a headline
  12172. :priority on the priority cookie in a headline
  12173. :item on the first line of a plain list item
  12174. :item-bullet on the bullet/number of a plain list item
  12175. :checkbox on the checkbox in a plain list item
  12176. :table in an org-mode table
  12177. :table-special on a special filed in a table
  12178. :table-table in a table.el table
  12179. :link on a hyperlink
  12180. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12181. :target on a <<target>>
  12182. :radio-target on a <<<radio-target>>>
  12183. :latex-fragment on a LaTeX fragment
  12184. :latex-preview on a LaTeX fragment with overlayed preview image
  12185. This function expects the position to be visible because it uses font-lock
  12186. faces as a help to recognize the following contexts: :table-special, :link,
  12187. and :keyword."
  12188. (let* ((f (get-text-property (point) 'face))
  12189. (faces (if (listp f) f (list f)))
  12190. (p (point)) clist o)
  12191. ;; First the large context
  12192. (cond
  12193. ((org-on-heading-p t)
  12194. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12195. (when (progn
  12196. (beginning-of-line 1)
  12197. (looking-at org-todo-line-tags-regexp))
  12198. (push (org-point-in-group p 1 :headline-stars) clist)
  12199. (push (org-point-in-group p 2 :todo-keyword) clist)
  12200. (push (org-point-in-group p 4 :tags) clist))
  12201. (goto-char p)
  12202. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12203. (if (looking-at "\\[#[A-Z0-9]\\]")
  12204. (push (org-point-in-group p 0 :priority) clist)))
  12205. ((org-at-item-p)
  12206. (push (org-point-in-group p 2 :item-bullet) clist)
  12207. (push (list :item (point-at-bol)
  12208. (save-excursion (org-end-of-item) (point)))
  12209. clist)
  12210. (and (org-at-item-checkbox-p)
  12211. (push (org-point-in-group p 0 :checkbox) clist)))
  12212. ((org-at-table-p)
  12213. (push (list :table (org-table-begin) (org-table-end)) clist)
  12214. (if (memq 'org-formula faces)
  12215. (push (list :table-special
  12216. (previous-single-property-change p 'face)
  12217. (next-single-property-change p 'face)) clist)))
  12218. ((org-at-table-p 'any)
  12219. (push (list :table-table) clist)))
  12220. (goto-char p)
  12221. ;; Now the small context
  12222. (cond
  12223. ((org-at-timestamp-p)
  12224. (push (org-point-in-group p 0 :timestamp) clist))
  12225. ((memq 'org-link faces)
  12226. (push (list :link
  12227. (previous-single-property-change p 'face)
  12228. (next-single-property-change p 'face)) clist))
  12229. ((memq 'org-special-keyword faces)
  12230. (push (list :keyword
  12231. (previous-single-property-change p 'face)
  12232. (next-single-property-change p 'face)) clist))
  12233. ((org-on-target-p)
  12234. (push (org-point-in-group p 0 :target) clist)
  12235. (goto-char (1- (match-beginning 0)))
  12236. (if (looking-at org-radio-target-regexp)
  12237. (push (org-point-in-group p 0 :radio-target) clist))
  12238. (goto-char p))
  12239. ((setq o (car (delq nil
  12240. (mapcar
  12241. (lambda (x)
  12242. (if (memq x org-latex-fragment-image-overlays) x))
  12243. (org-overlays-at (point))))))
  12244. (push (list :latex-fragment
  12245. (org-overlay-start o) (org-overlay-end o)) clist)
  12246. (push (list :latex-preview
  12247. (org-overlay-start o) (org-overlay-end o)) clist))
  12248. ((org-inside-LaTeX-fragment-p)
  12249. ;; FIXME: positions wrong.
  12250. (push (list :latex-fragment (point) (point)) clist)))
  12251. (setq clist (nreverse (delq nil clist)))
  12252. clist))
  12253. ;; FIXME: Compare with at-regexp-p Do we need both?
  12254. (defun org-in-regexp (re &optional nlines visually)
  12255. "Check if point is inside a match of regexp.
  12256. Normally only the current line is checked, but you can include NLINES extra
  12257. lines both before and after point into the search.
  12258. If VISUALLY is set, require that the cursor is not after the match but
  12259. really on, so that the block visually is on the match."
  12260. (catch 'exit
  12261. (let ((pos (point))
  12262. (eol (point-at-eol (+ 1 (or nlines 0))))
  12263. (inc (if visually 1 0)))
  12264. (save-excursion
  12265. (beginning-of-line (- 1 (or nlines 0)))
  12266. (while (re-search-forward re eol t)
  12267. (if (and (<= (match-beginning 0) pos)
  12268. (>= (+ inc (match-end 0)) pos))
  12269. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12270. (defun org-at-regexp-p (regexp)
  12271. "Is point inside a match of REGEXP in the current line?"
  12272. (catch 'exit
  12273. (save-excursion
  12274. (let ((pos (point)) (end (point-at-eol)))
  12275. (beginning-of-line 1)
  12276. (while (re-search-forward regexp end t)
  12277. (if (and (<= (match-beginning 0) pos)
  12278. (>= (match-end 0) pos))
  12279. (throw 'exit t)))
  12280. nil))))
  12281. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12282. "Call `multi-occur' with buffers for all agenda files."
  12283. (interactive "sOrg-files matching: \np")
  12284. (let* ((files (org-agenda-files))
  12285. (tnames (mapcar 'file-truename files))
  12286. (extra org-agenda-text-search-extra-files)
  12287. f)
  12288. (when (eq (car extra) 'agenda-archives)
  12289. (setq extra (cdr extra))
  12290. (setq files (org-add-archive-files files)))
  12291. (while (setq f (pop extra))
  12292. (unless (member (file-truename f) tnames)
  12293. (add-to-list 'files f 'append)
  12294. (add-to-list 'tnames (file-truename f) 'append)))
  12295. (multi-occur
  12296. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12297. regexp)))
  12298. (if (boundp 'occur-mode-find-occurrence-hook)
  12299. ;; Emacs 23
  12300. (add-hook 'occur-mode-find-occurrence-hook
  12301. (lambda ()
  12302. (when (org-mode-p)
  12303. (org-reveal))))
  12304. ;; Emacs 22
  12305. (defadvice occur-mode-goto-occurrence
  12306. (after org-occur-reveal activate)
  12307. (and (org-mode-p) (org-reveal)))
  12308. (defadvice occur-mode-goto-occurrence-other-window
  12309. (after org-occur-reveal activate)
  12310. (and (org-mode-p) (org-reveal)))
  12311. (defadvice occur-mode-display-occurrence
  12312. (after org-occur-reveal activate)
  12313. (when (org-mode-p)
  12314. (let ((pos (occur-mode-find-occurrence)))
  12315. (with-current-buffer (marker-buffer pos)
  12316. (save-excursion
  12317. (goto-char pos)
  12318. (org-reveal)))))))
  12319. (defun org-uniquify (list)
  12320. "Remove duplicate elements from LIST."
  12321. (let (res)
  12322. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12323. res))
  12324. (defun org-delete-all (elts list)
  12325. "Remove all elements in ELTS from LIST."
  12326. (while elts
  12327. (setq list (delete (pop elts) list)))
  12328. list)
  12329. (defun org-back-over-empty-lines ()
  12330. "Move backwards over witespace, to the beginning of the first empty line.
  12331. Returns the number of empty lines passed."
  12332. (let ((pos (point)))
  12333. (skip-chars-backward " \t\n\r")
  12334. (beginning-of-line 2)
  12335. (goto-char (min (point) pos))
  12336. (count-lines (point) pos)))
  12337. (defun org-skip-whitespace ()
  12338. (skip-chars-forward " \t\n\r"))
  12339. (defun org-point-in-group (point group &optional context)
  12340. "Check if POINT is in match-group GROUP.
  12341. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12342. match. If the match group does ot exist or point is not inside it,
  12343. return nil."
  12344. (and (match-beginning group)
  12345. (>= point (match-beginning group))
  12346. (<= point (match-end group))
  12347. (if context
  12348. (list context (match-beginning group) (match-end group))
  12349. t)))
  12350. (defun org-switch-to-buffer-other-window (&rest args)
  12351. "Switch to buffer in a second window on the current frame.
  12352. In particular, do not allow pop-up frames."
  12353. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12354. special-display-function)
  12355. (apply 'switch-to-buffer-other-window args)))
  12356. (defun org-combine-plists (&rest plists)
  12357. "Create a single property list from all plists in PLISTS.
  12358. The process starts by copying the first list, and then setting properties
  12359. from the other lists. Settings in the last list are the most significant
  12360. ones and overrule settings in the other lists."
  12361. (let ((rtn (copy-sequence (pop plists)))
  12362. p v ls)
  12363. (while plists
  12364. (setq ls (pop plists))
  12365. (while ls
  12366. (setq p (pop ls) v (pop ls))
  12367. (setq rtn (plist-put rtn p v))))
  12368. rtn))
  12369. (defun org-move-line-down (arg)
  12370. "Move the current line down. With prefix argument, move it past ARG lines."
  12371. (interactive "p")
  12372. (let ((col (current-column))
  12373. beg end pos)
  12374. (beginning-of-line 1) (setq beg (point))
  12375. (beginning-of-line 2) (setq end (point))
  12376. (beginning-of-line (+ 1 arg))
  12377. (setq pos (move-marker (make-marker) (point)))
  12378. (insert (delete-and-extract-region beg end))
  12379. (goto-char pos)
  12380. (org-move-to-column col)))
  12381. (defun org-move-line-up (arg)
  12382. "Move the current line up. With prefix argument, move it past ARG lines."
  12383. (interactive "p")
  12384. (let ((col (current-column))
  12385. beg end pos)
  12386. (beginning-of-line 1) (setq beg (point))
  12387. (beginning-of-line 2) (setq end (point))
  12388. (beginning-of-line (- arg))
  12389. (setq pos (move-marker (make-marker) (point)))
  12390. (insert (delete-and-extract-region beg end))
  12391. (goto-char pos)
  12392. (org-move-to-column col)))
  12393. (defun org-replace-escapes (string table)
  12394. "Replace %-escapes in STRING with values in TABLE.
  12395. TABLE is an association list with keys like \"%a\" and string values.
  12396. The sequences in STRING may contain normal field width and padding information,
  12397. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12398. so values can contain further %-escapes if they are define later in TABLE."
  12399. (let ((case-fold-search nil)
  12400. e re rpl)
  12401. (while (setq e (pop table))
  12402. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12403. (while (string-match re string)
  12404. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12405. (cdr e)))
  12406. (setq string (replace-match rpl t t string))))
  12407. string))
  12408. (defun org-sublist (list start end)
  12409. "Return a section of LIST, from START to END.
  12410. Counting starts at 1."
  12411. (let (rtn (c start))
  12412. (setq list (nthcdr (1- start) list))
  12413. (while (and list (<= c end))
  12414. (push (pop list) rtn)
  12415. (setq c (1+ c)))
  12416. (nreverse rtn)))
  12417. (defun org-find-base-buffer-visiting (file)
  12418. "Like `find-buffer-visiting' but alway return the base buffer and
  12419. not an indirect buffer."
  12420. (let ((buf (find-buffer-visiting file)))
  12421. (if buf
  12422. (or (buffer-base-buffer buf) buf)
  12423. nil)))
  12424. (defun org-image-file-name-regexp ()
  12425. "Return regexp matching the file names of images."
  12426. (if (fboundp 'image-file-name-regexp)
  12427. (image-file-name-regexp)
  12428. (let ((image-file-name-extensions
  12429. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12430. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12431. (concat "\\."
  12432. (regexp-opt (nconc (mapcar 'upcase
  12433. image-file-name-extensions)
  12434. image-file-name-extensions)
  12435. t)
  12436. "\\'"))))
  12437. (defun org-file-image-p (file)
  12438. "Return non-nil if FILE is an image."
  12439. (save-match-data
  12440. (string-match (org-image-file-name-regexp) file)))
  12441. (defun org-get-cursor-date ()
  12442. "Return the date at cursor in as a time.
  12443. This works in the calendar and in the agenda, anywhere else it just
  12444. returns the current time."
  12445. (let (date day defd)
  12446. (cond
  12447. ((eq major-mode 'calendar-mode)
  12448. (setq date (calendar-cursor-to-date)
  12449. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12450. ((eq major-mode 'org-agenda-mode)
  12451. (setq day (get-text-property (point) 'day))
  12452. (if day
  12453. (setq date (calendar-gregorian-from-absolute day)
  12454. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12455. (nth 2 date))))))
  12456. (or defd (current-time))))
  12457. (defvar org-agenda-action-marker (make-marker)
  12458. "Marker pointing to the entry for the next agenda action.")
  12459. (defun org-mark-entry-for-agenda-action ()
  12460. "Mark the current entry as target of an agenda action.
  12461. Agenda actions are actions executed from the agenda with the key `k',
  12462. which make use of the date at the cursor."
  12463. (interactive)
  12464. (move-marker org-agenda-action-marker
  12465. (save-excursion (org-back-to-heading t) (point))
  12466. (current-buffer))
  12467. (message
  12468. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12469. ;;; Paragraph filling stuff.
  12470. ;; We want this to be just right, so use the full arsenal.
  12471. (defun org-indent-line-function ()
  12472. "Indent line like previous, but further if previous was headline or item."
  12473. (interactive)
  12474. (let* ((pos (point))
  12475. (itemp (org-at-item-p))
  12476. column bpos bcol tpos tcol bullet btype bullet-type)
  12477. ;; Find the previous relevant line
  12478. (beginning-of-line 1)
  12479. (cond
  12480. ((looking-at "#") (setq column 0))
  12481. ((looking-at "\\*+ ") (setq column 0))
  12482. (t
  12483. (beginning-of-line 0)
  12484. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12485. (beginning-of-line 0))
  12486. (cond
  12487. ((looking-at "\\*+[ \t]+")
  12488. (if (not org-adapt-indentation)
  12489. (setq column 0)
  12490. (goto-char (match-end 0))
  12491. (setq column (current-column))))
  12492. ((org-in-item-p)
  12493. (org-beginning-of-item)
  12494. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12495. (setq bpos (match-beginning 1) tpos (match-end 0)
  12496. bcol (progn (goto-char bpos) (current-column))
  12497. tcol (progn (goto-char tpos) (current-column))
  12498. bullet (match-string 1)
  12499. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12500. (if (> tcol (+ bcol org-description-max-indent))
  12501. (setq tcol (+ bcol 5)))
  12502. (if (not itemp)
  12503. (setq column tcol)
  12504. (goto-char pos)
  12505. (beginning-of-line 1)
  12506. (if (looking-at "\\S-")
  12507. (progn
  12508. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12509. (setq bullet (match-string 1)
  12510. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12511. (setq column (if (equal btype bullet-type) bcol tcol)))
  12512. (setq column (org-get-indentation)))))
  12513. (t (setq column (org-get-indentation))))))
  12514. (goto-char pos)
  12515. (if (<= (current-column) (current-indentation))
  12516. (org-indent-line-to column)
  12517. (save-excursion (org-indent-line-to column)))
  12518. (setq column (current-column))
  12519. (beginning-of-line 1)
  12520. (if (looking-at
  12521. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12522. (replace-match (concat "\\1" (format org-property-format
  12523. (match-string 2) (match-string 3)))
  12524. t nil))
  12525. (org-move-to-column column)))
  12526. (defun org-set-autofill-regexps ()
  12527. (interactive)
  12528. ;; In the paragraph separator we include headlines, because filling
  12529. ;; text in a line directly attached to a headline would otherwise
  12530. ;; fill the headline as well.
  12531. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12532. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12533. ;; The paragraph starter includes hand-formatted lists.
  12534. (org-set-local 'paragraph-start
  12535. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12536. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12537. ;; But only if the user has not turned off tables or fixed-width regions
  12538. (org-set-local
  12539. 'auto-fill-inhibit-regexp
  12540. (concat "\\*+ \\|#\\+"
  12541. "\\|[ \t]*" org-keyword-time-regexp
  12542. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12543. (concat
  12544. "\\|[ \t]*["
  12545. (if org-enable-table-editor "|" "")
  12546. (if org-enable-fixed-width-editor ":" "")
  12547. "]"))))
  12548. ;; We use our own fill-paragraph function, to make sure that tables
  12549. ;; and fixed-width regions are not wrapped. That function will pass
  12550. ;; through to `fill-paragraph' when appropriate.
  12551. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12552. ; Adaptive filling: To get full control, first make sure that
  12553. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12554. (org-set-local 'adaptive-fill-regexp "\000")
  12555. (org-set-local 'adaptive-fill-function
  12556. 'org-adaptive-fill-function)
  12557. (org-set-local
  12558. 'align-mode-rules-list
  12559. '((org-in-buffer-settings
  12560. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12561. (modes . '(org-mode))))))
  12562. (defun org-fill-paragraph (&optional justify)
  12563. "Re-align a table, pass through to fill-paragraph if no table."
  12564. (let ((table-p (org-at-table-p))
  12565. (table.el-p (org-at-table.el-p)))
  12566. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12567. (save-excursion (goto-char (point-at-bol))
  12568. (looking-at outline-regexp)))
  12569. t) ; skip headlines
  12570. (table.el-p t) ; skip table.el tables
  12571. (table-p (org-table-align) t) ; align org-mode tables
  12572. (t nil)))) ; call paragraph-fill
  12573. ;; For reference, this is the default value of adaptive-fill-regexp
  12574. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12575. (defun org-adaptive-fill-function ()
  12576. "Return a fill prefix for org-mode files.
  12577. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12578. work correctly."
  12579. (cond ((looking-at "#[ \t]+")
  12580. (match-string 0))
  12581. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12582. (save-excursion
  12583. (if (> (match-end 1) (+ (match-beginning 1)
  12584. org-description-max-indent))
  12585. (goto-char (+ (match-beginning 1) 5))
  12586. (goto-char (match-end 0)))
  12587. (make-string (current-column) ?\ )))
  12588. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12589. (save-excursion
  12590. (goto-char (match-end 0))
  12591. (make-string (current-column) ?\ )))
  12592. (t nil)))
  12593. ;;; Other stuff.
  12594. (defun org-toggle-fixed-width-section (arg)
  12595. "Toggle the fixed-width export.
  12596. If there is no active region, the QUOTE keyword at the current headline is
  12597. inserted or removed. When present, it causes the text between this headline
  12598. and the next to be exported as fixed-width text, and unmodified.
  12599. If there is an active region, this command adds or removes a colon as the
  12600. first character of this line. If the first character of a line is a colon,
  12601. this line is also exported in fixed-width font."
  12602. (interactive "P")
  12603. (let* ((cc 0)
  12604. (regionp (org-region-active-p))
  12605. (beg (if regionp (region-beginning) (point)))
  12606. (end (if regionp (region-end)))
  12607. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12608. (case-fold-search nil)
  12609. (re "[ \t]*\\(:\\)")
  12610. off)
  12611. (if regionp
  12612. (save-excursion
  12613. (goto-char beg)
  12614. (setq cc (current-column))
  12615. (beginning-of-line 1)
  12616. (setq off (looking-at re))
  12617. (while (> nlines 0)
  12618. (setq nlines (1- nlines))
  12619. (beginning-of-line 1)
  12620. (cond
  12621. (arg
  12622. (org-move-to-column cc t)
  12623. (insert ":\n")
  12624. (forward-line -1))
  12625. ((and off (looking-at re))
  12626. (replace-match "" t t nil 1))
  12627. ((not off) (org-move-to-column cc t) (insert ":")))
  12628. (forward-line 1)))
  12629. (save-excursion
  12630. (org-back-to-heading)
  12631. (if (looking-at (concat outline-regexp
  12632. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12633. (replace-match "" t t nil 1)
  12634. (if (looking-at outline-regexp)
  12635. (progn
  12636. (goto-char (match-end 0))
  12637. (insert org-quote-string " "))))))))
  12638. ;;;; Functions extending outline functionality
  12639. (defun org-beginning-of-line (&optional arg)
  12640. "Go to the beginning of the current line. If that is invisible, continue
  12641. to a visible line beginning. This makes the function of C-a more intuitive.
  12642. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12643. first attempt, and only move to after the tags when the cursor is already
  12644. beyond the end of the headline."
  12645. (interactive "P")
  12646. (let ((pos (point)) refpos)
  12647. (beginning-of-line 1)
  12648. (if (bobp)
  12649. nil
  12650. (backward-char 1)
  12651. (if (org-invisible-p)
  12652. (while (and (not (bobp)) (org-invisible-p))
  12653. (backward-char 1)
  12654. (beginning-of-line 1))
  12655. (forward-char 1)))
  12656. (when org-special-ctrl-a/e
  12657. (cond
  12658. ((and (looking-at org-complex-heading-regexp)
  12659. (= (char-after (match-end 1)) ?\ ))
  12660. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12661. (point-at-eol)))
  12662. (goto-char
  12663. (if (eq org-special-ctrl-a/e t)
  12664. (cond ((> pos refpos) refpos)
  12665. ((= pos (point)) refpos)
  12666. (t (point)))
  12667. (cond ((> pos (point)) (point))
  12668. ((not (eq last-command this-command)) (point))
  12669. (t refpos)))))
  12670. ((org-at-item-p)
  12671. (goto-char
  12672. (if (eq org-special-ctrl-a/e t)
  12673. (cond ((> pos (match-end 4)) (match-end 4))
  12674. ((= pos (point)) (match-end 4))
  12675. (t (point)))
  12676. (cond ((> pos (point)) (point))
  12677. ((not (eq last-command this-command)) (point))
  12678. (t (match-end 4))))))))
  12679. (org-no-warnings
  12680. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12681. (defun org-end-of-line (&optional arg)
  12682. "Go to the end of the line.
  12683. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12684. first attempt, and only move to after the tags when the cursor is already
  12685. beyond the end of the headline."
  12686. (interactive "P")
  12687. (if (or (not org-special-ctrl-a/e)
  12688. (not (org-on-heading-p)))
  12689. (end-of-line arg)
  12690. (let ((pos (point)))
  12691. (beginning-of-line 1)
  12692. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12693. (if (eq org-special-ctrl-a/e t)
  12694. (if (or (< pos (match-beginning 1))
  12695. (= pos (match-end 0)))
  12696. (goto-char (match-beginning 1))
  12697. (goto-char (match-end 0)))
  12698. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12699. (goto-char (match-end 0))
  12700. (goto-char (match-beginning 1))))
  12701. (end-of-line arg))))
  12702. (org-no-warnings
  12703. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12704. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12705. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12706. (defun org-kill-line (&optional arg)
  12707. "Kill line, to tags or end of line."
  12708. (interactive "P")
  12709. (cond
  12710. ((or (not org-special-ctrl-k)
  12711. (bolp)
  12712. (not (org-on-heading-p)))
  12713. (call-interactively 'kill-line))
  12714. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12715. (kill-region (point) (match-beginning 1))
  12716. (org-set-tags nil t))
  12717. (t (kill-region (point) (point-at-eol)))))
  12718. (define-key org-mode-map "\C-k" 'org-kill-line)
  12719. (defun org-yank ()
  12720. "Yank, and if the yanked text is a single subtree, fold it.
  12721. In fact, if the yanked text is a sequence of subtrees, fold all of them."
  12722. (interactive)
  12723. (if org-yank-folded-subtrees
  12724. (let ((beg (point)) end)
  12725. (call-interactively 'yank)
  12726. (setq end (point))
  12727. (goto-char beg)
  12728. (when (and (bolp)
  12729. (org-kill-is-subtree-p))
  12730. (or (looking-at outline-regexp)
  12731. (re-search-forward (concat "^" outline-regexp) end t))
  12732. (while (and (< (point) end) (looking-at outline-regexp))
  12733. (hide-subtree)
  12734. (org-cycle-show-empty-lines 'folded)
  12735. (condition-case nil
  12736. (outline-forward-same-level 1)
  12737. (error (goto-char end)))))
  12738. (goto-char end)
  12739. (skip-chars-forward " \t\n\r"))
  12740. (call-interactively 'yank)))
  12741. (define-key org-mode-map "\C-y" 'org-yank)
  12742. (defun org-invisible-p ()
  12743. "Check if point is at a character currently not visible."
  12744. ;; Early versions of noutline don't have `outline-invisible-p'.
  12745. (if (fboundp 'outline-invisible-p)
  12746. (outline-invisible-p)
  12747. (get-char-property (point) 'invisible)))
  12748. (defun org-invisible-p2 ()
  12749. "Check if point is at a character currently not visible."
  12750. (save-excursion
  12751. (if (and (eolp) (not (bobp))) (backward-char 1))
  12752. ;; Early versions of noutline don't have `outline-invisible-p'.
  12753. (if (fboundp 'outline-invisible-p)
  12754. (outline-invisible-p)
  12755. (get-char-property (point) 'invisible))))
  12756. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12757. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12758. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12759. (defun org-at-heading-or-item-p ()
  12760. (or (org-on-heading-p) (org-at-item-p)))
  12761. (defun org-on-target-p ()
  12762. (or (org-in-regexp org-radio-target-regexp)
  12763. (org-in-regexp org-target-regexp)))
  12764. (defun org-up-heading-all (arg)
  12765. "Move to the heading line of which the present line is a subheading.
  12766. This function considers both visible and invisible heading lines.
  12767. With argument, move up ARG levels."
  12768. (if (fboundp 'outline-up-heading-all)
  12769. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12770. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12771. (defun org-up-heading-safe ()
  12772. "Move to the heading line of which the present line is a subheading.
  12773. This version will not throw an error. It will return the level of the
  12774. headline found, or nil if no higher level is found."
  12775. (let ((pos (point)) start-level level
  12776. (re (concat "^" outline-regexp)))
  12777. (catch 'exit
  12778. (outline-back-to-heading t)
  12779. (setq start-level (funcall outline-level))
  12780. (if (equal start-level 1) (throw 'exit nil))
  12781. (while (re-search-backward re nil t)
  12782. (setq level (funcall outline-level))
  12783. (if (< level start-level) (throw 'exit level)))
  12784. nil)))
  12785. (defun org-first-sibling-p ()
  12786. "Is this heading the first child of its parents?"
  12787. (interactive)
  12788. (let ((re (concat "^" outline-regexp))
  12789. level l)
  12790. (unless (org-at-heading-p t)
  12791. (error "Not at a heading"))
  12792. (setq level (funcall outline-level))
  12793. (save-excursion
  12794. (if (not (re-search-backward re nil t))
  12795. t
  12796. (setq l (funcall outline-level))
  12797. (< l level)))))
  12798. (defun org-goto-sibling (&optional previous)
  12799. "Goto the next sibling, even if it is invisible.
  12800. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12801. when a sibling was found. When none is found, return nil and don't
  12802. move point."
  12803. (let ((fun (if previous 're-search-backward 're-search-forward))
  12804. (pos (point))
  12805. (re (concat "^" outline-regexp))
  12806. level l)
  12807. (when (condition-case nil (org-back-to-heading t) (error nil))
  12808. (setq level (funcall outline-level))
  12809. (catch 'exit
  12810. (or previous (forward-char 1))
  12811. (while (funcall fun re nil t)
  12812. (setq l (funcall outline-level))
  12813. (when (< l level) (goto-char pos) (throw 'exit nil))
  12814. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12815. (goto-char pos)
  12816. nil))))
  12817. (defun org-show-siblings ()
  12818. "Show all siblings of the current headline."
  12819. (save-excursion
  12820. (while (org-goto-sibling) (org-flag-heading nil)))
  12821. (save-excursion
  12822. (while (org-goto-sibling 'previous)
  12823. (org-flag-heading nil))))
  12824. (defun org-show-hidden-entry ()
  12825. "Show an entry where even the heading is hidden."
  12826. (save-excursion
  12827. (org-show-entry)))
  12828. (defun org-flag-heading (flag &optional entry)
  12829. "Flag the current heading. FLAG non-nil means make invisible.
  12830. When ENTRY is non-nil, show the entire entry."
  12831. (save-excursion
  12832. (org-back-to-heading t)
  12833. ;; Check if we should show the entire entry
  12834. (if entry
  12835. (progn
  12836. (org-show-entry)
  12837. (save-excursion
  12838. (and (outline-next-heading)
  12839. (org-flag-heading nil))))
  12840. (outline-flag-region (max (point-min) (1- (point)))
  12841. (save-excursion (outline-end-of-heading) (point))
  12842. flag))))
  12843. (defun org-forward-same-level (arg)
  12844. "Move forward to the ARG'th subheading at same level as this one.
  12845. Stop at the first and last subheadings of a superior heading.
  12846. This is like outline-forward-same-level, but invisible headings are ok."
  12847. (interactive "p")
  12848. (outline-back-to-heading t)
  12849. (while (> arg 0)
  12850. (let ((point-to-move-to (save-excursion
  12851. (org-get-next-sibling))))
  12852. (if point-to-move-to
  12853. (progn
  12854. (goto-char point-to-move-to)
  12855. (setq arg (1- arg)))
  12856. (progn
  12857. (setq arg 0)
  12858. (error "No following same-level heading"))))))
  12859. (defun org-get-next-sibling ()
  12860. "Move to next heading of the same level, and return point.
  12861. If there is no such heading, return nil.
  12862. This is like outline-next-sibling, but invisible headings are ok."
  12863. (let ((level (funcall outline-level)))
  12864. (outline-next-heading)
  12865. (while (and (not (eobp)) (> (funcall outline-level) level))
  12866. (outline-next-heading))
  12867. (if (or (eobp) (< (funcall outline-level) level))
  12868. nil
  12869. (point))))
  12870. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12871. ;; This is an exact copy of the original function, but it uses
  12872. ;; `org-back-to-heading', to make it work also in invisible
  12873. ;; trees. And is uses an invisible-OK argument.
  12874. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12875. (org-back-to-heading invisible-OK)
  12876. (let ((first t)
  12877. (level (funcall outline-level)))
  12878. (while (and (not (eobp))
  12879. (or first (> (funcall outline-level) level)))
  12880. (setq first nil)
  12881. (outline-next-heading))
  12882. (unless to-heading
  12883. (if (memq (preceding-char) '(?\n ?\^M))
  12884. (progn
  12885. ;; Go to end of line before heading
  12886. (forward-char -1)
  12887. (if (memq (preceding-char) '(?\n ?\^M))
  12888. ;; leave blank line before heading
  12889. (forward-char -1))))))
  12890. (point))
  12891. (defun org-show-subtree ()
  12892. "Show everything after this heading at deeper levels."
  12893. (outline-flag-region
  12894. (point)
  12895. (save-excursion
  12896. (outline-end-of-subtree) (outline-next-heading) (point))
  12897. nil))
  12898. (defun org-show-entry ()
  12899. "Show the body directly following this heading.
  12900. Show the heading too, if it is currently invisible."
  12901. (interactive)
  12902. (save-excursion
  12903. (condition-case nil
  12904. (progn
  12905. (org-back-to-heading t)
  12906. (outline-flag-region
  12907. (max (point-min) (1- (point)))
  12908. (save-excursion
  12909. (re-search-forward
  12910. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12911. (or (match-beginning 1) (point-max)))
  12912. nil))
  12913. (error nil))))
  12914. (defun org-make-options-regexp (kwds)
  12915. "Make a regular expression for keyword lines."
  12916. (concat
  12917. "^"
  12918. "#?[ \t]*\\+\\("
  12919. (mapconcat 'regexp-quote kwds "\\|")
  12920. "\\):[ \t]*"
  12921. "\\(.+\\)"))
  12922. ;; Make isearch reveal the necessary context
  12923. (defun org-isearch-end ()
  12924. "Reveal context after isearch exits."
  12925. (when isearch-success ; only if search was successful
  12926. (if (featurep 'xemacs)
  12927. ;; Under XEmacs, the hook is run in the correct place,
  12928. ;; we directly show the context.
  12929. (org-show-context 'isearch)
  12930. ;; In Emacs the hook runs *before* restoring the overlays.
  12931. ;; So we have to use a one-time post-command-hook to do this.
  12932. ;; (Emacs 22 has a special variable, see function `org-mode')
  12933. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12934. isearch-mode-end-hook-quit)
  12935. ;; Only when the isearch was not quitted.
  12936. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12937. 'append 'local)))))
  12938. (defun org-isearch-post-command ()
  12939. "Remove self from hook, and show context."
  12940. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12941. (org-show-context 'isearch))
  12942. ;;;; Integration with and fixes for other packages
  12943. ;;; Imenu support
  12944. (defvar org-imenu-markers nil
  12945. "All markers currently used by Imenu.")
  12946. (make-variable-buffer-local 'org-imenu-markers)
  12947. (defun org-imenu-new-marker (&optional pos)
  12948. "Return a new marker for use by Imenu, and remember the marker."
  12949. (let ((m (make-marker)))
  12950. (move-marker m (or pos (point)))
  12951. (push m org-imenu-markers)
  12952. m))
  12953. (defun org-imenu-get-tree ()
  12954. "Produce the index for Imenu."
  12955. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12956. (setq org-imenu-markers nil)
  12957. (let* ((n org-imenu-depth)
  12958. (re (concat "^" outline-regexp))
  12959. (subs (make-vector (1+ n) nil))
  12960. (last-level 0)
  12961. m tree level head)
  12962. (save-excursion
  12963. (save-restriction
  12964. (widen)
  12965. (goto-char (point-max))
  12966. (while (re-search-backward re nil t)
  12967. (setq level (org-reduced-level (funcall outline-level)))
  12968. (when (<= level n)
  12969. (looking-at org-complex-heading-regexp)
  12970. (setq head (org-link-display-format
  12971. (org-match-string-no-properties 4))
  12972. m (org-imenu-new-marker))
  12973. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12974. (if (>= level last-level)
  12975. (push (cons head m) (aref subs level))
  12976. (push (cons head (aref subs (1+ level))) (aref subs level))
  12977. (loop for i from (1+ level) to n do (aset subs i nil)))
  12978. (setq last-level level)))))
  12979. (aref subs 1)))
  12980. (eval-after-load "imenu"
  12981. '(progn
  12982. (add-hook 'imenu-after-jump-hook
  12983. (lambda ()
  12984. (if (eq major-mode 'org-mode)
  12985. (org-show-context 'org-goto))))))
  12986. (defun org-link-display-format (link)
  12987. "Replace a link with either the description, or the link target
  12988. if no description is present"
  12989. (save-match-data
  12990. (if (string-match org-bracket-link-analytic-regexp link)
  12991. (replace-match (or (match-string 5 link)
  12992. (concat (match-string 1 link)
  12993. (match-string 3 link)))
  12994. nil nil link)
  12995. link)))
  12996. ;; Speedbar support
  12997. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12998. "Overlay marking the agenda restriction line in speedbar.")
  12999. (org-overlay-put org-speedbar-restriction-lock-overlay
  13000. 'face 'org-agenda-restriction-lock)
  13001. (org-overlay-put org-speedbar-restriction-lock-overlay
  13002. 'help-echo "Agendas are currently limited to this item.")
  13003. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13004. (defun org-speedbar-set-agenda-restriction ()
  13005. "Restrict future agenda commands to the location at point in speedbar.
  13006. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13007. (interactive)
  13008. (require 'org-agenda)
  13009. (let (p m tp np dir txt w)
  13010. (cond
  13011. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13012. 'org-imenu t))
  13013. (setq m (get-text-property p 'org-imenu-marker))
  13014. (save-excursion
  13015. (save-restriction
  13016. (set-buffer (marker-buffer m))
  13017. (goto-char m)
  13018. (org-agenda-set-restriction-lock 'subtree))))
  13019. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13020. 'speedbar-function 'speedbar-find-file))
  13021. (setq tp (previous-single-property-change
  13022. (1+ p) 'speedbar-function)
  13023. np (next-single-property-change
  13024. tp 'speedbar-function)
  13025. dir (speedbar-line-directory)
  13026. txt (buffer-substring-no-properties (or tp (point-min))
  13027. (or np (point-max))))
  13028. (save-excursion
  13029. (save-restriction
  13030. (set-buffer (find-file-noselect
  13031. (let ((default-directory dir))
  13032. (expand-file-name txt))))
  13033. (unless (org-mode-p)
  13034. (error "Cannot restrict to non-Org-mode file"))
  13035. (org-agenda-set-restriction-lock 'file))))
  13036. (t (error "Don't know how to restrict Org-mode's agenda")))
  13037. (org-move-overlay org-speedbar-restriction-lock-overlay
  13038. (point-at-bol) (point-at-eol))
  13039. (setq current-prefix-arg nil)
  13040. (org-agenda-maybe-redo)))
  13041. (eval-after-load "speedbar"
  13042. '(progn
  13043. (speedbar-add-supported-extension ".org")
  13044. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13045. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13046. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13047. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13048. (add-hook 'speedbar-visiting-tag-hook
  13049. (lambda () (org-show-context 'org-goto)))))
  13050. ;;; Fixes and Hacks for problems with other packages
  13051. ;; Make flyspell not check words in links, to not mess up our keymap
  13052. (defun org-mode-flyspell-verify ()
  13053. "Don't let flyspell put overlays at active buttons."
  13054. (not (get-text-property (point) 'keymap)))
  13055. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13056. (eval-after-load "bookmark"
  13057. '(if (boundp 'bookmark-after-jump-hook)
  13058. ;; We can use the hook
  13059. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13060. ;; Hook not available, use advice
  13061. (defadvice bookmark-jump (after org-make-visible activate)
  13062. "Make the position visible."
  13063. (org-bookmark-jump-unhide))))
  13064. (defun org-bookmark-jump-unhide ()
  13065. "Unhide the current position, to show the bookmark location."
  13066. (and (org-mode-p)
  13067. (or (org-invisible-p)
  13068. (save-excursion (goto-char (max (point-min) (1- (point))))
  13069. (org-invisible-p)))
  13070. (org-show-context 'bookmark-jump)))
  13071. ;; Make session.el ignore our circular variable
  13072. (eval-after-load "session"
  13073. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13074. ;;;; Experimental code
  13075. (defun org-closed-in-range ()
  13076. "Sparse tree of items closed in a certain time range.
  13077. Still experimental, may disappear in the future."
  13078. (interactive)
  13079. ;; Get the time interval from the user.
  13080. (let* ((time1 (time-to-seconds
  13081. (org-read-date nil 'to-time nil "Starting date: ")))
  13082. (time2 (time-to-seconds
  13083. (org-read-date nil 'to-time nil "End date:")))
  13084. ;; callback function
  13085. (callback (lambda ()
  13086. (let ((time
  13087. (time-to-seconds
  13088. (apply 'encode-time
  13089. (org-parse-time-string
  13090. (match-string 1))))))
  13091. ;; check if time in interval
  13092. (and (>= time time1) (<= time time2))))))
  13093. ;; make tree, check each match with the callback
  13094. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13095. ;;;; Finish up
  13096. (provide 'org)
  13097. (run-hooks 'org-load-hook)
  13098. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13099. ;;; org.el ends here