org.el 514 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008
  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.03pre03
  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. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.03pre03"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (when (org-bound-and-true-p org-modules)
  124. (let ((a (member 'org-infojs org-modules)))
  125. (and a (setcar a 'org-jsinfo))))
  126. (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)
  127. "Modules that should always be loaded together with org.el.
  128. If a description starts with <C>, the file is not part of Emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution.
  131. You can also use this system to load external packages (i.e. neither Org
  132. core modules, not modules from the CONTRIB directory). Just add symbols
  133. to the end of the list. If the package is called org-xyz.el, then you need
  134. to add the symbol `xyz', and the package must have a call to
  135. (provide 'org-xyz)"
  136. :group 'org
  137. :set 'org-set-modules
  138. :type
  139. '(set :greedy t
  140. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  141. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  142. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  143. (const :tag " info: Links to Info nodes" org-info)
  144. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  145. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  146. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  147. (const :tag " mew Links to Mew folders/messages" org-mew)
  148. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  149. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  150. (const :tag " vm: Links to VM folders/messages" org-vm)
  151. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  152. (const :tag " mouse: Additional mouse support" org-mouse)
  153. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  154. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  155. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  156. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  157. (const :tag "C eval: Include command output as text" org-eval)
  158. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  159. (const :tag "C id: Global id's for identifying entries" org-id)
  160. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  161. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  162. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  163. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  164. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  165. (const :tag "C registry: A registry for Org links" org-registry)
  166. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  167. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  168. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  169. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  170. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  171. (defgroup org-startup nil
  172. "Options concerning startup of Org-mode."
  173. :tag "Org Startup"
  174. :group 'org)
  175. (defcustom org-startup-folded t
  176. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  177. This can also be configured on a per-file basis by adding one of
  178. the following lines anywhere in the buffer:
  179. #+STARTUP: fold
  180. #+STARTUP: nofold
  181. #+STARTUP: content"
  182. :group 'org-startup
  183. :type '(choice
  184. (const :tag "nofold: show all" nil)
  185. (const :tag "fold: overview" t)
  186. (const :tag "content: all headlines" content)))
  187. (defcustom org-startup-truncated t
  188. "Non-nil means, entering Org-mode will set `truncate-lines'.
  189. This is useful since some lines containing links can be very long and
  190. uninteresting. Also tables look terrible when wrapped."
  191. :group 'org-startup
  192. :type 'boolean)
  193. (defcustom org-startup-align-all-tables nil
  194. "Non-nil means, align all tables when visiting a file.
  195. This is useful when the column width in tables is forced with <N> cookies
  196. in table fields. Such tables will look correct only after the first re-align.
  197. This can also be configured on a per-file basis by adding one of
  198. the following lines anywhere in the buffer:
  199. #+STARTUP: align
  200. #+STARTUP: noalign"
  201. :group 'org-startup
  202. :type 'boolean)
  203. (defcustom org-insert-mode-line-in-empty-file nil
  204. "Non-nil means insert the first line setting Org-mode in empty files.
  205. When the function `org-mode' is called interactively in an empty file, this
  206. normally means that the file name does not automatically trigger Org-mode.
  207. To ensure that the file will always be in Org-mode in the future, a
  208. line enforcing Org-mode will be inserted into the buffer, if this option
  209. has been set."
  210. :group 'org-startup
  211. :type 'boolean)
  212. (defcustom org-replace-disputed-keys nil
  213. "Non-nil means use alternative key bindings for some keys.
  214. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  215. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  216. If you want to use Org-mode together with one of these other modes,
  217. or more generally if you would like to move some Org-mode commands to
  218. other keys, set this variable and configure the keys with the variable
  219. `org-disputed-keys'.
  220. This option is only relevant at load-time of Org-mode, and must be set
  221. *before* org.el is loaded. Changing it requires a restart of Emacs to
  222. become effective."
  223. :group 'org-startup
  224. :type 'boolean)
  225. (if (fboundp 'defvaralias)
  226. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  227. (defcustom org-disputed-keys
  228. '(([(shift up)] . [(meta p)])
  229. ([(shift down)] . [(meta n)])
  230. ([(shift left)] . [(meta -)])
  231. ([(shift right)] . [(meta +)])
  232. ([(control shift right)] . [(meta shift +)])
  233. ([(control shift left)] . [(meta shift -)]))
  234. "Keys for which Org-mode and other modes compete.
  235. This is an alist, cars are the default keys, second element specifies
  236. the alternative to use when `org-replace-disputed-keys' is t.
  237. Keys can be specified in any syntax supported by `define-key'.
  238. The value of this option takes effect only at Org-mode's startup,
  239. therefore you'll have to restart Emacs to apply it after changing."
  240. :group 'org-startup
  241. :type 'alist)
  242. (defun org-key (key)
  243. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  244. Or return the original if not disputed."
  245. (if org-replace-disputed-keys
  246. (let* ((nkey (key-description key))
  247. (x (org-find-if (lambda (x)
  248. (equal (key-description (car x)) nkey))
  249. org-disputed-keys)))
  250. (if x (cdr x) key))
  251. key))
  252. (defun org-find-if (predicate seq)
  253. (catch 'exit
  254. (while seq
  255. (if (funcall predicate (car seq))
  256. (throw 'exit (car seq))
  257. (pop seq)))))
  258. (defun org-defkey (keymap key def)
  259. "Define a key, possibly translated, as returned by `org-key'."
  260. (define-key keymap (org-key key) def))
  261. (defcustom org-ellipsis nil
  262. "The ellipsis to use in the Org-mode outline.
  263. When nil, just use the standard three dots. When a string, use that instead,
  264. When a face, use the standart 3 dots, but with the specified face.
  265. The change affects only Org-mode (which will then use its own display table).
  266. Changing this requires executing `M-x org-mode' in a buffer to become
  267. effective."
  268. :group 'org-startup
  269. :type '(choice (const :tag "Default" nil)
  270. (face :tag "Face" :value org-warning)
  271. (string :tag "String" :value "...#")))
  272. (defvar org-display-table nil
  273. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  274. (defgroup org-keywords nil
  275. "Keywords in Org-mode."
  276. :tag "Org Keywords"
  277. :group 'org)
  278. (defcustom org-deadline-string "DEADLINE:"
  279. "String to mark deadline entries.
  280. A deadline is this string, followed by a time stamp. Should be a word,
  281. terminated by a colon. You can insert a schedule keyword and
  282. a timestamp with \\[org-deadline].
  283. Changes become only effective after restarting Emacs."
  284. :group 'org-keywords
  285. :type 'string)
  286. (defcustom org-scheduled-string "SCHEDULED:"
  287. "String to mark scheduled TODO entries.
  288. A schedule is this string, followed by a time stamp. Should be a word,
  289. terminated by a colon. You can insert a schedule keyword and
  290. a timestamp with \\[org-schedule].
  291. Changes become only effective after restarting Emacs."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-closed-string "CLOSED:"
  295. "String used as the prefix for timestamps logging closing a TODO entry."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-clock-string "CLOCK:"
  299. "String used as prefix for timestamps clocking work hours on an item."
  300. :group 'org-keywords
  301. :type 'string)
  302. (defcustom org-comment-string "COMMENT"
  303. "Entries starting with this keyword will never be exported.
  304. An entry can be toggled between COMMENT and normal with
  305. \\[org-toggle-comment].
  306. Changes become only effective after restarting Emacs."
  307. :group 'org-keywords
  308. :type 'string)
  309. (defcustom org-quote-string "QUOTE"
  310. "Entries starting with this keyword will be exported in fixed-width font.
  311. Quoting applies only to the text in the entry following the headline, and does
  312. not extend beyond the next headline, even if that is lower level.
  313. An entry can be toggled between QUOTE and normal with
  314. \\[org-toggle-fixed-width-section]."
  315. :group 'org-keywords
  316. :type 'string)
  317. (defconst org-repeat-re
  318. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  319. "Regular expression for specifying repeated events.
  320. After a match, group 1 contains the repeat expression.")
  321. (defgroup org-structure nil
  322. "Options concerning the general structure of Org-mode files."
  323. :tag "Org Structure"
  324. :group 'org)
  325. (defgroup org-reveal-location nil
  326. "Options about how to make context of a location visible."
  327. :tag "Org Reveal Location"
  328. :group 'org-structure)
  329. (defconst org-context-choice
  330. '(choice
  331. (const :tag "Always" t)
  332. (const :tag "Never" nil)
  333. (repeat :greedy t :tag "Individual contexts"
  334. (cons
  335. (choice :tag "Context"
  336. (const agenda)
  337. (const org-goto)
  338. (const occur-tree)
  339. (const tags-tree)
  340. (const link-search)
  341. (const mark-goto)
  342. (const bookmark-jump)
  343. (const isearch)
  344. (const default))
  345. (boolean))))
  346. "Contexts for the reveal options.")
  347. (defcustom org-show-hierarchy-above '((default . t))
  348. "Non-nil means, show full hierarchy when revealing a location.
  349. Org-mode often shows locations in an org-mode file which might have
  350. been invisible before. When this is set, the hierarchy of headings
  351. above the exposed location is shown.
  352. Turning this off for example for sparse trees makes them very compact.
  353. Instead of t, this can also be an alist specifying this option for different
  354. contexts. Valid contexts are
  355. agenda when exposing an entry from the agenda
  356. org-goto when using the command `org-goto' on key C-c C-j
  357. occur-tree when using the command `org-occur' on key C-c /
  358. tags-tree when constructing a sparse tree based on tags matches
  359. link-search when exposing search matches associated with a link
  360. mark-goto when exposing the jump goal of a mark
  361. bookmark-jump when exposing a bookmark location
  362. isearch when exiting from an incremental search
  363. default default for all contexts not set explicitly"
  364. :group 'org-reveal-location
  365. :type org-context-choice)
  366. (defcustom org-show-following-heading '((default . nil))
  367. "Non-nil means, show following heading when revealing a location.
  368. Org-mode often shows locations in an org-mode file which might have
  369. been invisible before. When this is set, the heading following the
  370. match is shown.
  371. Turning this off for example for sparse trees makes them very compact,
  372. but makes it harder to edit the location of the match. In such a case,
  373. use the command \\[org-reveal] to show more context.
  374. Instead of t, this can also be an alist specifying this option for different
  375. contexts. See `org-show-hierarchy-above' for valid contexts."
  376. :group 'org-reveal-location
  377. :type org-context-choice)
  378. (defcustom org-show-siblings '((default . nil) (isearch t))
  379. "Non-nil means, show all sibling heading when revealing a location.
  380. Org-mode often shows locations in an org-mode file which might have
  381. been invisible before. When this is set, the sibling of the current entry
  382. heading are all made visible. If `org-show-hierarchy-above' is t,
  383. the same happens on each level of the hierarchy above the current entry.
  384. By default this is on for the isearch context, off for all other contexts.
  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-entry-below '((default . nil))
  393. "Non-nil means, show the entry below a headline 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 text below the headline that is
  396. exposed is also shown.
  397. By default this is off for all contexts.
  398. Instead of t, this can also be an alist specifying this option for different
  399. contexts. See `org-show-hierarchy-above' for valid contexts."
  400. :group 'org-reveal-location
  401. :type org-context-choice)
  402. (defcustom org-indirect-buffer-display 'other-window
  403. "How should indirect tree buffers be displayed?
  404. This applies to indirect buffers created with the commands
  405. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  406. Valid values are:
  407. current-window Display in the current window
  408. other-window Just display in another window.
  409. dedicated-frame Create one new frame, and re-use it each time.
  410. new-frame Make a new frame each time. Note that in this case
  411. previously-made indirect buffers are kept, and you need to
  412. kill these buffers yourself."
  413. :group 'org-structure
  414. :group 'org-agenda-windows
  415. :type '(choice
  416. (const :tag "In current window" current-window)
  417. (const :tag "In current frame, other window" other-window)
  418. (const :tag "Each time a new frame" new-frame)
  419. (const :tag "One dedicated frame" dedicated-frame)))
  420. (defgroup org-cycle nil
  421. "Options concerning visibility cycling in Org-mode."
  422. :tag "Org Cycle"
  423. :group 'org-structure)
  424. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  425. "Names of drawers. Drawers are not opened by cycling on the headline above.
  426. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  427. this:
  428. :DRAWERNAME:
  429. .....
  430. :END:
  431. The drawer \"PROPERTIES\" is special for capturing properties through
  432. the property API.
  433. Drawers can be defined on the per-file basis with a line like:
  434. #+DRAWERS: HIDDEN STATE PROPERTIES"
  435. :group 'org-structure
  436. :type '(repeat (string :tag "Drawer Name")))
  437. (defcustom org-cycle-global-at-bob nil
  438. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  439. This makes it possible to do global cycling without having to use S-TAB or
  440. C-u TAB. For this special case to work, the first line of the buffer
  441. must not be a headline - it may be empty ot some other text. When used in
  442. this way, `org-cycle-hook' is disables temporarily, to make sure the
  443. cursor stays at the beginning of the buffer.
  444. When this option is nil, don't do anything special at the beginning
  445. of the buffer."
  446. :group 'org-cycle
  447. :type 'boolean)
  448. (defcustom org-cycle-emulate-tab t
  449. "Where should `org-cycle' emulate TAB.
  450. nil Never
  451. white Only in completely white lines
  452. whitestart Only at the beginning of lines, before the first non-white char
  453. t Everywhere except in headlines
  454. exc-hl-bol Everywhere except at the start of a headline
  455. If TAB is used in a place where it does not emulate TAB, the current subtree
  456. visibility is cycled."
  457. :group 'org-cycle
  458. :type '(choice (const :tag "Never" nil)
  459. (const :tag "Only in completely white lines" white)
  460. (const :tag "Before first char in a line" whitestart)
  461. (const :tag "Everywhere except in headlines" t)
  462. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  463. ))
  464. (defcustom org-cycle-separator-lines 2
  465. "Number of empty lines needed to keep an empty line between collapsed trees.
  466. If you leave an empty line between the end of a subtree and the following
  467. headline, this empty line is hidden when the subtree is folded.
  468. Org-mode will leave (exactly) one empty line visible if the number of
  469. empty lines is equal or larger to the number given in this variable.
  470. So the default 2 means, at least 2 empty lines after the end of a subtree
  471. are needed to produce free space between a collapsed subtree and the
  472. following headline.
  473. Special case: when 0, never leave empty lines in collapsed view."
  474. :group 'org-cycle
  475. :type 'integer)
  476. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  477. org-cycle-hide-drawers
  478. org-cycle-show-empty-lines
  479. org-optimize-window-after-visibility-change)
  480. "Hook that is run after `org-cycle' has changed the buffer visibility.
  481. The function(s) in this hook must accept a single argument which indicates
  482. the new state that was set by the most recent `org-cycle' command. The
  483. argument is a symbol. After a global state change, it can have the values
  484. `overview', `content', or `all'. After a local state change, it can have
  485. the values `folded', `children', or `subtree'."
  486. :group 'org-cycle
  487. :type 'hook)
  488. (defgroup org-edit-structure nil
  489. "Options concerning structure editing in Org-mode."
  490. :tag "Org Edit Structure"
  491. :group 'org-structure)
  492. (defcustom org-odd-levels-only nil
  493. "Non-nil means, skip even levels and only use odd levels for the outline.
  494. This has the effect that two stars are being added/taken away in
  495. promotion/demotion commands. It also influences how levels are
  496. handled by the exporters.
  497. Changing it requires restart of `font-lock-mode' to become effective
  498. for fontification also in regions already fontified.
  499. You may also set this on a per-file basis by adding one of the following
  500. lines to the buffer:
  501. #+STARTUP: odd
  502. #+STARTUP: oddeven"
  503. :group 'org-edit-structure
  504. :group 'org-font-lock
  505. :type 'boolean)
  506. (defcustom org-adapt-indentation t
  507. "Non-nil means, adapt indentation when promoting and demoting.
  508. When this is set and the *entire* text in an entry is indented, the
  509. indentation is increased by one space in a demotion command, and
  510. decreased by one in a promotion command. If any line in the entry
  511. body starts at column 0, indentation is not changed at all."
  512. :group 'org-edit-structure
  513. :type 'boolean)
  514. (defcustom org-special-ctrl-a/e nil
  515. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  516. When t, `C-a' will bring back the cursor to the beginning of the
  517. headline text, i.e. after the stars and after a possible TODO keyword.
  518. In an item, this will be the position after the bullet.
  519. When the cursor is already at that position, another `C-a' will bring
  520. it to the beginning of the line.
  521. `C-e' will jump to the end of the headline, ignoring the presence of tags
  522. in the headline. A second `C-e' will then jump to the true end of the
  523. line, after any tags.
  524. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  525. and only a directly following, identical keypress will bring the cursor
  526. to the special positions."
  527. :group 'org-edit-structure
  528. :type '(choice
  529. (const :tag "off" nil)
  530. (const :tag "after bullet first" t)
  531. (const :tag "border first" reversed)))
  532. (if (fboundp 'defvaralias)
  533. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  534. (defcustom org-special-ctrl-k nil
  535. "Non-nil means `C-k' will behave specially in headlines.
  536. When nil, `C-k' will call the default `kill-line' command.
  537. When t, the following will happen while the cursor is in the headline:
  538. - When the cursor is at the beginning of a headline, kill the entire
  539. line and possible the folded subtree below the line.
  540. - When in the middle of the headline text, kill the headline up to the tags.
  541. - When after the headline text, kill the tags."
  542. :group 'org-edit-structure
  543. :type 'boolean)
  544. (defcustom org-M-RET-may-split-line '((default . t))
  545. "Non-nil means, M-RET will split the line at the cursor position.
  546. When nil, it will go to the end of the line before making a
  547. new line.
  548. You may also set this option in a different way for different
  549. contexts. Valid contexts are:
  550. headline when creating a new headline
  551. item when creating a new item
  552. table in a table field
  553. default the value to be used for all contexts not explicitly
  554. customized"
  555. :group 'org-structure
  556. :group 'org-table
  557. :type '(choice
  558. (const :tag "Always" t)
  559. (const :tag "Never" nil)
  560. (repeat :greedy t :tag "Individual contexts"
  561. (cons
  562. (choice :tag "Context"
  563. (const headline)
  564. (const item)
  565. (const table)
  566. (const default))
  567. (boolean)))))
  568. (defcustom org-blank-before-new-entry '((heading . nil)
  569. (plain-list-item . nil))
  570. "Should `org-insert-heading' leave a blank line before new heading/item?
  571. The value is an alist, with `heading' and `plain-list-item' as car,
  572. and a boolean flag as cdr."
  573. :group 'org-edit-structure
  574. :type '(list
  575. (cons (const heading) (boolean))
  576. (cons (const plain-list-item) (boolean))))
  577. (defcustom org-insert-heading-hook nil
  578. "Hook being run after inserting a new heading."
  579. :group 'org-edit-structure
  580. :type 'hook)
  581. (defcustom org-enable-fixed-width-editor t
  582. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  583. This currently only means, they are never auto-wrapped.
  584. When nil, such lines will be treated like ordinary lines.
  585. See also the QUOTE keyword."
  586. :group 'org-edit-structure
  587. :type 'boolean)
  588. (defcustom org-goto-auto-isearch t
  589. "Non-nil means, typing characters in org-goto starts incremental search."
  590. :group 'org-edit-structure
  591. :type 'boolean)
  592. (defgroup org-sparse-trees nil
  593. "Options concerning sparse trees in Org-mode."
  594. :tag "Org Sparse Trees"
  595. :group 'org-structure)
  596. (defcustom org-highlight-sparse-tree-matches t
  597. "Non-nil means, highlight all matches that define a sparse tree.
  598. The highlights will automatically disappear the next time the buffer is
  599. changed by an edit command."
  600. :group 'org-sparse-trees
  601. :type 'boolean)
  602. (defcustom org-remove-highlights-with-change t
  603. "Non-nil means, any change to the buffer will remove temporary highlights.
  604. Such highlights are created by `org-occur' and `org-clock-display'.
  605. When nil, `C-c C-c needs to be used to get rid of the highlights.
  606. The highlights created by `org-preview-latex-fragment' always need
  607. `C-c C-c' to be removed."
  608. :group 'org-sparse-trees
  609. :group 'org-time
  610. :type 'boolean)
  611. (defcustom org-occur-hook '(org-first-headline-recenter)
  612. "Hook that is run after `org-occur' has constructed a sparse tree.
  613. This can be used to recenter the window to show as much of the structure
  614. as possible."
  615. :group 'org-sparse-trees
  616. :type 'hook)
  617. (defgroup org-plain-lists nil
  618. "Options concerning plain lists in Org-mode."
  619. :tag "Org Plain lists"
  620. :group 'org-structure)
  621. (defcustom org-cycle-include-plain-lists nil
  622. "Non-nil means, include plain lists into visibility cycling.
  623. This means that during cycling, plain list items will *temporarily* be
  624. interpreted as outline headlines with a level given by 1000+i where i is the
  625. indentation of the bullet. In all other operations, plain list items are
  626. not seen as headlines. For example, you cannot assign a TODO keyword to
  627. such an item."
  628. :group 'org-plain-lists
  629. :type 'boolean)
  630. (defcustom org-plain-list-ordered-item-terminator t
  631. "The character that makes a line with leading number an ordered list item.
  632. Valid values are ?. and ?\). To get both terminators, use t. While
  633. ?. may look nicer, it creates the danger that a line with leading
  634. number may be incorrectly interpreted as an item. ?\) therefore is
  635. the safe choice."
  636. :group 'org-plain-lists
  637. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  638. (const :tag "paren like in \"2)\"" ?\))
  639. (const :tab "both" t)))
  640. (defcustom org-empty-line-terminates-plain-lists nil
  641. "Non-nil means, an empty line ends all plain list levels.
  642. When nil, empty lines are part of the preceeding item."
  643. :group 'org-plain-lists
  644. :type 'boolean)
  645. (defcustom org-auto-renumber-ordered-lists t
  646. "Non-nil means, automatically renumber ordered plain lists.
  647. Renumbering happens when the sequence have been changed with
  648. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  649. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  650. :group 'org-plain-lists
  651. :type 'boolean)
  652. (defcustom org-provide-checkbox-statistics t
  653. "Non-nil means, update checkbox statistics after insert and toggle.
  654. When this is set, checkbox statistics is updated each time you either insert
  655. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  656. with \\[org-ctrl-c-ctrl-c\\]."
  657. :group 'org-plain-lists
  658. :type 'boolean)
  659. (defcustom org-description-max-indent 20
  660. "Maximum indentation for the second line of a description list.
  661. When the indentation would be larger than this, it will become
  662. 5 characters instead."
  663. :group 'org-plain-lists
  664. :type 'integer)
  665. (defgroup org-imenu-and-speedbar nil
  666. "Options concerning imenu and speedbar in Org-mode."
  667. :tag "Org Imenu and Speedbar"
  668. :group 'org-structure)
  669. (defcustom org-imenu-depth 2
  670. "The maximum level for Imenu access to Org-mode headlines.
  671. This also applied for speedbar access."
  672. :group 'org-imenu-and-speedbar
  673. :type 'number)
  674. (defgroup org-table nil
  675. "Options concerning tables in Org-mode."
  676. :tag "Org Table"
  677. :group 'org)
  678. (defcustom org-enable-table-editor 'optimized
  679. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  680. When nil, such lines will be treated like ordinary lines.
  681. When equal to the symbol `optimized', the table editor will be optimized to
  682. do the following:
  683. - Automatic overwrite mode in front of whitespace in table fields.
  684. This makes the structure of the table stay in tact as long as the edited
  685. field does not exceed the column width.
  686. - Minimize the number of realigns. Normally, the table is aligned each time
  687. TAB or RET are pressed to move to another field. With optimization this
  688. happens only if changes to a field might have changed the column width.
  689. Optimization requires replacing the functions `self-insert-command',
  690. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  691. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  692. very good at guessing when a re-align will be necessary, but you can always
  693. force one with \\[org-ctrl-c-ctrl-c].
  694. If you would like to use the optimized version in Org-mode, but the
  695. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  696. This variable can be used to turn on and off the table editor during a session,
  697. but in order to toggle optimization, a restart is required.
  698. See also the variable `org-table-auto-blank-field'."
  699. :group 'org-table
  700. :type '(choice
  701. (const :tag "off" nil)
  702. (const :tag "on" t)
  703. (const :tag "on, optimized" optimized)))
  704. (defcustom org-table-tab-recognizes-table.el t
  705. "Non-nil means, TAB will automatically notice a table.el table.
  706. When it sees such a table, it moves point into it and - if necessary -
  707. calls `table-recognize-table'."
  708. :group 'org-table-editing
  709. :type 'boolean)
  710. (defgroup org-link nil
  711. "Options concerning links in Org-mode."
  712. :tag "Org Link"
  713. :group 'org)
  714. (defvar org-link-abbrev-alist-local nil
  715. "Buffer-local version of `org-link-abbrev-alist', which see.
  716. The value of this is taken from the #+LINK lines.")
  717. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  718. (defcustom org-link-abbrev-alist nil
  719. "Alist of link abbreviations.
  720. The car of each element is a string, to be replaced at the start of a link.
  721. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  722. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  723. [[linkkey:tag][description]]
  724. If REPLACE is a string, the tag will simply be appended to create the link.
  725. If the string contains \"%s\", the tag will be inserted there.
  726. REPLACE may also be a function that will be called with the tag as the
  727. only argument to create the link, which should be returned as a string.
  728. See the manual for examples."
  729. :group 'org-link
  730. :type 'alist)
  731. (defcustom org-descriptive-links t
  732. "Non-nil means, hide link part and only show description of bracket links.
  733. Bracket links are like [[link][descritpion]]. This variable sets the initial
  734. state in new org-mode buffers. The setting can then be toggled on a
  735. per-buffer basis from the Org->Hyperlinks menu."
  736. :group 'org-link
  737. :type 'boolean)
  738. (defcustom org-link-file-path-type 'adaptive
  739. "How the path name in file links should be stored.
  740. Valid values are:
  741. relative Relative to the current directory, i.e. the directory of the file
  742. into which the link is being inserted.
  743. absolute Absolute path, if possible with ~ for home directory.
  744. noabbrev Absolute path, no abbreviation of home directory.
  745. adaptive Use relative path for files in the current directory and sub-
  746. directories of it. For other files, use an absolute path."
  747. :group 'org-link
  748. :type '(choice
  749. (const relative)
  750. (const absolute)
  751. (const noabbrev)
  752. (const adaptive)))
  753. (defcustom org-activate-links '(bracket angle plain radio tag date)
  754. "Types of links that should be activated in Org-mode files.
  755. This is a list of symbols, each leading to the activation of a certain link
  756. type. In principle, it does not hurt to turn on most link types - there may
  757. be a small gain when turning off unused link types. The types are:
  758. bracket The recommended [[link][description]] or [[link]] links with hiding.
  759. angular Links in angular brackes that may contain whitespace like
  760. <bbdb:Carsten Dominik>.
  761. plain Plain links in normal text, no whitespace, like http://google.com.
  762. radio Text that is matched by a radio target, see manual for details.
  763. tag Tag settings in a headline (link to tag search).
  764. date Time stamps (link to calendar).
  765. Changing this variable requires a restart of Emacs to become effective."
  766. :group 'org-link
  767. :type '(set (const :tag "Double bracket links (new style)" bracket)
  768. (const :tag "Angular bracket links (old style)" angular)
  769. (const :tag "Plain text links" plain)
  770. (const :tag "Radio target matches" radio)
  771. (const :tag "Tags" tag)
  772. (const :tag "Timestamps" date)))
  773. (defcustom org-make-link-description-function nil
  774. "Function to use to generate link descriptions from links. If
  775. nil the link location will be used. This function must take two
  776. parameters; the first is the link and the second the description
  777. org-insert-link has generated, and should return the description
  778. to use."
  779. :group 'org-link
  780. :type 'function)
  781. (defgroup org-link-store nil
  782. "Options concerning storing links in Org-mode."
  783. :tag "Org Store Link"
  784. :group 'org-link)
  785. (defcustom org-email-link-description-format "Email %c: %.30s"
  786. "Format of the description part of a link to an email or usenet message.
  787. The following %-excapes will be replaced by corresponding information:
  788. %F full \"From\" field
  789. %f name, taken from \"From\" field, address if no name
  790. %T full \"To\" field
  791. %t first name in \"To\" field, address if no name
  792. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  793. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  794. %s subject
  795. %m message-id.
  796. You may use normal field width specification between the % and the letter.
  797. This is for example useful to limit the length of the subject.
  798. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  799. :group 'org-link-store
  800. :type 'string)
  801. (defcustom org-from-is-user-regexp
  802. (let (r1 r2)
  803. (when (and user-mail-address (not (string= user-mail-address "")))
  804. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  805. (when (and user-full-name (not (string= user-full-name "")))
  806. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  807. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  808. "Regexp mached against the \"From:\" header of an email or usenet message.
  809. It should match if the message is from the user him/herself."
  810. :group 'org-link-store
  811. :type 'regexp)
  812. (defcustom org-context-in-file-links t
  813. "Non-nil means, file links from `org-store-link' contain context.
  814. A search string will be added to the file name with :: as separator and
  815. used to find the context when the link is activated by the command
  816. `org-open-at-point'.
  817. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  818. negates this setting for the duration of the command."
  819. :group 'org-link-store
  820. :type 'boolean)
  821. (defcustom org-keep-stored-link-after-insertion nil
  822. "Non-nil means, keep link in list for entire session.
  823. The command `org-store-link' adds a link pointing to the current
  824. location to an internal list. These links accumulate during a session.
  825. The command `org-insert-link' can be used to insert links into any
  826. Org-mode file (offering completion for all stored links). When this
  827. option is nil, every link which has been inserted once using \\[org-insert-link]
  828. will be removed from the list, to make completing the unused links
  829. more efficient."
  830. :group 'org-link-store
  831. :type 'boolean)
  832. (defgroup org-link-follow nil
  833. "Options concerning following links in Org-mode."
  834. :tag "Org Follow Link"
  835. :group 'org-link)
  836. (defcustom org-follow-link-hook nil
  837. "Hook that is run after a link has been followed."
  838. :group 'org-link-follow
  839. :type 'hook)
  840. (defcustom org-tab-follows-link nil
  841. "Non-nil means, on links TAB will follow the link.
  842. Needs to be set before org.el is loaded."
  843. :group 'org-link-follow
  844. :type 'boolean)
  845. (defcustom org-return-follows-link nil
  846. "Non-nil means, on links RET will follow the link.
  847. Needs to be set before org.el is loaded."
  848. :group 'org-link-follow
  849. :type 'boolean)
  850. (defcustom org-mouse-1-follows-link
  851. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  852. "Non-nil means, mouse-1 on a link will follow the link.
  853. A longer mouse click will still set point. Does not work on XEmacs.
  854. Needs to be set before org.el is loaded."
  855. :group 'org-link-follow
  856. :type 'boolean)
  857. (defcustom org-mark-ring-length 4
  858. "Number of different positions to be recorded in the ring
  859. Changing this requires a restart of Emacs to work correctly."
  860. :group 'org-link-follow
  861. :type 'interger)
  862. (defcustom org-link-frame-setup
  863. '((vm . vm-visit-folder-other-frame)
  864. (gnus . gnus-other-frame)
  865. (file . find-file-other-window))
  866. "Setup the frame configuration for following links.
  867. When following a link with Emacs, it may often be useful to display
  868. this link in another window or frame. This variable can be used to
  869. set this up for the different types of links.
  870. For VM, use any of
  871. `vm-visit-folder'
  872. `vm-visit-folder-other-frame'
  873. For Gnus, use any of
  874. `gnus'
  875. `gnus-other-frame'
  876. For FILE, use any of
  877. `find-file'
  878. `find-file-other-window'
  879. `find-file-other-frame'
  880. For the calendar, use the variable `calendar-setup'.
  881. For BBDB, it is currently only possible to display the matches in
  882. another window."
  883. :group 'org-link-follow
  884. :type '(list
  885. (cons (const vm)
  886. (choice
  887. (const vm-visit-folder)
  888. (const vm-visit-folder-other-window)
  889. (const vm-visit-folder-other-frame)))
  890. (cons (const gnus)
  891. (choice
  892. (const gnus)
  893. (const gnus-other-frame)))
  894. (cons (const file)
  895. (choice
  896. (const find-file)
  897. (const find-file-other-window)
  898. (const find-file-other-frame)))))
  899. (defcustom org-display-internal-link-with-indirect-buffer nil
  900. "Non-nil means, use indirect buffer to display infile links.
  901. Activating internal links (from one location in a file to another location
  902. in the same file) normally just jumps to the location. When the link is
  903. activated with a C-u prefix (or with mouse-3), the link is displayed in
  904. another window. When this option is set, the other window actually displays
  905. an indirect buffer clone of the current buffer, to avoid any visibility
  906. changes to the current buffer."
  907. :group 'org-link-follow
  908. :type 'boolean)
  909. (defcustom org-open-non-existing-files nil
  910. "Non-nil means, `org-open-file' will open non-existing files.
  911. When nil, an error will be generated."
  912. :group 'org-link-follow
  913. :type 'boolean)
  914. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  915. "Function and arguments to call for following mailto links.
  916. This is a list with the first element being a lisp function, and the
  917. remaining elements being arguments to the function. In string arguments,
  918. %a will be replaced by the address, and %s will be replaced by the subject
  919. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  920. :group 'org-link-follow
  921. :type '(choice
  922. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  923. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  924. (const :tag "message-mail" (message-mail "%a" "%s"))
  925. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  926. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  927. "Non-nil means, ask for confirmation before executing shell links.
  928. Shell links can be dangerous: just think about a link
  929. [[shell:rm -rf ~/*][Google Search]]
  930. This link would show up in your Org-mode document as \"Google Search\",
  931. but really it would remove your entire home directory.
  932. Therefore we advise against setting this variable to nil.
  933. Just change it to `y-or-n-p' of you want to confirm with a
  934. single keystroke rather than having to type \"yes\"."
  935. :group 'org-link-follow
  936. :type '(choice
  937. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  938. (const :tag "with y-or-n (faster)" y-or-n-p)
  939. (const :tag "no confirmation (dangerous)" nil)))
  940. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  941. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  942. Elisp links can be dangerous: just think about a link
  943. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  944. This link would show up in your Org-mode document as \"Google Search\",
  945. but really it would remove your entire home directory.
  946. Therefore we advise against setting this variable to nil.
  947. Just change it to `y-or-n-p' of you want to confirm with a
  948. single keystroke rather than having to type \"yes\"."
  949. :group 'org-link-follow
  950. :type '(choice
  951. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  952. (const :tag "with y-or-n (faster)" y-or-n-p)
  953. (const :tag "no confirmation (dangerous)" nil)))
  954. (defconst org-file-apps-defaults-gnu
  955. '((remote . emacs)
  956. (t . mailcap))
  957. "Default file applications on a UNIX or GNU/Linux system.
  958. See `org-file-apps'.")
  959. (defconst org-file-apps-defaults-macosx
  960. '((remote . emacs)
  961. (t . "open %s")
  962. ("ps" . "gv %s")
  963. ("ps.gz" . "gv %s")
  964. ("eps" . "gv %s")
  965. ("eps.gz" . "gv %s")
  966. ("dvi" . "xdvi %s")
  967. ("fig" . "xfig %s"))
  968. "Default file applications on a MacOS X system.
  969. The system \"open\" is known as a default, but we use X11 applications
  970. for some files for which the OS does not have a good default.
  971. See `org-file-apps'.")
  972. (defconst org-file-apps-defaults-windowsnt
  973. (list
  974. '(remote . emacs)
  975. (cons t
  976. (list (if (featurep 'xemacs)
  977. 'mswindows-shell-execute
  978. 'w32-shell-execute)
  979. "open" 'file)))
  980. "Default file applications on a Windows NT system.
  981. The system \"open\" is used for most files.
  982. See `org-file-apps'.")
  983. (defcustom org-file-apps
  984. '(
  985. ("txt" . emacs)
  986. ("tex" . emacs)
  987. ("ltx" . emacs)
  988. ("org" . emacs)
  989. ("el" . emacs)
  990. ("bib" . emacs)
  991. )
  992. "External applications for opening `file:path' items in a document.
  993. Org-mode uses system defaults for different file types, but
  994. you can use this variable to set the application for a given file
  995. extension. The entries in this list are cons cells where the car identifies
  996. files and the cdr the corresponding command. Possible values for the
  997. file identifier are
  998. \"ext\" A string identifying an extension
  999. `directory' Matches a directory
  1000. `remote' Matches a remote file, accessible through tramp or efs.
  1001. Remote files most likely should be visited through Emacs
  1002. because external applications cannot handle such paths.
  1003. t Default for all remaining files
  1004. Possible values for the command are:
  1005. `emacs' The file will be visited by the current Emacs process.
  1006. `default' Use the default application for this file type.
  1007. string A command to be executed by a shell; %s will be replaced
  1008. by the path to the file.
  1009. sexp A Lisp form which will be evaluated. The file path will
  1010. be available in the Lisp variable `file'.
  1011. For more examples, see the system specific constants
  1012. `org-file-apps-defaults-macosx'
  1013. `org-file-apps-defaults-windowsnt'
  1014. `org-file-apps-defaults-gnu'."
  1015. :group 'org-link-follow
  1016. :type '(repeat
  1017. (cons (choice :value ""
  1018. (string :tag "Extension")
  1019. (const :tag "Default for unrecognized files" t)
  1020. (const :tag "Remote file" remote)
  1021. (const :tag "Links to a directory" directory))
  1022. (choice :value ""
  1023. (const :tag "Visit with Emacs" emacs)
  1024. (const :tag "Use system default" default)
  1025. (string :tag "Command")
  1026. (sexp :tag "Lisp form")))))
  1027. (defgroup org-refile nil
  1028. "Options concerning refiling entries in Org-mode."
  1029. :tag "Org Remember"
  1030. :group 'org)
  1031. (defcustom org-directory "~/org"
  1032. "Directory with org files.
  1033. This directory will be used as default to prompt for org files.
  1034. Used by the hooks for remember.el."
  1035. :group 'org-refile
  1036. :group 'org-remember
  1037. :type 'directory)
  1038. (defcustom org-default-notes-file "~/.notes"
  1039. "Default target for storing notes.
  1040. Used by the hooks for remember.el. This can be a string, or nil to mean
  1041. the value of `remember-data-file'.
  1042. You can set this on a per-template basis with the variable
  1043. `org-remember-templates'."
  1044. :group 'org-refile
  1045. :group 'org-remember
  1046. :type '(choice
  1047. (const :tag "Default from remember-data-file" nil)
  1048. file))
  1049. (defcustom org-goto-interface 'outline
  1050. "The default interface to be used for `org-goto'.
  1051. Allowed vaues are:
  1052. outline The interface shows an outline of the relevant file
  1053. and the correct heading is found by moving through
  1054. the outline or by searching with incremental search.
  1055. outline-path-completion Headlines in the current buffer are offered via
  1056. completion."
  1057. :group 'org-refile
  1058. :type '(choice
  1059. (const :tag "Outline" outline)
  1060. (const :tag "Outline-path-completion" outline-path-completion)))
  1061. (defcustom org-reverse-note-order nil
  1062. "Non-nil means, store new notes at the beginning of a file or entry.
  1063. When nil, new notes will be filed to the end of a file or entry.
  1064. This can also be a list with cons cells of regular expressions that
  1065. are matched against file names, and values."
  1066. :group 'org-remember
  1067. :type '(choice
  1068. (const :tag "Reverse always" t)
  1069. (const :tag "Reverse never" nil)
  1070. (repeat :tag "By file name regexp"
  1071. (cons regexp boolean))))
  1072. (defcustom org-refile-targets nil
  1073. "Targets for refiling entries with \\[org-refile].
  1074. This is list of cons cells. Each cell contains:
  1075. - a specification of the files to be considered, either a list of files,
  1076. or a symbol whose function or variable value will be used to retrieve
  1077. a file name or a list of file names. Nil means, refile to a different
  1078. heading in the current buffer.
  1079. - A specification of how to find candidate refile targets. This may be
  1080. any of
  1081. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1082. This tag has to be present in all target headlines, inheritance will
  1083. not be considered.
  1084. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1085. todo keyword.
  1086. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1087. headlines that are refiling targets.
  1088. - a cons cell (:level . N). Any headline of level N is considered a target.
  1089. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1090. :group 'org-remember
  1091. :type '(repeat
  1092. (cons
  1093. (choice :value org-agenda-files
  1094. (const :tag "All agenda files" org-agenda-files)
  1095. (const :tag "Current buffer" nil)
  1096. (function) (variable) (file))
  1097. (choice :tag "Identify target headline by"
  1098. (cons :tag "Specific tag" (const :tag) (string))
  1099. (cons :tag "TODO keyword" (const :todo) (string))
  1100. (cons :tag "Regular expression" (const :regexp) (regexp))
  1101. (cons :tag "Level number" (const :level) (integer))
  1102. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1103. (defcustom org-refile-use-outline-path nil
  1104. "Non-nil means, provide refile targets as paths.
  1105. So a level 3 headline will be available as level1/level2/level3.
  1106. When the value is `file', also include the file name (without directory)
  1107. into the path. When `full-file-path', include the full file path."
  1108. :group 'org-remember
  1109. :type '(choice
  1110. (const :tag "Not" nil)
  1111. (const :tag "Yes" t)
  1112. (const :tag "Start with file name" file)
  1113. (const :tag "Start with full file path" full-file-path)))
  1114. (defgroup org-todo nil
  1115. "Options concerning TODO items in Org-mode."
  1116. :tag "Org TODO"
  1117. :group 'org)
  1118. (defgroup org-progress nil
  1119. "Options concerning Progress logging in Org-mode."
  1120. :tag "Org Progress"
  1121. :group 'org-time)
  1122. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1123. "List of TODO entry keyword sequences and their interpretation.
  1124. \\<org-mode-map>This is a list of sequences.
  1125. Each sequence starts with a symbol, either `sequence' or `type',
  1126. indicating if the keywords should be interpreted as a sequence of
  1127. action steps, or as different types of TODO items. The first
  1128. keywords are states requiring action - these states will select a headline
  1129. for inclusion into the global TODO list Org-mode produces. If one of
  1130. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1131. signify that no further action is necessary. If \"|\" is not found,
  1132. the last keyword is treated as the only DONE state of the sequence.
  1133. The command \\[org-todo] cycles an entry through these states, and one
  1134. additional state where no keyword is present. For details about this
  1135. cycling, see the manual.
  1136. TODO keywords and interpretation can also be set on a per-file basis with
  1137. the special #+SEQ_TODO and #+TYP_TODO lines.
  1138. Each keyword can optionally specify a character for fast state selection
  1139. \(in combination with the variable `org-use-fast-todo-selection')
  1140. and specifiers for state change logging, using the same syntax
  1141. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1142. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1143. indicates to record a time stamp each time this state is selected.
  1144. Each keyword may also specify if a timestamp or a note should be
  1145. recorded when entering or leaving the state, by adding additional
  1146. characters in the parenthesis after the keyword. This looks like this:
  1147. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1148. record only the time of the state change. With X and Y being either
  1149. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1150. Y when leaving the state if and only if the *target* state does not
  1151. define X. You may omit any of the fast-selection key or X or /Y,
  1152. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1153. For backward compatibility, this variable may also be just a list
  1154. of keywords - in this case the interptetation (sequence or type) will be
  1155. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1156. :group 'org-todo
  1157. :group 'org-keywords
  1158. :type '(choice
  1159. (repeat :tag "Old syntax, just keywords"
  1160. (string :tag "Keyword"))
  1161. (repeat :tag "New syntax"
  1162. (cons
  1163. (choice
  1164. :tag "Interpretation"
  1165. (const :tag "Sequence (cycling hits every state)" sequence)
  1166. (const :tag "Type (cycling directly to DONE)" type))
  1167. (repeat
  1168. (string :tag "Keyword"))))))
  1169. (defvar org-todo-keywords-1 nil
  1170. "All TODO and DONE keywords active in a buffer.")
  1171. (make-variable-buffer-local 'org-todo-keywords-1)
  1172. (defvar org-todo-keywords-for-agenda nil)
  1173. (defvar org-done-keywords-for-agenda nil)
  1174. (defvar org-agenda-contributing-files nil)
  1175. (defvar org-not-done-keywords nil)
  1176. (make-variable-buffer-local 'org-not-done-keywords)
  1177. (defvar org-done-keywords nil)
  1178. (make-variable-buffer-local 'org-done-keywords)
  1179. (defvar org-todo-heads nil)
  1180. (make-variable-buffer-local 'org-todo-heads)
  1181. (defvar org-todo-sets nil)
  1182. (make-variable-buffer-local 'org-todo-sets)
  1183. (defvar org-todo-log-states nil)
  1184. (make-variable-buffer-local 'org-todo-log-states)
  1185. (defvar org-todo-kwd-alist nil)
  1186. (make-variable-buffer-local 'org-todo-kwd-alist)
  1187. (defvar org-todo-key-alist nil)
  1188. (make-variable-buffer-local 'org-todo-key-alist)
  1189. (defvar org-todo-key-trigger nil)
  1190. (make-variable-buffer-local 'org-todo-key-trigger)
  1191. (defcustom org-todo-interpretation 'sequence
  1192. "Controls how TODO keywords are interpreted.
  1193. This variable is in principle obsolete and is only used for
  1194. backward compatibility, if the interpretation of todo keywords is
  1195. not given already in `org-todo-keywords'. See that variable for
  1196. more information."
  1197. :group 'org-todo
  1198. :group 'org-keywords
  1199. :type '(choice (const sequence)
  1200. (const type)))
  1201. (defcustom org-use-fast-todo-selection 'prefix
  1202. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1203. This variable describes if and under what circumstances the cycling
  1204. mechanism for TODO keywords will be replaced by a single-key, direct
  1205. selection scheme.
  1206. When nil, fast selection is never used.
  1207. When the symbol `prefix', it will be used when `org-todo' is called with
  1208. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1209. in an agenda buffer.
  1210. When t, fast selection is used by default. In this case, the prefix
  1211. argument forces cycling instead.
  1212. In all cases, the special interface is only used if access keys have actually
  1213. been assigned by the user, i.e. if keywords in the configuration are followed
  1214. by a letter in parenthesis, like TODO(t)."
  1215. :group 'org-todo
  1216. :type '(choice
  1217. (const :tag "Never" nil)
  1218. (const :tag "By default" t)
  1219. (const :tag "Only with C-u C-c C-t" prefix)))
  1220. (defcustom org-after-todo-state-change-hook nil
  1221. "Hook which is run after the state of a TODO item was changed.
  1222. The new state (a string with a TODO keyword, or nil) is available in the
  1223. Lisp variable `state'."
  1224. :group 'org-todo
  1225. :type 'hook)
  1226. (defcustom org-log-done nil
  1227. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1228. When equal to the list (done), also prompt for a closing note.
  1229. This can also be configured on a per-file basis by adding one of
  1230. the following lines anywhere in the buffer:
  1231. #+STARTUP: logdone
  1232. #+STARTUP: lognotedone
  1233. #+STARTUP: nologdone"
  1234. :group 'org-todo
  1235. :group 'org-progress
  1236. :type '(choice
  1237. (const :tag "No logging" nil)
  1238. (const :tag "Record CLOSED timestamp" time)
  1239. (const :tag "Record CLOSED timestamp with closing note." note)))
  1240. ;; Normalize old uses of org-log-done.
  1241. (cond
  1242. ((eq org-log-done t) (setq org-log-done 'time))
  1243. ((and (listp org-log-done) (memq 'done org-log-done))
  1244. (setq org-log-done 'note)))
  1245. (defcustom org-log-note-clock-out nil
  1246. "Non-nil means, recored a note when clocking out of an item.
  1247. This can also be configured on a per-file basis by adding one of
  1248. the following lines anywhere in the buffer:
  1249. #+STARTUP: lognoteclock-out
  1250. #+STARTUP: nolognoteclock-out"
  1251. :group 'org-todo
  1252. :group 'org-progress
  1253. :type 'boolean)
  1254. (defcustom org-log-done-with-time t
  1255. "Non-nil means, the CLOSED time stamp will contain date and time.
  1256. When nil, only the date will be recorded."
  1257. :group 'org-progress
  1258. :type 'boolean)
  1259. (defcustom org-log-note-headings
  1260. '((done . "CLOSING NOTE %t")
  1261. (state . "State %-12s %t")
  1262. (note . "Note taken on %t")
  1263. (clock-out . ""))
  1264. "Headings for notes added to entries.
  1265. The value is an alist, with the car being a symbol indicating the note
  1266. context, and the cdr is the heading to be used. The heading may also be the
  1267. empty string.
  1268. %t in the heading will be replaced by a time stamp.
  1269. %s will be replaced by the new TODO state, in double quotes.
  1270. %u will be replaced by the user name.
  1271. %U will be replaced by the full user name."
  1272. :group 'org-todo
  1273. :group 'org-progress
  1274. :type '(list :greedy t
  1275. (cons (const :tag "Heading when closing an item" done) string)
  1276. (cons (const :tag
  1277. "Heading when changing todo state (todo sequence only)"
  1278. state) string)
  1279. (cons (const :tag "Heading when just taking a note" note) string)
  1280. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1281. (unless (assq 'note org-log-note-headings)
  1282. (push '(note . "%t") org-log-note-headings))
  1283. (defcustom org-log-states-order-reversed t
  1284. "Non-nil means, the latest state change note will be directly after heading.
  1285. When nil, the notes will be orderer according to time."
  1286. :group 'org-todo
  1287. :group 'org-progress
  1288. :type 'boolean)
  1289. (defcustom org-log-repeat 'time
  1290. "Non-nil means, record moving through the DONE state when triggering repeat.
  1291. An auto-repeating tasks is immediately switched back to TODO when marked
  1292. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1293. the TODO keyword definition, or recording a closing note by setting
  1294. `org-log-done', there will be no record of the task moving through DONE.
  1295. This variable forces taking a note anyway. Possible values are:
  1296. nil Don't force a record
  1297. time Record a time stamp
  1298. note Record a note
  1299. This option can also be set with on a per-file-basis with
  1300. #+STARTUP: logrepeat
  1301. #+STARTUP: lognoterepeat
  1302. #+STARTUP: nologrepeat
  1303. You can have local logging settings for a subtree by setting the LOGGING
  1304. property to one or more of these keywords."
  1305. :group 'org-todo
  1306. :group 'org-progress
  1307. :type '(choice
  1308. (const :tag "Don't force a record" nil)
  1309. (const :tag "Force recording the DONE state" time)
  1310. (const :tag "Force recording a note with the DONE state" note)))
  1311. (defgroup org-priorities nil
  1312. "Priorities in Org-mode."
  1313. :tag "Org Priorities"
  1314. :group 'org-todo)
  1315. (defcustom org-highest-priority ?A
  1316. "The highest priority of TODO items. A character like ?A, ?B etc.
  1317. Must have a smaller ASCII number than `org-lowest-priority'."
  1318. :group 'org-priorities
  1319. :type 'character)
  1320. (defcustom org-lowest-priority ?C
  1321. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1322. Must have a larger ASCII number than `org-highest-priority'."
  1323. :group 'org-priorities
  1324. :type 'character)
  1325. (defcustom org-default-priority ?B
  1326. "The default priority of TODO items.
  1327. This is the priority an item get if no explicit priority is given."
  1328. :group 'org-priorities
  1329. :type 'character)
  1330. (defcustom org-priority-start-cycle-with-default t
  1331. "Non-nil means, start with default priority when starting to cycle.
  1332. When this is nil, the first step in the cycle will be (depending on the
  1333. command used) one higher or lower that the default priority."
  1334. :group 'org-priorities
  1335. :type 'boolean)
  1336. (defgroup org-time nil
  1337. "Options concerning time stamps and deadlines in Org-mode."
  1338. :tag "Org Time"
  1339. :group 'org)
  1340. (defcustom org-insert-labeled-timestamps-at-point nil
  1341. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1342. When nil, these labeled time stamps are forces into the second line of an
  1343. entry, just after the headline. When scheduling from the global TODO list,
  1344. the time stamp will always be forced into the second line."
  1345. :group 'org-time
  1346. :type 'boolean)
  1347. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1348. "Formats for `format-time-string' which are used for time stamps.
  1349. It is not recommended to change this constant.")
  1350. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1351. "Number of minutes to round time stamps to.
  1352. These are two values, the first applies when first creating a time stamp.
  1353. The second applies when changing it with the commands `S-up' and `S-down'.
  1354. When changing the time stamp, this means that it will change in steps
  1355. of N minutes, as given by the second value.
  1356. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1357. numbers should be factors of 60, so for example 5, 10, 15.
  1358. When this is larger than 1, you can still force an exact time-stamp by using
  1359. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1360. and by using a prefix arg to `S-up/down' to specify the exact number
  1361. of minutes to shift."
  1362. :group 'org-time
  1363. :get '(lambda (var) ; Make sure all entries have 5 elements
  1364. (if (integerp (default-value var))
  1365. (list (default-value var) 5)
  1366. (default-value var)))
  1367. :type '(list
  1368. (integer :tag "when inserting times")
  1369. (integer :tag "when modifying times")))
  1370. ;; Normalize old customizations of this variable.
  1371. (when (integerp org-time-stamp-rounding-minutes)
  1372. (setq org-time-stamp-rounding-minutes
  1373. (list org-time-stamp-rounding-minutes
  1374. org-time-stamp-rounding-minutes)))
  1375. (defcustom org-display-custom-times nil
  1376. "Non-nil means, overlay custom formats over all time stamps.
  1377. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1378. To turn this on on a per-file basis, insert anywhere in the file:
  1379. #+STARTUP: customtime"
  1380. :group 'org-time
  1381. :set 'set-default
  1382. :type 'sexp)
  1383. (make-variable-buffer-local 'org-display-custom-times)
  1384. (defcustom org-time-stamp-custom-formats
  1385. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1386. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1387. These are overlayed over the default ISO format if the variable
  1388. `org-display-custom-times' is set. Time like %H:%M should be at the
  1389. end of the second format."
  1390. :group 'org-time
  1391. :type 'sexp)
  1392. (defun org-time-stamp-format (&optional long inactive)
  1393. "Get the right format for a time string."
  1394. (let ((f (if long (cdr org-time-stamp-formats)
  1395. (car org-time-stamp-formats))))
  1396. (if inactive
  1397. (concat "[" (substring f 1 -1) "]")
  1398. f)))
  1399. (defcustom org-deadline-warning-days 14
  1400. "No. of days before expiration during which a deadline becomes active.
  1401. This variable governs the display in sparse trees and in the agenda.
  1402. When 0 or negative, it means use this number (the absolute value of it)
  1403. even if a deadline has a different individual lead time specified."
  1404. :group 'org-time
  1405. :group 'org-agenda-daily/weekly
  1406. :type 'number)
  1407. (defcustom org-read-date-prefer-future t
  1408. "Non-nil means, assume future for incomplete date input from user.
  1409. This affects the following situations:
  1410. 1. The user gives a day, but no month.
  1411. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1412. read this as the third of *next* month. However, if you enter \"17\",
  1413. it will be considered as *this* month.
  1414. 2. The user gives a month but not a year.
  1415. For example, if it is april and you enter \"feb 2\", this will be read
  1416. as feb 2, *next* year. \"May 5\", however, will be this year.
  1417. Currently this does not work for ISO week specifications.
  1418. When this option is nil, the current month and year will always be used
  1419. as defaults."
  1420. :group 'org-time
  1421. :type 'boolean)
  1422. (defcustom org-read-date-display-live t
  1423. "Non-nil means, display current interpretation of date prompt live.
  1424. This display will be in an overlay, in the minibuffer."
  1425. :group 'org-time
  1426. :type 'boolean)
  1427. (defcustom org-read-date-popup-calendar t
  1428. "Non-nil means, pop up a calendar when prompting for a date.
  1429. In the calendar, the date can be selected with mouse-1. However, the
  1430. minibuffer will also be active, and you can simply enter the date as well.
  1431. When nil, only the minibuffer will be available."
  1432. :group 'org-time
  1433. :type 'boolean)
  1434. (if (fboundp 'defvaralias)
  1435. (defvaralias 'org-popup-calendar-for-date-prompt
  1436. 'org-read-date-popup-calendar))
  1437. (defcustom org-extend-today-until 0
  1438. "The hour when your day really ends.
  1439. This has influence for the following applications:
  1440. - When switching the agenda to \"today\". It it is still earlier than
  1441. the time given here, the day recognized as TODAY is actually yesterday.
  1442. - When a date is read from the user and it is still before the time given
  1443. here, the current date and time will be assumed to be yesterday, 23:59.
  1444. FIXME:
  1445. IMPORTANT: This is still a very experimental feature, it may disappear
  1446. again or it may be extended to mean more things."
  1447. :group 'org-time
  1448. :type 'number)
  1449. (defcustom org-edit-timestamp-down-means-later nil
  1450. "Non-nil means, S-down will increase the time in a time stamp.
  1451. When nil, S-up will increase."
  1452. :group 'org-time
  1453. :type 'boolean)
  1454. (defcustom org-calendar-follow-timestamp-change t
  1455. "Non-nil means, make the calendar window follow timestamp changes.
  1456. When a timestamp is modified and the calendar window is visible, it will be
  1457. moved to the new date."
  1458. :group 'org-time
  1459. :type 'boolean)
  1460. (defgroup org-tags nil
  1461. "Options concerning tags in Org-mode."
  1462. :tag "Org Tags"
  1463. :group 'org)
  1464. (defcustom org-tag-alist nil
  1465. "List of tags allowed in Org-mode files.
  1466. When this list is nil, Org-mode will base TAG input on what is already in the
  1467. buffer.
  1468. The value of this variable is an alist, the car of each entry must be a
  1469. keyword as a string, the cdr may be a character that is used to select
  1470. that tag through the fast-tag-selection interface.
  1471. See the manual for details."
  1472. :group 'org-tags
  1473. :type '(repeat
  1474. (choice
  1475. (cons (string :tag "Tag name")
  1476. (character :tag "Access char"))
  1477. (const :tag "Start radio group" (:startgroup))
  1478. (const :tag "End radio group" (:endgroup)))))
  1479. (defcustom org-use-fast-tag-selection 'auto
  1480. "Non-nil means, use fast tag selection scheme.
  1481. This is a special interface to select and deselect tags with single keys.
  1482. When nil, fast selection is never used.
  1483. When the symbol `auto', fast selection is used if and only if selection
  1484. characters for tags have been configured, either through the variable
  1485. `org-tag-alist' or through a #+TAGS line in the buffer.
  1486. When t, fast selection is always used and selection keys are assigned
  1487. automatically if necessary."
  1488. :group 'org-tags
  1489. :type '(choice
  1490. (const :tag "Always" t)
  1491. (const :tag "Never" nil)
  1492. (const :tag "When selection characters are configured" 'auto)))
  1493. (defcustom org-fast-tag-selection-single-key nil
  1494. "Non-nil means, fast tag selection exits after first change.
  1495. When nil, you have to press RET to exit it.
  1496. During fast tag selection, you can toggle this flag with `C-c'.
  1497. This variable can also have the value `expert'. In this case, the window
  1498. displaying the tags menu is not even shown, until you press C-c again."
  1499. :group 'org-tags
  1500. :type '(choice
  1501. (const :tag "No" nil)
  1502. (const :tag "Yes" t)
  1503. (const :tag "Expert" expert)))
  1504. (defvar org-fast-tag-selection-include-todo nil
  1505. "Non-nil means, fast tags selection interface will also offer TODO states.
  1506. This is an undocumented feature, you should not rely on it.")
  1507. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1508. "The column to which tags should be indented in a headline.
  1509. If this number is positive, it specifies the column. If it is negative,
  1510. it means that the tags should be flushright to that column. For example,
  1511. -80 works well for a normal 80 character screen."
  1512. :group 'org-tags
  1513. :type 'integer)
  1514. (defcustom org-auto-align-tags t
  1515. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1516. These operations change the length of a headline and therefore shift
  1517. the tags around. With this options turned on, after each such operation
  1518. the tags are again aligned to `org-tags-column'."
  1519. :group 'org-tags
  1520. :type 'boolean)
  1521. (defcustom org-use-tag-inheritance t
  1522. "Non-nil means, tags in levels apply also for sublevels.
  1523. When nil, only the tags directly given in a specific line apply there.
  1524. If you turn off this option, you very likely want to turn on the
  1525. companion option `org-tags-match-list-sublevels'.
  1526. This may also be a list of tags that should be inherited, or a regexp that
  1527. matches tags that should be inherited."
  1528. :group 'org-tags
  1529. :type '(choice
  1530. (const :tag "Not" nil)
  1531. (const :tag "Always" t)
  1532. (repeat :tag "Specific tags" (string :tag "Tag"))
  1533. (regexp :tag "Tags matched by regexp")))
  1534. (defun org-tag-inherit-p (tag)
  1535. "Check if TAG is one that should be inherited."
  1536. (cond
  1537. ((eq org-use-tag-inheritance t) t)
  1538. ((not org-use-tag-inheritance) nil)
  1539. ((stringp org-use-tag-inheritance)
  1540. (string-match org-use-tag-inheritance tag))
  1541. ((listp org-use-tag-inheritance)
  1542. (member tag org-use-tag-inheritance))
  1543. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1544. (defcustom org-tags-match-list-sublevels nil
  1545. "Non-nil means list also sublevels of headlines matching tag search.
  1546. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1547. the sublevels of a headline matching a tag search often also match
  1548. the same search. Listing all of them can create very long lists.
  1549. Setting this variable to nil causes subtrees of a match to be skipped.
  1550. This option is off by default, because inheritance in on. If you turn
  1551. inheritance off, you very likely want to turn this option on.
  1552. As a special case, if the tag search is restricted to TODO items, the
  1553. value of this variable is ignored and sublevels are always checked, to
  1554. make sure all corresponding TODO items find their way into the list."
  1555. :group 'org-tags
  1556. :type 'boolean)
  1557. (defvar org-tags-history nil
  1558. "History of minibuffer reads for tags.")
  1559. (defvar org-last-tags-completion-table nil
  1560. "The last used completion table for tags.")
  1561. (defvar org-after-tags-change-hook nil
  1562. "Hook that is run after the tags in a line have changed.")
  1563. (defgroup org-properties nil
  1564. "Options concerning properties in Org-mode."
  1565. :tag "Org Properties"
  1566. :group 'org)
  1567. (defcustom org-property-format "%-10s %s"
  1568. "How property key/value pairs should be formatted by `indent-line'.
  1569. When `indent-line' hits a property definition, it will format the line
  1570. according to this format, mainly to make sure that the values are
  1571. lined-up with respect to each other."
  1572. :group 'org-properties
  1573. :type 'string)
  1574. (defcustom org-use-property-inheritance nil
  1575. "Non-nil means, properties apply also for sublevels.
  1576. This setting is chiefly used during property searches. Turning it on can
  1577. cause significant overhead when doing a search, which is why it is not
  1578. on by default.
  1579. When nil, only the properties directly given in the current entry count.
  1580. When t, every property is inherited. The value may also be a list of
  1581. properties that should have inheritance, or a regular expression matching
  1582. properties that should be inherited.
  1583. However, note that some special properties use inheritance under special
  1584. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1585. and the properties ending in \"_ALL\" when they are used as descriptor
  1586. for valid values of a property.
  1587. Note for programmers:
  1588. When querying an entry with `org-entry-get', you can control if inheritance
  1589. should be used. By default, `org-entry-get' looks only at the local
  1590. properties. You can request inheritance by setting the inherit argument
  1591. to t (to force inheritance) or to `selective' (to respect the setting
  1592. in this variable)."
  1593. :group 'org-properties
  1594. :type '(choice
  1595. (const :tag "Not" nil)
  1596. (const :tag "Always" t)
  1597. (repeat :tag "Specific properties" (string :tag "Property"))
  1598. (regexp :tag "Properties matched by regexp")))
  1599. (defun org-property-inherit-p (property)
  1600. "Check if PROPERTY is one that should be inherited."
  1601. (cond
  1602. ((eq org-use-property-inheritance t) t)
  1603. ((not org-use-property-inheritance) nil)
  1604. ((stringp org-use-property-inheritance)
  1605. (string-match org-use-property-inheritance property))
  1606. ((listp org-use-property-inheritance)
  1607. (member property org-use-property-inheritance))
  1608. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1609. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1610. "The default column format, if no other format has been defined.
  1611. This variable can be set on the per-file basis by inserting a line
  1612. #+COLUMNS: %25ITEM ....."
  1613. :group 'org-properties
  1614. :type 'string)
  1615. (defcustom org-effort-property "Effort"
  1616. "The property that is being used to keep track of effort estimates.
  1617. Effort estimates given in this property need to have the format H:MM."
  1618. :group 'org-properties
  1619. :group 'org-progress
  1620. :type '(string :tag "Property"))
  1621. (defconst org-global-properties-fixed
  1622. '(("VISIBILITY_ALL" . "folded children content all"))
  1623. "List of property/value pairs that can be inherited by any entry.
  1624. These are fixed values, for the preset properties.")
  1625. (defcustom org-global-properties nil
  1626. "List of property/value pairs that can be inherited by any entry.
  1627. You can set buffer-local values for this by adding lines like
  1628. #+PROPERTY: NAME VALUE"
  1629. :group 'org-properties
  1630. :type '(repeat
  1631. (cons (string :tag "Property")
  1632. (string :tag "Value"))))
  1633. (defvar org-local-properties nil
  1634. "List of property/value pairs that can be inherited by any entry.
  1635. Valid for the current buffer.
  1636. This variable is populated from #+PROPERTY lines.")
  1637. (defgroup org-agenda nil
  1638. "Options concerning agenda views in Org-mode."
  1639. :tag "Org Agenda"
  1640. :group 'org)
  1641. (defvar org-category nil
  1642. "Variable used by org files to set a category for agenda display.
  1643. Such files should use a file variable to set it, for example
  1644. # -*- mode: org; org-category: \"ELisp\"
  1645. or contain a special line
  1646. #+CATEGORY: ELisp
  1647. If the file does not specify a category, then file's base name
  1648. is used instead.")
  1649. (make-variable-buffer-local 'org-category)
  1650. (defcustom org-agenda-files nil
  1651. "The files to be used for agenda display.
  1652. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1653. \\[org-remove-file]. You can also use customize to edit the list.
  1654. If an entry is a directory, all files in that directory that are matched by
  1655. `org-agenda-file-regexp' will be part of the file list.
  1656. If the value of the variable is not a list but a single file name, then
  1657. the list of agenda files is actually stored and maintained in that file, one
  1658. agenda file per line."
  1659. :group 'org-agenda
  1660. :type '(choice
  1661. (repeat :tag "List of files and directories" file)
  1662. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1663. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1664. "Regular expression to match files for `org-agenda-files'.
  1665. If any element in the list in that variable contains a directory instead
  1666. of a normal file, all files in that directory that are matched by this
  1667. regular expression will be included."
  1668. :group 'org-agenda
  1669. :type 'regexp)
  1670. (defcustom org-agenda-text-search-extra-files nil
  1671. "List of extra files to be searched by text search commands.
  1672. These files will be search in addition to the agenda files by the
  1673. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1674. Note that these files will only be searched for text search commands,
  1675. not for the other agenda views like todo lists, tag searches or the weekly
  1676. agenda. This variable is intended to list notes and possibly archive files
  1677. that should also be searched by these two commands.
  1678. In fact, if the first element in the list is the symbol `agenda-archives',
  1679. than all archive files of all agenda files will be added to the search
  1680. scope."
  1681. :group 'org-agenda
  1682. :type '(set :greedy t
  1683. (const :tag "Agenda Archives" agenda-archives)
  1684. (repeat :inline t (file))))
  1685. (if (fboundp 'defvaralias)
  1686. (defvaralias 'org-agenda-multi-occur-extra-files
  1687. 'org-agenda-text-search-extra-files))
  1688. (defcustom org-agenda-skip-unavailable-files nil
  1689. "t means to just skip non-reachable files in `org-agenda-files'.
  1690. Nil means to remove them, after a query, from the list."
  1691. :group 'org-agenda
  1692. :type 'boolean)
  1693. (defcustom org-calendar-to-agenda-key [?c]
  1694. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1695. The command `org-calendar-goto-agenda' will be bound to this key. The
  1696. default is the character `c' because then `c' can be used to switch back and
  1697. forth between agenda and calendar."
  1698. :group 'org-agenda
  1699. :type 'sexp)
  1700. (eval-after-load "calendar"
  1701. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1702. 'org-calendar-goto-agenda))
  1703. (defgroup org-latex nil
  1704. "Options for embedding LaTeX code into Org-mode."
  1705. :tag "Org LaTeX"
  1706. :group 'org)
  1707. (defcustom org-format-latex-options
  1708. '(:foreground default :background default :scale 1.0
  1709. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1710. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1711. "Options for creating images from LaTeX fragments.
  1712. This is a property list with the following properties:
  1713. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1714. `default' means use the foreground of the default face.
  1715. :background the background color, or \"Transparent\".
  1716. `default' means use the background of the default face.
  1717. :scale a scaling factor for the size of the images.
  1718. :html-foreground, :html-background, :html-scale
  1719. the same numbers for HTML export.
  1720. :matchers a list indicating which matchers should be used to
  1721. find LaTeX fragments. Valid members of this list are:
  1722. \"begin\" find environments
  1723. \"$\" find math expressions surrounded by $...$
  1724. \"$$\" find math expressions surrounded by $$....$$
  1725. \"\\(\" find math expressions surrounded by \\(...\\)
  1726. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1727. :group 'org-latex
  1728. :type 'plist)
  1729. (defcustom org-format-latex-header "\\documentclass{article}
  1730. \\usepackage{fullpage} % do not remove
  1731. \\usepackage{amssymb}
  1732. \\usepackage[usenames]{color}
  1733. \\usepackage{amsmath}
  1734. \\usepackage{latexsym}
  1735. \\usepackage[mathscr]{eucal}
  1736. \\pagestyle{empty} % do not remove"
  1737. "The document header used for processing LaTeX fragments."
  1738. :group 'org-latex
  1739. :type 'string)
  1740. (defgroup org-font-lock nil
  1741. "Font-lock settings for highlighting in Org-mode."
  1742. :tag "Org Font Lock"
  1743. :group 'org)
  1744. (defcustom org-level-color-stars-only nil
  1745. "Non-nil means fontify only the stars in each headline.
  1746. When nil, the entire headline is fontified.
  1747. Changing it requires restart of `font-lock-mode' to become effective
  1748. also in regions already fontified."
  1749. :group 'org-font-lock
  1750. :type 'boolean)
  1751. (defcustom org-hide-leading-stars nil
  1752. "Non-nil means, hide the first N-1 stars in a headline.
  1753. This works by using the face `org-hide' for these stars. This
  1754. face is white for a light background, and black for a dark
  1755. background. You may have to customize the face `org-hide' to
  1756. make this work.
  1757. Changing it requires restart of `font-lock-mode' to become effective
  1758. also in regions already fontified.
  1759. You may also set this on a per-file basis by adding one of the following
  1760. lines to the buffer:
  1761. #+STARTUP: hidestars
  1762. #+STARTUP: showstars"
  1763. :group 'org-font-lock
  1764. :type 'boolean)
  1765. (defcustom org-fontify-done-headline nil
  1766. "Non-nil means, change the face of a headline if it is marked DONE.
  1767. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1768. When this is non-nil, the headline after the keyword is set to the
  1769. `org-headline-done' as an additional indication."
  1770. :group 'org-font-lock
  1771. :type 'boolean)
  1772. (defcustom org-fontify-emphasized-text t
  1773. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1774. Changing this variable requires a restart of Emacs to take effect."
  1775. :group 'org-font-lock
  1776. :type 'boolean)
  1777. (defcustom org-highlight-latex-fragments-and-specials nil
  1778. "Non-nil means, fontify what is treated specially by the exporters."
  1779. :group 'org-font-lock
  1780. :type 'boolean)
  1781. (defcustom org-hide-emphasis-markers nil
  1782. "Non-nil mean font-lock should hide the emphasis marker characters."
  1783. :group 'org-font-lock
  1784. :type 'boolean)
  1785. (defvar org-emph-re nil
  1786. "Regular expression for matching emphasis.")
  1787. (defvar org-verbatim-re nil
  1788. "Regular expression for matching verbatim text.")
  1789. (defvar org-emphasis-regexp-components) ; defined just below
  1790. (defvar org-emphasis-alist) ; defined just below
  1791. (defun org-set-emph-re (var val)
  1792. "Set variable and compute the emphasis regular expression."
  1793. (set var val)
  1794. (when (and (boundp 'org-emphasis-alist)
  1795. (boundp 'org-emphasis-regexp-components)
  1796. org-emphasis-alist org-emphasis-regexp-components)
  1797. (let* ((e org-emphasis-regexp-components)
  1798. (pre (car e))
  1799. (post (nth 1 e))
  1800. (border (nth 2 e))
  1801. (body (nth 3 e))
  1802. (nl (nth 4 e))
  1803. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1804. (body1 (concat body "*?"))
  1805. (markers (mapconcat 'car org-emphasis-alist ""))
  1806. (vmarkers (mapconcat
  1807. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1808. org-emphasis-alist "")))
  1809. ;; make sure special characters appear at the right position in the class
  1810. (if (string-match "\\^" markers)
  1811. (setq markers (concat (replace-match "" t t markers) "^")))
  1812. (if (string-match "-" markers)
  1813. (setq markers (concat (replace-match "" t t markers) "-")))
  1814. (if (string-match "\\^" vmarkers)
  1815. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1816. (if (string-match "-" vmarkers)
  1817. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1818. (if (> nl 0)
  1819. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1820. (int-to-string nl) "\\}")))
  1821. ;; Make the regexp
  1822. (setq org-emph-re
  1823. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1824. "\\("
  1825. "\\([" markers "]\\)"
  1826. "\\("
  1827. "[^" border "]\\|"
  1828. "[^" border (if (and nil stacked) markers) "]"
  1829. body1
  1830. "[^" border (if (and nil stacked) markers) "]"
  1831. "\\)"
  1832. "\\3\\)"
  1833. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1834. (setq org-verbatim-re
  1835. (concat "\\([" pre "]\\|^\\)"
  1836. "\\("
  1837. "\\([" vmarkers "]\\)"
  1838. "\\("
  1839. "[^" border "]\\|"
  1840. "[^" border "]"
  1841. body1
  1842. "[^" border "]"
  1843. "\\)"
  1844. "\\3\\)"
  1845. "\\([" post "]\\|$\\)")))))
  1846. (defcustom org-emphasis-regexp-components
  1847. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1848. "Components used to build the regular expression for emphasis.
  1849. This is a list with 6 entries. Terminology: In an emphasis string
  1850. like \" *strong word* \", we call the initial space PREMATCH, the final
  1851. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1852. and \"trong wor\" is the body. The different components in this variable
  1853. specify what is allowed/forbidden in each part:
  1854. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1855. post Chars allowed as postmatch. End of line will be allowed too.
  1856. border The chars *forbidden* as border characters.
  1857. body-regexp A regexp like \".\" to match a body character. Don't use
  1858. non-shy groups here, and don't allow newline here.
  1859. newline The maximum number of newlines allowed in an emphasis exp.
  1860. Use customize to modify this, or restart Emacs after changing it."
  1861. :group 'org-font-lock
  1862. :set 'org-set-emph-re
  1863. :type '(list
  1864. (sexp :tag "Allowed chars in pre ")
  1865. (sexp :tag "Allowed chars in post ")
  1866. (sexp :tag "Forbidden chars in border ")
  1867. (sexp :tag "Regexp for body ")
  1868. (integer :tag "number of newlines allowed")
  1869. (option (boolean :tag "Stacking (DISABLED) "))))
  1870. (defcustom org-emphasis-alist
  1871. `(("*" bold "<b>" "</b>")
  1872. ("/" italic "<i>" "</i>")
  1873. ("_" underline "<u>" "</u>")
  1874. ("=" org-code "<code>" "</code>" verbatim)
  1875. ("~" org-verbatim "" "" verbatim)
  1876. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1877. "<del>" "</del>")
  1878. )
  1879. "Special syntax for emphasized text.
  1880. Text starting and ending with a special character will be emphasized, for
  1881. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1882. characters, the face to be used by font-lock for highlighting in Org-mode
  1883. Emacs buffers, and the HTML tags to be used for this.
  1884. Use customize to modify this, or restart Emacs after changing it."
  1885. :group 'org-font-lock
  1886. :set 'org-set-emph-re
  1887. :type '(repeat
  1888. (list
  1889. (string :tag "Marker character")
  1890. (choice
  1891. (face :tag "Font-lock-face")
  1892. (plist :tag "Face property list"))
  1893. (string :tag "HTML start tag")
  1894. (string :tag "HTML end tag")
  1895. (option (const verbatim)))))
  1896. ;;; Miscellaneous options
  1897. (defgroup org-completion nil
  1898. "Completion in Org-mode."
  1899. :tag "Org Completion"
  1900. :group 'org)
  1901. (defcustom org-completion-fallback-command 'hippie-expand
  1902. "The expansion command called by \\[org-complete] in normal context.
  1903. Normal means, no org-mode-specific context."
  1904. :group 'org-completion
  1905. :type 'function)
  1906. ;;; Functions and variables from ther packages
  1907. ;; Declared here to avoid compiler warnings
  1908. ;; XEmacs only
  1909. (defvar outline-mode-menu-heading)
  1910. (defvar outline-mode-menu-show)
  1911. (defvar outline-mode-menu-hide)
  1912. (defvar zmacs-regions) ; XEmacs regions
  1913. ;; Emacs only
  1914. (defvar mark-active)
  1915. ;; Various packages
  1916. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1917. (declare-function calendar-forward-day "cal-move" (arg))
  1918. (declare-function calendar-goto-date "cal-move" (date))
  1919. (declare-function calendar-goto-today "cal-move" ())
  1920. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1921. (defvar calc-embedded-close-formula)
  1922. (defvar calc-embedded-open-formula)
  1923. (declare-function cdlatex-tab "ext:cdlatex" ())
  1924. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1925. (defvar font-lock-unfontify-region-function)
  1926. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1927. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1928. (defvar iswitchb-temp-buflist)
  1929. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1930. (declare-function org-agenda-skip "org-agenda" ())
  1931. (declare-function org-format-agenda-item "org-agenda"
  1932. (extra txt &optional category tags dotime noprefix remove-re))
  1933. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1934. (declare-function org-agenda-change-all-lines "org-agenda"
  1935. (newhead hdmarker &optional fixface))
  1936. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1937. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1938. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1939. (beg end))
  1940. (declare-function parse-time-string "parse-time" (string))
  1941. (declare-function remember "remember" (&optional initial))
  1942. (declare-function remember-buffer-desc "remember" ())
  1943. (declare-function remember-finalize "remember" ())
  1944. (defvar remember-save-after-remembering)
  1945. (defvar remember-data-file)
  1946. (defvar remember-register)
  1947. (defvar remember-buffer)
  1948. (defvar remember-handler-functions)
  1949. (defvar remember-annotation-functions)
  1950. (defvar texmathp-why)
  1951. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1952. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1953. (defvar w3m-current-url)
  1954. (defvar w3m-current-title)
  1955. (defvar org-latex-regexps)
  1956. ;;; Autoload and prepare some org modules
  1957. ;; Some table stuff that needs to be defined here, because it is used
  1958. ;; by the functions setting up org-mode or checking for table context.
  1959. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1960. "Detects an org-type or table-type table.")
  1961. (defconst org-table-line-regexp "^[ \t]*|"
  1962. "Detects an org-type table line.")
  1963. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1964. "Detects an org-type table line.")
  1965. (defconst org-table-hline-regexp "^[ \t]*|-"
  1966. "Detects an org-type table hline.")
  1967. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1968. "Detects a table-type table hline.")
  1969. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1970. "Searching from within a table (any type) this finds the first line
  1971. outside the table.")
  1972. ;; Autoload the functions in org-table.el that are needed by functions here.
  1973. (eval-and-compile
  1974. (org-autoload "org-table"
  1975. '(org-table-align org-table-begin org-table-blank-field
  1976. org-table-convert org-table-convert-region org-table-copy-down
  1977. org-table-copy-region org-table-create
  1978. org-table-create-or-convert-from-region
  1979. org-table-create-with-table.el org-table-current-dline
  1980. org-table-cut-region org-table-delete-column org-table-edit-field
  1981. org-table-edit-formulas org-table-end org-table-eval-formula
  1982. org-table-export org-table-field-info
  1983. org-table-get-stored-formulas org-table-goto-column
  1984. org-table-hline-and-move org-table-import org-table-insert-column
  1985. org-table-insert-hline org-table-insert-row org-table-iterate
  1986. org-table-justify-field-maybe org-table-kill-row
  1987. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1988. org-table-move-column org-table-move-column-left
  1989. org-table-move-column-right org-table-move-row
  1990. org-table-move-row-down org-table-move-row-up
  1991. org-table-next-field org-table-next-row org-table-paste-rectangle
  1992. org-table-previous-field org-table-recalculate
  1993. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1994. org-table-toggle-coordinate-overlays
  1995. org-table-toggle-formula-debugger org-table-wrap-region
  1996. orgtbl-mode turn-on-orgtbl)))
  1997. (defun org-at-table-p (&optional table-type)
  1998. "Return t if the cursor is inside an org-type table.
  1999. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2000. (if org-enable-table-editor
  2001. (save-excursion
  2002. (beginning-of-line 1)
  2003. (looking-at (if table-type org-table-any-line-regexp
  2004. org-table-line-regexp)))
  2005. nil))
  2006. (defsubst org-table-p () (org-at-table-p))
  2007. (defun org-at-table.el-p ()
  2008. "Return t if and only if we are at a table.el table."
  2009. (and (org-at-table-p 'any)
  2010. (save-excursion
  2011. (goto-char (org-table-begin 'any))
  2012. (looking-at org-table1-hline-regexp))))
  2013. (defun org-table-recognize-table.el ()
  2014. "If there is a table.el table nearby, recognize it and move into it."
  2015. (if org-table-tab-recognizes-table.el
  2016. (if (org-at-table.el-p)
  2017. (progn
  2018. (beginning-of-line 1)
  2019. (if (looking-at org-table-dataline-regexp)
  2020. nil
  2021. (if (looking-at org-table1-hline-regexp)
  2022. (progn
  2023. (beginning-of-line 2)
  2024. (if (looking-at org-table-any-border-regexp)
  2025. (beginning-of-line -1)))))
  2026. (if (re-search-forward "|" (org-table-end t) t)
  2027. (progn
  2028. (require 'table)
  2029. (if (table--at-cell-p (point))
  2030. t
  2031. (message "recognizing table.el table...")
  2032. (table-recognize-table)
  2033. (message "recognizing table.el table...done")))
  2034. (error "This should not happen..."))
  2035. t)
  2036. nil)
  2037. nil))
  2038. (defun org-at-table-hline-p ()
  2039. "Return t if the cursor is inside a hline in a table."
  2040. (if org-enable-table-editor
  2041. (save-excursion
  2042. (beginning-of-line 1)
  2043. (looking-at org-table-hline-regexp))
  2044. nil))
  2045. (defvar org-table-clean-did-remove-column nil)
  2046. (defun org-table-map-tables (function)
  2047. "Apply FUNCTION to the start of all tables in the buffer."
  2048. (save-excursion
  2049. (save-restriction
  2050. (widen)
  2051. (goto-char (point-min))
  2052. (while (re-search-forward org-table-any-line-regexp nil t)
  2053. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2054. (beginning-of-line 1)
  2055. (if (looking-at org-table-line-regexp)
  2056. (save-excursion (funcall function)))
  2057. (re-search-forward org-table-any-border-regexp nil 1))))
  2058. (message "Mapping tables: done"))
  2059. ;; Declare and autoload functions from org-exp.el
  2060. (declare-function org-default-export-plist "org-exp")
  2061. (declare-function org-infile-export-plist "org-exp")
  2062. (declare-function org-get-current-options "org-exp")
  2063. (eval-and-compile
  2064. (org-autoload "org-exp"
  2065. '(org-export org-export-as-ascii org-export-visible
  2066. org-insert-export-options-template org-export-as-html-and-open
  2067. org-export-as-html-batch org-export-as-html-to-buffer
  2068. org-replace-region-by-html org-export-region-as-html
  2069. org-export-as-html org-export-icalendar-this-file
  2070. org-export-icalendar-all-agenda-files
  2071. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2072. ;; Declare and autoload functions from org-exp.el
  2073. (eval-and-compile
  2074. (org-autoload "org-exp"
  2075. '(org-agenda org-agenda-list org-search-view
  2076. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2077. org-diary org-agenda-to-appt)))
  2078. ;; Autoload org-remember
  2079. (eval-and-compile
  2080. (org-autoload "org-remember"
  2081. '(org-remember-insinuate org-remember-annotation
  2082. org-remember-apply-template org-remember org-remember-handler)))
  2083. ;; Autoload org-clock.el
  2084. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2085. (beg end))
  2086. (defvar org-clock-marker (make-marker)
  2087. "Marker recording the last clock-in.")
  2088. (eval-and-compile
  2089. (org-autoload
  2090. "org-clock"
  2091. '(org-clock-in org-clock-out org-clock-cancel
  2092. org-clock-goto org-clock-sum org-clock-display
  2093. org-remove-clock-overlays org-clock-report
  2094. org-clocktable-shift org-dblock-write:clocktable
  2095. org-get-clocktable)))
  2096. (defun org-clock-update-time-maybe ()
  2097. "If this is a CLOCK line, update it and return t.
  2098. Otherwise, return nil."
  2099. (interactive)
  2100. (save-excursion
  2101. (beginning-of-line 1)
  2102. (skip-chars-forward " \t")
  2103. (when (looking-at org-clock-string)
  2104. (let ((re (concat "[ \t]*" org-clock-string
  2105. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2106. "\\([ \t]*=>.*\\)?"))
  2107. ts te h m s)
  2108. (if (not (looking-at re))
  2109. nil
  2110. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2111. (end-of-line 1)
  2112. (setq ts (match-string 1)
  2113. te (match-string 2))
  2114. (setq s (- (time-to-seconds
  2115. (apply 'encode-time (org-parse-time-string te)))
  2116. (time-to-seconds
  2117. (apply 'encode-time (org-parse-time-string ts))))
  2118. h (floor (/ s 3600))
  2119. s (- s (* 3600 h))
  2120. m (floor (/ s 60))
  2121. s (- s (* 60 s)))
  2122. (insert " => " (format "%2d:%02d" h m))
  2123. t)))))
  2124. (defun org-check-running-clock ()
  2125. "Check if the current buffer contains the running clock.
  2126. If yes, offer to stop it and to save the buffer with the changes."
  2127. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2128. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2129. (buffer-name))))
  2130. (org-clock-out)
  2131. (when (y-or-n-p "Save changed buffer?")
  2132. (save-buffer))))
  2133. (defun org-clocktable-try-shift (dir n)
  2134. "Check if this line starts a clock table, if yes, shift the time block."
  2135. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2136. (org-clocktable-shift dir n)))
  2137. ;; Autoload archiving code
  2138. ;; The stuff that is needed for cycling and tags has to be defined here.
  2139. (defgroup org-archive nil
  2140. "Options concerning archiving in Org-mode."
  2141. :tag "Org Archive"
  2142. :group 'org-structure)
  2143. (defcustom org-archive-location "%s_archive::"
  2144. "The location where subtrees should be archived.
  2145. Otherwise, the value of this variable is a string, consisting of two
  2146. parts, separated by a double-colon.
  2147. The first part is a file name - when omitted, archiving happens in the same
  2148. file. %s will be replaced by the current file name (without directory part).
  2149. Archiving to a different file is useful to keep archived entries from
  2150. contributing to the Org-mode Agenda.
  2151. The part after the double colon is a headline. The archived entries will be
  2152. filed under that headline. When omitted, the subtrees are simply filed away
  2153. at the end of the file, as top-level entries.
  2154. Here are a few examples:
  2155. \"%s_archive::\"
  2156. If the current file is Projects.org, archive in file
  2157. Projects.org_archive, as top-level trees. This is the default.
  2158. \"::* Archived Tasks\"
  2159. Archive in the current file, under the top-level headline
  2160. \"* Archived Tasks\".
  2161. \"~/org/archive.org::\"
  2162. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2163. \"basement::** Finished Tasks\"
  2164. Archive in file ./basement (relative path), as level 3 trees
  2165. below the level 2 heading \"** Finished Tasks\".
  2166. You may set this option on a per-file basis by adding to the buffer a
  2167. line like
  2168. #+ARCHIVE: basement::** Finished Tasks
  2169. You may also define it locally for a subtree by setting an ARCHIVE property
  2170. in the entry. If such a property is found in an entry, or anywhere up
  2171. the hierarchy, it will be used."
  2172. :group 'org-archive
  2173. :type 'string)
  2174. (defcustom org-archive-tag "ARCHIVE"
  2175. "The tag that marks a subtree as archived.
  2176. An archived subtree does not open during visibility cycling, and does
  2177. not contribute to the agenda listings.
  2178. After changing this, font-lock must be restarted in the relevant buffers to
  2179. get the proper fontification."
  2180. :group 'org-archive
  2181. :group 'org-keywords
  2182. :type 'string)
  2183. (defcustom org-agenda-skip-archived-trees t
  2184. "Non-nil means, the agenda will skip any items located in archived trees.
  2185. An archived tree is a tree marked with the tag ARCHIVE."
  2186. :group 'org-archive
  2187. :group 'org-agenda-skip
  2188. :type 'boolean)
  2189. (defcustom org-cycle-open-archived-trees nil
  2190. "Non-nil means, `org-cycle' will open archived trees.
  2191. An archived tree is a tree marked with the tag ARCHIVE.
  2192. When nil, archived trees will stay folded. You can still open them with
  2193. normal outline commands like `show-all', but not with the cycling commands."
  2194. :group 'org-archive
  2195. :group 'org-cycle
  2196. :type 'boolean)
  2197. (defcustom org-sparse-tree-open-archived-trees nil
  2198. "Non-nil means sparse tree construction shows matches in archived trees.
  2199. When nil, matches in these trees are highlighted, but the trees are kept in
  2200. collapsed state."
  2201. :group 'org-archive
  2202. :group 'org-sparse-trees
  2203. :type 'boolean)
  2204. (defun org-cycle-hide-archived-subtrees (state)
  2205. "Re-hide all archived subtrees after a visibility state change."
  2206. (when (and (not org-cycle-open-archived-trees)
  2207. (not (memq state '(overview folded))))
  2208. (save-excursion
  2209. (let* ((globalp (memq state '(contents all)))
  2210. (beg (if globalp (point-min) (point)))
  2211. (end (if globalp (point-max) (org-end-of-subtree t))))
  2212. (org-hide-archived-subtrees beg end)
  2213. (goto-char beg)
  2214. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2215. (message "%s" (substitute-command-keys
  2216. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2217. (defun org-force-cycle-archived ()
  2218. "Cycle subtree even if it is archived."
  2219. (interactive)
  2220. (setq this-command 'org-cycle)
  2221. (let ((org-cycle-open-archived-trees t))
  2222. (call-interactively 'org-cycle)))
  2223. (defun org-hide-archived-subtrees (beg end)
  2224. "Re-hide all archived subtrees after a visibility state change."
  2225. (save-excursion
  2226. (let* ((re (concat ":" org-archive-tag ":")))
  2227. (goto-char beg)
  2228. (while (re-search-forward re end t)
  2229. (and (org-on-heading-p) (hide-subtree))
  2230. (org-end-of-subtree t)))))
  2231. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2232. (eval-and-compile
  2233. (org-autoload "org-archive"
  2234. '(org-add-archive-files org-archive-subtree
  2235. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2236. ;; Autoload Column View Code
  2237. (declare-function org-columns-number-to-string "org-colview")
  2238. (declare-function org-columns-get-format-and-top-level "org-colview")
  2239. (declare-function org-columns-compute "org-colview")
  2240. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2241. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2242. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2243. org-columns org-insert-columns-dblock))
  2244. ;;; Variables for pre-computed regular expressions, all buffer local
  2245. (defvar org-drawer-regexp nil
  2246. "Matches first line of a hidden block.")
  2247. (make-variable-buffer-local 'org-drawer-regexp)
  2248. (defvar org-todo-regexp nil
  2249. "Matches any of the TODO state keywords.")
  2250. (make-variable-buffer-local 'org-todo-regexp)
  2251. (defvar org-not-done-regexp nil
  2252. "Matches any of the TODO state keywords except the last one.")
  2253. (make-variable-buffer-local 'org-not-done-regexp)
  2254. (defvar org-todo-line-regexp nil
  2255. "Matches a headline and puts TODO state into group 2 if present.")
  2256. (make-variable-buffer-local 'org-todo-line-regexp)
  2257. (defvar org-complex-heading-regexp nil
  2258. "Matches a headline and puts everything into groups:
  2259. group 1: the stars
  2260. group 2: The todo keyword, maybe
  2261. group 3: Priority cookie
  2262. group 4: True headline
  2263. group 5: Tags")
  2264. (make-variable-buffer-local 'org-complex-heading-regexp)
  2265. (defvar org-todo-line-tags-regexp nil
  2266. "Matches a headline and puts TODO state into group 2 if present.
  2267. Also put tags into group 4 if tags are present.")
  2268. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2269. (defvar org-nl-done-regexp nil
  2270. "Matches newline followed by a headline with the DONE keyword.")
  2271. (make-variable-buffer-local 'org-nl-done-regexp)
  2272. (defvar org-looking-at-done-regexp nil
  2273. "Matches the DONE keyword a point.")
  2274. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2275. (defvar org-ds-keyword-length 12
  2276. "Maximum length of the Deadline and SCHEDULED keywords.")
  2277. (make-variable-buffer-local 'org-ds-keyword-length)
  2278. (defvar org-deadline-regexp nil
  2279. "Matches the DEADLINE keyword.")
  2280. (make-variable-buffer-local 'org-deadline-regexp)
  2281. (defvar org-deadline-time-regexp nil
  2282. "Matches the DEADLINE keyword together with a time stamp.")
  2283. (make-variable-buffer-local 'org-deadline-time-regexp)
  2284. (defvar org-deadline-line-regexp nil
  2285. "Matches the DEADLINE keyword and the rest of the line.")
  2286. (make-variable-buffer-local 'org-deadline-line-regexp)
  2287. (defvar org-scheduled-regexp nil
  2288. "Matches the SCHEDULED keyword.")
  2289. (make-variable-buffer-local 'org-scheduled-regexp)
  2290. (defvar org-scheduled-time-regexp nil
  2291. "Matches the SCHEDULED keyword together with a time stamp.")
  2292. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2293. (defvar org-closed-time-regexp nil
  2294. "Matches the CLOSED keyword together with a time stamp.")
  2295. (make-variable-buffer-local 'org-closed-time-regexp)
  2296. (defvar org-keyword-time-regexp nil
  2297. "Matches any of the 4 keywords, together with the time stamp.")
  2298. (make-variable-buffer-local 'org-keyword-time-regexp)
  2299. (defvar org-keyword-time-not-clock-regexp nil
  2300. "Matches any of the 3 keywords, together with the time stamp.")
  2301. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2302. (defvar org-maybe-keyword-time-regexp nil
  2303. "Matches a timestamp, possibly preceeded by a keyword.")
  2304. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2305. (defvar org-planning-or-clock-line-re nil
  2306. "Matches a line with planning or clock info.")
  2307. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2308. (defconst org-plain-time-of-day-regexp
  2309. (concat
  2310. "\\(\\<[012]?[0-9]"
  2311. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2312. "\\(--?"
  2313. "\\(\\<[012]?[0-9]"
  2314. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2315. "\\)?")
  2316. "Regular expression to match a plain time or time range.
  2317. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2318. groups carry important information:
  2319. 0 the full match
  2320. 1 the first time, range or not
  2321. 8 the second time, if it is a range.")
  2322. (defconst org-plain-time-extension-regexp
  2323. (concat
  2324. "\\(\\<[012]?[0-9]"
  2325. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2326. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2327. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2328. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2329. groups carry important information:
  2330. 0 the full match
  2331. 7 hours of duration
  2332. 9 minutes of duration")
  2333. (defconst org-stamp-time-of-day-regexp
  2334. (concat
  2335. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2336. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2337. "\\(--?"
  2338. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2339. "Regular expression to match a timestamp time or time range.
  2340. After a match, the following groups carry important information:
  2341. 0 the full match
  2342. 1 date plus weekday, for backreferencing to make sure both times on same day
  2343. 2 the first time, range or not
  2344. 4 the second time, if it is a range.")
  2345. (defconst org-startup-options
  2346. '(("fold" org-startup-folded t)
  2347. ("overview" org-startup-folded t)
  2348. ("nofold" org-startup-folded nil)
  2349. ("showall" org-startup-folded nil)
  2350. ("content" org-startup-folded content)
  2351. ("hidestars" org-hide-leading-stars t)
  2352. ("showstars" org-hide-leading-stars nil)
  2353. ("odd" org-odd-levels-only t)
  2354. ("oddeven" org-odd-levels-only nil)
  2355. ("align" org-startup-align-all-tables t)
  2356. ("noalign" org-startup-align-all-tables nil)
  2357. ("customtime" org-display-custom-times t)
  2358. ("logdone" org-log-done time)
  2359. ("lognotedone" org-log-done note)
  2360. ("nologdone" org-log-done nil)
  2361. ("lognoteclock-out" org-log-note-clock-out t)
  2362. ("nolognoteclock-out" org-log-note-clock-out nil)
  2363. ("logrepeat" org-log-repeat state)
  2364. ("lognoterepeat" org-log-repeat note)
  2365. ("nologrepeat" org-log-repeat nil)
  2366. ("constcgs" constants-unit-system cgs)
  2367. ("constSI" constants-unit-system SI))
  2368. "Variable associated with STARTUP options for org-mode.
  2369. Each element is a list of three items: The startup options as written
  2370. in the #+STARTUP line, the corresponding variable, and the value to
  2371. set this variable to if the option is found. An optional forth element PUSH
  2372. means to push this value onto the list in the variable.")
  2373. (defun org-set-regexps-and-options ()
  2374. "Precompute regular expressions for current buffer."
  2375. (when (org-mode-p)
  2376. (org-set-local 'org-todo-kwd-alist nil)
  2377. (org-set-local 'org-todo-key-alist nil)
  2378. (org-set-local 'org-todo-key-trigger nil)
  2379. (org-set-local 'org-todo-keywords-1 nil)
  2380. (org-set-local 'org-done-keywords nil)
  2381. (org-set-local 'org-todo-heads nil)
  2382. (org-set-local 'org-todo-sets nil)
  2383. (org-set-local 'org-todo-log-states nil)
  2384. (let ((re (org-make-options-regexp
  2385. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2386. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2387. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2388. (splitre "[ \t]+")
  2389. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2390. tail sep kws1 prio props drawers
  2391. ext-setup-or-nil setup-contents (start 0))
  2392. (save-excursion
  2393. (save-restriction
  2394. (widen)
  2395. (goto-char (point-min))
  2396. (while (or (and ext-setup-or-nil
  2397. (string-match re ext-setup-or-nil start)
  2398. (setq start (match-end 0)))
  2399. (and (setq ext-setup-or-nil nil start 0)
  2400. (re-search-forward re nil t)))
  2401. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2402. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2403. (cond
  2404. ((equal key "CATEGORY")
  2405. (if (string-match "[ \t]+$" value)
  2406. (setq value (replace-match "" t t value)))
  2407. (setq cat value))
  2408. ((member key '("SEQ_TODO" "TODO"))
  2409. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2410. ((equal key "TYP_TODO")
  2411. (push (cons 'type (org-split-string value splitre)) kwds))
  2412. ((equal key "TAGS")
  2413. (setq tags (append tags (org-split-string value splitre))))
  2414. ((equal key "COLUMNS")
  2415. (org-set-local 'org-columns-default-format value))
  2416. ((equal key "LINK")
  2417. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2418. (push (cons (match-string 1 value)
  2419. (org-trim (match-string 2 value)))
  2420. links)))
  2421. ((equal key "PRIORITIES")
  2422. (setq prio (org-split-string value " +")))
  2423. ((equal key "PROPERTY")
  2424. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2425. (push (cons (match-string 1 value) (match-string 2 value))
  2426. props)))
  2427. ((equal key "DRAWERS")
  2428. (setq drawers (org-split-string value splitre)))
  2429. ((equal key "CONSTANTS")
  2430. (setq const (append const (org-split-string value splitre))))
  2431. ((equal key "STARTUP")
  2432. (let ((opts (org-split-string value splitre))
  2433. l var val)
  2434. (while (setq l (pop opts))
  2435. (when (setq l (assoc l org-startup-options))
  2436. (setq var (nth 1 l) val (nth 2 l))
  2437. (if (not (nth 3 l))
  2438. (set (make-local-variable var) val)
  2439. (if (not (listp (symbol-value var)))
  2440. (set (make-local-variable var) nil))
  2441. (set (make-local-variable var) (symbol-value var))
  2442. (add-to-list var val))))))
  2443. ((equal key "ARCHIVE")
  2444. (string-match " *$" value)
  2445. (setq arch (replace-match "" t t value))
  2446. (remove-text-properties 0 (length arch)
  2447. '(face t fontified t) arch))
  2448. ((equal key "SETUPFILE")
  2449. (setq setup-contents (org-file-contents
  2450. (expand-file-name
  2451. (org-remove-double-quotes value))
  2452. 'noerror))
  2453. (if (not ext-setup-or-nil)
  2454. (setq ext-setup-or-nil setup-contents start 0)
  2455. (setq ext-setup-or-nil
  2456. (concat (substring ext-setup-or-nil 0 start)
  2457. "\n" setup-contents "\n"
  2458. (substring ext-setup-or-nil start)))))
  2459. ))))
  2460. (when cat
  2461. (org-set-local 'org-category (intern cat))
  2462. (push (cons "CATEGORY" cat) props))
  2463. (when prio
  2464. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2465. (setq prio (mapcar 'string-to-char prio))
  2466. (org-set-local 'org-highest-priority (nth 0 prio))
  2467. (org-set-local 'org-lowest-priority (nth 1 prio))
  2468. (org-set-local 'org-default-priority (nth 2 prio)))
  2469. (and props (org-set-local 'org-local-properties (nreverse props)))
  2470. (and drawers (org-set-local 'org-drawers drawers))
  2471. (and arch (org-set-local 'org-archive-location arch))
  2472. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2473. ;; Process the TODO keywords
  2474. (unless kwds
  2475. ;; Use the global values as if they had been given locally.
  2476. (setq kwds (default-value 'org-todo-keywords))
  2477. (if (stringp (car kwds))
  2478. (setq kwds (list (cons org-todo-interpretation
  2479. (default-value 'org-todo-keywords)))))
  2480. (setq kwds (reverse kwds)))
  2481. (setq kwds (nreverse kwds))
  2482. (let (inter kws kw)
  2483. (while (setq kws (pop kwds))
  2484. (setq inter (pop kws) sep (member "|" kws)
  2485. kws0 (delete "|" (copy-sequence kws))
  2486. kwsa nil
  2487. kws1 (mapcar
  2488. (lambda (x)
  2489. ;; 1 2
  2490. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2491. (progn
  2492. (setq kw (match-string 1 x)
  2493. key (and (match-end 2) (match-string 2 x))
  2494. log (org-extract-log-state-settings x))
  2495. (push (cons kw (and key (string-to-char key))) kwsa)
  2496. (and log (push log org-todo-log-states))
  2497. kw)
  2498. (error "Invalid TODO keyword %s" x)))
  2499. kws0)
  2500. kwsa (if kwsa (append '((:startgroup))
  2501. (nreverse kwsa)
  2502. '((:endgroup))))
  2503. hw (car kws1)
  2504. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2505. tail (list inter hw (car dws) (org-last dws)))
  2506. (add-to-list 'org-todo-heads hw 'append)
  2507. (push kws1 org-todo-sets)
  2508. (setq org-done-keywords (append org-done-keywords dws nil))
  2509. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2510. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2511. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2512. (setq org-todo-sets (nreverse org-todo-sets)
  2513. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2514. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2515. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2516. ;; Process the constants
  2517. (when const
  2518. (let (e cst)
  2519. (while (setq e (pop const))
  2520. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2521. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2522. (setq org-table-formula-constants-local cst)))
  2523. ;; Process the tags.
  2524. (when tags
  2525. (let (e tgs)
  2526. (while (setq e (pop tags))
  2527. (cond
  2528. ((equal e "{") (push '(:startgroup) tgs))
  2529. ((equal e "}") (push '(:endgroup) tgs))
  2530. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2531. (push (cons (match-string 1 e)
  2532. (string-to-char (match-string 2 e)))
  2533. tgs))
  2534. (t (push (list e) tgs))))
  2535. (org-set-local 'org-tag-alist nil)
  2536. (while (setq e (pop tgs))
  2537. (or (and (stringp (car e))
  2538. (assoc (car e) org-tag-alist))
  2539. (push e org-tag-alist))))))
  2540. ;; Compute the regular expressions and other local variables
  2541. (if (not org-done-keywords)
  2542. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2543. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2544. (length org-scheduled-string)
  2545. (length org-clock-string)
  2546. (length org-closed-string)))
  2547. org-drawer-regexp
  2548. (concat "^[ \t]*:\\("
  2549. (mapconcat 'regexp-quote org-drawers "\\|")
  2550. "\\):[ \t]*$")
  2551. org-not-done-keywords
  2552. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2553. org-todo-regexp
  2554. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2555. "\\|") "\\)\\>")
  2556. org-not-done-regexp
  2557. (concat "\\<\\("
  2558. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2559. "\\)\\>")
  2560. org-todo-line-regexp
  2561. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2562. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2563. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2564. org-complex-heading-regexp
  2565. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2566. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2567. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2568. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2569. org-nl-done-regexp
  2570. (concat "\n\\*+[ \t]+"
  2571. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2572. "\\)" "\\>")
  2573. org-todo-line-tags-regexp
  2574. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2575. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2576. (org-re
  2577. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2578. org-looking-at-done-regexp
  2579. (concat "^" "\\(?:"
  2580. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2581. "\\>")
  2582. org-deadline-regexp (concat "\\<" org-deadline-string)
  2583. org-deadline-time-regexp
  2584. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2585. org-deadline-line-regexp
  2586. (concat "\\<\\(" org-deadline-string "\\).*")
  2587. org-scheduled-regexp
  2588. (concat "\\<" org-scheduled-string)
  2589. org-scheduled-time-regexp
  2590. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2591. org-closed-time-regexp
  2592. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2593. org-keyword-time-regexp
  2594. (concat "\\<\\(" org-scheduled-string
  2595. "\\|" org-deadline-string
  2596. "\\|" org-closed-string
  2597. "\\|" org-clock-string "\\)"
  2598. " *[[<]\\([^]>]+\\)[]>]")
  2599. org-keyword-time-not-clock-regexp
  2600. (concat "\\<\\(" org-scheduled-string
  2601. "\\|" org-deadline-string
  2602. "\\|" org-closed-string
  2603. "\\)"
  2604. " *[[<]\\([^]>]+\\)[]>]")
  2605. org-maybe-keyword-time-regexp
  2606. (concat "\\(\\<\\(" org-scheduled-string
  2607. "\\|" org-deadline-string
  2608. "\\|" org-closed-string
  2609. "\\|" org-clock-string "\\)\\)?"
  2610. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2611. org-planning-or-clock-line-re
  2612. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2613. "\\|" org-deadline-string
  2614. "\\|" org-closed-string "\\|" org-clock-string
  2615. "\\)\\>\\)")
  2616. )
  2617. (org-compute-latex-and-specials-regexp)
  2618. (org-set-font-lock-defaults)))
  2619. (defun org-file-contents (file &optional noerror)
  2620. "Return the contents of FILE, as a string."
  2621. (if (or (not file)
  2622. (not (file-readable-p file)))
  2623. (if noerror
  2624. (progn
  2625. (message "Cannot read file %s" file)
  2626. (ding) (sit-for 2)
  2627. "")
  2628. (error "Cannot read file %s" file))
  2629. (with-temp-buffer
  2630. (insert-file-contents file)
  2631. (buffer-string))))
  2632. (defun org-extract-log-state-settings (x)
  2633. "Extract the log state setting from a TODO keyword string.
  2634. This will extract info from a string like \"WAIT(w@/!)\"."
  2635. (let (kw key log1 log2)
  2636. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2637. (setq kw (match-string 1 x)
  2638. key (and (match-end 2) (match-string 2 x))
  2639. log1 (and (match-end 3) (match-string 3 x))
  2640. log2 (and (match-end 4) (match-string 4 x)))
  2641. (and (or log1 log2)
  2642. (list kw
  2643. (and log1 (if (equal log1 "!") 'time 'note))
  2644. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2645. (defun org-remove-keyword-keys (list)
  2646. "Remove a pair of parenthesis at the end of each string in LIST."
  2647. (mapcar (lambda (x)
  2648. (if (string-match "(.*)$" x)
  2649. (substring x 0 (match-beginning 0))
  2650. x))
  2651. list))
  2652. ;; FIXME: this could be done much better, using second characters etc.
  2653. (defun org-assign-fast-keys (alist)
  2654. "Assign fast keys to a keyword-key alist.
  2655. Respect keys that are already there."
  2656. (let (new e k c c1 c2 (char ?a))
  2657. (while (setq e (pop alist))
  2658. (cond
  2659. ((equal e '(:startgroup)) (push e new))
  2660. ((equal e '(:endgroup)) (push e new))
  2661. (t
  2662. (setq k (car e) c2 nil)
  2663. (if (cdr e)
  2664. (setq c (cdr e))
  2665. ;; automatically assign a character.
  2666. (setq c1 (string-to-char
  2667. (downcase (substring
  2668. k (if (= (string-to-char k) ?@) 1 0)))))
  2669. (if (or (rassoc c1 new) (rassoc c1 alist))
  2670. (while (or (rassoc char new) (rassoc char alist))
  2671. (setq char (1+ char)))
  2672. (setq c2 c1))
  2673. (setq c (or c2 char)))
  2674. (push (cons k c) new))))
  2675. (nreverse new)))
  2676. ;;; Some variables used in various places
  2677. (defvar org-window-configuration nil
  2678. "Used in various places to store a window configuration.")
  2679. (defvar org-finish-function nil
  2680. "Function to be called when `C-c C-c' is used.
  2681. This is for getting out of special buffers like remember.")
  2682. ;; FIXME: Occasionally check by commenting these, to make sure
  2683. ;; no other functions uses these, forgetting to let-bind them.
  2684. (defvar entry)
  2685. (defvar state)
  2686. (defvar last-state)
  2687. (defvar date)
  2688. (defvar description)
  2689. ;; Defined somewhere in this file, but used before definition.
  2690. (defvar org-html-entities)
  2691. (defvar org-struct-menu)
  2692. (defvar org-org-menu)
  2693. (defvar org-tbl-menu)
  2694. (defvar org-agenda-keymap)
  2695. ;;;; Define the Org-mode
  2696. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2697. (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."))
  2698. ;; We use a before-change function to check if a table might need
  2699. ;; an update.
  2700. (defvar org-table-may-need-update t
  2701. "Indicates that a table might need an update.
  2702. This variable is set by `org-before-change-function'.
  2703. `org-table-align' sets it back to nil.")
  2704. (defun org-before-change-function (beg end)
  2705. "Every change indicates that a table might need an update."
  2706. (setq org-table-may-need-update t))
  2707. (defvar org-mode-map)
  2708. (defvar org-mode-hook nil)
  2709. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2710. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2711. (defvar org-table-buffer-is-an nil)
  2712. (defconst org-outline-regexp "\\*+ ")
  2713. ;;;###autoload
  2714. (define-derived-mode org-mode outline-mode "Org"
  2715. "Outline-based notes management and organizer, alias
  2716. \"Carsten's outline-mode for keeping track of everything.\"
  2717. Org-mode develops organizational tasks around a NOTES file which
  2718. contains information about projects as plain text. Org-mode is
  2719. implemented on top of outline-mode, which is ideal to keep the content
  2720. of large files well structured. It supports ToDo items, deadlines and
  2721. time stamps, which magically appear in the diary listing of the Emacs
  2722. calendar. Tables are easily created with a built-in table editor.
  2723. Plain text URL-like links connect to websites, emails (VM), Usenet
  2724. messages (Gnus), BBDB entries, and any files related to the project.
  2725. For printing and sharing of notes, an Org-mode file (or a part of it)
  2726. can be exported as a structured ASCII or HTML file.
  2727. The following commands are available:
  2728. \\{org-mode-map}"
  2729. ;; Get rid of Outline menus, they are not needed
  2730. ;; Need to do this here because define-derived-mode sets up
  2731. ;; the keymap so late. Still, it is a waste to call this each time
  2732. ;; we switch another buffer into org-mode.
  2733. (if (featurep 'xemacs)
  2734. (when (boundp 'outline-mode-menu-heading)
  2735. ;; Assume this is Greg's port, it used easymenu
  2736. (easy-menu-remove outline-mode-menu-heading)
  2737. (easy-menu-remove outline-mode-menu-show)
  2738. (easy-menu-remove outline-mode-menu-hide))
  2739. (define-key org-mode-map [menu-bar headings] 'undefined)
  2740. (define-key org-mode-map [menu-bar hide] 'undefined)
  2741. (define-key org-mode-map [menu-bar show] 'undefined))
  2742. (org-load-modules-maybe)
  2743. (easy-menu-add org-org-menu)
  2744. (easy-menu-add org-tbl-menu)
  2745. (org-install-agenda-files-menu)
  2746. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2747. (org-add-to-invisibility-spec '(org-cwidth))
  2748. (when (featurep 'xemacs)
  2749. (org-set-local 'line-move-ignore-invisible t))
  2750. (org-set-local 'outline-regexp org-outline-regexp)
  2751. (org-set-local 'outline-level 'org-outline-level)
  2752. (when (and org-ellipsis
  2753. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2754. (fboundp 'make-glyph-code))
  2755. (unless org-display-table
  2756. (setq org-display-table (make-display-table)))
  2757. (set-display-table-slot
  2758. org-display-table 4
  2759. (vconcat (mapcar
  2760. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2761. org-ellipsis)))
  2762. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2763. (setq buffer-display-table org-display-table))
  2764. (org-set-regexps-and-options)
  2765. ;; Calc embedded
  2766. (org-set-local 'calc-embedded-open-mode "# ")
  2767. (modify-syntax-entry ?# "<")
  2768. (modify-syntax-entry ?@ "w")
  2769. (if org-startup-truncated (setq truncate-lines t))
  2770. (org-set-local 'font-lock-unfontify-region-function
  2771. 'org-unfontify-region)
  2772. ;; Activate before-change-function
  2773. (org-set-local 'org-table-may-need-update t)
  2774. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2775. 'local)
  2776. ;; Check for running clock before killing a buffer
  2777. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2778. ;; Paragraphs and auto-filling
  2779. (org-set-autofill-regexps)
  2780. (setq indent-line-function 'org-indent-line-function)
  2781. (org-update-radio-target-regexp)
  2782. ;; Comment characters
  2783. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2784. (org-set-local 'comment-padding " ")
  2785. ;; Align options lines
  2786. (org-set-local
  2787. 'align-mode-rules-list
  2788. '((org-in-buffer-settings
  2789. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2790. (modes . '(org-mode)))))
  2791. ;; Imenu
  2792. (org-set-local 'imenu-create-index-function
  2793. 'org-imenu-get-tree)
  2794. ;; Make isearch reveal context
  2795. (if (or (featurep 'xemacs)
  2796. (not (boundp 'outline-isearch-open-invisible-function)))
  2797. ;; Emacs 21 and XEmacs make use of the hook
  2798. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2799. ;; Emacs 22 deals with this through a special variable
  2800. (org-set-local 'outline-isearch-open-invisible-function
  2801. (lambda (&rest ignore) (org-show-context 'isearch))))
  2802. ;; If empty file that did not turn on org-mode automatically, make it to.
  2803. (if (and org-insert-mode-line-in-empty-file
  2804. (interactive-p)
  2805. (= (point-min) (point-max)))
  2806. (insert "# -*- mode: org -*-\n\n"))
  2807. (unless org-inhibit-startup
  2808. (when org-startup-align-all-tables
  2809. (let ((bmp (buffer-modified-p)))
  2810. (org-table-map-tables 'org-table-align)
  2811. (set-buffer-modified-p bmp)))
  2812. (org-set-startup-visibility)))
  2813. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2814. (defun org-current-time ()
  2815. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2816. (if (> (car org-time-stamp-rounding-minutes) 1)
  2817. (let ((r (car org-time-stamp-rounding-minutes))
  2818. (time (decode-time)))
  2819. (apply 'encode-time
  2820. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2821. (nthcdr 2 time))))
  2822. (current-time)))
  2823. ;;;; Font-Lock stuff, including the activators
  2824. (defvar org-mouse-map (make-sparse-keymap))
  2825. (org-defkey org-mouse-map
  2826. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2827. (org-defkey org-mouse-map
  2828. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2829. (when org-mouse-1-follows-link
  2830. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2831. (when org-tab-follows-link
  2832. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2833. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2834. (when org-return-follows-link
  2835. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2836. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2837. (require 'font-lock)
  2838. (defconst org-non-link-chars "]\t\n\r<>")
  2839. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2840. "shell" "elisp"))
  2841. (defvar org-link-types-re nil
  2842. "Matches a link that has a url-like prefix like \"http:\"")
  2843. (defvar org-link-re-with-space nil
  2844. "Matches a link with spaces, optional angular brackets around it.")
  2845. (defvar org-link-re-with-space2 nil
  2846. "Matches a link with spaces, optional angular brackets around it.")
  2847. (defvar org-angle-link-re nil
  2848. "Matches link with angular brackets, spaces are allowed.")
  2849. (defvar org-plain-link-re nil
  2850. "Matches plain link, without spaces.")
  2851. (defvar org-bracket-link-regexp nil
  2852. "Matches a link in double brackets.")
  2853. (defvar org-bracket-link-analytic-regexp nil
  2854. "Regular expression used to analyze links.
  2855. Here is what the match groups contain after a match:
  2856. 1: http:
  2857. 2: http
  2858. 3: path
  2859. 4: [desc]
  2860. 5: desc")
  2861. (defvar org-any-link-re nil
  2862. "Regular expression matching any link.")
  2863. (defun org-make-link-regexps ()
  2864. "Update the link regular expressions.
  2865. This should be called after the variable `org-link-types' has changed."
  2866. (setq org-link-types-re
  2867. (concat
  2868. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2869. org-link-re-with-space
  2870. (concat
  2871. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2872. "\\([^" org-non-link-chars " ]"
  2873. "[^" org-non-link-chars "]*"
  2874. "[^" org-non-link-chars " ]\\)>?")
  2875. org-link-re-with-space2
  2876. (concat
  2877. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2878. "\\([^" org-non-link-chars " ]"
  2879. "[^]\t\n\r]*"
  2880. "[^" org-non-link-chars " ]\\)>?")
  2881. org-angle-link-re
  2882. (concat
  2883. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2884. "\\([^" org-non-link-chars " ]"
  2885. "[^" org-non-link-chars "]*"
  2886. "\\)>")
  2887. org-plain-link-re
  2888. (concat
  2889. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2890. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2891. org-bracket-link-regexp
  2892. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2893. org-bracket-link-analytic-regexp
  2894. (concat
  2895. "\\[\\["
  2896. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2897. "\\([^]]+\\)"
  2898. "\\]"
  2899. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2900. "\\]")
  2901. org-any-link-re
  2902. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2903. org-angle-link-re "\\)\\|\\("
  2904. org-plain-link-re "\\)")))
  2905. (org-make-link-regexps)
  2906. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2907. "Regular expression for fast time stamp matching.")
  2908. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2909. "Regular expression for fast time stamp matching.")
  2910. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2911. "Regular expression matching time strings for analysis.
  2912. This one does not require the space after the date, so it can be used
  2913. on a string that terminates immediately after the date.")
  2914. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2915. "Regular expression matching time strings for analysis.")
  2916. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2917. "Regular expression matching time stamps, with groups.")
  2918. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2919. "Regular expression matching time stamps (also [..]), with groups.")
  2920. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2921. "Regular expression matching a time stamp range.")
  2922. (defconst org-tr-regexp-both
  2923. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2924. "Regular expression matching a time stamp range.")
  2925. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2926. org-ts-regexp "\\)?")
  2927. "Regular expression matching a time stamp or time stamp range.")
  2928. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2929. org-ts-regexp-both "\\)?")
  2930. "Regular expression matching a time stamp or time stamp range.
  2931. The time stamps may be either active or inactive.")
  2932. (defvar org-emph-face nil)
  2933. (defun org-do-emphasis-faces (limit)
  2934. "Run through the buffer and add overlays to links."
  2935. (let (rtn)
  2936. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2937. (if (not (= (char-after (match-beginning 3))
  2938. (char-after (match-beginning 4))))
  2939. (progn
  2940. (setq rtn t)
  2941. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2942. 'face
  2943. (nth 1 (assoc (match-string 3)
  2944. org-emphasis-alist)))
  2945. (add-text-properties (match-beginning 2) (match-end 2)
  2946. '(font-lock-multiline t))
  2947. (when org-hide-emphasis-markers
  2948. (add-text-properties (match-end 4) (match-beginning 5)
  2949. '(invisible org-link))
  2950. (add-text-properties (match-beginning 3) (match-end 3)
  2951. '(invisible org-link)))))
  2952. (backward-char 1))
  2953. rtn))
  2954. (defun org-emphasize (&optional char)
  2955. "Insert or change an emphasis, i.e. a font like bold or italic.
  2956. If there is an active region, change that region to a new emphasis.
  2957. If there is no region, just insert the marker characters and position
  2958. the cursor between them.
  2959. CHAR should be either the marker character, or the first character of the
  2960. HTML tag associated with that emphasis. If CHAR is a space, the means
  2961. to remove the emphasis of the selected region.
  2962. If char is not given (for example in an interactive call) it
  2963. will be prompted for."
  2964. (interactive)
  2965. (let ((eal org-emphasis-alist) e det
  2966. (erc org-emphasis-regexp-components)
  2967. (prompt "")
  2968. (string "") beg end move tag c s)
  2969. (if (org-region-active-p)
  2970. (setq beg (region-beginning) end (region-end)
  2971. string (buffer-substring beg end))
  2972. (setq move t))
  2973. (while (setq e (pop eal))
  2974. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2975. c (aref tag 0))
  2976. (push (cons c (string-to-char (car e))) det)
  2977. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2978. (substring tag 1)))))
  2979. (unless char
  2980. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2981. (setq char (read-char-exclusive)))
  2982. (setq char (or (cdr (assoc char det)) char))
  2983. (if (equal char ?\ )
  2984. (setq s "" move nil)
  2985. (unless (assoc (char-to-string char) org-emphasis-alist)
  2986. (error "No such emphasis marker: \"%c\"" char))
  2987. (setq s (char-to-string char)))
  2988. (while (and (> (length string) 1)
  2989. (equal (substring string 0 1) (substring string -1))
  2990. (assoc (substring string 0 1) org-emphasis-alist))
  2991. (setq string (substring string 1 -1)))
  2992. (setq string (concat s string s))
  2993. (if beg (delete-region beg end))
  2994. (unless (or (bolp)
  2995. (string-match (concat "[" (nth 0 erc) "\n]")
  2996. (char-to-string (char-before (point)))))
  2997. (insert " "))
  2998. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2999. (char-to-string (char-after (point))))
  3000. (insert " ") (backward-char 1))
  3001. (insert string)
  3002. (and move (backward-char 1))))
  3003. (defconst org-nonsticky-props
  3004. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3005. (defun org-activate-plain-links (limit)
  3006. "Run through the buffer and add overlays to links."
  3007. (catch 'exit
  3008. (let (f)
  3009. (while (re-search-forward org-plain-link-re limit t)
  3010. (setq f (get-text-property (match-beginning 0) 'face))
  3011. (if (or (eq f 'org-tag)
  3012. (and (listp f) (memq 'org-tag f)))
  3013. nil
  3014. (add-text-properties (match-beginning 0) (match-end 0)
  3015. (list 'mouse-face 'highlight
  3016. 'rear-nonsticky org-nonsticky-props
  3017. 'keymap org-mouse-map
  3018. ))
  3019. (throw 'exit t))))))
  3020. (defun org-activate-code (limit)
  3021. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3022. (unless (get-text-property (match-beginning 1) 'face)
  3023. (remove-text-properties (match-beginning 0) (match-end 0)
  3024. '(display t invisible t intangible t))
  3025. t)))
  3026. (defun org-activate-angle-links (limit)
  3027. "Run through the buffer and add overlays to links."
  3028. (if (re-search-forward org-angle-link-re limit t)
  3029. (progn
  3030. (add-text-properties (match-beginning 0) (match-end 0)
  3031. (list 'mouse-face 'highlight
  3032. 'rear-nonsticky org-nonsticky-props
  3033. 'keymap org-mouse-map
  3034. ))
  3035. t)))
  3036. (defun org-activate-bracket-links (limit)
  3037. "Run through the buffer and add overlays to bracketed links."
  3038. (if (re-search-forward org-bracket-link-regexp limit t)
  3039. (let* ((help (concat "LINK: "
  3040. (org-match-string-no-properties 1)))
  3041. ;; FIXME: above we should remove the escapes.
  3042. ;; but that requires another match, protecting match data,
  3043. ;; a lot of overhead for font-lock.
  3044. (ip (org-maybe-intangible
  3045. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3046. 'keymap org-mouse-map 'mouse-face 'highlight
  3047. 'font-lock-multiline t 'help-echo help)))
  3048. (vp (list 'rear-nonsticky org-nonsticky-props
  3049. 'keymap org-mouse-map 'mouse-face 'highlight
  3050. ' font-lock-multiline t 'help-echo help)))
  3051. ;; We need to remove the invisible property here. Table narrowing
  3052. ;; may have made some of this invisible.
  3053. (remove-text-properties (match-beginning 0) (match-end 0)
  3054. '(invisible nil))
  3055. (if (match-end 3)
  3056. (progn
  3057. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3058. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3059. (add-text-properties (match-end 3) (match-end 0) ip))
  3060. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3061. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3062. (add-text-properties (match-end 1) (match-end 0) ip))
  3063. t)))
  3064. (defun org-activate-dates (limit)
  3065. "Run through the buffer and add overlays to dates."
  3066. (if (re-search-forward org-tsr-regexp-both limit t)
  3067. (progn
  3068. (add-text-properties (match-beginning 0) (match-end 0)
  3069. (list 'mouse-face 'highlight
  3070. 'rear-nonsticky org-nonsticky-props
  3071. 'keymap org-mouse-map))
  3072. (when org-display-custom-times
  3073. (if (match-end 3)
  3074. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3075. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3076. t)))
  3077. (defvar org-target-link-regexp nil
  3078. "Regular expression matching radio targets in plain text.")
  3079. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3080. "Regular expression matching a link target.")
  3081. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3082. "Regular expression matching a radio target.")
  3083. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3084. "Regular expression matching any target.")
  3085. (defun org-activate-target-links (limit)
  3086. "Run through the buffer and add overlays to target matches."
  3087. (when org-target-link-regexp
  3088. (let ((case-fold-search t))
  3089. (if (re-search-forward org-target-link-regexp limit t)
  3090. (progn
  3091. (add-text-properties (match-beginning 0) (match-end 0)
  3092. (list 'mouse-face 'highlight
  3093. 'rear-nonsticky org-nonsticky-props
  3094. 'keymap org-mouse-map
  3095. 'help-echo "Radio target link"
  3096. 'org-linked-text t))
  3097. t)))))
  3098. (defun org-update-radio-target-regexp ()
  3099. "Find all radio targets in this file and update the regular expression."
  3100. (interactive)
  3101. (when (memq 'radio org-activate-links)
  3102. (setq org-target-link-regexp
  3103. (org-make-target-link-regexp (org-all-targets 'radio)))
  3104. (org-restart-font-lock)))
  3105. (defun org-hide-wide-columns (limit)
  3106. (let (s e)
  3107. (setq s (text-property-any (point) (or limit (point-max))
  3108. 'org-cwidth t))
  3109. (when s
  3110. (setq e (next-single-property-change s 'org-cwidth))
  3111. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3112. (goto-char e)
  3113. t)))
  3114. (defvar org-latex-and-specials-regexp nil
  3115. "Regular expression for highlighting export special stuff.")
  3116. (defvar org-match-substring-regexp)
  3117. (defvar org-match-substring-with-braces-regexp)
  3118. (defvar org-export-html-special-string-regexps)
  3119. (defun org-compute-latex-and-specials-regexp ()
  3120. "Compute regular expression for stuff treated specially by exporters."
  3121. (if (not org-highlight-latex-fragments-and-specials)
  3122. (org-set-local 'org-latex-and-specials-regexp nil)
  3123. (require 'org-exp)
  3124. (let*
  3125. ((matchers (plist-get org-format-latex-options :matchers))
  3126. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3127. org-latex-regexps)))
  3128. (options (org-combine-plists (org-default-export-plist)
  3129. (org-infile-export-plist)))
  3130. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3131. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3132. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3133. (org-export-html-expand (plist-get options :expand-quoted-html))
  3134. (org-export-with-special-strings (plist-get options :special-strings))
  3135. (re-sub
  3136. (cond
  3137. ((equal org-export-with-sub-superscripts '{})
  3138. (list org-match-substring-with-braces-regexp))
  3139. (org-export-with-sub-superscripts
  3140. (list org-match-substring-regexp))
  3141. (t nil)))
  3142. (re-latex
  3143. (if org-export-with-LaTeX-fragments
  3144. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3145. (re-macros
  3146. (if org-export-with-TeX-macros
  3147. (list (concat "\\\\"
  3148. (regexp-opt
  3149. (append (mapcar 'car org-html-entities)
  3150. (if (boundp 'org-latex-entities)
  3151. org-latex-entities nil))
  3152. 'words))) ; FIXME
  3153. ))
  3154. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3155. (re-special (if org-export-with-special-strings
  3156. (mapcar (lambda (x) (car x))
  3157. org-export-html-special-string-regexps)))
  3158. (re-rest
  3159. (delq nil
  3160. (list
  3161. (if org-export-html-expand "@<[^>\n]+>")
  3162. ))))
  3163. (org-set-local
  3164. 'org-latex-and-specials-regexp
  3165. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3166. re-rest) "\\|")))))
  3167. (defun org-do-latex-and-special-faces (limit)
  3168. "Run through the buffer and add overlays to links."
  3169. (when org-latex-and-specials-regexp
  3170. (let (rtn d)
  3171. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3172. limit t))
  3173. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3174. 'face))
  3175. '(org-code org-verbatim underline)))
  3176. (progn
  3177. (setq rtn t
  3178. d (cond ((member (char-after (1+ (match-beginning 0)))
  3179. '(?_ ?^)) 1)
  3180. (t 0)))
  3181. (font-lock-prepend-text-property
  3182. (+ d (match-beginning 0)) (match-end 0)
  3183. 'face 'org-latex-and-export-specials)
  3184. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3185. '(font-lock-multiline t)))))
  3186. rtn)))
  3187. (defun org-restart-font-lock ()
  3188. "Restart font-lock-mode, to force refontification."
  3189. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3190. (font-lock-mode -1)
  3191. (font-lock-mode 1)))
  3192. (defun org-all-targets (&optional radio)
  3193. "Return a list of all targets in this file.
  3194. With optional argument RADIO, only find radio targets."
  3195. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3196. rtn)
  3197. (save-excursion
  3198. (goto-char (point-min))
  3199. (while (re-search-forward re nil t)
  3200. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3201. rtn)))
  3202. (defun org-make-target-link-regexp (targets)
  3203. "Make regular expression matching all strings in TARGETS.
  3204. The regular expression finds the targets also if there is a line break
  3205. between words."
  3206. (and targets
  3207. (concat
  3208. "\\<\\("
  3209. (mapconcat
  3210. (lambda (x)
  3211. (while (string-match " +" x)
  3212. (setq x (replace-match "\\s-+" t t x)))
  3213. x)
  3214. targets
  3215. "\\|")
  3216. "\\)\\>")))
  3217. (defun org-activate-tags (limit)
  3218. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3219. (progn
  3220. (add-text-properties (match-beginning 1) (match-end 1)
  3221. (list 'mouse-face 'highlight
  3222. 'rear-nonsticky org-nonsticky-props
  3223. 'keymap org-mouse-map))
  3224. t)))
  3225. (defun org-outline-level ()
  3226. (save-excursion
  3227. (looking-at outline-regexp)
  3228. (if (match-beginning 1)
  3229. (+ (org-get-string-indentation (match-string 1)) 1000)
  3230. (1- (- (match-end 0) (match-beginning 0))))))
  3231. (defvar org-font-lock-keywords nil)
  3232. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3233. "Regular expression matching a property line.")
  3234. (defvar org-font-lock-hook nil
  3235. "Functions to be called for special font lock stuff.")
  3236. (defun org-font-lock-hook (limit)
  3237. (run-hook-with-args 'org-font-lock-hook limit))
  3238. (defun org-set-font-lock-defaults ()
  3239. (let* ((em org-fontify-emphasized-text)
  3240. (lk org-activate-links)
  3241. (org-font-lock-extra-keywords
  3242. (list
  3243. ;; Call the hook
  3244. '(org-font-lock-hook)
  3245. ;; Headlines
  3246. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3247. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3248. ;; Table lines
  3249. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3250. (1 'org-table t))
  3251. ;; Table internals
  3252. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3253. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3254. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3255. ;; Drawers
  3256. (list org-drawer-regexp '(0 'org-special-keyword t))
  3257. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3258. ;; Properties
  3259. (list org-property-re
  3260. '(1 'org-special-keyword t)
  3261. '(3 'org-property-value t))
  3262. (if org-format-transports-properties-p
  3263. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3264. ;; Links
  3265. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3266. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3267. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3268. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3269. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3270. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3271. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3272. '(org-hide-wide-columns (0 nil append))
  3273. ;; TODO lines
  3274. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3275. '(1 (org-get-todo-face 1) t))
  3276. ;; DONE
  3277. (if org-fontify-done-headline
  3278. (list (concat "^[*]+ +\\<\\("
  3279. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3280. "\\)\\(.*\\)")
  3281. '(2 'org-headline-done t))
  3282. nil)
  3283. ;; Priorities
  3284. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3285. ;; Special keywords
  3286. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3287. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3288. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3289. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3290. ;; Emphasis
  3291. (if em
  3292. (if (featurep 'xemacs)
  3293. '(org-do-emphasis-faces (0 nil append))
  3294. '(org-do-emphasis-faces)))
  3295. ;; Checkboxes
  3296. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3297. 2 'bold prepend)
  3298. (if org-provide-checkbox-statistics
  3299. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3300. (0 (org-get-checkbox-statistics-face) t)))
  3301. ;; Description list items
  3302. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3303. 2 'bold prepend)
  3304. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3305. '(1 'org-archived prepend))
  3306. ;; Specials
  3307. '(org-do-latex-and-special-faces)
  3308. ;; Code
  3309. '(org-activate-code (1 'org-code t))
  3310. ;; COMMENT
  3311. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3312. "\\|" org-quote-string "\\)\\>")
  3313. '(1 'org-special-keyword t))
  3314. '("^#.*" (0 'font-lock-comment-face t))
  3315. )))
  3316. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3317. ;; Now set the full font-lock-keywords
  3318. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3319. (org-set-local 'font-lock-defaults
  3320. '(org-font-lock-keywords t nil nil backward-paragraph))
  3321. (kill-local-variable 'font-lock-keywords) nil))
  3322. (defvar org-m nil)
  3323. (defvar org-l nil)
  3324. (defvar org-f nil)
  3325. (defun org-get-level-face (n)
  3326. "Get the right face for match N in font-lock matching of healdines."
  3327. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3328. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3329. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3330. (cond
  3331. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3332. ((eq n 2) org-f)
  3333. (t (if org-level-color-stars-only nil org-f))))
  3334. (defun org-get-todo-face (kwd)
  3335. "Get the right face for a TODO keyword KWD.
  3336. If KWD is a number, get the corresponding match group."
  3337. (if (numberp kwd) (setq kwd (match-string kwd)))
  3338. (or (cdr (assoc kwd org-todo-keyword-faces))
  3339. (and (member kwd org-done-keywords) 'org-done)
  3340. 'org-todo))
  3341. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3342. "Remove fontification and activation overlays from links."
  3343. (font-lock-default-unfontify-region beg end)
  3344. (let* ((buffer-undo-list t)
  3345. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3346. (inhibit-modification-hooks t)
  3347. deactivate-mark buffer-file-name buffer-file-truename)
  3348. (remove-text-properties beg end
  3349. '(mouse-face t keymap t org-linked-text t
  3350. invisible t intangible t))))
  3351. ;;;; Visibility cycling, including org-goto and indirect buffer
  3352. ;;; Cycling
  3353. (defvar org-cycle-global-status nil)
  3354. (make-variable-buffer-local 'org-cycle-global-status)
  3355. (defvar org-cycle-subtree-status nil)
  3356. (make-variable-buffer-local 'org-cycle-subtree-status)
  3357. ;;;###autoload
  3358. (defun org-cycle (&optional arg)
  3359. "Visibility cycling for Org-mode.
  3360. - When this function is called with a prefix argument, rotate the entire
  3361. buffer through 3 states (global cycling)
  3362. 1. OVERVIEW: Show only top-level headlines.
  3363. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3364. 3. SHOW ALL: Show everything.
  3365. When called with two C-c C-u prefixes, switch to the startup visibility,
  3366. determined by the variable `org-startup-folded', and by any VISIBILITY
  3367. properties in the buffer.
  3368. - When point is at the beginning of a headline, rotate the subtree started
  3369. by this line through 3 different states (local cycling)
  3370. 1. FOLDED: Only the main headline is shown.
  3371. 2. CHILDREN: The main headline and the direct children are shown.
  3372. From this state, you can move to one of the children
  3373. and zoom in further.
  3374. 3. SUBTREE: Show the entire subtree, including body text.
  3375. - When there is a numeric prefix, go up to a heading with level ARG, do
  3376. a `show-subtree' and return to the previous cursor position. If ARG
  3377. is negative, go up that many levels.
  3378. - When point is not at the beginning of a headline, execute the global
  3379. binding for TAB, which is re-indenting the line. See the option
  3380. `org-cycle-emulate-tab' for details.
  3381. - Special case: if point is at the beginning of the buffer and there is
  3382. no headline in line 1, this function will act as if called with prefix arg.
  3383. But only if also the variable `org-cycle-global-at-bob' is t."
  3384. (interactive "P")
  3385. (org-load-modules-maybe)
  3386. (let* ((outline-regexp
  3387. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3388. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3389. outline-regexp))
  3390. (bob-special (and org-cycle-global-at-bob (bobp)
  3391. (not (looking-at outline-regexp))))
  3392. (org-cycle-hook
  3393. (if bob-special
  3394. (delq 'org-optimize-window-after-visibility-change
  3395. (copy-sequence org-cycle-hook))
  3396. org-cycle-hook))
  3397. (pos (point)))
  3398. (if (or bob-special (equal arg '(4)))
  3399. ;; special case: use global cycling
  3400. (setq arg t))
  3401. (cond
  3402. ((equal arg '(16))
  3403. (org-set-startup-visibility)
  3404. (message "Startup visibility, plus VISIBILITY properties."))
  3405. ((org-at-table-p 'any)
  3406. ;; Enter the table or move to the next field in the table
  3407. (or (org-table-recognize-table.el)
  3408. (progn
  3409. (if arg (org-table-edit-field t)
  3410. (org-table-justify-field-maybe)
  3411. (call-interactively 'org-table-next-field)))))
  3412. ((eq arg t) ;; Global cycling
  3413. (cond
  3414. ((and (eq last-command this-command)
  3415. (eq org-cycle-global-status 'overview))
  3416. ;; We just created the overview - now do table of contents
  3417. ;; This can be slow in very large buffers, so indicate action
  3418. (message "CONTENTS...")
  3419. (org-content)
  3420. (message "CONTENTS...done")
  3421. (setq org-cycle-global-status 'contents)
  3422. (run-hook-with-args 'org-cycle-hook 'contents))
  3423. ((and (eq last-command this-command)
  3424. (eq org-cycle-global-status 'contents))
  3425. ;; We just showed the table of contents - now show everything
  3426. (show-all)
  3427. (message "SHOW ALL")
  3428. (setq org-cycle-global-status 'all)
  3429. (run-hook-with-args 'org-cycle-hook 'all))
  3430. (t
  3431. ;; Default action: go to overview
  3432. (org-overview)
  3433. (message "OVERVIEW")
  3434. (setq org-cycle-global-status 'overview)
  3435. (run-hook-with-args 'org-cycle-hook 'overview))))
  3436. ((and org-drawers org-drawer-regexp
  3437. (save-excursion
  3438. (beginning-of-line 1)
  3439. (looking-at org-drawer-regexp)))
  3440. ;; Toggle block visibility
  3441. (org-flag-drawer
  3442. (not (get-char-property (match-end 0) 'invisible))))
  3443. ((integerp arg)
  3444. ;; Show-subtree, ARG levels up from here.
  3445. (save-excursion
  3446. (org-back-to-heading)
  3447. (outline-up-heading (if (< arg 0) (- arg)
  3448. (- (funcall outline-level) arg)))
  3449. (org-show-subtree)))
  3450. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3451. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3452. ;; At a heading: rotate between three different views
  3453. (org-back-to-heading)
  3454. (let ((goal-column 0) eoh eol eos)
  3455. ;; First, some boundaries
  3456. (save-excursion
  3457. (org-back-to-heading)
  3458. (save-excursion
  3459. (beginning-of-line 2)
  3460. (while (and (not (eobp)) ;; this is like `next-line'
  3461. (get-char-property (1- (point)) 'invisible))
  3462. (beginning-of-line 2)) (setq eol (point)))
  3463. (outline-end-of-heading) (setq eoh (point))
  3464. (org-end-of-subtree t)
  3465. (unless (eobp)
  3466. (skip-chars-forward " \t\n")
  3467. (beginning-of-line 1) ; in case this is an item
  3468. )
  3469. (setq eos (1- (point))))
  3470. ;; Find out what to do next and set `this-command'
  3471. (cond
  3472. ((= eos eoh)
  3473. ;; Nothing is hidden behind this heading
  3474. (message "EMPTY ENTRY")
  3475. (setq org-cycle-subtree-status nil)
  3476. (save-excursion
  3477. (goto-char eos)
  3478. (outline-next-heading)
  3479. (if (org-invisible-p) (org-flag-heading nil))))
  3480. ((or (>= eol eos)
  3481. (not (string-match "\\S-" (buffer-substring eol eos))))
  3482. ;; Entire subtree is hidden in one line: open it
  3483. (org-show-entry)
  3484. (show-children)
  3485. (message "CHILDREN")
  3486. (save-excursion
  3487. (goto-char eos)
  3488. (outline-next-heading)
  3489. (if (org-invisible-p) (org-flag-heading nil)))
  3490. (setq org-cycle-subtree-status 'children)
  3491. (run-hook-with-args 'org-cycle-hook 'children))
  3492. ((and (eq last-command this-command)
  3493. (eq org-cycle-subtree-status 'children))
  3494. ;; We just showed the children, now show everything.
  3495. (org-show-subtree)
  3496. (message "SUBTREE")
  3497. (setq org-cycle-subtree-status 'subtree)
  3498. (run-hook-with-args 'org-cycle-hook 'subtree))
  3499. (t
  3500. ;; Default action: hide the subtree.
  3501. (hide-subtree)
  3502. (message "FOLDED")
  3503. (setq org-cycle-subtree-status 'folded)
  3504. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3505. ;; TAB emulation and template completion
  3506. (buffer-read-only (org-back-to-heading))
  3507. ((org-try-structure-completion))
  3508. ((org-try-cdlatex-tab))
  3509. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3510. (or (not (bolp))
  3511. (not (looking-at outline-regexp))))
  3512. (call-interactively (global-key-binding "\t")))
  3513. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3514. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3515. (or (and (eq org-cycle-emulate-tab 'white)
  3516. (= (match-end 0) (point-at-eol)))
  3517. (and (eq org-cycle-emulate-tab 'whitestart)
  3518. (>= (match-end 0) pos))))
  3519. t
  3520. (eq org-cycle-emulate-tab t))
  3521. (call-interactively (global-key-binding "\t")))
  3522. (t (save-excursion
  3523. (org-back-to-heading)
  3524. (org-cycle))))))
  3525. ;;;###autoload
  3526. (defun org-global-cycle (&optional arg)
  3527. "Cycle the global visibility. For details see `org-cycle'.
  3528. With C-u prefix arg, switch to startup visibility.
  3529. With a numeric prefix, show all headlines up to that level."
  3530. (interactive "P")
  3531. (let ((org-cycle-include-plain-lists
  3532. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3533. (cond
  3534. ((integerp arg)
  3535. (show-all)
  3536. (hide-sublevels arg)
  3537. (setq org-cycle-global-status 'contents))
  3538. ((equal arg '(4))
  3539. (org-set-startup-visibility)
  3540. (message "Startup visibility, plus VISIBILITY properties."))
  3541. (t
  3542. (org-cycle '(4))))))
  3543. (defun org-set-startup-visibility ()
  3544. "Set the visibility required by startup options and properties."
  3545. (cond
  3546. ((eq org-startup-folded t)
  3547. (org-cycle '(4)))
  3548. ((eq org-startup-folded 'content)
  3549. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3550. (org-cycle '(4)) (org-cycle '(4)))))
  3551. (org-set-visibility-according-to-property 'no-cleanup)
  3552. (org-cycle-hide-archived-subtrees 'all)
  3553. (org-cycle-hide-drawers 'all)
  3554. (org-cycle-show-empty-lines 'all))
  3555. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3556. "Switch subtree visibilities according to :VISIBILITY: property."
  3557. (interactive)
  3558. (let (state)
  3559. (save-excursion
  3560. (goto-char (point-min))
  3561. (while (re-search-forward
  3562. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3563. nil t)
  3564. (setq state (match-string 1))
  3565. (save-excursion
  3566. (org-back-to-heading t)
  3567. (hide-subtree)
  3568. (org-reveal)
  3569. (cond
  3570. ((equal state '("fold" "folded"))
  3571. (hide-subtree))
  3572. ((equal state "children")
  3573. (org-show-hidden-entry)
  3574. (show-children))
  3575. ((equal state "content")
  3576. (save-excursion
  3577. (save-restriction
  3578. (org-narrow-to-subtree)
  3579. (org-content))))
  3580. ((member state '("all" "showall"))
  3581. (show-subtree)))))
  3582. (unless no-cleanup
  3583. (org-cycle-hide-archived-subtrees 'all)
  3584. (org-cycle-hide-drawers 'all)
  3585. (org-cycle-show-empty-lines 'all)))))
  3586. (defun org-overview ()
  3587. "Switch to overview mode, shoing only top-level headlines.
  3588. Really, this shows all headlines with level equal or greater than the level
  3589. of the first headline in the buffer. This is important, because if the
  3590. first headline is not level one, then (hide-sublevels 1) gives confusing
  3591. results."
  3592. (interactive)
  3593. (let ((level (save-excursion
  3594. (goto-char (point-min))
  3595. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3596. (progn
  3597. (goto-char (match-beginning 0))
  3598. (funcall outline-level))))))
  3599. (and level (hide-sublevels level))))
  3600. (defun org-content (&optional arg)
  3601. "Show all headlines in the buffer, like a table of contents.
  3602. With numerical argument N, show content up to level N."
  3603. (interactive "P")
  3604. (save-excursion
  3605. ;; Visit all headings and show their offspring
  3606. (and (integerp arg) (org-overview))
  3607. (goto-char (point-max))
  3608. (catch 'exit
  3609. (while (and (progn (condition-case nil
  3610. (outline-previous-visible-heading 1)
  3611. (error (goto-char (point-min))))
  3612. t)
  3613. (looking-at outline-regexp))
  3614. (if (integerp arg)
  3615. (show-children (1- arg))
  3616. (show-branches))
  3617. (if (bobp) (throw 'exit nil))))))
  3618. (defun org-optimize-window-after-visibility-change (state)
  3619. "Adjust the window after a change in outline visibility.
  3620. This function is the default value of the hook `org-cycle-hook'."
  3621. (when (get-buffer-window (current-buffer))
  3622. (cond
  3623. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3624. ; ((eq state 'overview) (org-beginning-of-line))
  3625. ((eq state 'content) nil)
  3626. ((eq state 'all) nil)
  3627. ((eq state 'folded) nil)
  3628. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3629. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3630. (defun org-compact-display-after-subtree-move ()
  3631. (let (beg end)
  3632. (save-excursion
  3633. (if (org-up-heading-safe)
  3634. (progn
  3635. (hide-subtree)
  3636. (show-entry)
  3637. (show-children)
  3638. (org-cycle-show-empty-lines 'children)
  3639. (org-cycle-hide-drawers 'children))
  3640. (org-overview)))))
  3641. (defun org-cycle-show-empty-lines (state)
  3642. "Show empty lines above all visible headlines.
  3643. The region to be covered depends on STATE when called through
  3644. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3645. entire buffer covered. Note that an empty line is only shown if there
  3646. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3647. (when (> org-cycle-separator-lines 0)
  3648. (save-excursion
  3649. (let* ((n org-cycle-separator-lines)
  3650. (re (cond
  3651. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3652. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3653. (t (let ((ns (number-to-string (- n 2))))
  3654. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3655. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3656. beg end)
  3657. (cond
  3658. ((memq state '(overview contents t))
  3659. (setq beg (point-min) end (point-max)))
  3660. ((memq state '(children folded))
  3661. (setq beg (point) end (progn (org-end-of-subtree t t)
  3662. (beginning-of-line 2)
  3663. (point)))))
  3664. (when beg
  3665. (goto-char beg)
  3666. (while (re-search-forward re end t)
  3667. (if (not (get-char-property (match-end 1) 'invisible))
  3668. (outline-flag-region
  3669. (match-beginning 1) (match-end 1) nil)))))))
  3670. ;; Never hide empty lines at the end of the file.
  3671. (save-excursion
  3672. (goto-char (point-max))
  3673. (outline-previous-heading)
  3674. (outline-end-of-heading)
  3675. (if (and (looking-at "[ \t\n]+")
  3676. (= (match-end 0) (point-max)))
  3677. (outline-flag-region (point) (match-end 0) nil))))
  3678. (defun org-cycle-hide-drawers (state)
  3679. "Re-hide all drawers after a visibility state change."
  3680. (when (and (org-mode-p)
  3681. (not (memq state '(overview folded))))
  3682. (save-excursion
  3683. (let* ((globalp (memq state '(contents all)))
  3684. (beg (if globalp (point-min) (point)))
  3685. (end (if globalp (point-max) (org-end-of-subtree t))))
  3686. (goto-char beg)
  3687. (while (re-search-forward org-drawer-regexp end t)
  3688. (org-flag-drawer t))))))
  3689. (defun org-flag-drawer (flag)
  3690. (save-excursion
  3691. (beginning-of-line 1)
  3692. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3693. (let ((b (match-end 0))
  3694. (outline-regexp org-outline-regexp))
  3695. (if (re-search-forward
  3696. "^[ \t]*:END:"
  3697. (save-excursion (outline-next-heading) (point)) t)
  3698. (outline-flag-region b (point-at-eol) flag)
  3699. (error ":END: line missing"))))))
  3700. (defun org-subtree-end-visible-p ()
  3701. "Is the end of the current subtree visible?"
  3702. (pos-visible-in-window-p
  3703. (save-excursion (org-end-of-subtree t) (point))))
  3704. (defun org-first-headline-recenter (&optional N)
  3705. "Move cursor to the first headline and recenter the headline.
  3706. Optional argument N means, put the headline into the Nth line of the window."
  3707. (goto-char (point-min))
  3708. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3709. (beginning-of-line)
  3710. (recenter (prefix-numeric-value N))))
  3711. ;;; Org-goto
  3712. (defvar org-goto-window-configuration nil)
  3713. (defvar org-goto-marker nil)
  3714. (defvar org-goto-map
  3715. (let ((map (make-sparse-keymap)))
  3716. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3717. (while (setq cmd (pop cmds))
  3718. (substitute-key-definition cmd cmd map global-map)))
  3719. (suppress-keymap map)
  3720. (org-defkey map "\C-m" 'org-goto-ret)
  3721. (org-defkey map [(return)] 'org-goto-ret)
  3722. (org-defkey map [(left)] 'org-goto-left)
  3723. (org-defkey map [(right)] 'org-goto-right)
  3724. (org-defkey map [(control ?g)] 'org-goto-quit)
  3725. (org-defkey map "\C-i" 'org-cycle)
  3726. (org-defkey map [(tab)] 'org-cycle)
  3727. (org-defkey map [(down)] 'outline-next-visible-heading)
  3728. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3729. (if org-goto-auto-isearch
  3730. (if (fboundp 'define-key-after)
  3731. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3732. nil)
  3733. (org-defkey map "q" 'org-goto-quit)
  3734. (org-defkey map "n" 'outline-next-visible-heading)
  3735. (org-defkey map "p" 'outline-previous-visible-heading)
  3736. (org-defkey map "f" 'outline-forward-same-level)
  3737. (org-defkey map "b" 'outline-backward-same-level)
  3738. (org-defkey map "u" 'outline-up-heading))
  3739. (org-defkey map "/" 'org-occur)
  3740. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3741. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3742. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3743. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3744. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3745. map))
  3746. (defconst org-goto-help
  3747. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3748. RET=jump to location [Q]uit and return to previous location
  3749. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3750. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3751. (defun org-goto (&optional alternative-interface)
  3752. "Look up a different location in the current file, keeping current visibility.
  3753. When you want look-up or go to a different location in a document, the
  3754. fastest way is often to fold the entire buffer and then dive into the tree.
  3755. This method has the disadvantage, that the previous location will be folded,
  3756. which may not be what you want.
  3757. This command works around this by showing a copy of the current buffer
  3758. in an indirect buffer, in overview mode. You can dive into the tree in
  3759. that copy, use org-occur and incremental search to find a location.
  3760. When pressing RET or `Q', the command returns to the original buffer in
  3761. which the visibility is still unchanged. After RET is will also jump to
  3762. the location selected in the indirect buffer and expose the
  3763. the headline hierarchy above."
  3764. (interactive "P")
  3765. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3766. (org-refile-use-outline-path t)
  3767. (interface
  3768. (if (not alternative-interface)
  3769. org-goto-interface
  3770. (if (eq org-goto-interface 'outline)
  3771. 'outline-path-completion
  3772. 'outline)))
  3773. (org-goto-start-pos (point))
  3774. (selected-point
  3775. (if (eq interface 'outline)
  3776. (car (org-get-location (current-buffer) org-goto-help))
  3777. (nth 3 (org-refile-get-location "Goto: ")))))
  3778. (if selected-point
  3779. (progn
  3780. (org-mark-ring-push org-goto-start-pos)
  3781. (goto-char selected-point)
  3782. (if (or (org-invisible-p) (org-invisible-p2))
  3783. (org-show-context 'org-goto)))
  3784. (message "Quit"))))
  3785. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3786. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3787. (defvar org-goto-local-auto-isearch-map) ; defined below
  3788. (defun org-get-location (buf help)
  3789. "Let the user select a location in the Org-mode buffer BUF.
  3790. This function uses a recursive edit. It returns the selected position
  3791. or nil."
  3792. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3793. (isearch-hide-immediately nil)
  3794. (isearch-search-fun-function
  3795. (lambda () 'org-goto-local-search-forward-headings))
  3796. (org-goto-selected-point org-goto-exit-command))
  3797. (save-excursion
  3798. (save-window-excursion
  3799. (delete-other-windows)
  3800. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3801. (switch-to-buffer
  3802. (condition-case nil
  3803. (make-indirect-buffer (current-buffer) "*org-goto*")
  3804. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3805. (with-output-to-temp-buffer "*Help*"
  3806. (princ help))
  3807. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3808. (setq buffer-read-only nil)
  3809. (let ((org-startup-truncated t)
  3810. (org-startup-folded nil)
  3811. (org-startup-align-all-tables nil))
  3812. (org-mode)
  3813. (org-overview))
  3814. (setq buffer-read-only t)
  3815. (if (and (boundp 'org-goto-start-pos)
  3816. (integer-or-marker-p org-goto-start-pos))
  3817. (let ((org-show-hierarchy-above t)
  3818. (org-show-siblings t)
  3819. (org-show-following-heading t))
  3820. (goto-char org-goto-start-pos)
  3821. (and (org-invisible-p) (org-show-context)))
  3822. (goto-char (point-min)))
  3823. (org-beginning-of-line)
  3824. (message "Select location and press RET")
  3825. (use-local-map org-goto-map)
  3826. (recursive-edit)
  3827. ))
  3828. (kill-buffer "*org-goto*")
  3829. (cons org-goto-selected-point org-goto-exit-command)))
  3830. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3831. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3832. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3833. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3834. (defun org-goto-local-search-forward-headings (string bound noerror)
  3835. "Search and make sure that anu matches are in headlines."
  3836. (catch 'return
  3837. (while (search-forward string bound noerror)
  3838. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3839. (and (member :headline context)
  3840. (not (member :tags context))))
  3841. (throw 'return (point))))))
  3842. (defun org-goto-local-auto-isearch ()
  3843. "Start isearch."
  3844. (interactive)
  3845. (goto-char (point-min))
  3846. (let ((keys (this-command-keys)))
  3847. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3848. (isearch-mode t)
  3849. (isearch-process-search-char (string-to-char keys)))))
  3850. (defun org-goto-ret (&optional arg)
  3851. "Finish `org-goto' by going to the new location."
  3852. (interactive "P")
  3853. (setq org-goto-selected-point (point)
  3854. org-goto-exit-command 'return)
  3855. (throw 'exit nil))
  3856. (defun org-goto-left ()
  3857. "Finish `org-goto' by going to the new location."
  3858. (interactive)
  3859. (if (org-on-heading-p)
  3860. (progn
  3861. (beginning-of-line 1)
  3862. (setq org-goto-selected-point (point)
  3863. org-goto-exit-command 'left)
  3864. (throw 'exit nil))
  3865. (error "Not on a heading")))
  3866. (defun org-goto-right ()
  3867. "Finish `org-goto' by going to the new location."
  3868. (interactive)
  3869. (if (org-on-heading-p)
  3870. (progn
  3871. (setq org-goto-selected-point (point)
  3872. org-goto-exit-command 'right)
  3873. (throw 'exit nil))
  3874. (error "Not on a heading")))
  3875. (defun org-goto-quit ()
  3876. "Finish `org-goto' without cursor motion."
  3877. (interactive)
  3878. (setq org-goto-selected-point nil)
  3879. (setq org-goto-exit-command 'quit)
  3880. (throw 'exit nil))
  3881. ;;; Indirect buffer display of subtrees
  3882. (defvar org-indirect-dedicated-frame nil
  3883. "This is the frame being used for indirect tree display.")
  3884. (defvar org-last-indirect-buffer nil)
  3885. (defun org-tree-to-indirect-buffer (&optional arg)
  3886. "Create indirect buffer and narrow it to current subtree.
  3887. With numerical prefix ARG, go up to this level and then take that tree.
  3888. If ARG is negative, go up that many levels.
  3889. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3890. indirect buffer previously made with this command, to avoid proliferation of
  3891. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3892. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3893. is kept so that you can work with several indirect buffers at the same time.
  3894. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3895. requests that a new frame be made for the new buffer, so that the dedicated
  3896. frame is not changed."
  3897. (interactive "P")
  3898. (let ((cbuf (current-buffer))
  3899. (cwin (selected-window))
  3900. (pos (point))
  3901. beg end level heading ibuf)
  3902. (save-excursion
  3903. (org-back-to-heading t)
  3904. (when (numberp arg)
  3905. (setq level (org-outline-level))
  3906. (if (< arg 0) (setq arg (+ level arg)))
  3907. (while (> (setq level (org-outline-level)) arg)
  3908. (outline-up-heading 1 t)))
  3909. (setq beg (point)
  3910. heading (org-get-heading))
  3911. (org-end-of-subtree t) (setq end (point)))
  3912. (if (and (buffer-live-p org-last-indirect-buffer)
  3913. (not (eq org-indirect-buffer-display 'new-frame))
  3914. (not arg))
  3915. (kill-buffer org-last-indirect-buffer))
  3916. (setq ibuf (org-get-indirect-buffer cbuf)
  3917. org-last-indirect-buffer ibuf)
  3918. (cond
  3919. ((or (eq org-indirect-buffer-display 'new-frame)
  3920. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3921. (select-frame (make-frame))
  3922. (delete-other-windows)
  3923. (switch-to-buffer ibuf)
  3924. (org-set-frame-title heading))
  3925. ((eq org-indirect-buffer-display 'dedicated-frame)
  3926. (raise-frame
  3927. (select-frame (or (and org-indirect-dedicated-frame
  3928. (frame-live-p org-indirect-dedicated-frame)
  3929. org-indirect-dedicated-frame)
  3930. (setq org-indirect-dedicated-frame (make-frame)))))
  3931. (delete-other-windows)
  3932. (switch-to-buffer ibuf)
  3933. (org-set-frame-title (concat "Indirect: " heading)))
  3934. ((eq org-indirect-buffer-display 'current-window)
  3935. (switch-to-buffer ibuf))
  3936. ((eq org-indirect-buffer-display 'other-window)
  3937. (pop-to-buffer ibuf))
  3938. (t (error "Invalid value.")))
  3939. (if (featurep 'xemacs)
  3940. (save-excursion (org-mode) (turn-on-font-lock)))
  3941. (narrow-to-region beg end)
  3942. (show-all)
  3943. (goto-char pos)
  3944. (and (window-live-p cwin) (select-window cwin))))
  3945. (defun org-get-indirect-buffer (&optional buffer)
  3946. (setq buffer (or buffer (current-buffer)))
  3947. (let ((n 1) (base (buffer-name buffer)) bname)
  3948. (while (buffer-live-p
  3949. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3950. (setq n (1+ n)))
  3951. (condition-case nil
  3952. (make-indirect-buffer buffer bname 'clone)
  3953. (error (make-indirect-buffer buffer bname)))))
  3954. (defun org-set-frame-title (title)
  3955. "Set the title of the current frame to the string TITLE."
  3956. ;; FIXME: how to name a single frame in XEmacs???
  3957. (unless (featurep 'xemacs)
  3958. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3959. ;;;; Structure editing
  3960. ;;; Inserting headlines
  3961. (defun org-insert-heading (&optional force-heading)
  3962. "Insert a new heading or item with same depth at point.
  3963. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3964. If point is at the beginning of a headline, insert a sibling before the
  3965. current headline. If point is not at the beginning, do not split the line,
  3966. but create the new hedline after the current line."
  3967. (interactive "P")
  3968. (if (= (buffer-size) 0)
  3969. (insert "\n* ")
  3970. (when (or force-heading (not (org-insert-item)))
  3971. (let* ((head (save-excursion
  3972. (condition-case nil
  3973. (progn
  3974. (org-back-to-heading)
  3975. (match-string 0))
  3976. (error "*"))))
  3977. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3978. pos)
  3979. (cond
  3980. ((and (org-on-heading-p) (bolp)
  3981. (or (bobp)
  3982. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3983. ;; insert before the current line
  3984. (open-line (if blank 2 1)))
  3985. ((and (bolp)
  3986. (or (bobp)
  3987. (save-excursion
  3988. (backward-char 1) (not (org-invisible-p)))))
  3989. ;; insert right here
  3990. nil)
  3991. (t
  3992. ;; in the middle of the line
  3993. (org-show-entry)
  3994. (let ((split
  3995. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3996. tags pos)
  3997. (if (org-on-heading-p)
  3998. (progn
  3999. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4000. (setq tags (and (match-end 2) (match-string 2)))
  4001. (and (match-end 1)
  4002. (delete-region (match-beginning 1) (match-end 1)))
  4003. (setq pos (point-at-bol))
  4004. (or split (end-of-line 1))
  4005. (delete-horizontal-space)
  4006. (newline (if blank 2 1))
  4007. (when tags
  4008. (save-excursion
  4009. (goto-char pos)
  4010. (end-of-line 1)
  4011. (insert " " tags)
  4012. (org-set-tags nil 'align))))
  4013. (or split (end-of-line 1))
  4014. (newline (if blank 2 1))))))
  4015. (insert head) (just-one-space)
  4016. (setq pos (point))
  4017. (end-of-line 1)
  4018. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4019. (run-hooks 'org-insert-heading-hook)))))
  4020. (defun org-get-heading (&optional no-tags)
  4021. "Return the heading of the current entry, without the stars."
  4022. (save-excursion
  4023. (org-back-to-heading t)
  4024. (if (looking-at
  4025. (if no-tags
  4026. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4027. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4028. (match-string 1) "")))
  4029. (defun org-insert-heading-after-current ()
  4030. "Insert a new heading with same level as current, after current subtree."
  4031. (interactive)
  4032. (org-back-to-heading)
  4033. (org-insert-heading)
  4034. (org-move-subtree-down)
  4035. (end-of-line 1))
  4036. (defun org-insert-todo-heading (arg)
  4037. "Insert a new heading with the same level and TODO state as current heading.
  4038. If the heading has no TODO state, or if the state is DONE, use the first
  4039. state (TODO by default). Also with prefix arg, force first state."
  4040. (interactive "P")
  4041. (when (not (org-insert-item 'checkbox))
  4042. (org-insert-heading)
  4043. (save-excursion
  4044. (org-back-to-heading)
  4045. (outline-previous-heading)
  4046. (looking-at org-todo-line-regexp))
  4047. (if (or arg
  4048. (not (match-beginning 2))
  4049. (member (match-string 2) org-done-keywords))
  4050. (insert (car org-todo-keywords-1) " ")
  4051. (insert (match-string 2) " "))))
  4052. (defun org-insert-subheading (arg)
  4053. "Insert a new subheading and demote it.
  4054. Works for outline headings and for plain lists alike."
  4055. (interactive "P")
  4056. (org-insert-heading arg)
  4057. (cond
  4058. ((org-on-heading-p) (org-do-demote))
  4059. ((org-at-item-p) (org-indent-item 1))))
  4060. (defun org-insert-todo-subheading (arg)
  4061. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4062. Works for outline headings and for plain lists alike."
  4063. (interactive "P")
  4064. (org-insert-todo-heading arg)
  4065. (cond
  4066. ((org-on-heading-p) (org-do-demote))
  4067. ((org-at-item-p) (org-indent-item 1))))
  4068. ;;; Promotion and Demotion
  4069. (defun org-promote-subtree ()
  4070. "Promote the entire subtree.
  4071. See also `org-promote'."
  4072. (interactive)
  4073. (save-excursion
  4074. (org-map-tree 'org-promote))
  4075. (org-fix-position-after-promote))
  4076. (defun org-demote-subtree ()
  4077. "Demote the entire subtree. See `org-demote'.
  4078. See also `org-promote'."
  4079. (interactive)
  4080. (save-excursion
  4081. (org-map-tree 'org-demote))
  4082. (org-fix-position-after-promote))
  4083. (defun org-do-promote ()
  4084. "Promote the current heading higher up the tree.
  4085. If the region is active in `transient-mark-mode', promote all headings
  4086. in the region."
  4087. (interactive)
  4088. (save-excursion
  4089. (if (org-region-active-p)
  4090. (org-map-region 'org-promote (region-beginning) (region-end))
  4091. (org-promote)))
  4092. (org-fix-position-after-promote))
  4093. (defun org-do-demote ()
  4094. "Demote the current heading lower down the tree.
  4095. If the region is active in `transient-mark-mode', demote all headings
  4096. in the region."
  4097. (interactive)
  4098. (save-excursion
  4099. (if (org-region-active-p)
  4100. (org-map-region 'org-demote (region-beginning) (region-end))
  4101. (org-demote)))
  4102. (org-fix-position-after-promote))
  4103. (defun org-fix-position-after-promote ()
  4104. "Make sure that after pro/demotion cursor position is right."
  4105. (let ((pos (point)))
  4106. (when (save-excursion
  4107. (beginning-of-line 1)
  4108. (looking-at org-todo-line-regexp)
  4109. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4110. (cond ((eobp) (insert " "))
  4111. ((eolp) (insert " "))
  4112. ((equal (char-after) ?\ ) (forward-char 1))))))
  4113. (defun org-reduced-level (l)
  4114. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4115. (defun org-get-valid-level (level &optional change)
  4116. "Rectify a level change under the influence of `org-odd-levels-only'
  4117. LEVEL is a current level, CHANGE is by how much the level should be
  4118. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4119. even level numbers will become the next higher odd number."
  4120. (if org-odd-levels-only
  4121. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4122. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4123. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4124. (max 1 (+ level change))))
  4125. (if (boundp 'define-obsolete-function-alias)
  4126. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4127. (define-obsolete-function-alias 'org-get-legal-level
  4128. 'org-get-valid-level)
  4129. (define-obsolete-function-alias 'org-get-legal-level
  4130. 'org-get-valid-level "23.1")))
  4131. (defun org-promote ()
  4132. "Promote the current heading higher up the tree.
  4133. If the region is active in `transient-mark-mode', promote all headings
  4134. in the region."
  4135. (org-back-to-heading t)
  4136. (let* ((level (save-match-data (funcall outline-level)))
  4137. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4138. (diff (abs (- level (length up-head) -1))))
  4139. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4140. (replace-match up-head nil t)
  4141. ;; Fixup tag positioning
  4142. (and org-auto-align-tags (org-set-tags nil t))
  4143. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4144. (defun org-demote ()
  4145. "Demote the current heading lower down the tree.
  4146. If the region is active in `transient-mark-mode', demote all headings
  4147. in the region."
  4148. (org-back-to-heading t)
  4149. (let* ((level (save-match-data (funcall outline-level)))
  4150. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4151. (diff (abs (- level (length down-head) -1))))
  4152. (replace-match down-head nil t)
  4153. ;; Fixup tag positioning
  4154. (and org-auto-align-tags (org-set-tags nil t))
  4155. (if org-adapt-indentation (org-fixup-indentation diff))))
  4156. (defun org-map-tree (fun)
  4157. "Call FUN for every heading underneath the current one."
  4158. (org-back-to-heading)
  4159. (let ((level (funcall outline-level)))
  4160. (save-excursion
  4161. (funcall fun)
  4162. (while (and (progn
  4163. (outline-next-heading)
  4164. (> (funcall outline-level) level))
  4165. (not (eobp)))
  4166. (funcall fun)))))
  4167. (defun org-map-region (fun beg end)
  4168. "Call FUN for every heading between BEG and END."
  4169. (let ((org-ignore-region t))
  4170. (save-excursion
  4171. (setq end (copy-marker end))
  4172. (goto-char beg)
  4173. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4174. (< (point) end))
  4175. (funcall fun))
  4176. (while (and (progn
  4177. (outline-next-heading)
  4178. (< (point) end))
  4179. (not (eobp)))
  4180. (funcall fun)))))
  4181. (defun org-fixup-indentation (diff)
  4182. "Change the indentation in the current entry by DIFF
  4183. However, if any line in the current entry has no indentation, or if it
  4184. would end up with no indentation after the change, nothing at all is done."
  4185. (save-excursion
  4186. (let ((end (save-excursion (outline-next-heading)
  4187. (point-marker)))
  4188. (prohibit (if (> diff 0)
  4189. "^\\S-"
  4190. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4191. col)
  4192. (unless (save-excursion (end-of-line 1)
  4193. (re-search-forward prohibit end t))
  4194. (while (and (< (point) end)
  4195. (re-search-forward "^[ \t]+" end t))
  4196. (goto-char (match-end 0))
  4197. (setq col (current-column))
  4198. (if (< diff 0) (replace-match ""))
  4199. (indent-to (+ diff col))))
  4200. (move-marker end nil))))
  4201. (defun org-convert-to-odd-levels ()
  4202. "Convert an org-mode file with all levels allowed to one with odd levels.
  4203. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4204. level 5 etc."
  4205. (interactive)
  4206. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4207. (let ((org-odd-levels-only nil) n)
  4208. (save-excursion
  4209. (goto-char (point-min))
  4210. (while (re-search-forward "^\\*\\*+ " nil t)
  4211. (setq n (- (length (match-string 0)) 2))
  4212. (while (>= (setq n (1- n)) 0)
  4213. (org-demote))
  4214. (end-of-line 1))))))
  4215. (defun org-convert-to-oddeven-levels ()
  4216. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4217. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4218. section with an even level, conversion would destroy the structure of the file. An error
  4219. is signaled in this case."
  4220. (interactive)
  4221. (goto-char (point-min))
  4222. ;; First check if there are no even levels
  4223. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4224. (org-show-context t)
  4225. (error "Not all levels are odd in this file. Conversion not possible."))
  4226. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4227. (let ((org-odd-levels-only nil) n)
  4228. (save-excursion
  4229. (goto-char (point-min))
  4230. (while (re-search-forward "^\\*\\*+ " nil t)
  4231. (setq n (/ (1- (length (match-string 0))) 2))
  4232. (while (>= (setq n (1- n)) 0)
  4233. (org-promote))
  4234. (end-of-line 1))))))
  4235. (defun org-tr-level (n)
  4236. "Make N odd if required."
  4237. (if org-odd-levels-only (1+ (/ n 2)) n))
  4238. ;;; Vertical tree motion, cutting and pasting of subtrees
  4239. (defun org-move-subtree-up (&optional arg)
  4240. "Move the current subtree up past ARG headlines of the same level."
  4241. (interactive "p")
  4242. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4243. (defun org-move-subtree-down (&optional arg)
  4244. "Move the current subtree down past ARG headlines of the same level."
  4245. (interactive "p")
  4246. (setq arg (prefix-numeric-value arg))
  4247. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4248. 'outline-get-last-sibling))
  4249. (ins-point (make-marker))
  4250. (cnt (abs arg))
  4251. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4252. ;; Select the tree
  4253. (org-back-to-heading)
  4254. (setq beg0 (point))
  4255. (save-excursion
  4256. (setq ne-beg (org-back-over-empty-lines))
  4257. (setq beg (point)))
  4258. (save-match-data
  4259. (save-excursion (outline-end-of-heading)
  4260. (setq folded (org-invisible-p)))
  4261. (outline-end-of-subtree))
  4262. (outline-next-heading)
  4263. (setq ne-end (org-back-over-empty-lines))
  4264. (setq end (point))
  4265. (goto-char beg0)
  4266. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4267. ;; include less whitespace
  4268. (save-excursion
  4269. (goto-char beg)
  4270. (forward-line (- ne-beg ne-end))
  4271. (setq beg (point))))
  4272. ;; Find insertion point, with error handling
  4273. (while (> cnt 0)
  4274. (or (and (funcall movfunc) (looking-at outline-regexp))
  4275. (progn (goto-char beg0)
  4276. (error "Cannot move past superior level or buffer limit")))
  4277. (setq cnt (1- cnt)))
  4278. (if (> arg 0)
  4279. ;; Moving forward - still need to move over subtree
  4280. (progn (org-end-of-subtree t t)
  4281. (save-excursion
  4282. (org-back-over-empty-lines)
  4283. (or (bolp) (newline)))))
  4284. (setq ne-ins (org-back-over-empty-lines))
  4285. (move-marker ins-point (point))
  4286. (setq txt (buffer-substring beg end))
  4287. (org-save-markers-in-region beg end)
  4288. (delete-region beg end)
  4289. (outline-flag-region (1- beg) beg nil)
  4290. (outline-flag-region (1- (point)) (point) nil)
  4291. (let ((bbb (point)))
  4292. (insert-before-markers txt)
  4293. (org-reinstall-markers-in-region bbb)
  4294. (move-marker ins-point bbb))
  4295. (or (bolp) (insert "\n"))
  4296. (setq ins-end (point))
  4297. (goto-char ins-point)
  4298. (org-skip-whitespace)
  4299. (when (and (< arg 0)
  4300. (org-first-sibling-p)
  4301. (> ne-ins ne-beg))
  4302. ;; Move whitespace back to beginning
  4303. (save-excursion
  4304. (goto-char ins-end)
  4305. (let ((kill-whole-line t))
  4306. (kill-line (- ne-ins ne-beg)) (point)))
  4307. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4308. (move-marker ins-point nil)
  4309. (org-compact-display-after-subtree-move)
  4310. (unless folded
  4311. (org-show-entry)
  4312. (show-children)
  4313. (org-cycle-hide-drawers 'children))))
  4314. (defvar org-subtree-clip ""
  4315. "Clipboard for cut and paste of subtrees.
  4316. This is actually only a copy of the kill, because we use the normal kill
  4317. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4318. (defvar org-subtree-clip-folded nil
  4319. "Was the last copied subtree folded?
  4320. This is used to fold the tree back after pasting.")
  4321. (defun org-cut-subtree (&optional n)
  4322. "Cut the current subtree into the clipboard.
  4323. With prefix arg N, cut this many sequential subtrees.
  4324. This is a short-hand for marking the subtree and then cutting it."
  4325. (interactive "p")
  4326. (org-copy-subtree n 'cut))
  4327. (defun org-copy-subtree (&optional n cut force-store-markers)
  4328. "Cut the current subtree into the clipboard.
  4329. With prefix arg N, cut this many sequential subtrees.
  4330. This is a short-hand for marking the subtree and then copying it.
  4331. If CUT is non-nil, actually cut the subtree.
  4332. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4333. of some markers in the region, even if CUT is non-nil. This is
  4334. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4335. (interactive "p")
  4336. (let (beg end folded (beg0 (point)))
  4337. (if (interactive-p)
  4338. (org-back-to-heading nil) ; take what looks like a subtree
  4339. (org-back-to-heading t)) ; take what is really there
  4340. (org-back-over-empty-lines)
  4341. (setq beg (point))
  4342. (skip-chars-forward " \t\r\n")
  4343. (save-match-data
  4344. (save-excursion (outline-end-of-heading)
  4345. (setq folded (org-invisible-p)))
  4346. (condition-case nil
  4347. (outline-forward-same-level (1- n))
  4348. (error nil))
  4349. (org-end-of-subtree t t))
  4350. (org-back-over-empty-lines)
  4351. (setq end (point))
  4352. (goto-char beg0)
  4353. (when (> end beg)
  4354. (setq org-subtree-clip-folded folded)
  4355. (when (or cut force-store-markers)
  4356. (org-save-markers-in-region beg end))
  4357. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4358. (setq org-subtree-clip (current-kill 0))
  4359. (message "%s: Subtree(s) with %d characters"
  4360. (if cut "Cut" "Copied")
  4361. (length org-subtree-clip)))))
  4362. (defun org-paste-subtree (&optional level tree)
  4363. "Paste the clipboard as a subtree, with modification of headline level.
  4364. The entire subtree is promoted or demoted in order to match a new headline
  4365. level. By default, the new level is derived from the visible headings
  4366. before and after the insertion point, and taken to be the inferior headline
  4367. level of the two. So if the previous visible heading is level 3 and the
  4368. next is level 4 (or vice versa), level 4 will be used for insertion.
  4369. This makes sure that the subtree remains an independent subtree and does
  4370. not swallow low level entries.
  4371. You can also force a different level, either by using a numeric prefix
  4372. argument, or by inserting the heading marker by hand. For example, if the
  4373. cursor is after \"*****\", then the tree will be shifted to level 5.
  4374. If you want to insert the tree as is, just use \\[yank].
  4375. If optional TREE is given, use this text instead of the kill ring."
  4376. (interactive "P")
  4377. (unless (org-kill-is-subtree-p tree)
  4378. (error "%s"
  4379. (substitute-command-keys
  4380. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4381. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4382. (^re (concat "^\\(" outline-regexp "\\)"))
  4383. (re (concat "\\(" outline-regexp "\\)"))
  4384. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4385. (old-level (if (string-match ^re txt)
  4386. (- (match-end 0) (match-beginning 0) 1)
  4387. -1))
  4388. (force-level (cond (level (prefix-numeric-value level))
  4389. ((string-match
  4390. ^re_ (buffer-substring (point-at-bol) (point)))
  4391. (- (match-end 1) (match-beginning 1)))
  4392. (t nil)))
  4393. (previous-level (save-excursion
  4394. (condition-case nil
  4395. (progn
  4396. (outline-previous-visible-heading 1)
  4397. (if (looking-at re)
  4398. (- (match-end 0) (match-beginning 0) 1)
  4399. 1))
  4400. (error 1))))
  4401. (next-level (save-excursion
  4402. (condition-case nil
  4403. (progn
  4404. (or (looking-at outline-regexp)
  4405. (outline-next-visible-heading 1))
  4406. (if (looking-at re)
  4407. (- (match-end 0) (match-beginning 0) 1)
  4408. 1))
  4409. (error 1))))
  4410. (new-level (or force-level (max previous-level next-level)))
  4411. (shift (if (or (= old-level -1)
  4412. (= new-level -1)
  4413. (= old-level new-level))
  4414. 0
  4415. (- new-level old-level)))
  4416. (delta (if (> shift 0) -1 1))
  4417. (func (if (> shift 0) 'org-demote 'org-promote))
  4418. (org-odd-levels-only nil)
  4419. beg end)
  4420. ;; Remove the forced level indicator
  4421. (if force-level
  4422. (delete-region (point-at-bol) (point)))
  4423. ;; Paste
  4424. (beginning-of-line 1)
  4425. (org-back-over-empty-lines)
  4426. (setq beg (point))
  4427. (insert-before-markers txt)
  4428. (unless (string-match "\n\\'" txt) (insert "\n"))
  4429. (org-reinstall-markers-in-region beg)
  4430. (setq end (point))
  4431. (goto-char beg)
  4432. (skip-chars-forward " \t\n\r")
  4433. (setq beg (point))
  4434. ;; Shift if necessary
  4435. (unless (= shift 0)
  4436. (save-restriction
  4437. (narrow-to-region beg end)
  4438. (while (not (= shift 0))
  4439. (org-map-region func (point-min) (point-max))
  4440. (setq shift (+ delta shift)))
  4441. (goto-char (point-min))))
  4442. (when (interactive-p)
  4443. (message "Clipboard pasted as level %d subtree" new-level))
  4444. (if (and kill-ring
  4445. (eq org-subtree-clip (current-kill 0))
  4446. org-subtree-clip-folded)
  4447. ;; The tree was folded before it was killed/copied
  4448. (hide-subtree))))
  4449. (defun org-kill-is-subtree-p (&optional txt)
  4450. "Check if the current kill is an outline subtree, or a set of trees.
  4451. Returns nil if kill does not start with a headline, or if the first
  4452. headline level is not the largest headline level in the tree.
  4453. So this will actually accept several entries of equal levels as well,
  4454. which is OK for `org-paste-subtree'.
  4455. If optional TXT is given, check this string instead of the current kill."
  4456. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4457. (start-level (and kill
  4458. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4459. org-outline-regexp "\\)")
  4460. kill)
  4461. (- (match-end 2) (match-beginning 2) 1)))
  4462. (re (concat "^" org-outline-regexp))
  4463. (start (1+ (match-beginning 2))))
  4464. (if (not start-level)
  4465. (progn
  4466. nil) ;; does not even start with a heading
  4467. (catch 'exit
  4468. (while (setq start (string-match re kill (1+ start)))
  4469. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4470. (throw 'exit nil)))
  4471. t))))
  4472. (defvar org-markers-to-move nil
  4473. "Markers that should be moved with a cut-and-paste operation.
  4474. Those markers are stored together with their positions relative to
  4475. the start of the region.")
  4476. (defun org-save-markers-in-region (beg end)
  4477. "Check markers in region.
  4478. If these markers are between BEG and END, record their position relative
  4479. to BEG, so that after moving the block of text, we can put the markers back
  4480. into place.
  4481. This function gets called just before an entry or tree gets cut from the
  4482. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4483. called immediately, to move the markers with the entries."
  4484. (setq org-markers-to-move nil)
  4485. (when (featurep 'org-clock)
  4486. (org-clock-save-markers-for-cut-and-paste beg end))
  4487. (when (featurep 'org-agenda)
  4488. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4489. (defun org-check-and-save-marker (marker beg end)
  4490. "Check if MARKER is between BEG and END.
  4491. If yes, remember the marker and the distance to BEG."
  4492. (when (and (marker-buffer marker)
  4493. (equal (marker-buffer marker) (current-buffer)))
  4494. (if (and (>= marker beg) (< marker end))
  4495. (push (cons marker (- marker beg)) org-markers-to-move))))
  4496. (defun org-reinstall-markers-in-region (beg)
  4497. "Move all remembered markers to their position relative to BEG."
  4498. (mapc (lambda (x)
  4499. (move-marker (car x) (+ beg (cdr x))))
  4500. org-markers-to-move)
  4501. (setq org-markers-to-move nil))
  4502. (defun org-narrow-to-subtree ()
  4503. "Narrow buffer to the current subtree."
  4504. (interactive)
  4505. (save-excursion
  4506. (save-match-data
  4507. (narrow-to-region
  4508. (progn (org-back-to-heading) (point))
  4509. (progn (org-end-of-subtree t t) (point))))))
  4510. ;;; Outline Sorting
  4511. (defun org-sort (with-case)
  4512. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4513. Optional argument WITH-CASE means sort case-sensitively."
  4514. (interactive "P")
  4515. (if (org-at-table-p)
  4516. (org-call-with-arg 'org-table-sort-lines with-case)
  4517. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4518. (defun org-sort-remove-invisible (s)
  4519. (remove-text-properties 0 (length s) org-rm-props s)
  4520. (while (string-match org-bracket-link-regexp s)
  4521. (setq s (replace-match (if (match-end 2)
  4522. (match-string 3 s)
  4523. (match-string 1 s)) t t s)))
  4524. s)
  4525. (defvar org-priority-regexp) ; defined later in the file
  4526. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4527. "Sort entries on a certain level of an outline tree.
  4528. If there is an active region, the entries in the region are sorted.
  4529. Else, if the cursor is before the first entry, sort the top-level items.
  4530. Else, the children of the entry at point are sorted.
  4531. Sorting can be alphabetically, numerically, and by date/time as given by
  4532. the first time stamp in the entry. The command prompts for the sorting
  4533. type unless it has been given to the function through the SORTING-TYPE
  4534. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4535. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4536. called with point at the beginning of the record. It must return either
  4537. a string or a number that should serve as the sorting key for that record.
  4538. Comparing entries ignores case by default. However, with an optional argument
  4539. WITH-CASE, the sorting considers case as well."
  4540. (interactive "P")
  4541. (let ((case-func (if with-case 'identity 'downcase))
  4542. start beg end stars re re2
  4543. txt what tmp plain-list-p)
  4544. ;; Find beginning and end of region to sort
  4545. (cond
  4546. ((org-region-active-p)
  4547. ;; we will sort the region
  4548. (setq end (region-end)
  4549. what "region")
  4550. (goto-char (region-beginning))
  4551. (if (not (org-on-heading-p)) (outline-next-heading))
  4552. (setq start (point)))
  4553. ((org-at-item-p)
  4554. ;; we will sort this plain list
  4555. (org-beginning-of-item-list) (setq start (point))
  4556. (org-end-of-item-list) (setq end (point))
  4557. (goto-char start)
  4558. (setq plain-list-p t
  4559. what "plain list"))
  4560. ((or (org-on-heading-p)
  4561. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4562. ;; we will sort the children of the current headline
  4563. (org-back-to-heading)
  4564. (setq start (point)
  4565. end (progn (org-end-of-subtree t t)
  4566. (org-back-over-empty-lines)
  4567. (point))
  4568. what "children")
  4569. (goto-char start)
  4570. (show-subtree)
  4571. (outline-next-heading))
  4572. (t
  4573. ;; we will sort the top-level entries in this file
  4574. (goto-char (point-min))
  4575. (or (org-on-heading-p) (outline-next-heading))
  4576. (setq start (point) end (point-max) what "top-level")
  4577. (goto-char start)
  4578. (show-all)))
  4579. (setq beg (point))
  4580. (if (>= beg end) (error "Nothing to sort"))
  4581. (unless plain-list-p
  4582. (looking-at "\\(\\*+\\)")
  4583. (setq stars (match-string 1)
  4584. re (concat "^" (regexp-quote stars) " +")
  4585. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4586. txt (buffer-substring beg end))
  4587. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4588. (if (and (not (equal stars "*")) (string-match re2 txt))
  4589. (error "Region to sort contains a level above the first entry")))
  4590. (unless sorting-type
  4591. (message
  4592. (if plain-list-p
  4593. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4594. "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:")
  4595. what)
  4596. (setq sorting-type (read-char-exclusive))
  4597. (and (= (downcase sorting-type) ?f)
  4598. (setq getkey-func
  4599. (completing-read "Sort using function: "
  4600. obarray 'fboundp t nil nil))
  4601. (setq getkey-func (intern getkey-func)))
  4602. (and (= (downcase sorting-type) ?r)
  4603. (setq property
  4604. (completing-read "Property: "
  4605. (mapcar 'list (org-buffer-property-keys t))
  4606. nil t))))
  4607. (message "Sorting entries...")
  4608. (save-restriction
  4609. (narrow-to-region start end)
  4610. (let ((dcst (downcase sorting-type))
  4611. (now (current-time)))
  4612. (sort-subr
  4613. (/= dcst sorting-type)
  4614. ;; This function moves to the beginning character of the "record" to
  4615. ;; be sorted.
  4616. (if plain-list-p
  4617. (lambda nil
  4618. (if (org-at-item-p) t (goto-char (point-max))))
  4619. (lambda nil
  4620. (if (re-search-forward re nil t)
  4621. (goto-char (match-beginning 0))
  4622. (goto-char (point-max)))))
  4623. ;; This function moves to the last character of the "record" being
  4624. ;; sorted.
  4625. (if plain-list-p
  4626. 'org-end-of-item
  4627. (lambda nil
  4628. (save-match-data
  4629. (condition-case nil
  4630. (outline-forward-same-level 1)
  4631. (error
  4632. (goto-char (point-max)))))))
  4633. ;; This function returns the value that gets sorted against.
  4634. (if plain-list-p
  4635. (lambda nil
  4636. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4637. (cond
  4638. ((= dcst ?n)
  4639. (string-to-number (buffer-substring (match-end 0)
  4640. (point-at-eol))))
  4641. ((= dcst ?a)
  4642. (buffer-substring (match-end 0) (point-at-eol)))
  4643. ((= dcst ?t)
  4644. (if (re-search-forward org-ts-regexp
  4645. (point-at-eol) t)
  4646. (org-time-string-to-time (match-string 0))
  4647. now))
  4648. ((= dcst ?f)
  4649. (if getkey-func
  4650. (progn
  4651. (setq tmp (funcall getkey-func))
  4652. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4653. tmp)
  4654. (error "Invalid key function `%s'" getkey-func)))
  4655. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4656. (lambda nil
  4657. (cond
  4658. ((= dcst ?n)
  4659. (if (looking-at outline-regexp)
  4660. (string-to-number (buffer-substring (match-end 0)
  4661. (point-at-eol)))
  4662. nil))
  4663. ((= dcst ?a)
  4664. (funcall case-func (buffer-substring (point-at-bol)
  4665. (point-at-eol))))
  4666. ((= dcst ?t)
  4667. (if (re-search-forward org-ts-regexp
  4668. (save-excursion
  4669. (forward-line 2)
  4670. (point)) t)
  4671. (org-time-string-to-time (match-string 0))
  4672. now))
  4673. ((= dcst ?p)
  4674. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4675. (string-to-char (match-string 2))
  4676. org-default-priority))
  4677. ((= dcst ?r)
  4678. (or (org-entry-get nil property) ""))
  4679. ((= dcst ?o)
  4680. (if (looking-at org-complex-heading-regexp)
  4681. (- 9999 (length (member (match-string 2)
  4682. org-todo-keywords-1)))))
  4683. ((= dcst ?f)
  4684. (if getkey-func
  4685. (progn
  4686. (setq tmp (funcall getkey-func))
  4687. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4688. tmp)
  4689. (error "Invalid key function `%s'" getkey-func)))
  4690. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4691. nil
  4692. (cond
  4693. ((= dcst ?a) 'string<)
  4694. ((= dcst ?t) 'time-less-p)
  4695. (t nil)))))
  4696. (message "Sorting entries...done")))
  4697. (defun org-do-sort (table what &optional with-case sorting-type)
  4698. "Sort TABLE of WHAT according to SORTING-TYPE.
  4699. The user will be prompted for the SORTING-TYPE if the call to this
  4700. function does not specify it. WHAT is only for the prompt, to indicate
  4701. what is being sorted. The sorting key will be extracted from
  4702. the car of the elements of the table.
  4703. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4704. (unless sorting-type
  4705. (message
  4706. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4707. what)
  4708. (setq sorting-type (read-char-exclusive)))
  4709. (let ((dcst (downcase sorting-type))
  4710. extractfun comparefun)
  4711. ;; Define the appropriate functions
  4712. (cond
  4713. ((= dcst ?n)
  4714. (setq extractfun 'string-to-number
  4715. comparefun (if (= dcst sorting-type) '< '>)))
  4716. ((= dcst ?a)
  4717. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4718. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4719. comparefun (if (= dcst sorting-type)
  4720. 'string<
  4721. (lambda (a b) (and (not (string< a b))
  4722. (not (string= a b)))))))
  4723. ((= dcst ?t)
  4724. (setq extractfun
  4725. (lambda (x)
  4726. (if (string-match org-ts-regexp x)
  4727. (time-to-seconds
  4728. (org-time-string-to-time (match-string 0 x)))
  4729. 0))
  4730. comparefun (if (= dcst sorting-type) '< '>)))
  4731. (t (error "Invalid sorting type `%c'" sorting-type)))
  4732. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4733. table)
  4734. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4735. ;;;; Plain list items, including checkboxes
  4736. ;;; Plain list items
  4737. (defun org-at-item-p ()
  4738. "Is point in a line starting a hand-formatted item?"
  4739. (let ((llt org-plain-list-ordered-item-terminator))
  4740. (save-excursion
  4741. (goto-char (point-at-bol))
  4742. (looking-at
  4743. (cond
  4744. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4745. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4746. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4747. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4748. (defun org-in-item-p ()
  4749. "It the cursor inside a plain list item.
  4750. Does not have to be the first line."
  4751. (save-excursion
  4752. (condition-case nil
  4753. (progn
  4754. (org-beginning-of-item)
  4755. (org-at-item-p)
  4756. t)
  4757. (error nil))))
  4758. (defun org-insert-item (&optional checkbox)
  4759. "Insert a new item at the current level.
  4760. Return t when things worked, nil when we are not in an item."
  4761. (when (save-excursion
  4762. (condition-case nil
  4763. (progn
  4764. (org-beginning-of-item)
  4765. (org-at-item-p)
  4766. (if (org-invisible-p) (error "Invisible item"))
  4767. t)
  4768. (error nil)))
  4769. (let* ((bul (match-string 0))
  4770. (descp (save-excursion (goto-char (match-beginning 0))
  4771. (beginning-of-line 1)
  4772. (save-match-data
  4773. (looking-at "[ \t]*.*? ::"))))
  4774. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4775. (match-end 0)))
  4776. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4777. pos)
  4778. (if descp (setq checkbox nil))
  4779. (cond
  4780. ((and (org-at-item-p) (<= (point) eow))
  4781. ;; before the bullet
  4782. (beginning-of-line 1)
  4783. (open-line (if blank 2 1)))
  4784. ((<= (point) eow)
  4785. (beginning-of-line 1))
  4786. (t
  4787. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4788. (end-of-line 1)
  4789. (delete-horizontal-space))
  4790. (newline (if blank 2 1))))
  4791. (insert bul
  4792. (if checkbox "[ ]" "")
  4793. (if descp (concat (if checkbox " " "")
  4794. (read-string "Term: ") " :: ") ""))
  4795. (just-one-space)
  4796. (setq pos (point))
  4797. (end-of-line 1)
  4798. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4799. (org-maybe-renumber-ordered-list)
  4800. (and checkbox (org-update-checkbox-count-maybe))
  4801. t))
  4802. ;;; Checkboxes
  4803. (defun org-at-item-checkbox-p ()
  4804. "Is point at a line starting a plain-list item with a checklet?"
  4805. (and (org-at-item-p)
  4806. (save-excursion
  4807. (goto-char (match-end 0))
  4808. (skip-chars-forward " \t")
  4809. (looking-at "\\[[- X]\\]"))))
  4810. (defun org-toggle-checkbox (&optional arg)
  4811. "Toggle the checkbox in the current line."
  4812. (interactive "P")
  4813. (catch 'exit
  4814. (let (beg end status (firstnew 'unknown))
  4815. (cond
  4816. ((org-region-active-p)
  4817. (setq beg (region-beginning) end (region-end)))
  4818. ((org-on-heading-p)
  4819. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4820. ((org-at-item-checkbox-p)
  4821. (let ((pos (point)))
  4822. (replace-match
  4823. (cond (arg "[-]")
  4824. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4825. (t "[ ]"))
  4826. t t)
  4827. (goto-char pos))
  4828. (throw 'exit t))
  4829. (t (error "Not at a checkbox or heading, and no active region")))
  4830. (save-excursion
  4831. (goto-char beg)
  4832. (while (< (point) end)
  4833. (when (org-at-item-checkbox-p)
  4834. (setq status (equal (match-string 0) "[X]"))
  4835. (when (eq firstnew 'unknown)
  4836. (setq firstnew (not status)))
  4837. (replace-match
  4838. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4839. (beginning-of-line 2)))))
  4840. (org-update-checkbox-count-maybe))
  4841. (defun org-update-checkbox-count-maybe ()
  4842. "Update checkbox statistics unless turned off by user."
  4843. (when org-provide-checkbox-statistics
  4844. (org-update-checkbox-count)))
  4845. (defun org-update-checkbox-count (&optional all)
  4846. "Update the checkbox statistics in the current section.
  4847. This will find all statistic cookies like [57%] and [6/12] and update them
  4848. with the current numbers. With optional prefix argument ALL, do this for
  4849. the whole buffer."
  4850. (interactive "P")
  4851. (save-excursion
  4852. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4853. (beg (condition-case nil
  4854. (progn (outline-back-to-heading) (point))
  4855. (error (point-min))))
  4856. (end (move-marker (make-marker)
  4857. (progn (outline-next-heading) (point))))
  4858. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4859. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4860. (re-find (concat re "\\|" re-box))
  4861. beg-cookie end-cookie is-percent c-on c-off lim
  4862. eline curr-ind next-ind continue-from startsearch
  4863. (cstat 0)
  4864. )
  4865. (when all
  4866. (goto-char (point-min))
  4867. (outline-next-heading)
  4868. (setq beg (point) end (point-max)))
  4869. (goto-char end)
  4870. ;; find each statistic cookie
  4871. (while (re-search-backward re-find beg t)
  4872. (setq beg-cookie (match-beginning 1)
  4873. end-cookie (match-end 1)
  4874. cstat (+ cstat (if end-cookie 1 0))
  4875. startsearch (point-at-eol)
  4876. continue-from (point-at-bol)
  4877. is-percent (match-beginning 2)
  4878. lim (cond
  4879. ((org-on-heading-p) (outline-next-heading) (point))
  4880. ((org-at-item-p) (org-end-of-item) (point))
  4881. (t nil))
  4882. c-on 0
  4883. c-off 0)
  4884. (when lim
  4885. ;; find first checkbox for this cookie and gather
  4886. ;; statistics from all that are at this indentation level
  4887. (goto-char startsearch)
  4888. (if (re-search-forward re-box lim t)
  4889. (progn
  4890. (org-beginning-of-item)
  4891. (setq curr-ind (org-get-indentation))
  4892. (setq next-ind curr-ind)
  4893. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4894. (save-excursion (end-of-line) (setq eline (point)))
  4895. (if (re-search-forward re-box eline t)
  4896. (if (member (match-string 2) '("[ ]" "[-]"))
  4897. (setq c-off (1+ c-off))
  4898. (setq c-on (1+ c-on))
  4899. )
  4900. )
  4901. (org-end-of-item)
  4902. (setq next-ind (org-get-indentation))
  4903. )))
  4904. (goto-char continue-from)
  4905. ;; update cookie
  4906. (when end-cookie
  4907. (delete-region beg-cookie end-cookie)
  4908. (goto-char beg-cookie)
  4909. (insert
  4910. (if is-percent
  4911. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4912. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4913. ;; update items checkbox if it has one
  4914. (when (org-at-item-p)
  4915. (org-beginning-of-item)
  4916. (when (and (> (+ c-on c-off) 0)
  4917. (re-search-forward re-box (point-at-eol) t))
  4918. (setq beg-cookie (match-beginning 2)
  4919. end-cookie (match-end 2))
  4920. (delete-region beg-cookie end-cookie)
  4921. (goto-char beg-cookie)
  4922. (cond ((= c-off 0) (insert "[X]"))
  4923. ((= c-on 0) (insert "[ ]"))
  4924. (t (insert "[-]")))
  4925. )))
  4926. (goto-char continue-from))
  4927. (when (interactive-p)
  4928. (message "Checkbox satistics updated %s (%d places)"
  4929. (if all "in entire file" "in current outline entry") cstat)))))
  4930. (defun org-get-checkbox-statistics-face ()
  4931. "Select the face for checkbox statistics.
  4932. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4933. it will be `org-todo'."
  4934. (if (match-end 1)
  4935. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4936. (if (and (> (match-end 2) (match-beginning 2))
  4937. (equal (match-string 2) (match-string 3)))
  4938. 'org-done
  4939. 'org-todo)))
  4940. (defun org-get-indentation (&optional line)
  4941. "Get the indentation of the current line, interpreting tabs.
  4942. When LINE is given, assume it represents a line and compute its indentation."
  4943. (if line
  4944. (if (string-match "^ *" (org-remove-tabs line))
  4945. (match-end 0))
  4946. (save-excursion
  4947. (beginning-of-line 1)
  4948. (skip-chars-forward " \t")
  4949. (current-column))))
  4950. (defun org-remove-tabs (s &optional width)
  4951. "Replace tabulators in S with spaces.
  4952. Assumes that s is a single line, starting in column 0."
  4953. (setq width (or width tab-width))
  4954. (while (string-match "\t" s)
  4955. (setq s (replace-match
  4956. (make-string
  4957. (- (* width (/ (+ (match-beginning 0) width) width))
  4958. (match-beginning 0)) ?\ )
  4959. t t s)))
  4960. s)
  4961. (defun org-fix-indentation (line ind)
  4962. "Fix indentation in LINE.
  4963. IND is a cons cell with target and minimum indentation.
  4964. If the current indenation in LINE is smaller than the minimum,
  4965. leave it alone. If it is larger than ind, set it to the target."
  4966. (let* ((l (org-remove-tabs line))
  4967. (i (org-get-indentation l))
  4968. (i1 (car ind)) (i2 (cdr ind)))
  4969. (if (>= i i2) (setq l (substring line i2)))
  4970. (if (> i1 0)
  4971. (concat (make-string i1 ?\ ) l)
  4972. l)))
  4973. (defun org-beginning-of-item ()
  4974. "Go to the beginning of the current hand-formatted item.
  4975. If the cursor is not in an item, throw an error."
  4976. (interactive)
  4977. (let ((pos (point))
  4978. (limit (save-excursion
  4979. (condition-case nil
  4980. (progn
  4981. (org-back-to-heading)
  4982. (beginning-of-line 2) (point))
  4983. (error (point-min)))))
  4984. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4985. ind ind1)
  4986. (if (org-at-item-p)
  4987. (beginning-of-line 1)
  4988. (beginning-of-line 1)
  4989. (skip-chars-forward " \t")
  4990. (setq ind (current-column))
  4991. (if (catch 'exit
  4992. (while t
  4993. (beginning-of-line 0)
  4994. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4995. (if (looking-at "[ \t]*$")
  4996. (setq ind1 ind-empty)
  4997. (skip-chars-forward " \t")
  4998. (setq ind1 (current-column)))
  4999. (if (< ind1 ind)
  5000. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5001. nil
  5002. (goto-char pos)
  5003. (error "Not in an item")))))
  5004. (defun org-end-of-item ()
  5005. "Go to the end of the current hand-formatted item.
  5006. If the cursor is not in an item, throw an error."
  5007. (interactive)
  5008. (let* ((pos (point))
  5009. ind1
  5010. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5011. (limit (save-excursion (outline-next-heading) (point)))
  5012. (ind (save-excursion
  5013. (org-beginning-of-item)
  5014. (skip-chars-forward " \t")
  5015. (current-column)))
  5016. (end (catch 'exit
  5017. (while t
  5018. (beginning-of-line 2)
  5019. (if (eobp) (throw 'exit (point)))
  5020. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5021. (if (looking-at "[ \t]*$")
  5022. (setq ind1 ind-empty)
  5023. (skip-chars-forward " \t")
  5024. (setq ind1 (current-column)))
  5025. (if (<= ind1 ind)
  5026. (throw 'exit (point-at-bol)))))))
  5027. (if end
  5028. (goto-char end)
  5029. (goto-char pos)
  5030. (error "Not in an item"))))
  5031. (defun org-next-item ()
  5032. "Move to the beginning of the next item in the current plain list.
  5033. Error if not at a plain list, or if this is the last item in the list."
  5034. (interactive)
  5035. (let (ind ind1 (pos (point)))
  5036. (org-beginning-of-item)
  5037. (setq ind (org-get-indentation))
  5038. (org-end-of-item)
  5039. (setq ind1 (org-get-indentation))
  5040. (unless (and (org-at-item-p) (= ind ind1))
  5041. (goto-char pos)
  5042. (error "On last item"))))
  5043. (defun org-previous-item ()
  5044. "Move to the beginning of the previous item in the current plain list.
  5045. Error if not at a plain list, or if this is the first item in the list."
  5046. (interactive)
  5047. (let (beg ind ind1 (pos (point)))
  5048. (org-beginning-of-item)
  5049. (setq beg (point))
  5050. (setq ind (org-get-indentation))
  5051. (goto-char beg)
  5052. (catch 'exit
  5053. (while t
  5054. (beginning-of-line 0)
  5055. (if (looking-at "[ \t]*$")
  5056. nil
  5057. (if (<= (setq ind1 (org-get-indentation)) ind)
  5058. (throw 'exit t)))))
  5059. (condition-case nil
  5060. (if (or (not (org-at-item-p))
  5061. (< ind1 (1- ind)))
  5062. (error "")
  5063. (org-beginning-of-item))
  5064. (error (goto-char pos)
  5065. (error "On first item")))))
  5066. (defun org-first-list-item-p ()
  5067. "Is this heading the item in a plain list?"
  5068. (unless (org-at-item-p)
  5069. (error "Not at a plain list item"))
  5070. (org-beginning-of-item)
  5071. (= (point) (save-excursion (org-beginning-of-item-list))))
  5072. (defun org-move-item-down ()
  5073. "Move the plain list item at point down, i.e. swap with following item.
  5074. Subitems (items with larger indentation) are considered part of the item,
  5075. so this really moves item trees."
  5076. (interactive)
  5077. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5078. (org-beginning-of-item)
  5079. (setq beg0 (point))
  5080. (save-excursion
  5081. (setq ne-beg (org-back-over-empty-lines))
  5082. (setq beg (point)))
  5083. (goto-char beg0)
  5084. (setq ind (org-get-indentation))
  5085. (org-end-of-item)
  5086. (setq end0 (point))
  5087. (setq ind1 (org-get-indentation))
  5088. (setq ne-end (org-back-over-empty-lines))
  5089. (setq end (point))
  5090. (goto-char beg0)
  5091. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5092. ;; include less whitespace
  5093. (save-excursion
  5094. (goto-char beg)
  5095. (forward-line (- ne-beg ne-end))
  5096. (setq beg (point))))
  5097. (goto-char end0)
  5098. (if (and (org-at-item-p) (= ind ind1))
  5099. (progn
  5100. (org-end-of-item)
  5101. (org-back-over-empty-lines)
  5102. (setq txt (buffer-substring beg end))
  5103. (save-excursion
  5104. (delete-region beg end))
  5105. (setq pos (point))
  5106. (insert txt)
  5107. (goto-char pos) (org-skip-whitespace)
  5108. (org-maybe-renumber-ordered-list))
  5109. (goto-char pos)
  5110. (error "Cannot move this item further down"))))
  5111. (defun org-move-item-up (arg)
  5112. "Move the plain list item at point up, i.e. swap with previous item.
  5113. Subitems (items with larger indentation) are considered part of the item,
  5114. so this really moves item trees."
  5115. (interactive "p")
  5116. (let (beg beg0 end ind ind1 (pos (point)) txt
  5117. ne-beg ne-ins ins-end)
  5118. (org-beginning-of-item)
  5119. (setq beg0 (point))
  5120. (setq ind (org-get-indentation))
  5121. (save-excursion
  5122. (setq ne-beg (org-back-over-empty-lines))
  5123. (setq beg (point)))
  5124. (goto-char beg0)
  5125. (org-end-of-item)
  5126. (setq end (point))
  5127. (goto-char beg0)
  5128. (catch 'exit
  5129. (while t
  5130. (beginning-of-line 0)
  5131. (if (looking-at "[ \t]*$")
  5132. (if org-empty-line-terminates-plain-lists
  5133. (progn
  5134. (goto-char pos)
  5135. (error "Cannot move this item further up"))
  5136. nil)
  5137. (if (<= (setq ind1 (org-get-indentation)) ind)
  5138. (throw 'exit t)))))
  5139. (condition-case nil
  5140. (org-beginning-of-item)
  5141. (error (goto-char beg)
  5142. (error "Cannot move this item further up")))
  5143. (setq ind1 (org-get-indentation))
  5144. (if (and (org-at-item-p) (= ind ind1))
  5145. (progn
  5146. (setq ne-ins (org-back-over-empty-lines))
  5147. (setq txt (buffer-substring beg end))
  5148. (save-excursion
  5149. (delete-region beg end))
  5150. (setq pos (point))
  5151. (insert txt)
  5152. (setq ins-end (point))
  5153. (goto-char pos) (org-skip-whitespace)
  5154. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5155. ;; Move whitespace back to beginning
  5156. (save-excursion
  5157. (goto-char ins-end)
  5158. (let ((kill-whole-line t))
  5159. (kill-line (- ne-ins ne-beg)) (point)))
  5160. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5161. (org-maybe-renumber-ordered-list))
  5162. (goto-char pos)
  5163. (error "Cannot move this item further up"))))
  5164. (defun org-maybe-renumber-ordered-list ()
  5165. "Renumber the ordered list at point if setup allows it.
  5166. This tests the user option `org-auto-renumber-ordered-lists' before
  5167. doing the renumbering."
  5168. (interactive)
  5169. (when (and org-auto-renumber-ordered-lists
  5170. (org-at-item-p))
  5171. (if (match-beginning 3)
  5172. (org-renumber-ordered-list 1)
  5173. (org-fix-bullet-type))))
  5174. (defun org-maybe-renumber-ordered-list-safe ()
  5175. (condition-case nil
  5176. (save-excursion
  5177. (org-maybe-renumber-ordered-list))
  5178. (error nil)))
  5179. (defun org-cycle-list-bullet (&optional which)
  5180. "Cycle through the different itemize/enumerate bullets.
  5181. This cycle the entire list level through the sequence:
  5182. `-' -> `+' -> `*' -> `1.' -> `1)'
  5183. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5184. 0 meand `-', 1 means `+' etc."
  5185. (interactive "P")
  5186. (org-preserve-lc
  5187. (org-beginning-of-item-list)
  5188. (org-at-item-p)
  5189. (beginning-of-line 1)
  5190. (let ((current (match-string 0))
  5191. (prevp (eq which 'previous))
  5192. new)
  5193. (setq new (cond
  5194. ((and (numberp which)
  5195. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5196. ((string-match "-" current) (if prevp "1)" "+"))
  5197. ((string-match "\\+" current)
  5198. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5199. ((string-match "\\*" current) (if prevp "+" "1."))
  5200. ((string-match "\\." current) (if prevp "*" "1)"))
  5201. ((string-match ")" current) (if prevp "1." "-"))
  5202. (t (error "This should not happen"))))
  5203. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5204. (org-fix-bullet-type)
  5205. (org-maybe-renumber-ordered-list))))
  5206. (defun org-get-string-indentation (s)
  5207. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5208. (let ((n -1) (i 0) (w tab-width) c)
  5209. (catch 'exit
  5210. (while (< (setq n (1+ n)) (length s))
  5211. (setq c (aref s n))
  5212. (cond ((= c ?\ ) (setq i (1+ i)))
  5213. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5214. (t (throw 'exit t)))))
  5215. i))
  5216. (defun org-renumber-ordered-list (arg)
  5217. "Renumber an ordered plain list.
  5218. Cursor needs to be in the first line of an item, the line that starts
  5219. with something like \"1.\" or \"2)\"."
  5220. (interactive "p")
  5221. (unless (and (org-at-item-p)
  5222. (match-beginning 3))
  5223. (error "This is not an ordered list"))
  5224. (let ((line (org-current-line))
  5225. (col (current-column))
  5226. (ind (org-get-string-indentation
  5227. (buffer-substring (point-at-bol) (match-beginning 3))))
  5228. ;; (term (substring (match-string 3) -1))
  5229. ind1 (n (1- arg))
  5230. fmt)
  5231. ;; find where this list begins
  5232. (org-beginning-of-item-list)
  5233. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5234. (setq fmt (concat "%d" (match-string 1)))
  5235. (beginning-of-line 0)
  5236. ;; walk forward and replace these numbers
  5237. (catch 'exit
  5238. (while t
  5239. (catch 'next
  5240. (beginning-of-line 2)
  5241. (if (eobp) (throw 'exit nil))
  5242. (if (looking-at "[ \t]*$") (throw 'next nil))
  5243. (skip-chars-forward " \t") (setq ind1 (current-column))
  5244. (if (> ind1 ind) (throw 'next t))
  5245. (if (< ind1 ind) (throw 'exit t))
  5246. (if (not (org-at-item-p)) (throw 'exit nil))
  5247. (delete-region (match-beginning 2) (match-end 2))
  5248. (goto-char (match-beginning 2))
  5249. (insert (format fmt (setq n (1+ n)))))))
  5250. (goto-line line)
  5251. (org-move-to-column col)))
  5252. (defun org-fix-bullet-type ()
  5253. "Make sure all items in this list have the same bullet as the firsst item."
  5254. (interactive)
  5255. (unless (org-at-item-p) (error "This is not a list"))
  5256. (let ((line (org-current-line))
  5257. (col (current-column))
  5258. (ind (current-indentation))
  5259. ind1 bullet)
  5260. ;; find where this list begins
  5261. (org-beginning-of-item-list)
  5262. (beginning-of-line 1)
  5263. ;; find out what the bullet type is
  5264. (looking-at "[ \t]*\\(\\S-+\\)")
  5265. (setq bullet (match-string 1))
  5266. ;; walk forward and replace these numbers
  5267. (beginning-of-line 0)
  5268. (catch 'exit
  5269. (while t
  5270. (catch 'next
  5271. (beginning-of-line 2)
  5272. (if (eobp) (throw 'exit nil))
  5273. (if (looking-at "[ \t]*$") (throw 'next nil))
  5274. (skip-chars-forward " \t") (setq ind1 (current-column))
  5275. (if (> ind1 ind) (throw 'next t))
  5276. (if (< ind1 ind) (throw 'exit t))
  5277. (if (not (org-at-item-p)) (throw 'exit nil))
  5278. (skip-chars-forward " \t")
  5279. (looking-at "\\S-+")
  5280. (replace-match bullet))))
  5281. (goto-line line)
  5282. (org-move-to-column col)
  5283. (if (string-match "[0-9]" bullet)
  5284. (org-renumber-ordered-list 1))))
  5285. (defun org-beginning-of-item-list ()
  5286. "Go to the beginning of the current item list.
  5287. I.e. to the first item in this list."
  5288. (interactive)
  5289. (org-beginning-of-item)
  5290. (let ((pos (point-at-bol))
  5291. (ind (org-get-indentation))
  5292. ind1)
  5293. ;; find where this list begins
  5294. (catch 'exit
  5295. (while t
  5296. (catch 'next
  5297. (beginning-of-line 0)
  5298. (if (looking-at "[ \t]*$")
  5299. (throw (if (bobp) 'exit 'next) t))
  5300. (skip-chars-forward " \t") (setq ind1 (current-column))
  5301. (if (or (< ind1 ind)
  5302. (and (= ind1 ind)
  5303. (not (org-at-item-p)))
  5304. (bobp))
  5305. (throw 'exit t)
  5306. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5307. (goto-char pos)))
  5308. (defun org-end-of-item-list ()
  5309. "Go to the end of the current item list.
  5310. I.e. to the text after the last item."
  5311. (interactive)
  5312. (org-beginning-of-item)
  5313. (let ((pos (point-at-bol))
  5314. (ind (org-get-indentation))
  5315. ind1)
  5316. ;; find where this list begins
  5317. (catch 'exit
  5318. (while t
  5319. (catch 'next
  5320. (beginning-of-line 2)
  5321. (if (looking-at "[ \t]*$")
  5322. (throw (if (eobp) 'exit 'next) t))
  5323. (skip-chars-forward " \t") (setq ind1 (current-column))
  5324. (if (or (< ind1 ind)
  5325. (and (= ind1 ind)
  5326. (not (org-at-item-p)))
  5327. (eobp))
  5328. (progn
  5329. (setq pos (point-at-bol))
  5330. (throw 'exit t))))))
  5331. (goto-char pos)))
  5332. (defvar org-last-indent-begin-marker (make-marker))
  5333. (defvar org-last-indent-end-marker (make-marker))
  5334. (defun org-outdent-item (arg)
  5335. "Outdent a local list item."
  5336. (interactive "p")
  5337. (org-indent-item (- arg)))
  5338. (defun org-indent-item (arg)
  5339. "Indent a local list item."
  5340. (interactive "p")
  5341. (unless (org-at-item-p)
  5342. (error "Not on an item"))
  5343. (save-excursion
  5344. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5345. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5346. (setq beg org-last-indent-begin-marker
  5347. end org-last-indent-end-marker)
  5348. (org-beginning-of-item)
  5349. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5350. (org-end-of-item)
  5351. (setq end (move-marker org-last-indent-end-marker (point))))
  5352. (goto-char beg)
  5353. (setq tmp (org-item-indent-positions)
  5354. ind (car tmp)
  5355. ind-down (nth 2 tmp)
  5356. ind-up (nth 1 tmp)
  5357. delta (if (> arg 0)
  5358. (if ind-down (- ind-down ind) 2)
  5359. (if ind-up (- ind-up ind) -2)))
  5360. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5361. (while (< (point) end)
  5362. (beginning-of-line 1)
  5363. (skip-chars-forward " \t") (setq ind1 (current-column))
  5364. (delete-region (point-at-bol) (point))
  5365. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5366. (beginning-of-line 2))))
  5367. (org-fix-bullet-type)
  5368. (org-maybe-renumber-ordered-list-safe)
  5369. (save-excursion
  5370. (beginning-of-line 0)
  5371. (condition-case nil (org-beginning-of-item) (error nil))
  5372. (org-maybe-renumber-ordered-list-safe)))
  5373. (defun org-item-indent-positions ()
  5374. "Return indentation for plain list items.
  5375. This returns a list with three values: The current indentation, the
  5376. parent indentation and the indentation a child should habe.
  5377. Assumes cursor in item line."
  5378. (let* ((bolpos (point-at-bol))
  5379. (ind (org-get-indentation))
  5380. ind-down ind-up pos)
  5381. (save-excursion
  5382. (org-beginning-of-item-list)
  5383. (skip-chars-backward "\n\r \t")
  5384. (when (org-in-item-p)
  5385. (org-beginning-of-item)
  5386. (setq ind-up (org-get-indentation))))
  5387. (setq pos (point))
  5388. (save-excursion
  5389. (cond
  5390. ((and (condition-case nil (progn (org-previous-item) t)
  5391. (error nil))
  5392. (or (forward-char 1) t)
  5393. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5394. (setq ind-down (org-get-indentation)))
  5395. ((and (goto-char pos)
  5396. (org-at-item-p))
  5397. (goto-char (match-end 0))
  5398. (skip-chars-forward " \t")
  5399. (setq ind-down (current-column)))))
  5400. (list ind ind-up ind-down)))
  5401. ;;; The orgstruct minor mode
  5402. ;; Define a minor mode which can be used in other modes in order to
  5403. ;; integrate the org-mode structure editing commands.
  5404. ;; This is really a hack, because the org-mode structure commands use
  5405. ;; keys which normally belong to the major mode. Here is how it
  5406. ;; works: The minor mode defines all the keys necessary to operate the
  5407. ;; structure commands, but wraps the commands into a function which
  5408. ;; tests if the cursor is currently at a headline or a plain list
  5409. ;; item. If that is the case, the structure command is used,
  5410. ;; temporarily setting many Org-mode variables like regular
  5411. ;; expressions for filling etc. However, when any of those keys is
  5412. ;; used at a different location, function uses `key-binding' to look
  5413. ;; up if the key has an associated command in another currently active
  5414. ;; keymap (minor modes, major mode, global), and executes that
  5415. ;; command. There might be problems if any of the keys is otherwise
  5416. ;; used as a prefix key.
  5417. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5418. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5419. ;; addresses this by checking explicitly for both bindings.
  5420. (defvar orgstruct-mode-map (make-sparse-keymap)
  5421. "Keymap for the minor `orgstruct-mode'.")
  5422. (defvar org-local-vars nil
  5423. "List of local variables, for use by `orgstruct-mode'")
  5424. ;;;###autoload
  5425. (define-minor-mode orgstruct-mode
  5426. "Toggle the minor more `orgstruct-mode'.
  5427. This mode is for using Org-mode structure commands in other modes.
  5428. The following key behave as if Org-mode was active, if the cursor
  5429. is on a headline, or on a plain list item (both in the definition
  5430. of Org-mode).
  5431. M-up Move entry/item up
  5432. M-down Move entry/item down
  5433. M-left Promote
  5434. M-right Demote
  5435. M-S-up Move entry/item up
  5436. M-S-down Move entry/item down
  5437. M-S-left Promote subtree
  5438. M-S-right Demote subtree
  5439. M-q Fill paragraph and items like in Org-mode
  5440. C-c ^ Sort entries
  5441. C-c - Cycle list bullet
  5442. TAB Cycle item visibility
  5443. M-RET Insert new heading/item
  5444. S-M-RET Insert new TODO heading / Chekbox item
  5445. C-c C-c Set tags / toggle checkbox"
  5446. nil " OrgStruct" nil
  5447. (org-load-modules-maybe)
  5448. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5449. ;;;###autoload
  5450. (defun turn-on-orgstruct ()
  5451. "Unconditionally turn on `orgstruct-mode'."
  5452. (orgstruct-mode 1))
  5453. ;;;###autoload
  5454. (defun turn-on-orgstruct++ ()
  5455. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5456. In addition to setting orgstruct-mode, this also exports all indentation and
  5457. autofilling variables from org-mode into the buffer. Note that turning
  5458. off orgstruct-mode will *not* remove these additional settings."
  5459. (orgstruct-mode 1)
  5460. (let (var val)
  5461. (mapc
  5462. (lambda (x)
  5463. (when (string-match
  5464. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5465. (symbol-name (car x)))
  5466. (setq var (car x) val (nth 1 x))
  5467. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5468. org-local-vars)))
  5469. (defun orgstruct-error ()
  5470. "Error when there is no default binding for a structure key."
  5471. (interactive)
  5472. (error "This key has no function outside structure elements"))
  5473. (defun orgstruct-setup ()
  5474. "Setup orgstruct keymaps."
  5475. (let ((nfunc 0)
  5476. (bindings
  5477. (list
  5478. '([(meta up)] org-metaup)
  5479. '([(meta down)] org-metadown)
  5480. '([(meta left)] org-metaleft)
  5481. '([(meta right)] org-metaright)
  5482. '([(meta shift up)] org-shiftmetaup)
  5483. '([(meta shift down)] org-shiftmetadown)
  5484. '([(meta shift left)] org-shiftmetaleft)
  5485. '([(meta shift right)] org-shiftmetaright)
  5486. '([(shift up)] org-shiftup)
  5487. '([(shift down)] org-shiftdown)
  5488. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5489. '("\M-q" fill-paragraph)
  5490. '("\C-c^" org-sort)
  5491. '("\C-c-" org-cycle-list-bullet)))
  5492. elt key fun cmd)
  5493. (while (setq elt (pop bindings))
  5494. (setq nfunc (1+ nfunc))
  5495. (setq key (org-key (car elt))
  5496. fun (nth 1 elt)
  5497. cmd (orgstruct-make-binding fun nfunc key))
  5498. (org-defkey orgstruct-mode-map key cmd))
  5499. ;; Special treatment needed for TAB and RET
  5500. (org-defkey orgstruct-mode-map [(tab)]
  5501. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5502. (org-defkey orgstruct-mode-map "\C-i"
  5503. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5504. (org-defkey orgstruct-mode-map "\M-\C-m"
  5505. (orgstruct-make-binding 'org-insert-heading 105
  5506. "\M-\C-m" [(meta return)]))
  5507. (org-defkey orgstruct-mode-map [(meta return)]
  5508. (orgstruct-make-binding 'org-insert-heading 106
  5509. [(meta return)] "\M-\C-m"))
  5510. (org-defkey orgstruct-mode-map [(shift meta return)]
  5511. (orgstruct-make-binding 'org-insert-todo-heading 107
  5512. [(meta return)] "\M-\C-m"))
  5513. (unless org-local-vars
  5514. (setq org-local-vars (org-get-local-variables)))
  5515. t))
  5516. (defun orgstruct-make-binding (fun n &rest keys)
  5517. "Create a function for binding in the structure minor mode.
  5518. FUN is the command to call inside a table. N is used to create a unique
  5519. command name. KEYS are keys that should be checked in for a command
  5520. to execute outside of tables."
  5521. (eval
  5522. (list 'defun
  5523. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5524. '(arg)
  5525. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5526. "Outside of structure, run the binding of `"
  5527. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5528. "'.")
  5529. '(interactive "p")
  5530. (list 'if
  5531. '(org-context-p 'headline 'item)
  5532. (list 'org-run-like-in-org-mode (list 'quote fun))
  5533. (list 'let '(orgstruct-mode)
  5534. (list 'call-interactively
  5535. (append '(or)
  5536. (mapcar (lambda (k)
  5537. (list 'key-binding k))
  5538. keys)
  5539. '('orgstruct-error))))))))
  5540. (defun org-context-p (&rest contexts)
  5541. "Check if local context is and of CONTEXTS.
  5542. Possible values in the list of contexts are `table', `headline', and `item'."
  5543. (let ((pos (point)))
  5544. (goto-char (point-at-bol))
  5545. (prog1 (or (and (memq 'table contexts)
  5546. (looking-at "[ \t]*|"))
  5547. (and (memq 'headline contexts)
  5548. (looking-at "\\*+"))
  5549. (and (memq 'item contexts)
  5550. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5551. (goto-char pos))))
  5552. (defun org-get-local-variables ()
  5553. "Return a list of all local variables in an org-mode buffer."
  5554. (let (varlist)
  5555. (with-current-buffer (get-buffer-create "*Org tmp*")
  5556. (erase-buffer)
  5557. (org-mode)
  5558. (setq varlist (buffer-local-variables)))
  5559. (kill-buffer "*Org tmp*")
  5560. (delq nil
  5561. (mapcar
  5562. (lambda (x)
  5563. (setq x
  5564. (if (symbolp x)
  5565. (list x)
  5566. (list (car x) (list 'quote (cdr x)))))
  5567. (if (string-match
  5568. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5569. (symbol-name (car x)))
  5570. x nil))
  5571. varlist))))
  5572. ;;;###autoload
  5573. (defun org-run-like-in-org-mode (cmd)
  5574. (org-load-modules-maybe)
  5575. (unless org-local-vars
  5576. (setq org-local-vars (org-get-local-variables)))
  5577. (eval (list 'let org-local-vars
  5578. (list 'call-interactively (list 'quote cmd)))))
  5579. ;;;; Archiving
  5580. (defun org-get-category (&optional pos)
  5581. "Get the category applying to position POS."
  5582. (get-text-property (or pos (point)) 'org-category))
  5583. (defun org-refresh-category-properties ()
  5584. "Refresh category text properties in the buffer."
  5585. (let ((def-cat (cond
  5586. ((null org-category)
  5587. (if buffer-file-name
  5588. (file-name-sans-extension
  5589. (file-name-nondirectory buffer-file-name))
  5590. "???"))
  5591. ((symbolp org-category) (symbol-name org-category))
  5592. (t org-category)))
  5593. beg end cat pos optionp)
  5594. (org-unmodified
  5595. (save-excursion
  5596. (save-restriction
  5597. (widen)
  5598. (goto-char (point-min))
  5599. (put-text-property (point) (point-max) 'org-category def-cat)
  5600. (while (re-search-forward
  5601. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5602. (setq pos (match-end 0)
  5603. optionp (equal (char-after (match-beginning 0)) ?#)
  5604. cat (org-trim (match-string 2)))
  5605. (if optionp
  5606. (setq beg (point-at-bol) end (point-max))
  5607. (org-back-to-heading t)
  5608. (setq beg (point) end (org-end-of-subtree t t)))
  5609. (put-text-property beg end 'org-category cat)
  5610. (goto-char pos)))))))
  5611. ;;;; Link Stuff
  5612. ;;; Link abbreviations
  5613. (defun org-link-expand-abbrev (link)
  5614. "Apply replacements as defined in `org-link-abbrev-alist."
  5615. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5616. (let* ((key (match-string 1 link))
  5617. (as (or (assoc key org-link-abbrev-alist-local)
  5618. (assoc key org-link-abbrev-alist)))
  5619. (tag (and (match-end 2) (match-string 3 link)))
  5620. rpl)
  5621. (if (not as)
  5622. link
  5623. (setq rpl (cdr as))
  5624. (cond
  5625. ((symbolp rpl) (funcall rpl tag))
  5626. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5627. (t (concat rpl tag)))))
  5628. link))
  5629. ;;; Storing and inserting links
  5630. (defvar org-insert-link-history nil
  5631. "Minibuffer history for links inserted with `org-insert-link'.")
  5632. (defvar org-stored-links nil
  5633. "Contains the links stored with `org-store-link'.")
  5634. (defvar org-store-link-plist nil
  5635. "Plist with info about the most recently link created with `org-store-link'.")
  5636. (defvar org-link-protocols nil
  5637. "Link protocols added to Org-mode using `org-add-link-type'.")
  5638. (defvar org-store-link-functions nil
  5639. "List of functions that are called to create and store a link.
  5640. Each function will be called in turn until one returns a non-nil
  5641. value. Each function should check if it is responsible for creating
  5642. this link (for example by looking at the major mode).
  5643. If not, it must exit and return nil.
  5644. If yes, it should return a non-nil value after a calling
  5645. `org-store-link-props' with a list of properties and values.
  5646. Special properties are:
  5647. :type The link prefix. like \"http\". This must be given.
  5648. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5649. This is obligatory as well.
  5650. :description Optional default description for the second pair
  5651. of brackets in an Org-mode link. The user can still change
  5652. this when inserting this link into an Org-mode buffer.
  5653. In addition to these, any additional properties can be specified
  5654. and then used in remember templates.")
  5655. (defun org-add-link-type (type &optional follow export)
  5656. "Add TYPE to the list of `org-link-types'.
  5657. Re-compute all regular expressions depending on `org-link-types'
  5658. FOLLOW and EXPORT are two functions.
  5659. FOLLOW should take the link path as the single argument and do whatever
  5660. is necessary to follow the link, for example find a file or display
  5661. a mail message.
  5662. EXPORT should format the link path for export to one of the export formats.
  5663. It should be a function accepting three arguments:
  5664. path the path of the link, the text after the prefix (like \"http:\")
  5665. desc the description of the link, if any, nil if there was no descripton
  5666. format the export format, a symbol like `html' or `latex'.
  5667. The function may use the FORMAT information to return different values
  5668. depending on the format. The return value will be put literally into
  5669. the exported file.
  5670. Org-mode has a built-in default for exporting links. If you are happy with
  5671. this default, there is no need to define an export function for the link
  5672. type. For a simple example of an export function, see `org-bbdb.el'."
  5673. (add-to-list 'org-link-types type t)
  5674. (org-make-link-regexps)
  5675. (if (assoc type org-link-protocols)
  5676. (setcdr (assoc type org-link-protocols) (list follow export))
  5677. (push (list type follow export) org-link-protocols)))
  5678. ;;;###autoload
  5679. (defun org-store-link (arg)
  5680. "\\<org-mode-map>Store an org-link to the current location.
  5681. This link is added to `org-stored-links' and can later be inserted
  5682. into an org-buffer with \\[org-insert-link].
  5683. For some link types, a prefix arg is interpreted:
  5684. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5685. For file links, arg negates `org-context-in-file-links'."
  5686. (interactive "P")
  5687. (org-load-modules-maybe)
  5688. (setq org-store-link-plist nil) ; reset
  5689. (let (link cpltxt desc description search txt)
  5690. (cond
  5691. ((run-hook-with-args-until-success 'org-store-link-functions)
  5692. (setq link (plist-get org-store-link-plist :link)
  5693. desc (or (plist-get org-store-link-plist :description) link)))
  5694. ((eq major-mode 'calendar-mode)
  5695. (let ((cd (calendar-cursor-to-date)))
  5696. (setq link
  5697. (format-time-string
  5698. (car org-time-stamp-formats)
  5699. (apply 'encode-time
  5700. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5701. nil nil nil))))
  5702. (org-store-link-props :type "calendar" :date cd)))
  5703. ((eq major-mode 'w3-mode)
  5704. (setq cpltxt (url-view-url t)
  5705. link (org-make-link cpltxt))
  5706. (org-store-link-props :type "w3" :url (url-view-url t)))
  5707. ((eq major-mode 'w3m-mode)
  5708. (setq cpltxt (or w3m-current-title w3m-current-url)
  5709. link (org-make-link w3m-current-url))
  5710. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5711. ((setq search (run-hook-with-args-until-success
  5712. 'org-create-file-search-functions))
  5713. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5714. "::" search))
  5715. (setq cpltxt (or description link)))
  5716. ((eq major-mode 'image-mode)
  5717. (setq cpltxt (concat "file:"
  5718. (abbreviate-file-name buffer-file-name))
  5719. link (org-make-link cpltxt))
  5720. (org-store-link-props :type "image" :file buffer-file-name))
  5721. ((eq major-mode 'dired-mode)
  5722. ;; link to the file in the current line
  5723. (setq cpltxt (concat "file:"
  5724. (abbreviate-file-name
  5725. (expand-file-name
  5726. (dired-get-filename nil t))))
  5727. link (org-make-link cpltxt)))
  5728. ((and buffer-file-name (org-mode-p))
  5729. ;; Just link to current headline
  5730. (setq cpltxt (concat "file:"
  5731. (abbreviate-file-name buffer-file-name)))
  5732. ;; Add a context search string
  5733. (when (org-xor org-context-in-file-links arg)
  5734. ;; Check if we are on a target
  5735. (if (org-in-regexp "<<\\(.*?\\)>>")
  5736. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5737. (setq txt (cond
  5738. ((org-on-heading-p) nil)
  5739. ((org-region-active-p)
  5740. (buffer-substring (region-beginning) (region-end)))
  5741. (t nil)))
  5742. (when (or (null txt) (string-match "\\S-" txt))
  5743. (setq cpltxt
  5744. (concat cpltxt "::"
  5745. (condition-case nil
  5746. (org-make-org-heading-search-string txt)
  5747. (error "")))
  5748. desc "NONE"))))
  5749. (if (string-match "::\\'" cpltxt)
  5750. (setq cpltxt (substring cpltxt 0 -2)))
  5751. (setq link (org-make-link cpltxt)))
  5752. ((buffer-file-name (buffer-base-buffer))
  5753. ;; Just link to this file here.
  5754. (setq cpltxt (concat "file:"
  5755. (abbreviate-file-name
  5756. (buffer-file-name (buffer-base-buffer)))))
  5757. ;; Add a context string
  5758. (when (org-xor org-context-in-file-links arg)
  5759. (setq txt (if (org-region-active-p)
  5760. (buffer-substring (region-beginning) (region-end))
  5761. (buffer-substring (point-at-bol) (point-at-eol))))
  5762. ;; Only use search option if there is some text.
  5763. (when (string-match "\\S-" txt)
  5764. (setq cpltxt
  5765. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5766. desc "NONE")))
  5767. (setq link (org-make-link cpltxt)))
  5768. ((interactive-p)
  5769. (error "Cannot link to a buffer which is not visiting a file"))
  5770. (t (setq link nil)))
  5771. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5772. (setq link (or link cpltxt)
  5773. desc (or desc cpltxt))
  5774. (if (equal desc "NONE") (setq desc nil))
  5775. (if (and (interactive-p) link)
  5776. (progn
  5777. (setq org-stored-links
  5778. (cons (list link desc) org-stored-links))
  5779. (message "Stored: %s" (or desc link)))
  5780. (and link (org-make-link-string link desc)))))
  5781. (defun org-store-link-props (&rest plist)
  5782. "Store link properties, extract names and addresses."
  5783. (let (x adr)
  5784. (when (setq x (plist-get plist :from))
  5785. (setq adr (mail-extract-address-components x))
  5786. (plist-put plist :fromname (car adr))
  5787. (plist-put plist :fromaddress (nth 1 adr)))
  5788. (when (setq x (plist-get plist :to))
  5789. (setq adr (mail-extract-address-components x))
  5790. (plist-put plist :toname (car adr))
  5791. (plist-put plist :toaddress (nth 1 adr))))
  5792. (let ((from (plist-get plist :from))
  5793. (to (plist-get plist :to)))
  5794. (when (and from to org-from-is-user-regexp)
  5795. (plist-put plist :fromto
  5796. (if (string-match org-from-is-user-regexp from)
  5797. (concat "to %t")
  5798. (concat "from %f")))))
  5799. (setq org-store-link-plist plist))
  5800. (defun org-add-link-props (&rest plist)
  5801. "Add these properties to the link property list."
  5802. (let (key value)
  5803. (while plist
  5804. (setq key (pop plist) value (pop plist))
  5805. (setq org-store-link-plist
  5806. (plist-put org-store-link-plist key value)))))
  5807. (defun org-email-link-description (&optional fmt)
  5808. "Return the description part of an email link.
  5809. This takes information from `org-store-link-plist' and formats it
  5810. according to FMT (default from `org-email-link-description-format')."
  5811. (setq fmt (or fmt org-email-link-description-format))
  5812. (let* ((p org-store-link-plist)
  5813. (to (plist-get p :toaddress))
  5814. (from (plist-get p :fromaddress))
  5815. (table
  5816. (list
  5817. (cons "%c" (plist-get p :fromto))
  5818. (cons "%F" (plist-get p :from))
  5819. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5820. (cons "%T" (plist-get p :to))
  5821. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5822. (cons "%s" (plist-get p :subject))
  5823. (cons "%m" (plist-get p :message-id)))))
  5824. (when (string-match "%c" fmt)
  5825. ;; Check if the user wrote this message
  5826. (if (and org-from-is-user-regexp from to
  5827. (save-match-data (string-match org-from-is-user-regexp from)))
  5828. (setq fmt (replace-match "to %t" t t fmt))
  5829. (setq fmt (replace-match "from %f" t t fmt))))
  5830. (org-replace-escapes fmt table)))
  5831. (defun org-make-org-heading-search-string (&optional string heading)
  5832. "Make search string for STRING or current headline."
  5833. (interactive)
  5834. (let ((s (or string (org-get-heading))))
  5835. (unless (and string (not heading))
  5836. ;; We are using a headline, clean up garbage in there.
  5837. (if (string-match org-todo-regexp s)
  5838. (setq s (replace-match "" t t s)))
  5839. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5840. (setq s (replace-match "" t t s)))
  5841. (setq s (org-trim s))
  5842. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5843. org-comment-string "\\)") s)
  5844. (setq s (replace-match "" t t s)))
  5845. (while (string-match org-ts-regexp s)
  5846. (setq s (replace-match "" t t s))))
  5847. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5848. (setq s (replace-match " " t t s)))
  5849. (or string (setq s (concat "*" s))) ; Add * for headlines
  5850. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5851. (defun org-make-link (&rest strings)
  5852. "Concatenate STRINGS."
  5853. (apply 'concat strings))
  5854. (defun org-make-link-string (link &optional description)
  5855. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5856. (unless (string-match "\\S-" link)
  5857. (error "Empty link"))
  5858. (when (stringp description)
  5859. ;; Remove brackets from the description, they are fatal.
  5860. (while (string-match "\\[" description)
  5861. (setq description (replace-match "{" t t description)))
  5862. (while (string-match "\\]" description)
  5863. (setq description (replace-match "}" t t description))))
  5864. (when (equal (org-link-escape link) description)
  5865. ;; No description needed, it is identical
  5866. (setq description nil))
  5867. (when (and (not description)
  5868. (not (equal link (org-link-escape link))))
  5869. (setq description link))
  5870. (concat "[[" (org-link-escape link) "]"
  5871. (if description (concat "[" description "]") "")
  5872. "]"))
  5873. (defconst org-link-escape-chars
  5874. '((?\ . "%20")
  5875. (?\[ . "%5B")
  5876. (?\] . "%5D")
  5877. (?\340 . "%E0") ; `a
  5878. (?\342 . "%E2") ; ^a
  5879. (?\347 . "%E7") ; ,c
  5880. (?\350 . "%E8") ; `e
  5881. (?\351 . "%E9") ; 'e
  5882. (?\352 . "%EA") ; ^e
  5883. (?\356 . "%EE") ; ^i
  5884. (?\364 . "%F4") ; ^o
  5885. (?\371 . "%F9") ; `u
  5886. (?\373 . "%FB") ; ^u
  5887. (?\; . "%3B")
  5888. (?? . "%3F")
  5889. (?= . "%3D")
  5890. (?+ . "%2B")
  5891. )
  5892. "Association list of escapes for some characters problematic in links.
  5893. This is the list that is used for internal purposes.")
  5894. (defconst org-link-escape-chars-browser
  5895. '((?\ . "%20")) ; 32 for the SPC char
  5896. "Association list of escapes for some characters problematic in links.
  5897. This is the list that is used before handing over to the browser.")
  5898. (defun org-link-escape (text &optional table)
  5899. "Escape charaters in TEXT that are problematic for links."
  5900. (setq table (or table org-link-escape-chars))
  5901. (when text
  5902. (let ((re (mapconcat (lambda (x) (regexp-quote
  5903. (char-to-string (car x))))
  5904. table "\\|")))
  5905. (while (string-match re text)
  5906. (setq text
  5907. (replace-match
  5908. (cdr (assoc (string-to-char (match-string 0 text))
  5909. table))
  5910. t t text)))
  5911. text)))
  5912. (defun org-link-unescape (text &optional table)
  5913. "Reverse the action of `org-link-escape'."
  5914. (setq table (or table org-link-escape-chars))
  5915. (when text
  5916. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5917. table "\\|")))
  5918. (while (string-match re text)
  5919. (setq text
  5920. (replace-match
  5921. (char-to-string (car (rassoc (match-string 0 text) table)))
  5922. t t text)))
  5923. text)))
  5924. (defun org-xor (a b)
  5925. "Exclusive or."
  5926. (if a (not b) b))
  5927. (defun org-get-header (header)
  5928. "Find a header field in the current buffer."
  5929. (save-excursion
  5930. (goto-char (point-min))
  5931. (let ((case-fold-search t) s)
  5932. (cond
  5933. ((eq header 'from)
  5934. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5935. (setq s (match-string 1)))
  5936. (while (string-match "\"" s)
  5937. (setq s (replace-match "" t t s)))
  5938. (if (string-match "[<(].*" s)
  5939. (setq s (replace-match "" t t s))))
  5940. ((eq header 'message-id)
  5941. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5942. (setq s (match-string 1))))
  5943. ((eq header 'subject)
  5944. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5945. (setq s (match-string 1)))))
  5946. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5947. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5948. s)))
  5949. (defun org-fixup-message-id-for-http (s)
  5950. "Replace special characters in a message id, so it can be used in an http query."
  5951. (while (string-match "<" s)
  5952. (setq s (replace-match "%3C" t t s)))
  5953. (while (string-match ">" s)
  5954. (setq s (replace-match "%3E" t t s)))
  5955. (while (string-match "@" s)
  5956. (setq s (replace-match "%40" t t s)))
  5957. s)
  5958. ;;;###autoload
  5959. (defun org-insert-link-global ()
  5960. "Insert a link like Org-mode does.
  5961. This command can be called in any mode to insert a link in Org-mode syntax."
  5962. (interactive)
  5963. (org-load-modules-maybe)
  5964. (org-run-like-in-org-mode 'org-insert-link))
  5965. (defun org-insert-link (&optional complete-file link-location)
  5966. "Insert a link. At the prompt, enter the link.
  5967. Completion can be used to select a link previously stored with
  5968. `org-store-link'. When the empty string is entered (i.e. if you just
  5969. press RET at the prompt), the link defaults to the most recently
  5970. stored link. As SPC triggers completion in the minibuffer, you need to
  5971. use M-SPC or C-q SPC to force the insertion of a space character.
  5972. You will also be prompted for a description, and if one is given, it will
  5973. be displayed in the buffer instead of the link.
  5974. If there is already a link at point, this command will allow you to edit link
  5975. and description parts.
  5976. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5977. be selected using completion. The path to the file will be relative to the
  5978. current directory if the file is in the current directory or a subdirectory.
  5979. Otherwise, the link will be the absolute path as completed in the minibuffer
  5980. \(i.e. normally ~/path/to/file).
  5981. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5982. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5983. of `org-keep-stored-link-after-insertion'.
  5984. If `org-make-link-description-function' is non-nil, this function will be
  5985. called with the link target, and the result will be the default
  5986. link description.
  5987. If the LINK-LOCATION parameter is non-nil, this value will be
  5988. used as the link location instead of reading one interactively."
  5989. (interactive "P")
  5990. (let* ((wcf (current-window-configuration))
  5991. (region (if (org-region-active-p)
  5992. (buffer-substring (region-beginning) (region-end))))
  5993. (remove (and region (list (region-beginning) (region-end))))
  5994. (desc region)
  5995. tmphist ; byte-compile incorrectly complains about this
  5996. (link link-location)
  5997. entry file)
  5998. (cond
  5999. (link-location) ; specified by arg, just use it.
  6000. ((org-in-regexp org-bracket-link-regexp 1)
  6001. ;; We do have a link at point, and we are going to edit it.
  6002. (setq remove (list (match-beginning 0) (match-end 0)))
  6003. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6004. (setq link (read-string "Link: "
  6005. (org-link-unescape
  6006. (org-match-string-no-properties 1)))))
  6007. ((or (org-in-regexp org-angle-link-re)
  6008. (org-in-regexp org-plain-link-re))
  6009. ;; Convert to bracket link
  6010. (setq remove (list (match-beginning 0) (match-end 0))
  6011. link (read-string "Link: "
  6012. (org-remove-angle-brackets (match-string 0)))))
  6013. ((equal complete-file '(4))
  6014. ;; Completing read for file names.
  6015. (setq file (read-file-name "File: "))
  6016. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6017. (pwd1 (file-name-as-directory (abbreviate-file-name
  6018. (expand-file-name ".")))))
  6019. (cond
  6020. ((equal complete-file '(16))
  6021. (setq link (org-make-link
  6022. "file:"
  6023. (abbreviate-file-name (expand-file-name file)))))
  6024. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6025. (setq link (org-make-link "file:" (match-string 1 file))))
  6026. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6027. (expand-file-name file))
  6028. (setq link (org-make-link
  6029. "file:" (match-string 1 (expand-file-name file)))))
  6030. (t (setq link (org-make-link "file:" file))))))
  6031. (t
  6032. ;; Read link, with completion for stored links.
  6033. (with-output-to-temp-buffer "*Org Links*"
  6034. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6035. (when org-stored-links
  6036. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6037. (princ (mapconcat
  6038. (lambda (x)
  6039. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6040. (reverse org-stored-links) "\n"))))
  6041. (let ((cw (selected-window)))
  6042. (select-window (get-buffer-window "*Org Links*"))
  6043. (shrink-window-if-larger-than-buffer)
  6044. (setq truncate-lines t)
  6045. (select-window cw))
  6046. ;; Fake a link history, containing the stored links.
  6047. (setq tmphist (append (mapcar 'car org-stored-links)
  6048. org-insert-link-history))
  6049. (unwind-protect
  6050. (setq link (org-completing-read
  6051. "Link: "
  6052. (append
  6053. (mapcar (lambda (x) (list (concat (car x) ":")))
  6054. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6055. (mapcar (lambda (x) (list (concat x ":")))
  6056. org-link-types))
  6057. nil nil nil
  6058. 'tmphist
  6059. (or (car (car org-stored-links)))))
  6060. (set-window-configuration wcf)
  6061. (kill-buffer "*Org Links*"))
  6062. (setq entry (assoc link org-stored-links))
  6063. (or entry (push link org-insert-link-history))
  6064. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6065. (not org-keep-stored-link-after-insertion))
  6066. (setq org-stored-links (delq (assoc link org-stored-links)
  6067. org-stored-links)))
  6068. (setq desc (or desc (nth 1 entry)))))
  6069. (if (string-match org-plain-link-re link)
  6070. ;; URL-like link, normalize the use of angular brackets.
  6071. (setq link (org-make-link (org-remove-angle-brackets link))))
  6072. ;; Check if we are linking to the current file with a search option
  6073. ;; If yes, simplify the link by using only the search option.
  6074. (when (and buffer-file-name
  6075. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6076. (let* ((path (match-string 1 link))
  6077. (case-fold-search nil)
  6078. (search (match-string 2 link)))
  6079. (save-match-data
  6080. (if (equal (file-truename buffer-file-name) (file-truename path))
  6081. ;; We are linking to this same file, with a search option
  6082. (setq link search)))))
  6083. ;; Check if we can/should use a relative path. If yes, simplify the link
  6084. (when (string-match "\\<file:\\(.*\\)" link)
  6085. (let* ((path (match-string 1 link))
  6086. (origpath path)
  6087. (case-fold-search nil))
  6088. (cond
  6089. ((eq org-link-file-path-type 'absolute)
  6090. (setq path (abbreviate-file-name (expand-file-name path))))
  6091. ((eq org-link-file-path-type 'noabbrev)
  6092. (setq path (expand-file-name path)))
  6093. ((eq org-link-file-path-type 'relative)
  6094. (setq path (file-relative-name path)))
  6095. (t
  6096. (save-match-data
  6097. (if (string-match (concat "^" (regexp-quote
  6098. (file-name-as-directory
  6099. (expand-file-name "."))))
  6100. (expand-file-name path))
  6101. ;; We are linking a file with relative path name.
  6102. (setq path (substring (expand-file-name path)
  6103. (match-end 0)))))))
  6104. (setq link (concat "file:" path))
  6105. (if (equal desc origpath)
  6106. (setq desc path))))
  6107. (if org-make-link-description-function
  6108. (setq desc (funcall org-make-link-description-function link desc)))
  6109. (setq desc (read-string "Description: " desc))
  6110. (unless (string-match "\\S-" desc) (setq desc nil))
  6111. (if remove (apply 'delete-region remove))
  6112. (insert (org-make-link-string link desc))))
  6113. (defun org-completing-read (&rest args)
  6114. (let ((minibuffer-local-completion-map
  6115. (copy-keymap minibuffer-local-completion-map)))
  6116. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6117. (apply 'completing-read args)))
  6118. ;;; Opening/following a link
  6119. (defvar org-link-search-failed nil)
  6120. (defun org-next-link ()
  6121. "Move forward to the next link.
  6122. If the link is in hidden text, expose it."
  6123. (interactive)
  6124. (when (and org-link-search-failed (eq this-command last-command))
  6125. (goto-char (point-min))
  6126. (message "Link search wrapped back to beginning of buffer"))
  6127. (setq org-link-search-failed nil)
  6128. (let* ((pos (point))
  6129. (ct (org-context))
  6130. (a (assoc :link ct)))
  6131. (if a (goto-char (nth 2 a)))
  6132. (if (re-search-forward org-any-link-re nil t)
  6133. (progn
  6134. (goto-char (match-beginning 0))
  6135. (if (org-invisible-p) (org-show-context)))
  6136. (goto-char pos)
  6137. (setq org-link-search-failed t)
  6138. (error "No further link found"))))
  6139. (defun org-previous-link ()
  6140. "Move backward to the previous link.
  6141. If the link is in hidden text, expose it."
  6142. (interactive)
  6143. (when (and org-link-search-failed (eq this-command last-command))
  6144. (goto-char (point-max))
  6145. (message "Link search wrapped back to end of buffer"))
  6146. (setq org-link-search-failed nil)
  6147. (let* ((pos (point))
  6148. (ct (org-context))
  6149. (a (assoc :link ct)))
  6150. (if a (goto-char (nth 1 a)))
  6151. (if (re-search-backward org-any-link-re nil t)
  6152. (progn
  6153. (goto-char (match-beginning 0))
  6154. (if (org-invisible-p) (org-show-context)))
  6155. (goto-char pos)
  6156. (setq org-link-search-failed t)
  6157. (error "No further link found"))))
  6158. (defun org-find-file-at-mouse (ev)
  6159. "Open file link or URL at mouse."
  6160. (interactive "e")
  6161. (mouse-set-point ev)
  6162. (org-open-at-point 'in-emacs))
  6163. (defun org-open-at-mouse (ev)
  6164. "Open file link or URL at mouse."
  6165. (interactive "e")
  6166. (mouse-set-point ev)
  6167. (org-open-at-point))
  6168. (defvar org-window-config-before-follow-link nil
  6169. "The window configuration before following a link.
  6170. This is saved in case the need arises to restore it.")
  6171. (defvar org-open-link-marker (make-marker)
  6172. "Marker pointing to the location where `org-open-at-point; was called.")
  6173. ;;;###autoload
  6174. (defun org-open-at-point-global ()
  6175. "Follow a link like Org-mode does.
  6176. This command can be called in any mode to follow a link that has
  6177. Org-mode syntax."
  6178. (interactive)
  6179. (org-run-like-in-org-mode 'org-open-at-point))
  6180. ;;;###autoload
  6181. (defun org-open-link-from-string (s &optional arg)
  6182. "Open a link in the string S, as if it was in Org-mode."
  6183. (interactive "sLink: \nP")
  6184. (with-temp-buffer
  6185. (let ((org-inhibit-startup t))
  6186. (org-mode)
  6187. (insert s)
  6188. (goto-char (point-min))
  6189. (org-open-at-point arg))))
  6190. (defun org-open-at-point (&optional in-emacs)
  6191. "Open link at or after point.
  6192. If there is no link at point, this function will search forward up to
  6193. the end of the current subtree.
  6194. Normally, files will be opened by an appropriate application. If the
  6195. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6196. (interactive "P")
  6197. (org-load-modules-maybe)
  6198. (move-marker org-open-link-marker (point))
  6199. (setq org-window-config-before-follow-link (current-window-configuration))
  6200. (org-remove-occur-highlights nil nil t)
  6201. (if (org-at-timestamp-p t)
  6202. (org-follow-timestamp-link)
  6203. (let (type path link line search (pos (point)))
  6204. (catch 'match
  6205. (save-excursion
  6206. (skip-chars-forward "^]\n\r")
  6207. (when (org-in-regexp org-bracket-link-regexp)
  6208. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6209. (while (string-match " *\n *" link)
  6210. (setq link (replace-match " " t t link)))
  6211. (setq link (org-link-expand-abbrev link))
  6212. (if (string-match org-link-re-with-space2 link)
  6213. (setq type (match-string 1 link) path (match-string 2 link))
  6214. (setq type "thisfile" path link))
  6215. (throw 'match t)))
  6216. (when (get-text-property (point) 'org-linked-text)
  6217. (setq type "thisfile"
  6218. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6219. (1+ (point)) (point))
  6220. path (buffer-substring
  6221. (previous-single-property-change pos 'org-linked-text)
  6222. (next-single-property-change pos 'org-linked-text)))
  6223. (throw 'match t))
  6224. (save-excursion
  6225. (when (or (org-in-regexp org-angle-link-re)
  6226. (org-in-regexp org-plain-link-re))
  6227. (setq type (match-string 1) path (match-string 2))
  6228. (throw 'match t)))
  6229. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6230. (setq type "tree-match"
  6231. path (match-string 1))
  6232. (throw 'match t))
  6233. (save-excursion
  6234. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6235. (setq type "tags"
  6236. path (match-string 1))
  6237. (while (string-match ":" path)
  6238. (setq path (replace-match "+" t t path)))
  6239. (throw 'match t))))
  6240. (unless path
  6241. (error "No link found"))
  6242. ;; Remove any trailing spaces in path
  6243. (if (string-match " +\\'" path)
  6244. (setq path (replace-match "" t t path)))
  6245. (cond
  6246. ((assoc type org-link-protocols)
  6247. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6248. ((equal type "mailto")
  6249. (let ((cmd (car org-link-mailto-program))
  6250. (args (cdr org-link-mailto-program)) args1
  6251. (address path) (subject "") a)
  6252. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6253. (setq address (match-string 1 path)
  6254. subject (org-link-escape (match-string 2 path))))
  6255. (while args
  6256. (cond
  6257. ((not (stringp (car args))) (push (pop args) args1))
  6258. (t (setq a (pop args))
  6259. (if (string-match "%a" a)
  6260. (setq a (replace-match address t t a)))
  6261. (if (string-match "%s" a)
  6262. (setq a (replace-match subject t t a)))
  6263. (push a args1))))
  6264. (apply cmd (nreverse args1))))
  6265. ((member type '("http" "https" "ftp" "news"))
  6266. (browse-url (concat type ":" (org-link-escape
  6267. path org-link-escape-chars-browser))))
  6268. ((member type '("message"))
  6269. (browse-url (concat type ":" path)))
  6270. ((string= type "tags")
  6271. (org-tags-view in-emacs path))
  6272. ((string= type "thisfile")
  6273. (if in-emacs
  6274. (switch-to-buffer-other-window
  6275. (org-get-buffer-for-internal-link (current-buffer)))
  6276. (org-mark-ring-push))
  6277. (let ((cmd `(org-link-search
  6278. ,path
  6279. ,(cond ((equal in-emacs '(4)) 'occur)
  6280. ((equal in-emacs '(16)) 'org-occur)
  6281. (t nil))
  6282. ,pos)))
  6283. (condition-case nil (eval cmd)
  6284. (error (progn (widen) (eval cmd))))))
  6285. ((string= type "tree-match")
  6286. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6287. ((string= type "file")
  6288. (if (string-match "::\\([0-9]+\\)\\'" path)
  6289. (setq line (string-to-number (match-string 1 path))
  6290. path (substring path 0 (match-beginning 0)))
  6291. (if (string-match "::\\(.+\\)\\'" path)
  6292. (setq search (match-string 1 path)
  6293. path (substring path 0 (match-beginning 0)))))
  6294. (if (string-match "[*?{]" (file-name-nondirectory path))
  6295. (dired path)
  6296. (org-open-file path in-emacs line search)))
  6297. ((string= type "news")
  6298. (require 'org-gnus)
  6299. (org-gnus-follow-link path))
  6300. ((string= type "shell")
  6301. (let ((cmd path))
  6302. (if (or (not org-confirm-shell-link-function)
  6303. (funcall org-confirm-shell-link-function
  6304. (format "Execute \"%s\" in shell? "
  6305. (org-add-props cmd nil
  6306. 'face 'org-warning))))
  6307. (progn
  6308. (message "Executing %s" cmd)
  6309. (shell-command cmd))
  6310. (error "Abort"))))
  6311. ((string= type "elisp")
  6312. (let ((cmd path))
  6313. (if (or (not org-confirm-elisp-link-function)
  6314. (funcall org-confirm-elisp-link-function
  6315. (format "Execute \"%s\" as elisp? "
  6316. (org-add-props cmd nil
  6317. 'face 'org-warning))))
  6318. (message "%s => %s" cmd (eval (read cmd)))
  6319. (error "Abort"))))
  6320. (t
  6321. (browse-url-at-point)))))
  6322. (move-marker org-open-link-marker nil)
  6323. (run-hook-with-args 'org-follow-link-hook))
  6324. ;;;; Time estimates
  6325. (defun org-get-effort (&optional pom)
  6326. "Get the effort estimate for the current entry."
  6327. (org-entry-get pom org-effort-property))
  6328. ;;; File search
  6329. (defvar org-create-file-search-functions nil
  6330. "List of functions to construct the right search string for a file link.
  6331. These functions are called in turn with point at the location to
  6332. which the link should point.
  6333. A function in the hook should first test if it would like to
  6334. handle this file type, for example by checking the major-mode or
  6335. the file extension. If it decides not to handle this file, it
  6336. should just return nil to give other functions a chance. If it
  6337. does handle the file, it must return the search string to be used
  6338. when following the link. The search string will be part of the
  6339. file link, given after a double colon, and `org-open-at-point'
  6340. will automatically search for it. If special measures must be
  6341. taken to make the search successful, another function should be
  6342. added to the companion hook `org-execute-file-search-functions',
  6343. which see.
  6344. A function in this hook may also use `setq' to set the variable
  6345. `description' to provide a suggestion for the descriptive text to
  6346. be used for this link when it gets inserted into an Org-mode
  6347. buffer with \\[org-insert-link].")
  6348. (defvar org-execute-file-search-functions nil
  6349. "List of functions to execute a file search triggered by a link.
  6350. Functions added to this hook must accept a single argument, the
  6351. search string that was part of the file link, the part after the
  6352. double colon. The function must first check if it would like to
  6353. handle this search, for example by checking the major-mode or the
  6354. file extension. If it decides not to handle this search, it
  6355. should just return nil to give other functions a chance. If it
  6356. does handle the search, it must return a non-nil value to keep
  6357. other functions from trying.
  6358. Each function can access the current prefix argument through the
  6359. variable `current-prefix-argument'. Note that a single prefix is
  6360. used to force opening a link in Emacs, so it may be good to only
  6361. use a numeric or double prefix to guide the search function.
  6362. In case this is needed, a function in this hook can also restore
  6363. the window configuration before `org-open-at-point' was called using:
  6364. (set-window-configuration org-window-config-before-follow-link)")
  6365. (defun org-link-search (s &optional type avoid-pos)
  6366. "Search for a link search option.
  6367. If S is surrounded by forward slashes, it is interpreted as a
  6368. regular expression. In org-mode files, this will create an `org-occur'
  6369. sparse tree. In ordinary files, `occur' will be used to list matches.
  6370. If the current buffer is in `dired-mode', grep will be used to search
  6371. in all files. If AVOID-POS is given, ignore matches near that position."
  6372. (let ((case-fold-search t)
  6373. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6374. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6375. (append '(("") (" ") ("\t") ("\n"))
  6376. org-emphasis-alist)
  6377. "\\|") "\\)"))
  6378. (pos (point))
  6379. (pre nil) (post nil)
  6380. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6381. (cond
  6382. ;; First check if there are any special
  6383. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6384. ;; Now try the builtin stuff
  6385. ((save-excursion
  6386. (goto-char (point-min))
  6387. (and
  6388. (re-search-forward
  6389. (concat "<<" (regexp-quote s0) ">>") nil t)
  6390. (setq type 'dedicated
  6391. pos (match-beginning 0))))
  6392. ;; There is an exact target for this
  6393. (goto-char pos))
  6394. ((string-match "^/\\(.*\\)/$" s)
  6395. ;; A regular expression
  6396. (cond
  6397. ((org-mode-p)
  6398. (org-occur (match-string 1 s)))
  6399. ;;((eq major-mode 'dired-mode)
  6400. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6401. (t (org-do-occur (match-string 1 s)))))
  6402. (t
  6403. ;; A normal search strings
  6404. (when (equal (string-to-char s) ?*)
  6405. ;; Anchor on headlines, post may include tags.
  6406. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6407. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6408. s (substring s 1)))
  6409. (remove-text-properties
  6410. 0 (length s)
  6411. '(face nil mouse-face nil keymap nil fontified nil) s)
  6412. ;; Make a series of regular expressions to find a match
  6413. (setq words (org-split-string s "[ \n\r\t]+")
  6414. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6415. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6416. "\\)" markers)
  6417. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6418. re2a (concat "[ \t\r\n]" re2a_)
  6419. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6420. re4 (concat "[^a-zA-Z_]" re4_)
  6421. re1 (concat pre re2 post)
  6422. re3 (concat pre (if pre re4_ re4) post)
  6423. re5 (concat pre ".*" re4)
  6424. re2 (concat pre re2)
  6425. re2a (concat pre (if pre re2a_ re2a))
  6426. re4 (concat pre (if pre re4_ re4))
  6427. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6428. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6429. re5 "\\)"
  6430. ))
  6431. (cond
  6432. ((eq type 'org-occur) (org-occur reall))
  6433. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6434. (t (goto-char (point-min))
  6435. (setq type 'fuzzy)
  6436. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6437. (org-search-not-self 1 re1 nil t)
  6438. (org-search-not-self 1 re2 nil t)
  6439. (org-search-not-self 1 re2a nil t)
  6440. (org-search-not-self 1 re3 nil t)
  6441. (org-search-not-self 1 re4 nil t)
  6442. (org-search-not-self 1 re5 nil t)
  6443. )
  6444. (goto-char (match-beginning 1))
  6445. (goto-char pos)
  6446. (error "No match")))))
  6447. (t
  6448. ;; Normal string-search
  6449. (goto-char (point-min))
  6450. (if (search-forward s nil t)
  6451. (goto-char (match-beginning 0))
  6452. (error "No match"))))
  6453. (and (org-mode-p) (org-show-context 'link-search))
  6454. type))
  6455. (defun org-search-not-self (group &rest args)
  6456. "Execute `re-search-forward', but only accept matches that do not
  6457. enclose the position of `org-open-link-marker'."
  6458. (let ((m org-open-link-marker))
  6459. (catch 'exit
  6460. (while (apply 're-search-forward args)
  6461. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6462. (goto-char (match-end group))
  6463. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6464. (> (match-beginning 0) (marker-position m))
  6465. (< (match-end 0) (marker-position m)))
  6466. (save-match-data
  6467. (or (not (org-in-regexp
  6468. org-bracket-link-analytic-regexp 1))
  6469. (not (match-end 4)) ; no description
  6470. (and (<= (match-beginning 4) (point))
  6471. (>= (match-end 4) (point))))))
  6472. (throw 'exit (point))))))))
  6473. (defun org-get-buffer-for-internal-link (buffer)
  6474. "Return a buffer to be used for displaying the link target of internal links."
  6475. (cond
  6476. ((not org-display-internal-link-with-indirect-buffer)
  6477. buffer)
  6478. ((string-match "(Clone)$" (buffer-name buffer))
  6479. (message "Buffer is already a clone, not making another one")
  6480. ;; we also do not modify visibility in this case
  6481. buffer)
  6482. (t ; make a new indirect buffer for displaying the link
  6483. (let* ((bn (buffer-name buffer))
  6484. (ibn (concat bn "(Clone)"))
  6485. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6486. (with-current-buffer ib (org-overview))
  6487. ib))))
  6488. (defun org-do-occur (regexp &optional cleanup)
  6489. "Call the Emacs command `occur'.
  6490. If CLEANUP is non-nil, remove the printout of the regular expression
  6491. in the *Occur* buffer. This is useful if the regex is long and not useful
  6492. to read."
  6493. (occur regexp)
  6494. (when cleanup
  6495. (let ((cwin (selected-window)) win beg end)
  6496. (when (setq win (get-buffer-window "*Occur*"))
  6497. (select-window win))
  6498. (goto-char (point-min))
  6499. (when (re-search-forward "match[a-z]+" nil t)
  6500. (setq beg (match-end 0))
  6501. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6502. (setq end (1- (match-beginning 0)))))
  6503. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6504. (goto-char (point-min))
  6505. (select-window cwin))))
  6506. ;;; The mark ring for links jumps
  6507. (defvar org-mark-ring nil
  6508. "Mark ring for positions before jumps in Org-mode.")
  6509. (defvar org-mark-ring-last-goto nil
  6510. "Last position in the mark ring used to go back.")
  6511. ;; Fill and close the ring
  6512. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6513. (loop for i from 1 to org-mark-ring-length do
  6514. (push (make-marker) org-mark-ring))
  6515. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6516. org-mark-ring)
  6517. (defun org-mark-ring-push (&optional pos buffer)
  6518. "Put the current position or POS into the mark ring and rotate it."
  6519. (interactive)
  6520. (setq pos (or pos (point)))
  6521. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6522. (move-marker (car org-mark-ring)
  6523. (or pos (point))
  6524. (or buffer (current-buffer)))
  6525. (message "%s"
  6526. (substitute-command-keys
  6527. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6528. (defun org-mark-ring-goto (&optional n)
  6529. "Jump to the previous position in the mark ring.
  6530. With prefix arg N, jump back that many stored positions. When
  6531. called several times in succession, walk through the entire ring.
  6532. Org-mode commands jumping to a different position in the current file,
  6533. or to another Org-mode file, automatically push the old position
  6534. onto the ring."
  6535. (interactive "p")
  6536. (let (p m)
  6537. (if (eq last-command this-command)
  6538. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6539. (setq p org-mark-ring))
  6540. (setq org-mark-ring-last-goto p)
  6541. (setq m (car p))
  6542. (switch-to-buffer (marker-buffer m))
  6543. (goto-char m)
  6544. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6545. (defun org-remove-angle-brackets (s)
  6546. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6547. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6548. s)
  6549. (defun org-add-angle-brackets (s)
  6550. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6551. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6552. s)
  6553. (defun org-remove-double-quotes (s)
  6554. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6555. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6556. s)
  6557. ;;; Following specific links
  6558. (defun org-follow-timestamp-link ()
  6559. (cond
  6560. ((org-at-date-range-p t)
  6561. (let ((org-agenda-start-on-weekday)
  6562. (t1 (match-string 1))
  6563. (t2 (match-string 2)))
  6564. (setq t1 (time-to-days (org-time-string-to-time t1))
  6565. t2 (time-to-days (org-time-string-to-time t2)))
  6566. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6567. ((org-at-timestamp-p t)
  6568. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6569. (substring (match-string 1) 0 10)))
  6570. 1))
  6571. (t (error "This should not happen"))))
  6572. ;;; Following file links
  6573. (defvar org-wait nil)
  6574. (defun org-open-file (path &optional in-emacs line search)
  6575. "Open the file at PATH.
  6576. First, this expands any special file name abbreviations. Then the
  6577. configuration variable `org-file-apps' is checked if it contains an
  6578. entry for this file type, and if yes, the corresponding command is launched.
  6579. If no application is found, Emacs simply visits the file.
  6580. With optional argument IN-EMACS, Emacs will visit the file.
  6581. Optional LINE specifies a line to go to, optional SEARCH a string to
  6582. search for. If LINE or SEARCH is given, the file will always be
  6583. opened in Emacs.
  6584. If the file does not exist, an error is thrown."
  6585. (setq in-emacs (or in-emacs line search))
  6586. (let* ((file (if (equal path "")
  6587. buffer-file-name
  6588. (substitute-in-file-name (expand-file-name path))))
  6589. (apps (append org-file-apps (org-default-apps)))
  6590. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6591. (dirp (if remp nil (file-directory-p file)))
  6592. (dfile (downcase file))
  6593. (old-buffer (current-buffer))
  6594. (old-pos (point))
  6595. (old-mode major-mode)
  6596. ext cmd)
  6597. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6598. (setq ext (match-string 1 dfile))
  6599. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6600. (setq ext (match-string 1 dfile))))
  6601. (if in-emacs
  6602. (setq cmd 'emacs)
  6603. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6604. (and dirp (cdr (assoc 'directory apps)))
  6605. (cdr (assoc ext apps))
  6606. (cdr (assoc t apps)))))
  6607. (when (eq cmd 'mailcap)
  6608. (require 'mailcap)
  6609. (mailcap-parse-mailcaps)
  6610. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6611. (command (mailcap-mime-info mime-type)))
  6612. (if (stringp command)
  6613. (setq cmd command)
  6614. (setq cmd 'emacs))))
  6615. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6616. (not (file-exists-p file))
  6617. (not org-open-non-existing-files))
  6618. (error "No such file: %s" file))
  6619. (cond
  6620. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6621. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6622. (while (string-match "['\"]%s['\"]" cmd)
  6623. (setq cmd (replace-match "%s" t t cmd)))
  6624. (while (string-match "%s" cmd)
  6625. (setq cmd (replace-match
  6626. (save-match-data (shell-quote-argument file))
  6627. t t cmd)))
  6628. (save-window-excursion
  6629. (start-process-shell-command cmd nil cmd)
  6630. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6631. ))
  6632. ((or (stringp cmd)
  6633. (eq cmd 'emacs))
  6634. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6635. (widen)
  6636. (if line (goto-line line)
  6637. (if search (org-link-search search))))
  6638. ((consp cmd)
  6639. (eval cmd))
  6640. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6641. (and (org-mode-p) (eq old-mode 'org-mode)
  6642. (or (not (equal old-buffer (current-buffer)))
  6643. (not (equal old-pos (point))))
  6644. (org-mark-ring-push old-pos old-buffer))))
  6645. (defun org-default-apps ()
  6646. "Return the default applications for this operating system."
  6647. (cond
  6648. ((eq system-type 'darwin)
  6649. org-file-apps-defaults-macosx)
  6650. ((eq system-type 'windows-nt)
  6651. org-file-apps-defaults-windowsnt)
  6652. (t org-file-apps-defaults-gnu)))
  6653. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6654. (defun org-file-remote-p (file)
  6655. "Test whether FILE specifies a location on a remote system.
  6656. Return non-nil if the location is indeed remote.
  6657. For example, the filename \"/user@host:/foo\" specifies a location
  6658. on the system \"/user@host:\"."
  6659. (cond ((fboundp 'file-remote-p)
  6660. (file-remote-p file))
  6661. ((fboundp 'tramp-handle-file-remote-p)
  6662. (tramp-handle-file-remote-p file))
  6663. ((and (boundp 'ange-ftp-name-format)
  6664. (string-match (car ange-ftp-name-format) file))
  6665. t)
  6666. (t nil)))
  6667. ;;;; Refiling
  6668. (defun org-get-org-file ()
  6669. "Read a filename, with default directory `org-directory'."
  6670. (let ((default (or org-default-notes-file remember-data-file)))
  6671. (read-file-name (format "File name [%s]: " default)
  6672. (file-name-as-directory org-directory)
  6673. default)))
  6674. (defun org-notes-order-reversed-p ()
  6675. "Check if the current file should receive notes in reversed order."
  6676. (cond
  6677. ((not org-reverse-note-order) nil)
  6678. ((eq t org-reverse-note-order) t)
  6679. ((not (listp org-reverse-note-order)) nil)
  6680. (t (catch 'exit
  6681. (let ((all org-reverse-note-order)
  6682. entry)
  6683. (while (setq entry (pop all))
  6684. (if (string-match (car entry) buffer-file-name)
  6685. (throw 'exit (cdr entry))))
  6686. nil)))))
  6687. (defvar org-refile-target-table nil
  6688. "The list of refile targets, created by `org-refile'.")
  6689. (defvar org-agenda-new-buffers nil
  6690. "Buffers created to visit agenda files.")
  6691. (defun org-get-refile-targets (&optional default-buffer)
  6692. "Produce a table with refile targets."
  6693. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6694. targets txt re files f desc descre)
  6695. (with-current-buffer (or default-buffer (current-buffer))
  6696. (while (setq entry (pop entries))
  6697. (setq files (car entry) desc (cdr entry))
  6698. (cond
  6699. ((null files) (setq files (list (current-buffer))))
  6700. ((eq files 'org-agenda-files)
  6701. (setq files (org-agenda-files 'unrestricted)))
  6702. ((and (symbolp files) (fboundp files))
  6703. (setq files (funcall files)))
  6704. ((and (symbolp files) (boundp files))
  6705. (setq files (symbol-value files))))
  6706. (if (stringp files) (setq files (list files)))
  6707. (cond
  6708. ((eq (car desc) :tag)
  6709. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6710. ((eq (car desc) :todo)
  6711. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6712. ((eq (car desc) :regexp)
  6713. (setq descre (cdr desc)))
  6714. ((eq (car desc) :level)
  6715. (setq descre (concat "^\\*\\{" (number-to-string
  6716. (if org-odd-levels-only
  6717. (1- (* 2 (cdr desc)))
  6718. (cdr desc)))
  6719. "\\}[ \t]")))
  6720. ((eq (car desc) :maxlevel)
  6721. (setq descre (concat "^\\*\\{1," (number-to-string
  6722. (if org-odd-levels-only
  6723. (1- (* 2 (cdr desc)))
  6724. (cdr desc)))
  6725. "\\}[ \t]")))
  6726. (t (error "Bad refiling target description %s" desc)))
  6727. (while (setq f (pop files))
  6728. (save-excursion
  6729. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6730. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6731. (save-excursion
  6732. (save-restriction
  6733. (widen)
  6734. (goto-char (point-min))
  6735. (while (re-search-forward descre nil t)
  6736. (goto-char (point-at-bol))
  6737. (when (looking-at org-complex-heading-regexp)
  6738. (setq txt (match-string 4)
  6739. re (concat "^" (regexp-quote
  6740. (buffer-substring (match-beginning 1)
  6741. (match-end 4)))))
  6742. (if (match-end 5) (setq re (concat re "[ \t]+"
  6743. (regexp-quote
  6744. (match-string 5)))))
  6745. (setq re (concat re "[ \t]*$"))
  6746. (when org-refile-use-outline-path
  6747. (setq txt (mapconcat 'identity
  6748. (append
  6749. (if (eq org-refile-use-outline-path 'file)
  6750. (list (file-name-nondirectory
  6751. (buffer-file-name (buffer-base-buffer))))
  6752. (if (eq org-refile-use-outline-path 'full-file-path)
  6753. (list (buffer-file-name (buffer-base-buffer)))))
  6754. (org-get-outline-path)
  6755. (list txt))
  6756. "/")))
  6757. (push (list txt f re (point)) targets))
  6758. (goto-char (point-at-eol))))))))
  6759. (nreverse targets))))
  6760. (defun org-get-outline-path ()
  6761. "Return the outline path to the current entry, as a list."
  6762. (let (rtn)
  6763. (save-excursion
  6764. (while (org-up-heading-safe)
  6765. (when (looking-at org-complex-heading-regexp)
  6766. (push (org-match-string-no-properties 4) rtn)))
  6767. rtn)))
  6768. (defvar org-refile-history nil
  6769. "History for refiling operations.")
  6770. (defun org-refile (&optional goto default-buffer)
  6771. "Move the entry at point to another heading.
  6772. The list of target headings is compiled using the information in
  6773. `org-refile-targets', which see. This list is created before each use
  6774. and will therefore always be up-to-date.
  6775. At the target location, the entry is filed as a subitem of the target heading.
  6776. Depending on `org-reverse-note-order', the new subitem will either be the
  6777. first of the last subitem.
  6778. With prefix arg GOTO, the command will only visit the target location,
  6779. not actually move anything.
  6780. With a double prefix `C-c C-c', go to the location where the last refiling
  6781. operation has put the subtree."
  6782. (interactive "P")
  6783. (let* ((cbuf (current-buffer))
  6784. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6785. pos it nbuf file re level reversed)
  6786. (if (equal goto '(16))
  6787. (org-refile-goto-last-stored)
  6788. (when (setq it (org-refile-get-location
  6789. (if goto "Goto: " "Refile to: ") default-buffer))
  6790. (setq file (nth 1 it)
  6791. re (nth 2 it)
  6792. pos (nth 3 it))
  6793. (setq nbuf (or (find-buffer-visiting file)
  6794. (find-file-noselect file)))
  6795. (if goto
  6796. (progn
  6797. (switch-to-buffer nbuf)
  6798. (goto-char pos)
  6799. (org-show-context 'org-goto))
  6800. (org-copy-subtree 1 nil t)
  6801. (save-excursion
  6802. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6803. (find-file-noselect file))))
  6804. (setq reversed (org-notes-order-reversed-p))
  6805. (save-excursion
  6806. (save-restriction
  6807. (widen)
  6808. (goto-char pos)
  6809. (looking-at outline-regexp)
  6810. (setq level (org-get-valid-level (funcall outline-level) 1))
  6811. (goto-char
  6812. (if reversed
  6813. (outline-next-heading)
  6814. (or (save-excursion (outline-get-next-sibling))
  6815. (org-end-of-subtree t t)
  6816. (point-max))))
  6817. (bookmark-set "org-refile-last-stored")
  6818. (org-paste-subtree level))))
  6819. (org-cut-subtree)
  6820. (setq org-markers-to-move nil)
  6821. (message "Entry refiled to \"%s\"" (car it)))))))
  6822. (defun org-refile-goto-last-stored ()
  6823. "Go to the location where the last refile was stored."
  6824. (interactive)
  6825. (bookmark-jump "org-refile-last-stored")
  6826. (message "This is the location of the last refile"))
  6827. (defun org-refile-get-location (&optional prompt default-buffer)
  6828. "Prompt the user for a refile location, using PROMPT."
  6829. (let ((org-refile-targets org-refile-targets)
  6830. (org-refile-use-outline-path org-refile-use-outline-path))
  6831. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6832. (unless org-refile-target-table
  6833. (error "No refile targets"))
  6834. (let* ((cbuf (current-buffer))
  6835. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6836. (fname (and filename (file-truename filename)))
  6837. (tbl (mapcar
  6838. (lambda (x)
  6839. (if (not (equal fname (file-truename (nth 1 x))))
  6840. (cons (concat (car x) " (" (file-name-nondirectory
  6841. (nth 1 x)) ")")
  6842. (cdr x))
  6843. x))
  6844. org-refile-target-table))
  6845. (completion-ignore-case t))
  6846. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6847. tbl)))
  6848. ;;;; Dynamic blocks
  6849. (defun org-find-dblock (name)
  6850. "Find the first dynamic block with name NAME in the buffer.
  6851. If not found, stay at current position and return nil."
  6852. (let (pos)
  6853. (save-excursion
  6854. (goto-char (point-min))
  6855. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6856. nil t)
  6857. (match-beginning 0))))
  6858. (if pos (goto-char pos))
  6859. pos))
  6860. (defconst org-dblock-start-re
  6861. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6862. "Matches the startline of a dynamic block, with parameters.")
  6863. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6864. "Matches the end of a dyhamic block.")
  6865. (defun org-create-dblock (plist)
  6866. "Create a dynamic block section, with parameters taken from PLIST.
  6867. PLIST must containe a :name entry which is used as name of the block."
  6868. (unless (bolp) (newline))
  6869. (let ((name (plist-get plist :name)))
  6870. (insert "#+BEGIN: " name)
  6871. (while plist
  6872. (if (eq (car plist) :name)
  6873. (setq plist (cddr plist))
  6874. (insert " " (prin1-to-string (pop plist)))))
  6875. (insert "\n\n#+END:\n")
  6876. (beginning-of-line -2)))
  6877. (defun org-prepare-dblock ()
  6878. "Prepare dynamic block for refresh.
  6879. This empties the block, puts the cursor at the insert position and returns
  6880. the property list including an extra property :name with the block name."
  6881. (unless (looking-at org-dblock-start-re)
  6882. (error "Not at a dynamic block"))
  6883. (let* ((begdel (1+ (match-end 0)))
  6884. (name (org-no-properties (match-string 1)))
  6885. (params (append (list :name name)
  6886. (read (concat "(" (match-string 3) ")")))))
  6887. (unless (re-search-forward org-dblock-end-re nil t)
  6888. (error "Dynamic block not terminated"))
  6889. (setq params
  6890. (append params
  6891. (list :content (buffer-substring
  6892. begdel (match-beginning 0)))))
  6893. (delete-region begdel (match-beginning 0))
  6894. (goto-char begdel)
  6895. (open-line 1)
  6896. params))
  6897. (defun org-map-dblocks (&optional command)
  6898. "Apply COMMAND to all dynamic blocks in the current buffer.
  6899. If COMMAND is not given, use `org-update-dblock'."
  6900. (let ((cmd (or command 'org-update-dblock))
  6901. pos)
  6902. (save-excursion
  6903. (goto-char (point-min))
  6904. (while (re-search-forward org-dblock-start-re nil t)
  6905. (goto-char (setq pos (match-beginning 0)))
  6906. (condition-case nil
  6907. (funcall cmd)
  6908. (error (message "Error during update of dynamic block")))
  6909. (goto-char pos)
  6910. (unless (re-search-forward org-dblock-end-re nil t)
  6911. (error "Dynamic block not terminated"))))))
  6912. (defun org-dblock-update (&optional arg)
  6913. "User command for updating dynamic blocks.
  6914. Update the dynamic block at point. With prefix ARG, update all dynamic
  6915. blocks in the buffer."
  6916. (interactive "P")
  6917. (if arg
  6918. (org-update-all-dblocks)
  6919. (or (looking-at org-dblock-start-re)
  6920. (org-beginning-of-dblock))
  6921. (org-update-dblock)))
  6922. (defun org-update-dblock ()
  6923. "Update the dynamic block at point
  6924. This means to empty the block, parse for parameters and then call
  6925. the correct writing function."
  6926. (save-window-excursion
  6927. (let* ((pos (point))
  6928. (line (org-current-line))
  6929. (params (org-prepare-dblock))
  6930. (name (plist-get params :name))
  6931. (cmd (intern (concat "org-dblock-write:" name))))
  6932. (message "Updating dynamic block `%s' at line %d..." name line)
  6933. (funcall cmd params)
  6934. (message "Updating dynamic block `%s' at line %d...done" name line)
  6935. (goto-char pos))))
  6936. (defun org-beginning-of-dblock ()
  6937. "Find the beginning of the dynamic block at point.
  6938. Error if there is no scuh block at point."
  6939. (let ((pos (point))
  6940. beg)
  6941. (end-of-line 1)
  6942. (if (and (re-search-backward org-dblock-start-re nil t)
  6943. (setq beg (match-beginning 0))
  6944. (re-search-forward org-dblock-end-re nil t)
  6945. (> (match-end 0) pos))
  6946. (goto-char beg)
  6947. (goto-char pos)
  6948. (error "Not in a dynamic block"))))
  6949. (defun org-update-all-dblocks ()
  6950. "Update all dynamic blocks in the buffer.
  6951. This function can be used in a hook."
  6952. (when (org-mode-p)
  6953. (org-map-dblocks 'org-update-dblock)))
  6954. ;;;; Completion
  6955. (defconst org-additional-option-like-keywords
  6956. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6957. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6958. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6959. (defcustom org-structure-template-alist
  6960. '(
  6961. ("s" "#+begin_src ?\n\n#+end_src"
  6962. "<src lang=\"?\">\n\n</src>")
  6963. ("e" "#+begin_example\n?\n#+end_example"
  6964. "<example>\n?\n</example>")
  6965. ("q" "#+begin_quote\n?\n#+end_quote"
  6966. "<quote>\n?\n</quote>")
  6967. ("v" "#+begin_verse\n?\n#+end_verse"
  6968. "<verse>\n?\n/verse>")
  6969. ("l" "#+begin_latex\n?\n#+end_latex"
  6970. "<literal style=\"latex\">\n?\n</literal>")
  6971. ("L" "#+latex: "
  6972. "<literal style=\"latex\">?</literal>")
  6973. ("h" "#+begin_html\n?\n#+end_html"
  6974. "<literal style=\"html\">\n?\n</literal>")
  6975. ("H" "#+html: "
  6976. "<literal style=\"html\">?</literal>")
  6977. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6978. ("A" "#+ascii: ")
  6979. ("i" "#+include: %file ?"
  6980. "<include file=%file markup=\"?\">")
  6981. )
  6982. "Structure completion elements.
  6983. This is a list of abbreviation keys and values. The value gets inserted
  6984. it you type @samp{.} followed by the key and then the completion key,
  6985. usually `M-TAB'. %file will be replaced by a file name after prompting
  6986. for the file uning completion.
  6987. There are two templates for each key, the first uses the original Org syntax,
  6988. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6989. the default when the /org-mtags.el/ module has been loaded. See also the
  6990. variable `org-mtags-prefere-muse-templates'.
  6991. This is an experimental feature, it is undecided if it is going to stay in."
  6992. :group 'org-completion
  6993. :type '(repeat
  6994. (string :tag "Key")
  6995. (string :tag "Template")
  6996. (string :tag "Muse Template")))
  6997. (defun org-try-structure-completion ()
  6998. "Try to complete a structure template before point.
  6999. This looks for strings like \"<e\" on an otherwise empty line and
  7000. expands them."
  7001. (let ((l (buffer-substring (point-at-bol) (point)))
  7002. a)
  7003. (when (and (looking-at "[ \t]*$")
  7004. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7005. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7006. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7007. (match-beginning 1)) a)
  7008. t)))
  7009. (defun org-complete-expand-structure-template (start cell)
  7010. "Expand a structure template."
  7011. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7012. (rpl (nth (if musep 2 1) cell)))
  7013. (delete-region start (point))
  7014. (when (string-match "\\`#\\+" rpl)
  7015. (cond
  7016. ((bolp))
  7017. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7018. (delete-region (point-at-bol) (point)))
  7019. (t (newline))))
  7020. (setq start (point))
  7021. (if (string-match "%file" rpl)
  7022. (setq rpl (replace-match
  7023. (concat
  7024. "\""
  7025. (save-match-data
  7026. (abbreviate-file-name (read-file-name "Include file: ")))
  7027. "\"")
  7028. t t rpl)))
  7029. (insert rpl)
  7030. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7031. (defun org-complete (&optional arg)
  7032. "Perform completion on word at point.
  7033. At the beginning of a headline, this completes TODO keywords as given in
  7034. `org-todo-keywords'.
  7035. If the current word is preceded by a backslash, completes the TeX symbols
  7036. that are supported for HTML support.
  7037. If the current word is preceded by \"#+\", completes special words for
  7038. setting file options.
  7039. In the line after \"#+STARTUP:, complete valid keywords.\"
  7040. At all other locations, this simply calls the value of
  7041. `org-completion-fallback-command'."
  7042. (interactive "P")
  7043. (org-without-partial-completion
  7044. (catch 'exit
  7045. (let* ((a nil)
  7046. (end (point))
  7047. (beg1 (save-excursion
  7048. (skip-chars-backward (org-re "[:alnum:]_@"))
  7049. (point)))
  7050. (beg (save-excursion
  7051. (skip-chars-backward "a-zA-Z0-9_:$")
  7052. (point)))
  7053. (confirm (lambda (x) (stringp (car x))))
  7054. (searchhead (equal (char-before beg) ?*))
  7055. (struct
  7056. (when (and (member (char-before beg1) '(?. ?<))
  7057. (setq a (assoc (buffer-substring beg1 (point))
  7058. org-structure-template-alist)))
  7059. (org-complete-expand-structure-template (1- beg1) a)
  7060. (throw 'exit t)))
  7061. (tag (and (equal (char-before beg1) ?:)
  7062. (equal (char-after (point-at-bol)) ?*)))
  7063. (prop (and (equal (char-before beg1) ?:)
  7064. (not (equal (char-after (point-at-bol)) ?*))))
  7065. (texp (equal (char-before beg) ?\\))
  7066. (link (equal (char-before beg) ?\[))
  7067. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7068. beg)
  7069. "#+"))
  7070. (startup (string-match "^#\\+STARTUP:.*"
  7071. (buffer-substring (point-at-bol) (point))))
  7072. (completion-ignore-case opt)
  7073. (type nil)
  7074. (tbl nil)
  7075. (table (cond
  7076. (opt
  7077. (setq type :opt)
  7078. (require 'org-exp)
  7079. (append
  7080. (mapcar
  7081. (lambda (x)
  7082. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7083. (cons (match-string 2 x) (match-string 1 x)))
  7084. (org-split-string (org-get-current-options) "\n"))
  7085. (mapcar 'list org-additional-option-like-keywords)))
  7086. (startup
  7087. (setq type :startup)
  7088. org-startup-options)
  7089. (link (append org-link-abbrev-alist-local
  7090. org-link-abbrev-alist))
  7091. (texp
  7092. (setq type :tex)
  7093. org-html-entities)
  7094. ((string-match "\\`\\*+[ \t]+\\'"
  7095. (buffer-substring (point-at-bol) beg))
  7096. (setq type :todo)
  7097. (mapcar 'list org-todo-keywords-1))
  7098. (searchhead
  7099. (setq type :searchhead)
  7100. (save-excursion
  7101. (goto-char (point-min))
  7102. (while (re-search-forward org-todo-line-regexp nil t)
  7103. (push (list
  7104. (org-make-org-heading-search-string
  7105. (match-string 3) t))
  7106. tbl)))
  7107. tbl)
  7108. (tag (setq type :tag beg beg1)
  7109. (or org-tag-alist (org-get-buffer-tags)))
  7110. (prop (setq type :prop beg beg1)
  7111. (mapcar 'list (org-buffer-property-keys nil t t)))
  7112. (t (progn
  7113. (call-interactively org-completion-fallback-command)
  7114. (throw 'exit nil)))))
  7115. (pattern (buffer-substring-no-properties beg end))
  7116. (completion (try-completion pattern table confirm)))
  7117. (cond ((eq completion t)
  7118. (if (not (assoc (upcase pattern) table))
  7119. (message "Already complete")
  7120. (if (and (equal type :opt)
  7121. (not (member (car (assoc (upcase pattern) table))
  7122. org-additional-option-like-keywords)))
  7123. (insert (substring (cdr (assoc (upcase pattern) table))
  7124. (length pattern)))
  7125. (if (memq type '(:tag :prop)) (insert ":")))))
  7126. ((null completion)
  7127. (message "Can't find completion for \"%s\"" pattern)
  7128. (ding))
  7129. ((not (string= pattern completion))
  7130. (delete-region beg end)
  7131. (if (string-match " +$" completion)
  7132. (setq completion (replace-match "" t t completion)))
  7133. (insert completion)
  7134. (if (get-buffer-window "*Completions*")
  7135. (delete-window (get-buffer-window "*Completions*")))
  7136. (if (assoc completion table)
  7137. (if (eq type :todo) (insert " ")
  7138. (if (memq type '(:tag :prop)) (insert ":"))))
  7139. (if (and (equal type :opt) (assoc completion table))
  7140. (message "%s" (substitute-command-keys
  7141. "Press \\[org-complete] again to insert example settings"))))
  7142. (t
  7143. (message "Making completion list...")
  7144. (let ((list (sort (all-completions pattern table confirm)
  7145. 'string<)))
  7146. (with-output-to-temp-buffer "*Completions*"
  7147. (condition-case nil
  7148. ;; Protection needed for XEmacs and emacs 21
  7149. (display-completion-list list pattern)
  7150. (error (display-completion-list list)))))
  7151. (message "Making completion list...%s" "done")))))))
  7152. ;;;; TODO, DEADLINE, Comments
  7153. (defun org-toggle-comment ()
  7154. "Change the COMMENT state of an entry."
  7155. (interactive)
  7156. (save-excursion
  7157. (org-back-to-heading)
  7158. (let (case-fold-search)
  7159. (if (looking-at (concat outline-regexp
  7160. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7161. (replace-match "" t t nil 1)
  7162. (if (looking-at outline-regexp)
  7163. (progn
  7164. (goto-char (match-end 0))
  7165. (insert org-comment-string " ")))))))
  7166. (defvar org-last-todo-state-is-todo nil
  7167. "This is non-nil when the last TODO state change led to a TODO state.
  7168. If the last change removed the TODO tag or switched to DONE, then
  7169. this is nil.")
  7170. (defvar org-setting-tags nil) ; dynamically skiped
  7171. (defun org-parse-local-options (string var)
  7172. "Parse STRING for startup setting relevant for variable VAR."
  7173. (let ((rtn (symbol-value var))
  7174. e opts)
  7175. (save-match-data
  7176. (if (or (not string) (not (string-match "\\S-" string)))
  7177. rtn
  7178. (setq opts (delq nil (mapcar (lambda (x)
  7179. (setq e (assoc x org-startup-options))
  7180. (if (eq (nth 1 e) var) e nil))
  7181. (org-split-string string "[ \t]+"))))
  7182. (if (not opts)
  7183. rtn
  7184. (setq rtn nil)
  7185. (while (setq e (pop opts))
  7186. (if (not (nth 3 e))
  7187. (setq rtn (nth 2 e))
  7188. (if (not (listp rtn)) (setq rtn nil))
  7189. (push (nth 2 e) rtn)))
  7190. rtn)))))
  7191. (defvar org-blocker-hook nil
  7192. "Hook for functions that are allowed to block a state change.
  7193. Each function gets as its single argument a property list, see
  7194. `org-trigger-hook' for more information about this list.
  7195. If any of the functions in this hook returns nil, the state change
  7196. is blocked.")
  7197. (defvar org-trigger-hook nil
  7198. "Hook for functions that are triggered by a state change.
  7199. Each function gets as its single argument a property list with at least
  7200. the following elements:
  7201. (:type type-of-change :position pos-at-entry-start
  7202. :from old-state :to new-state)
  7203. Depending on the type, more properties may be present.
  7204. This mechanism is currently implemented for:
  7205. TODO state changes
  7206. ------------------
  7207. :type todo-state-change
  7208. :from previous state (keyword as a string), or nil
  7209. :to new state (keyword as a string), or nil")
  7210. (defun org-todo (&optional arg)
  7211. "Change the TODO state of an item.
  7212. The state of an item is given by a keyword at the start of the heading,
  7213. like
  7214. *** TODO Write paper
  7215. *** DONE Call mom
  7216. The different keywords are specified in the variable `org-todo-keywords'.
  7217. By default the available states are \"TODO\" and \"DONE\".
  7218. So for this example: when the item starts with TODO, it is changed to DONE.
  7219. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7220. DONE are present, add TODO at the beginning of the heading.
  7221. With C-u prefix arg, use completion to determine the new state.
  7222. With numeric prefix arg, switch to that state.
  7223. For calling through lisp, arg is also interpreted in the following way:
  7224. 'none -> empty state
  7225. \"\"(empty string) -> switch to empty state
  7226. 'done -> switch to DONE
  7227. 'nextset -> switch to the next set of keywords
  7228. 'previousset -> switch to the previous set of keywords
  7229. \"WAITING\" -> switch to the specified keyword, but only if it
  7230. really is a member of `org-todo-keywords'."
  7231. (interactive "P")
  7232. (save-excursion
  7233. (catch 'exit
  7234. (org-back-to-heading)
  7235. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7236. (or (looking-at (concat " +" org-todo-regexp " *"))
  7237. (looking-at " *"))
  7238. (let* ((match-data (match-data))
  7239. (startpos (point-at-bol))
  7240. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7241. (org-log-done org-log-done)
  7242. (org-log-repeat org-log-repeat)
  7243. (org-todo-log-states org-todo-log-states)
  7244. (this (match-string 1))
  7245. (hl-pos (match-beginning 0))
  7246. (head (org-get-todo-sequence-head this))
  7247. (ass (assoc head org-todo-kwd-alist))
  7248. (interpret (nth 1 ass))
  7249. (done-word (nth 3 ass))
  7250. (final-done-word (nth 4 ass))
  7251. (last-state (or this ""))
  7252. (completion-ignore-case t)
  7253. (member (member this org-todo-keywords-1))
  7254. (tail (cdr member))
  7255. (state (cond
  7256. ((and org-todo-key-trigger
  7257. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7258. (and (not arg) org-use-fast-todo-selection
  7259. (not (eq org-use-fast-todo-selection 'prefix)))))
  7260. ;; Use fast selection
  7261. (org-fast-todo-selection))
  7262. ((and (equal arg '(4))
  7263. (or (not org-use-fast-todo-selection)
  7264. (not org-todo-key-trigger)))
  7265. ;; Read a state with completion
  7266. (completing-read "State: " (mapcar (lambda(x) (list x))
  7267. org-todo-keywords-1)
  7268. nil t))
  7269. ((eq arg 'right)
  7270. (if this
  7271. (if tail (car tail) nil)
  7272. (car org-todo-keywords-1)))
  7273. ((eq arg 'left)
  7274. (if (equal member org-todo-keywords-1)
  7275. nil
  7276. (if this
  7277. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7278. org-todo-keywords-1)
  7279. (org-last org-todo-keywords-1))))
  7280. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7281. (setq arg nil))) ; hack to fall back to cycling
  7282. (arg
  7283. ;; user or caller requests a specific state
  7284. (cond
  7285. ((equal arg "") nil)
  7286. ((eq arg 'none) nil)
  7287. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7288. ((eq arg 'nextset)
  7289. (or (car (cdr (member head org-todo-heads)))
  7290. (car org-todo-heads)))
  7291. ((eq arg 'previousset)
  7292. (let ((org-todo-heads (reverse org-todo-heads)))
  7293. (or (car (cdr (member head org-todo-heads)))
  7294. (car org-todo-heads))))
  7295. ((car (member arg org-todo-keywords-1)))
  7296. ((nth (1- (prefix-numeric-value arg))
  7297. org-todo-keywords-1))))
  7298. ((null member) (or head (car org-todo-keywords-1)))
  7299. ((equal this final-done-word) nil) ;; -> make empty
  7300. ((null tail) nil) ;; -> first entry
  7301. ((eq interpret 'sequence)
  7302. (car tail))
  7303. ((memq interpret '(type priority))
  7304. (if (eq this-command last-command)
  7305. (car tail)
  7306. (if (> (length tail) 0)
  7307. (or done-word (car org-done-keywords))
  7308. nil)))
  7309. (t nil)))
  7310. (next (if state (concat " " state " ") " "))
  7311. (change-plist (list :type 'todo-state-change :from this :to state
  7312. :position startpos))
  7313. dolog now-done-p)
  7314. (when org-blocker-hook
  7315. (unless (save-excursion
  7316. (save-match-data
  7317. (run-hook-with-args-until-failure
  7318. 'org-blocker-hook change-plist)))
  7319. (if (interactive-p)
  7320. (error "TODO state change from %s to %s blocked" this state)
  7321. ;; fail silently
  7322. (message "TODO state change from %s to %s blocked" this state)
  7323. (throw 'exit nil))))
  7324. (store-match-data match-data)
  7325. (replace-match next t t)
  7326. (unless (pos-visible-in-window-p hl-pos)
  7327. (message "TODO state changed to %s" (org-trim next)))
  7328. (unless head
  7329. (setq head (org-get-todo-sequence-head state)
  7330. ass (assoc head org-todo-kwd-alist)
  7331. interpret (nth 1 ass)
  7332. done-word (nth 3 ass)
  7333. final-done-word (nth 4 ass)))
  7334. (when (memq arg '(nextset previousset))
  7335. (message "Keyword-Set %d/%d: %s"
  7336. (- (length org-todo-sets) -1
  7337. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7338. (length org-todo-sets)
  7339. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7340. (setq org-last-todo-state-is-todo
  7341. (not (member state org-done-keywords)))
  7342. (setq now-done-p (and (member state org-done-keywords)
  7343. (not (member this org-done-keywords))))
  7344. (and logging (org-local-logging logging))
  7345. (when (and (or org-todo-log-states org-log-done)
  7346. (not (memq arg '(nextset previousset))))
  7347. ;; we need to look at recording a time and note
  7348. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7349. (nth 2 (assoc this org-todo-log-states))))
  7350. (when (and state
  7351. (member state org-not-done-keywords)
  7352. (not (member this org-not-done-keywords)))
  7353. ;; This is now a todo state and was not one before
  7354. ;; If there was a CLOSED time stamp, get rid of it.
  7355. (org-add-planning-info nil nil 'closed))
  7356. (when (and now-done-p org-log-done)
  7357. ;; It is now done, and it was not done before
  7358. (org-add-planning-info 'closed (org-current-time))
  7359. (if (and (not dolog) (eq 'note org-log-done))
  7360. (org-add-log-setup 'done state 'findpos 'note)))
  7361. (when (and state dolog)
  7362. ;; This is a non-nil state, and we need to log it
  7363. (org-add-log-setup 'state state 'findpos dolog)))
  7364. ;; Fixup tag positioning
  7365. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7366. (run-hooks 'org-after-todo-state-change-hook)
  7367. (if (and arg (not (member state org-done-keywords)))
  7368. (setq head (org-get-todo-sequence-head state)))
  7369. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7370. ;; Do we need to trigger a repeat?
  7371. (when now-done-p (org-auto-repeat-maybe state))
  7372. ;; Fixup cursor location if close to the keyword
  7373. (if (and (outline-on-heading-p)
  7374. (not (bolp))
  7375. (save-excursion (beginning-of-line 1)
  7376. (looking-at org-todo-line-regexp))
  7377. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7378. (progn
  7379. (goto-char (or (match-end 2) (match-end 1)))
  7380. (just-one-space)))
  7381. (when org-trigger-hook
  7382. (save-excursion
  7383. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7384. (defun org-local-logging (value)
  7385. "Get logging settings from a property VALUE."
  7386. (let* (words w a)
  7387. ;; directly set the variables, they are already local.
  7388. (setq org-log-done nil
  7389. org-log-repeat nil
  7390. org-todo-log-states nil)
  7391. (setq words (org-split-string value))
  7392. (while (setq w (pop words))
  7393. (cond
  7394. ((setq a (assoc w org-startup-options))
  7395. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7396. (set (nth 1 a) (nth 2 a))))
  7397. ((setq a (org-extract-log-state-settings w))
  7398. (and (member (car a) org-todo-keywords-1)
  7399. (push a org-todo-log-states)))))))
  7400. (defun org-get-todo-sequence-head (kwd)
  7401. "Return the head of the TODO sequence to which KWD belongs.
  7402. If KWD is not set, check if there is a text property remembering the
  7403. right sequence."
  7404. (let (p)
  7405. (cond
  7406. ((not kwd)
  7407. (or (get-text-property (point-at-bol) 'org-todo-head)
  7408. (progn
  7409. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7410. nil (point-at-eol)))
  7411. (get-text-property p 'org-todo-head))))
  7412. ((not (member kwd org-todo-keywords-1))
  7413. (car org-todo-keywords-1))
  7414. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7415. (defun org-fast-todo-selection ()
  7416. "Fast TODO keyword selection with single keys.
  7417. Returns the new TODO keyword, or nil if no state change should occur."
  7418. (let* ((fulltable org-todo-key-alist)
  7419. (done-keywords org-done-keywords) ;; needed for the faces.
  7420. (maxlen (apply 'max (mapcar
  7421. (lambda (x)
  7422. (if (stringp (car x)) (string-width (car x)) 0))
  7423. fulltable)))
  7424. (expert nil)
  7425. (fwidth (+ maxlen 3 1 3))
  7426. (ncol (/ (- (window-width) 4) fwidth))
  7427. tg cnt e c tbl
  7428. groups ingroup)
  7429. (save-window-excursion
  7430. (if expert
  7431. (set-buffer (get-buffer-create " *Org todo*"))
  7432. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7433. (erase-buffer)
  7434. (org-set-local 'org-done-keywords done-keywords)
  7435. (setq tbl fulltable cnt 0)
  7436. (while (setq e (pop tbl))
  7437. (cond
  7438. ((equal e '(:startgroup))
  7439. (push '() groups) (setq ingroup t)
  7440. (when (not (= cnt 0))
  7441. (setq cnt 0)
  7442. (insert "\n"))
  7443. (insert "{ "))
  7444. ((equal e '(:endgroup))
  7445. (setq ingroup nil cnt 0)
  7446. (insert "}\n"))
  7447. (t
  7448. (setq tg (car e) c (cdr e))
  7449. (if ingroup (push tg (car groups)))
  7450. (setq tg (org-add-props tg nil 'face
  7451. (org-get-todo-face tg)))
  7452. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7453. (insert "[" c "] " tg (make-string
  7454. (- fwidth 4 (length tg)) ?\ ))
  7455. (when (= (setq cnt (1+ cnt)) ncol)
  7456. (insert "\n")
  7457. (if ingroup (insert " "))
  7458. (setq cnt 0)))))
  7459. (insert "\n")
  7460. (goto-char (point-min))
  7461. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7462. (fit-window-to-buffer))
  7463. (message "[a-z..]:Set [SPC]:clear")
  7464. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7465. (cond
  7466. ((or (= c ?\C-g)
  7467. (and (= c ?q) (not (rassoc c fulltable))))
  7468. (setq quit-flag t))
  7469. ((= c ?\ ) nil)
  7470. ((setq e (rassoc c fulltable) tg (car e))
  7471. tg)
  7472. (t (setq quit-flag t))))))
  7473. (defun org-entry-is-todo-p ()
  7474. (member (org-get-todo-state) org-not-done-keywords))
  7475. (defun org-entry-is-done-p ()
  7476. (member (org-get-todo-state) org-done-keywords))
  7477. (defun org-get-todo-state ()
  7478. (save-excursion
  7479. (org-back-to-heading t)
  7480. (and (looking-at org-todo-line-regexp)
  7481. (match-end 2)
  7482. (match-string 2))))
  7483. (defun org-at-date-range-p (&optional inactive-ok)
  7484. "Is the cursor inside a date range?"
  7485. (interactive)
  7486. (save-excursion
  7487. (catch 'exit
  7488. (let ((pos (point)))
  7489. (skip-chars-backward "^[<\r\n")
  7490. (skip-chars-backward "<[")
  7491. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7492. (>= (match-end 0) pos)
  7493. (throw 'exit t))
  7494. (skip-chars-backward "^<[\r\n")
  7495. (skip-chars-backward "<[")
  7496. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7497. (>= (match-end 0) pos)
  7498. (throw 'exit t)))
  7499. nil)))
  7500. (defun org-get-repeat ()
  7501. "Check if tere is a deadline/schedule with repeater in this entry."
  7502. (save-match-data
  7503. (save-excursion
  7504. (org-back-to-heading t)
  7505. (if (re-search-forward
  7506. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7507. (match-string 1)))))
  7508. (defvar org-last-changed-timestamp)
  7509. (defvar org-log-post-message)
  7510. (defvar org-log-note-purpose)
  7511. (defvar org-log-note-how)
  7512. (defun org-auto-repeat-maybe (done-word)
  7513. "Check if the current headline contains a repeated deadline/schedule.
  7514. If yes, set TODO state back to what it was and change the base date
  7515. of repeating deadline/scheduled time stamps to new date.
  7516. This function is run automatically after each state change to a DONE state."
  7517. ;; last-state is dynamically scoped into this function
  7518. (let* ((repeat (org-get-repeat))
  7519. (aa (assoc last-state org-todo-kwd-alist))
  7520. (interpret (nth 1 aa))
  7521. (head (nth 2 aa))
  7522. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7523. (msg "Entry repeats: ")
  7524. (org-log-done nil)
  7525. (org-todo-log-states nil)
  7526. (nshiftmax 10) (nshift 0)
  7527. re type n what ts mb0 time)
  7528. (when repeat
  7529. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7530. (org-todo (if (eq interpret 'type) last-state head))
  7531. (when org-log-repeat
  7532. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7533. (memq 'org-add-log-note post-command-hook))
  7534. ;; OK, we are already setup for some record
  7535. (if (eq org-log-repeat 'note)
  7536. ;; make sure we take a note, not only a time stamp
  7537. (setq org-log-note-how 'note))
  7538. ;; Set up for taking a record
  7539. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7540. 'findpos org-log-repeat)))
  7541. (org-back-to-heading t)
  7542. (org-add-planning-info nil nil 'closed)
  7543. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7544. org-deadline-time-regexp "\\)\\|\\("
  7545. org-ts-regexp "\\)"))
  7546. (while (re-search-forward
  7547. re (save-excursion (outline-next-heading) (point)) t)
  7548. (setq type (if (match-end 1) org-scheduled-string
  7549. (if (match-end 3) org-deadline-string "Plain:"))
  7550. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7551. mb0 (match-beginning 0))
  7552. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7553. (setq n (string-to-number (match-string 2 ts))
  7554. what (match-string 3 ts))
  7555. (if (equal what "w") (setq n (* n 7) what "d"))
  7556. ;; Preparation, see if we need to modify the start date for the change
  7557. (when (match-end 1)
  7558. (setq time (save-match-data (org-time-string-to-time ts)))
  7559. (cond
  7560. ((equal (match-string 1 ts) ".")
  7561. ;; Shift starting date to today
  7562. (org-timestamp-change
  7563. (- (time-to-days (current-time)) (time-to-days time))
  7564. 'day))
  7565. ((equal (match-string 1 ts) "+")
  7566. (while (or (= nshift 0)
  7567. (<= (time-to-days time) (time-to-days (current-time))))
  7568. (when (= (incf nshift) nshiftmax)
  7569. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7570. (error "Abort")))
  7571. (org-timestamp-change n (cdr (assoc what whata)))
  7572. (org-at-timestamp-p t)
  7573. (setq ts (match-string 1))
  7574. (setq time (save-match-data (org-time-string-to-time ts))))
  7575. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7576. ;; rematch, so that we have everything in place for the real shift
  7577. (org-at-timestamp-p t)
  7578. (setq ts (match-string 1))
  7579. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7580. (org-timestamp-change n (cdr (assoc what whata)))
  7581. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7582. (setq org-log-post-message msg)
  7583. (message "%s" msg))))
  7584. (defun org-show-todo-tree (arg)
  7585. "Make a compact tree which shows all headlines marked with TODO.
  7586. The tree will show the lines where the regexp matches, and all higher
  7587. headlines above the match.
  7588. With a \\[universal-argument] prefix, also show the DONE entries.
  7589. With a numeric prefix N, construct a sparse tree for the Nth element
  7590. of `org-todo-keywords-1'."
  7591. (interactive "P")
  7592. (let ((case-fold-search nil)
  7593. (kwd-re
  7594. (cond ((null arg) org-not-done-regexp)
  7595. ((equal arg '(4))
  7596. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7597. (mapcar 'list org-todo-keywords-1))))
  7598. (concat "\\("
  7599. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7600. "\\)\\>")))
  7601. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7602. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7603. org-todo-keywords-1)))
  7604. (t (error "Invalid prefix argument: %s" arg)))))
  7605. (message "%d TODO entries found"
  7606. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7607. (defun org-deadline (&optional remove)
  7608. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7609. With argument REMOVE, remove any deadline from the item."
  7610. (interactive "P")
  7611. (if remove
  7612. (progn
  7613. (org-remove-timestamp-with-keyword org-deadline-string)
  7614. (message "Item no longer has a deadline."))
  7615. (org-add-planning-info 'deadline nil 'closed)))
  7616. (defun org-schedule (&optional remove)
  7617. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7618. With argument REMOVE, remove any scheduling date from the item."
  7619. (interactive "P")
  7620. (if remove
  7621. (progn
  7622. (org-remove-timestamp-with-keyword org-scheduled-string)
  7623. (message "Item is no longer scheduled."))
  7624. (org-add-planning-info 'scheduled nil 'closed)))
  7625. (defun org-remove-timestamp-with-keyword (keyword)
  7626. "Remove all time stamps with KEYWORD in the current entry."
  7627. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7628. beg)
  7629. (save-excursion
  7630. (org-back-to-heading t)
  7631. (setq beg (point))
  7632. (org-end-of-subtree t t)
  7633. (while (re-search-backward re beg t)
  7634. (replace-match "")
  7635. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7636. (equal (char-before) ?\ ))
  7637. (backward-delete-char 1)
  7638. (if (string-match "^[ \t]*$" (buffer-substring
  7639. (point-at-bol) (point-at-eol)))
  7640. (delete-region (point-at-bol)
  7641. (min (point-max) (1+ (point-at-eol))))))))))
  7642. (defun org-add-planning-info (what &optional time &rest remove)
  7643. "Insert new timestamp with keyword in the line directly after the headline.
  7644. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7645. If non is given, the user is prompted for a date.
  7646. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7647. be removed."
  7648. (interactive)
  7649. (let (org-time-was-given org-end-time-was-given ts
  7650. end default-time default-input)
  7651. (when (and (not time) (memq what '(scheduled deadline)))
  7652. ;; Try to get a default date/time from existing timestamp
  7653. (save-excursion
  7654. (org-back-to-heading t)
  7655. (setq end (save-excursion (outline-next-heading) (point)))
  7656. (when (re-search-forward (if (eq what 'scheduled)
  7657. org-scheduled-time-regexp
  7658. org-deadline-time-regexp)
  7659. end t)
  7660. (setq ts (match-string 1)
  7661. default-time
  7662. (apply 'encode-time (org-parse-time-string ts))
  7663. default-input (and ts (org-get-compact-tod ts))))))
  7664. (when what
  7665. ;; If necessary, get the time from the user
  7666. (setq time (or time (org-read-date nil 'to-time nil nil
  7667. default-time default-input))))
  7668. (when (and org-insert-labeled-timestamps-at-point
  7669. (member what '(scheduled deadline)))
  7670. (insert
  7671. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7672. (org-insert-time-stamp time org-time-was-given
  7673. nil nil nil (list org-end-time-was-given))
  7674. (setq what nil))
  7675. (save-excursion
  7676. (save-restriction
  7677. (let (col list elt ts buffer-invisibility-spec)
  7678. (org-back-to-heading t)
  7679. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7680. (goto-char (match-end 1))
  7681. (setq col (current-column))
  7682. (goto-char (match-end 0))
  7683. (if (eobp) (insert "\n") (forward-char 1))
  7684. (if (and (not (looking-at outline-regexp))
  7685. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7686. "[^\r\n]*"))
  7687. (not (equal (match-string 1) org-clock-string)))
  7688. (narrow-to-region (match-beginning 0) (match-end 0))
  7689. (insert-before-markers "\n")
  7690. (backward-char 1)
  7691. (narrow-to-region (point) (point))
  7692. (org-indent-to-column col))
  7693. ;; Check if we have to remove something.
  7694. (setq list (cons what remove))
  7695. (while list
  7696. (setq elt (pop list))
  7697. (goto-char (point-min))
  7698. (when (or (and (eq elt 'scheduled)
  7699. (re-search-forward org-scheduled-time-regexp nil t))
  7700. (and (eq elt 'deadline)
  7701. (re-search-forward org-deadline-time-regexp nil t))
  7702. (and (eq elt 'closed)
  7703. (re-search-forward org-closed-time-regexp nil t)))
  7704. (replace-match "")
  7705. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7706. (if (looking-at " +") (replace-match ""))))
  7707. (goto-char (point-max))
  7708. (when what
  7709. (insert
  7710. (if (not (equal (char-before) ?\ )) " " "")
  7711. (cond ((eq what 'scheduled) org-scheduled-string)
  7712. ((eq what 'deadline) org-deadline-string)
  7713. ((eq what 'closed) org-closed-string))
  7714. " ")
  7715. (setq ts (org-insert-time-stamp
  7716. time
  7717. (or org-time-was-given
  7718. (and (eq what 'closed) org-log-done-with-time))
  7719. (eq what 'closed)
  7720. nil nil (list org-end-time-was-given)))
  7721. (end-of-line 1))
  7722. (goto-char (point-min))
  7723. (widen)
  7724. (if (and (looking-at "[ \t]+\n")
  7725. (equal (char-before) ?\n))
  7726. (delete-region (1- (point)) (point-at-eol)))
  7727. ts)))))
  7728. (defvar org-log-note-marker (make-marker))
  7729. (defvar org-log-note-purpose nil)
  7730. (defvar org-log-note-state nil)
  7731. (defvar org-log-note-how nil)
  7732. (defvar org-log-note-window-configuration nil)
  7733. (defvar org-log-note-return-to (make-marker))
  7734. (defvar org-log-post-message nil
  7735. "Message to be displayed after a log note has been stored.
  7736. The auto-repeater uses this.")
  7737. (defun org-add-note ()
  7738. "Add a note to the current entry.
  7739. This is done in the same way as adding a state change note."
  7740. (interactive)
  7741. (org-add-log-setup 'note nil t nil))
  7742. (defun org-add-log-setup (&optional purpose state findpos how)
  7743. "Set up the post command hook to take a note.
  7744. If this is about to TODO state change, the new state is expected in STATE.
  7745. When FINDPOS is non-nil, find the correct position for the note in
  7746. the current entry. If not, assume that it can be inserted at point."
  7747. (save-excursion
  7748. (when findpos
  7749. (org-back-to-heading t)
  7750. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7751. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7752. "[^\r\n]*\\)?"))
  7753. (goto-char (match-end 0))
  7754. (unless org-log-states-order-reversed
  7755. (and (= (char-after) ?\n) (forward-char 1))
  7756. (org-skip-over-state-notes)
  7757. (skip-chars-backward " \t\n\r")))
  7758. (move-marker org-log-note-marker (point))
  7759. (setq org-log-note-purpose purpose
  7760. org-log-note-state state
  7761. org-log-note-how how)
  7762. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7763. (defun org-skip-over-state-notes ()
  7764. "Skip past the list of State notes in an entry."
  7765. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7766. (while (looking-at "[ \t]*- State")
  7767. (condition-case nil
  7768. (org-next-item)
  7769. (error (org-end-of-item)))))
  7770. (defun org-add-log-note (&optional purpose)
  7771. "Pop up a window for taking a note, and add this note later at point."
  7772. (remove-hook 'post-command-hook 'org-add-log-note)
  7773. (setq org-log-note-window-configuration (current-window-configuration))
  7774. (delete-other-windows)
  7775. (move-marker org-log-note-return-to (point))
  7776. (switch-to-buffer (marker-buffer org-log-note-marker))
  7777. (goto-char org-log-note-marker)
  7778. (org-switch-to-buffer-other-window "*Org Note*")
  7779. (erase-buffer)
  7780. (if (memq org-log-note-how '(time state))
  7781. (org-store-log-note)
  7782. (let ((org-inhibit-startup t)) (org-mode))
  7783. (insert (format "# Insert note for %s.
  7784. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7785. (cond
  7786. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7787. ((eq org-log-note-purpose 'done) "closed todo item")
  7788. ((eq org-log-note-purpose 'state)
  7789. (format "state change to \"%s\"" org-log-note-state))
  7790. ((eq org-log-note-purpose 'note)
  7791. "this entry")
  7792. (t (error "This should not happen")))))
  7793. (org-set-local 'org-finish-function 'org-store-log-note)))
  7794. (defvar org-note-abort nil) ; dynamically scoped
  7795. (defun org-store-log-note ()
  7796. "Finish taking a log note, and insert it to where it belongs."
  7797. (let ((txt (buffer-string))
  7798. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7799. lines ind)
  7800. (kill-buffer (current-buffer))
  7801. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7802. (setq txt (replace-match "" t t txt)))
  7803. (if (string-match "\\s-+\\'" txt)
  7804. (setq txt (replace-match "" t t txt)))
  7805. (setq lines (org-split-string txt "\n"))
  7806. (when (and note (string-match "\\S-" note))
  7807. (setq note
  7808. (org-replace-escapes
  7809. note
  7810. (list (cons "%u" (user-login-name))
  7811. (cons "%U" user-full-name)
  7812. (cons "%t" (format-time-string
  7813. (org-time-stamp-format 'long 'inactive)
  7814. (current-time)))
  7815. (cons "%s" (if org-log-note-state
  7816. (concat "\"" org-log-note-state "\"")
  7817. "")))))
  7818. (if lines (setq note (concat note " \\\\")))
  7819. (push note lines))
  7820. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7821. (when lines
  7822. (save-excursion
  7823. (set-buffer (marker-buffer org-log-note-marker))
  7824. (save-excursion
  7825. (goto-char org-log-note-marker)
  7826. (move-marker org-log-note-marker nil)
  7827. (end-of-line 1)
  7828. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7829. (indent-relative nil)
  7830. (insert "- " (pop lines))
  7831. (org-indent-line-function)
  7832. (beginning-of-line 1)
  7833. (looking-at "[ \t]*")
  7834. (setq ind (concat (match-string 0) " "))
  7835. (end-of-line 1)
  7836. (while lines (insert "\n" ind (pop lines)))))))
  7837. (set-window-configuration org-log-note-window-configuration)
  7838. (with-current-buffer (marker-buffer org-log-note-return-to)
  7839. (goto-char org-log-note-return-to))
  7840. (move-marker org-log-note-return-to nil)
  7841. (and org-log-post-message (message "%s" org-log-post-message)))
  7842. (defun org-sparse-tree (&optional arg)
  7843. "Create a sparse tree, prompt for the details.
  7844. This command can create sparse trees. You first need to select the type
  7845. of match used to create the tree:
  7846. t Show entries with a specific TODO keyword.
  7847. T Show entries selected by a tags match.
  7848. p Enter a property name and its value (both with completion on existing
  7849. names/values) and show entries with that property.
  7850. r Show entries matching a regular expression
  7851. d Show deadlines due within `org-deadline-warning-days'."
  7852. (interactive "P")
  7853. (let (ans kwd value)
  7854. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7855. (setq ans (read-char-exclusive))
  7856. (cond
  7857. ((equal ans ?d)
  7858. (call-interactively 'org-check-deadlines))
  7859. ((equal ans ?b)
  7860. (call-interactively 'org-check-before-date))
  7861. ((equal ans ?t)
  7862. (org-show-todo-tree '(4)))
  7863. ((equal ans ?T)
  7864. (call-interactively 'org-tags-sparse-tree))
  7865. ((member ans '(?p ?P))
  7866. (setq kwd (completing-read "Property: "
  7867. (mapcar 'list (org-buffer-property-keys))))
  7868. (setq value (completing-read "Value: "
  7869. (mapcar 'list (org-property-values kwd))))
  7870. (unless (string-match "\\`{.*}\\'" value)
  7871. (setq value (concat "\"" value "\"")))
  7872. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7873. ((member ans '(?r ?R ?/))
  7874. (call-interactively 'org-occur))
  7875. (t (error "No such sparse tree command \"%c\"" ans)))))
  7876. (defvar org-occur-highlights nil
  7877. "List of overlays used for occur matches.")
  7878. (make-variable-buffer-local 'org-occur-highlights)
  7879. (defvar org-occur-parameters nil
  7880. "Parameters of the active org-occur calls.
  7881. This is a list, each call to org-occur pushes as cons cell,
  7882. containing the regular expression and the callback, onto the list.
  7883. The list can contain several entries if `org-occur' has been called
  7884. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7885. will only contain one set of parameters. When the highlights are
  7886. removed (for example with `C-c C-c', or with the next edit (depending
  7887. on `org-remove-highlights-with-change'), this variable is emptied
  7888. as well.")
  7889. (make-variable-buffer-local 'org-occur-parameters)
  7890. (defun org-occur (regexp &optional keep-previous callback)
  7891. "Make a compact tree which shows all matches of REGEXP.
  7892. The tree will show the lines where the regexp matches, and all higher
  7893. headlines above the match. It will also show the heading after the match,
  7894. to make sure editing the matching entry is easy.
  7895. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7896. call to `org-occur' will be kept, to allow stacking of calls to this
  7897. command.
  7898. If CALLBACK is non-nil, it is a function which is called to confirm
  7899. that the match should indeed be shown."
  7900. (interactive "sRegexp: \nP")
  7901. (unless keep-previous
  7902. (org-remove-occur-highlights nil nil t))
  7903. (push (cons regexp callback) org-occur-parameters)
  7904. (let ((cnt 0))
  7905. (save-excursion
  7906. (goto-char (point-min))
  7907. (if (or (not keep-previous) ; do not want to keep
  7908. (not org-occur-highlights)) ; no previous matches
  7909. ;; hide everything
  7910. (org-overview))
  7911. (while (re-search-forward regexp nil t)
  7912. (when (or (not callback)
  7913. (save-match-data (funcall callback)))
  7914. (setq cnt (1+ cnt))
  7915. (when org-highlight-sparse-tree-matches
  7916. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7917. (org-show-context 'occur-tree))))
  7918. (when org-remove-highlights-with-change
  7919. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7920. nil 'local))
  7921. (unless org-sparse-tree-open-archived-trees
  7922. (org-hide-archived-subtrees (point-min) (point-max)))
  7923. (run-hooks 'org-occur-hook)
  7924. (if (interactive-p)
  7925. (message "%d match(es) for regexp %s" cnt regexp))
  7926. cnt))
  7927. (defun org-show-context (&optional key)
  7928. "Make sure point and context and visible.
  7929. How much context is shown depends upon the variables
  7930. `org-show-hierarchy-above', `org-show-following-heading'. and
  7931. `org-show-siblings'."
  7932. (let ((heading-p (org-on-heading-p t))
  7933. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7934. (following-p (org-get-alist-option org-show-following-heading key))
  7935. (entry-p (org-get-alist-option org-show-entry-below key))
  7936. (siblings-p (org-get-alist-option org-show-siblings key)))
  7937. (catch 'exit
  7938. ;; Show heading or entry text
  7939. (if (and heading-p (not entry-p))
  7940. (org-flag-heading nil) ; only show the heading
  7941. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7942. (org-show-hidden-entry))) ; show entire entry
  7943. (when following-p
  7944. ;; Show next sibling, or heading below text
  7945. (save-excursion
  7946. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7947. (org-flag-heading nil))))
  7948. (when siblings-p (org-show-siblings))
  7949. (when hierarchy-p
  7950. ;; show all higher headings, possibly with siblings
  7951. (save-excursion
  7952. (while (and (condition-case nil
  7953. (progn (org-up-heading-all 1) t)
  7954. (error nil))
  7955. (not (bobp)))
  7956. (org-flag-heading nil)
  7957. (when siblings-p (org-show-siblings))))))))
  7958. (defun org-reveal (&optional siblings)
  7959. "Show current entry, hierarchy above it, and the following headline.
  7960. This can be used to show a consistent set of context around locations
  7961. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7962. not t for the search context.
  7963. With optional argument SIBLINGS, on each level of the hierarchy all
  7964. siblings are shown. This repairs the tree structure to what it would
  7965. look like when opened with hierarchical calls to `org-cycle'."
  7966. (interactive "P")
  7967. (let ((org-show-hierarchy-above t)
  7968. (org-show-following-heading t)
  7969. (org-show-siblings (if siblings t org-show-siblings)))
  7970. (org-show-context nil)))
  7971. (defun org-highlight-new-match (beg end)
  7972. "Highlight from BEG to END and mark the highlight is an occur headline."
  7973. (let ((ov (org-make-overlay beg end)))
  7974. (org-overlay-put ov 'face 'secondary-selection)
  7975. (push ov org-occur-highlights)))
  7976. (defun org-remove-occur-highlights (&optional beg end noremove)
  7977. "Remove the occur highlights from the buffer.
  7978. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7979. from the `before-change-functions' in the current buffer."
  7980. (interactive)
  7981. (unless org-inhibit-highlight-removal
  7982. (mapc 'org-delete-overlay org-occur-highlights)
  7983. (setq org-occur-highlights nil)
  7984. (setq org-occur-parameters nil)
  7985. (unless noremove
  7986. (remove-hook 'before-change-functions
  7987. 'org-remove-occur-highlights 'local))))
  7988. ;;;; Priorities
  7989. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7990. "Regular expression matching the priority indicator.")
  7991. (defvar org-remove-priority-next-time nil)
  7992. (defun org-priority-up ()
  7993. "Increase the priority of the current item."
  7994. (interactive)
  7995. (org-priority 'up))
  7996. (defun org-priority-down ()
  7997. "Decrease the priority of the current item."
  7998. (interactive)
  7999. (org-priority 'down))
  8000. (defun org-priority (&optional action)
  8001. "Change the priority of an item by ARG.
  8002. ACTION can be `set', `up', `down', or a character."
  8003. (interactive)
  8004. (setq action (or action 'set))
  8005. (let (current new news have remove)
  8006. (save-excursion
  8007. (org-back-to-heading)
  8008. (if (looking-at org-priority-regexp)
  8009. (setq current (string-to-char (match-string 2))
  8010. have t)
  8011. (setq current org-default-priority))
  8012. (cond
  8013. ((or (eq action 'set)
  8014. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8015. (if (not (eq action 'set))
  8016. (setq new action)
  8017. (message "Priority %c-%c, SPC to remove: "
  8018. org-highest-priority org-lowest-priority)
  8019. (setq new (read-char-exclusive)))
  8020. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8021. (= (upcase org-lowest-priority) org-lowest-priority))
  8022. (setq new (upcase new)))
  8023. (cond ((equal new ?\ ) (setq remove t))
  8024. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8025. (error "Priority must be between `%c' and `%c'"
  8026. org-highest-priority org-lowest-priority))))
  8027. ((eq action 'up)
  8028. (if (and (not have) (eq last-command this-command))
  8029. (setq new org-lowest-priority)
  8030. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8031. org-default-priority (1- current)))))
  8032. ((eq action 'down)
  8033. (if (and (not have) (eq last-command this-command))
  8034. (setq new org-highest-priority)
  8035. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8036. org-default-priority (1+ current)))))
  8037. (t (error "Invalid action")))
  8038. (if (or (< (upcase new) org-highest-priority)
  8039. (> (upcase new) org-lowest-priority))
  8040. (setq remove t))
  8041. (setq news (format "%c" new))
  8042. (if have
  8043. (if remove
  8044. (replace-match "" t t nil 1)
  8045. (replace-match news t t nil 2))
  8046. (if remove
  8047. (error "No priority cookie found in line")
  8048. (looking-at org-todo-line-regexp)
  8049. (if (match-end 2)
  8050. (progn
  8051. (goto-char (match-end 2))
  8052. (insert " [#" news "]"))
  8053. (goto-char (match-beginning 3))
  8054. (insert "[#" news "] ")))))
  8055. (org-preserve-lc (org-set-tags nil 'align))
  8056. (if remove
  8057. (message "Priority removed")
  8058. (message "Priority of current item set to %s" news))))
  8059. (defun org-get-priority (s)
  8060. "Find priority cookie and return priority."
  8061. (save-match-data
  8062. (if (not (string-match org-priority-regexp s))
  8063. (* 1000 (- org-lowest-priority org-default-priority))
  8064. (* 1000 (- org-lowest-priority
  8065. (string-to-char (match-string 2 s)))))))
  8066. ;;;; Tags
  8067. (defun org-scan-tags (action matcher &optional todo-only)
  8068. "Scan headline tags with inheritance and produce output ACTION.
  8069. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8070. evaluated, testing if a given set of tags qualifies a headline for
  8071. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8072. are included in the output."
  8073. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8074. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8075. (org-re
  8076. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8077. (props (list 'face nil
  8078. 'done-face 'org-done
  8079. 'undone-face nil
  8080. 'mouse-face 'highlight
  8081. 'org-not-done-regexp org-not-done-regexp
  8082. 'org-todo-regexp org-todo-regexp
  8083. 'keymap org-agenda-keymap
  8084. 'help-echo
  8085. (format "mouse-2 or RET jump to org file %s"
  8086. (abbreviate-file-name
  8087. (or (buffer-file-name (buffer-base-buffer))
  8088. (buffer-name (buffer-base-buffer)))))))
  8089. (case-fold-search nil)
  8090. lspos
  8091. tags tags-list tags-alist (llast 0) rtn level category i txt
  8092. todo marker entry priority)
  8093. (save-excursion
  8094. (goto-char (point-min))
  8095. (when (eq action 'sparse-tree)
  8096. (org-overview)
  8097. (org-remove-occur-highlights))
  8098. (while (re-search-forward re nil t)
  8099. (catch :skip
  8100. (setq todo (if (match-end 1) (match-string 2))
  8101. tags (if (match-end 4) (match-string 4)))
  8102. (goto-char (setq lspos (1+ (match-beginning 0))))
  8103. (setq level (org-reduced-level (funcall outline-level))
  8104. category (org-get-category))
  8105. (setq i llast llast level)
  8106. ;; remove tag lists from same and sublevels
  8107. (while (>= i level)
  8108. (when (setq entry (assoc i tags-alist))
  8109. (setq tags-alist (delete entry tags-alist)))
  8110. (setq i (1- i)))
  8111. ;; add the next tags
  8112. (when tags
  8113. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8114. tags-alist
  8115. (cons (cons level tags) tags-alist)))
  8116. ;; compile tags for current headline
  8117. (setq tags-list
  8118. (if org-use-tag-inheritance
  8119. (apply 'append (mapcar 'cdr tags-alist))
  8120. tags))
  8121. (when (and tags org-use-tag-inheritance
  8122. (not (eq t org-use-tag-inheritance)))
  8123. ;; selective inheritance, remove uninherited ones
  8124. (setcdr (car tags-alist)
  8125. (org-remove-uniherited-tags (cdar tags-alist))))
  8126. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8127. (eval matcher)
  8128. (or (not org-agenda-skip-archived-trees)
  8129. (not (member org-archive-tag tags-list))))
  8130. (and (eq action 'agenda) (org-agenda-skip))
  8131. ;; list this headline
  8132. (if (eq action 'sparse-tree)
  8133. (progn
  8134. (and org-highlight-sparse-tree-matches
  8135. (org-get-heading) (match-end 0)
  8136. (org-highlight-new-match
  8137. (match-beginning 0) (match-beginning 1)))
  8138. (org-show-context 'tags-tree))
  8139. (setq txt (org-format-agenda-item
  8140. ""
  8141. (concat
  8142. (if org-tags-match-list-sublevels
  8143. (make-string (1- level) ?.) "")
  8144. (org-get-heading))
  8145. category tags-list)
  8146. priority (org-get-priority txt))
  8147. (goto-char lspos)
  8148. (setq marker (org-agenda-new-marker))
  8149. (org-add-props txt props
  8150. 'org-marker marker 'org-hd-marker marker 'org-category category
  8151. 'priority priority 'type "tagsmatch")
  8152. (push txt rtn))
  8153. ;; if we are to skip sublevels, jump to end of subtree
  8154. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8155. (when (and (eq action 'sparse-tree)
  8156. (not org-sparse-tree-open-archived-trees))
  8157. (org-hide-archived-subtrees (point-min) (point-max)))
  8158. (nreverse rtn)))
  8159. (defun org-remove-uniherited-tags (tags)
  8160. "Remove all tags that are not inherited from the list TAGS."
  8161. (cond
  8162. ((eq org-use-tag-inheritance t) tags)
  8163. ((not org-use-tag-inheritance) nil)
  8164. ((stringp org-use-tag-inheritance)
  8165. (delq nil (mapcar
  8166. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8167. tags)))
  8168. ((listp org-use-tag-inheritance)
  8169. (org-delete-all org-use-tag-inheritance tags))))
  8170. (defvar todo-only) ;; dynamically scoped
  8171. (defun org-tags-sparse-tree (&optional todo-only match)
  8172. "Create a sparse tree according to tags string MATCH.
  8173. MATCH can contain positive and negative selection of tags, like
  8174. \"+WORK+URGENT-WITHBOSS\".
  8175. If optional argument TODO_ONLY is non-nil, only select lines that are
  8176. also TODO lines."
  8177. (interactive "P")
  8178. (org-prepare-agenda-buffers (list (current-buffer)))
  8179. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8180. (defvar org-cached-props nil)
  8181. (defun org-cached-entry-get (pom property)
  8182. (if (or (eq t org-use-property-inheritance)
  8183. (and (stringp org-use-property-inheritance)
  8184. (string-match org-use-property-inheritance property))
  8185. (and (listp org-use-property-inheritance)
  8186. (member property org-use-property-inheritance)))
  8187. ;; Caching is not possible, check it directly
  8188. (org-entry-get pom property 'inherit)
  8189. ;; Get all properties, so that we can do complicated checks easily
  8190. (cdr (assoc property (or org-cached-props
  8191. (setq org-cached-props
  8192. (org-entry-properties pom)))))))
  8193. (defun org-global-tags-completion-table (&optional files)
  8194. "Return the list of all tags in all agenda buffer/files."
  8195. (save-excursion
  8196. (org-uniquify
  8197. (delq nil
  8198. (apply 'append
  8199. (mapcar
  8200. (lambda (file)
  8201. (set-buffer (find-file-noselect file))
  8202. (append (org-get-buffer-tags)
  8203. (mapcar (lambda (x) (if (stringp (car-safe x))
  8204. (list (car-safe x)) nil))
  8205. org-tag-alist)))
  8206. (if (and files (car files))
  8207. files
  8208. (org-agenda-files))))))))
  8209. (defun org-make-tags-matcher (match)
  8210. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8211. ;; todo-only is scoped dynamically into this function, and the function
  8212. ;; may change it it the matcher asksk for it.
  8213. (unless match
  8214. ;; Get a new match request, with completion
  8215. (let ((org-last-tags-completion-table
  8216. (org-global-tags-completion-table)))
  8217. (setq match (completing-read
  8218. "Match: " 'org-tags-completion-function nil nil nil
  8219. 'org-tags-history))))
  8220. ;; Parse the string and create a lisp form
  8221. (let ((match0 match)
  8222. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8223. minus tag mm
  8224. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8225. orterms term orlist re-p str-p level-p level-op
  8226. prop-p pn pv po cat-p gv)
  8227. (if (string-match "/+" match)
  8228. ;; match contains also a todo-matching request
  8229. (progn
  8230. (setq tagsmatch (substring match 0 (match-beginning 0))
  8231. todomatch (substring match (match-end 0)))
  8232. (if (string-match "^!" todomatch)
  8233. (setq todo-only t todomatch (substring todomatch 1)))
  8234. (if (string-match "^\\s-*$" todomatch)
  8235. (setq todomatch nil)))
  8236. ;; only matching tags
  8237. (setq tagsmatch match todomatch nil))
  8238. ;; Make the tags matcher
  8239. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8240. (setq tagsmatcher t)
  8241. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8242. (while (setq term (pop orterms))
  8243. (while (and (equal (substring term -1) "\\") orterms)
  8244. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8245. (while (string-match re term)
  8246. (setq minus (and (match-end 1)
  8247. (equal (match-string 1 term) "-"))
  8248. tag (match-string 2 term)
  8249. re-p (equal (string-to-char tag) ?{)
  8250. level-p (match-end 4)
  8251. prop-p (match-end 5)
  8252. mm (cond
  8253. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8254. (level-p
  8255. (setq level-op (org-op-to-function (match-string 3 term)))
  8256. `(,level-op level ,(string-to-number
  8257. (match-string 4 term))))
  8258. (prop-p
  8259. (setq pn (match-string 5 term)
  8260. po (match-string 6 term)
  8261. pv (match-string 7 term)
  8262. cat-p (equal pn "CATEGORY")
  8263. re-p (equal (string-to-char pv) ?{)
  8264. str-p (equal (string-to-char pv) ?\")
  8265. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8266. (setq po (org-op-to-function po str-p))
  8267. (if (equal pn "CATEGORY")
  8268. (setq gv '(get-text-property (point) 'org-category))
  8269. (setq gv `(org-cached-entry-get nil ,pn)))
  8270. (if re-p
  8271. (if (eq po 'org<>)
  8272. `(not (string-match ,pv (or ,gv "")))
  8273. `(string-match ,pv (or ,gv "")))
  8274. (if str-p
  8275. `(,po (or ,gv "") ,pv)
  8276. `(,po (string-to-number (or ,gv ""))
  8277. ,(string-to-number pv) ))))
  8278. (t `(member ,(downcase tag) tags-list)))
  8279. mm (if minus (list 'not mm) mm)
  8280. term (substring term (match-end 0)))
  8281. (push mm tagsmatcher))
  8282. (push (if (> (length tagsmatcher) 1)
  8283. (cons 'and tagsmatcher)
  8284. (car tagsmatcher))
  8285. orlist)
  8286. (setq tagsmatcher nil))
  8287. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8288. (setq tagsmatcher
  8289. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8290. ;; Make the todo matcher
  8291. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8292. (setq todomatcher t)
  8293. (setq orterms (org-split-string todomatch "|") orlist nil)
  8294. (while (setq term (pop orterms))
  8295. (while (string-match re term)
  8296. (setq minus (and (match-end 1)
  8297. (equal (match-string 1 term) "-"))
  8298. kwd (match-string 2 term)
  8299. re-p (equal (string-to-char kwd) ?{)
  8300. term (substring term (match-end 0))
  8301. mm (if re-p
  8302. `(string-match ,(substring kwd 1 -1) todo)
  8303. (list 'equal 'todo kwd))
  8304. mm (if minus (list 'not mm) mm))
  8305. (push mm todomatcher))
  8306. (push (if (> (length todomatcher) 1)
  8307. (cons 'and todomatcher)
  8308. (car todomatcher))
  8309. orlist)
  8310. (setq todomatcher nil))
  8311. (setq todomatcher (if (> (length orlist) 1)
  8312. (cons 'or orlist) (car orlist))))
  8313. ;; Return the string and lisp forms of the matcher
  8314. (setq matcher (if todomatcher
  8315. (list 'and tagsmatcher todomatcher)
  8316. tagsmatcher))
  8317. (cons match0 matcher)))
  8318. (defun org-op-to-function (op &optional stringp)
  8319. (setq op
  8320. (cond
  8321. ((equal op "<" ) '(< string< ))
  8322. ((equal op ">" ) '(> org-string> ))
  8323. ((member op '("<=" "=<")) '(<= org-string<= ))
  8324. ((member op '(">=" "=>")) '(>= org-string>= ))
  8325. ((member op '("=" "==")) '(= string= ))
  8326. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8327. (nth (if stringp 1 0) op))
  8328. (defun org<> (a b) (not (= a b)))
  8329. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8330. (defun org-string>= (a b) (not (string< a b)))
  8331. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8332. (defun org-string<> (a b) (not (string= a b)))
  8333. (defun org-match-any-p (re list)
  8334. "Does re match any element of list?"
  8335. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8336. (delq nil list))
  8337. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8338. (defvar org-tags-overlay (org-make-overlay 1 1))
  8339. (org-detach-overlay org-tags-overlay)
  8340. (defun org-get-tags-at (&optional pos)
  8341. "Get a list of all headline tags applicable at POS.
  8342. POS defaults to point. If tags are inherited, the list contains
  8343. the targets in the same sequence as the headlines appear, i.e.
  8344. sthe tags of the current headline come last."
  8345. (interactive)
  8346. (let (tags ltags lastpos parent)
  8347. (save-excursion
  8348. (save-restriction
  8349. (widen)
  8350. (goto-char (or pos (point)))
  8351. (save-match-data
  8352. (condition-case nil
  8353. (progn
  8354. (org-back-to-heading t)
  8355. (while (not (equal lastpos (point)))
  8356. (setq lastpos (point))
  8357. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8358. (setq ltags (org-split-string
  8359. (org-match-string-no-properties 1) ":"))
  8360. (setq tags (append (org-remove-uniherited-tags ltags)
  8361. tags)))
  8362. (or org-use-tag-inheritance (error ""))
  8363. (org-up-heading-all 1)
  8364. (setq parent t)))
  8365. (error nil))))
  8366. tags)))
  8367. (defun org-toggle-tag (tag &optional onoff)
  8368. "Toggle the tag TAG for the current line.
  8369. If ONOFF is `on' or `off', don't toggle but set to this state."
  8370. (unless (org-on-heading-p t) (error "Not on headling"))
  8371. (let (res current)
  8372. (save-excursion
  8373. (beginning-of-line)
  8374. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8375. (point-at-eol) t)
  8376. (progn
  8377. (setq current (match-string 1))
  8378. (replace-match ""))
  8379. (setq current ""))
  8380. (setq current (nreverse (org-split-string current ":")))
  8381. (cond
  8382. ((eq onoff 'on)
  8383. (setq res t)
  8384. (or (member tag current) (push tag current)))
  8385. ((eq onoff 'off)
  8386. (or (not (member tag current)) (setq current (delete tag current))))
  8387. (t (if (member tag current)
  8388. (setq current (delete tag current))
  8389. (setq res t)
  8390. (push tag current))))
  8391. (end-of-line 1)
  8392. (if current
  8393. (progn
  8394. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8395. (org-set-tags nil t))
  8396. (delete-horizontal-space))
  8397. (run-hooks 'org-after-tags-change-hook))
  8398. res))
  8399. (defun org-align-tags-here (to-col)
  8400. ;; Assumes that this is a headline
  8401. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8402. (beginning-of-line 1)
  8403. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8404. (< pos (match-beginning 2)))
  8405. (progn
  8406. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8407. (goto-char (match-beginning 1))
  8408. (insert " ")
  8409. (delete-region (point) (1+ (match-beginning 2)))
  8410. (setq ncol (max (1+ (current-column))
  8411. (1+ col)
  8412. (if (> to-col 0)
  8413. to-col
  8414. (- (abs to-col) tags-l))))
  8415. (setq p (point))
  8416. (insert (make-string (- ncol (current-column)) ?\ ))
  8417. (setq ncol (current-column))
  8418. (tabify p (point-at-eol))
  8419. (org-move-to-column (min ncol col) t))
  8420. (goto-char pos))))
  8421. (defun org-set-tags (&optional arg just-align)
  8422. "Set the tags for the current headline.
  8423. With prefix ARG, realign all tags in headings in the current buffer."
  8424. (interactive "P")
  8425. (let* ((re (concat "^" outline-regexp))
  8426. (current (org-get-tags-string))
  8427. (col (current-column))
  8428. (org-setting-tags t)
  8429. table current-tags inherited-tags ; computed below when needed
  8430. tags p0 c0 c1 rpl)
  8431. (if arg
  8432. (save-excursion
  8433. (goto-char (point-min))
  8434. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8435. (while (re-search-forward re nil t)
  8436. (org-set-tags nil t)
  8437. (end-of-line 1)))
  8438. (message "All tags realigned to column %d" org-tags-column))
  8439. (if just-align
  8440. (setq tags current)
  8441. ;; Get a new set of tags from the user
  8442. (save-excursion
  8443. (setq table (or org-tag-alist (org-get-buffer-tags))
  8444. org-last-tags-completion-table table
  8445. current-tags (org-split-string current ":")
  8446. inherited-tags (nreverse
  8447. (nthcdr (length current-tags)
  8448. (nreverse (org-get-tags-at))))
  8449. tags
  8450. (if (or (eq t org-use-fast-tag-selection)
  8451. (and org-use-fast-tag-selection
  8452. (delq nil (mapcar 'cdr table))))
  8453. (org-fast-tag-selection
  8454. current-tags inherited-tags table
  8455. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8456. (let ((org-add-colon-after-tag-completion t))
  8457. (org-trim
  8458. (org-without-partial-completion
  8459. (completing-read "Tags: " 'org-tags-completion-function
  8460. nil nil current 'org-tags-history)))))))
  8461. (while (string-match "[-+&]+" tags)
  8462. ;; No boolean logic, just a list
  8463. (setq tags (replace-match ":" t t tags))))
  8464. (if (string-match "\\`[\t ]*\\'" tags)
  8465. (setq tags "")
  8466. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8467. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8468. ;; Insert new tags at the correct column
  8469. (beginning-of-line 1)
  8470. (cond
  8471. ((and (equal current "") (equal tags "")))
  8472. ((re-search-forward
  8473. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8474. (point-at-eol) t)
  8475. (if (equal tags "")
  8476. (setq rpl "")
  8477. (goto-char (match-beginning 0))
  8478. (setq c0 (current-column) p0 (point)
  8479. c1 (max (1+ c0) (if (> org-tags-column 0)
  8480. org-tags-column
  8481. (- (- org-tags-column) (length tags))))
  8482. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8483. (replace-match rpl t t)
  8484. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8485. tags)
  8486. (t (error "Tags alignment failed")))
  8487. (org-move-to-column col)
  8488. (unless just-align
  8489. (run-hooks 'org-after-tags-change-hook)))))
  8490. (defun org-change-tag-in-region (beg end tag off)
  8491. "Add or remove TAG for each entry in the region.
  8492. This works in the agenda, and also in an org-mode buffer."
  8493. (interactive
  8494. (list (region-beginning) (region-end)
  8495. (let ((org-last-tags-completion-table
  8496. (if (org-mode-p)
  8497. (org-get-buffer-tags)
  8498. (org-global-tags-completion-table))))
  8499. (completing-read
  8500. "Tag: " 'org-tags-completion-function nil nil nil
  8501. 'org-tags-history))
  8502. (progn
  8503. (message "[s]et or [r]emove? ")
  8504. (equal (read-char-exclusive) ?r))))
  8505. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8506. (let ((agendap (equal major-mode 'org-agenda-mode))
  8507. l1 l2 m buf pos newhead (cnt 0))
  8508. (goto-char end)
  8509. (setq l2 (1- (org-current-line)))
  8510. (goto-char beg)
  8511. (setq l1 (org-current-line))
  8512. (loop for l from l1 to l2 do
  8513. (goto-line l)
  8514. (setq m (get-text-property (point) 'org-hd-marker))
  8515. (when (or (and (org-mode-p) (org-on-heading-p))
  8516. (and agendap m))
  8517. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8518. pos (if agendap m (point)))
  8519. (with-current-buffer buf
  8520. (save-excursion
  8521. (save-restriction
  8522. (goto-char pos)
  8523. (setq cnt (1+ cnt))
  8524. (org-toggle-tag tag (if off 'off 'on))
  8525. (setq newhead (org-get-heading)))))
  8526. (and agendap (org-agenda-change-all-lines newhead m))))
  8527. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8528. (defun org-tags-completion-function (string predicate &optional flag)
  8529. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8530. (confirm (lambda (x) (stringp (car x)))))
  8531. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8532. (setq s1 (match-string 1 string)
  8533. s2 (match-string 2 string))
  8534. (setq s1 "" s2 string))
  8535. (cond
  8536. ((eq flag nil)
  8537. ;; try completion
  8538. (setq rtn (try-completion s2 ctable confirm))
  8539. (if (stringp rtn)
  8540. (setq rtn
  8541. (concat s1 s2 (substring rtn (length s2))
  8542. (if (and org-add-colon-after-tag-completion
  8543. (assoc rtn ctable))
  8544. ":" ""))))
  8545. rtn)
  8546. ((eq flag t)
  8547. ;; all-completions
  8548. (all-completions s2 ctable confirm)
  8549. )
  8550. ((eq flag 'lambda)
  8551. ;; exact match?
  8552. (assoc s2 ctable)))
  8553. ))
  8554. (defun org-fast-tag-insert (kwd tags face &optional end)
  8555. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8556. (insert (format "%-12s" (concat kwd ":"))
  8557. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8558. (or end "")))
  8559. (defun org-fast-tag-show-exit (flag)
  8560. (save-excursion
  8561. (goto-line 3)
  8562. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8563. (replace-match ""))
  8564. (when flag
  8565. (end-of-line 1)
  8566. (org-move-to-column (- (window-width) 19) t)
  8567. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8568. (defun org-set-current-tags-overlay (current prefix)
  8569. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8570. (if (featurep 'xemacs)
  8571. (org-overlay-display org-tags-overlay (concat prefix s)
  8572. 'secondary-selection)
  8573. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8574. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8575. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8576. "Fast tag selection with single keys.
  8577. CURRENT is the current list of tags in the headline, INHERITED is the
  8578. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8579. possibly with grouping information. TODO-TABLE is a similar table with
  8580. TODO keywords, should these have keys assigned to them.
  8581. If the keys are nil, a-z are automatically assigned.
  8582. Returns the new tags string, or nil to not change the current settings."
  8583. (let* ((fulltable (append table todo-table))
  8584. (maxlen (apply 'max (mapcar
  8585. (lambda (x)
  8586. (if (stringp (car x)) (string-width (car x)) 0))
  8587. fulltable)))
  8588. (buf (current-buffer))
  8589. (expert (eq org-fast-tag-selection-single-key 'expert))
  8590. (buffer-tags nil)
  8591. (fwidth (+ maxlen 3 1 3))
  8592. (ncol (/ (- (window-width) 4) fwidth))
  8593. (i-face 'org-done)
  8594. (c-face 'org-todo)
  8595. tg cnt e c char c1 c2 ntable tbl rtn
  8596. ov-start ov-end ov-prefix
  8597. (exit-after-next org-fast-tag-selection-single-key)
  8598. (done-keywords org-done-keywords)
  8599. groups ingroup)
  8600. (save-excursion
  8601. (beginning-of-line 1)
  8602. (if (looking-at
  8603. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8604. (setq ov-start (match-beginning 1)
  8605. ov-end (match-end 1)
  8606. ov-prefix "")
  8607. (setq ov-start (1- (point-at-eol))
  8608. ov-end (1+ ov-start))
  8609. (skip-chars-forward "^\n\r")
  8610. (setq ov-prefix
  8611. (concat
  8612. (buffer-substring (1- (point)) (point))
  8613. (if (> (current-column) org-tags-column)
  8614. " "
  8615. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8616. (org-move-overlay org-tags-overlay ov-start ov-end)
  8617. (save-window-excursion
  8618. (if expert
  8619. (set-buffer (get-buffer-create " *Org tags*"))
  8620. (delete-other-windows)
  8621. (split-window-vertically)
  8622. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8623. (erase-buffer)
  8624. (org-set-local 'org-done-keywords done-keywords)
  8625. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8626. (org-fast-tag-insert "Current" current c-face "\n\n")
  8627. (org-fast-tag-show-exit exit-after-next)
  8628. (org-set-current-tags-overlay current ov-prefix)
  8629. (setq tbl fulltable char ?a cnt 0)
  8630. (while (setq e (pop tbl))
  8631. (cond
  8632. ((equal e '(:startgroup))
  8633. (push '() groups) (setq ingroup t)
  8634. (when (not (= cnt 0))
  8635. (setq cnt 0)
  8636. (insert "\n"))
  8637. (insert "{ "))
  8638. ((equal e '(:endgroup))
  8639. (setq ingroup nil cnt 0)
  8640. (insert "}\n"))
  8641. (t
  8642. (setq tg (car e) c2 nil)
  8643. (if (cdr e)
  8644. (setq c (cdr e))
  8645. ;; automatically assign a character.
  8646. (setq c1 (string-to-char
  8647. (downcase (substring
  8648. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8649. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8650. (while (or (rassoc char ntable) (rassoc char table))
  8651. (setq char (1+ char)))
  8652. (setq c2 c1))
  8653. (setq c (or c2 char)))
  8654. (if ingroup (push tg (car groups)))
  8655. (setq tg (org-add-props tg nil 'face
  8656. (cond
  8657. ((not (assoc tg table))
  8658. (org-get-todo-face tg))
  8659. ((member tg current) c-face)
  8660. ((member tg inherited) i-face)
  8661. (t nil))))
  8662. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8663. (insert "[" c "] " tg (make-string
  8664. (- fwidth 4 (length tg)) ?\ ))
  8665. (push (cons tg c) ntable)
  8666. (when (= (setq cnt (1+ cnt)) ncol)
  8667. (insert "\n")
  8668. (if ingroup (insert " "))
  8669. (setq cnt 0)))))
  8670. (setq ntable (nreverse ntable))
  8671. (insert "\n")
  8672. (goto-char (point-min))
  8673. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8674. (fit-window-to-buffer))
  8675. (setq rtn
  8676. (catch 'exit
  8677. (while t
  8678. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8679. (if groups " [!] no groups" " [!]groups")
  8680. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8681. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8682. (cond
  8683. ((= c ?\r) (throw 'exit t))
  8684. ((= c ?!)
  8685. (setq groups (not groups))
  8686. (goto-char (point-min))
  8687. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8688. ((= c ?\C-c)
  8689. (if (not expert)
  8690. (org-fast-tag-show-exit
  8691. (setq exit-after-next (not exit-after-next)))
  8692. (setq expert nil)
  8693. (delete-other-windows)
  8694. (split-window-vertically)
  8695. (org-switch-to-buffer-other-window " *Org tags*")
  8696. (and (fboundp 'fit-window-to-buffer)
  8697. (fit-window-to-buffer))))
  8698. ((or (= c ?\C-g)
  8699. (and (= c ?q) (not (rassoc c ntable))))
  8700. (org-detach-overlay org-tags-overlay)
  8701. (setq quit-flag t))
  8702. ((= c ?\ )
  8703. (setq current nil)
  8704. (if exit-after-next (setq exit-after-next 'now)))
  8705. ((= c ?\t)
  8706. (condition-case nil
  8707. (setq tg (completing-read
  8708. "Tag: "
  8709. (or buffer-tags
  8710. (with-current-buffer buf
  8711. (org-get-buffer-tags)))))
  8712. (quit (setq tg "")))
  8713. (when (string-match "\\S-" tg)
  8714. (add-to-list 'buffer-tags (list tg))
  8715. (if (member tg current)
  8716. (setq current (delete tg current))
  8717. (push tg current)))
  8718. (if exit-after-next (setq exit-after-next 'now)))
  8719. ((setq e (rassoc c todo-table) tg (car e))
  8720. (with-current-buffer buf
  8721. (save-excursion (org-todo tg)))
  8722. (if exit-after-next (setq exit-after-next 'now)))
  8723. ((setq e (rassoc c ntable) tg (car e))
  8724. (if (member tg current)
  8725. (setq current (delete tg current))
  8726. (loop for g in groups do
  8727. (if (member tg g)
  8728. (mapc (lambda (x)
  8729. (setq current (delete x current)))
  8730. g)))
  8731. (push tg current))
  8732. (if exit-after-next (setq exit-after-next 'now))))
  8733. ;; Create a sorted list
  8734. (setq current
  8735. (sort current
  8736. (lambda (a b)
  8737. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8738. (if (eq exit-after-next 'now) (throw 'exit t))
  8739. (goto-char (point-min))
  8740. (beginning-of-line 2)
  8741. (delete-region (point) (point-at-eol))
  8742. (org-fast-tag-insert "Current" current c-face)
  8743. (org-set-current-tags-overlay current ov-prefix)
  8744. (while (re-search-forward
  8745. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8746. (setq tg (match-string 1))
  8747. (add-text-properties
  8748. (match-beginning 1) (match-end 1)
  8749. (list 'face
  8750. (cond
  8751. ((member tg current) c-face)
  8752. ((member tg inherited) i-face)
  8753. (t (get-text-property (match-beginning 1) 'face))))))
  8754. (goto-char (point-min)))))
  8755. (org-detach-overlay org-tags-overlay)
  8756. (if rtn
  8757. (mapconcat 'identity current ":")
  8758. nil))))
  8759. (defun org-get-tags-string ()
  8760. "Get the TAGS string in the current headline."
  8761. (unless (org-on-heading-p t)
  8762. (error "Not on a heading"))
  8763. (save-excursion
  8764. (beginning-of-line 1)
  8765. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8766. (org-match-string-no-properties 1)
  8767. "")))
  8768. (defun org-get-tags ()
  8769. "Get the list of tags specified in the current headline."
  8770. (org-split-string (org-get-tags-string) ":"))
  8771. (defun org-get-buffer-tags ()
  8772. "Get a table of all tags used in the buffer, for completion."
  8773. (let (tags)
  8774. (save-excursion
  8775. (goto-char (point-min))
  8776. (while (re-search-forward
  8777. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8778. (when (equal (char-after (point-at-bol 0)) ?*)
  8779. (mapc (lambda (x) (add-to-list 'tags x))
  8780. (org-split-string (org-match-string-no-properties 1) ":")))))
  8781. (mapcar 'list tags)))
  8782. ;;;; Properties
  8783. ;;; Setting and retrieving properties
  8784. (defconst org-special-properties
  8785. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8786. "TIMESTAMP" "TIMESTAMP_IA")
  8787. "The special properties valid in Org-mode.
  8788. These are properties that are not defined in the property drawer,
  8789. but in some other way.")
  8790. (defconst org-default-properties
  8791. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8792. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8793. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE")
  8794. "Some properties that are used by Org-mode for various purposes.
  8795. Being in this list makes sure that they are offered for completion.")
  8796. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8797. "Regular expression matching the first line of a property drawer.")
  8798. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8799. "Regular expression matching the first line of a property drawer.")
  8800. (defun org-property-action ()
  8801. "Do an action on properties."
  8802. (interactive)
  8803. (let (c)
  8804. (org-at-property-p)
  8805. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8806. (setq c (read-char-exclusive))
  8807. (cond
  8808. ((equal c ?s)
  8809. (call-interactively 'org-set-property))
  8810. ((equal c ?d)
  8811. (call-interactively 'org-delete-property))
  8812. ((equal c ?D)
  8813. (call-interactively 'org-delete-property-globally))
  8814. ((equal c ?c)
  8815. (call-interactively 'org-compute-property-at-point))
  8816. (t (error "No such property action %c" c)))))
  8817. (defun org-at-property-p ()
  8818. "Is the cursor in a property line?"
  8819. ;; FIXME: Does not check if we are actually in the drawer.
  8820. ;; FIXME: also returns true on any drawers.....
  8821. ;; This is used by C-c C-c for property action.
  8822. (save-excursion
  8823. (beginning-of-line 1)
  8824. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8825. (defun org-get-property-block (&optional beg end force)
  8826. "Return the (beg . end) range of the body of the property drawer.
  8827. BEG and END can be beginning and end of subtree, if not given
  8828. they will be found.
  8829. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8830. (catch 'exit
  8831. (save-excursion
  8832. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8833. (end (or end (progn (outline-next-heading) (point)))))
  8834. (goto-char beg)
  8835. (if (re-search-forward org-property-start-re end t)
  8836. (setq beg (1+ (match-end 0)))
  8837. (if force
  8838. (save-excursion
  8839. (org-insert-property-drawer)
  8840. (setq end (progn (outline-next-heading) (point))))
  8841. (throw 'exit nil))
  8842. (goto-char beg)
  8843. (if (re-search-forward org-property-start-re end t)
  8844. (setq beg (1+ (match-end 0)))))
  8845. (if (re-search-forward org-property-end-re end t)
  8846. (setq end (match-beginning 0))
  8847. (or force (throw 'exit nil))
  8848. (goto-char beg)
  8849. (setq end beg)
  8850. (org-indent-line-function)
  8851. (insert ":END:\n"))
  8852. (cons beg end)))))
  8853. (defun org-entry-properties (&optional pom which)
  8854. "Get all properties of the entry at point-or-marker POM.
  8855. This includes the TODO keyword, the tags, time strings for deadline,
  8856. scheduled, and clocking, and any additional properties defined in the
  8857. entry. The return value is an alist, keys may occur multiple times
  8858. if the property key was used several times.
  8859. POM may also be nil, in which case the current entry is used.
  8860. If WHICH is nil or `all', get all properties. If WHICH is
  8861. `special' or `standard', only get that subclass."
  8862. (setq which (or which 'all))
  8863. (org-with-point-at pom
  8864. (let ((clockstr (substring org-clock-string 0 -1))
  8865. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8866. beg end range props sum-props key value string clocksum)
  8867. (save-excursion
  8868. (when (condition-case nil (org-back-to-heading t) (error nil))
  8869. (setq beg (point))
  8870. (setq sum-props (get-text-property (point) 'org-summaries))
  8871. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8872. (outline-next-heading)
  8873. (setq end (point))
  8874. (when (memq which '(all special))
  8875. ;; Get the special properties, like TODO and tags
  8876. (goto-char beg)
  8877. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8878. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8879. (when (looking-at org-priority-regexp)
  8880. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8881. (when (and (setq value (org-get-tags-string))
  8882. (string-match "\\S-" value))
  8883. (push (cons "TAGS" value) props))
  8884. (when (setq value (org-get-tags-at))
  8885. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8886. props))
  8887. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8888. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8889. string (if (equal key clockstr)
  8890. (org-no-properties
  8891. (org-trim
  8892. (buffer-substring
  8893. (match-beginning 3) (goto-char (point-at-eol)))))
  8894. (substring (org-match-string-no-properties 3) 1 -1)))
  8895. (unless key
  8896. (if (= (char-after (match-beginning 3)) ?\[)
  8897. (setq key "TIMESTAMP_IA")
  8898. (setq key "TIMESTAMP")))
  8899. (when (or (equal key clockstr) (not (assoc key props)))
  8900. (push (cons key string) props)))
  8901. )
  8902. (when (memq which '(all standard))
  8903. ;; Get the standard properties, like :PORP: ...
  8904. (setq range (org-get-property-block beg end))
  8905. (when range
  8906. (goto-char (car range))
  8907. (while (re-search-forward
  8908. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8909. (cdr range) t)
  8910. (setq key (org-match-string-no-properties 1)
  8911. value (org-trim (or (org-match-string-no-properties 2) "")))
  8912. (unless (member key excluded)
  8913. (push (cons key (or value "")) props)))))
  8914. (if clocksum
  8915. (push (cons "CLOCKSUM"
  8916. (org-columns-number-to-string (/ (float clocksum) 60.)
  8917. 'add_times))
  8918. props))
  8919. (append sum-props (nreverse props)))))))
  8920. (defun org-entry-get (pom property &optional inherit)
  8921. "Get value of PROPERTY for entry at point-or-marker POM.
  8922. If INHERIT is non-nil and the entry does not have the property,
  8923. then also check higher levels of the hierarchy.
  8924. If INHERIT is the symbol `selective', use inheritance only if the setting
  8925. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8926. If the property is present but empty, the return value is the empty string.
  8927. If the property is not present at all, nil is returned."
  8928. (org-with-point-at pom
  8929. (if (and inherit (if (eq inherit 'selective)
  8930. (org-property-inherit-p property)
  8931. t))
  8932. (org-entry-get-with-inheritance property)
  8933. (if (member property org-special-properties)
  8934. ;; We need a special property. Use brute force, get all properties.
  8935. (cdr (assoc property (org-entry-properties nil 'special)))
  8936. (let ((range (org-get-property-block)))
  8937. (if (and range
  8938. (goto-char (car range))
  8939. (re-search-forward
  8940. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8941. (cdr range) t))
  8942. ;; Found the property, return it.
  8943. (if (match-end 1)
  8944. (org-match-string-no-properties 1)
  8945. "")))))))
  8946. (defun org-property-or-variable-value (var &optional inherit)
  8947. "Check if there is a property fixing the value of VAR.
  8948. If yes, return this value. If not, return the current value of the variable."
  8949. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8950. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8951. (read prop)
  8952. (symbol-value var))))
  8953. (defun org-entry-delete (pom property)
  8954. "Delete the property PROPERTY from entry at point-or-marker POM."
  8955. (org-with-point-at pom
  8956. (if (member property org-special-properties)
  8957. nil ; cannot delete these properties.
  8958. (let ((range (org-get-property-block)))
  8959. (if (and range
  8960. (goto-char (car range))
  8961. (re-search-forward
  8962. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8963. (cdr range) t))
  8964. (progn
  8965. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8966. t)
  8967. nil)))))
  8968. ;; Multi-values properties are properties that contain multiple values
  8969. ;; These values are assumed to be single words, separated by whitespace.
  8970. (defun org-entry-add-to-multivalued-property (pom property value)
  8971. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8972. (let* ((old (org-entry-get pom property))
  8973. (values (and old (org-split-string old "[ \t]"))))
  8974. (unless (member value values)
  8975. (setq values (cons value values))
  8976. (org-entry-put pom property
  8977. (mapconcat 'identity values " ")))))
  8978. (defun org-entry-remove-from-multivalued-property (pom property value)
  8979. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8980. (let* ((old (org-entry-get pom property))
  8981. (values (and old (org-split-string old "[ \t]"))))
  8982. (when (member value values)
  8983. (setq values (delete value values))
  8984. (org-entry-put pom property
  8985. (mapconcat 'identity values " ")))))
  8986. (defun org-entry-member-in-multivalued-property (pom property value)
  8987. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8988. (let* ((old (org-entry-get pom property))
  8989. (values (and old (org-split-string old "[ \t]"))))
  8990. (member value values)))
  8991. (defvar org-entry-property-inherited-from (make-marker))
  8992. (defun org-entry-get-with-inheritance (property)
  8993. "Get entry property, and search higher levels if not present."
  8994. (let (tmp)
  8995. (save-excursion
  8996. (save-restriction
  8997. (widen)
  8998. (catch 'ex
  8999. (while t
  9000. (when (setq tmp (org-entry-get nil property))
  9001. (org-back-to-heading t)
  9002. (move-marker org-entry-property-inherited-from (point))
  9003. (throw 'ex tmp))
  9004. (or (org-up-heading-safe) (throw 'ex nil)))))
  9005. (or tmp
  9006. (cdr (assoc property org-local-properties))
  9007. (cdr (assoc property org-global-properties))
  9008. (cdr (assoc property org-global-properties-fixed))))))
  9009. (defun org-entry-put (pom property value)
  9010. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9011. (org-with-point-at pom
  9012. (org-back-to-heading t)
  9013. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9014. range)
  9015. (cond
  9016. ((equal property "TODO")
  9017. (when (and (stringp value) (string-match "\\S-" value)
  9018. (not (member value org-todo-keywords-1)))
  9019. (error "\"%s\" is not a valid TODO state" value))
  9020. (if (or (not value)
  9021. (not (string-match "\\S-" value)))
  9022. (setq value 'none))
  9023. (org-todo value)
  9024. (org-set-tags nil 'align))
  9025. ((equal property "PRIORITY")
  9026. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9027. (string-to-char value) ?\ ))
  9028. (org-set-tags nil 'align))
  9029. ((equal property "SCHEDULED")
  9030. (if (re-search-forward org-scheduled-time-regexp end t)
  9031. (cond
  9032. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9033. ((eq value 'later) (org-timestamp-change 1 'day))
  9034. (t (call-interactively 'org-schedule)))
  9035. (call-interactively 'org-schedule)))
  9036. ((equal property "DEADLINE")
  9037. (if (re-search-forward org-deadline-time-regexp end t)
  9038. (cond
  9039. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9040. ((eq value 'later) (org-timestamp-change 1 'day))
  9041. (t (call-interactively 'org-deadline)))
  9042. (call-interactively 'org-deadline)))
  9043. ((member property org-special-properties)
  9044. (error "The %s property can not yet be set with `org-entry-put'"
  9045. property))
  9046. (t ; a non-special property
  9047. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9048. (setq range (org-get-property-block beg end 'force))
  9049. (goto-char (car range))
  9050. (if (re-search-forward
  9051. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9052. (progn
  9053. (delete-region (match-beginning 1) (match-end 1))
  9054. (goto-char (match-beginning 1)))
  9055. (goto-char (cdr range))
  9056. (insert "\n")
  9057. (backward-char 1)
  9058. (org-indent-line-function)
  9059. (insert ":" property ":"))
  9060. (and value (insert " " value))
  9061. (org-indent-line-function)))))))
  9062. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9063. "Get all property keys in the current buffer.
  9064. With INCLUDE-SPECIALS, also list the special properties that relect things
  9065. like tags and TODO state.
  9066. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9067. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9068. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9069. formats in the current buffer."
  9070. (let (rtn range cfmt cols s p)
  9071. (save-excursion
  9072. (save-restriction
  9073. (widen)
  9074. (goto-char (point-min))
  9075. (while (re-search-forward org-property-start-re nil t)
  9076. (setq range (org-get-property-block))
  9077. (goto-char (car range))
  9078. (while (re-search-forward
  9079. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9080. (cdr range) t)
  9081. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9082. (outline-next-heading))))
  9083. (when include-specials
  9084. (setq rtn (append org-special-properties rtn)))
  9085. (when include-defaults
  9086. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9087. (when include-columns
  9088. (save-excursion
  9089. (save-restriction
  9090. (widen)
  9091. (goto-char (point-min))
  9092. (while (re-search-forward
  9093. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9094. nil t)
  9095. (setq cfmt (match-string 2) s 0)
  9096. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9097. cfmt s)
  9098. (setq s (match-end 0)
  9099. p (match-string 1 cfmt))
  9100. (unless (or (equal p "ITEM")
  9101. (member p org-special-properties))
  9102. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9103. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9104. (defun org-property-values (key)
  9105. "Return a list of all values of property KEY."
  9106. (save-excursion
  9107. (save-restriction
  9108. (widen)
  9109. (goto-char (point-min))
  9110. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9111. values)
  9112. (while (re-search-forward re nil t)
  9113. (add-to-list 'values (org-trim (match-string 1))))
  9114. (delete "" values)))))
  9115. (defun org-insert-property-drawer ()
  9116. "Insert a property drawer into the current entry."
  9117. (interactive)
  9118. (org-back-to-heading t)
  9119. (looking-at outline-regexp)
  9120. (let ((indent (- (match-end 0)(match-beginning 0)))
  9121. (beg (point))
  9122. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9123. end hiddenp)
  9124. (outline-next-heading)
  9125. (setq end (point))
  9126. (goto-char beg)
  9127. (while (re-search-forward re end t))
  9128. (setq hiddenp (org-invisible-p))
  9129. (end-of-line 1)
  9130. (and (equal (char-after) ?\n) (forward-char 1))
  9131. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9132. (beginning-of-line 2))
  9133. (org-skip-over-state-notes)
  9134. (skip-chars-backward " \t\n\r")
  9135. (if (eq (char-before) ?*) (forward-char 1))
  9136. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9137. (beginning-of-line 0)
  9138. (org-indent-to-column indent)
  9139. (beginning-of-line 2)
  9140. (org-indent-to-column indent)
  9141. (beginning-of-line 0)
  9142. (if hiddenp
  9143. (save-excursion
  9144. (org-back-to-heading t)
  9145. (hide-entry))
  9146. (org-flag-drawer t))))
  9147. (defun org-set-property (property value)
  9148. "In the current entry, set PROPERTY to VALUE.
  9149. When called interactively, this will prompt for a property name, offering
  9150. completion on existing and default properties. And then it will prompt
  9151. for a value, offering competion either on allowed values (via an inherited
  9152. xxx_ALL property) or on existing values in other instances of this property
  9153. in the current file."
  9154. (interactive
  9155. (let* ((completion-ignore-case t)
  9156. (keys (org-buffer-property-keys nil t t))
  9157. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9158. (prop (if (member prop0 keys)
  9159. prop0
  9160. (or (cdr (assoc (downcase prop0)
  9161. (mapcar (lambda (x) (cons (downcase x) x))
  9162. keys)))
  9163. prop0)))
  9164. (cur (org-entry-get nil prop))
  9165. (allowed (org-property-get-allowed-values nil prop 'table))
  9166. (existing (mapcar 'list (org-property-values prop)))
  9167. (val (if allowed
  9168. (org-completing-read "Value: " allowed nil 'req-match)
  9169. (org-completing-read
  9170. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9171. (concat "[" cur "]") "")
  9172. ": ")
  9173. existing nil nil "" nil cur))))
  9174. (list prop (if (equal val "") cur val))))
  9175. (unless (equal (org-entry-get nil property) value)
  9176. (org-entry-put nil property value)))
  9177. (defun org-delete-property (property)
  9178. "In the current entry, delete PROPERTY."
  9179. (interactive
  9180. (let* ((completion-ignore-case t)
  9181. (prop (completing-read
  9182. "Property: " (org-entry-properties nil 'standard))))
  9183. (list prop)))
  9184. (message "Property %s %s" property
  9185. (if (org-entry-delete nil property)
  9186. "deleted"
  9187. "was not present in the entry")))
  9188. (defun org-delete-property-globally (property)
  9189. "Remove PROPERTY globally, from all entries."
  9190. (interactive
  9191. (let* ((completion-ignore-case t)
  9192. (prop (completing-read
  9193. "Globally remove property: "
  9194. (mapcar 'list (org-buffer-property-keys)))))
  9195. (list prop)))
  9196. (save-excursion
  9197. (save-restriction
  9198. (widen)
  9199. (goto-char (point-min))
  9200. (let ((cnt 0))
  9201. (while (re-search-forward
  9202. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9203. nil t)
  9204. (setq cnt (1+ cnt))
  9205. (replace-match ""))
  9206. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9207. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9208. (defun org-compute-property-at-point ()
  9209. "Compute the property at point.
  9210. This looks for an enclosing column format, extracts the operator and
  9211. then applies it to the proerty in the column format's scope."
  9212. (interactive)
  9213. (unless (org-at-property-p)
  9214. (error "Not at a property"))
  9215. (let ((prop (org-match-string-no-properties 2)))
  9216. (org-columns-get-format-and-top-level)
  9217. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9218. (error "No operator defined for property %s" prop))
  9219. (org-columns-compute prop)))
  9220. (defun org-property-get-allowed-values (pom property &optional table)
  9221. "Get allowed values for the property PROPERTY.
  9222. When TABLE is non-nil, return an alist that can directly be used for
  9223. completion."
  9224. (let (vals)
  9225. (cond
  9226. ((equal property "TODO")
  9227. (setq vals (org-with-point-at pom
  9228. (append org-todo-keywords-1 '("")))))
  9229. ((equal property "PRIORITY")
  9230. (let ((n org-lowest-priority))
  9231. (while (>= n org-highest-priority)
  9232. (push (char-to-string n) vals)
  9233. (setq n (1- n)))))
  9234. ((member property org-special-properties))
  9235. (t
  9236. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9237. (when (and vals (string-match "\\S-" vals))
  9238. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9239. (setq vals (mapcar (lambda (x)
  9240. (cond ((stringp x) x)
  9241. ((numberp x) (number-to-string x))
  9242. ((symbolp x) (symbol-name x))
  9243. (t "???")))
  9244. vals)))))
  9245. (if table (mapcar 'list vals) vals)))
  9246. (defun org-property-previous-allowed-value (&optional previous)
  9247. "Switch to the next allowed value for this property."
  9248. (interactive)
  9249. (org-property-next-allowed-value t))
  9250. (defun org-property-next-allowed-value (&optional previous)
  9251. "Switch to the next allowed value for this property."
  9252. (interactive)
  9253. (unless (org-at-property-p)
  9254. (error "Not at a property"))
  9255. (let* ((key (match-string 2))
  9256. (value (match-string 3))
  9257. (allowed (or (org-property-get-allowed-values (point) key)
  9258. (and (member value '("[ ]" "[-]" "[X]"))
  9259. '("[ ]" "[X]"))))
  9260. nval)
  9261. (unless allowed
  9262. (error "Allowed values for this property have not been defined"))
  9263. (if previous (setq allowed (reverse allowed)))
  9264. (if (member value allowed)
  9265. (setq nval (car (cdr (member value allowed)))))
  9266. (setq nval (or nval (car allowed)))
  9267. (if (equal nval value)
  9268. (error "Only one allowed value for this property"))
  9269. (org-at-property-p)
  9270. (replace-match (concat " :" key ": " nval) t t)
  9271. (org-indent-line-function)
  9272. (beginning-of-line 1)
  9273. (skip-chars-forward " \t")))
  9274. (defun org-find-entry-with-id (ident)
  9275. "Locate the entry that contains the ID property with exact value IDENT.
  9276. IDENT can be a string, a symbol or a number, this function will search for
  9277. the string representation of it.
  9278. Return the position where this entry starts, or nil if there is no such entry."
  9279. (let ((id (cond
  9280. ((stringp ident) ident)
  9281. ((symbol-name ident) (symbol-name ident))
  9282. ((numberp ident) (number-to-string ident))
  9283. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9284. (case-fold-search nil))
  9285. (save-excursion
  9286. (save-restriction
  9287. (widen)
  9288. (goto-char (point-min))
  9289. (when (re-search-forward
  9290. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9291. nil t)
  9292. (org-back-to-heading)
  9293. (point))))))
  9294. ;;;; Timestamps
  9295. (defvar org-last-changed-timestamp nil)
  9296. (defvar org-time-was-given) ; dynamically scoped parameter
  9297. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9298. (defvar org-ts-what) ; dynamically scoped parameter
  9299. (defun org-time-stamp (arg)
  9300. "Prompt for a date/time and insert a time stamp.
  9301. If the user specifies a time like HH:MM, or if this command is called
  9302. with a prefix argument, the time stamp will contain date and time.
  9303. Otherwise, only the date will be included. All parts of a date not
  9304. specified by the user will be filled in from the current date/time.
  9305. So if you press just return without typing anything, the time stamp
  9306. will represent the current date/time. If there is already a timestamp
  9307. at the cursor, it will be modified."
  9308. (interactive "P")
  9309. (let* ((ts nil)
  9310. (default-time
  9311. ;; Default time is either today, or, when entering a range,
  9312. ;; the range start.
  9313. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9314. (save-excursion
  9315. (re-search-backward
  9316. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9317. (- (point) 20) t)))
  9318. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9319. (current-time)))
  9320. (default-input (and ts (org-get-compact-tod ts)))
  9321. org-time-was-given org-end-time-was-given time)
  9322. (cond
  9323. ((and (org-at-timestamp-p)
  9324. (eq last-command 'org-time-stamp)
  9325. (eq this-command 'org-time-stamp))
  9326. (insert "--")
  9327. (setq time (let ((this-command this-command))
  9328. (org-read-date arg 'totime nil nil default-time default-input)))
  9329. (org-insert-time-stamp time (or org-time-was-given arg)))
  9330. ((org-at-timestamp-p)
  9331. (setq time (let ((this-command this-command))
  9332. (org-read-date arg 'totime nil nil default-time default-input)))
  9333. (when (org-at-timestamp-p) ; just to get the match data
  9334. (replace-match "")
  9335. (setq org-last-changed-timestamp
  9336. (org-insert-time-stamp
  9337. time (or org-time-was-given arg)
  9338. nil nil nil (list org-end-time-was-given))))
  9339. (message "Timestamp updated"))
  9340. (t
  9341. (setq time (let ((this-command this-command))
  9342. (org-read-date arg 'totime nil nil default-time default-input)))
  9343. (org-insert-time-stamp time (or org-time-was-given arg)
  9344. nil nil nil (list org-end-time-was-given))))))
  9345. ;; FIXME: can we use this for something else, like computing time differences?
  9346. (defun org-get-compact-tod (s)
  9347. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9348. (let* ((t1 (match-string 1 s))
  9349. (h1 (string-to-number (match-string 2 s)))
  9350. (m1 (string-to-number (match-string 3 s)))
  9351. (t2 (and (match-end 4) (match-string 5 s)))
  9352. (h2 (and t2 (string-to-number (match-string 6 s))))
  9353. (m2 (and t2 (string-to-number (match-string 7 s))))
  9354. dh dm)
  9355. (if (not t2)
  9356. t1
  9357. (setq dh (- h2 h1) dm (- m2 m1))
  9358. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9359. (concat t1 "+" (number-to-string dh)
  9360. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9361. (defun org-time-stamp-inactive (&optional arg)
  9362. "Insert an inactive time stamp.
  9363. An inactive time stamp is enclosed in square brackets instead of angle
  9364. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9365. does not link to the calendar and cannot be changed with the S-cursor keys.
  9366. So these are more for recording a certain time/date."
  9367. (interactive "P")
  9368. (let (org-time-was-given org-end-time-was-given time)
  9369. (setq time (org-read-date arg 'totime))
  9370. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9371. nil nil (list org-end-time-was-given))))
  9372. (defvar org-date-ovl (org-make-overlay 1 1))
  9373. (org-overlay-put org-date-ovl 'face 'org-warning)
  9374. (org-detach-overlay org-date-ovl)
  9375. (defvar org-ans1) ; dynamically scoped parameter
  9376. (defvar org-ans2) ; dynamically scoped parameter
  9377. (defvar org-plain-time-of-day-regexp) ; defined below
  9378. (defvar org-read-date-overlay nil)
  9379. (defvar org-dcst nil) ; dynamically scoped
  9380. (defun org-read-date (&optional with-time to-time from-string prompt
  9381. default-time default-input)
  9382. "Read a date, possibly a time, and make things smooth for the user.
  9383. The prompt will suggest to enter an ISO date, but you can also enter anything
  9384. which will at least partially be understood by `parse-time-string'.
  9385. Unrecognized parts of the date will default to the current day, month, year,
  9386. hour and minute. If this command is called to replace a timestamp at point,
  9387. of to enter the second timestamp of a range, the default time is taken from the
  9388. existing stamp. For example,
  9389. 3-2-5 --> 2003-02-05
  9390. feb 15 --> currentyear-02-15
  9391. sep 12 9 --> 2009-09-12
  9392. 12:45 --> today 12:45
  9393. 22 sept 0:34 --> currentyear-09-22 0:34
  9394. 12 --> currentyear-currentmonth-12
  9395. Fri --> nearest Friday (today or later)
  9396. etc.
  9397. Furthermore you can specify a relative date by giving, as the *first* thing
  9398. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9399. change in days weeks, months, years.
  9400. With a single plus or minus, the date is relative to today. With a double
  9401. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9402. +4d --> four days from today
  9403. +4 --> same as above
  9404. +2w --> two weeks from today
  9405. ++5 --> five days from default date
  9406. The function understands only English month and weekday abbreviations,
  9407. but this can be configured with the variables `parse-time-months' and
  9408. `parse-time-weekdays'.
  9409. While prompting, a calendar is popped up - you can also select the
  9410. date with the mouse (button 1). The calendar shows a period of three
  9411. months. To scroll it to other months, use the keys `>' and `<'.
  9412. If you don't like the calendar, turn it off with
  9413. \(setq org-read-date-popup-calendar nil)
  9414. With optional argument TO-TIME, the date will immediately be converted
  9415. to an internal time.
  9416. With an optional argument WITH-TIME, the prompt will suggest to also
  9417. insert a time. Note that when WITH-TIME is not set, you can still
  9418. enter a time, and this function will inform the calling routine about
  9419. this change. The calling routine may then choose to change the format
  9420. used to insert the time stamp into the buffer to include the time.
  9421. With optional argument FROM-STRING, read from this string instead from
  9422. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9423. the time/date that is used for everything that is not specified by the
  9424. user."
  9425. (require 'parse-time)
  9426. (let* ((org-time-stamp-rounding-minutes
  9427. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9428. (org-dcst org-display-custom-times)
  9429. (ct (org-current-time))
  9430. (def (or default-time ct))
  9431. (defdecode (decode-time def))
  9432. (dummy (progn
  9433. (when (< (nth 2 defdecode) org-extend-today-until)
  9434. (setcar (nthcdr 2 defdecode) -1)
  9435. (setcar (nthcdr 1 defdecode) 59)
  9436. (setq def (apply 'encode-time defdecode)
  9437. defdecode (decode-time def)))))
  9438. (calendar-move-hook nil)
  9439. (calendar-view-diary-initially-flag nil)
  9440. (view-diary-entries-initially nil)
  9441. (calendar-view-holidays-initially-flag nil)
  9442. (view-calendar-holidays-initially nil)
  9443. (timestr (format-time-string
  9444. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9445. (prompt (concat (if prompt (concat prompt " ") "")
  9446. (format "Date+time [%s]: " timestr)))
  9447. ans (org-ans0 "") org-ans1 org-ans2 final)
  9448. (cond
  9449. (from-string (setq ans from-string))
  9450. (org-read-date-popup-calendar
  9451. (save-excursion
  9452. (save-window-excursion
  9453. (calendar)
  9454. (calendar-forward-day (- (time-to-days def)
  9455. (calendar-absolute-from-gregorian
  9456. (calendar-current-date))))
  9457. (org-eval-in-calendar nil t)
  9458. (let* ((old-map (current-local-map))
  9459. (map (copy-keymap calendar-mode-map))
  9460. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9461. (org-defkey map (kbd "RET") 'org-calendar-select)
  9462. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9463. 'org-calendar-select-mouse)
  9464. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9465. 'org-calendar-select-mouse)
  9466. (org-defkey minibuffer-local-map [(meta shift left)]
  9467. (lambda () (interactive)
  9468. (org-eval-in-calendar '(calendar-backward-month 1))))
  9469. (org-defkey minibuffer-local-map [(meta shift right)]
  9470. (lambda () (interactive)
  9471. (org-eval-in-calendar '(calendar-forward-month 1))))
  9472. (org-defkey minibuffer-local-map [(meta shift up)]
  9473. (lambda () (interactive)
  9474. (org-eval-in-calendar '(calendar-backward-year 1))))
  9475. (org-defkey minibuffer-local-map [(meta shift down)]
  9476. (lambda () (interactive)
  9477. (org-eval-in-calendar '(calendar-forward-year 1))))
  9478. (org-defkey minibuffer-local-map [(shift up)]
  9479. (lambda () (interactive)
  9480. (org-eval-in-calendar '(calendar-backward-week 1))))
  9481. (org-defkey minibuffer-local-map [(shift down)]
  9482. (lambda () (interactive)
  9483. (org-eval-in-calendar '(calendar-forward-week 1))))
  9484. (org-defkey minibuffer-local-map [(shift left)]
  9485. (lambda () (interactive)
  9486. (org-eval-in-calendar '(calendar-backward-day 1))))
  9487. (org-defkey minibuffer-local-map [(shift right)]
  9488. (lambda () (interactive)
  9489. (org-eval-in-calendar '(calendar-forward-day 1))))
  9490. (org-defkey minibuffer-local-map ">"
  9491. (lambda () (interactive)
  9492. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9493. (org-defkey minibuffer-local-map "<"
  9494. (lambda () (interactive)
  9495. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9496. (unwind-protect
  9497. (progn
  9498. (use-local-map map)
  9499. (add-hook 'post-command-hook 'org-read-date-display)
  9500. (setq org-ans0 (read-string prompt default-input nil nil))
  9501. ;; org-ans0: from prompt
  9502. ;; org-ans1: from mouse click
  9503. ;; org-ans2: from calendar motion
  9504. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9505. (remove-hook 'post-command-hook 'org-read-date-display)
  9506. (use-local-map old-map)
  9507. (when org-read-date-overlay
  9508. (org-delete-overlay org-read-date-overlay)
  9509. (setq org-read-date-overlay nil)))))))
  9510. (t ; Naked prompt only
  9511. (unwind-protect
  9512. (setq ans (read-string prompt default-input nil timestr))
  9513. (when org-read-date-overlay
  9514. (org-delete-overlay org-read-date-overlay)
  9515. (setq org-read-date-overlay nil)))))
  9516. (setq final (org-read-date-analyze ans def defdecode))
  9517. (if to-time
  9518. (apply 'encode-time final)
  9519. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9520. (format "%04d-%02d-%02d %02d:%02d"
  9521. (nth 5 final) (nth 4 final) (nth 3 final)
  9522. (nth 2 final) (nth 1 final))
  9523. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9524. (defvar def)
  9525. (defvar defdecode)
  9526. (defvar with-time)
  9527. (defun org-read-date-display ()
  9528. "Display the currrent date prompt interpretation in the minibuffer."
  9529. (when org-read-date-display-live
  9530. (when org-read-date-overlay
  9531. (org-delete-overlay org-read-date-overlay))
  9532. (let ((p (point)))
  9533. (end-of-line 1)
  9534. (while (not (equal (buffer-substring
  9535. (max (point-min) (- (point) 4)) (point))
  9536. " "))
  9537. (insert " "))
  9538. (goto-char p))
  9539. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9540. " " (or org-ans1 org-ans2)))
  9541. (org-end-time-was-given nil)
  9542. (f (org-read-date-analyze ans def defdecode))
  9543. (fmts (if org-dcst
  9544. org-time-stamp-custom-formats
  9545. org-time-stamp-formats))
  9546. (fmt (if (or with-time
  9547. (and (boundp 'org-time-was-given) org-time-was-given))
  9548. (cdr fmts)
  9549. (car fmts)))
  9550. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9551. (when (and org-end-time-was-given
  9552. (string-match org-plain-time-of-day-regexp txt))
  9553. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9554. org-end-time-was-given
  9555. (substring txt (match-end 0)))))
  9556. (setq org-read-date-overlay
  9557. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9558. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9559. (defun org-read-date-analyze (ans def defdecode)
  9560. "Analyze the combined answer of the date prompt."
  9561. ;; FIXME: cleanup and comment
  9562. (let (delta deltan deltaw deltadef year month day
  9563. hour minute second wday pm h2 m2 tl wday1
  9564. iso-year iso-weekday iso-week iso-year iso-date)
  9565. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9566. (setq ans "+0"))
  9567. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9568. (setq ans (replace-match "" t t ans)
  9569. deltan (car delta)
  9570. deltaw (nth 1 delta)
  9571. deltadef (nth 2 delta)))
  9572. ;; Check if there is an iso week date in there
  9573. ;; If yes, sore the info and ostpone interpreting it until the rest
  9574. ;; of the parsing is done
  9575. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9576. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9577. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9578. iso-week (string-to-number (match-string 2 ans)))
  9579. (setq ans (replace-match "" t t ans)))
  9580. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9581. (when (string-match
  9582. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9583. (setq year (if (match-end 2)
  9584. (string-to-number (match-string 2 ans))
  9585. (string-to-number (format-time-string "%Y")))
  9586. month (string-to-number (match-string 3 ans))
  9587. day (string-to-number (match-string 4 ans)))
  9588. (if (< year 100) (setq year (+ 2000 year)))
  9589. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9590. t nil ans)))
  9591. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9592. ;; If there is a time with am/pm, and *no* time without it, we convert
  9593. ;; so that matching will be successful.
  9594. (loop for i from 1 to 2 do ; twice, for end time as well
  9595. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9596. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9597. (setq hour (string-to-number (match-string 1 ans))
  9598. minute (if (match-end 3)
  9599. (string-to-number (match-string 3 ans))
  9600. 0)
  9601. pm (equal ?p
  9602. (string-to-char (downcase (match-string 4 ans)))))
  9603. (if (and (= hour 12) (not pm))
  9604. (setq hour 0)
  9605. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9606. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9607. t t ans))))
  9608. ;; Check if a time range is given as a duration
  9609. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9610. (setq hour (string-to-number (match-string 1 ans))
  9611. h2 (+ hour (string-to-number (match-string 3 ans)))
  9612. minute (string-to-number (match-string 2 ans))
  9613. m2 (+ minute (if (match-end 5) (string-to-number
  9614. (match-string 5 ans))0)))
  9615. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9616. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9617. t t ans)))
  9618. ;; Check if there is a time range
  9619. (when (boundp 'org-end-time-was-given)
  9620. (setq org-time-was-given nil)
  9621. (when (and (string-match org-plain-time-of-day-regexp ans)
  9622. (match-end 8))
  9623. (setq org-end-time-was-given (match-string 8 ans))
  9624. (setq ans (concat (substring ans 0 (match-beginning 7))
  9625. (substring ans (match-end 7))))))
  9626. (setq tl (parse-time-string ans)
  9627. day (or (nth 3 tl) (nth 3 defdecode))
  9628. month (or (nth 4 tl)
  9629. (if (and org-read-date-prefer-future
  9630. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9631. (1+ (nth 4 defdecode))
  9632. (nth 4 defdecode)))
  9633. year (or (nth 5 tl)
  9634. (if (and org-read-date-prefer-future
  9635. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9636. (1+ (nth 5 defdecode))
  9637. (nth 5 defdecode)))
  9638. hour (or (nth 2 tl) (nth 2 defdecode))
  9639. minute (or (nth 1 tl) (nth 1 defdecode))
  9640. second (or (nth 0 tl) 0)
  9641. wday (nth 6 tl))
  9642. ;; Special date definitions below
  9643. (cond
  9644. (iso-week
  9645. ;; There was an iso week
  9646. (setq year (or iso-year year)
  9647. day (or iso-weekday wday 1)
  9648. wday nil ; to make sure that the trigger below does not match
  9649. iso-date (calendar-gregorian-from-absolute
  9650. (calendar-absolute-from-iso
  9651. (list iso-week day year))))
  9652. ; FIXME: Should we also push ISO weeks into the future?
  9653. ; (when (and org-read-date-prefer-future
  9654. ; (not iso-year)
  9655. ; (< (calendar-absolute-from-gregorian iso-date)
  9656. ; (time-to-days (current-time))))
  9657. ; (setq year (1+ year)
  9658. ; iso-date (calendar-gregorian-from-absolute
  9659. ; (calendar-absolute-from-iso
  9660. ; (list iso-week day year)))))
  9661. (setq month (car iso-date)
  9662. year (nth 2 iso-date)
  9663. day (nth 1 iso-date)))
  9664. (deltan
  9665. (unless deltadef
  9666. (let ((now (decode-time (current-time))))
  9667. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9668. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9669. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9670. ((equal deltaw "m") (setq month (+ month deltan)))
  9671. ((equal deltaw "y") (setq year (+ year deltan)))))
  9672. ((and wday (not (nth 3 tl)))
  9673. ;; Weekday was given, but no day, so pick that day in the week
  9674. ;; on or after the derived date.
  9675. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9676. (unless (equal wday wday1)
  9677. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9678. (if (and (boundp 'org-time-was-given)
  9679. (nth 2 tl))
  9680. (setq org-time-was-given t))
  9681. (if (< year 100) (setq year (+ 2000 year)))
  9682. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9683. (list second minute hour day month year)))
  9684. (defvar parse-time-weekdays)
  9685. (defun org-read-date-get-relative (s today default)
  9686. "Check string S for special relative date string.
  9687. TODAY and DEFAULT are internal times, for today and for a default.
  9688. Return shift list (N what def-flag)
  9689. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9690. N is the number of WHATs to shift.
  9691. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9692. the DEFAULT date rather than TODAY."
  9693. (when (and
  9694. (string-match
  9695. (concat
  9696. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9697. "\\([0-9]+\\)?"
  9698. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9699. "\\([ \t]\\|$\\)") s)
  9700. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9701. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9702. (string-to-char (substring (match-string 1 s) -1))
  9703. ?+))
  9704. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9705. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9706. (what (if (match-end 3) (match-string 3 s) "d"))
  9707. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9708. (date (if rel default today))
  9709. (wday (nth 6 (decode-time date)))
  9710. delta)
  9711. (if wday1
  9712. (progn
  9713. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9714. (if (= dir ?-) (setq delta (- delta 7)))
  9715. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9716. (list delta "d" rel))
  9717. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9718. (defun org-eval-in-calendar (form &optional keepdate)
  9719. "Eval FORM in the calendar window and return to current window.
  9720. Also, store the cursor date in variable org-ans2."
  9721. (let ((sw (selected-window)))
  9722. (select-window (get-buffer-window "*Calendar*"))
  9723. (eval form)
  9724. (when (and (not keepdate) (calendar-cursor-to-date))
  9725. (let* ((date (calendar-cursor-to-date))
  9726. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9727. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9728. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9729. (select-window sw)))
  9730. ; ;; Update the prompt to show new default date
  9731. ; (save-excursion
  9732. ; (goto-char (point-min))
  9733. ; (when (and org-ans2
  9734. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9735. ; (get-text-property (match-end 0) 'field))
  9736. ; (let ((inhibit-read-only t))
  9737. ; (replace-match (concat "[" org-ans2 "]") t t)
  9738. ; (add-text-properties (point-min) (1+ (match-end 0))
  9739. ; (text-properties-at (1+ (point-min)))))))))
  9740. (defun org-calendar-select ()
  9741. "Return to `org-read-date' with the date currently selected.
  9742. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9743. (interactive)
  9744. (when (calendar-cursor-to-date)
  9745. (let* ((date (calendar-cursor-to-date))
  9746. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9747. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9748. (if (active-minibuffer-window) (exit-minibuffer))))
  9749. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9750. "Insert a date stamp for the date given by the internal TIME.
  9751. WITH-HM means, use the stamp format that includes the time of the day.
  9752. INACTIVE means use square brackets instead of angular ones, so that the
  9753. stamp will not contribute to the agenda.
  9754. PRE and POST are optional strings to be inserted before and after the
  9755. stamp.
  9756. The command returns the inserted time stamp."
  9757. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9758. stamp)
  9759. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9760. (insert-before-markers (or pre ""))
  9761. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9762. (when (listp extra)
  9763. (setq extra (car extra))
  9764. (if (and (stringp extra)
  9765. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9766. (setq extra (format "-%02d:%02d"
  9767. (string-to-number (match-string 1 extra))
  9768. (string-to-number (match-string 2 extra))))
  9769. (setq extra nil)))
  9770. (when extra
  9771. (backward-char 1)
  9772. (insert-before-markers extra)
  9773. (forward-char 1))
  9774. (insert-before-markers (or post ""))
  9775. stamp))
  9776. (defun org-toggle-time-stamp-overlays ()
  9777. "Toggle the use of custom time stamp formats."
  9778. (interactive)
  9779. (setq org-display-custom-times (not org-display-custom-times))
  9780. (unless org-display-custom-times
  9781. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9782. (while (setq p (next-single-property-change p 'display))
  9783. (if (and (get-text-property p 'display)
  9784. (eq (get-text-property p 'face) 'org-date))
  9785. (remove-text-properties
  9786. p (setq p (next-single-property-change p 'display))
  9787. '(display t))))
  9788. (set-buffer-modified-p bmp)))
  9789. (if (featurep 'xemacs)
  9790. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9791. (org-restart-font-lock)
  9792. (setq org-table-may-need-update t)
  9793. (if org-display-custom-times
  9794. (message "Time stamps are overlayed with custom format")
  9795. (message "Time stamp overlays removed")))
  9796. (defun org-display-custom-time (beg end)
  9797. "Overlay modified time stamp format over timestamp between BEG and END."
  9798. (let* ((ts (buffer-substring beg end))
  9799. t1 w1 with-hm tf time str w2 (off 0))
  9800. (save-match-data
  9801. (setq t1 (org-parse-time-string ts t))
  9802. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9803. (setq off (- (match-end 0) (match-beginning 0)))))
  9804. (setq end (- end off))
  9805. (setq w1 (- end beg)
  9806. with-hm (and (nth 1 t1) (nth 2 t1))
  9807. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9808. time (org-fix-decoded-time t1)
  9809. str (org-add-props
  9810. (format-time-string
  9811. (substring tf 1 -1) (apply 'encode-time time))
  9812. nil 'mouse-face 'highlight)
  9813. w2 (length str))
  9814. (if (not (= w2 w1))
  9815. (add-text-properties (1+ beg) (+ 2 beg)
  9816. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9817. (if (featurep 'xemacs)
  9818. (progn
  9819. (put-text-property beg end 'invisible t)
  9820. (put-text-property beg end 'end-glyph (make-glyph str)))
  9821. (put-text-property beg end 'display str))))
  9822. (defun org-translate-time (string)
  9823. "Translate all timestamps in STRING to custom format.
  9824. But do this only if the variable `org-display-custom-times' is set."
  9825. (when org-display-custom-times
  9826. (save-match-data
  9827. (let* ((start 0)
  9828. (re org-ts-regexp-both)
  9829. t1 with-hm inactive tf time str beg end)
  9830. (while (setq start (string-match re string start))
  9831. (setq beg (match-beginning 0)
  9832. end (match-end 0)
  9833. t1 (save-match-data
  9834. (org-parse-time-string (substring string beg end) t))
  9835. with-hm (and (nth 1 t1) (nth 2 t1))
  9836. inactive (equal (substring string beg (1+ beg)) "[")
  9837. tf (funcall (if with-hm 'cdr 'car)
  9838. org-time-stamp-custom-formats)
  9839. time (org-fix-decoded-time t1)
  9840. str (format-time-string
  9841. (concat
  9842. (if inactive "[" "<") (substring tf 1 -1)
  9843. (if inactive "]" ">"))
  9844. (apply 'encode-time time))
  9845. string (replace-match str t t string)
  9846. start (+ start (length str)))))))
  9847. string)
  9848. (defun org-fix-decoded-time (time)
  9849. "Set 0 instead of nil for the first 6 elements of time.
  9850. Don't touch the rest."
  9851. (let ((n 0))
  9852. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9853. (defun org-days-to-time (timestamp-string)
  9854. "Difference between TIMESTAMP-STRING and now in days."
  9855. (- (time-to-days (org-time-string-to-time timestamp-string))
  9856. (time-to-days (current-time))))
  9857. (defun org-deadline-close (timestamp-string &optional ndays)
  9858. "Is the time in TIMESTAMP-STRING close to the current date?"
  9859. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9860. (and (< (org-days-to-time timestamp-string) ndays)
  9861. (not (org-entry-is-done-p))))
  9862. (defun org-get-wdays (ts)
  9863. "Get the deadline lead time appropriate for timestring TS."
  9864. (cond
  9865. ((<= org-deadline-warning-days 0)
  9866. ;; 0 or negative, enforce this value no matter what
  9867. (- org-deadline-warning-days))
  9868. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9869. ;; lead time is specified.
  9870. (floor (* (string-to-number (match-string 1 ts))
  9871. (cdr (assoc (match-string 2 ts)
  9872. '(("d" . 1) ("w" . 7)
  9873. ("m" . 30.4) ("y" . 365.25)))))))
  9874. ;; go for the default.
  9875. (t org-deadline-warning-days)))
  9876. (defun org-calendar-select-mouse (ev)
  9877. "Return to `org-read-date' with the date currently selected.
  9878. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9879. (interactive "e")
  9880. (mouse-set-point ev)
  9881. (when (calendar-cursor-to-date)
  9882. (let* ((date (calendar-cursor-to-date))
  9883. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9884. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9885. (if (active-minibuffer-window) (exit-minibuffer))))
  9886. (defun org-check-deadlines (ndays)
  9887. "Check if there are any deadlines due or past due.
  9888. A deadline is considered due if it happens within `org-deadline-warning-days'
  9889. days from today's date. If the deadline appears in an entry marked DONE,
  9890. it is not shown. The prefix arg NDAYS can be used to test that many
  9891. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9892. (interactive "P")
  9893. (let* ((org-warn-days
  9894. (cond
  9895. ((equal ndays '(4)) 100000)
  9896. (ndays (prefix-numeric-value ndays))
  9897. (t (abs org-deadline-warning-days))))
  9898. (case-fold-search nil)
  9899. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9900. (callback
  9901. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9902. (message "%d deadlines past-due or due within %d days"
  9903. (org-occur regexp nil callback)
  9904. org-warn-days)))
  9905. (defun org-check-before-date (date)
  9906. "Check if there are deadlines or scheduled entries before DATE."
  9907. (interactive (list (org-read-date)))
  9908. (let ((case-fold-search nil)
  9909. (regexp (concat "\\<\\(" org-deadline-string
  9910. "\\|" org-scheduled-string
  9911. "\\) *<\\([^>]+\\)>"))
  9912. (callback
  9913. (lambda () (time-less-p
  9914. (org-time-string-to-time (match-string 2))
  9915. (org-time-string-to-time date)))))
  9916. (message "%d entries before %s"
  9917. (org-occur regexp nil callback) date)))
  9918. (defun org-evaluate-time-range (&optional to-buffer)
  9919. "Evaluate a time range by computing the difference between start and end.
  9920. Normally the result is just printed in the echo area, but with prefix arg
  9921. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9922. If the time range is actually in a table, the result is inserted into the
  9923. next column.
  9924. For time difference computation, a year is assumed to be exactly 365
  9925. days in order to avoid rounding problems."
  9926. (interactive "P")
  9927. (or
  9928. (org-clock-update-time-maybe)
  9929. (save-excursion
  9930. (unless (org-at-date-range-p t)
  9931. (goto-char (point-at-bol))
  9932. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9933. (if (not (org-at-date-range-p t))
  9934. (error "Not at a time-stamp range, and none found in current line")))
  9935. (let* ((ts1 (match-string 1))
  9936. (ts2 (match-string 2))
  9937. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9938. (match-end (match-end 0))
  9939. (time1 (org-time-string-to-time ts1))
  9940. (time2 (org-time-string-to-time ts2))
  9941. (t1 (time-to-seconds time1))
  9942. (t2 (time-to-seconds time2))
  9943. (diff (abs (- t2 t1)))
  9944. (negative (< (- t2 t1) 0))
  9945. ;; (ys (floor (* 365 24 60 60)))
  9946. (ds (* 24 60 60))
  9947. (hs (* 60 60))
  9948. (fy "%dy %dd %02d:%02d")
  9949. (fy1 "%dy %dd")
  9950. (fd "%dd %02d:%02d")
  9951. (fd1 "%dd")
  9952. (fh "%02d:%02d")
  9953. y d h m align)
  9954. (if havetime
  9955. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9956. y 0
  9957. d (floor (/ diff ds)) diff (mod diff ds)
  9958. h (floor (/ diff hs)) diff (mod diff hs)
  9959. m (floor (/ diff 60)))
  9960. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9961. y 0
  9962. d (floor (+ (/ diff ds) 0.5))
  9963. h 0 m 0))
  9964. (if (not to-buffer)
  9965. (message "%s" (org-make-tdiff-string y d h m))
  9966. (if (org-at-table-p)
  9967. (progn
  9968. (goto-char match-end)
  9969. (setq align t)
  9970. (and (looking-at " *|") (goto-char (match-end 0))))
  9971. (goto-char match-end))
  9972. (if (looking-at
  9973. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9974. (replace-match ""))
  9975. (if negative (insert " -"))
  9976. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9977. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9978. (insert " " (format fh h m))))
  9979. (if align (org-table-align))
  9980. (message "Time difference inserted")))))
  9981. (defun org-make-tdiff-string (y d h m)
  9982. (let ((fmt "")
  9983. (l nil))
  9984. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9985. l (push y l)))
  9986. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9987. l (push d l)))
  9988. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9989. l (push h l)))
  9990. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9991. l (push m l)))
  9992. (apply 'format fmt (nreverse l))))
  9993. (defun org-time-string-to-time (s)
  9994. (apply 'encode-time (org-parse-time-string s)))
  9995. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9996. "Convert a time stamp to an absolute day number.
  9997. If there is a specifyer for a cyclic time stamp, get the closest date to
  9998. DAYNR.
  9999. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10000. (cond
  10001. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10002. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10003. daynr
  10004. (+ daynr 1000)))
  10005. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10006. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10007. (time-to-days (current-time))) (match-string 0 s)
  10008. prefer show-all))
  10009. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10010. (defun org-days-to-iso-week (days)
  10011. "Return the iso week number."
  10012. (require 'cal-iso)
  10013. (car (calendar-iso-from-absolute days)))
  10014. (defun org-small-year-to-year (year)
  10015. "Convert 2-digit years into 4-digit years.
  10016. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10017. The year 2000 cannot be abbreviated. Any year lager than 99
  10018. is retrned unchanged."
  10019. (if (< year 38)
  10020. (setq year (+ 2000 year))
  10021. (if (< year 100)
  10022. (setq year (+ 1900 year))))
  10023. year)
  10024. (defun org-time-from-absolute (d)
  10025. "Return the time corresponding to date D.
  10026. D may be an absolute day number, or a calendar-type list (month day year)."
  10027. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10028. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10029. (defun org-calendar-holiday ()
  10030. "List of holidays, for Diary display in Org-mode."
  10031. (require 'holidays)
  10032. (let ((hl (funcall
  10033. (if (fboundp 'calendar-check-holidays)
  10034. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10035. (if hl (mapconcat 'identity hl "; "))))
  10036. (defun org-diary-sexp-entry (sexp entry date)
  10037. "Process a SEXP diary ENTRY for DATE."
  10038. (require 'diary-lib)
  10039. (let ((result (if calendar-debug-sexp
  10040. (let ((stack-trace-on-error t))
  10041. (eval (car (read-from-string sexp))))
  10042. (condition-case nil
  10043. (eval (car (read-from-string sexp)))
  10044. (error
  10045. (beep)
  10046. (message "Bad sexp at line %d in %s: %s"
  10047. (org-current-line)
  10048. (buffer-file-name) sexp)
  10049. (sleep-for 2))))))
  10050. (cond ((stringp result) result)
  10051. ((and (consp result)
  10052. (stringp (cdr result))) (cdr result))
  10053. (result entry)
  10054. (t nil))))
  10055. (defun org-diary-to-ical-string (frombuf)
  10056. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10057. This uses the icalendar.el library."
  10058. (let* ((tmpdir (if (featurep 'xemacs)
  10059. (temp-directory)
  10060. temporary-file-directory))
  10061. (tmpfile (make-temp-name
  10062. (expand-file-name "orgics" tmpdir)))
  10063. buf rtn b e)
  10064. (save-excursion
  10065. (set-buffer frombuf)
  10066. (icalendar-export-region (point-min) (point-max) tmpfile)
  10067. (setq buf (find-buffer-visiting tmpfile))
  10068. (set-buffer buf)
  10069. (goto-char (point-min))
  10070. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10071. (setq b (match-beginning 0)))
  10072. (goto-char (point-max))
  10073. (if (re-search-backward "^END:VEVENT" nil t)
  10074. (setq e (match-end 0)))
  10075. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10076. (kill-buffer buf)
  10077. (kill-buffer frombuf)
  10078. (delete-file tmpfile)
  10079. rtn))
  10080. (defun org-closest-date (start current change prefer show-all)
  10081. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10082. When PREFER is `past' return a date that is either CURRENT or past.
  10083. When PREFER is `future', return a date that is either CURRENT or future.
  10084. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10085. ;; Make the proper lists from the dates
  10086. (catch 'exit
  10087. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10088. dn dw sday cday n1 n2
  10089. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10090. (setq start (org-date-to-gregorian start)
  10091. current (org-date-to-gregorian
  10092. (if show-all
  10093. current
  10094. (time-to-days (current-time))))
  10095. sday (calendar-absolute-from-gregorian start)
  10096. cday (calendar-absolute-from-gregorian current))
  10097. (if (<= cday sday) (throw 'exit sday))
  10098. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10099. (setq dn (string-to-number (match-string 1 change))
  10100. dw (cdr (assoc (match-string 2 change) a1)))
  10101. (error "Invalid change specifyer: %s" change))
  10102. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10103. (cond
  10104. ((eq dw 'day)
  10105. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10106. n2 (+ n1 dn)))
  10107. ((eq dw 'year)
  10108. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10109. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10110. (setq date1 (list m d y1)
  10111. n1 (calendar-absolute-from-gregorian date1)
  10112. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10113. n2 (calendar-absolute-from-gregorian date2)))
  10114. ((eq dw 'month)
  10115. ;; approx number of month between the tow dates
  10116. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10117. ;; How often does dn fit in there?
  10118. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10119. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10120. m (+ m nm)
  10121. ny (floor (/ m 12))
  10122. y (+ y ny)
  10123. m (- m (* ny 12)))
  10124. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10125. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10126. (setq m2 (+ m dn) y2 y)
  10127. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10128. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10129. (while (< n2 cday)
  10130. (setq n1 n2 m m2 y y2)
  10131. (setq m2 (+ m dn) y2 y)
  10132. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10133. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10134. (if show-all
  10135. (cond
  10136. ((eq prefer 'past) n1)
  10137. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10138. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10139. (cond
  10140. ((eq prefer 'past) n1)
  10141. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10142. (t (if (= cday n1) n1 n2)))))))
  10143. (defun org-date-to-gregorian (date)
  10144. "Turn any specification of DATE into a gregorian date for the calendar."
  10145. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10146. ((and (listp date) (= (length date) 3)) date)
  10147. ((stringp date)
  10148. (setq date (org-parse-time-string date))
  10149. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10150. ((listp date)
  10151. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10152. (defun org-parse-time-string (s &optional nodefault)
  10153. "Parse the standard Org-mode time string.
  10154. This should be a lot faster than the normal `parse-time-string'.
  10155. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10156. hour and minute fields will be nil if not given."
  10157. (if (string-match org-ts-regexp0 s)
  10158. (list 0
  10159. (if (or (match-beginning 8) (not nodefault))
  10160. (string-to-number (or (match-string 8 s) "0")))
  10161. (if (or (match-beginning 7) (not nodefault))
  10162. (string-to-number (or (match-string 7 s) "0")))
  10163. (string-to-number (match-string 4 s))
  10164. (string-to-number (match-string 3 s))
  10165. (string-to-number (match-string 2 s))
  10166. nil nil nil)
  10167. (make-list 9 0)))
  10168. (defun org-timestamp-up (&optional arg)
  10169. "Increase the date item at the cursor by one.
  10170. If the cursor is on the year, change the year. If it is on the month or
  10171. the day, change that.
  10172. With prefix ARG, change by that many units."
  10173. (interactive "p")
  10174. (org-timestamp-change (prefix-numeric-value arg)))
  10175. (defun org-timestamp-down (&optional arg)
  10176. "Decrease the date item at the cursor by one.
  10177. If the cursor is on the year, change the year. If it is on the month or
  10178. the day, change that.
  10179. With prefix ARG, change by that many units."
  10180. (interactive "p")
  10181. (org-timestamp-change (- (prefix-numeric-value arg))))
  10182. (defun org-timestamp-up-day (&optional arg)
  10183. "Increase the date in the time stamp by one day.
  10184. With prefix ARG, change that many days."
  10185. (interactive "p")
  10186. (if (and (not (org-at-timestamp-p t))
  10187. (org-on-heading-p))
  10188. (org-todo 'up)
  10189. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10190. (defun org-timestamp-down-day (&optional arg)
  10191. "Decrease the date in the time stamp by one day.
  10192. With prefix ARG, change that many days."
  10193. (interactive "p")
  10194. (if (and (not (org-at-timestamp-p t))
  10195. (org-on-heading-p))
  10196. (org-todo 'down)
  10197. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10198. (defun org-at-timestamp-p (&optional inactive-ok)
  10199. "Determine if the cursor is in or at a timestamp."
  10200. (interactive)
  10201. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10202. (pos (point))
  10203. (ans (or (looking-at tsr)
  10204. (save-excursion
  10205. (skip-chars-backward "^[<\n\r\t")
  10206. (if (> (point) (point-min)) (backward-char 1))
  10207. (and (looking-at tsr)
  10208. (> (- (match-end 0) pos) -1))))))
  10209. (and ans
  10210. (boundp 'org-ts-what)
  10211. (setq org-ts-what
  10212. (cond
  10213. ((= pos (match-beginning 0)) 'bracket)
  10214. ((= pos (1- (match-end 0))) 'bracket)
  10215. ((org-pos-in-match-range pos 2) 'year)
  10216. ((org-pos-in-match-range pos 3) 'month)
  10217. ((org-pos-in-match-range pos 7) 'hour)
  10218. ((org-pos-in-match-range pos 8) 'minute)
  10219. ((or (org-pos-in-match-range pos 4)
  10220. (org-pos-in-match-range pos 5)) 'day)
  10221. ((and (> pos (or (match-end 8) (match-end 5)))
  10222. (< pos (match-end 0)))
  10223. (- pos (or (match-end 8) (match-end 5))))
  10224. (t 'day))))
  10225. ans))
  10226. (defun org-toggle-timestamp-type ()
  10227. "Toggle the type (<active> or [inactive]) of a time stamp."
  10228. (interactive)
  10229. (when (org-at-timestamp-p t)
  10230. (save-excursion
  10231. (goto-char (match-beginning 0))
  10232. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10233. (goto-char (1- (match-end 0)))
  10234. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10235. (message "Timestamp is now %sactive"
  10236. (if (equal (char-before) ?>) "in" ""))))
  10237. (defun org-timestamp-change (n &optional what)
  10238. "Change the date in the time stamp at point.
  10239. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10240. `year', `minute', `second'. If WHAT is not given, the cursor position
  10241. in the timestamp determines what will be changed."
  10242. (let ((pos (point))
  10243. with-hm inactive
  10244. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10245. org-ts-what
  10246. extra rem
  10247. ts time time0)
  10248. (if (not (org-at-timestamp-p t))
  10249. (error "Not at a timestamp"))
  10250. (if (and (not what) (eq org-ts-what 'bracket))
  10251. (org-toggle-timestamp-type)
  10252. (if (and (not what) (not (eq org-ts-what 'day))
  10253. org-display-custom-times
  10254. (get-text-property (point) 'display)
  10255. (not (get-text-property (1- (point)) 'display)))
  10256. (setq org-ts-what 'day))
  10257. (setq org-ts-what (or what org-ts-what)
  10258. inactive (= (char-after (match-beginning 0)) ?\[)
  10259. ts (match-string 0))
  10260. (replace-match "")
  10261. (if (string-match
  10262. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10263. ts)
  10264. (setq extra (match-string 1 ts)))
  10265. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10266. (setq with-hm t))
  10267. (setq time0 (org-parse-time-string ts))
  10268. (when (and (eq org-ts-what 'minute)
  10269. (eq current-prefix-arg nil))
  10270. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10271. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10272. (setcar (cdr time0) (+ (nth 1 time0)
  10273. (if (> n 0) (- rem) (- dm rem))))))
  10274. (setq time
  10275. (encode-time (or (car time0) 0)
  10276. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10277. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10278. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10279. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10280. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10281. (nthcdr 6 time0)))
  10282. (when (integerp org-ts-what)
  10283. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10284. (if (eq what 'calendar)
  10285. (let ((cal-date (org-get-date-from-calendar)))
  10286. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10287. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10288. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10289. (setcar time0 (or (car time0) 0))
  10290. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10291. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10292. (setq time (apply 'encode-time time0))))
  10293. (setq org-last-changed-timestamp
  10294. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10295. (org-clock-update-time-maybe)
  10296. (goto-char pos)
  10297. ;; Try to recenter the calendar window, if any
  10298. (if (and org-calendar-follow-timestamp-change
  10299. (get-buffer-window "*Calendar*" t)
  10300. (memq org-ts-what '(day month year)))
  10301. (org-recenter-calendar (time-to-days time))))))
  10302. (defun org-modify-ts-extra (s pos n dm)
  10303. "Change the different parts of the lead-time and repeat fields in timestamp."
  10304. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10305. ng h m new rem)
  10306. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10307. (cond
  10308. ((or (org-pos-in-match-range pos 2)
  10309. (org-pos-in-match-range pos 3))
  10310. (setq m (string-to-number (match-string 3 s))
  10311. h (string-to-number (match-string 2 s)))
  10312. (if (org-pos-in-match-range pos 2)
  10313. (setq h (+ h n))
  10314. (setq n (* dm (org-no-warnings (signum n))))
  10315. (when (not (= 0 (setq rem (% m dm))))
  10316. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10317. (setq m (+ m n)))
  10318. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10319. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10320. (setq h (min 24 (max 0 h)))
  10321. (setq ng 1 new (format "-%02d:%02d" h m)))
  10322. ((org-pos-in-match-range pos 6)
  10323. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10324. ((org-pos-in-match-range pos 5)
  10325. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10326. ((org-pos-in-match-range pos 9)
  10327. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10328. ((org-pos-in-match-range pos 8)
  10329. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10330. (when ng
  10331. (setq s (concat
  10332. (substring s 0 (match-beginning ng))
  10333. new
  10334. (substring s (match-end ng))))))
  10335. s))
  10336. (defun org-recenter-calendar (date)
  10337. "If the calendar is visible, recenter it to DATE."
  10338. (let* ((win (selected-window))
  10339. (cwin (get-buffer-window "*Calendar*" t))
  10340. (calendar-move-hook nil))
  10341. (when cwin
  10342. (select-window cwin)
  10343. (calendar-goto-date (if (listp date) date
  10344. (calendar-gregorian-from-absolute date)))
  10345. (select-window win))))
  10346. (defun org-goto-calendar (&optional arg)
  10347. "Go to the Emacs calendar at the current date.
  10348. If there is a time stamp in the current line, go to that date.
  10349. A prefix ARG can be used to force the current date."
  10350. (interactive "P")
  10351. (let ((tsr org-ts-regexp) diff
  10352. (calendar-move-hook nil)
  10353. (calendar-view-holidays-initially-flag nil)
  10354. (view-calendar-holidays-initially nil)
  10355. (calendar-view-diary-initially-flag nil)
  10356. (view-diary-entries-initially nil))
  10357. (if (or (org-at-timestamp-p)
  10358. (save-excursion
  10359. (beginning-of-line 1)
  10360. (looking-at (concat ".*" tsr))))
  10361. (let ((d1 (time-to-days (current-time)))
  10362. (d2 (time-to-days
  10363. (org-time-string-to-time (match-string 1)))))
  10364. (setq diff (- d2 d1))))
  10365. (calendar)
  10366. (calendar-goto-today)
  10367. (if (and diff (not arg)) (calendar-forward-day diff))))
  10368. (defun org-get-date-from-calendar ()
  10369. "Return a list (month day year) of date at point in calendar."
  10370. (with-current-buffer "*Calendar*"
  10371. (save-match-data
  10372. (calendar-cursor-to-date))))
  10373. (defun org-date-from-calendar ()
  10374. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10375. If there is already a time stamp at the cursor position, update it."
  10376. (interactive)
  10377. (if (org-at-timestamp-p t)
  10378. (org-timestamp-change 0 'calendar)
  10379. (let ((cal-date (org-get-date-from-calendar)))
  10380. (org-insert-time-stamp
  10381. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10382. (defun org-minutes-to-hh:mm-string (m)
  10383. "Compute H:MM from a number of minutes."
  10384. (let ((h (/ m 60)))
  10385. (setq m (- m (* 60 h)))
  10386. (format "%d:%02d" h m)))
  10387. (defun org-hh:mm-string-to-minutes (s)
  10388. "Convert a string H:MM to a number of minutes."
  10389. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10390. (+ (* (string-to-number (match-string 1 s)) 60)
  10391. (string-to-number (match-string 2 s)))
  10392. 0))
  10393. ;;;; Agenda files
  10394. ;;;###autoload
  10395. (defun org-iswitchb (&optional arg)
  10396. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10397. With a prefix argument, restrict available to files.
  10398. With two prefix arguments, restrict available buffers to agenda files.
  10399. Due to some yet unresolved reason, global function
  10400. `iswitchb-mode' needs to be active for this function to work."
  10401. (interactive "P")
  10402. (require 'iswitchb)
  10403. (let ((enabled iswitchb-mode) blist)
  10404. (or enabled (iswitchb-mode 1))
  10405. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10406. ((equal arg '(16)) (org-buffer-list 'agenda))
  10407. (t (org-buffer-list))))
  10408. (unwind-protect
  10409. (let ((iswitchb-make-buflist-hook
  10410. (lambda ()
  10411. (setq iswitchb-temp-buflist
  10412. (mapcar 'buffer-name blist)))))
  10413. (switch-to-buffer
  10414. (iswitchb-read-buffer
  10415. "Switch-to: " nil t))
  10416. (or enabled (iswitchb-mode -1))))))
  10417. (defun org-buffer-list (&optional predicate tmp)
  10418. "Return a list of Org buffers.
  10419. PREDICATE can be either 'export, 'files or 'agenda.
  10420. 'export restrict the list to Export buffers.
  10421. 'files restrict the list to buffers visiting Org files.
  10422. 'agenda restrict the list to buffers visiting agenda files.
  10423. If TMP is non-nil, don't include temporary buffers."
  10424. (let (filter blist)
  10425. (setq filter
  10426. (cond ((eq predicate 'files) "\.org$")
  10427. ((eq predicate 'export) "\*Org .*Export")
  10428. (t "\*Org \\|\.org$")))
  10429. (setq blist
  10430. (mapcar
  10431. (lambda(b)
  10432. (let ((bname (buffer-name b))
  10433. (bfile (buffer-file-name b)))
  10434. (if (and (string-match filter bname)
  10435. (if (eq predicate 'agenda)
  10436. (member bfile
  10437. (mapcar (lambda(f) (file-truename f))
  10438. org-agenda-files)) t)
  10439. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10440. (buffer-list)))
  10441. (delete nil blist)))
  10442. (defun org-agenda-files (&optional unrestricted ext)
  10443. "Get the list of agenda files.
  10444. Optional UNRESTRICTED means return the full list even if a restriction
  10445. is currently in place.
  10446. When EXT is non-nil, try to add all files that are created by adding EXT
  10447. to the file nemes. Basically, this is a way to add the archive files
  10448. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10449. a string, \"_archive\" will be used."
  10450. (let ((files
  10451. (cond
  10452. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10453. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10454. ((listp org-agenda-files) org-agenda-files)
  10455. (t (error "Invalid value of `org-agenda-files'")))))
  10456. (setq files (apply 'append
  10457. (mapcar (lambda (f)
  10458. (if (file-directory-p f)
  10459. (directory-files
  10460. f t org-agenda-file-regexp)
  10461. (list f)))
  10462. files)))
  10463. (when org-agenda-skip-unavailable-files
  10464. (setq files (delq nil
  10465. (mapcar (function
  10466. (lambda (file)
  10467. (and (file-readable-p file) file)))
  10468. files))))
  10469. (when ext
  10470. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10471. ext "_archive"))
  10472. (setq files (apply 'append
  10473. (mapcar
  10474. (lambda (f)
  10475. (if (file-exists-p (concat f ext))
  10476. (list f (concat f ext))
  10477. (list f)))
  10478. files))))
  10479. files))
  10480. (defun org-edit-agenda-file-list ()
  10481. "Edit the list of agenda files.
  10482. Depending on setup, this either uses customize to edit the variable
  10483. `org-agenda-files', or it visits the file that is holding the list. In the
  10484. latter case, the buffer is set up in a way that saving it automatically kills
  10485. the buffer and restores the previous window configuration."
  10486. (interactive)
  10487. (if (stringp org-agenda-files)
  10488. (let ((cw (current-window-configuration)))
  10489. (find-file org-agenda-files)
  10490. (org-set-local 'org-window-configuration cw)
  10491. (org-add-hook 'after-save-hook
  10492. (lambda ()
  10493. (set-window-configuration
  10494. (prog1 org-window-configuration
  10495. (kill-buffer (current-buffer))))
  10496. (org-install-agenda-files-menu)
  10497. (message "New agenda file list installed"))
  10498. nil 'local)
  10499. (message "%s" (substitute-command-keys
  10500. "Edit list and finish with \\[save-buffer]")))
  10501. (customize-variable 'org-agenda-files)))
  10502. (defun org-store-new-agenda-file-list (list)
  10503. "Set new value for the agenda file list and save it correcly."
  10504. (if (stringp org-agenda-files)
  10505. (let ((f org-agenda-files) b)
  10506. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10507. (with-temp-file f
  10508. (insert (mapconcat 'identity list "\n") "\n")))
  10509. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10510. (setq org-agenda-files list)
  10511. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10512. (defun org-read-agenda-file-list ()
  10513. "Read the list of agenda files from a file."
  10514. (when (file-directory-p org-agenda-files)
  10515. (error "`org-agenda-files' cannot be a single directory"))
  10516. (when (stringp org-agenda-files)
  10517. (with-temp-buffer
  10518. (insert-file-contents org-agenda-files)
  10519. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10520. ;;;###autoload
  10521. (defun org-cycle-agenda-files ()
  10522. "Cycle through the files in `org-agenda-files'.
  10523. If the current buffer visits an agenda file, find the next one in the list.
  10524. If the current buffer does not, find the first agenda file."
  10525. (interactive)
  10526. (let* ((fs (org-agenda-files t))
  10527. (files (append fs (list (car fs))))
  10528. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10529. file)
  10530. (unless files (error "No agenda files"))
  10531. (catch 'exit
  10532. (while (setq file (pop files))
  10533. (if (equal (file-truename file) tcf)
  10534. (when (car files)
  10535. (find-file (car files))
  10536. (throw 'exit t))))
  10537. (find-file (car fs)))
  10538. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10539. (defun org-agenda-file-to-front (&optional to-end)
  10540. "Move/add the current file to the top of the agenda file list.
  10541. If the file is not present in the list, it is added to the front. If it is
  10542. present, it is moved there. With optional argument TO-END, add/move to the
  10543. end of the list."
  10544. (interactive "P")
  10545. (let ((org-agenda-skip-unavailable-files nil)
  10546. (file-alist (mapcar (lambda (x)
  10547. (cons (file-truename x) x))
  10548. (org-agenda-files t)))
  10549. (ctf (file-truename buffer-file-name))
  10550. x had)
  10551. (setq x (assoc ctf file-alist) had x)
  10552. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10553. (if to-end
  10554. (setq file-alist (append (delq x file-alist) (list x)))
  10555. (setq file-alist (cons x (delq x file-alist))))
  10556. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10557. (org-install-agenda-files-menu)
  10558. (message "File %s to %s of agenda file list"
  10559. (if had "moved" "added") (if to-end "end" "front"))))
  10560. (defun org-remove-file (&optional file)
  10561. "Remove current file from the list of files in variable `org-agenda-files'.
  10562. These are the files which are being checked for agenda entries.
  10563. Optional argument FILE means, use this file instead of the current."
  10564. (interactive)
  10565. (let* ((org-agenda-skip-unavailable-files nil)
  10566. (file (or file buffer-file-name))
  10567. (true-file (file-truename file))
  10568. (afile (abbreviate-file-name file))
  10569. (files (delq nil (mapcar
  10570. (lambda (x)
  10571. (if (equal true-file
  10572. (file-truename x))
  10573. nil x))
  10574. (org-agenda-files t)))))
  10575. (if (not (= (length files) (length (org-agenda-files t))))
  10576. (progn
  10577. (org-store-new-agenda-file-list files)
  10578. (org-install-agenda-files-menu)
  10579. (message "Removed file: %s" afile))
  10580. (message "File was not in list: %s (not removed)" afile))))
  10581. (defun org-file-menu-entry (file)
  10582. (vector file (list 'find-file file) t))
  10583. (defun org-check-agenda-file (file)
  10584. "Make sure FILE exists. If not, ask user what to do."
  10585. (when (not (file-exists-p file))
  10586. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10587. (abbreviate-file-name file))
  10588. (let ((r (downcase (read-char-exclusive))))
  10589. (cond
  10590. ((equal r ?r)
  10591. (org-remove-file file)
  10592. (throw 'nextfile t))
  10593. (t (error "Abort"))))))
  10594. (defun org-get-agenda-file-buffer (file)
  10595. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10596. it to the list of buffers which might be released later."
  10597. (let ((buf (org-find-base-buffer-visiting file)))
  10598. (if buf
  10599. buf ; just return it
  10600. ;; Make a new buffer and remember it
  10601. (setq buf (find-file-noselect file))
  10602. (if buf (push buf org-agenda-new-buffers))
  10603. buf)))
  10604. (defun org-release-buffers (blist)
  10605. "Release all buffers in list, asking the user for confirmation when needed.
  10606. When a buffer is unmodified, it is just killed. When modified, it is saved
  10607. \(if the user agrees) and then killed."
  10608. (let (buf file)
  10609. (while (setq buf (pop blist))
  10610. (setq file (buffer-file-name buf))
  10611. (when (and (buffer-modified-p buf)
  10612. file
  10613. (y-or-n-p (format "Save file %s? " file)))
  10614. (with-current-buffer buf (save-buffer)))
  10615. (kill-buffer buf))))
  10616. (defun org-prepare-agenda-buffers (files)
  10617. "Create buffers for all agenda files, protect archived trees and comments."
  10618. (interactive)
  10619. (let ((pa '(:org-archived t))
  10620. (pc '(:org-comment t))
  10621. (pall '(:org-archived t :org-comment t))
  10622. (inhibit-read-only t)
  10623. (rea (concat ":" org-archive-tag ":"))
  10624. bmp file re)
  10625. (save-excursion
  10626. (save-restriction
  10627. (while (setq file (pop files))
  10628. (if (bufferp file)
  10629. (set-buffer file)
  10630. (org-check-agenda-file file)
  10631. (set-buffer (org-get-agenda-file-buffer file)))
  10632. (widen)
  10633. (setq bmp (buffer-modified-p))
  10634. (org-refresh-category-properties)
  10635. (setq org-todo-keywords-for-agenda
  10636. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10637. (setq org-done-keywords-for-agenda
  10638. (append org-done-keywords-for-agenda org-done-keywords))
  10639. (save-excursion
  10640. (remove-text-properties (point-min) (point-max) pall)
  10641. (when org-agenda-skip-archived-trees
  10642. (goto-char (point-min))
  10643. (while (re-search-forward rea nil t)
  10644. (if (org-on-heading-p t)
  10645. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10646. (goto-char (point-min))
  10647. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10648. (while (re-search-forward re nil t)
  10649. (add-text-properties
  10650. (match-beginning 0) (org-end-of-subtree t) pc)))
  10651. (set-buffer-modified-p bmp))))))
  10652. ;;;; Embedded LaTeX
  10653. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10654. "Keymap for the minor `org-cdlatex-mode'.")
  10655. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10656. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10657. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10658. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10659. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10660. (defvar org-cdlatex-texmathp-advice-is-done nil
  10661. "Flag remembering if we have applied the advice to texmathp already.")
  10662. (define-minor-mode org-cdlatex-mode
  10663. "Toggle the minor `org-cdlatex-mode'.
  10664. This mode supports entering LaTeX environment and math in LaTeX fragments
  10665. in Org-mode.
  10666. \\{org-cdlatex-mode-map}"
  10667. nil " OCDL" nil
  10668. (when org-cdlatex-mode (require 'cdlatex))
  10669. (unless org-cdlatex-texmathp-advice-is-done
  10670. (setq org-cdlatex-texmathp-advice-is-done t)
  10671. (defadvice texmathp (around org-math-always-on activate)
  10672. "Always return t in org-mode buffers.
  10673. This is because we want to insert math symbols without dollars even outside
  10674. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10675. en embedded LaTeX fragement, let texmathp do its job.
  10676. \\[org-cdlatex-mode-map]"
  10677. (interactive)
  10678. (let (p)
  10679. (cond
  10680. ((not (org-mode-p)) ad-do-it)
  10681. ((eq this-command 'cdlatex-math-symbol)
  10682. (setq ad-return-value t
  10683. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10684. (t
  10685. (let ((p (org-inside-LaTeX-fragment-p)))
  10686. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10687. (setq ad-return-value t
  10688. texmathp-why '("Org-mode embedded math" . 0))
  10689. (if p ad-do-it)))))))))
  10690. (defun turn-on-org-cdlatex ()
  10691. "Unconditionally turn on `org-cdlatex-mode'."
  10692. (org-cdlatex-mode 1))
  10693. (defun org-inside-LaTeX-fragment-p ()
  10694. "Test if point is inside a LaTeX fragment.
  10695. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10696. sequence appearing also before point.
  10697. Even though the matchers for math are configurable, this function assumes
  10698. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10699. delimiters are skipped when they have been removed by customization.
  10700. The return value is nil, or a cons cell with the delimiter and
  10701. and the position of this delimiter.
  10702. This function does a reasonably good job, but can locally be fooled by
  10703. for example currency specifications. For example it will assume being in
  10704. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10705. fragments that are properly closed, but during editing, we have to live
  10706. with the uncertainty caused by missing closing delimiters. This function
  10707. looks only before point, not after."
  10708. (catch 'exit
  10709. (let ((pos (point))
  10710. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10711. (lim (progn
  10712. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10713. (point)))
  10714. dd-on str (start 0) m re)
  10715. (goto-char pos)
  10716. (when dodollar
  10717. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10718. re (nth 1 (assoc "$" org-latex-regexps)))
  10719. (while (string-match re str start)
  10720. (cond
  10721. ((= (match-end 0) (length str))
  10722. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10723. ((= (match-end 0) (- (length str) 5))
  10724. (throw 'exit nil))
  10725. (t (setq start (match-end 0))))))
  10726. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10727. (goto-char pos)
  10728. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10729. (and (match-beginning 2) (throw 'exit nil))
  10730. ;; count $$
  10731. (while (re-search-backward "\\$\\$" lim t)
  10732. (setq dd-on (not dd-on)))
  10733. (goto-char pos)
  10734. (if dd-on (cons "$$" m))))))
  10735. (defun org-try-cdlatex-tab ()
  10736. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10737. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10738. - inside a LaTeX fragment, or
  10739. - after the first word in a line, where an abbreviation expansion could
  10740. insert a LaTeX environment."
  10741. (when org-cdlatex-mode
  10742. (cond
  10743. ((save-excursion
  10744. (skip-chars-backward "a-zA-Z0-9*")
  10745. (skip-chars-backward " \t")
  10746. (bolp))
  10747. (cdlatex-tab) t)
  10748. ((org-inside-LaTeX-fragment-p)
  10749. (cdlatex-tab) t)
  10750. (t nil))))
  10751. (defun org-cdlatex-underscore-caret (&optional arg)
  10752. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10753. Revert to the normal definition outside of these fragments."
  10754. (interactive "P")
  10755. (if (org-inside-LaTeX-fragment-p)
  10756. (call-interactively 'cdlatex-sub-superscript)
  10757. (let (org-cdlatex-mode)
  10758. (call-interactively (key-binding (vector last-input-event))))))
  10759. (defun org-cdlatex-math-modify (&optional arg)
  10760. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10761. Revert to the normal definition outside of these fragments."
  10762. (interactive "P")
  10763. (if (org-inside-LaTeX-fragment-p)
  10764. (call-interactively 'cdlatex-math-modify)
  10765. (let (org-cdlatex-mode)
  10766. (call-interactively (key-binding (vector last-input-event))))))
  10767. (defvar org-latex-fragment-image-overlays nil
  10768. "List of overlays carrying the images of latex fragments.")
  10769. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10770. (defun org-remove-latex-fragment-image-overlays ()
  10771. "Remove all overlays with LaTeX fragment images in current buffer."
  10772. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10773. (setq org-latex-fragment-image-overlays nil))
  10774. (defun org-preview-latex-fragment (&optional subtree)
  10775. "Preview the LaTeX fragment at point, or all locally or globally.
  10776. If the cursor is in a LaTeX fragment, create the image and overlay
  10777. it over the source code. If there is no fragment at point, display
  10778. all fragments in the current text, from one headline to the next. With
  10779. prefix SUBTREE, display all fragments in the current subtree. With a
  10780. double prefix `C-u C-u', or when the cursor is before the first headline,
  10781. display all fragments in the buffer.
  10782. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10783. (interactive "P")
  10784. (org-remove-latex-fragment-image-overlays)
  10785. (save-excursion
  10786. (save-restriction
  10787. (let (beg end at msg)
  10788. (cond
  10789. ((or (equal subtree '(16))
  10790. (not (save-excursion
  10791. (re-search-backward (concat "^" outline-regexp) nil t))))
  10792. (setq beg (point-min) end (point-max)
  10793. msg "Creating images for buffer...%s"))
  10794. ((equal subtree '(4))
  10795. (org-back-to-heading)
  10796. (setq beg (point) end (org-end-of-subtree t)
  10797. msg "Creating images for subtree...%s"))
  10798. (t
  10799. (if (setq at (org-inside-LaTeX-fragment-p))
  10800. (goto-char (max (point-min) (- (cdr at) 2)))
  10801. (org-back-to-heading))
  10802. (setq beg (point) end (progn (outline-next-heading) (point))
  10803. msg (if at "Creating image...%s"
  10804. "Creating images for entry...%s"))))
  10805. (message msg "")
  10806. (narrow-to-region beg end)
  10807. (goto-char beg)
  10808. (org-format-latex
  10809. (concat "ltxpng/" (file-name-sans-extension
  10810. (file-name-nondirectory
  10811. buffer-file-name)))
  10812. default-directory 'overlays msg at 'forbuffer)
  10813. (message msg "done. Use `C-c C-c' to remove images.")))))
  10814. (defvar org-latex-regexps
  10815. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10816. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10817. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10818. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10819. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10820. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10821. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10822. "Regular expressions for matching embedded LaTeX.")
  10823. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10824. "Replace LaTeX fragments with links to an image, and produce images."
  10825. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10826. (let* ((prefixnodir (file-name-nondirectory prefix))
  10827. (absprefix (expand-file-name prefix dir))
  10828. (todir (file-name-directory absprefix))
  10829. (opt org-format-latex-options)
  10830. (matchers (plist-get opt :matchers))
  10831. (re-list org-latex-regexps)
  10832. (cnt 0) txt link beg end re e checkdir
  10833. m n block linkfile movefile ov)
  10834. ;; Check if there are old images files with this prefix, and remove them
  10835. (when (file-directory-p todir)
  10836. (mapc 'delete-file
  10837. (directory-files
  10838. todir 'full
  10839. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10840. ;; Check the different regular expressions
  10841. (while (setq e (pop re-list))
  10842. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10843. block (if (nth 3 e) "\n\n" ""))
  10844. (when (member m matchers)
  10845. (goto-char (point-min))
  10846. (while (re-search-forward re nil t)
  10847. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10848. (setq txt (match-string n)
  10849. beg (match-beginning n) end (match-end n)
  10850. cnt (1+ cnt)
  10851. linkfile (format "%s_%04d.png" prefix cnt)
  10852. movefile (format "%s_%04d.png" absprefix cnt)
  10853. link (concat block "[[file:" linkfile "]]" block))
  10854. (if msg (message msg cnt))
  10855. (goto-char beg)
  10856. (unless checkdir ; make sure the directory exists
  10857. (setq checkdir t)
  10858. (or (file-directory-p todir) (make-directory todir)))
  10859. (org-create-formula-image
  10860. txt movefile opt forbuffer)
  10861. (if overlays
  10862. (progn
  10863. (setq ov (org-make-overlay beg end))
  10864. (if (featurep 'xemacs)
  10865. (progn
  10866. (org-overlay-put ov 'invisible t)
  10867. (org-overlay-put
  10868. ov 'end-glyph
  10869. (make-glyph (vector 'png :file movefile))))
  10870. (org-overlay-put
  10871. ov 'display
  10872. (list 'image :type 'png :file movefile :ascent 'center)))
  10873. (push ov org-latex-fragment-image-overlays)
  10874. (goto-char end))
  10875. (delete-region beg end)
  10876. (insert link))))))))
  10877. ;; This function borrows from Ganesh Swami's latex2png.el
  10878. (defun org-create-formula-image (string tofile options buffer)
  10879. (let* ((tmpdir (if (featurep 'xemacs)
  10880. (temp-directory)
  10881. temporary-file-directory))
  10882. (texfilebase (make-temp-name
  10883. (expand-file-name "orgtex" tmpdir)))
  10884. (texfile (concat texfilebase ".tex"))
  10885. (dvifile (concat texfilebase ".dvi"))
  10886. (pngfile (concat texfilebase ".png"))
  10887. (fnh (if (featurep 'xemacs)
  10888. (font-height (get-face-font 'default))
  10889. (face-attribute 'default :height nil)))
  10890. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10891. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10892. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10893. "Black"))
  10894. (bg (or (plist-get options (if buffer :background :html-background))
  10895. "Transparent")))
  10896. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10897. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10898. (with-temp-file texfile
  10899. (insert org-format-latex-header
  10900. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10901. (let ((dir default-directory))
  10902. (condition-case nil
  10903. (progn
  10904. (cd tmpdir)
  10905. (call-process "latex" nil nil nil texfile))
  10906. (error nil))
  10907. (cd dir))
  10908. (if (not (file-exists-p dvifile))
  10909. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10910. (call-process "dvipng" nil nil nil
  10911. "-E" "-fg" fg "-bg" bg
  10912. "-D" dpi
  10913. ;;"-x" scale "-y" scale
  10914. "-T" "tight"
  10915. "-o" pngfile
  10916. dvifile)
  10917. (if (not (file-exists-p pngfile))
  10918. (progn (message "Failed to create png file from %s" texfile) nil)
  10919. ;; Use the requested file name and clean up
  10920. (copy-file pngfile tofile 'replace)
  10921. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10922. (delete-file (concat texfilebase e)))
  10923. pngfile))))
  10924. (defun org-dvipng-color (attr)
  10925. "Return an rgb color specification for dvipng."
  10926. (apply 'format "rgb %s %s %s"
  10927. (mapcar 'org-normalize-color
  10928. (color-values (face-attribute 'default attr nil)))))
  10929. (defun org-normalize-color (value)
  10930. "Return string to be used as color value for an RGB component."
  10931. (format "%g" (/ value 65535.0)))
  10932. ;;;; Key bindings
  10933. ;; Make `C-c C-x' a prefix key
  10934. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10935. ;; TAB key with modifiers
  10936. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10937. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10938. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10939. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10940. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10941. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10942. ;; The following line is necessary under Suse GNU/Linux
  10943. (unless (featurep 'xemacs)
  10944. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10945. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10946. (define-key org-mode-map [backtab] 'org-shifttab)
  10947. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10948. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10949. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10950. ;; Cursor keys with modifiers
  10951. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10952. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10953. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10954. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10955. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10956. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10957. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10958. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10959. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10960. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10961. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10962. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10963. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10964. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10965. ;;; Extra keys for tty access.
  10966. ;; We only set them when really needed because otherwise the
  10967. ;; menus don't show the simple keys
  10968. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10969. (not window-system))
  10970. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10971. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10972. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10973. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10974. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10975. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10976. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10977. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10978. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10979. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10980. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10981. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10982. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10983. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10984. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10985. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10986. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10987. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10988. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10989. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10990. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10991. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10992. ;; All the other keys
  10993. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10994. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10995. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10996. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10997. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10998. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10999. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11000. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11001. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11002. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11003. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11004. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11005. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11006. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11007. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11008. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11009. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11010. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11011. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11012. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11013. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  11014. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11015. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11016. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11017. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11018. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11019. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11020. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11021. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11022. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11023. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11024. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11025. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11026. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11027. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11028. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11029. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11030. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11031. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11032. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11033. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11034. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11035. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11036. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11037. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11038. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11039. (org-defkey org-mode-map "\C-m" 'org-return)
  11040. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11041. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11042. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11043. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11044. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11045. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  11046. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11047. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11048. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11049. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11050. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11051. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11052. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11053. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11054. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11055. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11056. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11057. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11058. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11059. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11060. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11061. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11062. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11063. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11064. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11065. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11066. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11067. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11068. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11069. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11070. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11071. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11072. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11073. (when (featurep 'xemacs)
  11074. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11075. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11076. (defun org-self-insert-command (N)
  11077. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11078. If the cursor is in a table looking at whitespace, the whitespace is
  11079. overwritten, and the table is not marked as requiring realignment."
  11080. (interactive "p")
  11081. (if (and (org-table-p)
  11082. (progn
  11083. ;; check if we blank the field, and if that triggers align
  11084. (and (featurep 'org-table) org-table-auto-blank-field
  11085. (member last-command
  11086. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11087. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11088. ;; got extra space, this field does not determine column width
  11089. (let (org-table-may-need-update) (org-table-blank-field))
  11090. ;; no extra space, this field may determine column width
  11091. (org-table-blank-field)))
  11092. t)
  11093. (eq N 1)
  11094. (looking-at "[^|\n]* |"))
  11095. (let (org-table-may-need-update)
  11096. (goto-char (1- (match-end 0)))
  11097. (delete-backward-char 1)
  11098. (goto-char (match-beginning 0))
  11099. (self-insert-command N))
  11100. (setq org-table-may-need-update t)
  11101. (self-insert-command N)
  11102. (org-fix-tags-on-the-fly)))
  11103. (defun org-fix-tags-on-the-fly ()
  11104. (when (and (equal (char-after (point-at-bol)) ?*)
  11105. (org-on-heading-p))
  11106. (org-align-tags-here org-tags-column)))
  11107. (defun org-delete-backward-char (N)
  11108. "Like `delete-backward-char', insert whitespace at field end in tables.
  11109. When deleting backwards, in tables this function will insert whitespace in
  11110. front of the next \"|\" separator, to keep the table aligned. The table will
  11111. still be marked for re-alignment if the field did fill the entire column,
  11112. because, in this case the deletion might narrow the column."
  11113. (interactive "p")
  11114. (if (and (org-table-p)
  11115. (eq N 1)
  11116. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11117. (looking-at ".*?|"))
  11118. (let ((pos (point))
  11119. (noalign (looking-at "[^|\n\r]* |"))
  11120. (c org-table-may-need-update))
  11121. (backward-delete-char N)
  11122. (skip-chars-forward "^|")
  11123. (insert " ")
  11124. (goto-char (1- pos))
  11125. ;; noalign: if there were two spaces at the end, this field
  11126. ;; does not determine the width of the column.
  11127. (if noalign (setq org-table-may-need-update c)))
  11128. (backward-delete-char N)
  11129. (org-fix-tags-on-the-fly)))
  11130. (defun org-delete-char (N)
  11131. "Like `delete-char', but insert whitespace at field end in tables.
  11132. When deleting characters, in tables this function will insert whitespace in
  11133. front of the next \"|\" separator, to keep the table aligned. The table will
  11134. still be marked for re-alignment if the field did fill the entire column,
  11135. because, in this case the deletion might narrow the column."
  11136. (interactive "p")
  11137. (if (and (org-table-p)
  11138. (not (bolp))
  11139. (not (= (char-after) ?|))
  11140. (eq N 1))
  11141. (if (looking-at ".*?|")
  11142. (let ((pos (point))
  11143. (noalign (looking-at "[^|\n\r]* |"))
  11144. (c org-table-may-need-update))
  11145. (replace-match (concat
  11146. (substring (match-string 0) 1 -1)
  11147. " |"))
  11148. (goto-char pos)
  11149. ;; noalign: if there were two spaces at the end, this field
  11150. ;; does not determine the width of the column.
  11151. (if noalign (setq org-table-may-need-update c)))
  11152. (delete-char N))
  11153. (delete-char N)
  11154. (org-fix-tags-on-the-fly)))
  11155. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11156. (put 'org-self-insert-command 'delete-selection t)
  11157. (put 'orgtbl-self-insert-command 'delete-selection t)
  11158. (put 'org-delete-char 'delete-selection 'supersede)
  11159. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11160. ;; Make `flyspell-mode' delay after some commands
  11161. (put 'org-self-insert-command 'flyspell-delayed t)
  11162. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11163. (put 'org-delete-char 'flyspell-delayed t)
  11164. (put 'org-delete-backward-char 'flyspell-delayed t)
  11165. ;; Make pabbrev-mode expand after org-mode commands
  11166. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11167. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11168. ;; How to do this: Measure non-white length of current string
  11169. ;; If equal to column width, we should realign.
  11170. (defun org-remap (map &rest commands)
  11171. "In MAP, remap the functions given in COMMANDS.
  11172. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11173. (let (new old)
  11174. (while commands
  11175. (setq old (pop commands) new (pop commands))
  11176. (if (fboundp 'command-remapping)
  11177. (org-defkey map (vector 'remap old) new)
  11178. (substitute-key-definition old new map global-map)))))
  11179. (when (eq org-enable-table-editor 'optimized)
  11180. ;; If the user wants maximum table support, we need to hijack
  11181. ;; some standard editing functions
  11182. (org-remap org-mode-map
  11183. 'self-insert-command 'org-self-insert-command
  11184. 'delete-char 'org-delete-char
  11185. 'delete-backward-char 'org-delete-backward-char)
  11186. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11187. (defun org-shiftcursor-error ()
  11188. "Throw an error because Shift-Cursor command was applied in wrong context."
  11189. (error "This command is active in special context like tables, headlines or timestamps"))
  11190. (defun org-shifttab (&optional arg)
  11191. "Global visibility cycling or move to previous table field.
  11192. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11193. on context.
  11194. See the individual commands for more information."
  11195. (interactive "P")
  11196. (cond
  11197. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11198. ((integerp arg)
  11199. (message "Content view to level: %d" arg)
  11200. (org-content (prefix-numeric-value arg))
  11201. (setq org-cycle-global-status 'overview))
  11202. (t (call-interactively 'org-global-cycle))))
  11203. (defun org-shiftmetaleft ()
  11204. "Promote subtree or delete table column.
  11205. Calls `org-promote-subtree', `org-outdent-item',
  11206. or `org-table-delete-column', depending on context.
  11207. See the individual commands for more information."
  11208. (interactive)
  11209. (cond
  11210. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11211. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11212. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11213. (t (org-shiftcursor-error))))
  11214. (defun org-shiftmetaright ()
  11215. "Demote subtree or insert table column.
  11216. Calls `org-demote-subtree', `org-indent-item',
  11217. or `org-table-insert-column', depending on context.
  11218. See the individual commands for more information."
  11219. (interactive)
  11220. (cond
  11221. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11222. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11223. ((org-at-item-p) (call-interactively 'org-indent-item))
  11224. (t (org-shiftcursor-error))))
  11225. (defun org-shiftmetaup (&optional arg)
  11226. "Move subtree up or kill table row.
  11227. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11228. `org-move-item-up' depending on context. See the individual commands
  11229. for more information."
  11230. (interactive "P")
  11231. (cond
  11232. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11233. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11234. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11235. (t (org-shiftcursor-error))))
  11236. (defun org-shiftmetadown (&optional arg)
  11237. "Move subtree down or insert table row.
  11238. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11239. `org-move-item-down', depending on context. See the individual
  11240. commands for more information."
  11241. (interactive "P")
  11242. (cond
  11243. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11244. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11245. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11246. (t (org-shiftcursor-error))))
  11247. (defun org-metaleft (&optional arg)
  11248. "Promote heading or move table column to left.
  11249. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11250. With no specific context, calls the Emacs default `backward-word'.
  11251. See the individual commands for more information."
  11252. (interactive "P")
  11253. (cond
  11254. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11255. ((or (org-on-heading-p) (org-region-active-p))
  11256. (call-interactively 'org-do-promote))
  11257. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11258. (t (call-interactively 'backward-word))))
  11259. (defun org-metaright (&optional arg)
  11260. "Demote subtree or move table column to right.
  11261. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11262. With no specific context, calls the Emacs default `forward-word'.
  11263. See the individual commands for more information."
  11264. (interactive "P")
  11265. (cond
  11266. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11267. ((or (org-on-heading-p) (org-region-active-p))
  11268. (call-interactively 'org-do-demote))
  11269. ((org-at-item-p) (call-interactively 'org-indent-item))
  11270. (t (call-interactively 'forward-word))))
  11271. (defun org-metaup (&optional arg)
  11272. "Move subtree up or move table row up.
  11273. Calls `org-move-subtree-up' or `org-table-move-row' or
  11274. `org-move-item-up', depending on context. See the individual commands
  11275. for more information."
  11276. (interactive "P")
  11277. (cond
  11278. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11279. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11280. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11281. (t (transpose-lines 1) (beginning-of-line -1))))
  11282. (defun org-metadown (&optional arg)
  11283. "Move subtree down or move table row down.
  11284. Calls `org-move-subtree-down' or `org-table-move-row' or
  11285. `org-move-item-down', depending on context. See the individual
  11286. commands for more information."
  11287. (interactive "P")
  11288. (cond
  11289. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11290. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11291. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11292. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11293. (defun org-shiftup (&optional arg)
  11294. "Increase item in timestamp or increase priority of current headline.
  11295. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11296. depending on context. See the individual commands for more information."
  11297. (interactive "P")
  11298. (cond
  11299. ((org-at-timestamp-p t)
  11300. (call-interactively (if org-edit-timestamp-down-means-later
  11301. 'org-timestamp-down 'org-timestamp-up)))
  11302. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11303. ((org-at-item-p) (call-interactively 'org-previous-item))
  11304. ((org-clocktable-try-shift 'up arg))
  11305. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11306. (defun org-shiftdown (&optional arg)
  11307. "Decrease item in timestamp or decrease priority of current headline.
  11308. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11309. depending on context. See the individual commands for more information."
  11310. (interactive "P")
  11311. (cond
  11312. ((org-at-timestamp-p t)
  11313. (call-interactively (if org-edit-timestamp-down-means-later
  11314. 'org-timestamp-up 'org-timestamp-down)))
  11315. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11316. ((org-clocktable-try-shift 'down arg))
  11317. (t (call-interactively 'org-next-item))))
  11318. (defun org-shiftright (&optional arg)
  11319. "Next TODO keyword or timestamp one day later, depending on context."
  11320. (interactive "P")
  11321. (cond
  11322. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11323. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11324. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11325. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11326. ((org-clocktable-try-shift 'right arg))
  11327. (t (org-shiftcursor-error))))
  11328. (defun org-shiftleft (&optional arg)
  11329. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11330. (interactive "P")
  11331. (cond
  11332. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11333. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11334. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11335. ((org-at-property-p)
  11336. (call-interactively 'org-property-previous-allowed-value))
  11337. ((org-clocktable-try-shift 'left arg))
  11338. (t (org-shiftcursor-error))))
  11339. (defun org-shiftcontrolright ()
  11340. "Switch to next TODO set."
  11341. (interactive)
  11342. (cond
  11343. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11344. (t (org-shiftcursor-error))))
  11345. (defun org-shiftcontrolleft ()
  11346. "Switch to previous TODO set."
  11347. (interactive)
  11348. (cond
  11349. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11350. (t (org-shiftcursor-error))))
  11351. (defun org-ctrl-c-ret ()
  11352. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11353. (interactive)
  11354. (cond
  11355. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11356. (t (call-interactively 'org-insert-heading))))
  11357. (defun org-copy-special ()
  11358. "Copy region in table or copy current subtree.
  11359. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11360. See the individual commands for more information."
  11361. (interactive)
  11362. (call-interactively
  11363. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11364. (defun org-cut-special ()
  11365. "Cut region in table or cut current subtree.
  11366. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11367. See the individual commands for more information."
  11368. (interactive)
  11369. (call-interactively
  11370. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11371. (defun org-paste-special (arg)
  11372. "Paste rectangular region into table, or past subtree relative to level.
  11373. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11374. See the individual commands for more information."
  11375. (interactive "P")
  11376. (if (org-at-table-p)
  11377. (org-table-paste-rectangle)
  11378. (org-paste-subtree arg)))
  11379. (defun org-ctrl-c-ctrl-c (&optional arg)
  11380. "Set tags in headline, or update according to changed information at point.
  11381. This command does many different things, depending on context:
  11382. - If the cursor is in a headline, prompt for tags and insert them
  11383. into the current line, aligned to `org-tags-column'. When called
  11384. with prefix arg, realign all tags in the current buffer.
  11385. - If the cursor is in one of the special #+KEYWORD lines, this
  11386. triggers scanning the buffer for these lines and updating the
  11387. information.
  11388. - If the cursor is inside a table, realign the table. This command
  11389. works even if the automatic table editor has been turned off.
  11390. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11391. the entire table.
  11392. - If the cursor is a the beginning of a dynamic block, update it.
  11393. - If the cursor is inside a table created by the table.el package,
  11394. activate that table.
  11395. - If the current buffer is a remember buffer, close note and file it.
  11396. with a prefix argument, file it without further interaction to the default
  11397. location.
  11398. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11399. links in this buffer.
  11400. - If the cursor is on a numbered item in a plain list, renumber the
  11401. ordered list.
  11402. - If the cursor is on a checkbox, toggle it."
  11403. (interactive "P")
  11404. (let ((org-enable-table-editor t))
  11405. (cond
  11406. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11407. org-occur-highlights
  11408. org-latex-fragment-image-overlays)
  11409. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11410. (org-remove-occur-highlights)
  11411. (org-remove-latex-fragment-image-overlays)
  11412. (message "Temporary highlights/overlays removed from current buffer"))
  11413. ((and (local-variable-p 'org-finish-function (current-buffer))
  11414. (fboundp org-finish-function))
  11415. (funcall org-finish-function))
  11416. ((org-at-property-p)
  11417. (call-interactively 'org-property-action))
  11418. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11419. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11420. ((org-at-table.el-p)
  11421. (require 'table)
  11422. (beginning-of-line 1)
  11423. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11424. (call-interactively 'table-recognize-table))
  11425. ((org-at-table-p)
  11426. (org-table-maybe-eval-formula)
  11427. (if arg
  11428. (call-interactively 'org-table-recalculate)
  11429. (org-table-maybe-recalculate-line))
  11430. (call-interactively 'org-table-align))
  11431. ((org-at-item-checkbox-p)
  11432. (call-interactively 'org-toggle-checkbox))
  11433. ((org-at-item-p)
  11434. (call-interactively 'org-maybe-renumber-ordered-list))
  11435. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11436. ;; Dynamic block
  11437. (beginning-of-line 1)
  11438. (org-update-dblock))
  11439. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11440. (cond
  11441. ((equal (match-string 1) "TBLFM")
  11442. ;; Recalculate the table before this line
  11443. (save-excursion
  11444. (beginning-of-line 1)
  11445. (skip-chars-backward " \r\n\t")
  11446. (if (org-at-table-p)
  11447. (org-call-with-arg 'org-table-recalculate t))))
  11448. (t
  11449. ; (org-set-regexps-and-options)
  11450. ; (org-restart-font-lock)
  11451. (let ((org-inhibit-startup t)) (org-mode-restart))
  11452. (message "Local setup has been refreshed"))))
  11453. (t (error "C-c C-c can do nothing useful at this location.")))))
  11454. (defun org-mode-restart ()
  11455. "Restart Org-mode, to scan again for special lines.
  11456. Also updates the keyword regular expressions."
  11457. (interactive)
  11458. (org-mode)
  11459. (message "Org-mode restarted"))
  11460. (defun org-kill-note-or-show-branches ()
  11461. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11462. (interactive)
  11463. (if (not org-finish-function)
  11464. (call-interactively 'show-branches)
  11465. (let ((org-note-abort t))
  11466. (funcall org-finish-function))))
  11467. (defun org-return (&optional indent)
  11468. "Goto next table row or insert a newline.
  11469. Calls `org-table-next-row' or `newline', depending on context.
  11470. See the individual commands for more information."
  11471. (interactive)
  11472. (cond
  11473. ((bobp) (if indent (newline-and-indent) (newline)))
  11474. ((and (org-at-heading-p)
  11475. (looking-at
  11476. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11477. (org-show-entry)
  11478. (end-of-line 1)
  11479. (newline))
  11480. ((org-at-table-p)
  11481. (org-table-justify-field-maybe)
  11482. (call-interactively 'org-table-next-row))
  11483. (t (if indent (newline-and-indent) (newline)))))
  11484. (defun org-return-indent ()
  11485. "Goto next table row or insert a newline and indent.
  11486. Calls `org-table-next-row' or `newline-and-indent', depending on
  11487. context. See the individual commands for more information."
  11488. (interactive)
  11489. (org-return t))
  11490. (defun org-ctrl-c-star ()
  11491. "Compute table, or change heading status of lines.
  11492. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11493. depending on context. This will also turn a plain list item or a normal
  11494. line into a subheading."
  11495. (interactive)
  11496. (cond
  11497. ((org-at-table-p)
  11498. (call-interactively 'org-table-recalculate))
  11499. ((org-region-active-p)
  11500. ;; Convert all lines in region to list items
  11501. (call-interactively 'org-toggle-region-headings))
  11502. ((org-on-heading-p)
  11503. (org-toggle-region-headings (point-at-bol)
  11504. (min (1+ (point-at-eol)) (point-max))))
  11505. ((org-at-item-p)
  11506. ;; Convert to heading
  11507. (let ((level (save-match-data
  11508. (save-excursion
  11509. (condition-case nil
  11510. (progn
  11511. (org-back-to-heading t)
  11512. (funcall outline-level))
  11513. (error 0))))))
  11514. (replace-match
  11515. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11516. (t (org-toggle-region-headings (point-at-bol)
  11517. (min (1+ (point-at-eol)) (point-max))))))
  11518. (defun org-ctrl-c-minus ()
  11519. "Insert separator line in table or modify bullet status of line.
  11520. Also turns a plain line or a region of lines into list items.
  11521. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11522. `org-cycle-list-bullet', depending on context."
  11523. (interactive)
  11524. (cond
  11525. ((org-at-table-p)
  11526. (call-interactively 'org-table-insert-hline))
  11527. ((org-on-heading-p)
  11528. ;; Convert to item
  11529. (save-excursion
  11530. (beginning-of-line 1)
  11531. (if (looking-at "\\*+ ")
  11532. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11533. ((org-region-active-p)
  11534. ;; Convert all lines in region to list items
  11535. (call-interactively 'org-toggle-region-items))
  11536. ((org-in-item-p)
  11537. (call-interactively 'org-cycle-list-bullet))
  11538. (t (org-toggle-region-items (point-at-bol)
  11539. (min (1+ (point-at-eol)) (point-max))))))
  11540. (defun org-toggle-region-items (beg end)
  11541. "Convert all lines in region to list items.
  11542. If the first line is already an item, convert all list items in the region
  11543. to normal lines."
  11544. (interactive "r")
  11545. (let (l2 l)
  11546. (save-excursion
  11547. (goto-char end)
  11548. (setq l2 (org-current-line))
  11549. (goto-char beg)
  11550. (beginning-of-line 1)
  11551. (setq l (1- (org-current-line)))
  11552. (if (org-at-item-p)
  11553. ;; We already have items, de-itemize
  11554. (while (< (setq l (1+ l)) l2)
  11555. (when (org-at-item-p)
  11556. (goto-char (match-beginning 2))
  11557. (delete-region (match-beginning 2) (match-end 2))
  11558. (and (looking-at "[ \t]+") (replace-match "")))
  11559. (beginning-of-line 2))
  11560. (while (< (setq l (1+ l)) l2)
  11561. (unless (org-at-item-p)
  11562. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11563. (replace-match "\\1- \\2")))
  11564. (beginning-of-line 2))))))
  11565. (defun org-toggle-region-headings (beg end)
  11566. "Convert all lines in region to list items.
  11567. If the first line is already an item, convert all list items in the region
  11568. to normal lines."
  11569. (interactive "r")
  11570. (let (l2 l)
  11571. (save-excursion
  11572. (goto-char end)
  11573. (setq l2 (org-current-line))
  11574. (goto-char beg)
  11575. (beginning-of-line 1)
  11576. (setq l (1- (org-current-line)))
  11577. (if (org-on-heading-p)
  11578. ;; We already have headlines, de-star them
  11579. (while (< (setq l (1+ l)) l2)
  11580. (when (org-on-heading-p t)
  11581. (and (looking-at outline-regexp) (replace-match "")))
  11582. (beginning-of-line 2))
  11583. (let* ((stars (save-excursion
  11584. (re-search-backward org-complex-heading-regexp nil t)
  11585. (or (match-string 1) "*")))
  11586. (add-stars (if org-odd-levels-only "**" "*"))
  11587. (rpl (concat stars add-stars " \\2")))
  11588. (while (< (setq l (1+ l)) l2)
  11589. (unless (org-on-heading-p)
  11590. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11591. (replace-match rpl)))
  11592. (beginning-of-line 2)))))))
  11593. (defun org-meta-return (&optional arg)
  11594. "Insert a new heading or wrap a region in a table.
  11595. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11596. See the individual commands for more information."
  11597. (interactive "P")
  11598. (cond
  11599. ((org-at-table-p)
  11600. (call-interactively 'org-table-wrap-region))
  11601. (t (call-interactively 'org-insert-heading))))
  11602. ;;; Menu entries
  11603. ;; Define the Org-mode menus
  11604. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11605. '("Tbl"
  11606. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11607. ["Next Field" org-cycle (org-at-table-p)]
  11608. ["Previous Field" org-shifttab (org-at-table-p)]
  11609. ["Next Row" org-return (org-at-table-p)]
  11610. "--"
  11611. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11612. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11613. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11614. "--"
  11615. ("Column"
  11616. ["Move Column Left" org-metaleft (org-at-table-p)]
  11617. ["Move Column Right" org-metaright (org-at-table-p)]
  11618. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11619. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11620. ("Row"
  11621. ["Move Row Up" org-metaup (org-at-table-p)]
  11622. ["Move Row Down" org-metadown (org-at-table-p)]
  11623. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11624. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11625. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11626. "--"
  11627. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11628. ("Rectangle"
  11629. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11630. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11631. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11632. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11633. "--"
  11634. ("Calculate"
  11635. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11636. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11637. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11638. "--"
  11639. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11640. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11641. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11642. "--"
  11643. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11644. "--"
  11645. ["Sum Column/Rectangle" org-table-sum
  11646. (or (org-at-table-p) (org-region-active-p))]
  11647. ["Which Column?" org-table-current-column (org-at-table-p)])
  11648. ["Debug Formulas"
  11649. org-table-toggle-formula-debugger
  11650. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11651. ["Show Col/Row Numbers"
  11652. org-table-toggle-coordinate-overlays
  11653. :style toggle
  11654. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11655. "--"
  11656. ["Create" org-table-create (and (not (org-at-table-p))
  11657. org-enable-table-editor)]
  11658. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11659. ["Import from File" org-table-import (not (org-at-table-p))]
  11660. ["Export to File" org-table-export (org-at-table-p)]
  11661. "--"
  11662. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11663. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11664. '("Org"
  11665. ("Show/Hide"
  11666. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11667. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11668. ["Sparse Tree..." org-sparse-tree t]
  11669. ["Reveal Context" org-reveal t]
  11670. ["Show All" show-all t]
  11671. "--"
  11672. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11673. "--"
  11674. ["New Heading" org-insert-heading t]
  11675. ("Navigate Headings"
  11676. ["Up" outline-up-heading t]
  11677. ["Next" outline-next-visible-heading t]
  11678. ["Previous" outline-previous-visible-heading t]
  11679. ["Next Same Level" outline-forward-same-level t]
  11680. ["Previous Same Level" outline-backward-same-level t]
  11681. "--"
  11682. ["Jump" org-goto t])
  11683. ("Edit Structure"
  11684. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11685. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11686. "--"
  11687. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11688. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11689. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11690. "--"
  11691. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11692. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11693. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11694. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11695. "--"
  11696. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11697. "--"
  11698. ["Convert to odd levels" org-convert-to-odd-levels t]
  11699. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11700. ("Editing"
  11701. ["Emphasis..." org-emphasize t])
  11702. ("Archive"
  11703. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11704. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11705. ; :active t :keys "C-u C-c C-x C-a"]
  11706. ["Sparse trees open ARCHIVE trees"
  11707. (setq org-sparse-tree-open-archived-trees
  11708. (not org-sparse-tree-open-archived-trees))
  11709. :style toggle :selected org-sparse-tree-open-archived-trees]
  11710. ["Cycling opens ARCHIVE trees"
  11711. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11712. :style toggle :selected org-cycle-open-archived-trees]
  11713. ["Agenda includes ARCHIVE trees"
  11714. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11715. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11716. "--"
  11717. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11718. ; ["Check and Move Children" (org-archive-subtree '(4))
  11719. ; :active t :keys "C-u C-c C-x C-s"]
  11720. )
  11721. "--"
  11722. ("TODO Lists"
  11723. ["TODO/DONE/-" org-todo t]
  11724. ("Select keyword"
  11725. ["Next keyword" org-shiftright (org-on-heading-p)]
  11726. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11727. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11728. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11729. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11730. ["Show TODO Tree" org-show-todo-tree t]
  11731. ["Global TODO list" org-todo-list t]
  11732. "--"
  11733. ["Set Priority" org-priority t]
  11734. ["Priority Up" org-shiftup t]
  11735. ["Priority Down" org-shiftdown t])
  11736. ("TAGS and Properties"
  11737. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11738. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11739. "--"
  11740. ["Set property" 'org-set-property t]
  11741. ["Column view of properties" org-columns t]
  11742. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11743. ("Dates and Scheduling"
  11744. ["Timestamp" org-time-stamp t]
  11745. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11746. ("Change Date"
  11747. ["1 Day Later" org-shiftright t]
  11748. ["1 Day Earlier" org-shiftleft t]
  11749. ["1 ... Later" org-shiftup t]
  11750. ["1 ... Earlier" org-shiftdown t])
  11751. ["Compute Time Range" org-evaluate-time-range t]
  11752. ["Schedule Item" org-schedule t]
  11753. ["Deadline" org-deadline t]
  11754. "--"
  11755. ["Custom time format" org-toggle-time-stamp-overlays
  11756. :style radio :selected org-display-custom-times]
  11757. "--"
  11758. ["Goto Calendar" org-goto-calendar t]
  11759. ["Date from Calendar" org-date-from-calendar t])
  11760. ("Logging work"
  11761. ["Clock in" org-clock-in t]
  11762. ["Clock out" org-clock-out t]
  11763. ["Clock cancel" org-clock-cancel t]
  11764. ["Goto running clock" org-clock-goto t]
  11765. ["Display times" org-clock-display t]
  11766. ["Create clock table" org-clock-report t]
  11767. "--"
  11768. ["Record DONE time"
  11769. (progn (setq org-log-done (not org-log-done))
  11770. (message "Switching to %s will %s record a timestamp"
  11771. (car org-done-keywords)
  11772. (if org-log-done "automatically" "not")))
  11773. :style toggle :selected org-log-done])
  11774. "--"
  11775. ["Agenda Command..." org-agenda t]
  11776. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11777. ("File List for Agenda")
  11778. ("Special views current file"
  11779. ["TODO Tree" org-show-todo-tree t]
  11780. ["Check Deadlines" org-check-deadlines t]
  11781. ["Timeline" org-timeline t]
  11782. ["Tags Tree" org-tags-sparse-tree t])
  11783. "--"
  11784. ("Hyperlinks"
  11785. ["Store Link (Global)" org-store-link t]
  11786. ["Insert Link" org-insert-link t]
  11787. ["Follow Link" org-open-at-point t]
  11788. "--"
  11789. ["Next link" org-next-link t]
  11790. ["Previous link" org-previous-link t]
  11791. "--"
  11792. ["Descriptive Links"
  11793. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11794. :style radio
  11795. :selected (member '(org-link) buffer-invisibility-spec)]
  11796. ["Literal Links"
  11797. (progn
  11798. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11799. :style radio
  11800. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11801. "--"
  11802. ["Export/Publish..." org-export t]
  11803. ("LaTeX"
  11804. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11805. :selected org-cdlatex-mode]
  11806. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11807. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11808. ["Modify math symbol" org-cdlatex-math-modify
  11809. (org-inside-LaTeX-fragment-p)]
  11810. ["Export LaTeX fragments as images"
  11811. (if (featurep 'org-exp)
  11812. (setq org-export-with-LaTeX-fragments
  11813. (not org-export-with-LaTeX-fragments))
  11814. (require 'org-exp))
  11815. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11816. org-export-with-LaTeX-fragments)])
  11817. "--"
  11818. ("Documentation"
  11819. ["Show Version" org-version t]
  11820. ["Info Documentation" org-info t])
  11821. ("Customize"
  11822. ["Browse Org Group" org-customize t]
  11823. "--"
  11824. ["Expand This Menu" org-create-customize-menu
  11825. (fboundp 'customize-menu-create)])
  11826. "--"
  11827. ["Refresh setup" org-mode-restart t]
  11828. ))
  11829. (defun org-info (&optional node)
  11830. "Read documentation for Org-mode in the info system.
  11831. With optional NODE, go directly to that node."
  11832. (interactive)
  11833. (info (format "(org)%s" (or node ""))))
  11834. (defun org-install-agenda-files-menu ()
  11835. (let ((bl (buffer-list)))
  11836. (save-excursion
  11837. (while bl
  11838. (set-buffer (pop bl))
  11839. (if (org-mode-p) (setq bl nil)))
  11840. (when (org-mode-p)
  11841. (easy-menu-change
  11842. '("Org") "File List for Agenda"
  11843. (append
  11844. (list
  11845. ["Edit File List" (org-edit-agenda-file-list) t]
  11846. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11847. ["Remove Current File from List" org-remove-file t]
  11848. ["Cycle through agenda files" org-cycle-agenda-files t]
  11849. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11850. "--")
  11851. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11852. ;;;; Documentation
  11853. (defun org-require-autoloaded-modules ()
  11854. (interactive)
  11855. (mapc 'require
  11856. '(org-agenda org-archive org-clock org-colview
  11857. org-exp org-export-latex org-publish
  11858. org-remember org-table)))
  11859. (defun org-customize ()
  11860. "Call the customize function with org as argument."
  11861. (interactive)
  11862. (org-load-modules-maybe)
  11863. (org-require-autoloaded-modules)
  11864. (customize-browse 'org))
  11865. (defun org-create-customize-menu ()
  11866. "Create a full customization menu for Org-mode, insert it into the menu."
  11867. (interactive)
  11868. (org-load-modules-maybe)
  11869. (org-require-autoloaded-modules)
  11870. (if (fboundp 'customize-menu-create)
  11871. (progn
  11872. (easy-menu-change
  11873. '("Org") "Customize"
  11874. `(["Browse Org group" org-customize t]
  11875. "--"
  11876. ,(customize-menu-create 'org)
  11877. ["Set" Custom-set t]
  11878. ["Save" Custom-save t]
  11879. ["Reset to Current" Custom-reset-current t]
  11880. ["Reset to Saved" Custom-reset-saved t]
  11881. ["Reset to Standard Settings" Custom-reset-standard t]))
  11882. (message "\"Org\"-menu now contains full customization menu"))
  11883. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11884. ;;;; Miscellaneous stuff
  11885. ;;; Generally useful functions
  11886. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11887. "Display the given MESSAGE as a warning."
  11888. (if (fboundp 'display-warning)
  11889. (display-warning 'org message
  11890. (if (featurep 'xemacs)
  11891. 'warning
  11892. :warning))
  11893. (let ((buf (get-buffer-create "*Org warnings*")))
  11894. (with-current-buffer buf
  11895. (goto-char (point-max))
  11896. (insert "Warning (Org): " message)
  11897. (unless (bolp)
  11898. (newline)))
  11899. (display-buffer buf)
  11900. (sit-for 0))))
  11901. (defun org-quote-csv-field (s)
  11902. "Quote field for inclusion in CSV material."
  11903. (if (string-match "[\",]" s)
  11904. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  11905. s))
  11906. (defun org-plist-delete (plist property)
  11907. "Delete PROPERTY from PLIST.
  11908. This is in contrast to merely setting it to 0."
  11909. (let (p)
  11910. (while plist
  11911. (if (not (eq property (car plist)))
  11912. (setq p (plist-put p (car plist) (nth 1 plist))))
  11913. (setq plist (cddr plist)))
  11914. p))
  11915. (defun org-force-self-insert (N)
  11916. "Needed to enforce self-insert under remapping."
  11917. (interactive "p")
  11918. (self-insert-command N))
  11919. (defun org-string-width (s)
  11920. "Compute width of string, ignoring invisible characters.
  11921. This ignores character with invisibility property `org-link', and also
  11922. characters with property `org-cwidth', because these will become invisible
  11923. upon the next fontification round."
  11924. (let (b l)
  11925. (when (or (eq t buffer-invisibility-spec)
  11926. (assq 'org-link buffer-invisibility-spec))
  11927. (while (setq b (text-property-any 0 (length s)
  11928. 'invisible 'org-link s))
  11929. (setq s (concat (substring s 0 b)
  11930. (substring s (or (next-single-property-change
  11931. b 'invisible s) (length s)))))))
  11932. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11933. (setq s (concat (substring s 0 b)
  11934. (substring s (or (next-single-property-change
  11935. b 'org-cwidth s) (length s))))))
  11936. (setq l (string-width s) b -1)
  11937. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11938. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11939. l))
  11940. (defun org-base-buffer (buffer)
  11941. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  11942. (if (not buffer)
  11943. buffer
  11944. (or (buffer-base-buffer buffer)
  11945. buffer)))
  11946. (defun org-trim (s)
  11947. "Remove whitespace at beginning and end of string."
  11948. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11949. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11950. s)
  11951. (defun org-wrap (string &optional width lines)
  11952. "Wrap string to either a number of lines, or a width in characters.
  11953. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11954. that costs. If there is a word longer than WIDTH, the text is actually
  11955. wrapped to the length of that word.
  11956. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11957. many lines, whatever width that takes.
  11958. The return value is a list of lines, without newlines at the end."
  11959. (let* ((words (org-split-string string "[ \t\n]+"))
  11960. (maxword (apply 'max (mapcar 'org-string-width words)))
  11961. w ll)
  11962. (cond (width
  11963. (org-do-wrap words (max maxword width)))
  11964. (lines
  11965. (setq w maxword)
  11966. (setq ll (org-do-wrap words maxword))
  11967. (if (<= (length ll) lines)
  11968. ll
  11969. (setq ll words)
  11970. (while (> (length ll) lines)
  11971. (setq w (1+ w))
  11972. (setq ll (org-do-wrap words w)))
  11973. ll))
  11974. (t (error "Cannot wrap this")))))
  11975. (defun org-do-wrap (words width)
  11976. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11977. (let (lines line)
  11978. (while words
  11979. (setq line (pop words))
  11980. (while (and words (< (+ (length line) (length (car words))) width))
  11981. (setq line (concat line " " (pop words))))
  11982. (setq lines (push line lines)))
  11983. (nreverse lines)))
  11984. (defun org-split-string (string &optional separators)
  11985. "Splits STRING into substrings at SEPARATORS.
  11986. No empty strings are returned if there are matches at the beginning
  11987. and end of string."
  11988. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11989. (start 0)
  11990. notfirst
  11991. (list nil))
  11992. (while (and (string-match rexp string
  11993. (if (and notfirst
  11994. (= start (match-beginning 0))
  11995. (< start (length string)))
  11996. (1+ start) start))
  11997. (< (match-beginning 0) (length string)))
  11998. (setq notfirst t)
  11999. (or (eq (match-beginning 0) 0)
  12000. (and (eq (match-beginning 0) (match-end 0))
  12001. (eq (match-beginning 0) start))
  12002. (setq list
  12003. (cons (substring string start (match-beginning 0))
  12004. list)))
  12005. (setq start (match-end 0)))
  12006. (or (eq start (length string))
  12007. (setq list
  12008. (cons (substring string start)
  12009. list)))
  12010. (nreverse list)))
  12011. (defun org-context ()
  12012. "Return a list of contexts of the current cursor position.
  12013. If several contexts apply, all are returned.
  12014. Each context entry is a list with a symbol naming the context, and
  12015. two positions indicating start and end of the context. Possible
  12016. contexts are:
  12017. :headline anywhere in a headline
  12018. :headline-stars on the leading stars in a headline
  12019. :todo-keyword on a TODO keyword (including DONE) in a headline
  12020. :tags on the TAGS in a headline
  12021. :priority on the priority cookie in a headline
  12022. :item on the first line of a plain list item
  12023. :item-bullet on the bullet/number of a plain list item
  12024. :checkbox on the checkbox in a plain list item
  12025. :table in an org-mode table
  12026. :table-special on a special filed in a table
  12027. :table-table in a table.el table
  12028. :link on a hyperlink
  12029. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12030. :target on a <<target>>
  12031. :radio-target on a <<<radio-target>>>
  12032. :latex-fragment on a LaTeX fragment
  12033. :latex-preview on a LaTeX fragment with overlayed preview image
  12034. This function expects the position to be visible because it uses font-lock
  12035. faces as a help to recognize the following contexts: :table-special, :link,
  12036. and :keyword."
  12037. (let* ((f (get-text-property (point) 'face))
  12038. (faces (if (listp f) f (list f)))
  12039. (p (point)) clist o)
  12040. ;; First the large context
  12041. (cond
  12042. ((org-on-heading-p t)
  12043. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12044. (when (progn
  12045. (beginning-of-line 1)
  12046. (looking-at org-todo-line-tags-regexp))
  12047. (push (org-point-in-group p 1 :headline-stars) clist)
  12048. (push (org-point-in-group p 2 :todo-keyword) clist)
  12049. (push (org-point-in-group p 4 :tags) clist))
  12050. (goto-char p)
  12051. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12052. (if (looking-at "\\[#[A-Z0-9]\\]")
  12053. (push (org-point-in-group p 0 :priority) clist)))
  12054. ((org-at-item-p)
  12055. (push (org-point-in-group p 2 :item-bullet) clist)
  12056. (push (list :item (point-at-bol)
  12057. (save-excursion (org-end-of-item) (point)))
  12058. clist)
  12059. (and (org-at-item-checkbox-p)
  12060. (push (org-point-in-group p 0 :checkbox) clist)))
  12061. ((org-at-table-p)
  12062. (push (list :table (org-table-begin) (org-table-end)) clist)
  12063. (if (memq 'org-formula faces)
  12064. (push (list :table-special
  12065. (previous-single-property-change p 'face)
  12066. (next-single-property-change p 'face)) clist)))
  12067. ((org-at-table-p 'any)
  12068. (push (list :table-table) clist)))
  12069. (goto-char p)
  12070. ;; Now the small context
  12071. (cond
  12072. ((org-at-timestamp-p)
  12073. (push (org-point-in-group p 0 :timestamp) clist))
  12074. ((memq 'org-link faces)
  12075. (push (list :link
  12076. (previous-single-property-change p 'face)
  12077. (next-single-property-change p 'face)) clist))
  12078. ((memq 'org-special-keyword faces)
  12079. (push (list :keyword
  12080. (previous-single-property-change p 'face)
  12081. (next-single-property-change p 'face)) clist))
  12082. ((org-on-target-p)
  12083. (push (org-point-in-group p 0 :target) clist)
  12084. (goto-char (1- (match-beginning 0)))
  12085. (if (looking-at org-radio-target-regexp)
  12086. (push (org-point-in-group p 0 :radio-target) clist))
  12087. (goto-char p))
  12088. ((setq o (car (delq nil
  12089. (mapcar
  12090. (lambda (x)
  12091. (if (memq x org-latex-fragment-image-overlays) x))
  12092. (org-overlays-at (point))))))
  12093. (push (list :latex-fragment
  12094. (org-overlay-start o) (org-overlay-end o)) clist)
  12095. (push (list :latex-preview
  12096. (org-overlay-start o) (org-overlay-end o)) clist))
  12097. ((org-inside-LaTeX-fragment-p)
  12098. ;; FIXME: positions wrong.
  12099. (push (list :latex-fragment (point) (point)) clist)))
  12100. (setq clist (nreverse (delq nil clist)))
  12101. clist))
  12102. ;; FIXME: Compare with at-regexp-p Do we need both?
  12103. (defun org-in-regexp (re &optional nlines visually)
  12104. "Check if point is inside a match of regexp.
  12105. Normally only the current line is checked, but you can include NLINES extra
  12106. lines both before and after point into the search.
  12107. If VISUALLY is set, require that the cursor is not after the match but
  12108. really on, so that the block visually is on the match."
  12109. (catch 'exit
  12110. (let ((pos (point))
  12111. (eol (point-at-eol (+ 1 (or nlines 0))))
  12112. (inc (if visually 1 0)))
  12113. (save-excursion
  12114. (beginning-of-line (- 1 (or nlines 0)))
  12115. (while (re-search-forward re eol t)
  12116. (if (and (<= (match-beginning 0) pos)
  12117. (>= (+ inc (match-end 0)) pos))
  12118. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12119. (defun org-at-regexp-p (regexp)
  12120. "Is point inside a match of REGEXP in the current line?"
  12121. (catch 'exit
  12122. (save-excursion
  12123. (let ((pos (point)) (end (point-at-eol)))
  12124. (beginning-of-line 1)
  12125. (while (re-search-forward regexp end t)
  12126. (if (and (<= (match-beginning 0) pos)
  12127. (>= (match-end 0) pos))
  12128. (throw 'exit t)))
  12129. nil))))
  12130. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12131. "Call `multi-occur' with buffers for all agenda files."
  12132. (interactive "sOrg-files matching: \np")
  12133. (let* ((files (org-agenda-files))
  12134. (tnames (mapcar 'file-truename files))
  12135. (extra org-agenda-text-search-extra-files)
  12136. f)
  12137. (when (eq (car extra) 'agenda-archives)
  12138. (setq extra (cdr extra))
  12139. (setq files (org-add-archive-files files)))
  12140. (while (setq f (pop extra))
  12141. (unless (member (file-truename f) tnames)
  12142. (add-to-list 'files f 'append)
  12143. (add-to-list 'tnames (file-truename f) 'append)))
  12144. (multi-occur
  12145. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12146. regexp)))
  12147. (if (boundp 'occur-mode-find-occurrence-hook)
  12148. ;; Emacs 23
  12149. (add-hook 'occur-mode-find-occurrence-hook
  12150. (lambda ()
  12151. (when (org-mode-p)
  12152. (org-reveal))))
  12153. ;; Emacs 22
  12154. (defadvice occur-mode-goto-occurrence
  12155. (after org-occur-reveal activate)
  12156. (and (org-mode-p) (org-reveal)))
  12157. (defadvice occur-mode-goto-occurrence-other-window
  12158. (after org-occur-reveal activate)
  12159. (and (org-mode-p) (org-reveal)))
  12160. (defadvice occur-mode-display-occurrence
  12161. (after org-occur-reveal activate)
  12162. (when (org-mode-p)
  12163. (let ((pos (occur-mode-find-occurrence)))
  12164. (with-current-buffer (marker-buffer pos)
  12165. (save-excursion
  12166. (goto-char pos)
  12167. (org-reveal)))))))
  12168. (defun org-uniquify (list)
  12169. "Remove duplicate elements from LIST."
  12170. (let (res)
  12171. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12172. res))
  12173. (defun org-delete-all (elts list)
  12174. "Remove all elements in ELTS from LIST."
  12175. (while elts
  12176. (setq list (delete (pop elts) list)))
  12177. list)
  12178. (defun org-back-over-empty-lines ()
  12179. "Move backwards over witespace, to the beginning of the first empty line.
  12180. Returns the number of empty lines passed."
  12181. (let ((pos (point)))
  12182. (skip-chars-backward " \t\n\r")
  12183. (beginning-of-line 2)
  12184. (goto-char (min (point) pos))
  12185. (count-lines (point) pos)))
  12186. (defun org-skip-whitespace ()
  12187. (skip-chars-forward " \t\n\r"))
  12188. (defun org-point-in-group (point group &optional context)
  12189. "Check if POINT is in match-group GROUP.
  12190. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12191. match. If the match group does ot exist or point is not inside it,
  12192. return nil."
  12193. (and (match-beginning group)
  12194. (>= point (match-beginning group))
  12195. (<= point (match-end group))
  12196. (if context
  12197. (list context (match-beginning group) (match-end group))
  12198. t)))
  12199. (defun org-switch-to-buffer-other-window (&rest args)
  12200. "Switch to buffer in a second window on the current frame.
  12201. In particular, do not allow pop-up frames."
  12202. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12203. special-display-function)
  12204. (apply 'switch-to-buffer-other-window args)))
  12205. (defun org-combine-plists (&rest plists)
  12206. "Create a single property list from all plists in PLISTS.
  12207. The process starts by copying the first list, and then setting properties
  12208. from the other lists. Settings in the last list are the most significant
  12209. ones and overrule settings in the other lists."
  12210. (let ((rtn (copy-sequence (pop plists)))
  12211. p v ls)
  12212. (while plists
  12213. (setq ls (pop plists))
  12214. (while ls
  12215. (setq p (pop ls) v (pop ls))
  12216. (setq rtn (plist-put rtn p v))))
  12217. rtn))
  12218. (defun org-move-line-down (arg)
  12219. "Move the current line down. With prefix argument, move it past ARG lines."
  12220. (interactive "p")
  12221. (let ((col (current-column))
  12222. beg end pos)
  12223. (beginning-of-line 1) (setq beg (point))
  12224. (beginning-of-line 2) (setq end (point))
  12225. (beginning-of-line (+ 1 arg))
  12226. (setq pos (move-marker (make-marker) (point)))
  12227. (insert (delete-and-extract-region beg end))
  12228. (goto-char pos)
  12229. (org-move-to-column col)))
  12230. (defun org-move-line-up (arg)
  12231. "Move the current line up. With prefix argument, move it past ARG lines."
  12232. (interactive "p")
  12233. (let ((col (current-column))
  12234. beg end pos)
  12235. (beginning-of-line 1) (setq beg (point))
  12236. (beginning-of-line 2) (setq end (point))
  12237. (beginning-of-line (- arg))
  12238. (setq pos (move-marker (make-marker) (point)))
  12239. (insert (delete-and-extract-region beg end))
  12240. (goto-char pos)
  12241. (org-move-to-column col)))
  12242. (defun org-replace-escapes (string table)
  12243. "Replace %-escapes in STRING with values in TABLE.
  12244. TABLE is an association list with keys like \"%a\" and string values.
  12245. The sequences in STRING may contain normal field width and padding information,
  12246. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12247. so values can contain further %-escapes if they are define later in TABLE."
  12248. (let ((case-fold-search nil)
  12249. e re rpl)
  12250. (while (setq e (pop table))
  12251. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12252. (while (string-match re string)
  12253. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12254. (cdr e)))
  12255. (setq string (replace-match rpl t t string))))
  12256. string))
  12257. (defun org-sublist (list start end)
  12258. "Return a section of LIST, from START to END.
  12259. Counting starts at 1."
  12260. (let (rtn (c start))
  12261. (setq list (nthcdr (1- start) list))
  12262. (while (and list (<= c end))
  12263. (push (pop list) rtn)
  12264. (setq c (1+ c)))
  12265. (nreverse rtn)))
  12266. (defun org-find-base-buffer-visiting (file)
  12267. "Like `find-buffer-visiting' but alway return the base buffer and
  12268. not an indirect buffer."
  12269. (let ((buf (find-buffer-visiting file)))
  12270. (if buf
  12271. (or (buffer-base-buffer buf) buf)
  12272. nil)))
  12273. (defun org-image-file-name-regexp ()
  12274. "Return regexp matching the file names of images."
  12275. (if (fboundp 'image-file-name-regexp)
  12276. (image-file-name-regexp)
  12277. (let ((image-file-name-extensions
  12278. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12279. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12280. (concat "\\."
  12281. (regexp-opt (nconc (mapcar 'upcase
  12282. image-file-name-extensions)
  12283. image-file-name-extensions)
  12284. t)
  12285. "\\'"))))
  12286. (defun org-file-image-p (file)
  12287. "Return non-nil if FILE is an image."
  12288. (save-match-data
  12289. (string-match (org-image-file-name-regexp) file)))
  12290. ;;; Paragraph filling stuff.
  12291. ;; We want this to be just right, so use the full arsenal.
  12292. (defun org-indent-line-function ()
  12293. "Indent line like previous, but further if previous was headline or item."
  12294. (interactive)
  12295. (let* ((pos (point))
  12296. (itemp (org-at-item-p))
  12297. column bpos bcol tpos tcol bullet btype bullet-type)
  12298. ;; Find the previous relevant line
  12299. (beginning-of-line 1)
  12300. (cond
  12301. ((looking-at "#") (setq column 0))
  12302. ((looking-at "\\*+ ") (setq column 0))
  12303. (t
  12304. (beginning-of-line 0)
  12305. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12306. (beginning-of-line 0))
  12307. (cond
  12308. ((looking-at "\\*+[ \t]+")
  12309. (goto-char (match-end 0))
  12310. (setq column (current-column)))
  12311. ((org-in-item-p)
  12312. (org-beginning-of-item)
  12313. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12314. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12315. (setq bpos (match-beginning 1) tpos (match-end 0)
  12316. bcol (progn (goto-char bpos) (current-column))
  12317. tcol (progn (goto-char tpos) (current-column))
  12318. bullet (match-string 1)
  12319. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12320. (if (> tcol (+ bcol org-description-max-indent))
  12321. (setq tcol (+ bcol 5)))
  12322. (if (not itemp)
  12323. (setq column tcol)
  12324. (goto-char pos)
  12325. (beginning-of-line 1)
  12326. (if (looking-at "\\S-")
  12327. (progn
  12328. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12329. (setq bullet (match-string 1)
  12330. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12331. (setq column (if (equal btype bullet-type) bcol tcol)))
  12332. (setq column (org-get-indentation)))))
  12333. (t (setq column (org-get-indentation))))))
  12334. (goto-char pos)
  12335. (if (<= (current-column) (current-indentation))
  12336. (org-indent-line-to column)
  12337. (save-excursion (org-indent-line-to column)))
  12338. (setq column (current-column))
  12339. (beginning-of-line 1)
  12340. (if (looking-at
  12341. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12342. (replace-match (concat "\\1" (format org-property-format
  12343. (match-string 2) (match-string 3)))
  12344. t nil))
  12345. (org-move-to-column column)))
  12346. (defun org-set-autofill-regexps ()
  12347. (interactive)
  12348. ;; In the paragraph separator we include headlines, because filling
  12349. ;; text in a line directly attached to a headline would otherwise
  12350. ;; fill the headline as well.
  12351. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12352. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12353. ;; The paragraph starter includes hand-formatted lists.
  12354. (org-set-local 'paragraph-start
  12355. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12356. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12357. ;; But only if the user has not turned off tables or fixed-width regions
  12358. (org-set-local
  12359. 'auto-fill-inhibit-regexp
  12360. (concat "\\*+ \\|#\\+"
  12361. "\\|[ \t]*" org-keyword-time-regexp
  12362. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12363. (concat
  12364. "\\|[ \t]*["
  12365. (if org-enable-table-editor "|" "")
  12366. (if org-enable-fixed-width-editor ":" "")
  12367. "]"))))
  12368. ;; We use our own fill-paragraph function, to make sure that tables
  12369. ;; and fixed-width regions are not wrapped. That function will pass
  12370. ;; through to `fill-paragraph' when appropriate.
  12371. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12372. ; Adaptive filling: To get full control, first make sure that
  12373. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12374. (org-set-local 'adaptive-fill-regexp "\000")
  12375. (org-set-local 'adaptive-fill-function
  12376. 'org-adaptive-fill-function)
  12377. (org-set-local
  12378. 'align-mode-rules-list
  12379. '((org-in-buffer-settings
  12380. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12381. (modes . '(org-mode))))))
  12382. (defun org-fill-paragraph (&optional justify)
  12383. "Re-align a table, pass through to fill-paragraph if no table."
  12384. (let ((table-p (org-at-table-p))
  12385. (table.el-p (org-at-table.el-p)))
  12386. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12387. (save-excursion (goto-char (point-at-bol))
  12388. (looking-at outline-regexp)))
  12389. t) ; skip headlines
  12390. (table.el-p t) ; skip table.el tables
  12391. (table-p (org-table-align) t) ; align org-mode tables
  12392. (t nil)))) ; call paragraph-fill
  12393. ;; For reference, this is the default value of adaptive-fill-regexp
  12394. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12395. (defun org-adaptive-fill-function ()
  12396. "Return a fill prefix for org-mode files.
  12397. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12398. work correctly."
  12399. (cond ((looking-at "#[ \t]+")
  12400. (match-string 0))
  12401. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12402. (save-excursion
  12403. (if (> (match-end 1) (+ (match-beginning 1)
  12404. org-description-max-indent))
  12405. (goto-char (+ (match-beginning 1) 5))
  12406. (goto-char (match-end 0)))
  12407. (make-string (current-column) ?\ )))
  12408. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12409. (save-excursion
  12410. (goto-char (match-end 0))
  12411. (make-string (current-column) ?\ )))
  12412. (t nil)))
  12413. ;;; Other stuff.
  12414. (defun org-toggle-fixed-width-section (arg)
  12415. "Toggle the fixed-width export.
  12416. If there is no active region, the QUOTE keyword at the current headline is
  12417. inserted or removed. When present, it causes the text between this headline
  12418. and the next to be exported as fixed-width text, and unmodified.
  12419. If there is an active region, this command adds or removes a colon as the
  12420. first character of this line. If the first character of a line is a colon,
  12421. this line is also exported in fixed-width font."
  12422. (interactive "P")
  12423. (let* ((cc 0)
  12424. (regionp (org-region-active-p))
  12425. (beg (if regionp (region-beginning) (point)))
  12426. (end (if regionp (region-end)))
  12427. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12428. (case-fold-search nil)
  12429. (re "[ \t]*\\(:\\)")
  12430. off)
  12431. (if regionp
  12432. (save-excursion
  12433. (goto-char beg)
  12434. (setq cc (current-column))
  12435. (beginning-of-line 1)
  12436. (setq off (looking-at re))
  12437. (while (> nlines 0)
  12438. (setq nlines (1- nlines))
  12439. (beginning-of-line 1)
  12440. (cond
  12441. (arg
  12442. (org-move-to-column cc t)
  12443. (insert ":\n")
  12444. (forward-line -1))
  12445. ((and off (looking-at re))
  12446. (replace-match "" t t nil 1))
  12447. ((not off) (org-move-to-column cc t) (insert ":")))
  12448. (forward-line 1)))
  12449. (save-excursion
  12450. (org-back-to-heading)
  12451. (if (looking-at (concat outline-regexp
  12452. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12453. (replace-match "" t t nil 1)
  12454. (if (looking-at outline-regexp)
  12455. (progn
  12456. (goto-char (match-end 0))
  12457. (insert org-quote-string " "))))))))
  12458. ;;;; Functions extending outline functionality
  12459. (defun org-beginning-of-line (&optional arg)
  12460. "Go to the beginning of the current line. If that is invisible, continue
  12461. to a visible line beginning. This makes the function of C-a more intuitive.
  12462. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12463. first attempt, and only move to after the tags when the cursor is already
  12464. beyond the end of the headline."
  12465. (interactive "P")
  12466. (let ((pos (point)))
  12467. (beginning-of-line 1)
  12468. (if (bobp)
  12469. nil
  12470. (backward-char 1)
  12471. (if (org-invisible-p)
  12472. (while (and (not (bobp)) (org-invisible-p))
  12473. (backward-char 1)
  12474. (beginning-of-line 1))
  12475. (forward-char 1)))
  12476. (when org-special-ctrl-a/e
  12477. (cond
  12478. ((and (looking-at org-todo-line-regexp)
  12479. (= (char-after (match-end 1)) ?\ ))
  12480. (goto-char
  12481. (if (eq org-special-ctrl-a/e t)
  12482. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12483. ((= pos (point)) (match-beginning 3))
  12484. (t (point)))
  12485. (cond ((> pos (point)) (point))
  12486. ((not (eq last-command this-command)) (point))
  12487. (t (match-beginning 3))))))
  12488. ((org-at-item-p)
  12489. (goto-char
  12490. (if (eq org-special-ctrl-a/e t)
  12491. (cond ((> pos (match-end 4)) (match-end 4))
  12492. ((= pos (point)) (match-end 4))
  12493. (t (point)))
  12494. (cond ((> pos (point)) (point))
  12495. ((not (eq last-command this-command)) (point))
  12496. (t (match-end 4))))))))))
  12497. (defun org-end-of-line (&optional arg)
  12498. "Go to the end of the line.
  12499. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12500. first attempt, and only move to after the tags when the cursor is already
  12501. beyond the end of the headline."
  12502. (interactive "P")
  12503. (if (or (not org-special-ctrl-a/e)
  12504. (not (org-on-heading-p)))
  12505. (end-of-line arg)
  12506. (let ((pos (point)))
  12507. (beginning-of-line 1)
  12508. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12509. (if (eq org-special-ctrl-a/e t)
  12510. (if (or (< pos (match-beginning 1))
  12511. (= pos (match-end 0)))
  12512. (goto-char (match-beginning 1))
  12513. (goto-char (match-end 0)))
  12514. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12515. (goto-char (match-end 0))
  12516. (goto-char (match-beginning 1))))
  12517. (end-of-line arg)))))
  12518. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12519. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12520. (defun org-kill-line (&optional arg)
  12521. "Kill line, to tags or end of line."
  12522. (interactive "P")
  12523. (cond
  12524. ((or (not org-special-ctrl-k)
  12525. (bolp)
  12526. (not (org-on-heading-p)))
  12527. (call-interactively 'kill-line))
  12528. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12529. (kill-region (point) (match-beginning 1))
  12530. (org-set-tags nil t))
  12531. (t (kill-region (point) (point-at-eol)))))
  12532. (define-key org-mode-map "\C-k" 'org-kill-line)
  12533. (defun org-invisible-p ()
  12534. "Check if point is at a character currently not visible."
  12535. ;; Early versions of noutline don't have `outline-invisible-p'.
  12536. (if (fboundp 'outline-invisible-p)
  12537. (outline-invisible-p)
  12538. (get-char-property (point) 'invisible)))
  12539. (defun org-invisible-p2 ()
  12540. "Check if point is at a character currently not visible."
  12541. (save-excursion
  12542. (if (and (eolp) (not (bobp))) (backward-char 1))
  12543. ;; Early versions of noutline don't have `outline-invisible-p'.
  12544. (if (fboundp 'outline-invisible-p)
  12545. (outline-invisible-p)
  12546. (get-char-property (point) 'invisible))))
  12547. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12548. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12549. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12550. (defun org-at-heading-or-item-p ()
  12551. (or (org-on-heading-p) (org-at-item-p)))
  12552. (defun org-on-target-p ()
  12553. (or (org-in-regexp org-radio-target-regexp)
  12554. (org-in-regexp org-target-regexp)))
  12555. (defun org-up-heading-all (arg)
  12556. "Move to the heading line of which the present line is a subheading.
  12557. This function considers both visible and invisible heading lines.
  12558. With argument, move up ARG levels."
  12559. (if (fboundp 'outline-up-heading-all)
  12560. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12561. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12562. (defun org-up-heading-safe ()
  12563. "Move to the heading line of which the present line is a subheading.
  12564. This version will not throw an error. It will return the level of the
  12565. headline found, or nil if no higher level is found."
  12566. (let ((pos (point)) start-level level
  12567. (re (concat "^" outline-regexp)))
  12568. (catch 'exit
  12569. (outline-back-to-heading t)
  12570. (setq start-level (funcall outline-level))
  12571. (if (equal start-level 1) (throw 'exit nil))
  12572. (while (re-search-backward re nil t)
  12573. (setq level (funcall outline-level))
  12574. (if (< level start-level) (throw 'exit level)))
  12575. nil)))
  12576. (defun org-first-sibling-p ()
  12577. "Is this heading the first child of its parents?"
  12578. (interactive)
  12579. (let ((re (concat "^" outline-regexp))
  12580. level l)
  12581. (unless (org-at-heading-p t)
  12582. (error "Not at a heading"))
  12583. (setq level (funcall outline-level))
  12584. (save-excursion
  12585. (if (not (re-search-backward re nil t))
  12586. t
  12587. (setq l (funcall outline-level))
  12588. (< l level)))))
  12589. (defun org-goto-sibling (&optional previous)
  12590. "Goto the next sibling, even if it is invisible.
  12591. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12592. when a sibling was found. When none is found, return nil and don't
  12593. move point."
  12594. (let ((fun (if previous 're-search-backward 're-search-forward))
  12595. (pos (point))
  12596. (re (concat "^" outline-regexp))
  12597. level l)
  12598. (when (condition-case nil (org-back-to-heading t) (error nil))
  12599. (setq level (funcall outline-level))
  12600. (catch 'exit
  12601. (or previous (forward-char 1))
  12602. (while (funcall fun re nil t)
  12603. (setq l (funcall outline-level))
  12604. (when (< l level) (goto-char pos) (throw 'exit nil))
  12605. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12606. (goto-char pos)
  12607. nil))))
  12608. (defun org-show-siblings ()
  12609. "Show all siblings of the current headline."
  12610. (save-excursion
  12611. (while (org-goto-sibling) (org-flag-heading nil)))
  12612. (save-excursion
  12613. (while (org-goto-sibling 'previous)
  12614. (org-flag-heading nil))))
  12615. (defun org-show-hidden-entry ()
  12616. "Show an entry where even the heading is hidden."
  12617. (save-excursion
  12618. (org-show-entry)))
  12619. (defun org-flag-heading (flag &optional entry)
  12620. "Flag the current heading. FLAG non-nil means make invisible.
  12621. When ENTRY is non-nil, show the entire entry."
  12622. (save-excursion
  12623. (org-back-to-heading t)
  12624. ;; Check if we should show the entire entry
  12625. (if entry
  12626. (progn
  12627. (org-show-entry)
  12628. (save-excursion
  12629. (and (outline-next-heading)
  12630. (org-flag-heading nil))))
  12631. (outline-flag-region (max (point-min) (1- (point)))
  12632. (save-excursion (outline-end-of-heading) (point))
  12633. flag))))
  12634. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12635. ;; This is an exact copy of the original function, but it uses
  12636. ;; `org-back-to-heading', to make it work also in invisible
  12637. ;; trees. And is uses an invisible-OK argument.
  12638. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12639. (org-back-to-heading invisible-OK)
  12640. (let ((first t)
  12641. (level (funcall outline-level)))
  12642. (while (and (not (eobp))
  12643. (or first (> (funcall outline-level) level)))
  12644. (setq first nil)
  12645. (outline-next-heading))
  12646. (unless to-heading
  12647. (if (memq (preceding-char) '(?\n ?\^M))
  12648. (progn
  12649. ;; Go to end of line before heading
  12650. (forward-char -1)
  12651. (if (memq (preceding-char) '(?\n ?\^M))
  12652. ;; leave blank line before heading
  12653. (forward-char -1))))))
  12654. (point))
  12655. (defun org-show-subtree ()
  12656. "Show everything after this heading at deeper levels."
  12657. (outline-flag-region
  12658. (point)
  12659. (save-excursion
  12660. (outline-end-of-subtree) (outline-next-heading) (point))
  12661. nil))
  12662. (defun org-show-entry ()
  12663. "Show the body directly following this heading.
  12664. Show the heading too, if it is currently invisible."
  12665. (interactive)
  12666. (save-excursion
  12667. (condition-case nil
  12668. (progn
  12669. (org-back-to-heading t)
  12670. (outline-flag-region
  12671. (max (point-min) (1- (point)))
  12672. (save-excursion
  12673. (re-search-forward
  12674. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12675. (or (match-beginning 1) (point-max)))
  12676. nil))
  12677. (error nil))))
  12678. (defun org-make-options-regexp (kwds)
  12679. "Make a regular expression for keyword lines."
  12680. (concat
  12681. "^"
  12682. "#?[ \t]*\\+\\("
  12683. (mapconcat 'regexp-quote kwds "\\|")
  12684. "\\):[ \t]*"
  12685. "\\(.+\\)"))
  12686. ;; Make isearch reveal the necessary context
  12687. (defun org-isearch-end ()
  12688. "Reveal context after isearch exits."
  12689. (when isearch-success ; only if search was successful
  12690. (if (featurep 'xemacs)
  12691. ;; Under XEmacs, the hook is run in the correct place,
  12692. ;; we directly show the context.
  12693. (org-show-context 'isearch)
  12694. ;; In Emacs the hook runs *before* restoring the overlays.
  12695. ;; So we have to use a one-time post-command-hook to do this.
  12696. ;; (Emacs 22 has a special variable, see function `org-mode')
  12697. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12698. isearch-mode-end-hook-quit)
  12699. ;; Only when the isearch was not quitted.
  12700. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12701. 'append 'local)))))
  12702. (defun org-isearch-post-command ()
  12703. "Remove self from hook, and show context."
  12704. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12705. (org-show-context 'isearch))
  12706. ;;;; Integration with and fixes for other packages
  12707. ;;; Imenu support
  12708. (defvar org-imenu-markers nil
  12709. "All markers currently used by Imenu.")
  12710. (make-variable-buffer-local 'org-imenu-markers)
  12711. (defun org-imenu-new-marker (&optional pos)
  12712. "Return a new marker for use by Imenu, and remember the marker."
  12713. (let ((m (make-marker)))
  12714. (move-marker m (or pos (point)))
  12715. (push m org-imenu-markers)
  12716. m))
  12717. (defun org-imenu-get-tree ()
  12718. "Produce the index for Imenu."
  12719. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12720. (setq org-imenu-markers nil)
  12721. (let* ((n org-imenu-depth)
  12722. (re (concat "^" outline-regexp))
  12723. (subs (make-vector (1+ n) nil))
  12724. (last-level 0)
  12725. m tree level head)
  12726. (save-excursion
  12727. (save-restriction
  12728. (widen)
  12729. (goto-char (point-max))
  12730. (while (re-search-backward re nil t)
  12731. (setq level (org-reduced-level (funcall outline-level)))
  12732. (when (<= level n)
  12733. (looking-at org-complex-heading-regexp)
  12734. (setq head (org-match-string-no-properties 4)
  12735. m (org-imenu-new-marker))
  12736. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12737. (if (>= level last-level)
  12738. (push (cons head m) (aref subs level))
  12739. (push (cons head (aref subs (1+ level))) (aref subs level))
  12740. (loop for i from (1+ level) to n do (aset subs i nil)))
  12741. (setq last-level level)))))
  12742. (aref subs 1)))
  12743. (eval-after-load "imenu"
  12744. '(progn
  12745. (add-hook 'imenu-after-jump-hook
  12746. (lambda () (org-show-context 'org-goto)))))
  12747. ;; Speedbar support
  12748. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12749. "Overlay marking the agenda restriction line in speedbar.")
  12750. (org-overlay-put org-speedbar-restriction-lock-overlay
  12751. 'face 'org-agenda-restriction-lock)
  12752. (org-overlay-put org-speedbar-restriction-lock-overlay
  12753. 'help-echo "Agendas are currently limited to this item.")
  12754. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12755. (defun org-speedbar-set-agenda-restriction ()
  12756. "Restrict future agenda commands to the location at point in speedbar.
  12757. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12758. (interactive)
  12759. (require 'org-agenda)
  12760. (let (p m tp np dir txt w)
  12761. (cond
  12762. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12763. 'org-imenu t))
  12764. (setq m (get-text-property p 'org-imenu-marker))
  12765. (save-excursion
  12766. (save-restriction
  12767. (set-buffer (marker-buffer m))
  12768. (goto-char m)
  12769. (org-agenda-set-restriction-lock 'subtree))))
  12770. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12771. 'speedbar-function 'speedbar-find-file))
  12772. (setq tp (previous-single-property-change
  12773. (1+ p) 'speedbar-function)
  12774. np (next-single-property-change
  12775. tp 'speedbar-function)
  12776. dir (speedbar-line-directory)
  12777. txt (buffer-substring-no-properties (or tp (point-min))
  12778. (or np (point-max))))
  12779. (save-excursion
  12780. (save-restriction
  12781. (set-buffer (find-file-noselect
  12782. (let ((default-directory dir))
  12783. (expand-file-name txt))))
  12784. (unless (org-mode-p)
  12785. (error "Cannot restrict to non-Org-mode file"))
  12786. (org-agenda-set-restriction-lock 'file))))
  12787. (t (error "Don't know how to restrict Org-mode's agenda")))
  12788. (org-move-overlay org-speedbar-restriction-lock-overlay
  12789. (point-at-bol) (point-at-eol))
  12790. (setq current-prefix-arg nil)
  12791. (org-agenda-maybe-redo)))
  12792. (eval-after-load "speedbar"
  12793. '(progn
  12794. (speedbar-add-supported-extension ".org")
  12795. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12796. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12797. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12798. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12799. (add-hook 'speedbar-visiting-tag-hook
  12800. (lambda () (org-show-context 'org-goto)))))
  12801. ;;; Fixes and Hacks for problems with other packages
  12802. ;; Make flyspell not check words in links, to not mess up our keymap
  12803. (defun org-mode-flyspell-verify ()
  12804. "Don't let flyspell put overlays at active buttons."
  12805. (not (get-text-property (point) 'keymap)))
  12806. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12807. (eval-after-load "bookmark"
  12808. '(if (boundp 'bookmark-after-jump-hook)
  12809. ;; We can use the hook
  12810. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12811. ;; Hook not available, use advice
  12812. (defadvice bookmark-jump (after org-make-visible activate)
  12813. "Make the position visible."
  12814. (org-bookmark-jump-unhide))))
  12815. (defun org-bookmark-jump-unhide ()
  12816. "Unhide the current position, to show the bookmark location."
  12817. (and (org-mode-p)
  12818. (or (org-invisible-p)
  12819. (save-excursion (goto-char (max (point-min) (1- (point))))
  12820. (org-invisible-p)))
  12821. (org-show-context 'bookmark-jump)))
  12822. ;; Make session.el ignore our circular variable
  12823. (eval-after-load "session"
  12824. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12825. ;;;; Experimental code
  12826. (defun org-closed-in-range ()
  12827. "Sparse tree of items closed in a certain time range.
  12828. Still experimental, may disappear in the future."
  12829. (interactive)
  12830. ;; Get the time interval from the user.
  12831. (let* ((time1 (time-to-seconds
  12832. (org-read-date nil 'to-time nil "Starting date: ")))
  12833. (time2 (time-to-seconds
  12834. (org-read-date nil 'to-time nil "End date:")))
  12835. ;; callback function
  12836. (callback (lambda ()
  12837. (let ((time
  12838. (time-to-seconds
  12839. (apply 'encode-time
  12840. (org-parse-time-string
  12841. (match-string 1))))))
  12842. ;; check if time in interval
  12843. (and (>= time time1) (<= time time2))))))
  12844. ;; make tree, check each match with the callback
  12845. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12846. ;;;; Finish up
  12847. (provide 'org)
  12848. (run-hooks 'org-load-hook)
  12849. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12850. ;;; org.el ends here