org.el 542 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731
  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.06b
  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.06b"
  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 " id: Global id's for identifying entries" org-id)
  144. (const :tag " info: Links to Info nodes" org-info)
  145. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  146. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  147. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  148. (const :tag " mew Links to Mew folders/messages" org-mew)
  149. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  150. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  151. (const :tag " vm: Links to VM folders/messages" org-vm)
  152. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  153. (const :tag " mouse: Additional mouse support" org-mouse)
  154. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  155. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  156. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  157. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  158. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  159. (const :tag "C eval: Include command output as text" org-eval)
  160. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  161. (const :tag "C id: Global id's for identifying entries" org-id)
  162. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  163. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  164. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  165. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  166. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  167. (const :tag "C registry: A registry for Org links" org-registry)
  168. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  169. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  170. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  171. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  172. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  173. (defgroup org-startup nil
  174. "Options concerning startup of Org-mode."
  175. :tag "Org Startup"
  176. :group 'org)
  177. (defcustom org-startup-folded t
  178. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  179. This can also be configured on a per-file basis by adding one of
  180. the following lines anywhere in the buffer:
  181. #+STARTUP: fold
  182. #+STARTUP: nofold
  183. #+STARTUP: content"
  184. :group 'org-startup
  185. :type '(choice
  186. (const :tag "nofold: show all" nil)
  187. (const :tag "fold: overview" t)
  188. (const :tag "content: all headlines" content)))
  189. (defcustom org-startup-truncated t
  190. "Non-nil means, entering Org-mode will set `truncate-lines'.
  191. This is useful since some lines containing links can be very long and
  192. uninteresting. Also tables look terrible when wrapped."
  193. :group 'org-startup
  194. :type 'boolean)
  195. (defcustom org-startup-align-all-tables nil
  196. "Non-nil means, align all tables when visiting a file.
  197. This is useful when the column width in tables is forced with <N> cookies
  198. in table fields. Such tables will look correct only after the first re-align.
  199. This can also be configured on a per-file basis by adding one of
  200. the following lines anywhere in the buffer:
  201. #+STARTUP: align
  202. #+STARTUP: noalign"
  203. :group 'org-startup
  204. :type 'boolean)
  205. (defcustom org-insert-mode-line-in-empty-file nil
  206. "Non-nil means insert the first line setting Org-mode in empty files.
  207. When the function `org-mode' is called interactively in an empty file, this
  208. normally means that the file name does not automatically trigger Org-mode.
  209. To ensure that the file will always be in Org-mode in the future, a
  210. line enforcing Org-mode will be inserted into the buffer, if this option
  211. has been set."
  212. :group 'org-startup
  213. :type 'boolean)
  214. (defcustom org-replace-disputed-keys nil
  215. "Non-nil means use alternative key bindings for some keys.
  216. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  217. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  218. If you want to use Org-mode together with one of these other modes,
  219. or more generally if you would like to move some Org-mode commands to
  220. other keys, set this variable and configure the keys with the variable
  221. `org-disputed-keys'.
  222. This option is only relevant at load-time of Org-mode, and must be set
  223. *before* org.el is loaded. Changing it requires a restart of Emacs to
  224. become effective."
  225. :group 'org-startup
  226. :type 'boolean)
  227. (defcustom org-use-extra-keys nil
  228. "Non-nil means use extra key sequence definitions for certain
  229. commands. This happens automatically if you run XEmacs or if
  230. window-system is nil. This variable lets you do the same
  231. manually. You must set it before loading org.
  232. Example: on Carbon Emacs 22 running graphically, with an external
  233. keyboard on a Powerbook, the default way of setting M-left might
  234. not work for either Alt or ESC. Setting this variable will make
  235. it work for ESC."
  236. :group 'org-startup
  237. :type 'boolean)
  238. (if (fboundp 'defvaralias)
  239. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  240. (defcustom org-disputed-keys
  241. '(([(shift up)] . [(meta p)])
  242. ([(shift down)] . [(meta n)])
  243. ([(shift left)] . [(meta -)])
  244. ([(shift right)] . [(meta +)])
  245. ([(control shift right)] . [(meta shift +)])
  246. ([(control shift left)] . [(meta shift -)]))
  247. "Keys for which Org-mode and other modes compete.
  248. This is an alist, cars are the default keys, second element specifies
  249. the alternative to use when `org-replace-disputed-keys' is t.
  250. Keys can be specified in any syntax supported by `define-key'.
  251. The value of this option takes effect only at Org-mode's startup,
  252. therefore you'll have to restart Emacs to apply it after changing."
  253. :group 'org-startup
  254. :type 'alist)
  255. (defun org-key (key)
  256. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  257. Or return the original if not disputed."
  258. (if org-replace-disputed-keys
  259. (let* ((nkey (key-description key))
  260. (x (org-find-if (lambda (x)
  261. (equal (key-description (car x)) nkey))
  262. org-disputed-keys)))
  263. (if x (cdr x) key))
  264. key))
  265. (defun org-find-if (predicate seq)
  266. (catch 'exit
  267. (while seq
  268. (if (funcall predicate (car seq))
  269. (throw 'exit (car seq))
  270. (pop seq)))))
  271. (defun org-defkey (keymap key def)
  272. "Define a key, possibly translated, as returned by `org-key'."
  273. (define-key keymap (org-key key) def))
  274. (defcustom org-ellipsis nil
  275. "The ellipsis to use in the Org-mode outline.
  276. When nil, just use the standard three dots. When a string, use that instead,
  277. When a face, use the standart 3 dots, but with the specified face.
  278. The change affects only Org-mode (which will then use its own display table).
  279. Changing this requires executing `M-x org-mode' in a buffer to become
  280. effective."
  281. :group 'org-startup
  282. :type '(choice (const :tag "Default" nil)
  283. (face :tag "Face" :value org-warning)
  284. (string :tag "String" :value "...#")))
  285. (defvar org-display-table nil
  286. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  287. (defgroup org-keywords nil
  288. "Keywords in Org-mode."
  289. :tag "Org Keywords"
  290. :group 'org)
  291. (defcustom org-deadline-string "DEADLINE:"
  292. "String to mark deadline entries.
  293. A deadline is this string, followed by a time stamp. Should be a word,
  294. terminated by a colon. You can insert a schedule keyword and
  295. a timestamp with \\[org-deadline].
  296. Changes become only effective after restarting Emacs."
  297. :group 'org-keywords
  298. :type 'string)
  299. (defcustom org-scheduled-string "SCHEDULED:"
  300. "String to mark scheduled TODO entries.
  301. A schedule is this string, followed by a time stamp. Should be a word,
  302. terminated by a colon. You can insert a schedule keyword and
  303. a timestamp with \\[org-schedule].
  304. Changes become only effective after restarting Emacs."
  305. :group 'org-keywords
  306. :type 'string)
  307. (defcustom org-closed-string "CLOSED:"
  308. "String used as the prefix for timestamps logging closing a TODO entry."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-clock-string "CLOCK:"
  312. "String used as prefix for timestamps clocking work hours on an item."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defcustom org-comment-string "COMMENT"
  316. "Entries starting with this keyword will never be exported.
  317. An entry can be toggled between COMMENT and normal with
  318. \\[org-toggle-comment].
  319. Changes become only effective after restarting Emacs."
  320. :group 'org-keywords
  321. :type 'string)
  322. (defcustom org-quote-string "QUOTE"
  323. "Entries starting with this keyword will be exported in fixed-width font.
  324. Quoting applies only to the text in the entry following the headline, and does
  325. not extend beyond the next headline, even if that is lower level.
  326. An entry can be toggled between QUOTE and normal with
  327. \\[org-toggle-fixed-width-section]."
  328. :group 'org-keywords
  329. :type 'string)
  330. (defconst org-repeat-re
  331. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  332. "Regular expression for specifying repeated events.
  333. After a match, group 1 contains the repeat expression.")
  334. (defgroup org-structure nil
  335. "Options concerning the general structure of Org-mode files."
  336. :tag "Org Structure"
  337. :group 'org)
  338. (defgroup org-reveal-location nil
  339. "Options about how to make context of a location visible."
  340. :tag "Org Reveal Location"
  341. :group 'org-structure)
  342. (defconst org-context-choice
  343. '(choice
  344. (const :tag "Always" t)
  345. (const :tag "Never" nil)
  346. (repeat :greedy t :tag "Individual contexts"
  347. (cons
  348. (choice :tag "Context"
  349. (const agenda)
  350. (const org-goto)
  351. (const occur-tree)
  352. (const tags-tree)
  353. (const link-search)
  354. (const mark-goto)
  355. (const bookmark-jump)
  356. (const isearch)
  357. (const default))
  358. (boolean))))
  359. "Contexts for the reveal options.")
  360. (defcustom org-show-hierarchy-above '((default . t))
  361. "Non-nil means, show full hierarchy when revealing a location.
  362. Org-mode often shows locations in an org-mode file which might have
  363. been invisible before. When this is set, the hierarchy of headings
  364. above the exposed location is shown.
  365. Turning this off for example for sparse trees makes them very compact.
  366. Instead of t, this can also be an alist specifying this option for different
  367. contexts. Valid contexts are
  368. agenda when exposing an entry from the agenda
  369. org-goto when using the command `org-goto' on key C-c C-j
  370. occur-tree when using the command `org-occur' on key C-c /
  371. tags-tree when constructing a sparse tree based on tags matches
  372. link-search when exposing search matches associated with a link
  373. mark-goto when exposing the jump goal of a mark
  374. bookmark-jump when exposing a bookmark location
  375. isearch when exiting from an incremental search
  376. default default for all contexts not set explicitly"
  377. :group 'org-reveal-location
  378. :type org-context-choice)
  379. (defcustom org-show-following-heading '((default . nil))
  380. "Non-nil means, show following heading when revealing a location.
  381. Org-mode often shows locations in an org-mode file which might have
  382. been invisible before. When this is set, the heading following the
  383. match is shown.
  384. Turning this off for example for sparse trees makes them very compact,
  385. but makes it harder to edit the location of the match. In such a case,
  386. use the command \\[org-reveal] to show more context.
  387. Instead of t, this can also be an alist specifying this option for different
  388. contexts. See `org-show-hierarchy-above' for valid contexts."
  389. :group 'org-reveal-location
  390. :type org-context-choice)
  391. (defcustom org-show-siblings '((default . nil) (isearch t))
  392. "Non-nil means, show all sibling heading when revealing a location.
  393. Org-mode often shows locations in an org-mode file which might have
  394. been invisible before. When this is set, the sibling of the current entry
  395. heading are all made visible. If `org-show-hierarchy-above' is t,
  396. the same happens on each level of the hierarchy above the current entry.
  397. By default this is on for the isearch context, off for all other contexts.
  398. Turning this off for example for sparse trees makes them very compact,
  399. but makes it harder to edit the location of the match. In such a case,
  400. use the command \\[org-reveal] to show more context.
  401. Instead of t, this can also be an alist specifying this option for different
  402. contexts. See `org-show-hierarchy-above' for valid contexts."
  403. :group 'org-reveal-location
  404. :type org-context-choice)
  405. (defcustom org-show-entry-below '((default . nil))
  406. "Non-nil means, show the entry below a headline when revealing a location.
  407. Org-mode often shows locations in an org-mode file which might have
  408. been invisible before. When this is set, the text below the headline that is
  409. exposed is also shown.
  410. By default this is off for all contexts.
  411. Instead of t, this can also be an alist specifying this option for different
  412. contexts. See `org-show-hierarchy-above' for valid contexts."
  413. :group 'org-reveal-location
  414. :type org-context-choice)
  415. (defcustom org-indirect-buffer-display 'other-window
  416. "How should indirect tree buffers be displayed?
  417. This applies to indirect buffers created with the commands
  418. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  419. Valid values are:
  420. current-window Display in the current window
  421. other-window Just display in another window.
  422. dedicated-frame Create one new frame, and re-use it each time.
  423. new-frame Make a new frame each time. Note that in this case
  424. previously-made indirect buffers are kept, and you need to
  425. kill these buffers yourself."
  426. :group 'org-structure
  427. :group 'org-agenda-windows
  428. :type '(choice
  429. (const :tag "In current window" current-window)
  430. (const :tag "In current frame, other window" other-window)
  431. (const :tag "Each time a new frame" new-frame)
  432. (const :tag "One dedicated frame" dedicated-frame)))
  433. (defgroup org-cycle nil
  434. "Options concerning visibility cycling in Org-mode."
  435. :tag "Org Cycle"
  436. :group 'org-structure)
  437. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  438. "Names of drawers. Drawers are not opened by cycling on the headline above.
  439. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  440. this:
  441. :DRAWERNAME:
  442. .....
  443. :END:
  444. The drawer \"PROPERTIES\" is special for capturing properties through
  445. the property API.
  446. Drawers can be defined on the per-file basis with a line like:
  447. #+DRAWERS: HIDDEN STATE PROPERTIES"
  448. :group 'org-structure
  449. :type '(repeat (string :tag "Drawer Name")))
  450. (defcustom org-cycle-global-at-bob nil
  451. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  452. This makes it possible to do global cycling without having to use S-TAB or
  453. C-u TAB. For this special case to work, the first line of the buffer
  454. must not be a headline - it may be empty ot some other text. When used in
  455. this way, `org-cycle-hook' is disables temporarily, to make sure the
  456. cursor stays at the beginning of the buffer.
  457. When this option is nil, don't do anything special at the beginning
  458. of the buffer."
  459. :group 'org-cycle
  460. :type 'boolean)
  461. (defcustom org-cycle-emulate-tab t
  462. "Where should `org-cycle' emulate TAB.
  463. nil Never
  464. white Only in completely white lines
  465. whitestart Only at the beginning of lines, before the first non-white char
  466. t Everywhere except in headlines
  467. exc-hl-bol Everywhere except at the start of a headline
  468. If TAB is used in a place where it does not emulate TAB, the current subtree
  469. visibility is cycled."
  470. :group 'org-cycle
  471. :type '(choice (const :tag "Never" nil)
  472. (const :tag "Only in completely white lines" white)
  473. (const :tag "Before first char in a line" whitestart)
  474. (const :tag "Everywhere except in headlines" t)
  475. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  476. ))
  477. (defcustom org-cycle-separator-lines 2
  478. "Number of empty lines needed to keep an empty line between collapsed trees.
  479. If you leave an empty line between the end of a subtree and the following
  480. headline, this empty line is hidden when the subtree is folded.
  481. Org-mode will leave (exactly) one empty line visible if the number of
  482. empty lines is equal or larger to the number given in this variable.
  483. So the default 2 means, at least 2 empty lines after the end of a subtree
  484. are needed to produce free space between a collapsed subtree and the
  485. following headline.
  486. Special case: when 0, never leave empty lines in collapsed view."
  487. :group 'org-cycle
  488. :type 'integer)
  489. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  490. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  491. org-cycle-hide-drawers
  492. org-cycle-show-empty-lines
  493. org-optimize-window-after-visibility-change)
  494. "Hook that is run after `org-cycle' has changed the buffer visibility.
  495. The function(s) in this hook must accept a single argument which indicates
  496. the new state that was set by the most recent `org-cycle' command. The
  497. argument is a symbol. After a global state change, it can have the values
  498. `overview', `content', or `all'. After a local state change, it can have
  499. the values `folded', `children', or `subtree'."
  500. :group 'org-cycle
  501. :type 'hook)
  502. (defgroup org-edit-structure nil
  503. "Options concerning structure editing in Org-mode."
  504. :tag "Org Edit Structure"
  505. :group 'org-structure)
  506. (defcustom org-odd-levels-only nil
  507. "Non-nil means, skip even levels and only use odd levels for the outline.
  508. This has the effect that two stars are being added/taken away in
  509. promotion/demotion commands. It also influences how levels are
  510. handled by the exporters.
  511. Changing it requires restart of `font-lock-mode' to become effective
  512. for fontification also in regions already fontified.
  513. You may also set this on a per-file basis by adding one of the following
  514. lines to the buffer:
  515. #+STARTUP: odd
  516. #+STARTUP: oddeven"
  517. :group 'org-edit-structure
  518. :group 'org-font-lock
  519. :type 'boolean)
  520. (defcustom org-adapt-indentation t
  521. "Non-nil means, adapt indentation when promoting and demoting.
  522. When this is set and the *entire* text in an entry is indented, the
  523. indentation is increased by one space in a demotion command, and
  524. decreased by one in a promotion command. If any line in the entry
  525. body starts at column 0, indentation is not changed at all."
  526. :group 'org-edit-structure
  527. :type 'boolean)
  528. (defcustom org-special-ctrl-a/e nil
  529. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  530. When t, `C-a' will bring back the cursor to the beginning of the
  531. headline text, i.e. after the stars and after a possible TODO keyword.
  532. In an item, this will be the position after the bullet.
  533. When the cursor is already at that position, another `C-a' will bring
  534. it to the beginning of the line.
  535. `C-e' will jump to the end of the headline, ignoring the presence of tags
  536. in the headline. A second `C-e' will then jump to the true end of the
  537. line, after any tags.
  538. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  539. and only a directly following, identical keypress will bring the cursor
  540. to the special positions."
  541. :group 'org-edit-structure
  542. :type '(choice
  543. (const :tag "off" nil)
  544. (const :tag "after bullet first" t)
  545. (const :tag "border first" reversed)))
  546. (if (fboundp 'defvaralias)
  547. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  548. (defcustom org-special-ctrl-k nil
  549. "Non-nil means `C-k' will behave specially in headlines.
  550. When nil, `C-k' will call the default `kill-line' command.
  551. When t, the following will happen while the cursor is in the headline:
  552. - When the cursor is at the beginning of a headline, kill the entire
  553. line and possible the folded subtree below the line.
  554. - When in the middle of the headline text, kill the headline up to the tags.
  555. - When after the headline text, kill the tags."
  556. :group 'org-edit-structure
  557. :type 'boolean)
  558. (defcustom org-M-RET-may-split-line '((default . t))
  559. "Non-nil means, M-RET will split the line at the cursor position.
  560. When nil, it will go to the end of the line before making a
  561. new line.
  562. You may also set this option in a different way for different
  563. contexts. Valid contexts are:
  564. headline when creating a new headline
  565. item when creating a new item
  566. table in a table field
  567. default the value to be used for all contexts not explicitly
  568. customized"
  569. :group 'org-structure
  570. :group 'org-table
  571. :type '(choice
  572. (const :tag "Always" t)
  573. (const :tag "Never" nil)
  574. (repeat :greedy t :tag "Individual contexts"
  575. (cons
  576. (choice :tag "Context"
  577. (const headline)
  578. (const item)
  579. (const table)
  580. (const default))
  581. (boolean)))))
  582. (defcustom org-blank-before-new-entry '((heading . nil)
  583. (plain-list-item . nil))
  584. "Should `org-insert-heading' leave a blank line before new heading/item?
  585. The value is an alist, with `heading' and `plain-list-item' as car,
  586. and a boolean flag as cdr."
  587. :group 'org-edit-structure
  588. :type '(list
  589. (cons (const heading) (boolean))
  590. (cons (const plain-list-item) (boolean))))
  591. (defcustom org-insert-heading-hook nil
  592. "Hook being run after inserting a new heading."
  593. :group 'org-edit-structure
  594. :type 'hook)
  595. (defcustom org-enable-fixed-width-editor t
  596. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  597. This currently only means, they are never auto-wrapped.
  598. When nil, such lines will be treated like ordinary lines.
  599. See also the QUOTE keyword."
  600. :group 'org-edit-structure
  601. :type 'boolean)
  602. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  603. "The mode that should be used to edit fixed-width regions.
  604. These are the regions where each line starts with a colon."
  605. :group 'org-edit-structure
  606. :type '(choice
  607. (const artist-mode)
  608. (const picture-mode)
  609. (const fundamental-mode)
  610. (function :tag "Other (specify)")))
  611. (defcustom org-goto-auto-isearch t
  612. "Non-nil means, typing characters in org-goto starts incremental search."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defgroup org-sparse-trees nil
  616. "Options concerning sparse trees in Org-mode."
  617. :tag "Org Sparse Trees"
  618. :group 'org-structure)
  619. (defcustom org-highlight-sparse-tree-matches t
  620. "Non-nil means, highlight all matches that define a sparse tree.
  621. The highlights will automatically disappear the next time the buffer is
  622. changed by an edit command."
  623. :group 'org-sparse-trees
  624. :type 'boolean)
  625. (defcustom org-remove-highlights-with-change t
  626. "Non-nil means, any change to the buffer will remove temporary highlights.
  627. Such highlights are created by `org-occur' and `org-clock-display'.
  628. When nil, `C-c C-c needs to be used to get rid of the highlights.
  629. The highlights created by `org-preview-latex-fragment' always need
  630. `C-c C-c' to be removed."
  631. :group 'org-sparse-trees
  632. :group 'org-time
  633. :type 'boolean)
  634. (defcustom org-occur-hook '(org-first-headline-recenter)
  635. "Hook that is run after `org-occur' has constructed a sparse tree.
  636. This can be used to recenter the window to show as much of the structure
  637. as possible."
  638. :group 'org-sparse-trees
  639. :type 'hook)
  640. (defgroup org-plain-lists nil
  641. "Options concerning plain lists in Org-mode."
  642. :tag "Org Plain lists"
  643. :group 'org-structure)
  644. (defcustom org-cycle-include-plain-lists nil
  645. "Non-nil means, include plain lists into visibility cycling.
  646. This means that during cycling, plain list items will *temporarily* be
  647. interpreted as outline headlines with a level given by 1000+i where i is the
  648. indentation of the bullet. In all other operations, plain list items are
  649. not seen as headlines. For example, you cannot assign a TODO keyword to
  650. such an item."
  651. :group 'org-plain-lists
  652. :type 'boolean)
  653. (defcustom org-plain-list-ordered-item-terminator t
  654. "The character that makes a line with leading number an ordered list item.
  655. Valid values are ?. and ?\). To get both terminators, use t. While
  656. ?. may look nicer, it creates the danger that a line with leading
  657. number may be incorrectly interpreted as an item. ?\) therefore is
  658. the safe choice."
  659. :group 'org-plain-lists
  660. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  661. (const :tag "paren like in \"2)\"" ?\))
  662. (const :tab "both" t)))
  663. (defcustom org-empty-line-terminates-plain-lists nil
  664. "Non-nil means, an empty line ends all plain list levels.
  665. When nil, empty lines are part of the preceeding item."
  666. :group 'org-plain-lists
  667. :type 'boolean)
  668. (defcustom org-auto-renumber-ordered-lists t
  669. "Non-nil means, automatically renumber ordered plain lists.
  670. Renumbering happens when the sequence have been changed with
  671. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  672. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  673. :group 'org-plain-lists
  674. :type 'boolean)
  675. (defcustom org-provide-checkbox-statistics t
  676. "Non-nil means, update checkbox statistics after insert and toggle.
  677. When this is set, checkbox statistics is updated each time you either insert
  678. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  679. with \\[org-ctrl-c-ctrl-c\\]."
  680. :group 'org-plain-lists
  681. :type 'boolean)
  682. (defcustom org-description-max-indent 20
  683. "Maximum indentation for the second line of a description list.
  684. When the indentation would be larger than this, it will become
  685. 5 characters instead."
  686. :group 'org-plain-lists
  687. :type 'integer)
  688. (defgroup org-imenu-and-speedbar nil
  689. "Options concerning imenu and speedbar in Org-mode."
  690. :tag "Org Imenu and Speedbar"
  691. :group 'org-structure)
  692. (defcustom org-imenu-depth 2
  693. "The maximum level for Imenu access to Org-mode headlines.
  694. This also applied for speedbar access."
  695. :group 'org-imenu-and-speedbar
  696. :type 'number)
  697. (defgroup org-table nil
  698. "Options concerning tables in Org-mode."
  699. :tag "Org Table"
  700. :group 'org)
  701. (defcustom org-enable-table-editor 'optimized
  702. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  703. When nil, such lines will be treated like ordinary lines.
  704. When equal to the symbol `optimized', the table editor will be optimized to
  705. do the following:
  706. - Automatic overwrite mode in front of whitespace in table fields.
  707. This makes the structure of the table stay in tact as long as the edited
  708. field does not exceed the column width.
  709. - Minimize the number of realigns. Normally, the table is aligned each time
  710. TAB or RET are pressed to move to another field. With optimization this
  711. happens only if changes to a field might have changed the column width.
  712. Optimization requires replacing the functions `self-insert-command',
  713. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  714. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  715. very good at guessing when a re-align will be necessary, but you can always
  716. force one with \\[org-ctrl-c-ctrl-c].
  717. If you would like to use the optimized version in Org-mode, but the
  718. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  719. This variable can be used to turn on and off the table editor during a session,
  720. but in order to toggle optimization, a restart is required.
  721. See also the variable `org-table-auto-blank-field'."
  722. :group 'org-table
  723. :type '(choice
  724. (const :tag "off" nil)
  725. (const :tag "on" t)
  726. (const :tag "on, optimized" optimized)))
  727. (defcustom org-table-tab-recognizes-table.el t
  728. "Non-nil means, TAB will automatically notice a table.el table.
  729. When it sees such a table, it moves point into it and - if necessary -
  730. calls `table-recognize-table'."
  731. :group 'org-table-editing
  732. :type 'boolean)
  733. (defgroup org-link nil
  734. "Options concerning links in Org-mode."
  735. :tag "Org Link"
  736. :group 'org)
  737. (defvar org-link-abbrev-alist-local nil
  738. "Buffer-local version of `org-link-abbrev-alist', which see.
  739. The value of this is taken from the #+LINK lines.")
  740. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  741. (defcustom org-link-abbrev-alist nil
  742. "Alist of link abbreviations.
  743. The car of each element is a string, to be replaced at the start of a link.
  744. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  745. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  746. [[linkkey:tag][description]]
  747. If REPLACE is a string, the tag will simply be appended to create the link.
  748. If the string contains \"%s\", the tag will be inserted there.
  749. REPLACE may also be a function that will be called with the tag as the
  750. only argument to create the link, which should be returned as a string.
  751. See the manual for examples."
  752. :group 'org-link
  753. :type 'alist)
  754. (defcustom org-descriptive-links t
  755. "Non-nil means, hide link part and only show description of bracket links.
  756. Bracket links are like [[link][descritpion]]. This variable sets the initial
  757. state in new org-mode buffers. The setting can then be toggled on a
  758. per-buffer basis from the Org->Hyperlinks menu."
  759. :group 'org-link
  760. :type 'boolean)
  761. (defcustom org-link-file-path-type 'adaptive
  762. "How the path name in file links should be stored.
  763. Valid values are:
  764. relative Relative to the current directory, i.e. the directory of the file
  765. into which the link is being inserted.
  766. absolute Absolute path, if possible with ~ for home directory.
  767. noabbrev Absolute path, no abbreviation of home directory.
  768. adaptive Use relative path for files in the current directory and sub-
  769. directories of it. For other files, use an absolute path."
  770. :group 'org-link
  771. :type '(choice
  772. (const relative)
  773. (const absolute)
  774. (const noabbrev)
  775. (const adaptive)))
  776. (defcustom org-activate-links '(bracket angle plain radio tag date)
  777. "Types of links that should be activated in Org-mode files.
  778. This is a list of symbols, each leading to the activation of a certain link
  779. type. In principle, it does not hurt to turn on most link types - there may
  780. be a small gain when turning off unused link types. The types are:
  781. bracket The recommended [[link][description]] or [[link]] links with hiding.
  782. angular Links in angular brackes that may contain whitespace like
  783. <bbdb:Carsten Dominik>.
  784. plain Plain links in normal text, no whitespace, like http://google.com.
  785. radio Text that is matched by a radio target, see manual for details.
  786. tag Tag settings in a headline (link to tag search).
  787. date Time stamps (link to calendar).
  788. Changing this variable requires a restart of Emacs to become effective."
  789. :group 'org-link
  790. :type '(set (const :tag "Double bracket links (new style)" bracket)
  791. (const :tag "Angular bracket links (old style)" angular)
  792. (const :tag "Plain text links" plain)
  793. (const :tag "Radio target matches" radio)
  794. (const :tag "Tags" tag)
  795. (const :tag "Timestamps" date)))
  796. (defcustom org-make-link-description-function nil
  797. "Function to use to generate link descriptions from links. If
  798. nil the link location will be used. This function must take two
  799. parameters; the first is the link and the second the description
  800. org-insert-link has generated, and should return the description
  801. to use."
  802. :group 'org-link
  803. :type 'function)
  804. (defgroup org-link-store nil
  805. "Options concerning storing links in Org-mode."
  806. :tag "Org Store Link"
  807. :group 'org-link)
  808. (defcustom org-email-link-description-format "Email %c: %.30s"
  809. "Format of the description part of a link to an email or usenet message.
  810. The following %-excapes will be replaced by corresponding information:
  811. %F full \"From\" field
  812. %f name, taken from \"From\" field, address if no name
  813. %T full \"To\" field
  814. %t first name in \"To\" field, address if no name
  815. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  816. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  817. %s subject
  818. %m message-id.
  819. You may use normal field width specification between the % and the letter.
  820. This is for example useful to limit the length of the subject.
  821. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  822. :group 'org-link-store
  823. :type 'string)
  824. (defcustom org-from-is-user-regexp
  825. (let (r1 r2)
  826. (when (and user-mail-address (not (string= user-mail-address "")))
  827. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  828. (when (and user-full-name (not (string= user-full-name "")))
  829. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  830. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  831. "Regexp mached against the \"From:\" header of an email or usenet message.
  832. It should match if the message is from the user him/herself."
  833. :group 'org-link-store
  834. :type 'regexp)
  835. (defcustom org-context-in-file-links t
  836. "Non-nil means, file links from `org-store-link' contain context.
  837. A search string will be added to the file name with :: as separator and
  838. used to find the context when the link is activated by the command
  839. `org-open-at-point'.
  840. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  841. negates this setting for the duration of the command."
  842. :group 'org-link-store
  843. :type 'boolean)
  844. (defcustom org-keep-stored-link-after-insertion nil
  845. "Non-nil means, keep link in list for entire session.
  846. The command `org-store-link' adds a link pointing to the current
  847. location to an internal list. These links accumulate during a session.
  848. The command `org-insert-link' can be used to insert links into any
  849. Org-mode file (offering completion for all stored links). When this
  850. option is nil, every link which has been inserted once using \\[org-insert-link]
  851. will be removed from the list, to make completing the unused links
  852. more efficient."
  853. :group 'org-link-store
  854. :type 'boolean)
  855. (defgroup org-link-follow nil
  856. "Options concerning following links in Org-mode."
  857. :tag "Org Follow Link"
  858. :group 'org-link)
  859. (defcustom org-follow-link-hook nil
  860. "Hook that is run after a link has been followed."
  861. :group 'org-link-follow
  862. :type 'hook)
  863. (defcustom org-tab-follows-link nil
  864. "Non-nil means, on links TAB will follow the link.
  865. Needs to be set before org.el is loaded."
  866. :group 'org-link-follow
  867. :type 'boolean)
  868. (defcustom org-return-follows-link nil
  869. "Non-nil means, on links RET will follow the link.
  870. Needs to be set before org.el is loaded."
  871. :group 'org-link-follow
  872. :type 'boolean)
  873. (defcustom org-mouse-1-follows-link
  874. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  875. "Non-nil means, mouse-1 on a link will follow the link.
  876. A longer mouse click will still set point. Does not work on XEmacs.
  877. Needs to be set before org.el is loaded."
  878. :group 'org-link-follow
  879. :type 'boolean)
  880. (defcustom org-mark-ring-length 4
  881. "Number of different positions to be recorded in the ring
  882. Changing this requires a restart of Emacs to work correctly."
  883. :group 'org-link-follow
  884. :type 'interger)
  885. (defcustom org-link-frame-setup
  886. '((vm . vm-visit-folder-other-frame)
  887. (gnus . gnus-other-frame)
  888. (file . find-file-other-window))
  889. "Setup the frame configuration for following links.
  890. When following a link with Emacs, it may often be useful to display
  891. this link in another window or frame. This variable can be used to
  892. set this up for the different types of links.
  893. For VM, use any of
  894. `vm-visit-folder'
  895. `vm-visit-folder-other-frame'
  896. For Gnus, use any of
  897. `gnus'
  898. `gnus-other-frame'
  899. For FILE, use any of
  900. `find-file'
  901. `find-file-other-window'
  902. `find-file-other-frame'
  903. For the calendar, use the variable `calendar-setup'.
  904. For BBDB, it is currently only possible to display the matches in
  905. another window."
  906. :group 'org-link-follow
  907. :type '(list
  908. (cons (const vm)
  909. (choice
  910. (const vm-visit-folder)
  911. (const vm-visit-folder-other-window)
  912. (const vm-visit-folder-other-frame)))
  913. (cons (const gnus)
  914. (choice
  915. (const gnus)
  916. (const gnus-other-frame)))
  917. (cons (const file)
  918. (choice
  919. (const find-file)
  920. (const find-file-other-window)
  921. (const find-file-other-frame)))))
  922. (defcustom org-display-internal-link-with-indirect-buffer nil
  923. "Non-nil means, use indirect buffer to display infile links.
  924. Activating internal links (from one location in a file to another location
  925. in the same file) normally just jumps to the location. When the link is
  926. activated with a C-u prefix (or with mouse-3), the link is displayed in
  927. another window. When this option is set, the other window actually displays
  928. an indirect buffer clone of the current buffer, to avoid any visibility
  929. changes to the current buffer."
  930. :group 'org-link-follow
  931. :type 'boolean)
  932. (defcustom org-open-non-existing-files nil
  933. "Non-nil means, `org-open-file' will open non-existing files.
  934. When nil, an error will be generated."
  935. :group 'org-link-follow
  936. :type 'boolean)
  937. (defcustom org-open-directory-means-index-dot-org nil
  938. "Non-nil means, a link to a directory really means to index.org.
  939. When nil, following a directory link will run dired or open a finder/explorer
  940. window on that directory."
  941. :group 'org-link-follow
  942. :type 'boolean)
  943. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  944. "Function and arguments to call for following mailto links.
  945. This is a list with the first element being a lisp function, and the
  946. remaining elements being arguments to the function. In string arguments,
  947. %a will be replaced by the address, and %s will be replaced by the subject
  948. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  949. :group 'org-link-follow
  950. :type '(choice
  951. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  952. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  953. (const :tag "message-mail" (message-mail "%a" "%s"))
  954. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  955. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  956. "Non-nil means, ask for confirmation before executing shell links.
  957. Shell links can be dangerous: just think about a link
  958. [[shell:rm -rf ~/*][Google Search]]
  959. This link would show up in your Org-mode document as \"Google Search\",
  960. but really it would remove your entire home directory.
  961. Therefore we advise against setting this variable to nil.
  962. Just change it to `y-or-n-p' of you want to confirm with a
  963. single keystroke rather than having to type \"yes\"."
  964. :group 'org-link-follow
  965. :type '(choice
  966. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  967. (const :tag "with y-or-n (faster)" y-or-n-p)
  968. (const :tag "no confirmation (dangerous)" nil)))
  969. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  970. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  971. Elisp links can be dangerous: just think about a link
  972. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  973. This link would show up in your Org-mode document as \"Google Search\",
  974. but really it would remove your entire home directory.
  975. Therefore we advise against setting this variable to nil.
  976. Just change it to `y-or-n-p' of you want to confirm with a
  977. single keystroke rather than having to type \"yes\"."
  978. :group 'org-link-follow
  979. :type '(choice
  980. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  981. (const :tag "with y-or-n (faster)" y-or-n-p)
  982. (const :tag "no confirmation (dangerous)" nil)))
  983. (defconst org-file-apps-defaults-gnu
  984. '((remote . emacs)
  985. (t . mailcap))
  986. "Default file applications on a UNIX or GNU/Linux system.
  987. See `org-file-apps'.")
  988. (defconst org-file-apps-defaults-macosx
  989. '((remote . emacs)
  990. (t . "open %s")
  991. ("ps" . "gv %s")
  992. ("ps.gz" . "gv %s")
  993. ("eps" . "gv %s")
  994. ("eps.gz" . "gv %s")
  995. ("dvi" . "xdvi %s")
  996. ("fig" . "xfig %s"))
  997. "Default file applications on a MacOS X system.
  998. The system \"open\" is known as a default, but we use X11 applications
  999. for some files for which the OS does not have a good default.
  1000. See `org-file-apps'.")
  1001. (defconst org-file-apps-defaults-windowsnt
  1002. (list
  1003. '(remote . emacs)
  1004. (cons t
  1005. (list (if (featurep 'xemacs)
  1006. 'mswindows-shell-execute
  1007. 'w32-shell-execute)
  1008. "open" 'file)))
  1009. "Default file applications on a Windows NT system.
  1010. The system \"open\" is used for most files.
  1011. See `org-file-apps'.")
  1012. (defcustom org-file-apps
  1013. '(
  1014. ("txt" . emacs)
  1015. ("tex" . emacs)
  1016. ("ltx" . emacs)
  1017. ("org" . emacs)
  1018. ("el" . emacs)
  1019. ("bib" . emacs)
  1020. )
  1021. "External applications for opening `file:path' items in a document.
  1022. Org-mode uses system defaults for different file types, but
  1023. you can use this variable to set the application for a given file
  1024. extension. The entries in this list are cons cells where the car identifies
  1025. files and the cdr the corresponding command. Possible values for the
  1026. file identifier are
  1027. \"ext\" A string identifying an extension
  1028. `directory' Matches a directory
  1029. `remote' Matches a remote file, accessible through tramp or efs.
  1030. Remote files most likely should be visited through Emacs
  1031. because external applications cannot handle such paths.
  1032. t Default for all remaining files
  1033. Possible values for the command are:
  1034. `emacs' The file will be visited by the current Emacs process.
  1035. `default' Use the default application for this file type.
  1036. string A command to be executed by a shell; %s will be replaced
  1037. by the path to the file.
  1038. sexp A Lisp form which will be evaluated. The file path will
  1039. be available in the Lisp variable `file'.
  1040. For more examples, see the system specific constants
  1041. `org-file-apps-defaults-macosx'
  1042. `org-file-apps-defaults-windowsnt'
  1043. `org-file-apps-defaults-gnu'."
  1044. :group 'org-link-follow
  1045. :type '(repeat
  1046. (cons (choice :value ""
  1047. (string :tag "Extension")
  1048. (const :tag "Default for unrecognized files" t)
  1049. (const :tag "Remote file" remote)
  1050. (const :tag "Links to a directory" directory))
  1051. (choice :value ""
  1052. (const :tag "Visit with Emacs" emacs)
  1053. (const :tag "Use system default" default)
  1054. (string :tag "Command")
  1055. (sexp :tag "Lisp form")))))
  1056. (defgroup org-refile nil
  1057. "Options concerning refiling entries in Org-mode."
  1058. :tag "Org Remember"
  1059. :group 'org)
  1060. (defcustom org-directory "~/org"
  1061. "Directory with org files.
  1062. This directory will be used as default to prompt for org files.
  1063. Used by the hooks for remember.el."
  1064. :group 'org-refile
  1065. :group 'org-remember
  1066. :type 'directory)
  1067. (defcustom org-default-notes-file "~/.notes"
  1068. "Default target for storing notes.
  1069. Used by the hooks for remember.el. This can be a string, or nil to mean
  1070. the value of `remember-data-file'.
  1071. You can set this on a per-template basis with the variable
  1072. `org-remember-templates'."
  1073. :group 'org-refile
  1074. :group 'org-remember
  1075. :type '(choice
  1076. (const :tag "Default from remember-data-file" nil)
  1077. file))
  1078. (defcustom org-goto-interface 'outline
  1079. "The default interface to be used for `org-goto'.
  1080. Allowed vaues are:
  1081. outline The interface shows an outline of the relevant file
  1082. and the correct heading is found by moving through
  1083. the outline or by searching with incremental search.
  1084. outline-path-completion Headlines in the current buffer are offered via
  1085. completion."
  1086. :group 'org-refile
  1087. :type '(choice
  1088. (const :tag "Outline" outline)
  1089. (const :tag "Outline-path-completion" outline-path-completion)))
  1090. (defcustom org-reverse-note-order nil
  1091. "Non-nil means, store new notes at the beginning of a file or entry.
  1092. When nil, new notes will be filed to the end of a file or entry.
  1093. This can also be a list with cons cells of regular expressions that
  1094. are matched against file names, and values."
  1095. :group 'org-remember
  1096. :type '(choice
  1097. (const :tag "Reverse always" t)
  1098. (const :tag "Reverse never" nil)
  1099. (repeat :tag "By file name regexp"
  1100. (cons regexp boolean))))
  1101. (defcustom org-refile-targets nil
  1102. "Targets for refiling entries with \\[org-refile].
  1103. This is list of cons cells. Each cell contains:
  1104. - a specification of the files to be considered, either a list of files,
  1105. or a symbol whose function or variable value will be used to retrieve
  1106. a file name or a list of file names. Nil means, refile to a different
  1107. heading in the current buffer.
  1108. - A specification of how to find candidate refile targets. This may be
  1109. any of
  1110. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1111. This tag has to be present in all target headlines, inheritance will
  1112. not be considered.
  1113. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1114. todo keyword.
  1115. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1116. headlines that are refiling targets.
  1117. - a cons cell (:level . N). Any headline of level N is considered a target.
  1118. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1119. When this variable is nil, all top-level headlines in the current buffer
  1120. are used, equivalent to the vlaue `((nil . (:level . 1))'."
  1121. :group 'org-remember
  1122. :type '(repeat
  1123. (cons
  1124. (choice :value org-agenda-files
  1125. (const :tag "All agenda files" org-agenda-files)
  1126. (const :tag "Current buffer" nil)
  1127. (function) (variable) (file))
  1128. (choice :tag "Identify target headline by"
  1129. (cons :tag "Specific tag" (const :tag) (string))
  1130. (cons :tag "TODO keyword" (const :todo) (string))
  1131. (cons :tag "Regular expression" (const :regexp) (regexp))
  1132. (cons :tag "Level number" (const :level) (integer))
  1133. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1134. (defcustom org-refile-use-outline-path nil
  1135. "Non-nil means, provide refile targets as paths.
  1136. So a level 3 headline will be available as level1/level2/level3.
  1137. When the value is `file', also include the file name (without directory)
  1138. into the path. When `full-file-path', include the full file path."
  1139. :group 'org-remember
  1140. :type '(choice
  1141. (const :tag "Not" nil)
  1142. (const :tag "Yes" t)
  1143. (const :tag "Start with file name" file)
  1144. (const :tag "Start with full file path" full-file-path)))
  1145. (defgroup org-todo nil
  1146. "Options concerning TODO items in Org-mode."
  1147. :tag "Org TODO"
  1148. :group 'org)
  1149. (defgroup org-progress nil
  1150. "Options concerning Progress logging in Org-mode."
  1151. :tag "Org Progress"
  1152. :group 'org-time)
  1153. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1154. "List of TODO entry keyword sequences and their interpretation.
  1155. \\<org-mode-map>This is a list of sequences.
  1156. Each sequence starts with a symbol, either `sequence' or `type',
  1157. indicating if the keywords should be interpreted as a sequence of
  1158. action steps, or as different types of TODO items. The first
  1159. keywords are states requiring action - these states will select a headline
  1160. for inclusion into the global TODO list Org-mode produces. If one of
  1161. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1162. signify that no further action is necessary. If \"|\" is not found,
  1163. the last keyword is treated as the only DONE state of the sequence.
  1164. The command \\[org-todo] cycles an entry through these states, and one
  1165. additional state where no keyword is present. For details about this
  1166. cycling, see the manual.
  1167. TODO keywords and interpretation can also be set on a per-file basis with
  1168. the special #+SEQ_TODO and #+TYP_TODO lines.
  1169. Each keyword can optionally specify a character for fast state selection
  1170. \(in combination with the variable `org-use-fast-todo-selection')
  1171. and specifiers for state change logging, using the same syntax
  1172. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1173. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1174. indicates to record a time stamp each time this state is selected.
  1175. Each keyword may also specify if a timestamp or a note should be
  1176. recorded when entering or leaving the state, by adding additional
  1177. characters in the parenthesis after the keyword. This looks like this:
  1178. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1179. record only the time of the state change. With X and Y being either
  1180. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1181. Y when leaving the state if and only if the *target* state does not
  1182. define X. You may omit any of the fast-selection key or X or /Y,
  1183. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1184. For backward compatibility, this variable may also be just a list
  1185. of keywords - in this case the interptetation (sequence or type) will be
  1186. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1187. :group 'org-todo
  1188. :group 'org-keywords
  1189. :type '(choice
  1190. (repeat :tag "Old syntax, just keywords"
  1191. (string :tag "Keyword"))
  1192. (repeat :tag "New syntax"
  1193. (cons
  1194. (choice
  1195. :tag "Interpretation"
  1196. (const :tag "Sequence (cycling hits every state)" sequence)
  1197. (const :tag "Type (cycling directly to DONE)" type))
  1198. (repeat
  1199. (string :tag "Keyword"))))))
  1200. (defvar org-todo-keywords-1 nil
  1201. "All TODO and DONE keywords active in a buffer.")
  1202. (make-variable-buffer-local 'org-todo-keywords-1)
  1203. (defvar org-todo-keywords-for-agenda nil)
  1204. (defvar org-done-keywords-for-agenda nil)
  1205. (defvar org-todo-keyword-alist-for-agenda nil)
  1206. (defvar org-tag-alist-for-agenda nil)
  1207. (defvar org-agenda-contributing-files nil)
  1208. (defvar org-not-done-keywords nil)
  1209. (make-variable-buffer-local 'org-not-done-keywords)
  1210. (defvar org-done-keywords nil)
  1211. (make-variable-buffer-local 'org-done-keywords)
  1212. (defvar org-todo-heads nil)
  1213. (make-variable-buffer-local 'org-todo-heads)
  1214. (defvar org-todo-sets nil)
  1215. (make-variable-buffer-local 'org-todo-sets)
  1216. (defvar org-todo-log-states nil)
  1217. (make-variable-buffer-local 'org-todo-log-states)
  1218. (defvar org-todo-kwd-alist nil)
  1219. (make-variable-buffer-local 'org-todo-kwd-alist)
  1220. (defvar org-todo-key-alist nil)
  1221. (make-variable-buffer-local 'org-todo-key-alist)
  1222. (defvar org-todo-key-trigger nil)
  1223. (make-variable-buffer-local 'org-todo-key-trigger)
  1224. (defcustom org-todo-interpretation 'sequence
  1225. "Controls how TODO keywords are interpreted.
  1226. This variable is in principle obsolete and is only used for
  1227. backward compatibility, if the interpretation of todo keywords is
  1228. not given already in `org-todo-keywords'. See that variable for
  1229. more information."
  1230. :group 'org-todo
  1231. :group 'org-keywords
  1232. :type '(choice (const sequence)
  1233. (const type)))
  1234. (defcustom org-use-fast-todo-selection 'prefix
  1235. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1236. This variable describes if and under what circumstances the cycling
  1237. mechanism for TODO keywords will be replaced by a single-key, direct
  1238. selection scheme.
  1239. When nil, fast selection is never used.
  1240. When the symbol `prefix', it will be used when `org-todo' is called with
  1241. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1242. in an agenda buffer.
  1243. When t, fast selection is used by default. In this case, the prefix
  1244. argument forces cycling instead.
  1245. In all cases, the special interface is only used if access keys have actually
  1246. been assigned by the user, i.e. if keywords in the configuration are followed
  1247. by a letter in parenthesis, like TODO(t)."
  1248. :group 'org-todo
  1249. :type '(choice
  1250. (const :tag "Never" nil)
  1251. (const :tag "By default" t)
  1252. (const :tag "Only with C-u C-c C-t" prefix)))
  1253. (defcustom org-provide-todo-statistics t
  1254. "Non-nil means, update todo statistics after insert and toggle.
  1255. When this is set, todo statistics is updated in the parent of the current
  1256. entry each time a todo state is changed."
  1257. :group 'org-todo
  1258. :type 'boolean)
  1259. (defcustom org-after-todo-state-change-hook nil
  1260. "Hook which is run after the state of a TODO item was changed.
  1261. The new state (a string with a TODO keyword, or nil) is available in the
  1262. Lisp variable `state'."
  1263. :group 'org-todo
  1264. :type 'hook)
  1265. (defcustom org-log-done nil
  1266. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1267. When equal to the list (done), also prompt for a closing note.
  1268. This can also be configured on a per-file basis by adding one of
  1269. the following lines anywhere in the buffer:
  1270. #+STARTUP: logdone
  1271. #+STARTUP: lognotedone
  1272. #+STARTUP: nologdone"
  1273. :group 'org-todo
  1274. :group 'org-progress
  1275. :type '(choice
  1276. (const :tag "No logging" nil)
  1277. (const :tag "Record CLOSED timestamp" time)
  1278. (const :tag "Record CLOSED timestamp with closing note." note)))
  1279. ;; Normalize old uses of org-log-done.
  1280. (cond
  1281. ((eq org-log-done t) (setq org-log-done 'time))
  1282. ((and (listp org-log-done) (memq 'done org-log-done))
  1283. (setq org-log-done 'note)))
  1284. (defcustom org-log-note-clock-out nil
  1285. "Non-nil means, record a note when clocking out of an item.
  1286. This can also be configured on a per-file basis by adding one of
  1287. the following lines anywhere in the buffer:
  1288. #+STARTUP: lognoteclock-out
  1289. #+STARTUP: nolognoteclock-out"
  1290. :group 'org-todo
  1291. :group 'org-progress
  1292. :type 'boolean)
  1293. (defcustom org-log-done-with-time t
  1294. "Non-nil means, the CLOSED time stamp will contain date and time.
  1295. When nil, only the date will be recorded."
  1296. :group 'org-progress
  1297. :type 'boolean)
  1298. (defcustom org-log-note-headings
  1299. '((done . "CLOSING NOTE %t")
  1300. (state . "State %-12s %t")
  1301. (note . "Note taken on %t")
  1302. (clock-out . ""))
  1303. "Headings for notes added to entries.
  1304. The value is an alist, with the car being a symbol indicating the note
  1305. context, and the cdr is the heading to be used. The heading may also be the
  1306. empty string.
  1307. %t in the heading will be replaced by a time stamp.
  1308. %s will be replaced by the new TODO state, in double quotes.
  1309. %u will be replaced by the user name.
  1310. %U will be replaced by the full user name."
  1311. :group 'org-todo
  1312. :group 'org-progress
  1313. :type '(list :greedy t
  1314. (cons (const :tag "Heading when closing an item" done) string)
  1315. (cons (const :tag
  1316. "Heading when changing todo state (todo sequence only)"
  1317. state) string)
  1318. (cons (const :tag "Heading when just taking a note" note) string)
  1319. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1320. (unless (assq 'note org-log-note-headings)
  1321. (push '(note . "%t") org-log-note-headings))
  1322. (defcustom org-log-states-order-reversed t
  1323. "Non-nil means, the latest state change note will be directly after heading.
  1324. When nil, the notes will be orderer according to time."
  1325. :group 'org-todo
  1326. :group 'org-progress
  1327. :type 'boolean)
  1328. (defcustom org-log-repeat 'time
  1329. "Non-nil means, record moving through the DONE state when triggering repeat.
  1330. An auto-repeating tasks is immediately switched back to TODO when marked
  1331. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1332. the TODO keyword definition, or recording a closing note by setting
  1333. `org-log-done', there will be no record of the task moving through DONE.
  1334. This variable forces taking a note anyway. Possible values are:
  1335. nil Don't force a record
  1336. time Record a time stamp
  1337. note Record a note
  1338. This option can also be set with on a per-file-basis with
  1339. #+STARTUP: logrepeat
  1340. #+STARTUP: lognoterepeat
  1341. #+STARTUP: nologrepeat
  1342. You can have local logging settings for a subtree by setting the LOGGING
  1343. property to one or more of these keywords."
  1344. :group 'org-todo
  1345. :group 'org-progress
  1346. :type '(choice
  1347. (const :tag "Don't force a record" nil)
  1348. (const :tag "Force recording the DONE state" time)
  1349. (const :tag "Force recording a note with the DONE state" note)))
  1350. (defgroup org-priorities nil
  1351. "Priorities in Org-mode."
  1352. :tag "Org Priorities"
  1353. :group 'org-todo)
  1354. (defcustom org-highest-priority ?A
  1355. "The highest priority of TODO items. A character like ?A, ?B etc.
  1356. Must have a smaller ASCII number than `org-lowest-priority'."
  1357. :group 'org-priorities
  1358. :type 'character)
  1359. (defcustom org-lowest-priority ?C
  1360. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1361. Must have a larger ASCII number than `org-highest-priority'."
  1362. :group 'org-priorities
  1363. :type 'character)
  1364. (defcustom org-default-priority ?B
  1365. "The default priority of TODO items.
  1366. This is the priority an item get if no explicit priority is given."
  1367. :group 'org-priorities
  1368. :type 'character)
  1369. (defcustom org-priority-start-cycle-with-default t
  1370. "Non-nil means, start with default priority when starting to cycle.
  1371. When this is nil, the first step in the cycle will be (depending on the
  1372. command used) one higher or lower that the default priority."
  1373. :group 'org-priorities
  1374. :type 'boolean)
  1375. (defgroup org-time nil
  1376. "Options concerning time stamps and deadlines in Org-mode."
  1377. :tag "Org Time"
  1378. :group 'org)
  1379. (defcustom org-insert-labeled-timestamps-at-point nil
  1380. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1381. When nil, these labeled time stamps are forces into the second line of an
  1382. entry, just after the headline. When scheduling from the global TODO list,
  1383. the time stamp will always be forced into the second line."
  1384. :group 'org-time
  1385. :type 'boolean)
  1386. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1387. "Formats for `format-time-string' which are used for time stamps.
  1388. It is not recommended to change this constant.")
  1389. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1390. "Number of minutes to round time stamps to.
  1391. These are two values, the first applies when first creating a time stamp.
  1392. The second applies when changing it with the commands `S-up' and `S-down'.
  1393. When changing the time stamp, this means that it will change in steps
  1394. of N minutes, as given by the second value.
  1395. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1396. numbers should be factors of 60, so for example 5, 10, 15.
  1397. When this is larger than 1, you can still force an exact time-stamp by using
  1398. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1399. and by using a prefix arg to `S-up/down' to specify the exact number
  1400. of minutes to shift."
  1401. :group 'org-time
  1402. :get '(lambda (var) ; Make sure all entries have 5 elements
  1403. (if (integerp (default-value var))
  1404. (list (default-value var) 5)
  1405. (default-value var)))
  1406. :type '(list
  1407. (integer :tag "when inserting times")
  1408. (integer :tag "when modifying times")))
  1409. ;; Normalize old customizations of this variable.
  1410. (when (integerp org-time-stamp-rounding-minutes)
  1411. (setq org-time-stamp-rounding-minutes
  1412. (list org-time-stamp-rounding-minutes
  1413. org-time-stamp-rounding-minutes)))
  1414. (defcustom org-display-custom-times nil
  1415. "Non-nil means, overlay custom formats over all time stamps.
  1416. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1417. To turn this on on a per-file basis, insert anywhere in the file:
  1418. #+STARTUP: customtime"
  1419. :group 'org-time
  1420. :set 'set-default
  1421. :type 'sexp)
  1422. (make-variable-buffer-local 'org-display-custom-times)
  1423. (defcustom org-time-stamp-custom-formats
  1424. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1425. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1426. These are overlayed over the default ISO format if the variable
  1427. `org-display-custom-times' is set. Time like %H:%M should be at the
  1428. end of the second format."
  1429. :group 'org-time
  1430. :type 'sexp)
  1431. (defun org-time-stamp-format (&optional long inactive)
  1432. "Get the right format for a time string."
  1433. (let ((f (if long (cdr org-time-stamp-formats)
  1434. (car org-time-stamp-formats))))
  1435. (if inactive
  1436. (concat "[" (substring f 1 -1) "]")
  1437. f)))
  1438. (defcustom org-time-clocksum-format "%d:%02d"
  1439. "The format string used when creating CLOCKSUM lines, or when
  1440. org-mode generates a time duration."
  1441. :group 'org-time
  1442. :type 'string)
  1443. (defcustom org-deadline-warning-days 14
  1444. "No. of days before expiration during which a deadline becomes active.
  1445. This variable governs the display in sparse trees and in the agenda.
  1446. When 0 or negative, it means use this number (the absolute value of it)
  1447. even if a deadline has a different individual lead time specified."
  1448. :group 'org-time
  1449. :group 'org-agenda-daily/weekly
  1450. :type 'number)
  1451. (defcustom org-read-date-prefer-future t
  1452. "Non-nil means, assume future for incomplete date input from user.
  1453. This affects the following situations:
  1454. 1. The user gives a day, but no month.
  1455. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1456. read this as the third of *next* month. However, if you enter \"17\",
  1457. it will be considered as *this* month.
  1458. 2. The user gives a month but not a year.
  1459. For example, if it is april and you enter \"feb 2\", this will be read
  1460. as feb 2, *next* year. \"May 5\", however, will be this year.
  1461. Currently this does not work for ISO week specifications.
  1462. When this option is nil, the current month and year will always be used
  1463. as defaults."
  1464. :group 'org-time
  1465. :type 'boolean)
  1466. (defcustom org-read-date-display-live t
  1467. "Non-nil means, display current interpretation of date prompt live.
  1468. This display will be in an overlay, in the minibuffer."
  1469. :group 'org-time
  1470. :type 'boolean)
  1471. (defcustom org-read-date-popup-calendar t
  1472. "Non-nil means, pop up a calendar when prompting for a date.
  1473. In the calendar, the date can be selected with mouse-1. However, the
  1474. minibuffer will also be active, and you can simply enter the date as well.
  1475. When nil, only the minibuffer will be available."
  1476. :group 'org-time
  1477. :type 'boolean)
  1478. (if (fboundp 'defvaralias)
  1479. (defvaralias 'org-popup-calendar-for-date-prompt
  1480. 'org-read-date-popup-calendar))
  1481. (defcustom org-extend-today-until 0
  1482. "The hour when your day really ends.
  1483. This has influence for the following applications:
  1484. - When switching the agenda to \"today\". It it is still earlier than
  1485. the time given here, the day recognized as TODAY is actually yesterday.
  1486. - When a date is read from the user and it is still before the time given
  1487. here, the current date and time will be assumed to be yesterday, 23:59.
  1488. FIXME:
  1489. IMPORTANT: This is still a very experimental feature, it may disappear
  1490. again or it may be extended to mean more things."
  1491. :group 'org-time
  1492. :type 'number)
  1493. (defcustom org-edit-timestamp-down-means-later nil
  1494. "Non-nil means, S-down will increase the time in a time stamp.
  1495. When nil, S-up will increase."
  1496. :group 'org-time
  1497. :type 'boolean)
  1498. (defcustom org-calendar-follow-timestamp-change t
  1499. "Non-nil means, make the calendar window follow timestamp changes.
  1500. When a timestamp is modified and the calendar window is visible, it will be
  1501. moved to the new date."
  1502. :group 'org-time
  1503. :type 'boolean)
  1504. (defgroup org-tags nil
  1505. "Options concerning tags in Org-mode."
  1506. :tag "Org Tags"
  1507. :group 'org)
  1508. (defcustom org-tag-alist nil
  1509. "List of tags allowed in Org-mode files.
  1510. When this list is nil, Org-mode will base TAG input on what is already in the
  1511. buffer.
  1512. The value of this variable is an alist, the car of each entry must be a
  1513. keyword as a string, the cdr may be a character that is used to select
  1514. that tag through the fast-tag-selection interface.
  1515. See the manual for details."
  1516. :group 'org-tags
  1517. :type '(repeat
  1518. (choice
  1519. (cons (string :tag "Tag name")
  1520. (character :tag "Access char"))
  1521. (const :tag "Start radio group" (:startgroup))
  1522. (const :tag "End radio group" (:endgroup)))))
  1523. (defvar org-file-tags nil
  1524. "List of tags that can be inherited by all entries in the file.
  1525. The tags will be inherited if the variable `org-use-tag-inheritance'
  1526. says they should be.
  1527. This variable is populated from #+TAG lines.")
  1528. (defcustom org-use-fast-tag-selection 'auto
  1529. "Non-nil means, use fast tag selection scheme.
  1530. This is a special interface to select and deselect tags with single keys.
  1531. When nil, fast selection is never used.
  1532. When the symbol `auto', fast selection is used if and only if selection
  1533. characters for tags have been configured, either through the variable
  1534. `org-tag-alist' or through a #+TAGS line in the buffer.
  1535. When t, fast selection is always used and selection keys are assigned
  1536. automatically if necessary."
  1537. :group 'org-tags
  1538. :type '(choice
  1539. (const :tag "Always" t)
  1540. (const :tag "Never" nil)
  1541. (const :tag "When selection characters are configured" 'auto)))
  1542. (defcustom org-fast-tag-selection-single-key nil
  1543. "Non-nil means, fast tag selection exits after first change.
  1544. When nil, you have to press RET to exit it.
  1545. During fast tag selection, you can toggle this flag with `C-c'.
  1546. This variable can also have the value `expert'. In this case, the window
  1547. displaying the tags menu is not even shown, until you press C-c again."
  1548. :group 'org-tags
  1549. :type '(choice
  1550. (const :tag "No" nil)
  1551. (const :tag "Yes" t)
  1552. (const :tag "Expert" expert)))
  1553. (defvar org-fast-tag-selection-include-todo nil
  1554. "Non-nil means, fast tags selection interface will also offer TODO states.
  1555. This is an undocumented feature, you should not rely on it.")
  1556. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1557. "The column to which tags should be indented in a headline.
  1558. If this number is positive, it specifies the column. If it is negative,
  1559. it means that the tags should be flushright to that column. For example,
  1560. -80 works well for a normal 80 character screen."
  1561. :group 'org-tags
  1562. :type 'integer)
  1563. (defcustom org-auto-align-tags t
  1564. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1565. These operations change the length of a headline and therefore shift
  1566. the tags around. With this options turned on, after each such operation
  1567. the tags are again aligned to `org-tags-column'."
  1568. :group 'org-tags
  1569. :type 'boolean)
  1570. (defcustom org-use-tag-inheritance t
  1571. "Non-nil means, tags in levels apply also for sublevels.
  1572. When nil, only the tags directly given in a specific line apply there.
  1573. If this option is t, a match early-on in a tree can lead to a large
  1574. number of matches in the subtree. If you only want to see the first
  1575. match in a tree during a search, check out the variable
  1576. `org-tags-match-list-sublevels'.
  1577. This may also be a list of tags that should be inherited, or a regexp that
  1578. matches tags that should be inherited."
  1579. :group 'org-tags
  1580. :type '(choice
  1581. (const :tag "Not" nil)
  1582. (const :tag "Always" t)
  1583. (repeat :tag "Specific tags" (string :tag "Tag"))
  1584. (regexp :tag "Tags matched by regexp")))
  1585. (defun org-tag-inherit-p (tag)
  1586. "Check if TAG is one that should be inherited."
  1587. (cond
  1588. ((eq org-use-tag-inheritance t) t)
  1589. ((not org-use-tag-inheritance) nil)
  1590. ((stringp org-use-tag-inheritance)
  1591. (string-match org-use-tag-inheritance tag))
  1592. ((listp org-use-tag-inheritance)
  1593. (member tag org-use-tag-inheritance))
  1594. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1595. (defcustom org-tags-match-list-sublevels t
  1596. "Non-nil means list also sublevels of headlines matching tag search.
  1597. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1598. the sublevels of a headline matching a tag search often also match
  1599. the same search. Listing all of them can create very long lists.
  1600. Setting this variable to nil causes subtrees of a match to be skipped.
  1601. This option is off by default, because inheritance in on. If you turn
  1602. inheritance off, you very likely want to turn this option on.
  1603. As a special case, if the tag search is restricted to TODO items, the
  1604. value of this variable is ignored and sublevels are always checked, to
  1605. make sure all corresponding TODO items find their way into the list."
  1606. :group 'org-tags
  1607. :type 'boolean)
  1608. (defvar org-tags-history nil
  1609. "History of minibuffer reads for tags.")
  1610. (defvar org-last-tags-completion-table nil
  1611. "The last used completion table for tags.")
  1612. (defvar org-after-tags-change-hook nil
  1613. "Hook that is run after the tags in a line have changed.")
  1614. (defgroup org-properties nil
  1615. "Options concerning properties in Org-mode."
  1616. :tag "Org Properties"
  1617. :group 'org)
  1618. (defcustom org-property-format "%-10s %s"
  1619. "How property key/value pairs should be formatted by `indent-line'.
  1620. When `indent-line' hits a property definition, it will format the line
  1621. according to this format, mainly to make sure that the values are
  1622. lined-up with respect to each other."
  1623. :group 'org-properties
  1624. :type 'string)
  1625. (defcustom org-use-property-inheritance nil
  1626. "Non-nil means, properties apply also for sublevels.
  1627. This setting is chiefly used during property searches. Turning it on can
  1628. cause significant overhead when doing a search, which is why it is not
  1629. on by default.
  1630. When nil, only the properties directly given in the current entry count.
  1631. When t, every property is inherited. The value may also be a list of
  1632. properties that should have inheritance, or a regular expression matching
  1633. properties that should be inherited.
  1634. However, note that some special properties use inheritance under special
  1635. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1636. and the properties ending in \"_ALL\" when they are used as descriptor
  1637. for valid values of a property.
  1638. Note for programmers:
  1639. When querying an entry with `org-entry-get', you can control if inheritance
  1640. should be used. By default, `org-entry-get' looks only at the local
  1641. properties. You can request inheritance by setting the inherit argument
  1642. to t (to force inheritance) or to `selective' (to respect the setting
  1643. in this variable)."
  1644. :group 'org-properties
  1645. :type '(choice
  1646. (const :tag "Not" nil)
  1647. (const :tag "Always" t)
  1648. (repeat :tag "Specific properties" (string :tag "Property"))
  1649. (regexp :tag "Properties matched by regexp")))
  1650. (defun org-property-inherit-p (property)
  1651. "Check if PROPERTY is one that should be inherited."
  1652. (cond
  1653. ((eq org-use-property-inheritance t) t)
  1654. ((not org-use-property-inheritance) nil)
  1655. ((stringp org-use-property-inheritance)
  1656. (string-match org-use-property-inheritance property))
  1657. ((listp org-use-property-inheritance)
  1658. (member property org-use-property-inheritance))
  1659. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1660. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1661. "The default column format, if no other format has been defined.
  1662. This variable can be set on the per-file basis by inserting a line
  1663. #+COLUMNS: %25ITEM ....."
  1664. :group 'org-properties
  1665. :type 'string)
  1666. (defcustom org-columns-ellipses ".."
  1667. "The ellipses to be used when a field in column view is truncated.
  1668. When this is the empty string, as many characters as possible are shown,
  1669. but then there will be no visual indication that the field has been truncated.
  1670. When this is a string of length N, the last N characters of a truncated
  1671. field are replaced by this string. If the column is narrower than the
  1672. ellipses string, only part of the ellipses string will be shown."
  1673. :group 'org-properties
  1674. :type 'string)
  1675. (defcustom org-columns-modify-value-for-display-function nil
  1676. "Function that modifies values for display in column view.
  1677. For example, it can be used to cut out a certain part from a time stamp.
  1678. The function must take 2 argments:
  1679. column-title The tite of the column (*not* the property name)
  1680. value The value that should be modified.
  1681. The function should return the value that should be displayed,
  1682. or nil if the normal value should be used."
  1683. :group 'org-properties
  1684. :type 'function)
  1685. (defcustom org-effort-property "Effort"
  1686. "The property that is being used to keep track of effort estimates.
  1687. Effort estimates given in this property need to have the format H:MM."
  1688. :group 'org-properties
  1689. :group 'org-progress
  1690. :type '(string :tag "Property"))
  1691. (defconst org-global-properties-fixed
  1692. '(("VISIBILITY_ALL" . "folded children content all"))
  1693. "List of property/value pairs that can be inherited by any entry.
  1694. These are fixed values, for the preset properties.")
  1695. (defcustom org-global-properties nil
  1696. "List of property/value pairs that can be inherited by any entry.
  1697. You can set buffer-local values for this by adding lines like
  1698. #+PROPERTY: NAME VALUE"
  1699. :group 'org-properties
  1700. :type '(repeat
  1701. (cons (string :tag "Property")
  1702. (string :tag "Value"))))
  1703. (defvar org-file-properties nil
  1704. "List of property/value pairs that can be inherited by any entry.
  1705. Valid for the current buffer.
  1706. This variable is populated from #+PROPERTY lines.")
  1707. (make-variable-buffer-local 'org-file-properties)
  1708. (defgroup org-agenda nil
  1709. "Options concerning agenda views in Org-mode."
  1710. :tag "Org Agenda"
  1711. :group 'org)
  1712. (defvar org-category nil
  1713. "Variable used by org files to set a category for agenda display.
  1714. Such files should use a file variable to set it, for example
  1715. # -*- mode: org; org-category: \"ELisp\"
  1716. or contain a special line
  1717. #+CATEGORY: ELisp
  1718. If the file does not specify a category, then file's base name
  1719. is used instead.")
  1720. (make-variable-buffer-local 'org-category)
  1721. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1722. (defcustom org-agenda-files nil
  1723. "The files to be used for agenda display.
  1724. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1725. \\[org-remove-file]. You can also use customize to edit the list.
  1726. If an entry is a directory, all files in that directory that are matched by
  1727. `org-agenda-file-regexp' will be part of the file list.
  1728. If the value of the variable is not a list but a single file name, then
  1729. the list of agenda files is actually stored and maintained in that file, one
  1730. agenda file per line."
  1731. :group 'org-agenda
  1732. :type '(choice
  1733. (repeat :tag "List of files and directories" file)
  1734. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1735. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1736. "Regular expression to match files for `org-agenda-files'.
  1737. If any element in the list in that variable contains a directory instead
  1738. of a normal file, all files in that directory that are matched by this
  1739. regular expression will be included."
  1740. :group 'org-agenda
  1741. :type 'regexp)
  1742. (defcustom org-agenda-text-search-extra-files nil
  1743. "List of extra files to be searched by text search commands.
  1744. These files will be search in addition to the agenda files by the
  1745. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1746. Note that these files will only be searched for text search commands,
  1747. not for the other agenda views like todo lists, tag searches or the weekly
  1748. agenda. This variable is intended to list notes and possibly archive files
  1749. that should also be searched by these two commands.
  1750. In fact, if the first element in the list is the symbol `agenda-archives',
  1751. than all archive files of all agenda files will be added to the search
  1752. scope."
  1753. :group 'org-agenda
  1754. :type '(set :greedy t
  1755. (const :tag "Agenda Archives" agenda-archives)
  1756. (repeat :inline t (file))))
  1757. (if (fboundp 'defvaralias)
  1758. (defvaralias 'org-agenda-multi-occur-extra-files
  1759. 'org-agenda-text-search-extra-files))
  1760. (defcustom org-agenda-skip-unavailable-files nil
  1761. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1762. A nil value means to remove them, after a query, from the list."
  1763. :group 'org-agenda
  1764. :type 'boolean)
  1765. (defcustom org-calendar-to-agenda-key [?c]
  1766. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1767. The command `org-calendar-goto-agenda' will be bound to this key. The
  1768. default is the character `c' because then `c' can be used to switch back and
  1769. forth between agenda and calendar."
  1770. :group 'org-agenda
  1771. :type 'sexp)
  1772. (defcustom org-calendar-agenda-action-key [?k]
  1773. "The key to be installed in `calendar-mode-map' for agenda-action.
  1774. The command `org-agenda-action' will be bound to this key. The
  1775. default is the character `k' because we use the same key in the agenda."
  1776. :group 'org-agenda
  1777. :type 'sexp)
  1778. (eval-after-load "calendar"
  1779. '(progn
  1780. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1781. 'org-calendar-goto-agenda)
  1782. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1783. 'org-agenda-action)))
  1784. (defgroup org-latex nil
  1785. "Options for embedding LaTeX code into Org-mode."
  1786. :tag "Org LaTeX"
  1787. :group 'org)
  1788. (defcustom org-format-latex-options
  1789. '(:foreground default :background default :scale 1.0
  1790. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1791. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1792. "Options for creating images from LaTeX fragments.
  1793. This is a property list with the following properties:
  1794. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1795. `default' means use the foreground of the default face.
  1796. :background the background color, or \"Transparent\".
  1797. `default' means use the background of the default face.
  1798. :scale a scaling factor for the size of the images.
  1799. :html-foreground, :html-background, :html-scale
  1800. the same numbers for HTML export.
  1801. :matchers a list indicating which matchers should be used to
  1802. find LaTeX fragments. Valid members of this list are:
  1803. \"begin\" find environments
  1804. \"$\" find math expressions surrounded by $...$
  1805. \"$$\" find math expressions surrounded by $$....$$
  1806. \"\\(\" find math expressions surrounded by \\(...\\)
  1807. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1808. :group 'org-latex
  1809. :type 'plist)
  1810. (defcustom org-format-latex-header "\\documentclass{article}
  1811. \\usepackage{fullpage} % do not remove
  1812. \\usepackage{amssymb}
  1813. \\usepackage[usenames]{color}
  1814. \\usepackage{amsmath}
  1815. \\usepackage{latexsym}
  1816. \\usepackage[mathscr]{eucal}
  1817. \\pagestyle{empty} % do not remove"
  1818. "The document header used for processing LaTeX fragments."
  1819. :group 'org-latex
  1820. :type 'string)
  1821. (defgroup org-font-lock nil
  1822. "Font-lock settings for highlighting in Org-mode."
  1823. :tag "Org Font Lock"
  1824. :group 'org)
  1825. (defcustom org-level-color-stars-only nil
  1826. "Non-nil means fontify only the stars in each headline.
  1827. When nil, the entire headline is fontified.
  1828. Changing it requires restart of `font-lock-mode' to become effective
  1829. also in regions already fontified."
  1830. :group 'org-font-lock
  1831. :type 'boolean)
  1832. (defcustom org-hide-leading-stars nil
  1833. "Non-nil means, hide the first N-1 stars in a headline.
  1834. This works by using the face `org-hide' for these stars. This
  1835. face is white for a light background, and black for a dark
  1836. background. You may have to customize the face `org-hide' to
  1837. make this work.
  1838. Changing it requires restart of `font-lock-mode' to become effective
  1839. also in regions already fontified.
  1840. You may also set this on a per-file basis by adding one of the following
  1841. lines to the buffer:
  1842. #+STARTUP: hidestars
  1843. #+STARTUP: showstars"
  1844. :group 'org-font-lock
  1845. :type 'boolean)
  1846. (defcustom org-fontify-done-headline nil
  1847. "Non-nil means, change the face of a headline if it is marked DONE.
  1848. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1849. When this is non-nil, the headline after the keyword is set to the
  1850. `org-headline-done' as an additional indication."
  1851. :group 'org-font-lock
  1852. :type 'boolean)
  1853. (defcustom org-fontify-emphasized-text t
  1854. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1855. Changing this variable requires a restart of Emacs to take effect."
  1856. :group 'org-font-lock
  1857. :type 'boolean)
  1858. (defcustom org-highlight-latex-fragments-and-specials nil
  1859. "Non-nil means, fontify what is treated specially by the exporters."
  1860. :group 'org-font-lock
  1861. :type 'boolean)
  1862. (defcustom org-hide-emphasis-markers nil
  1863. "Non-nil mean font-lock should hide the emphasis marker characters."
  1864. :group 'org-font-lock
  1865. :type 'boolean)
  1866. (defvar org-emph-re nil
  1867. "Regular expression for matching emphasis.")
  1868. (defvar org-verbatim-re nil
  1869. "Regular expression for matching verbatim text.")
  1870. (defvar org-emphasis-regexp-components) ; defined just below
  1871. (defvar org-emphasis-alist) ; defined just below
  1872. (defun org-set-emph-re (var val)
  1873. "Set variable and compute the emphasis regular expression."
  1874. (set var val)
  1875. (when (and (boundp 'org-emphasis-alist)
  1876. (boundp 'org-emphasis-regexp-components)
  1877. org-emphasis-alist org-emphasis-regexp-components)
  1878. (let* ((e org-emphasis-regexp-components)
  1879. (pre (car e))
  1880. (post (nth 1 e))
  1881. (border (nth 2 e))
  1882. (body (nth 3 e))
  1883. (nl (nth 4 e))
  1884. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1885. (body1 (concat body "*?"))
  1886. (markers (mapconcat 'car org-emphasis-alist ""))
  1887. (vmarkers (mapconcat
  1888. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1889. org-emphasis-alist "")))
  1890. ;; make sure special characters appear at the right position in the class
  1891. (if (string-match "\\^" markers)
  1892. (setq markers (concat (replace-match "" t t markers) "^")))
  1893. (if (string-match "-" markers)
  1894. (setq markers (concat (replace-match "" t t markers) "-")))
  1895. (if (string-match "\\^" vmarkers)
  1896. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1897. (if (string-match "-" vmarkers)
  1898. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1899. (if (> nl 0)
  1900. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1901. (int-to-string nl) "\\}")))
  1902. ;; Make the regexp
  1903. (setq org-emph-re
  1904. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1905. "\\("
  1906. "\\([" markers "]\\)"
  1907. "\\("
  1908. "[^" border "]\\|"
  1909. "[^" border (if (and nil stacked) markers) "]"
  1910. body1
  1911. "[^" border (if (and nil stacked) markers) "]"
  1912. "\\)"
  1913. "\\3\\)"
  1914. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1915. (setq org-verbatim-re
  1916. (concat "\\([" pre "]\\|^\\)"
  1917. "\\("
  1918. "\\([" vmarkers "]\\)"
  1919. "\\("
  1920. "[^" border "]\\|"
  1921. "[^" border "]"
  1922. body1
  1923. "[^" border "]"
  1924. "\\)"
  1925. "\\3\\)"
  1926. "\\([" post "]\\|$\\)")))))
  1927. (defcustom org-emphasis-regexp-components
  1928. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1929. "Components used to build the regular expression for emphasis.
  1930. This is a list with 6 entries. Terminology: In an emphasis string
  1931. like \" *strong word* \", we call the initial space PREMATCH, the final
  1932. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1933. and \"trong wor\" is the body. The different components in this variable
  1934. specify what is allowed/forbidden in each part:
  1935. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1936. post Chars allowed as postmatch. End of line will be allowed too.
  1937. border The chars *forbidden* as border characters.
  1938. body-regexp A regexp like \".\" to match a body character. Don't use
  1939. non-shy groups here, and don't allow newline here.
  1940. newline The maximum number of newlines allowed in an emphasis exp.
  1941. Use customize to modify this, or restart Emacs after changing it."
  1942. :group 'org-font-lock
  1943. :set 'org-set-emph-re
  1944. :type '(list
  1945. (sexp :tag "Allowed chars in pre ")
  1946. (sexp :tag "Allowed chars in post ")
  1947. (sexp :tag "Forbidden chars in border ")
  1948. (sexp :tag "Regexp for body ")
  1949. (integer :tag "number of newlines allowed")
  1950. (option (boolean :tag "Please ignore this button"))))
  1951. (defcustom org-emphasis-alist
  1952. `(("*" bold "<b>" "</b>")
  1953. ("/" italic "<i>" "</i>")
  1954. ("_" underline "<u>" "</u>")
  1955. ("=" org-code "<code>" "</code>" verbatim)
  1956. ("~" org-verbatim "" "" verbatim)
  1957. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1958. "<del>" "</del>")
  1959. )
  1960. "Special syntax for emphasized text.
  1961. Text starting and ending with a special character will be emphasized, for
  1962. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1963. characters, the face to be used by font-lock for highlighting in Org-mode
  1964. Emacs buffers, and the HTML tags to be used for this.
  1965. Use customize to modify this, or restart Emacs after changing it."
  1966. :group 'org-font-lock
  1967. :set 'org-set-emph-re
  1968. :type '(repeat
  1969. (list
  1970. (string :tag "Marker character")
  1971. (choice
  1972. (face :tag "Font-lock-face")
  1973. (plist :tag "Face property list"))
  1974. (string :tag "HTML start tag")
  1975. (string :tag "HTML end tag")
  1976. (option (const verbatim)))))
  1977. ;;; Miscellaneous options
  1978. (defgroup org-completion nil
  1979. "Completion in Org-mode."
  1980. :tag "Org Completion"
  1981. :group 'org)
  1982. (defcustom org-completion-fallback-command 'hippie-expand
  1983. "The expansion command called by \\[org-complete] in normal context.
  1984. Normal means, no org-mode-specific context."
  1985. :group 'org-completion
  1986. :type 'function)
  1987. ;;; Functions and variables from ther packages
  1988. ;; Declared here to avoid compiler warnings
  1989. ;; XEmacs only
  1990. (defvar outline-mode-menu-heading)
  1991. (defvar outline-mode-menu-show)
  1992. (defvar outline-mode-menu-hide)
  1993. (defvar zmacs-regions) ; XEmacs regions
  1994. ;; Emacs only
  1995. (defvar mark-active)
  1996. ;; Various packages
  1997. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1998. (declare-function calendar-forward-day "cal-move" (arg))
  1999. (declare-function calendar-goto-date "cal-move" (date))
  2000. (declare-function calendar-goto-today "cal-move" ())
  2001. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2002. (defvar calc-embedded-close-formula)
  2003. (defvar calc-embedded-open-formula)
  2004. (declare-function cdlatex-tab "ext:cdlatex" ())
  2005. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2006. (defvar font-lock-unfontify-region-function)
  2007. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2008. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2009. (defvar iswitchb-temp-buflist)
  2010. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2011. (declare-function org-agenda-skip "org-agenda" ())
  2012. (declare-function org-format-agenda-item "org-agenda"
  2013. (extra txt &optional category tags dotime noprefix remove-re))
  2014. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2015. (declare-function org-agenda-change-all-lines "org-agenda"
  2016. (newhead hdmarker &optional fixface))
  2017. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2018. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2019. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2020. (beg end))
  2021. (declare-function parse-time-string "parse-time" (string))
  2022. (declare-function remember "remember" (&optional initial))
  2023. (declare-function remember-buffer-desc "remember" ())
  2024. (declare-function remember-finalize "remember" ())
  2025. (defvar remember-save-after-remembering)
  2026. (defvar remember-data-file)
  2027. (defvar remember-register)
  2028. (defvar remember-buffer)
  2029. (defvar remember-handler-functions)
  2030. (defvar remember-annotation-functions)
  2031. (defvar texmathp-why)
  2032. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2033. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2034. (defvar w3m-current-url)
  2035. (defvar w3m-current-title)
  2036. (defvar org-latex-regexps)
  2037. ;;; Autoload and prepare some org modules
  2038. ;; Some table stuff that needs to be defined here, because it is used
  2039. ;; by the functions setting up org-mode or checking for table context.
  2040. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2041. "Detects an org-type or table-type table.")
  2042. (defconst org-table-line-regexp "^[ \t]*|"
  2043. "Detects an org-type table line.")
  2044. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2045. "Detects an org-type table line.")
  2046. (defconst org-table-hline-regexp "^[ \t]*|-"
  2047. "Detects an org-type table hline.")
  2048. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2049. "Detects a table-type table hline.")
  2050. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2051. "Searching from within a table (any type) this finds the first line
  2052. outside the table.")
  2053. ;; Autoload the functions in org-table.el that are needed by functions here.
  2054. (eval-and-compile
  2055. (org-autoload "org-table"
  2056. '(org-table-align org-table-begin org-table-blank-field
  2057. org-table-convert org-table-convert-region org-table-copy-down
  2058. org-table-copy-region org-table-create
  2059. org-table-create-or-convert-from-region
  2060. org-table-create-with-table.el org-table-current-dline
  2061. org-table-cut-region org-table-delete-column org-table-edit-field
  2062. org-table-edit-formulas org-table-end org-table-eval-formula
  2063. org-table-export org-table-field-info
  2064. org-table-get-stored-formulas org-table-goto-column
  2065. org-table-hline-and-move org-table-import org-table-insert-column
  2066. org-table-insert-hline org-table-insert-row org-table-iterate
  2067. org-table-justify-field-maybe org-table-kill-row
  2068. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2069. org-table-move-column org-table-move-column-left
  2070. org-table-move-column-right org-table-move-row
  2071. org-table-move-row-down org-table-move-row-up
  2072. org-table-next-field org-table-next-row org-table-paste-rectangle
  2073. org-table-previous-field org-table-recalculate
  2074. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2075. org-table-toggle-coordinate-overlays
  2076. org-table-toggle-formula-debugger org-table-wrap-region
  2077. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2078. (defun org-at-table-p (&optional table-type)
  2079. "Return t if the cursor is inside an org-type table.
  2080. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2081. (if org-enable-table-editor
  2082. (save-excursion
  2083. (beginning-of-line 1)
  2084. (looking-at (if table-type org-table-any-line-regexp
  2085. org-table-line-regexp)))
  2086. nil))
  2087. (defsubst org-table-p () (org-at-table-p))
  2088. (defun org-at-table.el-p ()
  2089. "Return t if and only if we are at a table.el table."
  2090. (and (org-at-table-p 'any)
  2091. (save-excursion
  2092. (goto-char (org-table-begin 'any))
  2093. (looking-at org-table1-hline-regexp))))
  2094. (defun org-table-recognize-table.el ()
  2095. "If there is a table.el table nearby, recognize it and move into it."
  2096. (if org-table-tab-recognizes-table.el
  2097. (if (org-at-table.el-p)
  2098. (progn
  2099. (beginning-of-line 1)
  2100. (if (looking-at org-table-dataline-regexp)
  2101. nil
  2102. (if (looking-at org-table1-hline-regexp)
  2103. (progn
  2104. (beginning-of-line 2)
  2105. (if (looking-at org-table-any-border-regexp)
  2106. (beginning-of-line -1)))))
  2107. (if (re-search-forward "|" (org-table-end t) t)
  2108. (progn
  2109. (require 'table)
  2110. (if (table--at-cell-p (point))
  2111. t
  2112. (message "recognizing table.el table...")
  2113. (table-recognize-table)
  2114. (message "recognizing table.el table...done")))
  2115. (error "This should not happen..."))
  2116. t)
  2117. nil)
  2118. nil))
  2119. (defun org-at-table-hline-p ()
  2120. "Return t if the cursor is inside a hline in a table."
  2121. (if org-enable-table-editor
  2122. (save-excursion
  2123. (beginning-of-line 1)
  2124. (looking-at org-table-hline-regexp))
  2125. nil))
  2126. (defvar org-table-clean-did-remove-column nil)
  2127. (defun org-table-map-tables (function)
  2128. "Apply FUNCTION to the start of all tables in the buffer."
  2129. (save-excursion
  2130. (save-restriction
  2131. (widen)
  2132. (goto-char (point-min))
  2133. (while (re-search-forward org-table-any-line-regexp nil t)
  2134. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2135. (beginning-of-line 1)
  2136. (if (looking-at org-table-line-regexp)
  2137. (save-excursion (funcall function)))
  2138. (re-search-forward org-table-any-border-regexp nil 1))))
  2139. (message "Mapping tables: done"))
  2140. ;; Declare and autoload functions from org-exp.el
  2141. (declare-function org-default-export-plist "org-exp")
  2142. (declare-function org-infile-export-plist "org-exp")
  2143. (declare-function org-get-current-options "org-exp")
  2144. (eval-and-compile
  2145. (org-autoload "org-exp"
  2146. '(org-export org-export-as-ascii org-export-visible
  2147. org-insert-export-options-template org-export-as-html-and-open
  2148. org-export-as-html-batch org-export-as-html-to-buffer
  2149. org-replace-region-by-html org-export-region-as-html
  2150. org-export-as-html org-export-icalendar-this-file
  2151. org-export-icalendar-all-agenda-files
  2152. org-table-clean-before-export
  2153. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2154. ;; Declare and autoload functions from org-exp.el
  2155. (eval-and-compile
  2156. (org-autoload "org-exp"
  2157. '(org-agenda org-agenda-list org-search-view
  2158. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2159. org-diary org-agenda-to-appt)))
  2160. ;; Autoload org-remember
  2161. (eval-and-compile
  2162. (org-autoload "org-remember"
  2163. '(org-remember-insinuate org-remember-annotation
  2164. org-remember-apply-template org-remember org-remember-handler)))
  2165. ;; Autoload org-clock.el
  2166. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2167. (beg end))
  2168. (declare-function org-update-mode-line "org-clock" ())
  2169. (defvar org-clock-start-time)
  2170. (defvar org-clock-marker (make-marker)
  2171. "Marker recording the last clock-in.")
  2172. (eval-and-compile
  2173. (org-autoload
  2174. "org-clock"
  2175. '(org-clock-in org-clock-out org-clock-cancel
  2176. org-clock-goto org-clock-sum org-clock-display
  2177. org-remove-clock-overlays org-clock-report
  2178. org-clocktable-shift org-dblock-write:clocktable
  2179. org-get-clocktable)))
  2180. (defun org-clock-update-time-maybe ()
  2181. "If this is a CLOCK line, update it and return t.
  2182. Otherwise, return nil."
  2183. (interactive)
  2184. (save-excursion
  2185. (beginning-of-line 1)
  2186. (skip-chars-forward " \t")
  2187. (when (looking-at org-clock-string)
  2188. (let ((re (concat "[ \t]*" org-clock-string
  2189. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2190. "\\([ \t]*=>.*\\)?\\)?"))
  2191. ts te h m s)
  2192. (cond
  2193. ((not (looking-at re))
  2194. nil)
  2195. ((not (match-end 2))
  2196. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2197. (> org-clock-marker (point))
  2198. (<= org-clock-marker (point-at-eol)))
  2199. ;; The clock is running here
  2200. (setq org-clock-start-time
  2201. (apply 'encode-time
  2202. (org-parse-time-string (match-string 1))))
  2203. (org-update-mode-line)))
  2204. (t
  2205. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2206. (end-of-line 1)
  2207. (setq ts (match-string 1)
  2208. te (match-string 3))
  2209. (setq s (- (time-to-seconds
  2210. (apply 'encode-time (org-parse-time-string te)))
  2211. (time-to-seconds
  2212. (apply 'encode-time (org-parse-time-string ts))))
  2213. h (floor (/ s 3600))
  2214. s (- s (* 3600 h))
  2215. m (floor (/ s 60))
  2216. s (- s (* 60 s)))
  2217. (insert " => " (format "%2d:%02d" h m))
  2218. t))))))
  2219. (defun org-check-running-clock ()
  2220. "Check if the current buffer contains the running clock.
  2221. If yes, offer to stop it and to save the buffer with the changes."
  2222. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2223. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2224. (buffer-name))))
  2225. (org-clock-out)
  2226. (when (y-or-n-p "Save changed buffer?")
  2227. (save-buffer))))
  2228. (defun org-clocktable-try-shift (dir n)
  2229. "Check if this line starts a clock table, if yes, shift the time block."
  2230. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2231. (org-clocktable-shift dir n)))
  2232. ;; Autoload archiving code
  2233. ;; The stuff that is needed for cycling and tags has to be defined here.
  2234. (defgroup org-archive nil
  2235. "Options concerning archiving in Org-mode."
  2236. :tag "Org Archive"
  2237. :group 'org-structure)
  2238. (defcustom org-archive-location "%s_archive::"
  2239. "The location where subtrees should be archived.
  2240. Otherwise, the value of this variable is a string, consisting of two
  2241. parts, separated by a double-colon.
  2242. The first part is a file name - when omitted, archiving happens in the same
  2243. file. %s will be replaced by the current file name (without directory part).
  2244. Archiving to a different file is useful to keep archived entries from
  2245. contributing to the Org-mode Agenda.
  2246. The part after the double colon is a headline. The archived entries will be
  2247. filed under that headline. When omitted, the subtrees are simply filed away
  2248. at the end of the file, as top-level entries.
  2249. Here are a few examples:
  2250. \"%s_archive::\"
  2251. If the current file is Projects.org, archive in file
  2252. Projects.org_archive, as top-level trees. This is the default.
  2253. \"::* Archived Tasks\"
  2254. Archive in the current file, under the top-level headline
  2255. \"* Archived Tasks\".
  2256. \"~/org/archive.org::\"
  2257. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2258. \"basement::** Finished Tasks\"
  2259. Archive in file ./basement (relative path), as level 3 trees
  2260. below the level 2 heading \"** Finished Tasks\".
  2261. You may set this option on a per-file basis by adding to the buffer a
  2262. line like
  2263. #+ARCHIVE: basement::** Finished Tasks
  2264. You may also define it locally for a subtree by setting an ARCHIVE property
  2265. in the entry. If such a property is found in an entry, or anywhere up
  2266. the hierarchy, it will be used."
  2267. :group 'org-archive
  2268. :type 'string)
  2269. (defcustom org-archive-tag "ARCHIVE"
  2270. "The tag that marks a subtree as archived.
  2271. An archived subtree does not open during visibility cycling, and does
  2272. not contribute to the agenda listings.
  2273. After changing this, font-lock must be restarted in the relevant buffers to
  2274. get the proper fontification."
  2275. :group 'org-archive
  2276. :group 'org-keywords
  2277. :type 'string)
  2278. (defcustom org-agenda-skip-archived-trees t
  2279. "Non-nil means, the agenda will skip any items located in archived trees.
  2280. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2281. variable is no longer recommended, you should leave it at the value t.
  2282. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2283. :group 'org-archive
  2284. :group 'org-agenda-skip
  2285. :type 'boolean)
  2286. (defcustom org-cycle-open-archived-trees nil
  2287. "Non-nil means, `org-cycle' will open archived trees.
  2288. An archived tree is a tree marked with the tag ARCHIVE.
  2289. When nil, archived trees will stay folded. You can still open them with
  2290. normal outline commands like `show-all', but not with the cycling commands."
  2291. :group 'org-archive
  2292. :group 'org-cycle
  2293. :type 'boolean)
  2294. (defcustom org-sparse-tree-open-archived-trees nil
  2295. "Non-nil means sparse tree construction shows matches in archived trees.
  2296. When nil, matches in these trees are highlighted, but the trees are kept in
  2297. collapsed state."
  2298. :group 'org-archive
  2299. :group 'org-sparse-trees
  2300. :type 'boolean)
  2301. (defun org-cycle-hide-archived-subtrees (state)
  2302. "Re-hide all archived subtrees after a visibility state change."
  2303. (when (and (not org-cycle-open-archived-trees)
  2304. (not (memq state '(overview folded))))
  2305. (save-excursion
  2306. (let* ((globalp (memq state '(contents all)))
  2307. (beg (if globalp (point-min) (point)))
  2308. (end (if globalp (point-max) (org-end-of-subtree t))))
  2309. (org-hide-archived-subtrees beg end)
  2310. (goto-char beg)
  2311. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2312. (message "%s" (substitute-command-keys
  2313. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2314. (defun org-force-cycle-archived ()
  2315. "Cycle subtree even if it is archived."
  2316. (interactive)
  2317. (setq this-command 'org-cycle)
  2318. (let ((org-cycle-open-archived-trees t))
  2319. (call-interactively 'org-cycle)))
  2320. (defun org-hide-archived-subtrees (beg end)
  2321. "Re-hide all archived subtrees after a visibility state change."
  2322. (save-excursion
  2323. (let* ((re (concat ":" org-archive-tag ":")))
  2324. (goto-char beg)
  2325. (while (re-search-forward re end t)
  2326. (and (org-on-heading-p) (hide-subtree))
  2327. (org-end-of-subtree t)))))
  2328. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2329. (eval-and-compile
  2330. (org-autoload "org-archive"
  2331. '(org-add-archive-files org-archive-subtree
  2332. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2333. ;; Autoload Column View Code
  2334. (declare-function org-columns-number-to-string "org-colview")
  2335. (declare-function org-columns-get-format-and-top-level "org-colview")
  2336. (declare-function org-columns-compute "org-colview")
  2337. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2338. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2339. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2340. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2341. ;; Autoload ID code
  2342. (org-autoload "org-id"
  2343. '(org-id-get-create org-id-new org-id-copy org-id-get
  2344. org-id-get-with-outline-path-completion
  2345. org-id-get-with-outline-drilling
  2346. org-id-goto org-id-find))
  2347. ;;; Variables for pre-computed regular expressions, all buffer local
  2348. (defvar org-drawer-regexp nil
  2349. "Matches first line of a hidden block.")
  2350. (make-variable-buffer-local 'org-drawer-regexp)
  2351. (defvar org-todo-regexp nil
  2352. "Matches any of the TODO state keywords.")
  2353. (make-variable-buffer-local 'org-todo-regexp)
  2354. (defvar org-not-done-regexp nil
  2355. "Matches any of the TODO state keywords except the last one.")
  2356. (make-variable-buffer-local 'org-not-done-regexp)
  2357. (defvar org-todo-line-regexp nil
  2358. "Matches a headline and puts TODO state into group 2 if present.")
  2359. (make-variable-buffer-local 'org-todo-line-regexp)
  2360. (defvar org-complex-heading-regexp nil
  2361. "Matches a headline and puts everything into groups:
  2362. group 1: the stars
  2363. group 2: The todo keyword, maybe
  2364. group 3: Priority cookie
  2365. group 4: True headline
  2366. group 5: Tags")
  2367. (make-variable-buffer-local 'org-complex-heading-regexp)
  2368. (defvar org-todo-line-tags-regexp nil
  2369. "Matches a headline and puts TODO state into group 2 if present.
  2370. Also put tags into group 4 if tags are present.")
  2371. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2372. (defvar org-nl-done-regexp nil
  2373. "Matches newline followed by a headline with the DONE keyword.")
  2374. (make-variable-buffer-local 'org-nl-done-regexp)
  2375. (defvar org-looking-at-done-regexp nil
  2376. "Matches the DONE keyword a point.")
  2377. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2378. (defvar org-ds-keyword-length 12
  2379. "Maximum length of the Deadline and SCHEDULED keywords.")
  2380. (make-variable-buffer-local 'org-ds-keyword-length)
  2381. (defvar org-deadline-regexp nil
  2382. "Matches the DEADLINE keyword.")
  2383. (make-variable-buffer-local 'org-deadline-regexp)
  2384. (defvar org-deadline-time-regexp nil
  2385. "Matches the DEADLINE keyword together with a time stamp.")
  2386. (make-variable-buffer-local 'org-deadline-time-regexp)
  2387. (defvar org-deadline-line-regexp nil
  2388. "Matches the DEADLINE keyword and the rest of the line.")
  2389. (make-variable-buffer-local 'org-deadline-line-regexp)
  2390. (defvar org-scheduled-regexp nil
  2391. "Matches the SCHEDULED keyword.")
  2392. (make-variable-buffer-local 'org-scheduled-regexp)
  2393. (defvar org-scheduled-time-regexp nil
  2394. "Matches the SCHEDULED keyword together with a time stamp.")
  2395. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2396. (defvar org-closed-time-regexp nil
  2397. "Matches the CLOSED keyword together with a time stamp.")
  2398. (make-variable-buffer-local 'org-closed-time-regexp)
  2399. (defvar org-keyword-time-regexp nil
  2400. "Matches any of the 4 keywords, together with the time stamp.")
  2401. (make-variable-buffer-local 'org-keyword-time-regexp)
  2402. (defvar org-keyword-time-not-clock-regexp nil
  2403. "Matches any of the 3 keywords, together with the time stamp.")
  2404. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2405. (defvar org-maybe-keyword-time-regexp nil
  2406. "Matches a timestamp, possibly preceeded by a keyword.")
  2407. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2408. (defvar org-planning-or-clock-line-re nil
  2409. "Matches a line with planning or clock info.")
  2410. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2411. (defconst org-plain-time-of-day-regexp
  2412. (concat
  2413. "\\(\\<[012]?[0-9]"
  2414. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2415. "\\(--?"
  2416. "\\(\\<[012]?[0-9]"
  2417. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2418. "\\)?")
  2419. "Regular expression to match a plain time or time range.
  2420. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2421. groups carry important information:
  2422. 0 the full match
  2423. 1 the first time, range or not
  2424. 8 the second time, if it is a range.")
  2425. (defconst org-plain-time-extension-regexp
  2426. (concat
  2427. "\\(\\<[012]?[0-9]"
  2428. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2429. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2430. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2431. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2432. groups carry important information:
  2433. 0 the full match
  2434. 7 hours of duration
  2435. 9 minutes of duration")
  2436. (defconst org-stamp-time-of-day-regexp
  2437. (concat
  2438. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2439. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2440. "\\(--?"
  2441. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2442. "Regular expression to match a timestamp time or time range.
  2443. After a match, the following groups carry important information:
  2444. 0 the full match
  2445. 1 date plus weekday, for backreferencing to make sure both times on same day
  2446. 2 the first time, range or not
  2447. 4 the second time, if it is a range.")
  2448. (defconst org-startup-options
  2449. '(("fold" org-startup-folded t)
  2450. ("overview" org-startup-folded t)
  2451. ("nofold" org-startup-folded nil)
  2452. ("showall" org-startup-folded nil)
  2453. ("content" org-startup-folded content)
  2454. ("hidestars" org-hide-leading-stars t)
  2455. ("showstars" org-hide-leading-stars nil)
  2456. ("odd" org-odd-levels-only t)
  2457. ("oddeven" org-odd-levels-only nil)
  2458. ("align" org-startup-align-all-tables t)
  2459. ("noalign" org-startup-align-all-tables nil)
  2460. ("customtime" org-display-custom-times t)
  2461. ("logdone" org-log-done time)
  2462. ("lognotedone" org-log-done note)
  2463. ("nologdone" org-log-done nil)
  2464. ("lognoteclock-out" org-log-note-clock-out t)
  2465. ("nolognoteclock-out" org-log-note-clock-out nil)
  2466. ("logrepeat" org-log-repeat state)
  2467. ("lognoterepeat" org-log-repeat note)
  2468. ("nologrepeat" org-log-repeat nil)
  2469. ("constcgs" constants-unit-system cgs)
  2470. ("constSI" constants-unit-system SI))
  2471. "Variable associated with STARTUP options for org-mode.
  2472. Each element is a list of three items: The startup options as written
  2473. in the #+STARTUP line, the corresponding variable, and the value to
  2474. set this variable to if the option is found. An optional forth element PUSH
  2475. means to push this value onto the list in the variable.")
  2476. (defun org-set-regexps-and-options ()
  2477. "Precompute regular expressions for current buffer."
  2478. (when (org-mode-p)
  2479. (org-set-local 'org-todo-kwd-alist nil)
  2480. (org-set-local 'org-todo-key-alist nil)
  2481. (org-set-local 'org-todo-key-trigger nil)
  2482. (org-set-local 'org-todo-keywords-1 nil)
  2483. (org-set-local 'org-done-keywords nil)
  2484. (org-set-local 'org-todo-heads nil)
  2485. (org-set-local 'org-todo-sets nil)
  2486. (org-set-local 'org-todo-log-states nil)
  2487. (org-set-local 'org-file-properties nil)
  2488. (org-set-local 'org-file-tags nil)
  2489. (let ((re (org-make-options-regexp
  2490. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2491. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2492. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2493. (splitre "[ \t]+")
  2494. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2495. tail sep kws1 prio props ftags drawers
  2496. ext-setup-or-nil setup-contents (start 0))
  2497. (save-excursion
  2498. (save-restriction
  2499. (widen)
  2500. (goto-char (point-min))
  2501. (while (or (and ext-setup-or-nil
  2502. (string-match re ext-setup-or-nil start)
  2503. (setq start (match-end 0)))
  2504. (and (setq ext-setup-or-nil nil start 0)
  2505. (re-search-forward re nil t)))
  2506. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2507. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2508. (cond
  2509. ((equal key "CATEGORY")
  2510. (if (string-match "[ \t]+$" value)
  2511. (setq value (replace-match "" t t value)))
  2512. (setq cat value))
  2513. ((member key '("SEQ_TODO" "TODO"))
  2514. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2515. ((equal key "TYP_TODO")
  2516. (push (cons 'type (org-split-string value splitre)) kwds))
  2517. ((equal key "TAGS")
  2518. (setq tags (append tags (org-split-string value splitre))))
  2519. ((equal key "COLUMNS")
  2520. (org-set-local 'org-columns-default-format value))
  2521. ((equal key "LINK")
  2522. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2523. (push (cons (match-string 1 value)
  2524. (org-trim (match-string 2 value)))
  2525. links)))
  2526. ((equal key "PRIORITIES")
  2527. (setq prio (org-split-string value " +")))
  2528. ((equal key "PROPERTY")
  2529. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2530. (push (cons (match-string 1 value) (match-string 2 value))
  2531. props)))
  2532. ((equal key "FILETAGS")
  2533. (when (string-match "\\S-" value)
  2534. (setq ftags
  2535. (append
  2536. ftags
  2537. (apply 'append
  2538. (mapcar (lambda (x) (org-split-string x ":"))
  2539. (org-split-string value)))))))
  2540. ((equal key "DRAWERS")
  2541. (setq drawers (org-split-string value splitre)))
  2542. ((equal key "CONSTANTS")
  2543. (setq const (append const (org-split-string value splitre))))
  2544. ((equal key "STARTUP")
  2545. (let ((opts (org-split-string value splitre))
  2546. l var val)
  2547. (while (setq l (pop opts))
  2548. (when (setq l (assoc l org-startup-options))
  2549. (setq var (nth 1 l) val (nth 2 l))
  2550. (if (not (nth 3 l))
  2551. (set (make-local-variable var) val)
  2552. (if (not (listp (symbol-value var)))
  2553. (set (make-local-variable var) nil))
  2554. (set (make-local-variable var) (symbol-value var))
  2555. (add-to-list var val))))))
  2556. ((equal key "ARCHIVE")
  2557. (string-match " *$" value)
  2558. (setq arch (replace-match "" t t value))
  2559. (remove-text-properties 0 (length arch)
  2560. '(face t fontified t) arch))
  2561. ((equal key "SETUPFILE")
  2562. (setq setup-contents (org-file-contents
  2563. (expand-file-name
  2564. (org-remove-double-quotes value))
  2565. 'noerror))
  2566. (if (not ext-setup-or-nil)
  2567. (setq ext-setup-or-nil setup-contents start 0)
  2568. (setq ext-setup-or-nil
  2569. (concat (substring ext-setup-or-nil 0 start)
  2570. "\n" setup-contents "\n"
  2571. (substring ext-setup-or-nil start)))))
  2572. ))))
  2573. (when cat
  2574. (org-set-local 'org-category (intern cat))
  2575. (push (cons "CATEGORY" cat) props))
  2576. (when prio
  2577. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2578. (setq prio (mapcar 'string-to-char prio))
  2579. (org-set-local 'org-highest-priority (nth 0 prio))
  2580. (org-set-local 'org-lowest-priority (nth 1 prio))
  2581. (org-set-local 'org-default-priority (nth 2 prio)))
  2582. (and props (org-set-local 'org-file-properties (nreverse props)))
  2583. (and ftags (org-set-local 'org-file-tags ftags))
  2584. (and drawers (org-set-local 'org-drawers drawers))
  2585. (and arch (org-set-local 'org-archive-location arch))
  2586. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2587. ;; Process the TODO keywords
  2588. (unless kwds
  2589. ;; Use the global values as if they had been given locally.
  2590. (setq kwds (default-value 'org-todo-keywords))
  2591. (if (stringp (car kwds))
  2592. (setq kwds (list (cons org-todo-interpretation
  2593. (default-value 'org-todo-keywords)))))
  2594. (setq kwds (reverse kwds)))
  2595. (setq kwds (nreverse kwds))
  2596. (let (inter kws kw)
  2597. (while (setq kws (pop kwds))
  2598. (setq inter (pop kws) sep (member "|" kws)
  2599. kws0 (delete "|" (copy-sequence kws))
  2600. kwsa nil
  2601. kws1 (mapcar
  2602. (lambda (x)
  2603. ;; 1 2
  2604. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2605. (progn
  2606. (setq kw (match-string 1 x)
  2607. key (and (match-end 2) (match-string 2 x))
  2608. log (org-extract-log-state-settings x))
  2609. (push (cons kw (and key (string-to-char key))) kwsa)
  2610. (and log (push log org-todo-log-states))
  2611. kw)
  2612. (error "Invalid TODO keyword %s" x)))
  2613. kws0)
  2614. kwsa (if kwsa (append '((:startgroup))
  2615. (nreverse kwsa)
  2616. '((:endgroup))))
  2617. hw (car kws1)
  2618. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2619. tail (list inter hw (car dws) (org-last dws)))
  2620. (add-to-list 'org-todo-heads hw 'append)
  2621. (push kws1 org-todo-sets)
  2622. (setq org-done-keywords (append org-done-keywords dws nil))
  2623. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2624. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2625. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2626. (setq org-todo-sets (nreverse org-todo-sets)
  2627. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2628. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2629. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2630. ;; Process the constants
  2631. (when const
  2632. (let (e cst)
  2633. (while (setq e (pop const))
  2634. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2635. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2636. (setq org-table-formula-constants-local cst)))
  2637. ;; Process the tags.
  2638. (when tags
  2639. (let (e tgs)
  2640. (while (setq e (pop tags))
  2641. (cond
  2642. ((equal e "{") (push '(:startgroup) tgs))
  2643. ((equal e "}") (push '(:endgroup) tgs))
  2644. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2645. (push (cons (match-string 1 e)
  2646. (string-to-char (match-string 2 e)))
  2647. tgs))
  2648. (t (push (list e) tgs))))
  2649. (org-set-local 'org-tag-alist nil)
  2650. (while (setq e (pop tgs))
  2651. (or (and (stringp (car e))
  2652. (assoc (car e) org-tag-alist))
  2653. (push e org-tag-alist)))))
  2654. ;; Compute the regular expressions and other local variables
  2655. (if (not org-done-keywords)
  2656. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2657. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2658. (length org-scheduled-string)
  2659. (length org-clock-string)
  2660. (length org-closed-string)))
  2661. org-drawer-regexp
  2662. (concat "^[ \t]*:\\("
  2663. (mapconcat 'regexp-quote org-drawers "\\|")
  2664. "\\):[ \t]*$")
  2665. org-not-done-keywords
  2666. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2667. org-todo-regexp
  2668. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2669. "\\|") "\\)\\>")
  2670. org-not-done-regexp
  2671. (concat "\\<\\("
  2672. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2673. "\\)\\>")
  2674. org-todo-line-regexp
  2675. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2676. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2677. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2678. org-complex-heading-regexp
  2679. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2680. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2681. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2682. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2683. org-nl-done-regexp
  2684. (concat "\n\\*+[ \t]+"
  2685. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2686. "\\)" "\\>")
  2687. org-todo-line-tags-regexp
  2688. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2689. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2690. (org-re
  2691. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2692. org-looking-at-done-regexp
  2693. (concat "^" "\\(?:"
  2694. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2695. "\\>")
  2696. org-deadline-regexp (concat "\\<" org-deadline-string)
  2697. org-deadline-time-regexp
  2698. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2699. org-deadline-line-regexp
  2700. (concat "\\<\\(" org-deadline-string "\\).*")
  2701. org-scheduled-regexp
  2702. (concat "\\<" org-scheduled-string)
  2703. org-scheduled-time-regexp
  2704. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2705. org-closed-time-regexp
  2706. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2707. org-keyword-time-regexp
  2708. (concat "\\<\\(" org-scheduled-string
  2709. "\\|" org-deadline-string
  2710. "\\|" org-closed-string
  2711. "\\|" org-clock-string "\\)"
  2712. " *[[<]\\([^]>]+\\)[]>]")
  2713. org-keyword-time-not-clock-regexp
  2714. (concat "\\<\\(" org-scheduled-string
  2715. "\\|" org-deadline-string
  2716. "\\|" org-closed-string
  2717. "\\)"
  2718. " *[[<]\\([^]>]+\\)[]>]")
  2719. org-maybe-keyword-time-regexp
  2720. (concat "\\(\\<\\(" org-scheduled-string
  2721. "\\|" org-deadline-string
  2722. "\\|" org-closed-string
  2723. "\\|" org-clock-string "\\)\\)?"
  2724. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2725. org-planning-or-clock-line-re
  2726. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2727. "\\|" org-deadline-string
  2728. "\\|" org-closed-string "\\|" org-clock-string
  2729. "\\)\\>\\)")
  2730. )
  2731. (org-compute-latex-and-specials-regexp)
  2732. (org-set-font-lock-defaults))))
  2733. (defun org-file-contents (file &optional noerror)
  2734. "Return the contents of FILE, as a string."
  2735. (if (or (not file)
  2736. (not (file-readable-p file)))
  2737. (if noerror
  2738. (progn
  2739. (message "Cannot read file %s" file)
  2740. (ding) (sit-for 2)
  2741. "")
  2742. (error "Cannot read file %s" file))
  2743. (with-temp-buffer
  2744. (insert-file-contents file)
  2745. (buffer-string))))
  2746. (defun org-extract-log-state-settings (x)
  2747. "Extract the log state setting from a TODO keyword string.
  2748. This will extract info from a string like \"WAIT(w@/!)\"."
  2749. (let (kw key log1 log2)
  2750. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2751. (setq kw (match-string 1 x)
  2752. key (and (match-end 2) (match-string 2 x))
  2753. log1 (and (match-end 3) (match-string 3 x))
  2754. log2 (and (match-end 4) (match-string 4 x)))
  2755. (and (or log1 log2)
  2756. (list kw
  2757. (and log1 (if (equal log1 "!") 'time 'note))
  2758. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2759. (defun org-remove-keyword-keys (list)
  2760. "Remove a pair of parenthesis at the end of each string in LIST."
  2761. (mapcar (lambda (x)
  2762. (if (string-match "(.*)$" x)
  2763. (substring x 0 (match-beginning 0))
  2764. x))
  2765. list))
  2766. ;; FIXME: this could be done much better, using second characters etc.
  2767. (defun org-assign-fast-keys (alist)
  2768. "Assign fast keys to a keyword-key alist.
  2769. Respect keys that are already there."
  2770. (let (new e k c c1 c2 (char ?a))
  2771. (while (setq e (pop alist))
  2772. (cond
  2773. ((equal e '(:startgroup)) (push e new))
  2774. ((equal e '(:endgroup)) (push e new))
  2775. (t
  2776. (setq k (car e) c2 nil)
  2777. (if (cdr e)
  2778. (setq c (cdr e))
  2779. ;; automatically assign a character.
  2780. (setq c1 (string-to-char
  2781. (downcase (substring
  2782. k (if (= (string-to-char k) ?@) 1 0)))))
  2783. (if (or (rassoc c1 new) (rassoc c1 alist))
  2784. (while (or (rassoc char new) (rassoc char alist))
  2785. (setq char (1+ char)))
  2786. (setq c2 c1))
  2787. (setq c (or c2 char)))
  2788. (push (cons k c) new))))
  2789. (nreverse new)))
  2790. ;;; Some variables used in various places
  2791. (defvar org-window-configuration nil
  2792. "Used in various places to store a window configuration.")
  2793. (defvar org-finish-function nil
  2794. "Function to be called when `C-c C-c' is used.
  2795. This is for getting out of special buffers like remember.")
  2796. ;; FIXME: Occasionally check by commenting these, to make sure
  2797. ;; no other functions uses these, forgetting to let-bind them.
  2798. (defvar entry)
  2799. (defvar state)
  2800. (defvar last-state)
  2801. (defvar date)
  2802. (defvar description)
  2803. ;; Defined somewhere in this file, but used before definition.
  2804. (defvar org-html-entities)
  2805. (defvar org-struct-menu)
  2806. (defvar org-org-menu)
  2807. (defvar org-tbl-menu)
  2808. (defvar org-agenda-keymap)
  2809. ;;;; Define the Org-mode
  2810. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2811. (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."))
  2812. ;; We use a before-change function to check if a table might need
  2813. ;; an update.
  2814. (defvar org-table-may-need-update t
  2815. "Indicates that a table might need an update.
  2816. This variable is set by `org-before-change-function'.
  2817. `org-table-align' sets it back to nil.")
  2818. (defun org-before-change-function (beg end)
  2819. "Every change indicates that a table might need an update."
  2820. (setq org-table-may-need-update t))
  2821. (defvar org-mode-map)
  2822. (defvar org-mode-hook nil)
  2823. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2824. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2825. (defvar org-table-buffer-is-an nil)
  2826. (defconst org-outline-regexp "\\*+ ")
  2827. ;;;###autoload
  2828. (define-derived-mode org-mode outline-mode "Org"
  2829. "Outline-based notes management and organizer, alias
  2830. \"Carsten's outline-mode for keeping track of everything.\"
  2831. Org-mode develops organizational tasks around a NOTES file which
  2832. contains information about projects as plain text. Org-mode is
  2833. implemented on top of outline-mode, which is ideal to keep the content
  2834. of large files well structured. It supports ToDo items, deadlines and
  2835. time stamps, which magically appear in the diary listing of the Emacs
  2836. calendar. Tables are easily created with a built-in table editor.
  2837. Plain text URL-like links connect to websites, emails (VM), Usenet
  2838. messages (Gnus), BBDB entries, and any files related to the project.
  2839. For printing and sharing of notes, an Org-mode file (or a part of it)
  2840. can be exported as a structured ASCII or HTML file.
  2841. The following commands are available:
  2842. \\{org-mode-map}"
  2843. ;; Get rid of Outline menus, they are not needed
  2844. ;; Need to do this here because define-derived-mode sets up
  2845. ;; the keymap so late. Still, it is a waste to call this each time
  2846. ;; we switch another buffer into org-mode.
  2847. (if (featurep 'xemacs)
  2848. (when (boundp 'outline-mode-menu-heading)
  2849. ;; Assume this is Greg's port, it used easymenu
  2850. (easy-menu-remove outline-mode-menu-heading)
  2851. (easy-menu-remove outline-mode-menu-show)
  2852. (easy-menu-remove outline-mode-menu-hide))
  2853. (define-key org-mode-map [menu-bar headings] 'undefined)
  2854. (define-key org-mode-map [menu-bar hide] 'undefined)
  2855. (define-key org-mode-map [menu-bar show] 'undefined))
  2856. (org-load-modules-maybe)
  2857. (easy-menu-add org-org-menu)
  2858. (easy-menu-add org-tbl-menu)
  2859. (org-install-agenda-files-menu)
  2860. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2861. (org-add-to-invisibility-spec '(org-cwidth))
  2862. (when (featurep 'xemacs)
  2863. (org-set-local 'line-move-ignore-invisible t))
  2864. (org-set-local 'outline-regexp org-outline-regexp)
  2865. (org-set-local 'outline-level 'org-outline-level)
  2866. (when (and org-ellipsis
  2867. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2868. (fboundp 'make-glyph-code))
  2869. (unless org-display-table
  2870. (setq org-display-table (make-display-table)))
  2871. (set-display-table-slot
  2872. org-display-table 4
  2873. (vconcat (mapcar
  2874. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2875. org-ellipsis)))
  2876. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2877. (setq buffer-display-table org-display-table))
  2878. (org-set-regexps-and-options)
  2879. ;; Calc embedded
  2880. (org-set-local 'calc-embedded-open-mode "# ")
  2881. (modify-syntax-entry ?# "<")
  2882. (modify-syntax-entry ?@ "w")
  2883. (if org-startup-truncated (setq truncate-lines t))
  2884. (org-set-local 'font-lock-unfontify-region-function
  2885. 'org-unfontify-region)
  2886. ;; Activate before-change-function
  2887. (org-set-local 'org-table-may-need-update t)
  2888. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2889. 'local)
  2890. ;; Check for running clock before killing a buffer
  2891. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2892. ;; Paragraphs and auto-filling
  2893. (org-set-autofill-regexps)
  2894. (setq indent-line-function 'org-indent-line-function)
  2895. (org-update-radio-target-regexp)
  2896. ;; Comment characters
  2897. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2898. (org-set-local 'comment-padding " ")
  2899. ;; Align options lines
  2900. (org-set-local
  2901. 'align-mode-rules-list
  2902. '((org-in-buffer-settings
  2903. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2904. (modes . '(org-mode)))))
  2905. ;; Imenu
  2906. (org-set-local 'imenu-create-index-function
  2907. 'org-imenu-get-tree)
  2908. ;; Make isearch reveal context
  2909. (if (or (featurep 'xemacs)
  2910. (not (boundp 'outline-isearch-open-invisible-function)))
  2911. ;; Emacs 21 and XEmacs make use of the hook
  2912. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2913. ;; Emacs 22 deals with this through a special variable
  2914. (org-set-local 'outline-isearch-open-invisible-function
  2915. (lambda (&rest ignore) (org-show-context 'isearch))))
  2916. ;; If empty file that did not turn on org-mode automatically, make it to.
  2917. (if (and org-insert-mode-line-in-empty-file
  2918. (interactive-p)
  2919. (= (point-min) (point-max)))
  2920. (insert "# -*- mode: org -*-\n\n"))
  2921. (unless org-inhibit-startup
  2922. (when org-startup-align-all-tables
  2923. (let ((bmp (buffer-modified-p)))
  2924. (org-table-map-tables 'org-table-align)
  2925. (set-buffer-modified-p bmp)))
  2926. (org-set-startup-visibility)))
  2927. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2928. (defun org-current-time ()
  2929. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2930. (if (> (car org-time-stamp-rounding-minutes) 1)
  2931. (let ((r (car org-time-stamp-rounding-minutes))
  2932. (time (decode-time)))
  2933. (apply 'encode-time
  2934. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2935. (nthcdr 2 time))))
  2936. (current-time)))
  2937. ;;;; Font-Lock stuff, including the activators
  2938. (defvar org-mouse-map (make-sparse-keymap))
  2939. (org-defkey org-mouse-map
  2940. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2941. (org-defkey org-mouse-map
  2942. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2943. (when org-mouse-1-follows-link
  2944. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2945. (when org-tab-follows-link
  2946. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2947. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2948. (when org-return-follows-link
  2949. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2950. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2951. (require 'font-lock)
  2952. (defconst org-non-link-chars "]\t\n\r<>")
  2953. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2954. "shell" "elisp"))
  2955. (defvar org-link-types-re nil
  2956. "Matches a link that has a url-like prefix like \"http:\"")
  2957. (defvar org-link-re-with-space nil
  2958. "Matches a link with spaces, optional angular brackets around it.")
  2959. (defvar org-link-re-with-space2 nil
  2960. "Matches a link with spaces, optional angular brackets around it.")
  2961. (defvar org-angle-link-re nil
  2962. "Matches link with angular brackets, spaces are allowed.")
  2963. (defvar org-plain-link-re nil
  2964. "Matches plain link, without spaces.")
  2965. (defvar org-bracket-link-regexp nil
  2966. "Matches a link in double brackets.")
  2967. (defvar org-bracket-link-analytic-regexp nil
  2968. "Regular expression used to analyze links.
  2969. Here is what the match groups contain after a match:
  2970. 1: http:
  2971. 2: http
  2972. 3: path
  2973. 4: [desc]
  2974. 5: desc")
  2975. (defvar org-any-link-re nil
  2976. "Regular expression matching any link.")
  2977. (defun org-make-link-regexps ()
  2978. "Update the link regular expressions.
  2979. This should be called after the variable `org-link-types' has changed."
  2980. (setq org-link-types-re
  2981. (concat
  2982. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2983. org-link-re-with-space
  2984. (concat
  2985. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2986. "\\([^" org-non-link-chars " ]"
  2987. "[^" org-non-link-chars "]*"
  2988. "[^" org-non-link-chars " ]\\)>?")
  2989. org-link-re-with-space2
  2990. (concat
  2991. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2992. "\\([^" org-non-link-chars " ]"
  2993. "[^]\t\n\r]*"
  2994. "[^" org-non-link-chars " ]\\)>?")
  2995. org-angle-link-re
  2996. (concat
  2997. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2998. "\\([^" org-non-link-chars " ]"
  2999. "[^" org-non-link-chars "]*"
  3000. "\\)>")
  3001. org-plain-link-re
  3002. (concat
  3003. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3004. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3005. org-bracket-link-regexp
  3006. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3007. org-bracket-link-analytic-regexp
  3008. (concat
  3009. "\\[\\["
  3010. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3011. "\\([^]]+\\)"
  3012. "\\]"
  3013. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3014. "\\]")
  3015. org-any-link-re
  3016. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3017. org-angle-link-re "\\)\\|\\("
  3018. org-plain-link-re "\\)")))
  3019. (org-make-link-regexps)
  3020. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3021. "Regular expression for fast time stamp matching.")
  3022. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3023. "Regular expression for fast time stamp matching.")
  3024. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3025. "Regular expression matching time strings for analysis.
  3026. This one does not require the space after the date, so it can be used
  3027. on a string that terminates immediately after the date.")
  3028. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3029. "Regular expression matching time strings for analysis.")
  3030. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3031. "Regular expression matching time stamps, with groups.")
  3032. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3033. "Regular expression matching time stamps (also [..]), with groups.")
  3034. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3035. "Regular expression matching a time stamp range.")
  3036. (defconst org-tr-regexp-both
  3037. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3038. "Regular expression matching a time stamp range.")
  3039. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3040. org-ts-regexp "\\)?")
  3041. "Regular expression matching a time stamp or time stamp range.")
  3042. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3043. org-ts-regexp-both "\\)?")
  3044. "Regular expression matching a time stamp or time stamp range.
  3045. The time stamps may be either active or inactive.")
  3046. (defvar org-emph-face nil)
  3047. (defun org-do-emphasis-faces (limit)
  3048. "Run through the buffer and add overlays to links."
  3049. (let (rtn)
  3050. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3051. (if (not (= (char-after (match-beginning 3))
  3052. (char-after (match-beginning 4))))
  3053. (progn
  3054. (setq rtn t)
  3055. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3056. 'face
  3057. (nth 1 (assoc (match-string 3)
  3058. org-emphasis-alist)))
  3059. (add-text-properties (match-beginning 2) (match-end 2)
  3060. '(font-lock-multiline t))
  3061. (when org-hide-emphasis-markers
  3062. (add-text-properties (match-end 4) (match-beginning 5)
  3063. '(invisible org-link))
  3064. (add-text-properties (match-beginning 3) (match-end 3)
  3065. '(invisible org-link)))))
  3066. (backward-char 1))
  3067. rtn))
  3068. (defun org-emphasize (&optional char)
  3069. "Insert or change an emphasis, i.e. a font like bold or italic.
  3070. If there is an active region, change that region to a new emphasis.
  3071. If there is no region, just insert the marker characters and position
  3072. the cursor between them.
  3073. CHAR should be either the marker character, or the first character of the
  3074. HTML tag associated with that emphasis. If CHAR is a space, the means
  3075. to remove the emphasis of the selected region.
  3076. If char is not given (for example in an interactive call) it
  3077. will be prompted for."
  3078. (interactive)
  3079. (let ((eal org-emphasis-alist) e det
  3080. (erc org-emphasis-regexp-components)
  3081. (prompt "")
  3082. (string "") beg end move tag c s)
  3083. (if (org-region-active-p)
  3084. (setq beg (region-beginning) end (region-end)
  3085. string (buffer-substring beg end))
  3086. (setq move t))
  3087. (while (setq e (pop eal))
  3088. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3089. c (aref tag 0))
  3090. (push (cons c (string-to-char (car e))) det)
  3091. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3092. (substring tag 1)))))
  3093. (unless char
  3094. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3095. (setq char (read-char-exclusive)))
  3096. (setq char (or (cdr (assoc char det)) char))
  3097. (if (equal char ?\ )
  3098. (setq s "" move nil)
  3099. (unless (assoc (char-to-string char) org-emphasis-alist)
  3100. (error "No such emphasis marker: \"%c\"" char))
  3101. (setq s (char-to-string char)))
  3102. (while (and (> (length string) 1)
  3103. (equal (substring string 0 1) (substring string -1))
  3104. (assoc (substring string 0 1) org-emphasis-alist))
  3105. (setq string (substring string 1 -1)))
  3106. (setq string (concat s string s))
  3107. (if beg (delete-region beg end))
  3108. (unless (or (bolp)
  3109. (string-match (concat "[" (nth 0 erc) "\n]")
  3110. (char-to-string (char-before (point)))))
  3111. (insert " "))
  3112. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3113. (char-to-string (char-after (point))))
  3114. (insert " ") (backward-char 1))
  3115. (insert string)
  3116. (and move (backward-char 1))))
  3117. (defconst org-nonsticky-props
  3118. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3119. (defun org-activate-plain-links (limit)
  3120. "Run through the buffer and add overlays to links."
  3121. (catch 'exit
  3122. (let (f)
  3123. (while (re-search-forward org-plain-link-re limit t)
  3124. (setq f (get-text-property (match-beginning 0) 'face))
  3125. (if (or (eq f 'org-tag)
  3126. (and (listp f) (memq 'org-tag f)))
  3127. nil
  3128. (add-text-properties (match-beginning 0) (match-end 0)
  3129. (list 'mouse-face 'highlight
  3130. 'rear-nonsticky org-nonsticky-props
  3131. 'keymap org-mouse-map
  3132. ))
  3133. (throw 'exit t))))))
  3134. (defun org-activate-code (limit)
  3135. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3136. (progn
  3137. (remove-text-properties (match-beginning 0) (match-end 0)
  3138. '(display t invisible t intangible t))
  3139. t)))
  3140. (defun org-activate-angle-links (limit)
  3141. "Run through the buffer and add overlays to links."
  3142. (if (re-search-forward org-angle-link-re limit t)
  3143. (progn
  3144. (add-text-properties (match-beginning 0) (match-end 0)
  3145. (list 'mouse-face 'highlight
  3146. 'rear-nonsticky org-nonsticky-props
  3147. 'keymap org-mouse-map
  3148. ))
  3149. t)))
  3150. (defun org-activate-bracket-links (limit)
  3151. "Run through the buffer and add overlays to bracketed links."
  3152. (if (re-search-forward org-bracket-link-regexp limit t)
  3153. (let* ((help (concat "LINK: "
  3154. (org-match-string-no-properties 1)))
  3155. ;; FIXME: above we should remove the escapes.
  3156. ;; but that requires another match, protecting match data,
  3157. ;; a lot of overhead for font-lock.
  3158. (ip (org-maybe-intangible
  3159. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3160. 'keymap org-mouse-map 'mouse-face 'highlight
  3161. 'font-lock-multiline t 'help-echo help)))
  3162. (vp (list 'rear-nonsticky org-nonsticky-props
  3163. 'keymap org-mouse-map 'mouse-face 'highlight
  3164. ' font-lock-multiline t 'help-echo help)))
  3165. ;; We need to remove the invisible property here. Table narrowing
  3166. ;; may have made some of this invisible.
  3167. (remove-text-properties (match-beginning 0) (match-end 0)
  3168. '(invisible nil))
  3169. (if (match-end 3)
  3170. (progn
  3171. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3172. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3173. (add-text-properties (match-end 3) (match-end 0) ip))
  3174. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3175. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3176. (add-text-properties (match-end 1) (match-end 0) ip))
  3177. t)))
  3178. (defun org-activate-dates (limit)
  3179. "Run through the buffer and add overlays to dates."
  3180. (if (re-search-forward org-tsr-regexp-both limit t)
  3181. (progn
  3182. (add-text-properties (match-beginning 0) (match-end 0)
  3183. (list 'mouse-face 'highlight
  3184. 'rear-nonsticky org-nonsticky-props
  3185. 'keymap org-mouse-map))
  3186. (when org-display-custom-times
  3187. (if (match-end 3)
  3188. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3189. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3190. t)))
  3191. (defvar org-target-link-regexp nil
  3192. "Regular expression matching radio targets in plain text.")
  3193. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3194. "Regular expression matching a link target.")
  3195. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3196. "Regular expression matching a radio target.")
  3197. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3198. "Regular expression matching any target.")
  3199. (defun org-activate-target-links (limit)
  3200. "Run through the buffer and add overlays to target matches."
  3201. (when org-target-link-regexp
  3202. (let ((case-fold-search t))
  3203. (if (re-search-forward org-target-link-regexp limit t)
  3204. (progn
  3205. (add-text-properties (match-beginning 0) (match-end 0)
  3206. (list 'mouse-face 'highlight
  3207. 'rear-nonsticky org-nonsticky-props
  3208. 'keymap org-mouse-map
  3209. 'help-echo "Radio target link"
  3210. 'org-linked-text t))
  3211. t)))))
  3212. (defun org-update-radio-target-regexp ()
  3213. "Find all radio targets in this file and update the regular expression."
  3214. (interactive)
  3215. (when (memq 'radio org-activate-links)
  3216. (setq org-target-link-regexp
  3217. (org-make-target-link-regexp (org-all-targets 'radio)))
  3218. (org-restart-font-lock)))
  3219. (defun org-hide-wide-columns (limit)
  3220. (let (s e)
  3221. (setq s (text-property-any (point) (or limit (point-max))
  3222. 'org-cwidth t))
  3223. (when s
  3224. (setq e (next-single-property-change s 'org-cwidth))
  3225. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3226. (goto-char e)
  3227. t)))
  3228. (defvar org-latex-and-specials-regexp nil
  3229. "Regular expression for highlighting export special stuff.")
  3230. (defvar org-match-substring-regexp)
  3231. (defvar org-match-substring-with-braces-regexp)
  3232. (defvar org-export-html-special-string-regexps)
  3233. (defun org-compute-latex-and-specials-regexp ()
  3234. "Compute regular expression for stuff treated specially by exporters."
  3235. (if (not org-highlight-latex-fragments-and-specials)
  3236. (org-set-local 'org-latex-and-specials-regexp nil)
  3237. (require 'org-exp)
  3238. (let*
  3239. ((matchers (plist-get org-format-latex-options :matchers))
  3240. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3241. org-latex-regexps)))
  3242. (options (org-combine-plists (org-default-export-plist)
  3243. (org-infile-export-plist)))
  3244. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3245. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3246. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3247. (org-export-html-expand (plist-get options :expand-quoted-html))
  3248. (org-export-with-special-strings (plist-get options :special-strings))
  3249. (re-sub
  3250. (cond
  3251. ((equal org-export-with-sub-superscripts '{})
  3252. (list org-match-substring-with-braces-regexp))
  3253. (org-export-with-sub-superscripts
  3254. (list org-match-substring-regexp))
  3255. (t nil)))
  3256. (re-latex
  3257. (if org-export-with-LaTeX-fragments
  3258. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3259. (re-macros
  3260. (if org-export-with-TeX-macros
  3261. (list (concat "\\\\"
  3262. (regexp-opt
  3263. (append (mapcar 'car org-html-entities)
  3264. (if (boundp 'org-latex-entities)
  3265. org-latex-entities nil))
  3266. 'words))) ; FIXME
  3267. ))
  3268. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3269. (re-special (if org-export-with-special-strings
  3270. (mapcar (lambda (x) (car x))
  3271. org-export-html-special-string-regexps)))
  3272. (re-rest
  3273. (delq nil
  3274. (list
  3275. (if org-export-html-expand "@<[^>\n]+>")
  3276. ))))
  3277. (org-set-local
  3278. 'org-latex-and-specials-regexp
  3279. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3280. re-rest) "\\|")))))
  3281. (defun org-do-latex-and-special-faces (limit)
  3282. "Run through the buffer and add overlays to links."
  3283. (when org-latex-and-specials-regexp
  3284. (let (rtn d)
  3285. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3286. limit t))
  3287. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3288. 'face))
  3289. '(org-code org-verbatim underline)))
  3290. (progn
  3291. (setq rtn t
  3292. d (cond ((member (char-after (1+ (match-beginning 0)))
  3293. '(?_ ?^)) 1)
  3294. (t 0)))
  3295. (font-lock-prepend-text-property
  3296. (+ d (match-beginning 0)) (match-end 0)
  3297. 'face 'org-latex-and-export-specials)
  3298. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3299. '(font-lock-multiline t)))))
  3300. rtn)))
  3301. (defun org-restart-font-lock ()
  3302. "Restart font-lock-mode, to force refontification."
  3303. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3304. (font-lock-mode -1)
  3305. (font-lock-mode 1)))
  3306. (defun org-all-targets (&optional radio)
  3307. "Return a list of all targets in this file.
  3308. With optional argument RADIO, only find radio targets."
  3309. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3310. rtn)
  3311. (save-excursion
  3312. (goto-char (point-min))
  3313. (while (re-search-forward re nil t)
  3314. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3315. rtn)))
  3316. (defun org-make-target-link-regexp (targets)
  3317. "Make regular expression matching all strings in TARGETS.
  3318. The regular expression finds the targets also if there is a line break
  3319. between words."
  3320. (and targets
  3321. (concat
  3322. "\\<\\("
  3323. (mapconcat
  3324. (lambda (x)
  3325. (while (string-match " +" x)
  3326. (setq x (replace-match "\\s-+" t t x)))
  3327. x)
  3328. targets
  3329. "\\|")
  3330. "\\)\\>")))
  3331. (defun org-activate-tags (limit)
  3332. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3333. (progn
  3334. (add-text-properties (match-beginning 1) (match-end 1)
  3335. (list 'mouse-face 'highlight
  3336. 'rear-nonsticky org-nonsticky-props
  3337. 'keymap org-mouse-map))
  3338. t)))
  3339. (defun org-outline-level ()
  3340. (save-excursion
  3341. (looking-at outline-regexp)
  3342. (if (match-beginning 1)
  3343. (+ (org-get-string-indentation (match-string 1)) 1000)
  3344. (1- (- (match-end 0) (match-beginning 0))))))
  3345. (defvar org-font-lock-keywords nil)
  3346. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3347. "Regular expression matching a property line.")
  3348. (defvar org-font-lock-hook nil
  3349. "Functions to be called for special font lock stuff.")
  3350. (defun org-font-lock-hook (limit)
  3351. (run-hook-with-args 'org-font-lock-hook limit))
  3352. (defun org-set-font-lock-defaults ()
  3353. (let* ((em org-fontify-emphasized-text)
  3354. (lk org-activate-links)
  3355. (org-font-lock-extra-keywords
  3356. (list
  3357. ;; Call the hook
  3358. '(org-font-lock-hook)
  3359. ;; Headlines
  3360. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3361. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3362. ;; Table lines
  3363. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3364. (1 'org-table t))
  3365. ;; Table internals
  3366. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3367. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3368. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3369. ;; Drawers
  3370. (list org-drawer-regexp '(0 'org-special-keyword t))
  3371. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3372. ;; Properties
  3373. (list org-property-re
  3374. '(1 'org-special-keyword t)
  3375. '(3 'org-property-value t))
  3376. (if org-format-transports-properties-p
  3377. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3378. ;; Links
  3379. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3380. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3381. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3382. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3383. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3384. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3385. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3386. '(org-hide-wide-columns (0 nil append))
  3387. ;; TODO lines
  3388. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3389. '(1 (org-get-todo-face 1) t))
  3390. ;; DONE
  3391. (if org-fontify-done-headline
  3392. (list (concat "^[*]+ +\\<\\("
  3393. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3394. "\\)\\(.*\\)")
  3395. '(2 'org-headline-done t))
  3396. nil)
  3397. ;; Priorities
  3398. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3399. ;; Special keywords
  3400. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3401. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3402. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3403. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3404. ;; Emphasis
  3405. (if em
  3406. (if (featurep 'xemacs)
  3407. '(org-do-emphasis-faces (0 nil append))
  3408. '(org-do-emphasis-faces)))
  3409. ;; Checkboxes
  3410. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3411. 2 'bold prepend)
  3412. (if org-provide-checkbox-statistics
  3413. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3414. (0 (org-get-checkbox-statistics-face) t)))
  3415. ;; Description list items
  3416. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3417. 2 'bold prepend)
  3418. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3419. '(1 'org-archived prepend))
  3420. ;; Specials
  3421. '(org-do-latex-and-special-faces)
  3422. ;; Code
  3423. '(org-activate-code (1 'org-code t))
  3424. ;; COMMENT
  3425. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3426. "\\|" org-quote-string "\\)\\>")
  3427. '(1 'org-special-keyword t))
  3428. '("^#.*" (0 'font-lock-comment-face t))
  3429. )))
  3430. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3431. ;; Now set the full font-lock-keywords
  3432. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3433. (org-set-local 'font-lock-defaults
  3434. '(org-font-lock-keywords t nil nil backward-paragraph))
  3435. (kill-local-variable 'font-lock-keywords) nil))
  3436. (defvar org-m nil)
  3437. (defvar org-l nil)
  3438. (defvar org-f nil)
  3439. (defun org-get-level-face (n)
  3440. "Get the right face for match N in font-lock matching of healdines."
  3441. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3442. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3443. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3444. (cond
  3445. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3446. ((eq n 2) org-f)
  3447. (t (if org-level-color-stars-only nil org-f))))
  3448. (defun org-get-todo-face (kwd)
  3449. "Get the right face for a TODO keyword KWD.
  3450. If KWD is a number, get the corresponding match group."
  3451. (if (numberp kwd) (setq kwd (match-string kwd)))
  3452. (or (cdr (assoc kwd org-todo-keyword-faces))
  3453. (and (member kwd org-done-keywords) 'org-done)
  3454. 'org-todo))
  3455. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3456. "Remove fontification and activation overlays from links."
  3457. (font-lock-default-unfontify-region beg end)
  3458. (let* ((buffer-undo-list t)
  3459. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3460. (inhibit-modification-hooks t)
  3461. deactivate-mark buffer-file-name buffer-file-truename)
  3462. (remove-text-properties beg end
  3463. '(mouse-face t keymap t org-linked-text t
  3464. invisible t intangible t))))
  3465. ;;;; Visibility cycling, including org-goto and indirect buffer
  3466. ;;; Cycling
  3467. (defvar org-cycle-global-status nil)
  3468. (make-variable-buffer-local 'org-cycle-global-status)
  3469. (defvar org-cycle-subtree-status nil)
  3470. (make-variable-buffer-local 'org-cycle-subtree-status)
  3471. ;;;###autoload
  3472. (defun org-cycle (&optional arg)
  3473. "Visibility cycling for Org-mode.
  3474. - When this function is called with a prefix argument, rotate the entire
  3475. buffer through 3 states (global cycling)
  3476. 1. OVERVIEW: Show only top-level headlines.
  3477. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3478. 3. SHOW ALL: Show everything.
  3479. When called with two C-c C-u prefixes, switch to the startup visibility,
  3480. determined by the variable `org-startup-folded', and by any VISIBILITY
  3481. properties in the buffer.
  3482. - When point is at the beginning of a headline, rotate the subtree started
  3483. by this line through 3 different states (local cycling)
  3484. 1. FOLDED: Only the main headline is shown.
  3485. 2. CHILDREN: The main headline and the direct children are shown.
  3486. From this state, you can move to one of the children
  3487. and zoom in further.
  3488. 3. SUBTREE: Show the entire subtree, including body text.
  3489. - When there is a numeric prefix, go up to a heading with level ARG, do
  3490. a `show-subtree' and return to the previous cursor position. If ARG
  3491. is negative, go up that many levels.
  3492. - When point is not at the beginning of a headline, execute the global
  3493. binding for TAB, which is re-indenting the line. See the option
  3494. `org-cycle-emulate-tab' for details.
  3495. - Special case: if point is at the beginning of the buffer and there is
  3496. no headline in line 1, this function will act as if called with prefix arg.
  3497. But only if also the variable `org-cycle-global-at-bob' is t."
  3498. (interactive "P")
  3499. (org-load-modules-maybe)
  3500. (let* ((outline-regexp
  3501. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3502. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3503. outline-regexp))
  3504. (bob-special (and org-cycle-global-at-bob (bobp)
  3505. (not (looking-at outline-regexp))))
  3506. (org-cycle-hook
  3507. (if bob-special
  3508. (delq 'org-optimize-window-after-visibility-change
  3509. (copy-sequence org-cycle-hook))
  3510. org-cycle-hook))
  3511. (pos (point)))
  3512. (if (or bob-special (equal arg '(4)))
  3513. ;; special case: use global cycling
  3514. (setq arg t))
  3515. (cond
  3516. ((equal arg '(16))
  3517. (org-set-startup-visibility)
  3518. (message "Startup visibility, plus VISIBILITY properties."))
  3519. ((org-at-table-p 'any)
  3520. ;; Enter the table or move to the next field in the table
  3521. (or (org-table-recognize-table.el)
  3522. (progn
  3523. (if arg (org-table-edit-field t)
  3524. (org-table-justify-field-maybe)
  3525. (call-interactively 'org-table-next-field)))))
  3526. ((eq arg t) ;; Global cycling
  3527. (cond
  3528. ((and (eq last-command this-command)
  3529. (eq org-cycle-global-status 'overview))
  3530. ;; We just created the overview - now do table of contents
  3531. ;; This can be slow in very large buffers, so indicate action
  3532. (message "CONTENTS...")
  3533. (org-content)
  3534. (message "CONTENTS...done")
  3535. (setq org-cycle-global-status 'contents)
  3536. (run-hook-with-args 'org-cycle-hook 'contents))
  3537. ((and (eq last-command this-command)
  3538. (eq org-cycle-global-status 'contents))
  3539. ;; We just showed the table of contents - now show everything
  3540. (show-all)
  3541. (message "SHOW ALL")
  3542. (setq org-cycle-global-status 'all)
  3543. (run-hook-with-args 'org-cycle-hook 'all))
  3544. (t
  3545. ;; Default action: go to overview
  3546. (org-overview)
  3547. (message "OVERVIEW")
  3548. (setq org-cycle-global-status 'overview)
  3549. (run-hook-with-args 'org-cycle-hook 'overview))))
  3550. ((and org-drawers org-drawer-regexp
  3551. (save-excursion
  3552. (beginning-of-line 1)
  3553. (looking-at org-drawer-regexp)))
  3554. ;; Toggle block visibility
  3555. (org-flag-drawer
  3556. (not (get-char-property (match-end 0) 'invisible))))
  3557. ((integerp arg)
  3558. ;; Show-subtree, ARG levels up from here.
  3559. (save-excursion
  3560. (org-back-to-heading)
  3561. (outline-up-heading (if (< arg 0) (- arg)
  3562. (- (funcall outline-level) arg)))
  3563. (org-show-subtree)))
  3564. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3565. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3566. ;; At a heading: rotate between three different views
  3567. (org-back-to-heading)
  3568. (let ((goal-column 0) eoh eol eos)
  3569. ;; First, some boundaries
  3570. (save-excursion
  3571. (org-back-to-heading)
  3572. (save-excursion
  3573. (beginning-of-line 2)
  3574. (while (and (not (eobp)) ;; this is like `next-line'
  3575. (get-char-property (1- (point)) 'invisible))
  3576. (beginning-of-line 2)) (setq eol (point)))
  3577. (outline-end-of-heading) (setq eoh (point))
  3578. (org-end-of-subtree t)
  3579. (unless (eobp)
  3580. (skip-chars-forward " \t\n")
  3581. (beginning-of-line 1) ; in case this is an item
  3582. )
  3583. (setq eos (1- (point))))
  3584. ;; Find out what to do next and set `this-command'
  3585. (cond
  3586. ((= eos eoh)
  3587. ;; Nothing is hidden behind this heading
  3588. (message "EMPTY ENTRY")
  3589. (setq org-cycle-subtree-status nil)
  3590. (save-excursion
  3591. (goto-char eos)
  3592. (outline-next-heading)
  3593. (if (org-invisible-p) (org-flag-heading nil))))
  3594. ((or (>= eol eos)
  3595. (not (string-match "\\S-" (buffer-substring eol eos))))
  3596. ;; Entire subtree is hidden in one line: open it
  3597. (org-show-entry)
  3598. (show-children)
  3599. (message "CHILDREN")
  3600. (save-excursion
  3601. (goto-char eos)
  3602. (outline-next-heading)
  3603. (if (org-invisible-p) (org-flag-heading nil)))
  3604. (setq org-cycle-subtree-status 'children)
  3605. (run-hook-with-args 'org-cycle-hook 'children))
  3606. ((and (eq last-command this-command)
  3607. (eq org-cycle-subtree-status 'children))
  3608. ;; We just showed the children, now show everything.
  3609. (org-show-subtree)
  3610. (message "SUBTREE")
  3611. (setq org-cycle-subtree-status 'subtree)
  3612. (run-hook-with-args 'org-cycle-hook 'subtree))
  3613. (t
  3614. ;; Default action: hide the subtree.
  3615. (hide-subtree)
  3616. (message "FOLDED")
  3617. (setq org-cycle-subtree-status 'folded)
  3618. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3619. ;; TAB emulation and template completion
  3620. (buffer-read-only (org-back-to-heading))
  3621. ((org-try-structure-completion))
  3622. ((org-try-cdlatex-tab))
  3623. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3624. (or (not (bolp))
  3625. (not (looking-at outline-regexp))))
  3626. (call-interactively (global-key-binding "\t")))
  3627. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3628. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3629. (or (and (eq org-cycle-emulate-tab 'white)
  3630. (= (match-end 0) (point-at-eol)))
  3631. (and (eq org-cycle-emulate-tab 'whitestart)
  3632. (>= (match-end 0) pos))))
  3633. t
  3634. (eq org-cycle-emulate-tab t))
  3635. (call-interactively (global-key-binding "\t")))
  3636. (t (save-excursion
  3637. (org-back-to-heading)
  3638. (org-cycle))))))
  3639. ;;;###autoload
  3640. (defun org-global-cycle (&optional arg)
  3641. "Cycle the global visibility. For details see `org-cycle'.
  3642. With C-u prefix arg, switch to startup visibility.
  3643. With a numeric prefix, show all headlines up to that level."
  3644. (interactive "P")
  3645. (let ((org-cycle-include-plain-lists
  3646. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3647. (cond
  3648. ((integerp arg)
  3649. (show-all)
  3650. (hide-sublevels arg)
  3651. (setq org-cycle-global-status 'contents))
  3652. ((equal arg '(4))
  3653. (org-set-startup-visibility)
  3654. (message "Startup visibility, plus VISIBILITY properties."))
  3655. (t
  3656. (org-cycle '(4))))))
  3657. (defun org-set-startup-visibility ()
  3658. "Set the visibility required by startup options and properties."
  3659. (cond
  3660. ((eq org-startup-folded t)
  3661. (org-cycle '(4)))
  3662. ((eq org-startup-folded 'content)
  3663. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3664. (org-cycle '(4)) (org-cycle '(4)))))
  3665. (org-set-visibility-according-to-property 'no-cleanup)
  3666. (org-cycle-hide-archived-subtrees 'all)
  3667. (org-cycle-hide-drawers 'all)
  3668. (org-cycle-show-empty-lines 'all))
  3669. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3670. "Switch subtree visibilities according to :VISIBILITY: property."
  3671. (interactive)
  3672. (let (state)
  3673. (save-excursion
  3674. (goto-char (point-min))
  3675. (while (re-search-forward
  3676. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3677. nil t)
  3678. (setq state (match-string 1))
  3679. (save-excursion
  3680. (org-back-to-heading t)
  3681. (hide-subtree)
  3682. (org-reveal)
  3683. (cond
  3684. ((equal state '("fold" "folded"))
  3685. (hide-subtree))
  3686. ((equal state "children")
  3687. (org-show-hidden-entry)
  3688. (show-children))
  3689. ((equal state "content")
  3690. (save-excursion
  3691. (save-restriction
  3692. (org-narrow-to-subtree)
  3693. (org-content))))
  3694. ((member state '("all" "showall"))
  3695. (show-subtree)))))
  3696. (unless no-cleanup
  3697. (org-cycle-hide-archived-subtrees 'all)
  3698. (org-cycle-hide-drawers 'all)
  3699. (org-cycle-show-empty-lines 'all)))))
  3700. (defun org-overview ()
  3701. "Switch to overview mode, shoing only top-level headlines.
  3702. Really, this shows all headlines with level equal or greater than the level
  3703. of the first headline in the buffer. This is important, because if the
  3704. first headline is not level one, then (hide-sublevels 1) gives confusing
  3705. results."
  3706. (interactive)
  3707. (let ((level (save-excursion
  3708. (goto-char (point-min))
  3709. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3710. (progn
  3711. (goto-char (match-beginning 0))
  3712. (funcall outline-level))))))
  3713. (and level (hide-sublevels level))))
  3714. (defun org-content (&optional arg)
  3715. "Show all headlines in the buffer, like a table of contents.
  3716. With numerical argument N, show content up to level N."
  3717. (interactive "P")
  3718. (save-excursion
  3719. ;; Visit all headings and show their offspring
  3720. (and (integerp arg) (org-overview))
  3721. (goto-char (point-max))
  3722. (catch 'exit
  3723. (while (and (progn (condition-case nil
  3724. (outline-previous-visible-heading 1)
  3725. (error (goto-char (point-min))))
  3726. t)
  3727. (looking-at outline-regexp))
  3728. (if (integerp arg)
  3729. (show-children (1- arg))
  3730. (show-branches))
  3731. (if (bobp) (throw 'exit nil))))))
  3732. (defun org-optimize-window-after-visibility-change (state)
  3733. "Adjust the window after a change in outline visibility.
  3734. This function is the default value of the hook `org-cycle-hook'."
  3735. (when (get-buffer-window (current-buffer))
  3736. (cond
  3737. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3738. ; ((eq state 'overview) (org-beginning-of-line))
  3739. ((eq state 'content) nil)
  3740. ((eq state 'all) nil)
  3741. ((eq state 'folded) nil)
  3742. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3743. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3744. (defun org-compact-display-after-subtree-move ()
  3745. (let (beg end)
  3746. (save-excursion
  3747. (if (org-up-heading-safe)
  3748. (progn
  3749. (hide-subtree)
  3750. (show-entry)
  3751. (show-children)
  3752. (org-cycle-show-empty-lines 'children)
  3753. (org-cycle-hide-drawers 'children))
  3754. (org-overview)))))
  3755. (defun org-cycle-show-empty-lines (state)
  3756. "Show empty lines above all visible headlines.
  3757. The region to be covered depends on STATE when called through
  3758. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3759. entire buffer covered. Note that an empty line is only shown if there
  3760. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3761. (when (> org-cycle-separator-lines 0)
  3762. (save-excursion
  3763. (let* ((n org-cycle-separator-lines)
  3764. (re (cond
  3765. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3766. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3767. (t (let ((ns (number-to-string (- n 2))))
  3768. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3769. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3770. beg end)
  3771. (cond
  3772. ((memq state '(overview contents t))
  3773. (setq beg (point-min) end (point-max)))
  3774. ((memq state '(children folded))
  3775. (setq beg (point) end (progn (org-end-of-subtree t t)
  3776. (beginning-of-line 2)
  3777. (point)))))
  3778. (when beg
  3779. (goto-char beg)
  3780. (while (re-search-forward re end t)
  3781. (if (not (get-char-property (match-end 1) 'invisible))
  3782. (outline-flag-region
  3783. (match-beginning 1) (match-end 1) nil)))))))
  3784. ;; Never hide empty lines at the end of the file.
  3785. (save-excursion
  3786. (goto-char (point-max))
  3787. (outline-previous-heading)
  3788. (outline-end-of-heading)
  3789. (if (and (looking-at "[ \t\n]+")
  3790. (= (match-end 0) (point-max)))
  3791. (outline-flag-region (point) (match-end 0) nil))))
  3792. (defun org-show-empty-lines-in-parent ()
  3793. "Move to the parent and re-show empty lines before visible headlines."
  3794. (save-excursion
  3795. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3796. (org-cycle-show-empty-lines context))))
  3797. (defun org-cycle-hide-drawers (state)
  3798. "Re-hide all drawers after a visibility state change."
  3799. (when (and (org-mode-p)
  3800. (not (memq state '(overview folded))))
  3801. (save-excursion
  3802. (let* ((globalp (memq state '(contents all)))
  3803. (beg (if globalp (point-min) (point)))
  3804. (end (if globalp (point-max) (org-end-of-subtree t))))
  3805. (goto-char beg)
  3806. (while (re-search-forward org-drawer-regexp end t)
  3807. (org-flag-drawer t))))))
  3808. (defun org-flag-drawer (flag)
  3809. (save-excursion
  3810. (beginning-of-line 1)
  3811. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3812. (let ((b (match-end 0))
  3813. (outline-regexp org-outline-regexp))
  3814. (if (re-search-forward
  3815. "^[ \t]*:END:"
  3816. (save-excursion (outline-next-heading) (point)) t)
  3817. (outline-flag-region b (point-at-eol) flag)
  3818. (error ":END: line missing"))))))
  3819. (defun org-subtree-end-visible-p ()
  3820. "Is the end of the current subtree visible?"
  3821. (pos-visible-in-window-p
  3822. (save-excursion (org-end-of-subtree t) (point))))
  3823. (defun org-first-headline-recenter (&optional N)
  3824. "Move cursor to the first headline and recenter the headline.
  3825. Optional argument N means, put the headline into the Nth line of the window."
  3826. (goto-char (point-min))
  3827. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3828. (beginning-of-line)
  3829. (recenter (prefix-numeric-value N))))
  3830. ;;; Org-goto
  3831. (defvar org-goto-window-configuration nil)
  3832. (defvar org-goto-marker nil)
  3833. (defvar org-goto-map
  3834. (let ((map (make-sparse-keymap)))
  3835. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3836. (while (setq cmd (pop cmds))
  3837. (substitute-key-definition cmd cmd map global-map)))
  3838. (suppress-keymap map)
  3839. (org-defkey map "\C-m" 'org-goto-ret)
  3840. (org-defkey map [(return)] 'org-goto-ret)
  3841. (org-defkey map [(left)] 'org-goto-left)
  3842. (org-defkey map [(right)] 'org-goto-right)
  3843. (org-defkey map [(control ?g)] 'org-goto-quit)
  3844. (org-defkey map "\C-i" 'org-cycle)
  3845. (org-defkey map [(tab)] 'org-cycle)
  3846. (org-defkey map [(down)] 'outline-next-visible-heading)
  3847. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3848. (if org-goto-auto-isearch
  3849. (if (fboundp 'define-key-after)
  3850. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3851. nil)
  3852. (org-defkey map "q" 'org-goto-quit)
  3853. (org-defkey map "n" 'outline-next-visible-heading)
  3854. (org-defkey map "p" 'outline-previous-visible-heading)
  3855. (org-defkey map "f" 'outline-forward-same-level)
  3856. (org-defkey map "b" 'outline-backward-same-level)
  3857. (org-defkey map "u" 'outline-up-heading))
  3858. (org-defkey map "/" 'org-occur)
  3859. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3860. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3861. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3862. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3863. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3864. map))
  3865. (defconst org-goto-help
  3866. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3867. RET=jump to location [Q]uit and return to previous location
  3868. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3869. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3870. ;; FIXME: Docstring doe not mention both interfaces
  3871. (defun org-goto (&optional alternative-interface)
  3872. "Look up a different location in the current file, keeping current visibility.
  3873. When you want look-up or go to a different location in a document, the
  3874. fastest way is often to fold the entire buffer and then dive into the tree.
  3875. This method has the disadvantage, that the previous location will be folded,
  3876. which may not be what you want.
  3877. This command works around this by showing a copy of the current buffer
  3878. in an indirect buffer, in overview mode. You can dive into the tree in
  3879. that copy, use org-occur and incremental search to find a location.
  3880. When pressing RET or `Q', the command returns to the original buffer in
  3881. which the visibility is still unchanged. After RET is will also jump to
  3882. the location selected in the indirect buffer and expose the
  3883. the headline hierarchy above."
  3884. (interactive "P")
  3885. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3886. (org-refile-use-outline-path t)
  3887. (interface
  3888. (if (not alternative-interface)
  3889. org-goto-interface
  3890. (if (eq org-goto-interface 'outline)
  3891. 'outline-path-completion
  3892. 'outline)))
  3893. (org-goto-start-pos (point))
  3894. (selected-point
  3895. (if (eq interface 'outline)
  3896. (car (org-get-location (current-buffer) org-goto-help))
  3897. (nth 3 (org-refile-get-location "Goto: ")))))
  3898. (if selected-point
  3899. (progn
  3900. (org-mark-ring-push org-goto-start-pos)
  3901. (goto-char selected-point)
  3902. (if (or (org-invisible-p) (org-invisible-p2))
  3903. (org-show-context 'org-goto)))
  3904. (message "Quit"))))
  3905. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3906. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3907. (defvar org-goto-local-auto-isearch-map) ; defined below
  3908. (defun org-get-location (buf help)
  3909. "Let the user select a location in the Org-mode buffer BUF.
  3910. This function uses a recursive edit. It returns the selected position
  3911. or nil."
  3912. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3913. (isearch-hide-immediately nil)
  3914. (isearch-search-fun-function
  3915. (lambda () 'org-goto-local-search-headings))
  3916. (org-goto-selected-point org-goto-exit-command))
  3917. (save-excursion
  3918. (save-window-excursion
  3919. (delete-other-windows)
  3920. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3921. (switch-to-buffer
  3922. (condition-case nil
  3923. (make-indirect-buffer (current-buffer) "*org-goto*")
  3924. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3925. (with-output-to-temp-buffer "*Help*"
  3926. (princ help))
  3927. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3928. (setq buffer-read-only nil)
  3929. (let ((org-startup-truncated t)
  3930. (org-startup-folded nil)
  3931. (org-startup-align-all-tables nil))
  3932. (org-mode)
  3933. (org-overview))
  3934. (setq buffer-read-only t)
  3935. (if (and (boundp 'org-goto-start-pos)
  3936. (integer-or-marker-p org-goto-start-pos))
  3937. (let ((org-show-hierarchy-above t)
  3938. (org-show-siblings t)
  3939. (org-show-following-heading t))
  3940. (goto-char org-goto-start-pos)
  3941. (and (org-invisible-p) (org-show-context)))
  3942. (goto-char (point-min)))
  3943. (org-beginning-of-line)
  3944. (message "Select location and press RET")
  3945. (use-local-map org-goto-map)
  3946. (recursive-edit)
  3947. ))
  3948. (kill-buffer "*org-goto*")
  3949. (cons org-goto-selected-point org-goto-exit-command)))
  3950. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3951. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3952. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3953. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3954. (defun org-goto-local-search-headings (string bound noerror)
  3955. "Search and make sure that any matches are in headlines."
  3956. (catch 'return
  3957. (while (if isearch-forward
  3958. (search-forward string bound noerror)
  3959. (search-backward string bound noerror))
  3960. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3961. (and (member :headline context)
  3962. (not (member :tags context))))
  3963. (throw 'return (point))))))
  3964. (defun org-goto-local-auto-isearch ()
  3965. "Start isearch."
  3966. (interactive)
  3967. (goto-char (point-min))
  3968. (let ((keys (this-command-keys)))
  3969. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3970. (isearch-mode t)
  3971. (isearch-process-search-char (string-to-char keys)))))
  3972. (defun org-goto-ret (&optional arg)
  3973. "Finish `org-goto' by going to the new location."
  3974. (interactive "P")
  3975. (setq org-goto-selected-point (point)
  3976. org-goto-exit-command 'return)
  3977. (throw 'exit nil))
  3978. (defun org-goto-left ()
  3979. "Finish `org-goto' by going to the new location."
  3980. (interactive)
  3981. (if (org-on-heading-p)
  3982. (progn
  3983. (beginning-of-line 1)
  3984. (setq org-goto-selected-point (point)
  3985. org-goto-exit-command 'left)
  3986. (throw 'exit nil))
  3987. (error "Not on a heading")))
  3988. (defun org-goto-right ()
  3989. "Finish `org-goto' by going to the new location."
  3990. (interactive)
  3991. (if (org-on-heading-p)
  3992. (progn
  3993. (setq org-goto-selected-point (point)
  3994. org-goto-exit-command 'right)
  3995. (throw 'exit nil))
  3996. (error "Not on a heading")))
  3997. (defun org-goto-quit ()
  3998. "Finish `org-goto' without cursor motion."
  3999. (interactive)
  4000. (setq org-goto-selected-point nil)
  4001. (setq org-goto-exit-command 'quit)
  4002. (throw 'exit nil))
  4003. ;;; Indirect buffer display of subtrees
  4004. (defvar org-indirect-dedicated-frame nil
  4005. "This is the frame being used for indirect tree display.")
  4006. (defvar org-last-indirect-buffer nil)
  4007. (defun org-tree-to-indirect-buffer (&optional arg)
  4008. "Create indirect buffer and narrow it to current subtree.
  4009. With numerical prefix ARG, go up to this level and then take that tree.
  4010. If ARG is negative, go up that many levels.
  4011. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4012. indirect buffer previously made with this command, to avoid proliferation of
  4013. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4014. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4015. is kept so that you can work with several indirect buffers at the same time.
  4016. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4017. requests that a new frame be made for the new buffer, so that the dedicated
  4018. frame is not changed."
  4019. (interactive "P")
  4020. (let ((cbuf (current-buffer))
  4021. (cwin (selected-window))
  4022. (pos (point))
  4023. beg end level heading ibuf)
  4024. (save-excursion
  4025. (org-back-to-heading t)
  4026. (when (numberp arg)
  4027. (setq level (org-outline-level))
  4028. (if (< arg 0) (setq arg (+ level arg)))
  4029. (while (> (setq level (org-outline-level)) arg)
  4030. (outline-up-heading 1 t)))
  4031. (setq beg (point)
  4032. heading (org-get-heading))
  4033. (org-end-of-subtree t) (setq end (point)))
  4034. (if (and (buffer-live-p org-last-indirect-buffer)
  4035. (not (eq org-indirect-buffer-display 'new-frame))
  4036. (not arg))
  4037. (kill-buffer org-last-indirect-buffer))
  4038. (setq ibuf (org-get-indirect-buffer cbuf)
  4039. org-last-indirect-buffer ibuf)
  4040. (cond
  4041. ((or (eq org-indirect-buffer-display 'new-frame)
  4042. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4043. (select-frame (make-frame))
  4044. (delete-other-windows)
  4045. (switch-to-buffer ibuf)
  4046. (org-set-frame-title heading))
  4047. ((eq org-indirect-buffer-display 'dedicated-frame)
  4048. (raise-frame
  4049. (select-frame (or (and org-indirect-dedicated-frame
  4050. (frame-live-p org-indirect-dedicated-frame)
  4051. org-indirect-dedicated-frame)
  4052. (setq org-indirect-dedicated-frame (make-frame)))))
  4053. (delete-other-windows)
  4054. (switch-to-buffer ibuf)
  4055. (org-set-frame-title (concat "Indirect: " heading)))
  4056. ((eq org-indirect-buffer-display 'current-window)
  4057. (switch-to-buffer ibuf))
  4058. ((eq org-indirect-buffer-display 'other-window)
  4059. (pop-to-buffer ibuf))
  4060. (t (error "Invalid value.")))
  4061. (if (featurep 'xemacs)
  4062. (save-excursion (org-mode) (turn-on-font-lock)))
  4063. (narrow-to-region beg end)
  4064. (show-all)
  4065. (goto-char pos)
  4066. (and (window-live-p cwin) (select-window cwin))))
  4067. (defun org-get-indirect-buffer (&optional buffer)
  4068. (setq buffer (or buffer (current-buffer)))
  4069. (let ((n 1) (base (buffer-name buffer)) bname)
  4070. (while (buffer-live-p
  4071. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4072. (setq n (1+ n)))
  4073. (condition-case nil
  4074. (make-indirect-buffer buffer bname 'clone)
  4075. (error (make-indirect-buffer buffer bname)))))
  4076. (defun org-set-frame-title (title)
  4077. "Set the title of the current frame to the string TITLE."
  4078. ;; FIXME: how to name a single frame in XEmacs???
  4079. (unless (featurep 'xemacs)
  4080. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4081. ;;;; Structure editing
  4082. ;;; Inserting headlines
  4083. (defun org-insert-heading (&optional force-heading)
  4084. "Insert a new heading or item with same depth at point.
  4085. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4086. If point is at the beginning of a headline, insert a sibling before the
  4087. current headline. If point is not at the beginning, do not split the line,
  4088. but create the new hedline after the current line."
  4089. (interactive "P")
  4090. (if (= (buffer-size) 0)
  4091. (insert "\n* ")
  4092. (when (or force-heading (not (org-insert-item)))
  4093. (let* ((head (save-excursion
  4094. (condition-case nil
  4095. (progn
  4096. (org-back-to-heading)
  4097. (match-string 0))
  4098. (error "*"))))
  4099. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4100. pos)
  4101. (cond
  4102. ((and (org-on-heading-p) (bolp)
  4103. (or (bobp)
  4104. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4105. ;; insert before the current line
  4106. (open-line (if blank 2 1)))
  4107. ((and (bolp)
  4108. (or (bobp)
  4109. (save-excursion
  4110. (backward-char 1) (not (org-invisible-p)))))
  4111. ;; insert right here
  4112. nil)
  4113. (t
  4114. ;; in the middle of the line
  4115. (org-show-entry)
  4116. (let ((split
  4117. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4118. tags pos)
  4119. (if (org-on-heading-p)
  4120. (progn
  4121. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4122. (setq tags (and (match-end 2) (match-string 2)))
  4123. (and (match-end 1)
  4124. (delete-region (match-beginning 1) (match-end 1)))
  4125. (setq pos (point-at-bol))
  4126. (or split (end-of-line 1))
  4127. (delete-horizontal-space)
  4128. (newline (if blank 2 1))
  4129. (when tags
  4130. (save-excursion
  4131. (goto-char pos)
  4132. (end-of-line 1)
  4133. (insert " " tags)
  4134. (org-set-tags nil 'align))))
  4135. (or split (end-of-line 1))
  4136. (newline (if blank 2 1))))))
  4137. (insert head) (just-one-space)
  4138. (setq pos (point))
  4139. (end-of-line 1)
  4140. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4141. (run-hooks 'org-insert-heading-hook)))))
  4142. (defun org-get-heading (&optional no-tags)
  4143. "Return the heading of the current entry, without the stars."
  4144. (save-excursion
  4145. (org-back-to-heading t)
  4146. (if (looking-at
  4147. (if no-tags
  4148. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4149. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4150. (match-string 1) "")))
  4151. (defun org-insert-heading-after-current ()
  4152. "Insert a new heading with same level as current, after current subtree."
  4153. (interactive)
  4154. (org-back-to-heading)
  4155. (org-insert-heading)
  4156. (org-move-subtree-down)
  4157. (end-of-line 1))
  4158. (defun org-insert-todo-heading (arg)
  4159. "Insert a new heading with the same level and TODO state as current heading.
  4160. If the heading has no TODO state, or if the state is DONE, use the first
  4161. state (TODO by default). Also with prefix arg, force first state."
  4162. (interactive "P")
  4163. (when (not (org-insert-item 'checkbox))
  4164. (org-insert-heading)
  4165. (save-excursion
  4166. (org-back-to-heading)
  4167. (outline-previous-heading)
  4168. (looking-at org-todo-line-regexp))
  4169. (if (or arg
  4170. (not (match-beginning 2))
  4171. (member (match-string 2) org-done-keywords))
  4172. (insert (car org-todo-keywords-1) " ")
  4173. (insert (match-string 2) " "))
  4174. (when org-provide-todo-statistics
  4175. (org-update-parent-todo-statistics))))
  4176. (defun org-insert-subheading (arg)
  4177. "Insert a new subheading and demote it.
  4178. Works for outline headings and for plain lists alike."
  4179. (interactive "P")
  4180. (org-insert-heading arg)
  4181. (cond
  4182. ((org-on-heading-p) (org-do-demote))
  4183. ((org-at-item-p) (org-indent-item 1))))
  4184. (defun org-insert-todo-subheading (arg)
  4185. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4186. Works for outline headings and for plain lists alike."
  4187. (interactive "P")
  4188. (org-insert-todo-heading arg)
  4189. (cond
  4190. ((org-on-heading-p) (org-do-demote))
  4191. ((org-at-item-p) (org-indent-item 1))))
  4192. ;;; Promotion and Demotion
  4193. (defun org-promote-subtree ()
  4194. "Promote the entire subtree.
  4195. See also `org-promote'."
  4196. (interactive)
  4197. (save-excursion
  4198. (org-map-tree 'org-promote))
  4199. (org-fix-position-after-promote))
  4200. (defun org-demote-subtree ()
  4201. "Demote the entire subtree. See `org-demote'.
  4202. See also `org-promote'."
  4203. (interactive)
  4204. (save-excursion
  4205. (org-map-tree 'org-demote))
  4206. (org-fix-position-after-promote))
  4207. (defun org-do-promote ()
  4208. "Promote the current heading higher up the tree.
  4209. If the region is active in `transient-mark-mode', promote all headings
  4210. in the region."
  4211. (interactive)
  4212. (save-excursion
  4213. (if (org-region-active-p)
  4214. (org-map-region 'org-promote (region-beginning) (region-end))
  4215. (org-promote)))
  4216. (org-fix-position-after-promote))
  4217. (defun org-do-demote ()
  4218. "Demote the current heading lower down the tree.
  4219. If the region is active in `transient-mark-mode', demote all headings
  4220. in the region."
  4221. (interactive)
  4222. (save-excursion
  4223. (if (org-region-active-p)
  4224. (org-map-region 'org-demote (region-beginning) (region-end))
  4225. (org-demote)))
  4226. (org-fix-position-after-promote))
  4227. (defun org-fix-position-after-promote ()
  4228. "Make sure that after pro/demotion cursor position is right."
  4229. (let ((pos (point)))
  4230. (when (save-excursion
  4231. (beginning-of-line 1)
  4232. (looking-at org-todo-line-regexp)
  4233. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4234. (cond ((eobp) (insert " "))
  4235. ((eolp) (insert " "))
  4236. ((equal (char-after) ?\ ) (forward-char 1))))))
  4237. (defun org-reduced-level (l)
  4238. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4239. (defun org-get-valid-level (level &optional change)
  4240. "Rectify a level change under the influence of `org-odd-levels-only'
  4241. LEVEL is a current level, CHANGE is by how much the level should be
  4242. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4243. even level numbers will become the next higher odd number."
  4244. (if org-odd-levels-only
  4245. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4246. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4247. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4248. (max 1 (+ level change))))
  4249. (if (boundp 'define-obsolete-function-alias)
  4250. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4251. (define-obsolete-function-alias 'org-get-legal-level
  4252. 'org-get-valid-level)
  4253. (define-obsolete-function-alias 'org-get-legal-level
  4254. 'org-get-valid-level "23.1")))
  4255. (defun org-promote ()
  4256. "Promote the current heading higher up the tree.
  4257. If the region is active in `transient-mark-mode', promote all headings
  4258. in the region."
  4259. (org-back-to-heading t)
  4260. (let* ((level (save-match-data (funcall outline-level)))
  4261. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4262. (diff (abs (- level (length up-head) -1))))
  4263. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4264. (replace-match up-head nil t)
  4265. ;; Fixup tag positioning
  4266. (and org-auto-align-tags (org-set-tags nil t))
  4267. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4268. (defun org-demote ()
  4269. "Demote the current heading lower down the tree.
  4270. If the region is active in `transient-mark-mode', demote all headings
  4271. in the region."
  4272. (org-back-to-heading t)
  4273. (let* ((level (save-match-data (funcall outline-level)))
  4274. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4275. (diff (abs (- level (length down-head) -1))))
  4276. (replace-match down-head nil t)
  4277. ;; Fixup tag positioning
  4278. (and org-auto-align-tags (org-set-tags nil t))
  4279. (if org-adapt-indentation (org-fixup-indentation diff))))
  4280. (defun org-map-tree (fun)
  4281. "Call FUN for every heading underneath the current one."
  4282. (org-back-to-heading)
  4283. (let ((level (funcall outline-level)))
  4284. (save-excursion
  4285. (funcall fun)
  4286. (while (and (progn
  4287. (outline-next-heading)
  4288. (> (funcall outline-level) level))
  4289. (not (eobp)))
  4290. (funcall fun)))))
  4291. (defun org-map-region (fun beg end)
  4292. "Call FUN for every heading between BEG and END."
  4293. (let ((org-ignore-region t))
  4294. (save-excursion
  4295. (setq end (copy-marker end))
  4296. (goto-char beg)
  4297. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4298. (< (point) end))
  4299. (funcall fun))
  4300. (while (and (progn
  4301. (outline-next-heading)
  4302. (< (point) end))
  4303. (not (eobp)))
  4304. (funcall fun)))))
  4305. (defun org-fixup-indentation (diff)
  4306. "Change the indentation in the current entry by DIFF
  4307. However, if any line in the current entry has no indentation, or if it
  4308. would end up with no indentation after the change, nothing at all is done."
  4309. (save-excursion
  4310. (let ((end (save-excursion (outline-next-heading)
  4311. (point-marker)))
  4312. (prohibit (if (> diff 0)
  4313. "^\\S-"
  4314. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4315. col)
  4316. (unless (save-excursion (end-of-line 1)
  4317. (re-search-forward prohibit end t))
  4318. (while (and (< (point) end)
  4319. (re-search-forward "^[ \t]+" end t))
  4320. (goto-char (match-end 0))
  4321. (setq col (current-column))
  4322. (if (< diff 0) (replace-match ""))
  4323. (indent-to (+ diff col))))
  4324. (move-marker end nil))))
  4325. (defun org-convert-to-odd-levels ()
  4326. "Convert an org-mode file with all levels allowed to one with odd levels.
  4327. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4328. level 5 etc."
  4329. (interactive)
  4330. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4331. (let ((org-odd-levels-only nil) n)
  4332. (save-excursion
  4333. (goto-char (point-min))
  4334. (while (re-search-forward "^\\*\\*+ " nil t)
  4335. (setq n (- (length (match-string 0)) 2))
  4336. (while (>= (setq n (1- n)) 0)
  4337. (org-demote))
  4338. (end-of-line 1))))))
  4339. (defun org-convert-to-oddeven-levels ()
  4340. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4341. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4342. section with an even level, conversion would destroy the structure of the file. An error
  4343. is signaled in this case."
  4344. (interactive)
  4345. (goto-char (point-min))
  4346. ;; First check if there are no even levels
  4347. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4348. (org-show-context t)
  4349. (error "Not all levels are odd in this file. Conversion not possible."))
  4350. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4351. (let ((org-odd-levels-only nil) n)
  4352. (save-excursion
  4353. (goto-char (point-min))
  4354. (while (re-search-forward "^\\*\\*+ " nil t)
  4355. (setq n (/ (1- (length (match-string 0))) 2))
  4356. (while (>= (setq n (1- n)) 0)
  4357. (org-promote))
  4358. (end-of-line 1))))))
  4359. (defun org-tr-level (n)
  4360. "Make N odd if required."
  4361. (if org-odd-levels-only (1+ (/ n 2)) n))
  4362. ;;; Vertical tree motion, cutting and pasting of subtrees
  4363. (defun org-move-subtree-up (&optional arg)
  4364. "Move the current subtree up past ARG headlines of the same level."
  4365. (interactive "p")
  4366. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4367. (defun org-move-subtree-down (&optional arg)
  4368. "Move the current subtree down past ARG headlines of the same level."
  4369. (interactive "p")
  4370. (setq arg (prefix-numeric-value arg))
  4371. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4372. 'outline-get-last-sibling))
  4373. (ins-point (make-marker))
  4374. (cnt (abs arg))
  4375. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4376. ;; Select the tree
  4377. (org-back-to-heading)
  4378. (setq beg0 (point))
  4379. (save-excursion
  4380. (setq ne-beg (org-back-over-empty-lines))
  4381. (setq beg (point)))
  4382. (save-match-data
  4383. (save-excursion (outline-end-of-heading)
  4384. (setq folded (org-invisible-p)))
  4385. (outline-end-of-subtree))
  4386. (outline-next-heading)
  4387. (setq ne-end (org-back-over-empty-lines))
  4388. (setq end (point))
  4389. (goto-char beg0)
  4390. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4391. ;; include less whitespace
  4392. (save-excursion
  4393. (goto-char beg)
  4394. (forward-line (- ne-beg ne-end))
  4395. (setq beg (point))))
  4396. ;; Find insertion point, with error handling
  4397. (while (> cnt 0)
  4398. (or (and (funcall movfunc) (looking-at outline-regexp))
  4399. (progn (goto-char beg0)
  4400. (error "Cannot move past superior level or buffer limit")))
  4401. (setq cnt (1- cnt)))
  4402. (if (> arg 0)
  4403. ;; Moving forward - still need to move over subtree
  4404. (progn (org-end-of-subtree t t)
  4405. (save-excursion
  4406. (org-back-over-empty-lines)
  4407. (or (bolp) (newline)))))
  4408. (setq ne-ins (org-back-over-empty-lines))
  4409. (move-marker ins-point (point))
  4410. (setq txt (buffer-substring beg end))
  4411. (org-save-markers-in-region beg end)
  4412. (delete-region beg end)
  4413. (outline-flag-region (1- beg) beg nil)
  4414. (outline-flag-region (1- (point)) (point) nil)
  4415. (let ((bbb (point)))
  4416. (insert-before-markers txt)
  4417. (org-reinstall-markers-in-region bbb)
  4418. (move-marker ins-point bbb))
  4419. (or (bolp) (insert "\n"))
  4420. (setq ins-end (point))
  4421. (goto-char ins-point)
  4422. (org-skip-whitespace)
  4423. (when (and (< arg 0)
  4424. (org-first-sibling-p)
  4425. (> ne-ins ne-beg))
  4426. ;; Move whitespace back to beginning
  4427. (save-excursion
  4428. (goto-char ins-end)
  4429. (let ((kill-whole-line t))
  4430. (kill-line (- ne-ins ne-beg)) (point)))
  4431. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4432. (move-marker ins-point nil)
  4433. (org-compact-display-after-subtree-move)
  4434. (org-show-empty-lines-in-parent)
  4435. (unless folded
  4436. (org-show-entry)
  4437. (show-children)
  4438. (org-cycle-hide-drawers 'children))))
  4439. (defvar org-subtree-clip ""
  4440. "Clipboard for cut and paste of subtrees.
  4441. This is actually only a copy of the kill, because we use the normal kill
  4442. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4443. (defvar org-subtree-clip-folded nil
  4444. "Was the last copied subtree folded?
  4445. This is used to fold the tree back after pasting.")
  4446. (defun org-cut-subtree (&optional n)
  4447. "Cut the current subtree into the clipboard.
  4448. With prefix arg N, cut this many sequential subtrees.
  4449. This is a short-hand for marking the subtree and then cutting it."
  4450. (interactive "p")
  4451. (org-copy-subtree n 'cut))
  4452. (defun org-copy-subtree (&optional n cut force-store-markers)
  4453. "Cut the current subtree into the clipboard.
  4454. With prefix arg N, cut this many sequential subtrees.
  4455. This is a short-hand for marking the subtree and then copying it.
  4456. If CUT is non-nil, actually cut the subtree.
  4457. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4458. of some markers in the region, even if CUT is non-nil. This is
  4459. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4460. (interactive "p")
  4461. (let (beg end folded (beg0 (point)))
  4462. (if (interactive-p)
  4463. (org-back-to-heading nil) ; take what looks like a subtree
  4464. (org-back-to-heading t)) ; take what is really there
  4465. (org-back-over-empty-lines)
  4466. (setq beg (point))
  4467. (skip-chars-forward " \t\r\n")
  4468. (save-match-data
  4469. (save-excursion (outline-end-of-heading)
  4470. (setq folded (org-invisible-p)))
  4471. (condition-case nil
  4472. (outline-forward-same-level (1- n))
  4473. (error nil))
  4474. (org-end-of-subtree t t))
  4475. (org-back-over-empty-lines)
  4476. (setq end (point))
  4477. (goto-char beg0)
  4478. (when (> end beg)
  4479. (setq org-subtree-clip-folded folded)
  4480. (when (or cut force-store-markers)
  4481. (org-save-markers-in-region beg end))
  4482. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4483. (setq org-subtree-clip (current-kill 0))
  4484. (message "%s: Subtree(s) with %d characters"
  4485. (if cut "Cut" "Copied")
  4486. (length org-subtree-clip)))))
  4487. (defun org-paste-subtree (&optional level tree)
  4488. "Paste the clipboard as a subtree, with modification of headline level.
  4489. The entire subtree is promoted or demoted in order to match a new headline
  4490. level. By default, the new level is derived from the visible headings
  4491. before and after the insertion point, and taken to be the inferior headline
  4492. level of the two. So if the previous visible heading is level 3 and the
  4493. next is level 4 (or vice versa), level 4 will be used for insertion.
  4494. This makes sure that the subtree remains an independent subtree and does
  4495. not swallow low level entries.
  4496. You can also force a different level, either by using a numeric prefix
  4497. argument, or by inserting the heading marker by hand. For example, if the
  4498. cursor is after \"*****\", then the tree will be shifted to level 5.
  4499. If you want to insert the tree as is, just use \\[yank].
  4500. If optional TREE is given, use this text instead of the kill ring."
  4501. (interactive "P")
  4502. (unless (org-kill-is-subtree-p tree)
  4503. (error "%s"
  4504. (substitute-command-keys
  4505. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4506. (let* ((visp (not (org-invisible-p)))
  4507. (txt (or tree (and kill-ring (current-kill 0))))
  4508. (^re (concat "^\\(" outline-regexp "\\)"))
  4509. (re (concat "\\(" outline-regexp "\\)"))
  4510. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4511. (old-level (if (string-match ^re txt)
  4512. (- (match-end 0) (match-beginning 0) 1)
  4513. -1))
  4514. (force-level (cond (level (prefix-numeric-value level))
  4515. ((string-match
  4516. ^re_ (buffer-substring (point-at-bol) (point)))
  4517. (- (match-end 1) (match-beginning 1)))
  4518. (t nil)))
  4519. (previous-level (save-excursion
  4520. (condition-case nil
  4521. (progn
  4522. (outline-previous-visible-heading 1)
  4523. (if (looking-at re)
  4524. (- (match-end 0) (match-beginning 0) 1)
  4525. 1))
  4526. (error 1))))
  4527. (next-level (save-excursion
  4528. (condition-case nil
  4529. (progn
  4530. (or (looking-at outline-regexp)
  4531. (outline-next-visible-heading 1))
  4532. (if (looking-at re)
  4533. (- (match-end 0) (match-beginning 0) 1)
  4534. 1))
  4535. (error 1))))
  4536. (new-level (or force-level (max previous-level next-level)))
  4537. (shift (if (or (= old-level -1)
  4538. (= new-level -1)
  4539. (= old-level new-level))
  4540. 0
  4541. (- new-level old-level)))
  4542. (delta (if (> shift 0) -1 1))
  4543. (func (if (> shift 0) 'org-demote 'org-promote))
  4544. (org-odd-levels-only nil)
  4545. beg end)
  4546. ;; Remove the forced level indicator
  4547. (if force-level
  4548. (delete-region (point-at-bol) (point)))
  4549. ;; Paste
  4550. (beginning-of-line 1)
  4551. (org-back-over-empty-lines)
  4552. (setq beg (point))
  4553. (insert-before-markers txt)
  4554. (unless (string-match "\n\\'" txt) (insert "\n"))
  4555. (org-reinstall-markers-in-region beg)
  4556. (setq end (point))
  4557. (goto-char beg)
  4558. (skip-chars-forward " \t\n\r")
  4559. (setq beg (point))
  4560. (if (and (org-invisible-p) visp)
  4561. (save-excursion (outline-show-heading)))
  4562. ;; Shift if necessary
  4563. (unless (= shift 0)
  4564. (save-restriction
  4565. (narrow-to-region beg end)
  4566. (while (not (= shift 0))
  4567. (org-map-region func (point-min) (point-max))
  4568. (setq shift (+ delta shift)))
  4569. (goto-char (point-min))))
  4570. (when (interactive-p)
  4571. (message "Clipboard pasted as level %d subtree" new-level))
  4572. (if (and kill-ring
  4573. (eq org-subtree-clip (current-kill 0))
  4574. org-subtree-clip-folded)
  4575. ;; The tree was folded before it was killed/copied
  4576. (hide-subtree))))
  4577. (defun org-kill-is-subtree-p (&optional txt)
  4578. "Check if the current kill is an outline subtree, or a set of trees.
  4579. Returns nil if kill does not start with a headline, or if the first
  4580. headline level is not the largest headline level in the tree.
  4581. So this will actually accept several entries of equal levels as well,
  4582. which is OK for `org-paste-subtree'.
  4583. If optional TXT is given, check this string instead of the current kill."
  4584. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4585. (start-level (and kill
  4586. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4587. org-outline-regexp "\\)")
  4588. kill)
  4589. (- (match-end 2) (match-beginning 2) 1)))
  4590. (re (concat "^" org-outline-regexp))
  4591. (start (1+ (match-beginning 2))))
  4592. (if (not start-level)
  4593. (progn
  4594. nil) ;; does not even start with a heading
  4595. (catch 'exit
  4596. (while (setq start (string-match re kill (1+ start)))
  4597. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4598. (throw 'exit nil)))
  4599. t))))
  4600. (defvar org-markers-to-move nil
  4601. "Markers that should be moved with a cut-and-paste operation.
  4602. Those markers are stored together with their positions relative to
  4603. the start of the region.")
  4604. (defun org-save-markers-in-region (beg end)
  4605. "Check markers in region.
  4606. If these markers are between BEG and END, record their position relative
  4607. to BEG, so that after moving the block of text, we can put the markers back
  4608. into place.
  4609. This function gets called just before an entry or tree gets cut from the
  4610. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4611. called immediately, to move the markers with the entries."
  4612. (setq org-markers-to-move nil)
  4613. (when (featurep 'org-clock)
  4614. (org-clock-save-markers-for-cut-and-paste beg end))
  4615. (when (featurep 'org-agenda)
  4616. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4617. (defun org-check-and-save-marker (marker beg end)
  4618. "Check if MARKER is between BEG and END.
  4619. If yes, remember the marker and the distance to BEG."
  4620. (when (and (marker-buffer marker)
  4621. (equal (marker-buffer marker) (current-buffer)))
  4622. (if (and (>= marker beg) (< marker end))
  4623. (push (cons marker (- marker beg)) org-markers-to-move))))
  4624. (defun org-reinstall-markers-in-region (beg)
  4625. "Move all remembered markers to their position relative to BEG."
  4626. (mapc (lambda (x)
  4627. (move-marker (car x) (+ beg (cdr x))))
  4628. org-markers-to-move)
  4629. (setq org-markers-to-move nil))
  4630. (defun org-narrow-to-subtree ()
  4631. "Narrow buffer to the current subtree."
  4632. (interactive)
  4633. (save-excursion
  4634. (save-match-data
  4635. (narrow-to-region
  4636. (progn (org-back-to-heading) (point))
  4637. (progn (org-end-of-subtree t) (point))))))
  4638. ;;; Outline Sorting
  4639. (defun org-sort (with-case)
  4640. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4641. Optional argument WITH-CASE means sort case-sensitively."
  4642. (interactive "P")
  4643. (if (org-at-table-p)
  4644. (org-call-with-arg 'org-table-sort-lines with-case)
  4645. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4646. (defun org-sort-remove-invisible (s)
  4647. (remove-text-properties 0 (length s) org-rm-props s)
  4648. (while (string-match org-bracket-link-regexp s)
  4649. (setq s (replace-match (if (match-end 2)
  4650. (match-string 3 s)
  4651. (match-string 1 s)) t t s)))
  4652. s)
  4653. (defvar org-priority-regexp) ; defined later in the file
  4654. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4655. "Sort entries on a certain level of an outline tree.
  4656. If there is an active region, the entries in the region are sorted.
  4657. Else, if the cursor is before the first entry, sort the top-level items.
  4658. Else, the children of the entry at point are sorted.
  4659. Sorting can be alphabetically, numerically, and by date/time as given by
  4660. the first time stamp in the entry. The command prompts for the sorting
  4661. type unless it has been given to the function through the SORTING-TYPE
  4662. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4663. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4664. called with point at the beginning of the record. It must return either
  4665. a string or a number that should serve as the sorting key for that record.
  4666. Comparing entries ignores case by default. However, with an optional argument
  4667. WITH-CASE, the sorting considers case as well."
  4668. (interactive "P")
  4669. (let ((case-func (if with-case 'identity 'downcase))
  4670. start beg end stars re re2
  4671. txt what tmp plain-list-p)
  4672. ;; Find beginning and end of region to sort
  4673. (cond
  4674. ((org-region-active-p)
  4675. ;; we will sort the region
  4676. (setq end (region-end)
  4677. what "region")
  4678. (goto-char (region-beginning))
  4679. (if (not (org-on-heading-p)) (outline-next-heading))
  4680. (setq start (point)))
  4681. ((org-at-item-p)
  4682. ;; we will sort this plain list
  4683. (org-beginning-of-item-list) (setq start (point))
  4684. (org-end-of-item-list) (setq end (point))
  4685. (goto-char start)
  4686. (setq plain-list-p t
  4687. what "plain list"))
  4688. ((or (org-on-heading-p)
  4689. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4690. ;; we will sort the children of the current headline
  4691. (org-back-to-heading)
  4692. (setq start (point)
  4693. end (progn (org-end-of-subtree t t)
  4694. (org-back-over-empty-lines)
  4695. (point))
  4696. what "children")
  4697. (goto-char start)
  4698. (show-subtree)
  4699. (outline-next-heading))
  4700. (t
  4701. ;; we will sort the top-level entries in this file
  4702. (goto-char (point-min))
  4703. (or (org-on-heading-p) (outline-next-heading))
  4704. (setq start (point) end (point-max) what "top-level")
  4705. (goto-char start)
  4706. (show-all)))
  4707. (setq beg (point))
  4708. (if (>= beg end) (error "Nothing to sort"))
  4709. (unless plain-list-p
  4710. (looking-at "\\(\\*+\\)")
  4711. (setq stars (match-string 1)
  4712. re (concat "^" (regexp-quote stars) " +")
  4713. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4714. txt (buffer-substring beg end))
  4715. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4716. (if (and (not (equal stars "*")) (string-match re2 txt))
  4717. (error "Region to sort contains a level above the first entry")))
  4718. (unless sorting-type
  4719. (message
  4720. (if plain-list-p
  4721. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4722. "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:")
  4723. what)
  4724. (setq sorting-type (read-char-exclusive))
  4725. (and (= (downcase sorting-type) ?f)
  4726. (setq getkey-func
  4727. (completing-read "Sort using function: "
  4728. obarray 'fboundp t nil nil))
  4729. (setq getkey-func (intern getkey-func)))
  4730. (and (= (downcase sorting-type) ?r)
  4731. (setq property
  4732. (completing-read "Property: "
  4733. (mapcar 'list (org-buffer-property-keys t))
  4734. nil t))))
  4735. (message "Sorting entries...")
  4736. (save-restriction
  4737. (narrow-to-region start end)
  4738. (let ((dcst (downcase sorting-type))
  4739. (now (current-time)))
  4740. (sort-subr
  4741. (/= dcst sorting-type)
  4742. ;; This function moves to the beginning character of the "record" to
  4743. ;; be sorted.
  4744. (if plain-list-p
  4745. (lambda nil
  4746. (if (org-at-item-p) t (goto-char (point-max))))
  4747. (lambda nil
  4748. (if (re-search-forward re nil t)
  4749. (goto-char (match-beginning 0))
  4750. (goto-char (point-max)))))
  4751. ;; This function moves to the last character of the "record" being
  4752. ;; sorted.
  4753. (if plain-list-p
  4754. 'org-end-of-item
  4755. (lambda nil
  4756. (save-match-data
  4757. (condition-case nil
  4758. (outline-forward-same-level 1)
  4759. (error
  4760. (goto-char (point-max)))))))
  4761. ;; This function returns the value that gets sorted against.
  4762. (if plain-list-p
  4763. (lambda nil
  4764. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4765. (cond
  4766. ((= dcst ?n)
  4767. (string-to-number (buffer-substring (match-end 0)
  4768. (point-at-eol))))
  4769. ((= dcst ?a)
  4770. (buffer-substring (match-end 0) (point-at-eol)))
  4771. ((= dcst ?t)
  4772. (if (re-search-forward org-ts-regexp
  4773. (point-at-eol) t)
  4774. (org-time-string-to-time (match-string 0))
  4775. now))
  4776. ((= dcst ?f)
  4777. (if getkey-func
  4778. (progn
  4779. (setq tmp (funcall getkey-func))
  4780. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4781. tmp)
  4782. (error "Invalid key function `%s'" getkey-func)))
  4783. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4784. (lambda nil
  4785. (cond
  4786. ((= dcst ?n)
  4787. (if (looking-at org-complex-heading-regexp)
  4788. (string-to-number (match-string 4))
  4789. nil))
  4790. ((= dcst ?a)
  4791. (if (looking-at org-complex-heading-regexp)
  4792. (funcall case-func (match-string 4))
  4793. nil))
  4794. ((= dcst ?t)
  4795. (if (re-search-forward org-ts-regexp
  4796. (save-excursion
  4797. (forward-line 2)
  4798. (point)) t)
  4799. (org-time-string-to-time (match-string 0))
  4800. now))
  4801. ((= dcst ?p)
  4802. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4803. (string-to-char (match-string 2))
  4804. org-default-priority))
  4805. ((= dcst ?r)
  4806. (or (org-entry-get nil property) ""))
  4807. ((= dcst ?o)
  4808. (if (looking-at org-complex-heading-regexp)
  4809. (- 9999 (length (member (match-string 2)
  4810. org-todo-keywords-1)))))
  4811. ((= dcst ?f)
  4812. (if getkey-func
  4813. (progn
  4814. (setq tmp (funcall getkey-func))
  4815. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4816. tmp)
  4817. (error "Invalid key function `%s'" getkey-func)))
  4818. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4819. nil
  4820. (cond
  4821. ((= dcst ?a) 'string<)
  4822. ((= dcst ?t) 'time-less-p)
  4823. (t nil)))))
  4824. (message "Sorting entries...done")))
  4825. (defun org-do-sort (table what &optional with-case sorting-type)
  4826. "Sort TABLE of WHAT according to SORTING-TYPE.
  4827. The user will be prompted for the SORTING-TYPE if the call to this
  4828. function does not specify it. WHAT is only for the prompt, to indicate
  4829. what is being sorted. The sorting key will be extracted from
  4830. the car of the elements of the table.
  4831. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4832. (unless sorting-type
  4833. (message
  4834. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4835. what)
  4836. (setq sorting-type (read-char-exclusive)))
  4837. (let ((dcst (downcase sorting-type))
  4838. extractfun comparefun)
  4839. ;; Define the appropriate functions
  4840. (cond
  4841. ((= dcst ?n)
  4842. (setq extractfun 'string-to-number
  4843. comparefun (if (= dcst sorting-type) '< '>)))
  4844. ((= dcst ?a)
  4845. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4846. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4847. comparefun (if (= dcst sorting-type)
  4848. 'string<
  4849. (lambda (a b) (and (not (string< a b))
  4850. (not (string= a b)))))))
  4851. ((= dcst ?t)
  4852. (setq extractfun
  4853. (lambda (x)
  4854. (if (string-match org-ts-regexp x)
  4855. (time-to-seconds
  4856. (org-time-string-to-time (match-string 0 x)))
  4857. 0))
  4858. comparefun (if (= dcst sorting-type) '< '>)))
  4859. (t (error "Invalid sorting type `%c'" sorting-type)))
  4860. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4861. table)
  4862. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4863. ;;; Editing source examples
  4864. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4865. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4866. (defvar org-edit-src-force-single-line nil)
  4867. (defvar org-edit-src-from-org-mode nil)
  4868. (defvar org-edit-src-picture nil)
  4869. (define-minor-mode org-exit-edit-mode
  4870. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4871. (defun org-edit-src-code ()
  4872. "Edit the source code example at point.
  4873. An indirect buffer is created, and that buffer is then narrowed to the
  4874. example at point and switched to the correct language mode. When done,
  4875. exit by killing the buffer with \\[org-edit-src-exit]."
  4876. (interactive)
  4877. (let ((line (org-current-line))
  4878. (case-fold-search t)
  4879. (msg (substitute-command-keys
  4880. "Edit, then exit with C-c ' (C-c and single quote)"))
  4881. (info (org-edit-src-find-region-and-lang))
  4882. (org-mode-p (eq major-mode 'org-mode))
  4883. beg end lang lang-f single)
  4884. (if (not info)
  4885. nil
  4886. (setq beg (nth 0 info)
  4887. end (nth 1 info)
  4888. lang (nth 2 info)
  4889. single (nth 3 info)
  4890. lang-f (intern (concat lang "-mode")))
  4891. (unless (functionp lang-f)
  4892. (error "No such language mode: %s" lang-f))
  4893. (goto-line line)
  4894. (if (get-buffer "*Org Edit Src Example*")
  4895. (kill-buffer "*Org Edit Src Example*"))
  4896. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4897. "*Org Edit Src Example*"))
  4898. (narrow-to-region beg end)
  4899. (remove-text-properties beg end '(display nil invisible nil
  4900. intangible nil))
  4901. (let ((org-inhibit-startup t))
  4902. (funcall lang-f))
  4903. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4904. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4905. (when org-mode-p
  4906. (goto-char (point-min))
  4907. (while (re-search-forward "^," nil t)
  4908. (replace-match "")))
  4909. (goto-line line)
  4910. (org-exit-edit-mode)
  4911. (org-set-local 'header-line-format msg)
  4912. (message "%s" msg)
  4913. t)))
  4914. (defun org-edit-fixed-width-region ()
  4915. "Edit the fixed-width ascii drawing at point.
  4916. This must be a region where each line starts with ca colon followed by
  4917. a space character.
  4918. An indirect buffer is created, and that buffer is then narrowed to the
  4919. example at point and switched to artist-mode. When done,
  4920. exit by killing the buffer with \\[org-edit-src-exit]."
  4921. (interactive)
  4922. (let ((line (org-current-line))
  4923. (case-fold-search t)
  4924. (msg (substitute-command-keys
  4925. "Edit, then exit with C-c ' (C-c and single quote)"))
  4926. (org-mode-p (eq major-mode 'org-mode))
  4927. beg end lang lang-f)
  4928. (beginning-of-line 1)
  4929. (if (looking-at "[ \t]*[^:\n \t]")
  4930. nil
  4931. (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
  4932. (setq beg (point) end (match-end 0))
  4933. (save-excursion
  4934. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  4935. (setq beg (point-at-bol 2))
  4936. (setq beg (point))))
  4937. (save-excursion
  4938. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  4939. (setq end (match-beginning 0))
  4940. (setq end (point))))
  4941. (goto-line line)
  4942. (if (get-buffer "*Org Edit Picture*")
  4943. (kill-buffer "*Org Edit Picture*"))
  4944. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4945. "*Org Edit Picture*"))
  4946. (narrow-to-region beg end)
  4947. (remove-text-properties beg end '(display nil invisible nil
  4948. intangible nil))
  4949. (when (fboundp 'font-lock-unfontify-region)
  4950. (font-lock-unfontify-region (point-min) (point-max)))
  4951. (cond
  4952. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  4953. (fundamental-mode)
  4954. (artist-mode 1))
  4955. (t (funcall org-edit-fixed-width-region-mode)))
  4956. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  4957. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4958. (set (make-local-variable 'org-edit-src-picture) t)
  4959. (goto-char (point-min))
  4960. (while (re-search-forward "^[ \t]*: " nil t)
  4961. (replace-match ""))
  4962. (goto-line line)
  4963. (org-exit-edit-mode)
  4964. (org-set-local 'header-line-format msg)
  4965. (message "%s" msg)
  4966. t))))
  4967. (defun org-edit-src-find-region-and-lang ()
  4968. "Find the region and language for a local edit.
  4969. Return a list with beginning and end of the region, a string representing
  4970. the language, a switch telling of the content should be in a single line."
  4971. (let ((re-list
  4972. '(
  4973. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  4974. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  4975. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  4976. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  4977. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  4978. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  4979. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  4980. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  4981. ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
  4982. ("^#\\+html:" "\n" "html" single-line)
  4983. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  4984. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  4985. ("^#\\+latex:" "\n" "latex" single-line)
  4986. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  4987. ("^#\\+ascii:" "\n" "ascii" single-line)
  4988. ))
  4989. (pos (point))
  4990. re re1 re2 single beg end lang)
  4991. (catch 'exit
  4992. (while (setq entry (pop re-list))
  4993. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  4994. single (nth 3 entry))
  4995. (save-excursion
  4996. (if (or (looking-at re1)
  4997. (re-search-backward re1 nil t))
  4998. (progn
  4999. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5000. (if (and (re-search-forward re2 nil t)
  5001. (>= (match-end 0) pos))
  5002. (throw 'exit (list beg (match-beginning 0) lang single))))
  5003. (if (or (looking-at re2)
  5004. (re-search-forward re2 nil t))
  5005. (progn
  5006. (setq end (match-beginning 0))
  5007. (if (and (re-search-backward re1 nil t)
  5008. (<= (match-beginning 0) pos))
  5009. (throw 'exit
  5010. (list (match-end 0) end
  5011. (org-edit-src-get-lang lang) single)))))))))))
  5012. (defun org-edit-src-get-lang (lang)
  5013. "Extract the src language."
  5014. (let ((m (match-string 0)))
  5015. (cond
  5016. ((stringp lang) lang)
  5017. ((integerp lang) (match-string lang))
  5018. ((and (eq lang lang)
  5019. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5020. (match-string 1 m))
  5021. ((and (eq lang lang)
  5022. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5023. (match-string 1 m))
  5024. (t "fundamental"))))
  5025. (defun org-edit-src-exit ()
  5026. "Exit special edit and protect problematic lines."
  5027. (interactive)
  5028. (unless (buffer-base-buffer (current-buffer))
  5029. (error "This is not an indirect buffer, something is wrong..."))
  5030. (unless (> (point-min) 1)
  5031. (error "This buffer is not narrowed, something is wrong..."))
  5032. (goto-char (point-min))
  5033. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5034. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5035. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5036. (goto-char (point-min))
  5037. (while (re-search-forward "\n" nil t)
  5038. (replace-match " "))
  5039. (goto-char (point-min))
  5040. (if (looking-at "\\s-*") (replace-match " "))
  5041. (if (re-search-forward "\\s-+\\'" nil t)
  5042. (replace-match "")))
  5043. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5044. (goto-char (point-min))
  5045. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5046. (replace-match ",\\1"))
  5047. (when font-lock-mode
  5048. (font-lock-unfontify-region (point-min) (point-max)))
  5049. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5050. (when (org-bound-and-true-p org-edit-src-picture)
  5051. (goto-char (point-min))
  5052. (while (re-search-forward "^" nil t)
  5053. (replace-match ": "))
  5054. (when font-lock-mode
  5055. (font-lock-unfontify-region (point-min) (point-max)))
  5056. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5057. (kill-buffer (current-buffer))
  5058. (and (org-mode-p) (org-restart-font-lock)))
  5059. ;;;; Plain list items, including checkboxes
  5060. ;;; Plain list items
  5061. (defun org-at-item-p ()
  5062. "Is point in a line starting a hand-formatted item?"
  5063. (let ((llt org-plain-list-ordered-item-terminator))
  5064. (save-excursion
  5065. (goto-char (point-at-bol))
  5066. (looking-at
  5067. (cond
  5068. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5069. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5070. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  5071. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  5072. (defun org-in-item-p ()
  5073. "It the cursor inside a plain list item.
  5074. Does not have to be the first line."
  5075. (save-excursion
  5076. (condition-case nil
  5077. (progn
  5078. (org-beginning-of-item)
  5079. (org-at-item-p)
  5080. t)
  5081. (error nil))))
  5082. (defun org-insert-item (&optional checkbox)
  5083. "Insert a new item at the current level.
  5084. Return t when things worked, nil when we are not in an item."
  5085. (when (save-excursion
  5086. (condition-case nil
  5087. (progn
  5088. (org-beginning-of-item)
  5089. (org-at-item-p)
  5090. (if (org-invisible-p) (error "Invisible item"))
  5091. t)
  5092. (error nil)))
  5093. (let* ((bul (match-string 0))
  5094. (descp (save-excursion (goto-char (match-beginning 0))
  5095. (beginning-of-line 1)
  5096. (save-match-data
  5097. (looking-at "[ \t]*.*? ::"))))
  5098. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5099. (match-end 0)))
  5100. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5101. pos)
  5102. (if descp (setq checkbox nil))
  5103. (cond
  5104. ((and (org-at-item-p) (<= (point) eow))
  5105. ;; before the bullet
  5106. (beginning-of-line 1)
  5107. (open-line (if blank 2 1)))
  5108. ((<= (point) eow)
  5109. (beginning-of-line 1))
  5110. (t
  5111. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  5112. (end-of-line 1)
  5113. (delete-horizontal-space))
  5114. (newline (if blank 2 1))))
  5115. (insert bul
  5116. (if checkbox "[ ]" "")
  5117. (if descp (concat (if checkbox " " "")
  5118. (read-string "Term: ") " :: ") ""))
  5119. (just-one-space)
  5120. (setq pos (point))
  5121. (end-of-line 1)
  5122. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5123. (org-maybe-renumber-ordered-list)
  5124. (and checkbox (org-update-checkbox-count-maybe))
  5125. t))
  5126. ;;; Checkboxes
  5127. (defun org-at-item-checkbox-p ()
  5128. "Is point at a line starting a plain-list item with a checklet?"
  5129. (and (org-at-item-p)
  5130. (save-excursion
  5131. (goto-char (match-end 0))
  5132. (skip-chars-forward " \t")
  5133. (looking-at "\\[[- X]\\]"))))
  5134. (defun org-toggle-checkbox (&optional arg)
  5135. "Toggle the checkbox in the current line."
  5136. (interactive "P")
  5137. (catch 'exit
  5138. (let (beg end status (firstnew 'unknown))
  5139. (cond
  5140. ((org-region-active-p)
  5141. (setq beg (region-beginning) end (region-end)))
  5142. ((org-on-heading-p)
  5143. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5144. ((org-at-item-checkbox-p)
  5145. (let ((pos (point)))
  5146. (replace-match
  5147. (cond (arg "[-]")
  5148. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5149. (t "[ ]"))
  5150. t t)
  5151. (goto-char pos))
  5152. (throw 'exit t))
  5153. (t (error "Not at a checkbox or heading, and no active region")))
  5154. (save-excursion
  5155. (goto-char beg)
  5156. (while (< (point) end)
  5157. (when (org-at-item-checkbox-p)
  5158. (setq status (equal (match-string 0) "[X]"))
  5159. (when (eq firstnew 'unknown)
  5160. (setq firstnew (not status)))
  5161. (replace-match
  5162. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5163. (beginning-of-line 2)))))
  5164. (org-update-checkbox-count-maybe))
  5165. (defun org-update-checkbox-count-maybe ()
  5166. "Update checkbox statistics unless turned off by user."
  5167. (when org-provide-checkbox-statistics
  5168. (org-update-checkbox-count)))
  5169. (defun org-update-checkbox-count (&optional all)
  5170. "Update the checkbox statistics in the current section.
  5171. This will find all statistic cookies like [57%] and [6/12] and update them
  5172. with the current numbers. With optional prefix argument ALL, do this for
  5173. the whole buffer."
  5174. (interactive "P")
  5175. (save-excursion
  5176. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5177. (beg (condition-case nil
  5178. (progn (outline-back-to-heading) (point))
  5179. (error (point-min))))
  5180. (end (move-marker (make-marker)
  5181. (progn (outline-next-heading) (point))))
  5182. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  5183. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5184. (re-find (concat re "\\|" re-box))
  5185. beg-cookie end-cookie is-percent c-on c-off lim
  5186. eline curr-ind next-ind continue-from startsearch
  5187. (cstat 0)
  5188. )
  5189. (when all
  5190. (goto-char (point-min))
  5191. (outline-next-heading)
  5192. (setq beg (point) end (point-max)))
  5193. (goto-char end)
  5194. ;; find each statistic cookie
  5195. (while (re-search-backward re-find beg t)
  5196. (setq beg-cookie (match-beginning 1)
  5197. end-cookie (match-end 1)
  5198. cstat (+ cstat (if end-cookie 1 0))
  5199. startsearch (point-at-eol)
  5200. continue-from (point-at-bol)
  5201. is-percent (match-beginning 2)
  5202. lim (cond
  5203. ((org-on-heading-p) (outline-next-heading) (point))
  5204. ((org-at-item-p) (org-end-of-item) (point))
  5205. (t nil))
  5206. c-on 0
  5207. c-off 0)
  5208. (when lim
  5209. ;; find first checkbox for this cookie and gather
  5210. ;; statistics from all that are at this indentation level
  5211. (goto-char startsearch)
  5212. (if (re-search-forward re-box lim t)
  5213. (progn
  5214. (org-beginning-of-item)
  5215. (setq curr-ind (org-get-indentation))
  5216. (setq next-ind curr-ind)
  5217. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5218. (save-excursion (end-of-line) (setq eline (point)))
  5219. (if (re-search-forward re-box eline t)
  5220. (if (member (match-string 2) '("[ ]" "[-]"))
  5221. (setq c-off (1+ c-off))
  5222. (setq c-on (1+ c-on))
  5223. )
  5224. )
  5225. (org-end-of-item)
  5226. (setq next-ind (org-get-indentation))
  5227. )))
  5228. (goto-char continue-from)
  5229. ;; update cookie
  5230. (when end-cookie
  5231. (delete-region beg-cookie end-cookie)
  5232. (goto-char beg-cookie)
  5233. (insert
  5234. (if is-percent
  5235. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5236. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5237. ;; update items checkbox if it has one
  5238. (when (org-at-item-p)
  5239. (org-beginning-of-item)
  5240. (when (and (> (+ c-on c-off) 0)
  5241. (re-search-forward re-box (point-at-eol) t))
  5242. (setq beg-cookie (match-beginning 2)
  5243. end-cookie (match-end 2))
  5244. (delete-region beg-cookie end-cookie)
  5245. (goto-char beg-cookie)
  5246. (cond ((= c-off 0) (insert "[X]"))
  5247. ((= c-on 0) (insert "[ ]"))
  5248. (t (insert "[-]")))
  5249. )))
  5250. (goto-char continue-from))
  5251. (when (interactive-p)
  5252. (message "Checkbox satistics updated %s (%d places)"
  5253. (if all "in entire file" "in current outline entry") cstat)))))
  5254. (defun org-get-checkbox-statistics-face ()
  5255. "Select the face for checkbox statistics.
  5256. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5257. it will be `org-todo'."
  5258. (if (match-end 1)
  5259. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5260. (if (and (> (match-end 2) (match-beginning 2))
  5261. (equal (match-string 2) (match-string 3)))
  5262. 'org-done
  5263. 'org-todo)))
  5264. (defun org-get-indentation (&optional line)
  5265. "Get the indentation of the current line, interpreting tabs.
  5266. When LINE is given, assume it represents a line and compute its indentation."
  5267. (if line
  5268. (if (string-match "^ *" (org-remove-tabs line))
  5269. (match-end 0))
  5270. (save-excursion
  5271. (beginning-of-line 1)
  5272. (skip-chars-forward " \t")
  5273. (current-column))))
  5274. (defun org-remove-tabs (s &optional width)
  5275. "Replace tabulators in S with spaces.
  5276. Assumes that s is a single line, starting in column 0."
  5277. (setq width (or width tab-width))
  5278. (while (string-match "\t" s)
  5279. (setq s (replace-match
  5280. (make-string
  5281. (- (* width (/ (+ (match-beginning 0) width) width))
  5282. (match-beginning 0)) ?\ )
  5283. t t s)))
  5284. s)
  5285. (defun org-fix-indentation (line ind)
  5286. "Fix indentation in LINE.
  5287. IND is a cons cell with target and minimum indentation.
  5288. If the current indenation in LINE is smaller than the minimum,
  5289. leave it alone. If it is larger than ind, set it to the target."
  5290. (let* ((l (org-remove-tabs line))
  5291. (i (org-get-indentation l))
  5292. (i1 (car ind)) (i2 (cdr ind)))
  5293. (if (>= i i2) (setq l (substring line i2)))
  5294. (if (> i1 0)
  5295. (concat (make-string i1 ?\ ) l)
  5296. l)))
  5297. (defun org-beginning-of-item ()
  5298. "Go to the beginning of the current hand-formatted item.
  5299. If the cursor is not in an item, throw an error."
  5300. (interactive)
  5301. (let ((pos (point))
  5302. (limit (save-excursion
  5303. (condition-case nil
  5304. (progn
  5305. (org-back-to-heading)
  5306. (beginning-of-line 2) (point))
  5307. (error (point-min)))))
  5308. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5309. ind ind1)
  5310. (if (org-at-item-p)
  5311. (beginning-of-line 1)
  5312. (beginning-of-line 1)
  5313. (skip-chars-forward " \t")
  5314. (setq ind (current-column))
  5315. (if (catch 'exit
  5316. (while t
  5317. (beginning-of-line 0)
  5318. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5319. (if (looking-at "[ \t]*$")
  5320. (setq ind1 ind-empty)
  5321. (skip-chars-forward " \t")
  5322. (setq ind1 (current-column)))
  5323. (if (< ind1 ind)
  5324. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5325. nil
  5326. (goto-char pos)
  5327. (error "Not in an item")))))
  5328. (defun org-end-of-item ()
  5329. "Go to the end of the current hand-formatted item.
  5330. If the cursor is not in an item, throw an error."
  5331. (interactive)
  5332. (let* ((pos (point))
  5333. ind1
  5334. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5335. (limit (save-excursion (outline-next-heading) (point)))
  5336. (ind (save-excursion
  5337. (org-beginning-of-item)
  5338. (skip-chars-forward " \t")
  5339. (current-column)))
  5340. (end (catch 'exit
  5341. (while t
  5342. (beginning-of-line 2)
  5343. (if (eobp) (throw 'exit (point)))
  5344. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5345. (if (looking-at "[ \t]*$")
  5346. (setq ind1 ind-empty)
  5347. (skip-chars-forward " \t")
  5348. (setq ind1 (current-column)))
  5349. (if (<= ind1 ind)
  5350. (throw 'exit (point-at-bol)))))))
  5351. (if end
  5352. (goto-char end)
  5353. (goto-char pos)
  5354. (error "Not in an item"))))
  5355. (defun org-next-item ()
  5356. "Move to the beginning of the next item in the current plain list.
  5357. Error if not at a plain list, or if this is the last item in the list."
  5358. (interactive)
  5359. (let (ind ind1 (pos (point)))
  5360. (org-beginning-of-item)
  5361. (setq ind (org-get-indentation))
  5362. (org-end-of-item)
  5363. (setq ind1 (org-get-indentation))
  5364. (unless (and (org-at-item-p) (= ind ind1))
  5365. (goto-char pos)
  5366. (error "On last item"))))
  5367. (defun org-previous-item ()
  5368. "Move to the beginning of the previous item in the current plain list.
  5369. Error if not at a plain list, or if this is the first item in the list."
  5370. (interactive)
  5371. (let (beg ind ind1 (pos (point)))
  5372. (org-beginning-of-item)
  5373. (setq beg (point))
  5374. (setq ind (org-get-indentation))
  5375. (goto-char beg)
  5376. (catch 'exit
  5377. (while t
  5378. (beginning-of-line 0)
  5379. (if (looking-at "[ \t]*$")
  5380. nil
  5381. (if (<= (setq ind1 (org-get-indentation)) ind)
  5382. (throw 'exit t)))))
  5383. (condition-case nil
  5384. (if (or (not (org-at-item-p))
  5385. (< ind1 (1- ind)))
  5386. (error "")
  5387. (org-beginning-of-item))
  5388. (error (goto-char pos)
  5389. (error "On first item")))))
  5390. (defun org-first-list-item-p ()
  5391. "Is this heading the item in a plain list?"
  5392. (unless (org-at-item-p)
  5393. (error "Not at a plain list item"))
  5394. (org-beginning-of-item)
  5395. (= (point) (save-excursion (org-beginning-of-item-list))))
  5396. (defun org-move-item-down ()
  5397. "Move the plain list item at point down, i.e. swap with following item.
  5398. Subitems (items with larger indentation) are considered part of the item,
  5399. so this really moves item trees."
  5400. (interactive)
  5401. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5402. (org-beginning-of-item)
  5403. (setq beg0 (point))
  5404. (save-excursion
  5405. (setq ne-beg (org-back-over-empty-lines))
  5406. (setq beg (point)))
  5407. (goto-char beg0)
  5408. (setq ind (org-get-indentation))
  5409. (org-end-of-item)
  5410. (setq end0 (point))
  5411. (setq ind1 (org-get-indentation))
  5412. (setq ne-end (org-back-over-empty-lines))
  5413. (setq end (point))
  5414. (goto-char beg0)
  5415. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5416. ;; include less whitespace
  5417. (save-excursion
  5418. (goto-char beg)
  5419. (forward-line (- ne-beg ne-end))
  5420. (setq beg (point))))
  5421. (goto-char end0)
  5422. (if (and (org-at-item-p) (= ind ind1))
  5423. (progn
  5424. (org-end-of-item)
  5425. (org-back-over-empty-lines)
  5426. (setq txt (buffer-substring beg end))
  5427. (save-excursion
  5428. (delete-region beg end))
  5429. (setq pos (point))
  5430. (insert txt)
  5431. (goto-char pos) (org-skip-whitespace)
  5432. (org-maybe-renumber-ordered-list))
  5433. (goto-char pos)
  5434. (error "Cannot move this item further down"))))
  5435. (defun org-move-item-up (arg)
  5436. "Move the plain list item at point up, i.e. swap with previous item.
  5437. Subitems (items with larger indentation) are considered part of the item,
  5438. so this really moves item trees."
  5439. (interactive "p")
  5440. (let (beg beg0 end ind ind1 (pos (point)) txt
  5441. ne-beg ne-ins ins-end)
  5442. (org-beginning-of-item)
  5443. (setq beg0 (point))
  5444. (setq ind (org-get-indentation))
  5445. (save-excursion
  5446. (setq ne-beg (org-back-over-empty-lines))
  5447. (setq beg (point)))
  5448. (goto-char beg0)
  5449. (org-end-of-item)
  5450. (org-back-over-empty-lines)
  5451. (setq end (point))
  5452. (goto-char beg0)
  5453. (catch 'exit
  5454. (while t
  5455. (beginning-of-line 0)
  5456. (if (looking-at "[ \t]*$")
  5457. (if org-empty-line-terminates-plain-lists
  5458. (progn
  5459. (goto-char pos)
  5460. (error "Cannot move this item further up"))
  5461. nil)
  5462. (if (<= (setq ind1 (org-get-indentation)) ind)
  5463. (throw 'exit t)))))
  5464. (condition-case nil
  5465. (org-beginning-of-item)
  5466. (error (goto-char beg0)
  5467. (error "Cannot move this item further up")))
  5468. (setq ind1 (org-get-indentation))
  5469. (if (and (org-at-item-p) (= ind ind1))
  5470. (progn
  5471. (setq ne-ins (org-back-over-empty-lines))
  5472. (setq txt (buffer-substring beg end))
  5473. (save-excursion
  5474. (delete-region beg end))
  5475. (setq pos (point))
  5476. (insert txt)
  5477. (setq ins-end (point))
  5478. (goto-char pos) (org-skip-whitespace)
  5479. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5480. ;; Move whitespace back to beginning
  5481. (save-excursion
  5482. (goto-char ins-end)
  5483. (let ((kill-whole-line t))
  5484. (kill-line (- ne-ins ne-beg)) (point)))
  5485. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5486. (org-maybe-renumber-ordered-list))
  5487. (goto-char pos)
  5488. (error "Cannot move this item further up"))))
  5489. (defun org-maybe-renumber-ordered-list ()
  5490. "Renumber the ordered list at point if setup allows it.
  5491. This tests the user option `org-auto-renumber-ordered-lists' before
  5492. doing the renumbering."
  5493. (interactive)
  5494. (when (and org-auto-renumber-ordered-lists
  5495. (org-at-item-p))
  5496. (if (match-beginning 3)
  5497. (org-renumber-ordered-list 1)
  5498. (org-fix-bullet-type))))
  5499. (defun org-maybe-renumber-ordered-list-safe ()
  5500. (condition-case nil
  5501. (save-excursion
  5502. (org-maybe-renumber-ordered-list))
  5503. (error nil)))
  5504. (defun org-cycle-list-bullet (&optional which)
  5505. "Cycle through the different itemize/enumerate bullets.
  5506. This cycle the entire list level through the sequence:
  5507. `-' -> `+' -> `*' -> `1.' -> `1)'
  5508. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5509. 0 meand `-', 1 means `+' etc."
  5510. (interactive "P")
  5511. (org-preserve-lc
  5512. (org-beginning-of-item-list)
  5513. (org-at-item-p)
  5514. (beginning-of-line 1)
  5515. (let ((current (match-string 0))
  5516. (prevp (eq which 'previous))
  5517. new)
  5518. (setq new (cond
  5519. ((and (numberp which)
  5520. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5521. ((string-match "-" current) (if prevp "1)" "+"))
  5522. ((string-match "\\+" current)
  5523. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5524. ((string-match "\\*" current) (if prevp "+" "1."))
  5525. ((string-match "\\." current) (if prevp "*" "1)"))
  5526. ((string-match ")" current) (if prevp "1." "-"))
  5527. (t (error "This should not happen"))))
  5528. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5529. (org-fix-bullet-type)
  5530. (org-maybe-renumber-ordered-list))))
  5531. (defun org-get-string-indentation (s)
  5532. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5533. (let ((n -1) (i 0) (w tab-width) c)
  5534. (catch 'exit
  5535. (while (< (setq n (1+ n)) (length s))
  5536. (setq c (aref s n))
  5537. (cond ((= c ?\ ) (setq i (1+ i)))
  5538. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5539. (t (throw 'exit t)))))
  5540. i))
  5541. (defun org-renumber-ordered-list (arg)
  5542. "Renumber an ordered plain list.
  5543. Cursor needs to be in the first line of an item, the line that starts
  5544. with something like \"1.\" or \"2)\"."
  5545. (interactive "p")
  5546. (unless (and (org-at-item-p)
  5547. (match-beginning 3))
  5548. (error "This is not an ordered list"))
  5549. (let ((line (org-current-line))
  5550. (col (current-column))
  5551. (ind (org-get-string-indentation
  5552. (buffer-substring (point-at-bol) (match-beginning 3))))
  5553. ;; (term (substring (match-string 3) -1))
  5554. ind1 (n (1- arg))
  5555. fmt bobp)
  5556. ;; find where this list begins
  5557. (org-beginning-of-item-list)
  5558. (setq bobp (bobp))
  5559. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5560. (setq fmt (concat "%d" (match-string 1)))
  5561. (beginning-of-line 0)
  5562. ;; walk forward and replace these numbers
  5563. (catch 'exit
  5564. (while t
  5565. (catch 'next
  5566. (if bobp (setq bobp nil) (beginning-of-line 2))
  5567. (if (eobp) (throw 'exit nil))
  5568. (if (looking-at "[ \t]*$") (throw 'next nil))
  5569. (skip-chars-forward " \t") (setq ind1 (current-column))
  5570. (if (> ind1 ind) (throw 'next t))
  5571. (if (< ind1 ind) (throw 'exit t))
  5572. (if (not (org-at-item-p)) (throw 'exit nil))
  5573. (delete-region (match-beginning 2) (match-end 2))
  5574. (goto-char (match-beginning 2))
  5575. (insert (format fmt (setq n (1+ n)))))))
  5576. (goto-line line)
  5577. (org-move-to-column col)))
  5578. (defun org-fix-bullet-type ()
  5579. "Make sure all items in this list have the same bullet as the firsst item."
  5580. (interactive)
  5581. (unless (org-at-item-p) (error "This is not a list"))
  5582. (let ((line (org-current-line))
  5583. (col (current-column))
  5584. (ind (current-indentation))
  5585. ind1 bullet)
  5586. ;; find where this list begins
  5587. (org-beginning-of-item-list)
  5588. (beginning-of-line 1)
  5589. ;; find out what the bullet type is
  5590. (looking-at "[ \t]*\\(\\S-+\\)")
  5591. (setq bullet (match-string 1))
  5592. ;; walk forward and replace these numbers
  5593. (beginning-of-line 0)
  5594. (catch 'exit
  5595. (while t
  5596. (catch 'next
  5597. (beginning-of-line 2)
  5598. (if (eobp) (throw 'exit nil))
  5599. (if (looking-at "[ \t]*$") (throw 'next nil))
  5600. (skip-chars-forward " \t") (setq ind1 (current-column))
  5601. (if (> ind1 ind) (throw 'next t))
  5602. (if (< ind1 ind) (throw 'exit t))
  5603. (if (not (org-at-item-p)) (throw 'exit nil))
  5604. (skip-chars-forward " \t")
  5605. (looking-at "\\S-+")
  5606. (replace-match bullet))))
  5607. (goto-line line)
  5608. (org-move-to-column col)
  5609. (if (string-match "[0-9]" bullet)
  5610. (org-renumber-ordered-list 1))))
  5611. (defun org-beginning-of-item-list ()
  5612. "Go to the beginning of the current item list.
  5613. I.e. to the first item in this list."
  5614. (interactive)
  5615. (org-beginning-of-item)
  5616. (let ((pos (point-at-bol))
  5617. (ind (org-get-indentation))
  5618. ind1)
  5619. ;; find where this list begins
  5620. (catch 'exit
  5621. (while t
  5622. (catch 'next
  5623. (beginning-of-line 0)
  5624. (if (looking-at "[ \t]*$")
  5625. (throw (if (bobp) 'exit 'next) t))
  5626. (skip-chars-forward " \t") (setq ind1 (current-column))
  5627. (if (or (< ind1 ind)
  5628. (and (= ind1 ind)
  5629. (not (org-at-item-p)))
  5630. (and (= (point-at-bol) (point-min))
  5631. (setq pos (point-min))))
  5632. (throw 'exit t)
  5633. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5634. (goto-char pos)))
  5635. (defun org-end-of-item-list ()
  5636. "Go to the end of the current item list.
  5637. I.e. to the text after the last item."
  5638. (interactive)
  5639. (org-beginning-of-item)
  5640. (let ((pos (point-at-bol))
  5641. (ind (org-get-indentation))
  5642. ind1)
  5643. ;; find where this list begins
  5644. (catch 'exit
  5645. (while t
  5646. (catch 'next
  5647. (beginning-of-line 2)
  5648. (if (looking-at "[ \t]*$")
  5649. (throw (if (eobp) 'exit 'next) t))
  5650. (skip-chars-forward " \t") (setq ind1 (current-column))
  5651. (if (or (< ind1 ind)
  5652. (and (= ind1 ind)
  5653. (not (org-at-item-p)))
  5654. (eobp))
  5655. (progn
  5656. (setq pos (point-at-bol))
  5657. (throw 'exit t))))))
  5658. (goto-char pos)))
  5659. (defvar org-last-indent-begin-marker (make-marker))
  5660. (defvar org-last-indent-end-marker (make-marker))
  5661. (defun org-outdent-item (arg)
  5662. "Outdent a local list item."
  5663. (interactive "p")
  5664. (org-indent-item (- arg)))
  5665. (defun org-indent-item (arg)
  5666. "Indent a local list item."
  5667. (interactive "p")
  5668. (unless (org-at-item-p)
  5669. (error "Not on an item"))
  5670. (save-excursion
  5671. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5672. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5673. (setq beg org-last-indent-begin-marker
  5674. end org-last-indent-end-marker)
  5675. (org-beginning-of-item)
  5676. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5677. (org-end-of-item)
  5678. (setq end (move-marker org-last-indent-end-marker (point))))
  5679. (goto-char beg)
  5680. (setq tmp (org-item-indent-positions)
  5681. ind (car tmp)
  5682. ind-down (nth 2 tmp)
  5683. ind-up (nth 1 tmp)
  5684. delta (if (> arg 0)
  5685. (if ind-down (- ind-down ind) 2)
  5686. (if ind-up (- ind-up ind) -2)))
  5687. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5688. (while (< (point) end)
  5689. (beginning-of-line 1)
  5690. (skip-chars-forward " \t") (setq ind1 (current-column))
  5691. (delete-region (point-at-bol) (point))
  5692. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5693. (beginning-of-line 2))))
  5694. (org-fix-bullet-type)
  5695. (org-maybe-renumber-ordered-list-safe)
  5696. (save-excursion
  5697. (beginning-of-line 0)
  5698. (condition-case nil (org-beginning-of-item) (error nil))
  5699. (org-maybe-renumber-ordered-list-safe)))
  5700. (defun org-item-indent-positions ()
  5701. "Return indentation for plain list items.
  5702. This returns a list with three values: The current indentation, the
  5703. parent indentation and the indentation a child should habe.
  5704. Assumes cursor in item line."
  5705. (let* ((bolpos (point-at-bol))
  5706. (ind (org-get-indentation))
  5707. ind-down ind-up pos)
  5708. (save-excursion
  5709. (org-beginning-of-item-list)
  5710. (skip-chars-backward "\n\r \t")
  5711. (when (org-in-item-p)
  5712. (org-beginning-of-item)
  5713. (setq ind-up (org-get-indentation))))
  5714. (setq pos (point))
  5715. (save-excursion
  5716. (cond
  5717. ((and (condition-case nil (progn (org-previous-item) t)
  5718. (error nil))
  5719. (or (forward-char 1) t)
  5720. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5721. (setq ind-down (org-get-indentation)))
  5722. ((and (goto-char pos)
  5723. (org-at-item-p))
  5724. (goto-char (match-end 0))
  5725. (skip-chars-forward " \t")
  5726. (setq ind-down (current-column)))))
  5727. (list ind ind-up ind-down)))
  5728. ;;; The orgstruct minor mode
  5729. ;; Define a minor mode which can be used in other modes in order to
  5730. ;; integrate the org-mode structure editing commands.
  5731. ;; This is really a hack, because the org-mode structure commands use
  5732. ;; keys which normally belong to the major mode. Here is how it
  5733. ;; works: The minor mode defines all the keys necessary to operate the
  5734. ;; structure commands, but wraps the commands into a function which
  5735. ;; tests if the cursor is currently at a headline or a plain list
  5736. ;; item. If that is the case, the structure command is used,
  5737. ;; temporarily setting many Org-mode variables like regular
  5738. ;; expressions for filling etc. However, when any of those keys is
  5739. ;; used at a different location, function uses `key-binding' to look
  5740. ;; up if the key has an associated command in another currently active
  5741. ;; keymap (minor modes, major mode, global), and executes that
  5742. ;; command. There might be problems if any of the keys is otherwise
  5743. ;; used as a prefix key.
  5744. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5745. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5746. ;; addresses this by checking explicitly for both bindings.
  5747. (defvar orgstruct-mode-map (make-sparse-keymap)
  5748. "Keymap for the minor `orgstruct-mode'.")
  5749. (defvar org-local-vars nil
  5750. "List of local variables, for use by `orgstruct-mode'")
  5751. ;;;###autoload
  5752. (define-minor-mode orgstruct-mode
  5753. "Toggle the minor more `orgstruct-mode'.
  5754. This mode is for using Org-mode structure commands in other modes.
  5755. The following key behave as if Org-mode was active, if the cursor
  5756. is on a headline, or on a plain list item (both in the definition
  5757. of Org-mode).
  5758. M-up Move entry/item up
  5759. M-down Move entry/item down
  5760. M-left Promote
  5761. M-right Demote
  5762. M-S-up Move entry/item up
  5763. M-S-down Move entry/item down
  5764. M-S-left Promote subtree
  5765. M-S-right Demote subtree
  5766. M-q Fill paragraph and items like in Org-mode
  5767. C-c ^ Sort entries
  5768. C-c - Cycle list bullet
  5769. TAB Cycle item visibility
  5770. M-RET Insert new heading/item
  5771. S-M-RET Insert new TODO heading / Chekbox item
  5772. C-c C-c Set tags / toggle checkbox"
  5773. nil " OrgStruct" nil
  5774. (org-load-modules-maybe)
  5775. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5776. ;;;###autoload
  5777. (defun turn-on-orgstruct ()
  5778. "Unconditionally turn on `orgstruct-mode'."
  5779. (orgstruct-mode 1))
  5780. ;;;###autoload
  5781. (defun turn-on-orgstruct++ ()
  5782. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5783. In addition to setting orgstruct-mode, this also exports all indentation and
  5784. autofilling variables from org-mode into the buffer. Note that turning
  5785. off orgstruct-mode will *not* remove these additional settings."
  5786. (orgstruct-mode 1)
  5787. (let (var val)
  5788. (mapc
  5789. (lambda (x)
  5790. (when (string-match
  5791. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5792. (symbol-name (car x)))
  5793. (setq var (car x) val (nth 1 x))
  5794. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5795. org-local-vars)))
  5796. (defun orgstruct-error ()
  5797. "Error when there is no default binding for a structure key."
  5798. (interactive)
  5799. (error "This key has no function outside structure elements"))
  5800. (defun orgstruct-setup ()
  5801. "Setup orgstruct keymaps."
  5802. (let ((nfunc 0)
  5803. (bindings
  5804. (list
  5805. '([(meta up)] org-metaup)
  5806. '([(meta down)] org-metadown)
  5807. '([(meta left)] org-metaleft)
  5808. '([(meta right)] org-metaright)
  5809. '([(meta shift up)] org-shiftmetaup)
  5810. '([(meta shift down)] org-shiftmetadown)
  5811. '([(meta shift left)] org-shiftmetaleft)
  5812. '([(meta shift right)] org-shiftmetaright)
  5813. '([(shift up)] org-shiftup)
  5814. '([(shift down)] org-shiftdown)
  5815. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5816. '("\M-q" fill-paragraph)
  5817. '("\C-c^" org-sort)
  5818. '("\C-c-" org-cycle-list-bullet)))
  5819. elt key fun cmd)
  5820. (while (setq elt (pop bindings))
  5821. (setq nfunc (1+ nfunc))
  5822. (setq key (org-key (car elt))
  5823. fun (nth 1 elt)
  5824. cmd (orgstruct-make-binding fun nfunc key))
  5825. (org-defkey orgstruct-mode-map key cmd))
  5826. ;; Special treatment needed for TAB and RET
  5827. (org-defkey orgstruct-mode-map [(tab)]
  5828. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5829. (org-defkey orgstruct-mode-map "\C-i"
  5830. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5831. (org-defkey orgstruct-mode-map "\M-\C-m"
  5832. (orgstruct-make-binding 'org-insert-heading 105
  5833. "\M-\C-m" [(meta return)]))
  5834. (org-defkey orgstruct-mode-map [(meta return)]
  5835. (orgstruct-make-binding 'org-insert-heading 106
  5836. [(meta return)] "\M-\C-m"))
  5837. (org-defkey orgstruct-mode-map [(shift meta return)]
  5838. (orgstruct-make-binding 'org-insert-todo-heading 107
  5839. [(meta return)] "\M-\C-m"))
  5840. (unless org-local-vars
  5841. (setq org-local-vars (org-get-local-variables)))
  5842. t))
  5843. (defun orgstruct-make-binding (fun n &rest keys)
  5844. "Create a function for binding in the structure minor mode.
  5845. FUN is the command to call inside a table. N is used to create a unique
  5846. command name. KEYS are keys that should be checked in for a command
  5847. to execute outside of tables."
  5848. (eval
  5849. (list 'defun
  5850. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5851. '(arg)
  5852. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5853. "Outside of structure, run the binding of `"
  5854. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5855. "'.")
  5856. '(interactive "p")
  5857. (list 'if
  5858. '(org-context-p 'headline 'item)
  5859. (list 'org-run-like-in-org-mode (list 'quote fun))
  5860. (list 'let '(orgstruct-mode)
  5861. (list 'call-interactively
  5862. (append '(or)
  5863. (mapcar (lambda (k)
  5864. (list 'key-binding k))
  5865. keys)
  5866. '('orgstruct-error))))))))
  5867. (defun org-context-p (&rest contexts)
  5868. "Check if local context is any of CONTEXTS.
  5869. Possible values in the list of contexts are `table', `headline', and `item'."
  5870. (let ((pos (point)))
  5871. (goto-char (point-at-bol))
  5872. (prog1 (or (and (memq 'table contexts)
  5873. (looking-at "[ \t]*|"))
  5874. (and (memq 'headline contexts)
  5875. ;;????????? (looking-at "\\*+"))
  5876. (looking-at outline-regexp))
  5877. (and (memq 'item contexts)
  5878. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5879. (goto-char pos))))
  5880. (defun org-get-local-variables ()
  5881. "Return a list of all local variables in an org-mode buffer."
  5882. (let (varlist)
  5883. (with-current-buffer (get-buffer-create "*Org tmp*")
  5884. (erase-buffer)
  5885. (org-mode)
  5886. (setq varlist (buffer-local-variables)))
  5887. (kill-buffer "*Org tmp*")
  5888. (delq nil
  5889. (mapcar
  5890. (lambda (x)
  5891. (setq x
  5892. (if (symbolp x)
  5893. (list x)
  5894. (list (car x) (list 'quote (cdr x)))))
  5895. (if (string-match
  5896. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5897. (symbol-name (car x)))
  5898. x nil))
  5899. varlist))))
  5900. ;;;###autoload
  5901. (defun org-run-like-in-org-mode (cmd)
  5902. (org-load-modules-maybe)
  5903. (unless org-local-vars
  5904. (setq org-local-vars (org-get-local-variables)))
  5905. (eval (list 'let org-local-vars
  5906. (list 'call-interactively (list 'quote cmd)))))
  5907. ;;;; Archiving
  5908. (defun org-get-category (&optional pos)
  5909. "Get the category applying to position POS."
  5910. (get-text-property (or pos (point)) 'org-category))
  5911. (defun org-refresh-category-properties ()
  5912. "Refresh category text properties in the buffer."
  5913. (let ((def-cat (cond
  5914. ((null org-category)
  5915. (if buffer-file-name
  5916. (file-name-sans-extension
  5917. (file-name-nondirectory buffer-file-name))
  5918. "???"))
  5919. ((symbolp org-category) (symbol-name org-category))
  5920. (t org-category)))
  5921. beg end cat pos optionp)
  5922. (org-unmodified
  5923. (save-excursion
  5924. (save-restriction
  5925. (widen)
  5926. (goto-char (point-min))
  5927. (put-text-property (point) (point-max) 'org-category def-cat)
  5928. (while (re-search-forward
  5929. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5930. (setq pos (match-end 0)
  5931. optionp (equal (char-after (match-beginning 0)) ?#)
  5932. cat (org-trim (match-string 2)))
  5933. (if optionp
  5934. (setq beg (point-at-bol) end (point-max))
  5935. (org-back-to-heading t)
  5936. (setq beg (point) end (org-end-of-subtree t t)))
  5937. (put-text-property beg end 'org-category cat)
  5938. (goto-char pos)))))))
  5939. ;;;; Link Stuff
  5940. ;;; Link abbreviations
  5941. (defun org-link-expand-abbrev (link)
  5942. "Apply replacements as defined in `org-link-abbrev-alist."
  5943. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5944. (let* ((key (match-string 1 link))
  5945. (as (or (assoc key org-link-abbrev-alist-local)
  5946. (assoc key org-link-abbrev-alist)))
  5947. (tag (and (match-end 2) (match-string 3 link)))
  5948. rpl)
  5949. (if (not as)
  5950. link
  5951. (setq rpl (cdr as))
  5952. (cond
  5953. ((symbolp rpl) (funcall rpl tag))
  5954. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5955. (t (concat rpl tag)))))
  5956. link))
  5957. ;;; Storing and inserting links
  5958. (defvar org-insert-link-history nil
  5959. "Minibuffer history for links inserted with `org-insert-link'.")
  5960. (defvar org-stored-links nil
  5961. "Contains the links stored with `org-store-link'.")
  5962. (defvar org-store-link-plist nil
  5963. "Plist with info about the most recently link created with `org-store-link'.")
  5964. (defvar org-link-protocols nil
  5965. "Link protocols added to Org-mode using `org-add-link-type'.")
  5966. (defvar org-store-link-functions nil
  5967. "List of functions that are called to create and store a link.
  5968. Each function will be called in turn until one returns a non-nil
  5969. value. Each function should check if it is responsible for creating
  5970. this link (for example by looking at the major mode).
  5971. If not, it must exit and return nil.
  5972. If yes, it should return a non-nil value after a calling
  5973. `org-store-link-props' with a list of properties and values.
  5974. Special properties are:
  5975. :type The link prefix. like \"http\". This must be given.
  5976. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5977. This is obligatory as well.
  5978. :description Optional default description for the second pair
  5979. of brackets in an Org-mode link. The user can still change
  5980. this when inserting this link into an Org-mode buffer.
  5981. In addition to these, any additional properties can be specified
  5982. and then used in remember templates.")
  5983. (defun org-add-link-type (type &optional follow export)
  5984. "Add TYPE to the list of `org-link-types'.
  5985. Re-compute all regular expressions depending on `org-link-types'
  5986. FOLLOW and EXPORT are two functions.
  5987. FOLLOW should take the link path as the single argument and do whatever
  5988. is necessary to follow the link, for example find a file or display
  5989. a mail message.
  5990. EXPORT should format the link path for export to one of the export formats.
  5991. It should be a function accepting three arguments:
  5992. path the path of the link, the text after the prefix (like \"http:\")
  5993. desc the description of the link, if any, nil if there was no descripton
  5994. format the export format, a symbol like `html' or `latex'.
  5995. The function may use the FORMAT information to return different values
  5996. depending on the format. The return value will be put literally into
  5997. the exported file.
  5998. Org-mode has a built-in default for exporting links. If you are happy with
  5999. this default, there is no need to define an export function for the link
  6000. type. For a simple example of an export function, see `org-bbdb.el'."
  6001. (add-to-list 'org-link-types type t)
  6002. (org-make-link-regexps)
  6003. (if (assoc type org-link-protocols)
  6004. (setcdr (assoc type org-link-protocols) (list follow export))
  6005. (push (list type follow export) org-link-protocols)))
  6006. ;;;###autoload
  6007. (defun org-store-link (arg)
  6008. "\\<org-mode-map>Store an org-link to the current location.
  6009. This link is added to `org-stored-links' and can later be inserted
  6010. into an org-buffer with \\[org-insert-link].
  6011. For some link types, a prefix arg is interpreted:
  6012. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6013. For file links, arg negates `org-context-in-file-links'."
  6014. (interactive "P")
  6015. (org-load-modules-maybe)
  6016. (setq org-store-link-plist nil) ; reset
  6017. (let (link cpltxt desc description search txt)
  6018. (cond
  6019. ((run-hook-with-args-until-success 'org-store-link-functions)
  6020. (setq link (plist-get org-store-link-plist :link)
  6021. desc (or (plist-get org-store-link-plist :description) link)))
  6022. ((eq major-mode 'calendar-mode)
  6023. (let ((cd (calendar-cursor-to-date)))
  6024. (setq link
  6025. (format-time-string
  6026. (car org-time-stamp-formats)
  6027. (apply 'encode-time
  6028. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6029. nil nil nil))))
  6030. (org-store-link-props :type "calendar" :date cd)))
  6031. ((eq major-mode 'w3-mode)
  6032. (setq cpltxt (url-view-url t)
  6033. link (org-make-link cpltxt))
  6034. (org-store-link-props :type "w3" :url (url-view-url t)))
  6035. ((eq major-mode 'w3m-mode)
  6036. (setq cpltxt (or w3m-current-title w3m-current-url)
  6037. link (org-make-link w3m-current-url))
  6038. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6039. ((setq search (run-hook-with-args-until-success
  6040. 'org-create-file-search-functions))
  6041. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6042. "::" search))
  6043. (setq cpltxt (or description link)))
  6044. ((eq major-mode 'image-mode)
  6045. (setq cpltxt (concat "file:"
  6046. (abbreviate-file-name buffer-file-name))
  6047. link (org-make-link cpltxt))
  6048. (org-store-link-props :type "image" :file buffer-file-name))
  6049. ((eq major-mode 'dired-mode)
  6050. ;; link to the file in the current line
  6051. (setq cpltxt (concat "file:"
  6052. (abbreviate-file-name
  6053. (expand-file-name
  6054. (dired-get-filename nil t))))
  6055. link (org-make-link cpltxt)))
  6056. ((and buffer-file-name (org-mode-p))
  6057. ;; Just link to current headline
  6058. (setq cpltxt (concat "file:"
  6059. (abbreviate-file-name buffer-file-name)))
  6060. ;; Add a context search string
  6061. (when (org-xor org-context-in-file-links arg)
  6062. ;; Check if we are on a target
  6063. (if (org-in-regexp "<<\\(.*?\\)>>")
  6064. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6065. (setq txt (cond
  6066. ((org-on-heading-p) nil)
  6067. ((org-region-active-p)
  6068. (buffer-substring (region-beginning) (region-end)))
  6069. (t nil)))
  6070. (when (or (null txt) (string-match "\\S-" txt))
  6071. (setq cpltxt
  6072. (concat cpltxt "::"
  6073. (condition-case nil
  6074. (org-make-org-heading-search-string txt)
  6075. (error "")))
  6076. desc "NONE"))))
  6077. (if (string-match "::\\'" cpltxt)
  6078. (setq cpltxt (substring cpltxt 0 -2)))
  6079. (setq link (org-make-link cpltxt)))
  6080. ((buffer-file-name (buffer-base-buffer))
  6081. ;; Just link to this file here.
  6082. (setq cpltxt (concat "file:"
  6083. (abbreviate-file-name
  6084. (buffer-file-name (buffer-base-buffer)))))
  6085. ;; Add a context string
  6086. (when (org-xor org-context-in-file-links arg)
  6087. (setq txt (if (org-region-active-p)
  6088. (buffer-substring (region-beginning) (region-end))
  6089. (buffer-substring (point-at-bol) (point-at-eol))))
  6090. ;; Only use search option if there is some text.
  6091. (when (string-match "\\S-" txt)
  6092. (setq cpltxt
  6093. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6094. desc "NONE")))
  6095. (setq link (org-make-link cpltxt)))
  6096. ((interactive-p)
  6097. (error "Cannot link to a buffer which is not visiting a file"))
  6098. (t (setq link nil)))
  6099. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6100. (setq link (or link cpltxt)
  6101. desc (or desc cpltxt))
  6102. (if (equal desc "NONE") (setq desc nil))
  6103. (if (and (interactive-p) link)
  6104. (progn
  6105. (setq org-stored-links
  6106. (cons (list link desc) org-stored-links))
  6107. (message "Stored: %s" (or desc link)))
  6108. (and link (org-make-link-string link desc)))))
  6109. (defun org-store-link-props (&rest plist)
  6110. "Store link properties, extract names and addresses."
  6111. (let (x adr)
  6112. (when (setq x (plist-get plist :from))
  6113. (setq adr (mail-extract-address-components x))
  6114. (plist-put plist :fromname (car adr))
  6115. (plist-put plist :fromaddress (nth 1 adr)))
  6116. (when (setq x (plist-get plist :to))
  6117. (setq adr (mail-extract-address-components x))
  6118. (plist-put plist :toname (car adr))
  6119. (plist-put plist :toaddress (nth 1 adr))))
  6120. (let ((from (plist-get plist :from))
  6121. (to (plist-get plist :to)))
  6122. (when (and from to org-from-is-user-regexp)
  6123. (plist-put plist :fromto
  6124. (if (string-match org-from-is-user-regexp from)
  6125. (concat "to %t")
  6126. (concat "from %f")))))
  6127. (setq org-store-link-plist plist))
  6128. (defun org-add-link-props (&rest plist)
  6129. "Add these properties to the link property list."
  6130. (let (key value)
  6131. (while plist
  6132. (setq key (pop plist) value (pop plist))
  6133. (setq org-store-link-plist
  6134. (plist-put org-store-link-plist key value)))))
  6135. (defun org-email-link-description (&optional fmt)
  6136. "Return the description part of an email link.
  6137. This takes information from `org-store-link-plist' and formats it
  6138. according to FMT (default from `org-email-link-description-format')."
  6139. (setq fmt (or fmt org-email-link-description-format))
  6140. (let* ((p org-store-link-plist)
  6141. (to (plist-get p :toaddress))
  6142. (from (plist-get p :fromaddress))
  6143. (table
  6144. (list
  6145. (cons "%c" (plist-get p :fromto))
  6146. (cons "%F" (plist-get p :from))
  6147. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6148. (cons "%T" (plist-get p :to))
  6149. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6150. (cons "%s" (plist-get p :subject))
  6151. (cons "%m" (plist-get p :message-id)))))
  6152. (when (string-match "%c" fmt)
  6153. ;; Check if the user wrote this message
  6154. (if (and org-from-is-user-regexp from to
  6155. (save-match-data (string-match org-from-is-user-regexp from)))
  6156. (setq fmt (replace-match "to %t" t t fmt))
  6157. (setq fmt (replace-match "from %f" t t fmt))))
  6158. (org-replace-escapes fmt table)))
  6159. (defun org-make-org-heading-search-string (&optional string heading)
  6160. "Make search string for STRING or current headline."
  6161. (interactive)
  6162. (let ((s (or string (org-get-heading))))
  6163. (unless (and string (not heading))
  6164. ;; We are using a headline, clean up garbage in there.
  6165. (if (string-match org-todo-regexp s)
  6166. (setq s (replace-match "" t t s)))
  6167. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6168. (setq s (replace-match "" t t s)))
  6169. (setq s (org-trim s))
  6170. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6171. org-comment-string "\\)") s)
  6172. (setq s (replace-match "" t t s)))
  6173. (while (string-match org-ts-regexp s)
  6174. (setq s (replace-match "" t t s))))
  6175. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6176. (setq s (replace-match " " t t s)))
  6177. (or string (setq s (concat "*" s))) ; Add * for headlines
  6178. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6179. (defun org-make-link (&rest strings)
  6180. "Concatenate STRINGS."
  6181. (apply 'concat strings))
  6182. (defun org-make-link-string (link &optional description)
  6183. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6184. (unless (string-match "\\S-" link)
  6185. (error "Empty link"))
  6186. (when (stringp description)
  6187. ;; Remove brackets from the description, they are fatal.
  6188. (while (string-match "\\[" description)
  6189. (setq description (replace-match "{" t t description)))
  6190. (while (string-match "\\]" description)
  6191. (setq description (replace-match "}" t t description))))
  6192. (when (equal (org-link-escape link) description)
  6193. ;; No description needed, it is identical
  6194. (setq description nil))
  6195. (when (and (not description)
  6196. (not (equal link (org-link-escape link))))
  6197. (setq description (org-extract-attributes link)))
  6198. (concat "[[" (org-link-escape link) "]"
  6199. (if description (concat "[" description "]") "")
  6200. "]"))
  6201. (defconst org-link-escape-chars
  6202. '((?\ . "%20")
  6203. (?\[ . "%5B")
  6204. (?\] . "%5D")
  6205. (?\340 . "%E0") ; `a
  6206. (?\342 . "%E2") ; ^a
  6207. (?\347 . "%E7") ; ,c
  6208. (?\350 . "%E8") ; `e
  6209. (?\351 . "%E9") ; 'e
  6210. (?\352 . "%EA") ; ^e
  6211. (?\356 . "%EE") ; ^i
  6212. (?\364 . "%F4") ; ^o
  6213. (?\371 . "%F9") ; `u
  6214. (?\373 . "%FB") ; ^u
  6215. (?\; . "%3B")
  6216. (?? . "%3F")
  6217. (?= . "%3D")
  6218. (?+ . "%2B")
  6219. )
  6220. "Association list of escapes for some characters problematic in links.
  6221. This is the list that is used for internal purposes.")
  6222. (defconst org-link-escape-chars-browser
  6223. '((?\ . "%20")) ; 32 for the SPC char
  6224. "Association list of escapes for some characters problematic in links.
  6225. This is the list that is used before handing over to the browser.")
  6226. (defun org-link-escape (text &optional table)
  6227. "Escape charaters in TEXT that are problematic for links."
  6228. (setq table (or table org-link-escape-chars))
  6229. (when text
  6230. (let ((re (mapconcat (lambda (x) (regexp-quote
  6231. (char-to-string (car x))))
  6232. table "\\|")))
  6233. (while (string-match re text)
  6234. (setq text
  6235. (replace-match
  6236. (cdr (assoc (string-to-char (match-string 0 text))
  6237. table))
  6238. t t text)))
  6239. text)))
  6240. (defun org-link-unescape (text &optional table)
  6241. "Reverse the action of `org-link-escape'."
  6242. (setq table (or table org-link-escape-chars))
  6243. (when text
  6244. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6245. table "\\|")))
  6246. (while (string-match re text)
  6247. (setq text
  6248. (replace-match
  6249. (char-to-string (car (rassoc (match-string 0 text) table)))
  6250. t t text)))
  6251. text)))
  6252. (defun org-xor (a b)
  6253. "Exclusive or."
  6254. (if a (not b) b))
  6255. (defun org-get-header (header)
  6256. "Find a header field in the current buffer."
  6257. (save-excursion
  6258. (goto-char (point-min))
  6259. (let ((case-fold-search t) s)
  6260. (cond
  6261. ((eq header 'from)
  6262. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6263. (setq s (match-string 1)))
  6264. (while (string-match "\"" s)
  6265. (setq s (replace-match "" t t s)))
  6266. (if (string-match "[<(].*" s)
  6267. (setq s (replace-match "" t t s))))
  6268. ((eq header 'message-id)
  6269. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6270. (setq s (match-string 1))))
  6271. ((eq header 'subject)
  6272. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6273. (setq s (match-string 1)))))
  6274. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6275. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6276. s)))
  6277. (defun org-fixup-message-id-for-http (s)
  6278. "Replace special characters in a message id, so it can be used in an http query."
  6279. (while (string-match "<" s)
  6280. (setq s (replace-match "%3C" t t s)))
  6281. (while (string-match ">" s)
  6282. (setq s (replace-match "%3E" t t s)))
  6283. (while (string-match "@" s)
  6284. (setq s (replace-match "%40" t t s)))
  6285. s)
  6286. ;;;###autoload
  6287. (defun org-insert-link-global ()
  6288. "Insert a link like Org-mode does.
  6289. This command can be called in any mode to insert a link in Org-mode syntax."
  6290. (interactive)
  6291. (org-load-modules-maybe)
  6292. (org-run-like-in-org-mode 'org-insert-link))
  6293. (defun org-insert-link (&optional complete-file link-location)
  6294. "Insert a link. At the prompt, enter the link.
  6295. Completion can be used to select a link previously stored with
  6296. `org-store-link'. When the empty string is entered (i.e. if you just
  6297. press RET at the prompt), the link defaults to the most recently
  6298. stored link. As SPC triggers completion in the minibuffer, you need to
  6299. use M-SPC or C-q SPC to force the insertion of a space character.
  6300. You will also be prompted for a description, and if one is given, it will
  6301. be displayed in the buffer instead of the link.
  6302. If there is already a link at point, this command will allow you to edit link
  6303. and description parts.
  6304. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6305. be selected using completion. The path to the file will be relative to the
  6306. current directory if the file is in the current directory or a subdirectory.
  6307. Otherwise, the link will be the absolute path as completed in the minibuffer
  6308. \(i.e. normally ~/path/to/file).
  6309. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6310. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  6311. of `org-keep-stored-link-after-insertion'.
  6312. If `org-make-link-description-function' is non-nil, this function will be
  6313. called with the link target, and the result will be the default
  6314. link description.
  6315. If the LINK-LOCATION parameter is non-nil, this value will be
  6316. used as the link location instead of reading one interactively."
  6317. (interactive "P")
  6318. (let* ((wcf (current-window-configuration))
  6319. (region (if (org-region-active-p)
  6320. (buffer-substring (region-beginning) (region-end))))
  6321. (remove (and region (list (region-beginning) (region-end))))
  6322. (desc region)
  6323. tmphist ; byte-compile incorrectly complains about this
  6324. (link link-location)
  6325. entry file)
  6326. (cond
  6327. (link-location) ; specified by arg, just use it.
  6328. ((org-in-regexp org-bracket-link-regexp 1)
  6329. ;; We do have a link at point, and we are going to edit it.
  6330. (setq remove (list (match-beginning 0) (match-end 0)))
  6331. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6332. (setq link (read-string "Link: "
  6333. (org-link-unescape
  6334. (org-match-string-no-properties 1)))))
  6335. ((or (org-in-regexp org-angle-link-re)
  6336. (org-in-regexp org-plain-link-re))
  6337. ;; Convert to bracket link
  6338. (setq remove (list (match-beginning 0) (match-end 0))
  6339. link (read-string "Link: "
  6340. (org-remove-angle-brackets (match-string 0)))))
  6341. ((equal complete-file '(4))
  6342. ;; Completing read for file names.
  6343. (setq file (read-file-name "File: "))
  6344. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6345. (pwd1 (file-name-as-directory (abbreviate-file-name
  6346. (expand-file-name ".")))))
  6347. (cond
  6348. ((equal complete-file '(16))
  6349. (setq link (org-make-link
  6350. "file:"
  6351. (abbreviate-file-name (expand-file-name file)))))
  6352. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6353. (setq link (org-make-link "file:" (match-string 1 file))))
  6354. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6355. (expand-file-name file))
  6356. (setq link (org-make-link
  6357. "file:" (match-string 1 (expand-file-name file)))))
  6358. (t (setq link (org-make-link "file:" file))))))
  6359. (t
  6360. ;; Read link, with completion for stored links.
  6361. (with-output-to-temp-buffer "*Org Links*"
  6362. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6363. (when org-stored-links
  6364. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6365. (princ (mapconcat
  6366. (lambda (x)
  6367. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6368. (reverse org-stored-links) "\n"))))
  6369. (let ((cw (selected-window)))
  6370. (select-window (get-buffer-window "*Org Links*"))
  6371. (shrink-window-if-larger-than-buffer)
  6372. (setq truncate-lines t)
  6373. (select-window cw))
  6374. ;; Fake a link history, containing the stored links.
  6375. (setq tmphist (append (mapcar 'car org-stored-links)
  6376. org-insert-link-history))
  6377. (unwind-protect
  6378. (setq link (org-completing-read
  6379. "Link: "
  6380. (append
  6381. (mapcar (lambda (x) (list (concat (car x) ":")))
  6382. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6383. (mapcar (lambda (x) (list (concat x ":")))
  6384. org-link-types))
  6385. nil nil nil
  6386. 'tmphist
  6387. (or (car (car org-stored-links)))))
  6388. (set-window-configuration wcf)
  6389. (kill-buffer "*Org Links*"))
  6390. (setq entry (assoc link org-stored-links))
  6391. (or entry (push link org-insert-link-history))
  6392. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6393. (not org-keep-stored-link-after-insertion))
  6394. (setq org-stored-links (delq (assoc link org-stored-links)
  6395. org-stored-links)))
  6396. (setq desc (or desc (nth 1 entry)))))
  6397. (if (string-match org-plain-link-re link)
  6398. ;; URL-like link, normalize the use of angular brackets.
  6399. (setq link (org-make-link (org-remove-angle-brackets link))))
  6400. ;; Check if we are linking to the current file with a search option
  6401. ;; If yes, simplify the link by using only the search option.
  6402. (when (and buffer-file-name
  6403. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6404. (let* ((path (match-string 1 link))
  6405. (case-fold-search nil)
  6406. (search (match-string 2 link)))
  6407. (save-match-data
  6408. (if (equal (file-truename buffer-file-name) (file-truename path))
  6409. ;; We are linking to this same file, with a search option
  6410. (setq link search)))))
  6411. ;; Check if we can/should use a relative path. If yes, simplify the link
  6412. (when (string-match "\\<file:\\(.*\\)" link)
  6413. (let* ((path (match-string 1 link))
  6414. (origpath path)
  6415. (case-fold-search nil))
  6416. (cond
  6417. ((eq org-link-file-path-type 'absolute)
  6418. (setq path (abbreviate-file-name (expand-file-name path))))
  6419. ((eq org-link-file-path-type 'noabbrev)
  6420. (setq path (expand-file-name path)))
  6421. ((eq org-link-file-path-type 'relative)
  6422. (setq path (file-relative-name path)))
  6423. (t
  6424. (save-match-data
  6425. (if (string-match (concat "^" (regexp-quote
  6426. (file-name-as-directory
  6427. (expand-file-name "."))))
  6428. (expand-file-name path))
  6429. ;; We are linking a file with relative path name.
  6430. (setq path (substring (expand-file-name path)
  6431. (match-end 0)))))))
  6432. (setq link (concat "file:" path))
  6433. (if (equal desc origpath)
  6434. (setq desc path))))
  6435. (if org-make-link-description-function
  6436. (setq desc (funcall org-make-link-description-function link desc)))
  6437. (setq desc (read-string "Description: " desc))
  6438. (unless (string-match "\\S-" desc) (setq desc nil))
  6439. (if remove (apply 'delete-region remove))
  6440. (insert (org-make-link-string link desc))))
  6441. (defun org-completing-read (&rest args)
  6442. (let ((minibuffer-local-completion-map
  6443. (copy-keymap minibuffer-local-completion-map)))
  6444. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6445. (apply 'completing-read args)))
  6446. (defun org-extract-attributes (s)
  6447. "Extract the attributes cookie from a string and set as text property."
  6448. (let (a attr (start 0) key value)
  6449. (save-match-data
  6450. (when (string-match "{{\\([^}]+\\)}}$" s)
  6451. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6452. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6453. (setq key (match-string 1 a) value (match-string 2 a)
  6454. start (match-end 0)
  6455. attr (plist-put attr (intern key) value))))
  6456. (org-add-props s nil 'org-attributes attr))
  6457. s))
  6458. (defun org-attributes-to-string (plist)
  6459. "Format a property list into an HTML attribute list."
  6460. (let ((s "") key value)
  6461. (while plist
  6462. (setq key (pop plist) value (pop plist))
  6463. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  6464. s))
  6465. ;;; Opening/following a link
  6466. (defvar org-link-search-failed nil)
  6467. (defun org-next-link ()
  6468. "Move forward to the next link.
  6469. If the link is in hidden text, expose it."
  6470. (interactive)
  6471. (when (and org-link-search-failed (eq this-command last-command))
  6472. (goto-char (point-min))
  6473. (message "Link search wrapped back to beginning of buffer"))
  6474. (setq org-link-search-failed nil)
  6475. (let* ((pos (point))
  6476. (ct (org-context))
  6477. (a (assoc :link ct)))
  6478. (if a (goto-char (nth 2 a)))
  6479. (if (re-search-forward org-any-link-re nil t)
  6480. (progn
  6481. (goto-char (match-beginning 0))
  6482. (if (org-invisible-p) (org-show-context)))
  6483. (goto-char pos)
  6484. (setq org-link-search-failed t)
  6485. (error "No further link found"))))
  6486. (defun org-previous-link ()
  6487. "Move backward to the previous link.
  6488. If the link is in hidden text, expose it."
  6489. (interactive)
  6490. (when (and org-link-search-failed (eq this-command last-command))
  6491. (goto-char (point-max))
  6492. (message "Link search wrapped back to end of buffer"))
  6493. (setq org-link-search-failed nil)
  6494. (let* ((pos (point))
  6495. (ct (org-context))
  6496. (a (assoc :link ct)))
  6497. (if a (goto-char (nth 1 a)))
  6498. (if (re-search-backward org-any-link-re nil t)
  6499. (progn
  6500. (goto-char (match-beginning 0))
  6501. (if (org-invisible-p) (org-show-context)))
  6502. (goto-char pos)
  6503. (setq org-link-search-failed t)
  6504. (error "No further link found"))))
  6505. (defun org-find-file-at-mouse (ev)
  6506. "Open file link or URL at mouse."
  6507. (interactive "e")
  6508. (mouse-set-point ev)
  6509. (org-open-at-point 'in-emacs))
  6510. (defun org-open-at-mouse (ev)
  6511. "Open file link or URL at mouse."
  6512. (interactive "e")
  6513. (mouse-set-point ev)
  6514. (org-open-at-point))
  6515. (defvar org-window-config-before-follow-link nil
  6516. "The window configuration before following a link.
  6517. This is saved in case the need arises to restore it.")
  6518. (defvar org-open-link-marker (make-marker)
  6519. "Marker pointing to the location where `org-open-at-point; was called.")
  6520. ;;;###autoload
  6521. (defun org-open-at-point-global ()
  6522. "Follow a link like Org-mode does.
  6523. This command can be called in any mode to follow a link that has
  6524. Org-mode syntax."
  6525. (interactive)
  6526. (org-run-like-in-org-mode 'org-open-at-point))
  6527. ;;;###autoload
  6528. (defun org-open-link-from-string (s &optional arg)
  6529. "Open a link in the string S, as if it was in Org-mode."
  6530. (interactive "sLink: \nP")
  6531. (with-temp-buffer
  6532. (let ((org-inhibit-startup t))
  6533. (org-mode)
  6534. (insert s)
  6535. (goto-char (point-min))
  6536. (org-open-at-point arg))))
  6537. (defun org-open-at-point (&optional in-emacs)
  6538. "Open link at or after point.
  6539. If there is no link at point, this function will search forward up to
  6540. the end of the current subtree.
  6541. Normally, files will be opened by an appropriate application. If the
  6542. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6543. (interactive "P")
  6544. (org-load-modules-maybe)
  6545. (move-marker org-open-link-marker (point))
  6546. (setq org-window-config-before-follow-link (current-window-configuration))
  6547. (org-remove-occur-highlights nil nil t)
  6548. (if (org-at-timestamp-p t)
  6549. (org-follow-timestamp-link)
  6550. (let (type path link line search (pos (point)))
  6551. (catch 'match
  6552. (save-excursion
  6553. (skip-chars-forward "^]\n\r")
  6554. (when (org-in-regexp org-bracket-link-regexp)
  6555. (setq link (org-extract-attributes
  6556. (org-link-unescape (org-match-string-no-properties 1))))
  6557. (while (string-match " *\n *" link)
  6558. (setq link (replace-match " " t t link)))
  6559. (setq link (org-link-expand-abbrev link))
  6560. (cond
  6561. ((or (file-name-absolute-p link)
  6562. (string-match "^\\.\\.?/" link))
  6563. (setq type "file" path link))
  6564. ((string-match org-link-re-with-space2 link)
  6565. (setq type (match-string 1 link) path (match-string 2 link)))
  6566. (t (setq type "thisfile" path link)))
  6567. (throw 'match t)))
  6568. (when (get-text-property (point) 'org-linked-text)
  6569. (setq type "thisfile"
  6570. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6571. (1+ (point)) (point))
  6572. path (buffer-substring
  6573. (previous-single-property-change pos 'org-linked-text)
  6574. (next-single-property-change pos 'org-linked-text)))
  6575. (throw 'match t))
  6576. (save-excursion
  6577. (when (or (org-in-regexp org-angle-link-re)
  6578. (org-in-regexp org-plain-link-re))
  6579. (setq type (match-string 1) path (match-string 2))
  6580. (throw 'match t)))
  6581. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6582. (setq type "tree-match"
  6583. path (match-string 1))
  6584. (throw 'match t))
  6585. (save-excursion
  6586. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6587. (setq type "tags"
  6588. path (match-string 1))
  6589. (while (string-match ":" path)
  6590. (setq path (replace-match "+" t t path)))
  6591. (throw 'match t))))
  6592. (unless path
  6593. (error "No link found"))
  6594. ;; Remove any trailing spaces in path
  6595. (if (string-match " +\\'" path)
  6596. (setq path (replace-match "" t t path)))
  6597. (cond
  6598. ((assoc type org-link-protocols)
  6599. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6600. ((equal type "mailto")
  6601. (let ((cmd (car org-link-mailto-program))
  6602. (args (cdr org-link-mailto-program)) args1
  6603. (address path) (subject "") a)
  6604. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6605. (setq address (match-string 1 path)
  6606. subject (org-link-escape (match-string 2 path))))
  6607. (while args
  6608. (cond
  6609. ((not (stringp (car args))) (push (pop args) args1))
  6610. (t (setq a (pop args))
  6611. (if (string-match "%a" a)
  6612. (setq a (replace-match address t t a)))
  6613. (if (string-match "%s" a)
  6614. (setq a (replace-match subject t t a)))
  6615. (push a args1))))
  6616. (apply cmd (nreverse args1))))
  6617. ((member type '("http" "https" "ftp" "news"))
  6618. (browse-url (concat type ":" (org-link-escape
  6619. path org-link-escape-chars-browser))))
  6620. ((member type '("message"))
  6621. (browse-url (concat type ":" path)))
  6622. ((string= type "tags")
  6623. (org-tags-view in-emacs path))
  6624. ((string= type "thisfile")
  6625. (if in-emacs
  6626. (switch-to-buffer-other-window
  6627. (org-get-buffer-for-internal-link (current-buffer)))
  6628. (org-mark-ring-push))
  6629. (let ((cmd `(org-link-search
  6630. ,path
  6631. ,(cond ((equal in-emacs '(4)) 'occur)
  6632. ((equal in-emacs '(16)) 'org-occur)
  6633. (t nil))
  6634. ,pos)))
  6635. (condition-case nil (eval cmd)
  6636. (error (progn (widen) (eval cmd))))))
  6637. ((string= type "tree-match")
  6638. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6639. ((string= type "file")
  6640. (if (string-match "::\\([0-9]+\\)\\'" path)
  6641. (setq line (string-to-number (match-string 1 path))
  6642. path (substring path 0 (match-beginning 0)))
  6643. (if (string-match "::\\(.+\\)\\'" path)
  6644. (setq search (match-string 1 path)
  6645. path (substring path 0 (match-beginning 0)))))
  6646. (if (string-match "[*?{]" (file-name-nondirectory path))
  6647. (dired path)
  6648. (org-open-file path in-emacs line search)))
  6649. ((string= type "news")
  6650. (require 'org-gnus)
  6651. (org-gnus-follow-link path))
  6652. ((string= type "shell")
  6653. (let ((cmd path))
  6654. (if (or (not org-confirm-shell-link-function)
  6655. (funcall org-confirm-shell-link-function
  6656. (format "Execute \"%s\" in shell? "
  6657. (org-add-props cmd nil
  6658. 'face 'org-warning))))
  6659. (progn
  6660. (message "Executing %s" cmd)
  6661. (shell-command cmd))
  6662. (error "Abort"))))
  6663. ((string= type "elisp")
  6664. (let ((cmd path))
  6665. (if (or (not org-confirm-elisp-link-function)
  6666. (funcall org-confirm-elisp-link-function
  6667. (format "Execute \"%s\" as elisp? "
  6668. (org-add-props cmd nil
  6669. 'face 'org-warning))))
  6670. (message "%s => %s" cmd (eval (read cmd)))
  6671. (error "Abort"))))
  6672. (t
  6673. (browse-url-at-point)))))
  6674. (move-marker org-open-link-marker nil)
  6675. (run-hook-with-args 'org-follow-link-hook))
  6676. ;;;; Time estimates
  6677. (defun org-get-effort (&optional pom)
  6678. "Get the effort estimate for the current entry."
  6679. (org-entry-get pom org-effort-property))
  6680. ;;; File search
  6681. (defvar org-create-file-search-functions nil
  6682. "List of functions to construct the right search string for a file link.
  6683. These functions are called in turn with point at the location to
  6684. which the link should point.
  6685. A function in the hook should first test if it would like to
  6686. handle this file type, for example by checking the major-mode or
  6687. the file extension. If it decides not to handle this file, it
  6688. should just return nil to give other functions a chance. If it
  6689. does handle the file, it must return the search string to be used
  6690. when following the link. The search string will be part of the
  6691. file link, given after a double colon, and `org-open-at-point'
  6692. will automatically search for it. If special measures must be
  6693. taken to make the search successful, another function should be
  6694. added to the companion hook `org-execute-file-search-functions',
  6695. which see.
  6696. A function in this hook may also use `setq' to set the variable
  6697. `description' to provide a suggestion for the descriptive text to
  6698. be used for this link when it gets inserted into an Org-mode
  6699. buffer with \\[org-insert-link].")
  6700. (defvar org-execute-file-search-functions nil
  6701. "List of functions to execute a file search triggered by a link.
  6702. Functions added to this hook must accept a single argument, the
  6703. search string that was part of the file link, the part after the
  6704. double colon. The function must first check if it would like to
  6705. handle this search, for example by checking the major-mode or the
  6706. file extension. If it decides not to handle this search, it
  6707. should just return nil to give other functions a chance. If it
  6708. does handle the search, it must return a non-nil value to keep
  6709. other functions from trying.
  6710. Each function can access the current prefix argument through the
  6711. variable `current-prefix-argument'. Note that a single prefix is
  6712. used to force opening a link in Emacs, so it may be good to only
  6713. use a numeric or double prefix to guide the search function.
  6714. In case this is needed, a function in this hook can also restore
  6715. the window configuration before `org-open-at-point' was called using:
  6716. (set-window-configuration org-window-config-before-follow-link)")
  6717. (defun org-link-search (s &optional type avoid-pos)
  6718. "Search for a link search option.
  6719. If S is surrounded by forward slashes, it is interpreted as a
  6720. regular expression. In org-mode files, this will create an `org-occur'
  6721. sparse tree. In ordinary files, `occur' will be used to list matches.
  6722. If the current buffer is in `dired-mode', grep will be used to search
  6723. in all files. If AVOID-POS is given, ignore matches near that position."
  6724. (let ((case-fold-search t)
  6725. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6726. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6727. (append '(("") (" ") ("\t") ("\n"))
  6728. org-emphasis-alist)
  6729. "\\|") "\\)"))
  6730. (pos (point))
  6731. (pre nil) (post nil)
  6732. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6733. (cond
  6734. ;; First check if there are any special
  6735. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6736. ;; Now try the builtin stuff
  6737. ((save-excursion
  6738. (goto-char (point-min))
  6739. (and
  6740. (re-search-forward
  6741. (concat "<<" (regexp-quote s0) ">>") nil t)
  6742. (setq type 'dedicated
  6743. pos (match-beginning 0))))
  6744. ;; There is an exact target for this
  6745. (goto-char pos))
  6746. ((string-match "^/\\(.*\\)/$" s)
  6747. ;; A regular expression
  6748. (cond
  6749. ((org-mode-p)
  6750. (org-occur (match-string 1 s)))
  6751. ;;((eq major-mode 'dired-mode)
  6752. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6753. (t (org-do-occur (match-string 1 s)))))
  6754. (t
  6755. ;; A normal search strings
  6756. (when (equal (string-to-char s) ?*)
  6757. ;; Anchor on headlines, post may include tags.
  6758. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6759. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6760. s (substring s 1)))
  6761. (remove-text-properties
  6762. 0 (length s)
  6763. '(face nil mouse-face nil keymap nil fontified nil) s)
  6764. ;; Make a series of regular expressions to find a match
  6765. (setq words (org-split-string s "[ \n\r\t]+")
  6766. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6767. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6768. "\\)" markers)
  6769. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6770. re2a (concat "[ \t\r\n]" re2a_)
  6771. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6772. re4 (concat "[^a-zA-Z_]" re4_)
  6773. re1 (concat pre re2 post)
  6774. re3 (concat pre (if pre re4_ re4) post)
  6775. re5 (concat pre ".*" re4)
  6776. re2 (concat pre re2)
  6777. re2a (concat pre (if pre re2a_ re2a))
  6778. re4 (concat pre (if pre re4_ re4))
  6779. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6780. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6781. re5 "\\)"
  6782. ))
  6783. (cond
  6784. ((eq type 'org-occur) (org-occur reall))
  6785. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6786. (t (goto-char (point-min))
  6787. (setq type 'fuzzy)
  6788. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6789. (org-search-not-self 1 re1 nil t)
  6790. (org-search-not-self 1 re2 nil t)
  6791. (org-search-not-self 1 re2a nil t)
  6792. (org-search-not-self 1 re3 nil t)
  6793. (org-search-not-self 1 re4 nil t)
  6794. (org-search-not-self 1 re5 nil t)
  6795. )
  6796. (goto-char (match-beginning 1))
  6797. (goto-char pos)
  6798. (error "No match")))))
  6799. (t
  6800. ;; Normal string-search
  6801. (goto-char (point-min))
  6802. (if (search-forward s nil t)
  6803. (goto-char (match-beginning 0))
  6804. (error "No match"))))
  6805. (and (org-mode-p) (org-show-context 'link-search))
  6806. type))
  6807. (defun org-search-not-self (group &rest args)
  6808. "Execute `re-search-forward', but only accept matches that do not
  6809. enclose the position of `org-open-link-marker'."
  6810. (let ((m org-open-link-marker))
  6811. (catch 'exit
  6812. (while (apply 're-search-forward args)
  6813. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6814. (goto-char (match-end group))
  6815. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6816. (> (match-beginning 0) (marker-position m))
  6817. (< (match-end 0) (marker-position m)))
  6818. (save-match-data
  6819. (or (not (org-in-regexp
  6820. org-bracket-link-analytic-regexp 1))
  6821. (not (match-end 4)) ; no description
  6822. (and (<= (match-beginning 4) (point))
  6823. (>= (match-end 4) (point))))))
  6824. (throw 'exit (point))))))))
  6825. (defun org-get-buffer-for-internal-link (buffer)
  6826. "Return a buffer to be used for displaying the link target of internal links."
  6827. (cond
  6828. ((not org-display-internal-link-with-indirect-buffer)
  6829. buffer)
  6830. ((string-match "(Clone)$" (buffer-name buffer))
  6831. (message "Buffer is already a clone, not making another one")
  6832. ;; we also do not modify visibility in this case
  6833. buffer)
  6834. (t ; make a new indirect buffer for displaying the link
  6835. (let* ((bn (buffer-name buffer))
  6836. (ibn (concat bn "(Clone)"))
  6837. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6838. (with-current-buffer ib (org-overview))
  6839. ib))))
  6840. (defun org-do-occur (regexp &optional cleanup)
  6841. "Call the Emacs command `occur'.
  6842. If CLEANUP is non-nil, remove the printout of the regular expression
  6843. in the *Occur* buffer. This is useful if the regex is long and not useful
  6844. to read."
  6845. (occur regexp)
  6846. (when cleanup
  6847. (let ((cwin (selected-window)) win beg end)
  6848. (when (setq win (get-buffer-window "*Occur*"))
  6849. (select-window win))
  6850. (goto-char (point-min))
  6851. (when (re-search-forward "match[a-z]+" nil t)
  6852. (setq beg (match-end 0))
  6853. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6854. (setq end (1- (match-beginning 0)))))
  6855. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6856. (goto-char (point-min))
  6857. (select-window cwin))))
  6858. ;;; The mark ring for links jumps
  6859. (defvar org-mark-ring nil
  6860. "Mark ring for positions before jumps in Org-mode.")
  6861. (defvar org-mark-ring-last-goto nil
  6862. "Last position in the mark ring used to go back.")
  6863. ;; Fill and close the ring
  6864. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6865. (loop for i from 1 to org-mark-ring-length do
  6866. (push (make-marker) org-mark-ring))
  6867. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6868. org-mark-ring)
  6869. (defun org-mark-ring-push (&optional pos buffer)
  6870. "Put the current position or POS into the mark ring and rotate it."
  6871. (interactive)
  6872. (setq pos (or pos (point)))
  6873. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6874. (move-marker (car org-mark-ring)
  6875. (or pos (point))
  6876. (or buffer (current-buffer)))
  6877. (message "%s"
  6878. (substitute-command-keys
  6879. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6880. (defun org-mark-ring-goto (&optional n)
  6881. "Jump to the previous position in the mark ring.
  6882. With prefix arg N, jump back that many stored positions. When
  6883. called several times in succession, walk through the entire ring.
  6884. Org-mode commands jumping to a different position in the current file,
  6885. or to another Org-mode file, automatically push the old position
  6886. onto the ring."
  6887. (interactive "p")
  6888. (let (p m)
  6889. (if (eq last-command this-command)
  6890. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6891. (setq p org-mark-ring))
  6892. (setq org-mark-ring-last-goto p)
  6893. (setq m (car p))
  6894. (switch-to-buffer (marker-buffer m))
  6895. (goto-char m)
  6896. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6897. (defun org-remove-angle-brackets (s)
  6898. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6899. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6900. s)
  6901. (defun org-add-angle-brackets (s)
  6902. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6903. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6904. s)
  6905. (defun org-remove-double-quotes (s)
  6906. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6907. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6908. s)
  6909. ;;; Following specific links
  6910. (defun org-follow-timestamp-link ()
  6911. (cond
  6912. ((org-at-date-range-p t)
  6913. (let ((org-agenda-start-on-weekday)
  6914. (t1 (match-string 1))
  6915. (t2 (match-string 2)))
  6916. (setq t1 (time-to-days (org-time-string-to-time t1))
  6917. t2 (time-to-days (org-time-string-to-time t2)))
  6918. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6919. ((org-at-timestamp-p t)
  6920. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6921. (substring (match-string 1) 0 10)))
  6922. 1))
  6923. (t (error "This should not happen"))))
  6924. ;;; Following file links
  6925. (defvar org-wait nil)
  6926. (defun org-open-file (path &optional in-emacs line search)
  6927. "Open the file at PATH.
  6928. First, this expands any special file name abbreviations. Then the
  6929. configuration variable `org-file-apps' is checked if it contains an
  6930. entry for this file type, and if yes, the corresponding command is launched.
  6931. If no application is found, Emacs simply visits the file.
  6932. With optional argument IN-EMACS, Emacs will visit the file.
  6933. Optional LINE specifies a line to go to, optional SEARCH a string to
  6934. search for. If LINE or SEARCH is given, the file will always be
  6935. opened in Emacs.
  6936. If the file does not exist, an error is thrown."
  6937. (setq in-emacs (or in-emacs line search))
  6938. (let* ((file (if (equal path "")
  6939. buffer-file-name
  6940. (substitute-in-file-name (expand-file-name path))))
  6941. (apps (append org-file-apps (org-default-apps)))
  6942. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6943. (dirp (if remp nil (file-directory-p file)))
  6944. (file (if (and dirp org-open-directory-means-index-dot-org)
  6945. (concat (file-name-as-directory file) "index.org")
  6946. file))
  6947. (dfile (downcase file))
  6948. (old-buffer (current-buffer))
  6949. (old-pos (point))
  6950. (old-mode major-mode)
  6951. ext cmd)
  6952. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6953. (setq ext (match-string 1 dfile))
  6954. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6955. (setq ext (match-string 1 dfile))))
  6956. (if in-emacs
  6957. (setq cmd 'emacs)
  6958. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6959. (and dirp (cdr (assoc 'directory apps)))
  6960. (cdr (assoc ext apps))
  6961. (cdr (assoc t apps)))))
  6962. (when (eq cmd 'mailcap)
  6963. (require 'mailcap)
  6964. (mailcap-parse-mailcaps)
  6965. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6966. (command (mailcap-mime-info mime-type)))
  6967. (if (stringp command)
  6968. (setq cmd command)
  6969. (setq cmd 'emacs))))
  6970. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6971. (not (file-exists-p file))
  6972. (not org-open-non-existing-files))
  6973. (error "No such file: %s" file))
  6974. (cond
  6975. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6976. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6977. (while (string-match "['\"]%s['\"]" cmd)
  6978. (setq cmd (replace-match "%s" t t cmd)))
  6979. (while (string-match "%s" cmd)
  6980. (setq cmd (replace-match
  6981. (save-match-data
  6982. (shell-quote-argument
  6983. (convert-standard-filename file)))
  6984. t t cmd)))
  6985. (save-window-excursion
  6986. (start-process-shell-command cmd nil cmd)
  6987. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6988. ))
  6989. ((or (stringp cmd)
  6990. (eq cmd 'emacs))
  6991. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6992. (widen)
  6993. (if line (goto-line line)
  6994. (if search (org-link-search search))))
  6995. ((consp cmd)
  6996. (let ((file (convert-standard-filename file)))
  6997. (eval cmd)))
  6998. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6999. (and (org-mode-p) (eq old-mode 'org-mode)
  7000. (or (not (equal old-buffer (current-buffer)))
  7001. (not (equal old-pos (point))))
  7002. (org-mark-ring-push old-pos old-buffer))))
  7003. (defun org-default-apps ()
  7004. "Return the default applications for this operating system."
  7005. (cond
  7006. ((eq system-type 'darwin)
  7007. org-file-apps-defaults-macosx)
  7008. ((eq system-type 'windows-nt)
  7009. org-file-apps-defaults-windowsnt)
  7010. (t org-file-apps-defaults-gnu)))
  7011. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7012. (defun org-file-remote-p (file)
  7013. "Test whether FILE specifies a location on a remote system.
  7014. Return non-nil if the location is indeed remote.
  7015. For example, the filename \"/user@host:/foo\" specifies a location
  7016. on the system \"/user@host:\"."
  7017. (cond ((fboundp 'file-remote-p)
  7018. (file-remote-p file))
  7019. ((fboundp 'tramp-handle-file-remote-p)
  7020. (tramp-handle-file-remote-p file))
  7021. ((and (boundp 'ange-ftp-name-format)
  7022. (string-match (car ange-ftp-name-format) file))
  7023. t)
  7024. (t nil)))
  7025. ;;;; Refiling
  7026. (defun org-get-org-file ()
  7027. "Read a filename, with default directory `org-directory'."
  7028. (let ((default (or org-default-notes-file remember-data-file)))
  7029. (read-file-name (format "File name [%s]: " default)
  7030. (file-name-as-directory org-directory)
  7031. default)))
  7032. (defun org-notes-order-reversed-p ()
  7033. "Check if the current file should receive notes in reversed order."
  7034. (cond
  7035. ((not org-reverse-note-order) nil)
  7036. ((eq t org-reverse-note-order) t)
  7037. ((not (listp org-reverse-note-order)) nil)
  7038. (t (catch 'exit
  7039. (let ((all org-reverse-note-order)
  7040. entry)
  7041. (while (setq entry (pop all))
  7042. (if (string-match (car entry) buffer-file-name)
  7043. (throw 'exit (cdr entry))))
  7044. nil)))))
  7045. (defvar org-refile-target-table nil
  7046. "The list of refile targets, created by `org-refile'.")
  7047. (defvar org-agenda-new-buffers nil
  7048. "Buffers created to visit agenda files.")
  7049. (defun org-get-refile-targets (&optional default-buffer)
  7050. "Produce a table with refile targets."
  7051. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7052. targets txt re files f desc descre)
  7053. (with-current-buffer (or default-buffer (current-buffer))
  7054. (while (setq entry (pop entries))
  7055. (setq files (car entry) desc (cdr entry))
  7056. (cond
  7057. ((null files) (setq files (list (current-buffer))))
  7058. ((eq files 'org-agenda-files)
  7059. (setq files (org-agenda-files 'unrestricted)))
  7060. ((and (symbolp files) (fboundp files))
  7061. (setq files (funcall files)))
  7062. ((and (symbolp files) (boundp files))
  7063. (setq files (symbol-value files))))
  7064. (if (stringp files) (setq files (list files)))
  7065. (cond
  7066. ((eq (car desc) :tag)
  7067. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7068. ((eq (car desc) :todo)
  7069. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7070. ((eq (car desc) :regexp)
  7071. (setq descre (cdr desc)))
  7072. ((eq (car desc) :level)
  7073. (setq descre (concat "^\\*\\{" (number-to-string
  7074. (if org-odd-levels-only
  7075. (1- (* 2 (cdr desc)))
  7076. (cdr desc)))
  7077. "\\}[ \t]")))
  7078. ((eq (car desc) :maxlevel)
  7079. (setq descre (concat "^\\*\\{1," (number-to-string
  7080. (if org-odd-levels-only
  7081. (1- (* 2 (cdr desc)))
  7082. (cdr desc)))
  7083. "\\}[ \t]")))
  7084. (t (error "Bad refiling target description %s" desc)))
  7085. (while (setq f (pop files))
  7086. (save-excursion
  7087. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7088. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7089. (save-excursion
  7090. (save-restriction
  7091. (widen)
  7092. (goto-char (point-min))
  7093. (while (re-search-forward descre nil t)
  7094. (goto-char (point-at-bol))
  7095. (when (looking-at org-complex-heading-regexp)
  7096. (setq txt (match-string 4)
  7097. re (concat "^" (regexp-quote
  7098. (buffer-substring (match-beginning 1)
  7099. (match-end 4)))))
  7100. (if (match-end 5) (setq re (concat re "[ \t]+"
  7101. (regexp-quote
  7102. (match-string 5)))))
  7103. (setq re (concat re "[ \t]*$"))
  7104. (when org-refile-use-outline-path
  7105. (setq txt (mapconcat 'identity
  7106. (append
  7107. (if (eq org-refile-use-outline-path 'file)
  7108. (list (file-name-nondirectory
  7109. (buffer-file-name (buffer-base-buffer))))
  7110. (if (eq org-refile-use-outline-path 'full-file-path)
  7111. (list (buffer-file-name (buffer-base-buffer)))))
  7112. (org-get-outline-path)
  7113. (list txt))
  7114. "/")))
  7115. (push (list txt f re (point)) targets))
  7116. (goto-char (point-at-eol))))))))
  7117. (nreverse targets))))
  7118. (defun org-get-outline-path ()
  7119. "Return the outline path to the current entry, as a list."
  7120. (let (rtn)
  7121. (save-excursion
  7122. (while (org-up-heading-safe)
  7123. (when (looking-at org-complex-heading-regexp)
  7124. (push (org-match-string-no-properties 4) rtn)))
  7125. rtn)))
  7126. (defvar org-refile-history nil
  7127. "History for refiling operations.")
  7128. (defun org-refile (&optional goto default-buffer)
  7129. "Move the entry at point to another heading.
  7130. The list of target headings is compiled using the information in
  7131. `org-refile-targets', which see. This list is created before each use
  7132. and will therefore always be up-to-date.
  7133. At the target location, the entry is filed as a subitem of the target heading.
  7134. Depending on `org-reverse-note-order', the new subitem will either be the
  7135. first of the last subitem.
  7136. With prefix arg GOTO, the command will only visit the target location,
  7137. not actually move anything.
  7138. With a double prefix `C-u C-u', go to the location where the last refiling
  7139. operation has put the subtree."
  7140. (interactive "P")
  7141. (let* ((cbuf (current-buffer))
  7142. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7143. pos it nbuf file re level reversed)
  7144. (if (equal goto '(16))
  7145. (org-refile-goto-last-stored)
  7146. (when (setq it (org-refile-get-location
  7147. (if goto "Goto: " "Refile to: ") default-buffer))
  7148. (setq file (nth 1 it)
  7149. re (nth 2 it)
  7150. pos (nth 3 it))
  7151. (setq nbuf (or (find-buffer-visiting file)
  7152. (find-file-noselect file)))
  7153. (if goto
  7154. (progn
  7155. (switch-to-buffer nbuf)
  7156. (goto-char pos)
  7157. (org-show-context 'org-goto))
  7158. (org-copy-subtree 1 nil t)
  7159. (save-excursion
  7160. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7161. (find-file-noselect file))))
  7162. (setq reversed (org-notes-order-reversed-p))
  7163. (save-excursion
  7164. (save-restriction
  7165. (widen)
  7166. (goto-char pos)
  7167. (looking-at outline-regexp)
  7168. (setq level (org-get-valid-level (funcall outline-level) 1))
  7169. (goto-char
  7170. (if reversed
  7171. (outline-next-heading)
  7172. (or (save-excursion (outline-get-next-sibling))
  7173. (org-end-of-subtree t t)
  7174. (point-max))))
  7175. (bookmark-set "org-refile-last-stored")
  7176. (org-paste-subtree level))))
  7177. (org-cut-subtree)
  7178. (setq org-markers-to-move nil)
  7179. (message "Entry refiled to \"%s\"" (car it)))))))
  7180. (defun org-refile-goto-last-stored ()
  7181. "Go to the location where the last refile was stored."
  7182. (interactive)
  7183. (bookmark-jump "org-refile-last-stored")
  7184. (message "This is the location of the last refile"))
  7185. (defun org-refile-get-location (&optional prompt default-buffer)
  7186. "Prompt the user for a refile location, using PROMPT."
  7187. (let ((org-refile-targets org-refile-targets)
  7188. (org-refile-use-outline-path org-refile-use-outline-path))
  7189. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7190. (unless org-refile-target-table
  7191. (error "No refile targets"))
  7192. (let* ((cbuf (current-buffer))
  7193. (cfunc (if org-refile-use-outline-path
  7194. 'org-olpath-completing-read
  7195. 'completing-read))
  7196. (extra (if org-refile-use-outline-path "/" ""))
  7197. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7198. (fname (and filename (file-truename filename)))
  7199. (tbl (mapcar
  7200. (lambda (x)
  7201. (if (not (equal fname (file-truename (nth 1 x))))
  7202. (cons (concat (car x) extra " ("
  7203. (file-name-nondirectory (nth 1 x)) ")")
  7204. (cdr x))
  7205. (cons (concat (car x) extra) (cdr x))))
  7206. org-refile-target-table))
  7207. (completion-ignore-case t))
  7208. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7209. tbl)))
  7210. (defun org-olpath-completing-read (prompt collection &rest args)
  7211. "Read an outline path like a file name."
  7212. (let ((thetable collection))
  7213. (apply
  7214. 'completing-read prompt
  7215. (lambda (string predicate &optional flag)
  7216. (let (rtn r s f (l (length string)))
  7217. (cond
  7218. ((eq flag nil)
  7219. ;; try completion
  7220. (try-completion string thetable))
  7221. ((eq flag t)
  7222. ;; all-completions
  7223. (setq rtn (all-completions string thetable predicate))
  7224. (mapcar
  7225. (lambda (x)
  7226. (setq r (substring x l))
  7227. (if (string-match " ([^)]*)$" x)
  7228. (setq f (match-string 0 x))
  7229. (setq f ""))
  7230. (if (string-match "/" r)
  7231. (concat string (substring r 0 (match-end 0)) f)
  7232. x))
  7233. rtn))
  7234. ((eq flag 'lambda)
  7235. ;; exact match?
  7236. (assoc string thetable)))
  7237. ))
  7238. args)))
  7239. ;;;; Dynamic blocks
  7240. (defun org-find-dblock (name)
  7241. "Find the first dynamic block with name NAME in the buffer.
  7242. If not found, stay at current position and return nil."
  7243. (let (pos)
  7244. (save-excursion
  7245. (goto-char (point-min))
  7246. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7247. nil t)
  7248. (match-beginning 0))))
  7249. (if pos (goto-char pos))
  7250. pos))
  7251. (defconst org-dblock-start-re
  7252. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7253. "Matches the startline of a dynamic block, with parameters.")
  7254. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7255. "Matches the end of a dyhamic block.")
  7256. (defun org-create-dblock (plist)
  7257. "Create a dynamic block section, with parameters taken from PLIST.
  7258. PLIST must containe a :name entry which is used as name of the block."
  7259. (unless (bolp) (newline))
  7260. (let ((name (plist-get plist :name)))
  7261. (insert "#+BEGIN: " name)
  7262. (while plist
  7263. (if (eq (car plist) :name)
  7264. (setq plist (cddr plist))
  7265. (insert " " (prin1-to-string (pop plist)))))
  7266. (insert "\n\n#+END:\n")
  7267. (beginning-of-line -2)))
  7268. (defun org-prepare-dblock ()
  7269. "Prepare dynamic block for refresh.
  7270. This empties the block, puts the cursor at the insert position and returns
  7271. the property list including an extra property :name with the block name."
  7272. (unless (looking-at org-dblock-start-re)
  7273. (error "Not at a dynamic block"))
  7274. (let* ((begdel (1+ (match-end 0)))
  7275. (name (org-no-properties (match-string 1)))
  7276. (params (append (list :name name)
  7277. (read (concat "(" (match-string 3) ")")))))
  7278. (unless (re-search-forward org-dblock-end-re nil t)
  7279. (error "Dynamic block not terminated"))
  7280. (setq params
  7281. (append params
  7282. (list :content (buffer-substring
  7283. begdel (match-beginning 0)))))
  7284. (delete-region begdel (match-beginning 0))
  7285. (goto-char begdel)
  7286. (open-line 1)
  7287. params))
  7288. (defun org-map-dblocks (&optional command)
  7289. "Apply COMMAND to all dynamic blocks in the current buffer.
  7290. If COMMAND is not given, use `org-update-dblock'."
  7291. (let ((cmd (or command 'org-update-dblock))
  7292. pos)
  7293. (save-excursion
  7294. (goto-char (point-min))
  7295. (while (re-search-forward org-dblock-start-re nil t)
  7296. (goto-char (setq pos (match-beginning 0)))
  7297. (condition-case nil
  7298. (funcall cmd)
  7299. (error (message "Error during update of dynamic block")))
  7300. (goto-char pos)
  7301. (unless (re-search-forward org-dblock-end-re nil t)
  7302. (error "Dynamic block not terminated"))))))
  7303. (defun org-dblock-update (&optional arg)
  7304. "User command for updating dynamic blocks.
  7305. Update the dynamic block at point. With prefix ARG, update all dynamic
  7306. blocks in the buffer."
  7307. (interactive "P")
  7308. (if arg
  7309. (org-update-all-dblocks)
  7310. (or (looking-at org-dblock-start-re)
  7311. (org-beginning-of-dblock))
  7312. (org-update-dblock)))
  7313. (defun org-update-dblock ()
  7314. "Update the dynamic block at point
  7315. This means to empty the block, parse for parameters and then call
  7316. the correct writing function."
  7317. (save-window-excursion
  7318. (let* ((pos (point))
  7319. (line (org-current-line))
  7320. (params (org-prepare-dblock))
  7321. (name (plist-get params :name))
  7322. (cmd (intern (concat "org-dblock-write:" name))))
  7323. (message "Updating dynamic block `%s' at line %d..." name line)
  7324. (funcall cmd params)
  7325. (message "Updating dynamic block `%s' at line %d...done" name line)
  7326. (goto-char pos))))
  7327. (defun org-beginning-of-dblock ()
  7328. "Find the beginning of the dynamic block at point.
  7329. Error if there is no scuh block at point."
  7330. (let ((pos (point))
  7331. beg)
  7332. (end-of-line 1)
  7333. (if (and (re-search-backward org-dblock-start-re nil t)
  7334. (setq beg (match-beginning 0))
  7335. (re-search-forward org-dblock-end-re nil t)
  7336. (> (match-end 0) pos))
  7337. (goto-char beg)
  7338. (goto-char pos)
  7339. (error "Not in a dynamic block"))))
  7340. (defun org-update-all-dblocks ()
  7341. "Update all dynamic blocks in the buffer.
  7342. This function can be used in a hook."
  7343. (when (org-mode-p)
  7344. (org-map-dblocks 'org-update-dblock)))
  7345. ;;;; Completion
  7346. (defconst org-additional-option-like-keywords
  7347. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7348. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7349. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7350. "BEGIN_QUOTE" "END_QUOTE"
  7351. "BEGIN_VERSE" "END_VERSE"
  7352. "BEGIN_SRC" "END_SRC"))
  7353. (defcustom org-structure-template-alist
  7354. '(
  7355. ("s" "#+begin_src ?\n\n#+end_src"
  7356. "<src lang=\"?\">\n\n</src>")
  7357. ("e" "#+begin_example\n?\n#+end_example"
  7358. "<example>\n?\n</example>")
  7359. ("q" "#+begin_quote\n?\n#+end_quote"
  7360. "<quote>\n?\n</quote>")
  7361. ("v" "#+begin_verse\n?\n#+end_verse"
  7362. "<verse>\n?\n/verse>")
  7363. ("l" "#+begin_latex\n?\n#+end_latex"
  7364. "<literal style=\"latex\">\n?\n</literal>")
  7365. ("L" "#+latex: "
  7366. "<literal style=\"latex\">?</literal>")
  7367. ("h" "#+begin_html\n?\n#+end_html"
  7368. "<literal style=\"html\">\n?\n</literal>")
  7369. ("H" "#+html: "
  7370. "<literal style=\"html\">?</literal>")
  7371. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7372. ("A" "#+ascii: ")
  7373. ("i" "#+include %file ?"
  7374. "<include file=%file markup=\"?\">")
  7375. )
  7376. "Structure completion elements.
  7377. This is a list of abbreviation keys and values. The value gets inserted
  7378. it you type @samp{.} followed by the key and then the completion key,
  7379. usually `M-TAB'. %file will be replaced by a file name after prompting
  7380. for the file uning completion.
  7381. There are two templates for each key, the first uses the original Org syntax,
  7382. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7383. the default when the /org-mtags.el/ module has been loaded. See also the
  7384. variable `org-mtags-prefere-muse-templates'.
  7385. This is an experimental feature, it is undecided if it is going to stay in."
  7386. :group 'org-completion
  7387. :type '(repeat
  7388. (string :tag "Key")
  7389. (string :tag "Template")
  7390. (string :tag "Muse Template")))
  7391. (defun org-try-structure-completion ()
  7392. "Try to complete a structure template before point.
  7393. This looks for strings like \"<e\" on an otherwise empty line and
  7394. expands them."
  7395. (let ((l (buffer-substring (point-at-bol) (point)))
  7396. a)
  7397. (when (and (looking-at "[ \t]*$")
  7398. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7399. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7400. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7401. (match-beginning 1)) a)
  7402. t)))
  7403. (defun org-complete-expand-structure-template (start cell)
  7404. "Expand a structure template."
  7405. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7406. (rpl (nth (if musep 2 1) cell)))
  7407. (delete-region start (point))
  7408. (when (string-match "\\`#\\+" rpl)
  7409. (cond
  7410. ((bolp))
  7411. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7412. (delete-region (point-at-bol) (point)))
  7413. (t (newline))))
  7414. (setq start (point))
  7415. (if (string-match "%file" rpl)
  7416. (setq rpl (replace-match
  7417. (concat
  7418. "\""
  7419. (save-match-data
  7420. (abbreviate-file-name (read-file-name "Include file: ")))
  7421. "\"")
  7422. t t rpl)))
  7423. (insert rpl)
  7424. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7425. (defun org-complete (&optional arg)
  7426. "Perform completion on word at point.
  7427. At the beginning of a headline, this completes TODO keywords as given in
  7428. `org-todo-keywords'.
  7429. If the current word is preceded by a backslash, completes the TeX symbols
  7430. that are supported for HTML support.
  7431. If the current word is preceded by \"#+\", completes special words for
  7432. setting file options.
  7433. In the line after \"#+STARTUP:, complete valid keywords.\"
  7434. At all other locations, this simply calls the value of
  7435. `org-completion-fallback-command'."
  7436. (interactive "P")
  7437. (org-without-partial-completion
  7438. (catch 'exit
  7439. (let* ((a nil)
  7440. (end (point))
  7441. (beg1 (save-excursion
  7442. (skip-chars-backward (org-re "[:alnum:]_@"))
  7443. (point)))
  7444. (beg (save-excursion
  7445. (skip-chars-backward "a-zA-Z0-9_:$")
  7446. (point)))
  7447. (confirm (lambda (x) (stringp (car x))))
  7448. (searchhead (equal (char-before beg) ?*))
  7449. (struct
  7450. (when (and (member (char-before beg1) '(?. ?<))
  7451. (setq a (assoc (buffer-substring beg1 (point))
  7452. org-structure-template-alist)))
  7453. (org-complete-expand-structure-template (1- beg1) a)
  7454. (throw 'exit t)))
  7455. (tag (and (equal (char-before beg1) ?:)
  7456. (equal (char-after (point-at-bol)) ?*)))
  7457. (prop (and (equal (char-before beg1) ?:)
  7458. (not (equal (char-after (point-at-bol)) ?*))))
  7459. (texp (equal (char-before beg) ?\\))
  7460. (link (equal (char-before beg) ?\[))
  7461. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7462. beg)
  7463. "#+"))
  7464. (startup (string-match "^#\\+STARTUP:.*"
  7465. (buffer-substring (point-at-bol) (point))))
  7466. (completion-ignore-case opt)
  7467. (type nil)
  7468. (tbl nil)
  7469. (table (cond
  7470. (opt
  7471. (setq type :opt)
  7472. (require 'org-exp)
  7473. (append
  7474. (mapcar
  7475. (lambda (x)
  7476. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7477. (cons (match-string 2 x) (match-string 1 x)))
  7478. (org-split-string (org-get-current-options) "\n"))
  7479. (mapcar 'list org-additional-option-like-keywords)))
  7480. (startup
  7481. (setq type :startup)
  7482. org-startup-options)
  7483. (link (append org-link-abbrev-alist-local
  7484. org-link-abbrev-alist))
  7485. (texp
  7486. (setq type :tex)
  7487. org-html-entities)
  7488. ((string-match "\\`\\*+[ \t]+\\'"
  7489. (buffer-substring (point-at-bol) beg))
  7490. (setq type :todo)
  7491. (mapcar 'list org-todo-keywords-1))
  7492. (searchhead
  7493. (setq type :searchhead)
  7494. (save-excursion
  7495. (goto-char (point-min))
  7496. (while (re-search-forward org-todo-line-regexp nil t)
  7497. (push (list
  7498. (org-make-org-heading-search-string
  7499. (match-string 3) t))
  7500. tbl)))
  7501. tbl)
  7502. (tag (setq type :tag beg beg1)
  7503. (or org-tag-alist (org-get-buffer-tags)))
  7504. (prop (setq type :prop beg beg1)
  7505. (mapcar 'list (org-buffer-property-keys nil t t)))
  7506. (t (progn
  7507. (call-interactively org-completion-fallback-command)
  7508. (throw 'exit nil)))))
  7509. (pattern (buffer-substring-no-properties beg end))
  7510. (completion (try-completion pattern table confirm)))
  7511. (cond ((eq completion t)
  7512. (if (not (assoc (upcase pattern) table))
  7513. (message "Already complete")
  7514. (if (and (equal type :opt)
  7515. (not (member (car (assoc (upcase pattern) table))
  7516. org-additional-option-like-keywords)))
  7517. (insert (substring (cdr (assoc (upcase pattern) table))
  7518. (length pattern)))
  7519. (if (memq type '(:tag :prop)) (insert ":")))))
  7520. ((null completion)
  7521. (message "Can't find completion for \"%s\"" pattern)
  7522. (ding))
  7523. ((not (string= pattern completion))
  7524. (delete-region beg end)
  7525. (if (string-match " +$" completion)
  7526. (setq completion (replace-match "" t t completion)))
  7527. (insert completion)
  7528. (if (get-buffer-window "*Completions*")
  7529. (delete-window (get-buffer-window "*Completions*")))
  7530. (if (assoc completion table)
  7531. (if (eq type :todo) (insert " ")
  7532. (if (memq type '(:tag :prop)) (insert ":"))))
  7533. (if (and (equal type :opt) (assoc completion table))
  7534. (message "%s" (substitute-command-keys
  7535. "Press \\[org-complete] again to insert example settings"))))
  7536. (t
  7537. (message "Making completion list...")
  7538. (let ((list (sort (all-completions pattern table confirm)
  7539. 'string<)))
  7540. (with-output-to-temp-buffer "*Completions*"
  7541. (condition-case nil
  7542. ;; Protection needed for XEmacs and emacs 21
  7543. (display-completion-list list pattern)
  7544. (error (display-completion-list list)))))
  7545. (message "Making completion list...%s" "done")))))))
  7546. ;;;; TODO, DEADLINE, Comments
  7547. (defun org-toggle-comment ()
  7548. "Change the COMMENT state of an entry."
  7549. (interactive)
  7550. (save-excursion
  7551. (org-back-to-heading)
  7552. (let (case-fold-search)
  7553. (if (looking-at (concat outline-regexp
  7554. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7555. (replace-match "" t t nil 1)
  7556. (if (looking-at outline-regexp)
  7557. (progn
  7558. (goto-char (match-end 0))
  7559. (insert org-comment-string " ")))))))
  7560. (defvar org-last-todo-state-is-todo nil
  7561. "This is non-nil when the last TODO state change led to a TODO state.
  7562. If the last change removed the TODO tag or switched to DONE, then
  7563. this is nil.")
  7564. (defvar org-setting-tags nil) ; dynamically skiped
  7565. (defun org-parse-local-options (string var)
  7566. "Parse STRING for startup setting relevant for variable VAR."
  7567. (let ((rtn (symbol-value var))
  7568. e opts)
  7569. (save-match-data
  7570. (if (or (not string) (not (string-match "\\S-" string)))
  7571. rtn
  7572. (setq opts (delq nil (mapcar (lambda (x)
  7573. (setq e (assoc x org-startup-options))
  7574. (if (eq (nth 1 e) var) e nil))
  7575. (org-split-string string "[ \t]+"))))
  7576. (if (not opts)
  7577. rtn
  7578. (setq rtn nil)
  7579. (while (setq e (pop opts))
  7580. (if (not (nth 3 e))
  7581. (setq rtn (nth 2 e))
  7582. (if (not (listp rtn)) (setq rtn nil))
  7583. (push (nth 2 e) rtn)))
  7584. rtn)))))
  7585. (defvar org-blocker-hook nil
  7586. "Hook for functions that are allowed to block a state change.
  7587. Each function gets as its single argument a property list, see
  7588. `org-trigger-hook' for more information about this list.
  7589. If any of the functions in this hook returns nil, the state change
  7590. is blocked.")
  7591. (defvar org-trigger-hook nil
  7592. "Hook for functions that are triggered by a state change.
  7593. Each function gets as its single argument a property list with at least
  7594. the following elements:
  7595. (:type type-of-change :position pos-at-entry-start
  7596. :from old-state :to new-state)
  7597. Depending on the type, more properties may be present.
  7598. This mechanism is currently implemented for:
  7599. TODO state changes
  7600. ------------------
  7601. :type todo-state-change
  7602. :from previous state (keyword as a string), or nil
  7603. :to new state (keyword as a string), or nil")
  7604. (defun org-todo (&optional arg)
  7605. "Change the TODO state of an item.
  7606. The state of an item is given by a keyword at the start of the heading,
  7607. like
  7608. *** TODO Write paper
  7609. *** DONE Call mom
  7610. The different keywords are specified in the variable `org-todo-keywords'.
  7611. By default the available states are \"TODO\" and \"DONE\".
  7612. So for this example: when the item starts with TODO, it is changed to DONE.
  7613. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7614. DONE are present, add TODO at the beginning of the heading.
  7615. With C-u prefix arg, use completion to determine the new state.
  7616. With numeric prefix arg, switch to that state.
  7617. For calling through lisp, arg is also interpreted in the following way:
  7618. 'none -> empty state
  7619. \"\"(empty string) -> switch to empty state
  7620. 'done -> switch to DONE
  7621. 'nextset -> switch to the next set of keywords
  7622. 'previousset -> switch to the previous set of keywords
  7623. \"WAITING\" -> switch to the specified keyword, but only if it
  7624. really is a member of `org-todo-keywords'."
  7625. (interactive "P")
  7626. (save-excursion
  7627. (catch 'exit
  7628. (org-back-to-heading)
  7629. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7630. (or (looking-at (concat " +" org-todo-regexp " *"))
  7631. (looking-at " *"))
  7632. (let* ((match-data (match-data))
  7633. (startpos (point-at-bol))
  7634. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7635. (org-log-done org-log-done)
  7636. (org-log-repeat org-log-repeat)
  7637. (org-todo-log-states org-todo-log-states)
  7638. (this (match-string 1))
  7639. (hl-pos (match-beginning 0))
  7640. (head (org-get-todo-sequence-head this))
  7641. (ass (assoc head org-todo-kwd-alist))
  7642. (interpret (nth 1 ass))
  7643. (done-word (nth 3 ass))
  7644. (final-done-word (nth 4 ass))
  7645. (last-state (or this ""))
  7646. (completion-ignore-case t)
  7647. (member (member this org-todo-keywords-1))
  7648. (tail (cdr member))
  7649. (state (cond
  7650. ((and org-todo-key-trigger
  7651. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7652. (and (not arg) org-use-fast-todo-selection
  7653. (not (eq org-use-fast-todo-selection 'prefix)))))
  7654. ;; Use fast selection
  7655. (org-fast-todo-selection))
  7656. ((and (equal arg '(4))
  7657. (or (not org-use-fast-todo-selection)
  7658. (not org-todo-key-trigger)))
  7659. ;; Read a state with completion
  7660. (completing-read "State: " (mapcar (lambda(x) (list x))
  7661. org-todo-keywords-1)
  7662. nil t))
  7663. ((eq arg 'right)
  7664. (if this
  7665. (if tail (car tail) nil)
  7666. (car org-todo-keywords-1)))
  7667. ((eq arg 'left)
  7668. (if (equal member org-todo-keywords-1)
  7669. nil
  7670. (if this
  7671. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7672. org-todo-keywords-1)
  7673. (org-last org-todo-keywords-1))))
  7674. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7675. (setq arg nil))) ; hack to fall back to cycling
  7676. (arg
  7677. ;; user or caller requests a specific state
  7678. (cond
  7679. ((equal arg "") nil)
  7680. ((eq arg 'none) nil)
  7681. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7682. ((eq arg 'nextset)
  7683. (or (car (cdr (member head org-todo-heads)))
  7684. (car org-todo-heads)))
  7685. ((eq arg 'previousset)
  7686. (let ((org-todo-heads (reverse org-todo-heads)))
  7687. (or (car (cdr (member head org-todo-heads)))
  7688. (car org-todo-heads))))
  7689. ((car (member arg org-todo-keywords-1)))
  7690. ((nth (1- (prefix-numeric-value arg))
  7691. org-todo-keywords-1))))
  7692. ((null member) (or head (car org-todo-keywords-1)))
  7693. ((equal this final-done-word) nil) ;; -> make empty
  7694. ((null tail) nil) ;; -> first entry
  7695. ((eq interpret 'sequence)
  7696. (car tail))
  7697. ((memq interpret '(type priority))
  7698. (if (eq this-command last-command)
  7699. (car tail)
  7700. (if (> (length tail) 0)
  7701. (or done-word (car org-done-keywords))
  7702. nil)))
  7703. (t nil)))
  7704. (next (if state (concat " " state " ") " "))
  7705. (change-plist (list :type 'todo-state-change :from this :to state
  7706. :position startpos))
  7707. dolog now-done-p)
  7708. (when org-blocker-hook
  7709. (unless (save-excursion
  7710. (save-match-data
  7711. (run-hook-with-args-until-failure
  7712. 'org-blocker-hook change-plist)))
  7713. (if (interactive-p)
  7714. (error "TODO state change from %s to %s blocked" this state)
  7715. ;; fail silently
  7716. (message "TODO state change from %s to %s blocked" this state)
  7717. (throw 'exit nil))))
  7718. (store-match-data match-data)
  7719. (replace-match next t t)
  7720. (unless (pos-visible-in-window-p hl-pos)
  7721. (message "TODO state changed to %s" (org-trim next)))
  7722. (unless head
  7723. (setq head (org-get-todo-sequence-head state)
  7724. ass (assoc head org-todo-kwd-alist)
  7725. interpret (nth 1 ass)
  7726. done-word (nth 3 ass)
  7727. final-done-word (nth 4 ass)))
  7728. (when (memq arg '(nextset previousset))
  7729. (message "Keyword-Set %d/%d: %s"
  7730. (- (length org-todo-sets) -1
  7731. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7732. (length org-todo-sets)
  7733. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7734. (setq org-last-todo-state-is-todo
  7735. (not (member state org-done-keywords)))
  7736. (setq now-done-p (and (member state org-done-keywords)
  7737. (not (member this org-done-keywords))))
  7738. (and logging (org-local-logging logging))
  7739. (when (and (or org-todo-log-states org-log-done)
  7740. (not (memq arg '(nextset previousset))))
  7741. ;; we need to look at recording a time and note
  7742. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7743. (nth 2 (assoc this org-todo-log-states))))
  7744. (when (and state
  7745. (member state org-not-done-keywords)
  7746. (not (member this org-not-done-keywords)))
  7747. ;; This is now a todo state and was not one before
  7748. ;; If there was a CLOSED time stamp, get rid of it.
  7749. (org-add-planning-info nil nil 'closed))
  7750. (when (and now-done-p org-log-done)
  7751. ;; It is now done, and it was not done before
  7752. (org-add-planning-info 'closed (org-current-time))
  7753. (if (and (not dolog) (eq 'note org-log-done))
  7754. (org-add-log-setup 'done state 'findpos 'note)))
  7755. (when (and state dolog)
  7756. ;; This is a non-nil state, and we need to log it
  7757. (org-add-log-setup 'state state 'findpos dolog)))
  7758. ;; Fixup tag positioning
  7759. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7760. (when org-provide-todo-statistics
  7761. (org-update-parent-todo-statistics))
  7762. (run-hooks 'org-after-todo-state-change-hook)
  7763. (if (and arg (not (member state org-done-keywords)))
  7764. (setq head (org-get-todo-sequence-head state)))
  7765. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7766. ;; Do we need to trigger a repeat?
  7767. (when now-done-p (org-auto-repeat-maybe state))
  7768. ;; Fixup cursor location if close to the keyword
  7769. (if (and (outline-on-heading-p)
  7770. (not (bolp))
  7771. (save-excursion (beginning-of-line 1)
  7772. (looking-at org-todo-line-regexp))
  7773. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7774. (progn
  7775. (goto-char (or (match-end 2) (match-end 1)))
  7776. (just-one-space)))
  7777. (when org-trigger-hook
  7778. (save-excursion
  7779. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7780. (defun org-update-parent-todo-statistics ()
  7781. "Update any statistics cookie in the parent of the current headline."
  7782. (interactive)
  7783. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7784. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7785. (catch 'exit
  7786. (save-excursion
  7787. (setq level (org-up-heading-safe))
  7788. (unless (and level
  7789. (re-search-forward box-re (point-at-eol) t))
  7790. (throw 'exit nil))
  7791. (setq is-percent (match-end 2))
  7792. (save-match-data
  7793. (unless (outline-next-heading) (throw 'exit nil))
  7794. (while (looking-at org-todo-line-regexp)
  7795. (setq kwd (match-string 2))
  7796. (and kwd (setq cnt-all (1+ cnt-all)))
  7797. (and (member kwd org-done-keywords)
  7798. (setq cnt-done (1+ cnt-done)))
  7799. (condition-case nil
  7800. (outline-forward-same-level 1)
  7801. (error (end-of-line 1)))))
  7802. (replace-match
  7803. (if is-percent
  7804. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7805. (format "[%d/%d]" cnt-done cnt-all)))
  7806. (run-hook-with-args 'org-after-todo-statistics-hook
  7807. cnt-done (- cnt-all cnt-done))))))
  7808. (defvar org-after-todo-statistics-hook nil
  7809. "Hook that is called after a TODO statistics cookie has been updated.
  7810. Each function is called with two arguments: the number of not-done entries
  7811. and the number of done entries.
  7812. For example, the following function, when added to this hook, will switch
  7813. an entry to DONE when all children are done, and back to TODO when new
  7814. entries are set to a TODO status. Note that this hook is only called
  7815. when there is a statistics cookie in the headline!
  7816. (defun org-summary-todo (n-done n-not-done)
  7817. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7818. (let (org-log-done org-log-states) ; turn off logging
  7819. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7820. ")
  7821. (defun org-local-logging (value)
  7822. "Get logging settings from a property VALUE."
  7823. (let* (words w a)
  7824. ;; directly set the variables, they are already local.
  7825. (setq org-log-done nil
  7826. org-log-repeat nil
  7827. org-todo-log-states nil)
  7828. (setq words (org-split-string value))
  7829. (while (setq w (pop words))
  7830. (cond
  7831. ((setq a (assoc w org-startup-options))
  7832. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7833. (set (nth 1 a) (nth 2 a))))
  7834. ((setq a (org-extract-log-state-settings w))
  7835. (and (member (car a) org-todo-keywords-1)
  7836. (push a org-todo-log-states)))))))
  7837. (defun org-get-todo-sequence-head (kwd)
  7838. "Return the head of the TODO sequence to which KWD belongs.
  7839. If KWD is not set, check if there is a text property remembering the
  7840. right sequence."
  7841. (let (p)
  7842. (cond
  7843. ((not kwd)
  7844. (or (get-text-property (point-at-bol) 'org-todo-head)
  7845. (progn
  7846. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7847. nil (point-at-eol)))
  7848. (get-text-property p 'org-todo-head))))
  7849. ((not (member kwd org-todo-keywords-1))
  7850. (car org-todo-keywords-1))
  7851. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7852. (defun org-fast-todo-selection ()
  7853. "Fast TODO keyword selection with single keys.
  7854. Returns the new TODO keyword, or nil if no state change should occur."
  7855. (let* ((fulltable org-todo-key-alist)
  7856. (done-keywords org-done-keywords) ;; needed for the faces.
  7857. (maxlen (apply 'max (mapcar
  7858. (lambda (x)
  7859. (if (stringp (car x)) (string-width (car x)) 0))
  7860. fulltable)))
  7861. (expert nil)
  7862. (fwidth (+ maxlen 3 1 3))
  7863. (ncol (/ (- (window-width) 4) fwidth))
  7864. tg cnt e c tbl
  7865. groups ingroup)
  7866. (save-window-excursion
  7867. (if expert
  7868. (set-buffer (get-buffer-create " *Org todo*"))
  7869. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7870. (erase-buffer)
  7871. (org-set-local 'org-done-keywords done-keywords)
  7872. (setq tbl fulltable cnt 0)
  7873. (while (setq e (pop tbl))
  7874. (cond
  7875. ((equal e '(:startgroup))
  7876. (push '() groups) (setq ingroup t)
  7877. (when (not (= cnt 0))
  7878. (setq cnt 0)
  7879. (insert "\n"))
  7880. (insert "{ "))
  7881. ((equal e '(:endgroup))
  7882. (setq ingroup nil cnt 0)
  7883. (insert "}\n"))
  7884. (t
  7885. (setq tg (car e) c (cdr e))
  7886. (if ingroup (push tg (car groups)))
  7887. (setq tg (org-add-props tg nil 'face
  7888. (org-get-todo-face tg)))
  7889. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7890. (insert "[" c "] " tg (make-string
  7891. (- fwidth 4 (length tg)) ?\ ))
  7892. (when (= (setq cnt (1+ cnt)) ncol)
  7893. (insert "\n")
  7894. (if ingroup (insert " "))
  7895. (setq cnt 0)))))
  7896. (insert "\n")
  7897. (goto-char (point-min))
  7898. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7899. (fit-window-to-buffer))
  7900. (message "[a-z..]:Set [SPC]:clear")
  7901. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7902. (cond
  7903. ((or (= c ?\C-g)
  7904. (and (= c ?q) (not (rassoc c fulltable))))
  7905. (setq quit-flag t))
  7906. ((= c ?\ ) nil)
  7907. ((setq e (rassoc c fulltable) tg (car e))
  7908. tg)
  7909. (t (setq quit-flag t))))))
  7910. (defun org-entry-is-todo-p ()
  7911. (member (org-get-todo-state) org-not-done-keywords))
  7912. (defun org-entry-is-done-p ()
  7913. (member (org-get-todo-state) org-done-keywords))
  7914. (defun org-get-todo-state ()
  7915. (save-excursion
  7916. (org-back-to-heading t)
  7917. (and (looking-at org-todo-line-regexp)
  7918. (match-end 2)
  7919. (match-string 2))))
  7920. (defun org-at-date-range-p (&optional inactive-ok)
  7921. "Is the cursor inside a date range?"
  7922. (interactive)
  7923. (save-excursion
  7924. (catch 'exit
  7925. (let ((pos (point)))
  7926. (skip-chars-backward "^[<\r\n")
  7927. (skip-chars-backward "<[")
  7928. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7929. (>= (match-end 0) pos)
  7930. (throw 'exit t))
  7931. (skip-chars-backward "^<[\r\n")
  7932. (skip-chars-backward "<[")
  7933. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7934. (>= (match-end 0) pos)
  7935. (throw 'exit t)))
  7936. nil)))
  7937. (defun org-get-repeat ()
  7938. "Check if there is a deadline/schedule with repeater in this entry."
  7939. (save-match-data
  7940. (save-excursion
  7941. (org-back-to-heading t)
  7942. (if (re-search-forward
  7943. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7944. (match-string 1)))))
  7945. (defvar org-last-changed-timestamp)
  7946. (defvar org-last-inserted-timestamp)
  7947. (defvar org-log-post-message)
  7948. (defvar org-log-note-purpose)
  7949. (defvar org-log-note-how)
  7950. (defvar org-log-note-extra)
  7951. (defun org-auto-repeat-maybe (done-word)
  7952. "Check if the current headline contains a repeated deadline/schedule.
  7953. If yes, set TODO state back to what it was and change the base date
  7954. of repeating deadline/scheduled time stamps to new date.
  7955. This function is run automatically after each state change to a DONE state."
  7956. ;; last-state is dynamically scoped into this function
  7957. (let* ((repeat (org-get-repeat))
  7958. (aa (assoc last-state org-todo-kwd-alist))
  7959. (interpret (nth 1 aa))
  7960. (head (nth 2 aa))
  7961. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7962. (msg "Entry repeats: ")
  7963. (org-log-done nil)
  7964. (org-todo-log-states nil)
  7965. (nshiftmax 10) (nshift 0)
  7966. re type n what ts mb0 time)
  7967. (when repeat
  7968. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7969. (org-todo (if (eq interpret 'type) last-state head))
  7970. (when org-log-repeat
  7971. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7972. (memq 'org-add-log-note post-command-hook))
  7973. ;; OK, we are already setup for some record
  7974. (if (eq org-log-repeat 'note)
  7975. ;; make sure we take a note, not only a time stamp
  7976. (setq org-log-note-how 'note))
  7977. ;; Set up for taking a record
  7978. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7979. 'findpos org-log-repeat)))
  7980. (org-back-to-heading t)
  7981. (org-add-planning-info nil nil 'closed)
  7982. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7983. org-deadline-time-regexp "\\)\\|\\("
  7984. org-ts-regexp "\\)"))
  7985. (while (re-search-forward
  7986. re (save-excursion (outline-next-heading) (point)) t)
  7987. (setq type (if (match-end 1) org-scheduled-string
  7988. (if (match-end 3) org-deadline-string "Plain:"))
  7989. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7990. mb0 (match-beginning 0))
  7991. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7992. (setq n (string-to-number (match-string 2 ts))
  7993. what (match-string 3 ts))
  7994. (if (equal what "w") (setq n (* n 7) what "d"))
  7995. ;; Preparation, see if we need to modify the start date for the change
  7996. (when (match-end 1)
  7997. (setq time (save-match-data (org-time-string-to-time ts)))
  7998. (cond
  7999. ((equal (match-string 1 ts) ".")
  8000. ;; Shift starting date to today
  8001. (org-timestamp-change
  8002. (- (time-to-days (current-time)) (time-to-days time))
  8003. 'day))
  8004. ((equal (match-string 1 ts) "+")
  8005. (while (or (= nshift 0)
  8006. (<= (time-to-days time) (time-to-days (current-time))))
  8007. (when (= (incf nshift) nshiftmax)
  8008. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8009. (error "Abort")))
  8010. (org-timestamp-change n (cdr (assoc what whata)))
  8011. (org-at-timestamp-p t)
  8012. (setq ts (match-string 1))
  8013. (setq time (save-match-data (org-time-string-to-time ts))))
  8014. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8015. ;; rematch, so that we have everything in place for the real shift
  8016. (org-at-timestamp-p t)
  8017. (setq ts (match-string 1))
  8018. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8019. (org-timestamp-change n (cdr (assoc what whata)))
  8020. (setq msg (concat msg type org-last-changed-timestamp " "))))
  8021. (setq org-log-post-message msg)
  8022. (message "%s" msg))))
  8023. (defun org-show-todo-tree (arg)
  8024. "Make a compact tree which shows all headlines marked with TODO.
  8025. The tree will show the lines where the regexp matches, and all higher
  8026. headlines above the match.
  8027. With a \\[universal-argument] prefix, also show the DONE entries.
  8028. With a numeric prefix N, construct a sparse tree for the Nth element
  8029. of `org-todo-keywords-1'."
  8030. (interactive "P")
  8031. (let ((case-fold-search nil)
  8032. (kwd-re
  8033. (cond ((null arg) org-not-done-regexp)
  8034. ((equal arg '(4))
  8035. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  8036. (mapcar 'list org-todo-keywords-1))))
  8037. (concat "\\("
  8038. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8039. "\\)\\>")))
  8040. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8041. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8042. org-todo-keywords-1)))
  8043. (t (error "Invalid prefix argument: %s" arg)))))
  8044. (message "%d TODO entries found"
  8045. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8046. (defun org-deadline (&optional remove time)
  8047. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8048. With argument REMOVE, remove any deadline from the item.
  8049. When TIME is set, it should be an internal time specification, and the
  8050. scheduling will use the corresponding date."
  8051. (interactive "P")
  8052. (if remove
  8053. (progn
  8054. (org-remove-timestamp-with-keyword org-deadline-string)
  8055. (message "Item no longer has a deadline."))
  8056. (if (org-get-repeat)
  8057. (error "Cannot change deadline on task with repeater, please do that by hand")
  8058. (org-add-planning-info 'deadline time 'closed)
  8059. (message "Deadline on %s" org-last-inserted-timestamp))))
  8060. (defun org-schedule (&optional remove time)
  8061. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8062. With argument REMOVE, remove any scheduling date from the item.
  8063. When TIME is set, it should be an internal time specification, and the
  8064. scheduling will use the corresponding date."
  8065. (interactive "P")
  8066. (if remove
  8067. (progn
  8068. (org-remove-timestamp-with-keyword org-scheduled-string)
  8069. (message "Item is no longer scheduled."))
  8070. (if (org-get-repeat)
  8071. (error "Cannot reschedule task with repeater, please do that by hand")
  8072. (org-add-planning-info 'scheduled time 'closed)
  8073. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8074. (defun org-remove-timestamp-with-keyword (keyword)
  8075. "Remove all time stamps with KEYWORD in the current entry."
  8076. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8077. beg)
  8078. (save-excursion
  8079. (org-back-to-heading t)
  8080. (setq beg (point))
  8081. (org-end-of-subtree t t)
  8082. (while (re-search-backward re beg t)
  8083. (replace-match "")
  8084. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8085. (equal (char-before) ?\ ))
  8086. (backward-delete-char 1)
  8087. (if (string-match "^[ \t]*$" (buffer-substring
  8088. (point-at-bol) (point-at-eol)))
  8089. (delete-region (point-at-bol)
  8090. (min (point-max) (1+ (point-at-eol))))))))))
  8091. (defun org-add-planning-info (what &optional time &rest remove)
  8092. "Insert new timestamp with keyword in the line directly after the headline.
  8093. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8094. If non is given, the user is prompted for a date.
  8095. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8096. be removed."
  8097. (interactive)
  8098. (let (org-time-was-given org-end-time-was-given ts
  8099. end default-time default-input)
  8100. (when (and (not time) (memq what '(scheduled deadline)))
  8101. ;; Try to get a default date/time from existing timestamp
  8102. (save-excursion
  8103. (org-back-to-heading t)
  8104. (setq end (save-excursion (outline-next-heading) (point)))
  8105. (when (re-search-forward (if (eq what 'scheduled)
  8106. org-scheduled-time-regexp
  8107. org-deadline-time-regexp)
  8108. end t)
  8109. (setq ts (match-string 1)
  8110. default-time
  8111. (apply 'encode-time (org-parse-time-string ts))
  8112. default-input (and ts (org-get-compact-tod ts))))))
  8113. (when what
  8114. ;; If necessary, get the time from the user
  8115. (setq time (or time (org-read-date nil 'to-time nil nil
  8116. default-time default-input))))
  8117. (when (and org-insert-labeled-timestamps-at-point
  8118. (member what '(scheduled deadline)))
  8119. (insert
  8120. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8121. (org-insert-time-stamp time org-time-was-given
  8122. nil nil nil (list org-end-time-was-given))
  8123. (setq what nil))
  8124. (save-excursion
  8125. (save-restriction
  8126. (let (col list elt ts buffer-invisibility-spec)
  8127. (org-back-to-heading t)
  8128. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8129. (goto-char (match-end 1))
  8130. (setq col (current-column))
  8131. (goto-char (match-end 0))
  8132. (if (eobp) (insert "\n") (forward-char 1))
  8133. (if (and (not (looking-at outline-regexp))
  8134. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8135. "[^\r\n]*"))
  8136. (not (equal (match-string 1) org-clock-string)))
  8137. (narrow-to-region (match-beginning 0) (match-end 0))
  8138. (insert-before-markers "\n")
  8139. (backward-char 1)
  8140. (narrow-to-region (point) (point))
  8141. (and org-adapt-indentation (org-indent-to-column col)))
  8142. ;; Check if we have to remove something.
  8143. (setq list (cons what remove))
  8144. (while list
  8145. (setq elt (pop list))
  8146. (goto-char (point-min))
  8147. (when (or (and (eq elt 'scheduled)
  8148. (re-search-forward org-scheduled-time-regexp nil t))
  8149. (and (eq elt 'deadline)
  8150. (re-search-forward org-deadline-time-regexp nil t))
  8151. (and (eq elt 'closed)
  8152. (re-search-forward org-closed-time-regexp nil t)))
  8153. (replace-match "")
  8154. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8155. (if (looking-at " +") (replace-match ""))))
  8156. (goto-char (point-max))
  8157. (when what
  8158. (insert
  8159. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8160. (cond ((eq what 'scheduled) org-scheduled-string)
  8161. ((eq what 'deadline) org-deadline-string)
  8162. ((eq what 'closed) org-closed-string))
  8163. " ")
  8164. (setq ts (org-insert-time-stamp
  8165. time
  8166. (or org-time-was-given
  8167. (and (eq what 'closed) org-log-done-with-time))
  8168. (eq what 'closed)
  8169. nil nil (list org-end-time-was-given)))
  8170. (end-of-line 1))
  8171. (goto-char (point-min))
  8172. (widen)
  8173. (if (and (looking-at "[ \t]+\n")
  8174. (equal (char-before) ?\n))
  8175. (delete-region (1- (point)) (point-at-eol)))
  8176. ts)))))
  8177. (defvar org-log-note-marker (make-marker))
  8178. (defvar org-log-note-purpose nil)
  8179. (defvar org-log-note-state nil)
  8180. (defvar org-log-note-how nil)
  8181. (defvar org-log-note-extra nil)
  8182. (defvar org-log-note-window-configuration nil)
  8183. (defvar org-log-note-return-to (make-marker))
  8184. (defvar org-log-post-message nil
  8185. "Message to be displayed after a log note has been stored.
  8186. The auto-repeater uses this.")
  8187. (defun org-add-note ()
  8188. "Add a note to the current entry.
  8189. This is done in the same way as adding a state change note."
  8190. (interactive)
  8191. (org-add-log-setup 'note nil t nil))
  8192. (defun org-add-log-setup (&optional purpose state findpos how extra)
  8193. "Set up the post command hook to take a note.
  8194. If this is about to TODO state change, the new state is expected in STATE.
  8195. When FINDPOS is non-nil, find the correct position for the note in
  8196. the current entry. If not, assume that it can be inserted at point.
  8197. HOW is an indicator what kind of note should be created.
  8198. EXTRA is additional text that will be inserted into the notes buffer."
  8199. (save-excursion
  8200. (when findpos
  8201. (org-back-to-heading t)
  8202. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8203. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8204. "[^\r\n]*\\)?"))
  8205. (goto-char (match-end 0))
  8206. (unless org-log-states-order-reversed
  8207. (and (= (char-after) ?\n) (forward-char 1))
  8208. (org-skip-over-state-notes)
  8209. (skip-chars-backward " \t\n\r")))
  8210. (move-marker org-log-note-marker (point))
  8211. (setq org-log-note-purpose purpose
  8212. org-log-note-state state
  8213. org-log-note-how how
  8214. org-log-note-extra extra)
  8215. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8216. (defun org-skip-over-state-notes ()
  8217. "Skip past the list of State notes in an entry."
  8218. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8219. (while (looking-at "[ \t]*- State")
  8220. (condition-case nil
  8221. (org-next-item)
  8222. (error (org-end-of-item)))))
  8223. (defun org-add-log-note (&optional purpose)
  8224. "Pop up a window for taking a note, and add this note later at point."
  8225. (remove-hook 'post-command-hook 'org-add-log-note)
  8226. (setq org-log-note-window-configuration (current-window-configuration))
  8227. (delete-other-windows)
  8228. (move-marker org-log-note-return-to (point))
  8229. (switch-to-buffer (marker-buffer org-log-note-marker))
  8230. (goto-char org-log-note-marker)
  8231. (org-switch-to-buffer-other-window "*Org Note*")
  8232. (erase-buffer)
  8233. (if (memq org-log-note-how '(time state))
  8234. (org-store-log-note)
  8235. (let ((org-inhibit-startup t)) (org-mode))
  8236. (insert (format "# Insert note for %s.
  8237. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8238. (cond
  8239. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8240. ((eq org-log-note-purpose 'done) "closed todo item")
  8241. ((eq org-log-note-purpose 'state)
  8242. (format "state change to \"%s\"" org-log-note-state))
  8243. ((eq org-log-note-purpose 'note)
  8244. "this entry")
  8245. (t (error "This should not happen")))))
  8246. (if org-log-note-extra (insert org-log-note-extra))
  8247. (org-set-local 'org-finish-function 'org-store-log-note)))
  8248. (defvar org-note-abort nil) ; dynamically scoped
  8249. (defun org-store-log-note ()
  8250. "Finish taking a log note, and insert it to where it belongs."
  8251. (let ((txt (buffer-string))
  8252. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8253. lines ind)
  8254. (kill-buffer (current-buffer))
  8255. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8256. (setq txt (replace-match "" t t txt)))
  8257. (if (string-match "\\s-+\\'" txt)
  8258. (setq txt (replace-match "" t t txt)))
  8259. (setq lines (org-split-string txt "\n"))
  8260. (when (and note (string-match "\\S-" note))
  8261. (setq note
  8262. (org-replace-escapes
  8263. note
  8264. (list (cons "%u" (user-login-name))
  8265. (cons "%U" user-full-name)
  8266. (cons "%t" (format-time-string
  8267. (org-time-stamp-format 'long 'inactive)
  8268. (current-time)))
  8269. (cons "%s" (if org-log-note-state
  8270. (concat "\"" org-log-note-state "\"")
  8271. "")))))
  8272. (if lines (setq note (concat note " \\\\")))
  8273. (push note lines))
  8274. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8275. (when lines
  8276. (save-excursion
  8277. (set-buffer (marker-buffer org-log-note-marker))
  8278. (save-excursion
  8279. (goto-char org-log-note-marker)
  8280. (move-marker org-log-note-marker nil)
  8281. (end-of-line 1)
  8282. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8283. (indent-relative nil)
  8284. (insert "- " (pop lines))
  8285. (org-indent-line-function)
  8286. (beginning-of-line 1)
  8287. (looking-at "[ \t]*")
  8288. (setq ind (concat (match-string 0) " "))
  8289. (end-of-line 1)
  8290. (while lines (insert "\n" ind (pop lines)))))))
  8291. (set-window-configuration org-log-note-window-configuration)
  8292. (with-current-buffer (marker-buffer org-log-note-return-to)
  8293. (goto-char org-log-note-return-to))
  8294. (move-marker org-log-note-return-to nil)
  8295. (and org-log-post-message (message "%s" org-log-post-message)))
  8296. (defun org-sparse-tree (&optional arg)
  8297. "Create a sparse tree, prompt for the details.
  8298. This command can create sparse trees. You first need to select the type
  8299. of match used to create the tree:
  8300. t Show entries with a specific TODO keyword.
  8301. T Show entries selected by a tags match.
  8302. p Enter a property name and its value (both with completion on existing
  8303. names/values) and show entries with that property.
  8304. r Show entries matching a regular expression
  8305. d Show deadlines due within `org-deadline-warning-days'."
  8306. (interactive "P")
  8307. (let (ans kwd value)
  8308. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8309. (setq ans (read-char-exclusive))
  8310. (cond
  8311. ((equal ans ?d)
  8312. (call-interactively 'org-check-deadlines))
  8313. ((equal ans ?b)
  8314. (call-interactively 'org-check-before-date))
  8315. ((equal ans ?t)
  8316. (org-show-todo-tree '(4)))
  8317. ((equal ans ?T)
  8318. (call-interactively 'org-tags-sparse-tree))
  8319. ((member ans '(?p ?P))
  8320. (setq kwd (completing-read "Property: "
  8321. (mapcar 'list (org-buffer-property-keys))))
  8322. (setq value (completing-read "Value: "
  8323. (mapcar 'list (org-property-values kwd))))
  8324. (unless (string-match "\\`{.*}\\'" value)
  8325. (setq value (concat "\"" value "\"")))
  8326. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8327. ((member ans '(?r ?R ?/))
  8328. (call-interactively 'org-occur))
  8329. (t (error "No such sparse tree command \"%c\"" ans)))))
  8330. (defvar org-occur-highlights nil
  8331. "List of overlays used for occur matches.")
  8332. (make-variable-buffer-local 'org-occur-highlights)
  8333. (defvar org-occur-parameters nil
  8334. "Parameters of the active org-occur calls.
  8335. This is a list, each call to org-occur pushes as cons cell,
  8336. containing the regular expression and the callback, onto the list.
  8337. The list can contain several entries if `org-occur' has been called
  8338. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8339. will only contain one set of parameters. When the highlights are
  8340. removed (for example with `C-c C-c', or with the next edit (depending
  8341. on `org-remove-highlights-with-change'), this variable is emptied
  8342. as well.")
  8343. (make-variable-buffer-local 'org-occur-parameters)
  8344. (defun org-occur (regexp &optional keep-previous callback)
  8345. "Make a compact tree which shows all matches of REGEXP.
  8346. The tree will show the lines where the regexp matches, and all higher
  8347. headlines above the match. It will also show the heading after the match,
  8348. to make sure editing the matching entry is easy.
  8349. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8350. call to `org-occur' will be kept, to allow stacking of calls to this
  8351. command.
  8352. If CALLBACK is non-nil, it is a function which is called to confirm
  8353. that the match should indeed be shown."
  8354. (interactive "sRegexp: \nP")
  8355. (unless keep-previous
  8356. (org-remove-occur-highlights nil nil t))
  8357. (push (cons regexp callback) org-occur-parameters)
  8358. (let ((cnt 0))
  8359. (save-excursion
  8360. (goto-char (point-min))
  8361. (if (or (not keep-previous) ; do not want to keep
  8362. (not org-occur-highlights)) ; no previous matches
  8363. ;; hide everything
  8364. (org-overview))
  8365. (while (re-search-forward regexp nil t)
  8366. (when (or (not callback)
  8367. (save-match-data (funcall callback)))
  8368. (setq cnt (1+ cnt))
  8369. (when org-highlight-sparse-tree-matches
  8370. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8371. (org-show-context 'occur-tree))))
  8372. (when org-remove-highlights-with-change
  8373. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8374. nil 'local))
  8375. (unless org-sparse-tree-open-archived-trees
  8376. (org-hide-archived-subtrees (point-min) (point-max)))
  8377. (run-hooks 'org-occur-hook)
  8378. (if (interactive-p)
  8379. (message "%d match(es) for regexp %s" cnt regexp))
  8380. cnt))
  8381. (defun org-show-context (&optional key)
  8382. "Make sure point and context and visible.
  8383. How much context is shown depends upon the variables
  8384. `org-show-hierarchy-above', `org-show-following-heading'. and
  8385. `org-show-siblings'."
  8386. (let ((heading-p (org-on-heading-p t))
  8387. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8388. (following-p (org-get-alist-option org-show-following-heading key))
  8389. (entry-p (org-get-alist-option org-show-entry-below key))
  8390. (siblings-p (org-get-alist-option org-show-siblings key)))
  8391. (catch 'exit
  8392. ;; Show heading or entry text
  8393. (if (and heading-p (not entry-p))
  8394. (org-flag-heading nil) ; only show the heading
  8395. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8396. (org-show-hidden-entry))) ; show entire entry
  8397. (when following-p
  8398. ;; Show next sibling, or heading below text
  8399. (save-excursion
  8400. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8401. (org-flag-heading nil))))
  8402. (when siblings-p (org-show-siblings))
  8403. (when hierarchy-p
  8404. ;; show all higher headings, possibly with siblings
  8405. (save-excursion
  8406. (while (and (condition-case nil
  8407. (progn (org-up-heading-all 1) t)
  8408. (error nil))
  8409. (not (bobp)))
  8410. (org-flag-heading nil)
  8411. (when siblings-p (org-show-siblings))))))))
  8412. (defun org-reveal (&optional siblings)
  8413. "Show current entry, hierarchy above it, and the following headline.
  8414. This can be used to show a consistent set of context around locations
  8415. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8416. not t for the search context.
  8417. With optional argument SIBLINGS, on each level of the hierarchy all
  8418. siblings are shown. This repairs the tree structure to what it would
  8419. look like when opened with hierarchical calls to `org-cycle'."
  8420. (interactive "P")
  8421. (let ((org-show-hierarchy-above t)
  8422. (org-show-following-heading t)
  8423. (org-show-siblings (if siblings t org-show-siblings)))
  8424. (org-show-context nil)))
  8425. (defun org-highlight-new-match (beg end)
  8426. "Highlight from BEG to END and mark the highlight is an occur headline."
  8427. (let ((ov (org-make-overlay beg end)))
  8428. (org-overlay-put ov 'face 'secondary-selection)
  8429. (push ov org-occur-highlights)))
  8430. (defun org-remove-occur-highlights (&optional beg end noremove)
  8431. "Remove the occur highlights from the buffer.
  8432. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8433. from the `before-change-functions' in the current buffer."
  8434. (interactive)
  8435. (unless org-inhibit-highlight-removal
  8436. (mapc 'org-delete-overlay org-occur-highlights)
  8437. (setq org-occur-highlights nil)
  8438. (setq org-occur-parameters nil)
  8439. (unless noremove
  8440. (remove-hook 'before-change-functions
  8441. 'org-remove-occur-highlights 'local))))
  8442. ;;;; Priorities
  8443. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8444. "Regular expression matching the priority indicator.")
  8445. (defvar org-remove-priority-next-time nil)
  8446. (defun org-priority-up ()
  8447. "Increase the priority of the current item."
  8448. (interactive)
  8449. (org-priority 'up))
  8450. (defun org-priority-down ()
  8451. "Decrease the priority of the current item."
  8452. (interactive)
  8453. (org-priority 'down))
  8454. (defun org-priority (&optional action)
  8455. "Change the priority of an item by ARG.
  8456. ACTION can be `set', `up', `down', or a character."
  8457. (interactive)
  8458. (setq action (or action 'set))
  8459. (let (current new news have remove)
  8460. (save-excursion
  8461. (org-back-to-heading)
  8462. (if (looking-at org-priority-regexp)
  8463. (setq current (string-to-char (match-string 2))
  8464. have t)
  8465. (setq current org-default-priority))
  8466. (cond
  8467. ((or (eq action 'set)
  8468. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8469. (if (not (eq action 'set))
  8470. (setq new action)
  8471. (message "Priority %c-%c, SPC to remove: "
  8472. org-highest-priority org-lowest-priority)
  8473. (setq new (read-char-exclusive)))
  8474. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8475. (= (upcase org-lowest-priority) org-lowest-priority))
  8476. (setq new (upcase new)))
  8477. (cond ((equal new ?\ ) (setq remove t))
  8478. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8479. (error "Priority must be between `%c' and `%c'"
  8480. org-highest-priority org-lowest-priority))))
  8481. ((eq action 'up)
  8482. (if (and (not have) (eq last-command this-command))
  8483. (setq new org-lowest-priority)
  8484. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8485. org-default-priority (1- current)))))
  8486. ((eq action 'down)
  8487. (if (and (not have) (eq last-command this-command))
  8488. (setq new org-highest-priority)
  8489. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8490. org-default-priority (1+ current)))))
  8491. (t (error "Invalid action")))
  8492. (if (or (< (upcase new) org-highest-priority)
  8493. (> (upcase new) org-lowest-priority))
  8494. (setq remove t))
  8495. (setq news (format "%c" new))
  8496. (if have
  8497. (if remove
  8498. (replace-match "" t t nil 1)
  8499. (replace-match news t t nil 2))
  8500. (if remove
  8501. (error "No priority cookie found in line")
  8502. (looking-at org-todo-line-regexp)
  8503. (if (match-end 2)
  8504. (progn
  8505. (goto-char (match-end 2))
  8506. (insert " [#" news "]"))
  8507. (goto-char (match-beginning 3))
  8508. (insert "[#" news "] ")))))
  8509. (org-preserve-lc (org-set-tags nil 'align))
  8510. (if remove
  8511. (message "Priority removed")
  8512. (message "Priority of current item set to %s" news))))
  8513. (defun org-get-priority (s)
  8514. "Find priority cookie and return priority."
  8515. (save-match-data
  8516. (if (not (string-match org-priority-regexp s))
  8517. (* 1000 (- org-lowest-priority org-default-priority))
  8518. (* 1000 (- org-lowest-priority
  8519. (string-to-char (match-string 2 s)))))))
  8520. ;;;; Tags
  8521. (defvar org-agenda-archives-mode)
  8522. (defun org-scan-tags (action matcher &optional todo-only)
  8523. "Scan headline tags with inheritance and produce output ACTION.
  8524. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8525. or `agenda' to produce an entry list for an agenda view. It can also be
  8526. a Lisp form or a function that should be called at each matched headline, in
  8527. this case the return value is a list of all return values from these calls.
  8528. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8529. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8530. only lines with a TODO keyword are included in the output."
  8531. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8532. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8533. (org-re
  8534. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8535. (props (list 'face 'default
  8536. 'done-face 'org-done
  8537. 'undone-face 'default
  8538. 'mouse-face 'highlight
  8539. 'org-not-done-regexp org-not-done-regexp
  8540. 'org-todo-regexp org-todo-regexp
  8541. 'keymap org-agenda-keymap
  8542. 'help-echo
  8543. (format "mouse-2 or RET jump to org file %s"
  8544. (abbreviate-file-name
  8545. (or (buffer-file-name (buffer-base-buffer))
  8546. (buffer-name (buffer-base-buffer)))))))
  8547. (case-fold-search nil)
  8548. lspos tags tags-list
  8549. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8550. (llast 0) rtn rtn1 level category i txt
  8551. todo marker entry priority)
  8552. (when (not (member action '(agenda sparse-tree)))
  8553. (setq action (list 'lambda nil action)))
  8554. (save-excursion
  8555. (goto-char (point-min))
  8556. (when (eq action 'sparse-tree)
  8557. (org-overview)
  8558. (org-remove-occur-highlights))
  8559. (while (re-search-forward re nil t)
  8560. (catch :skip
  8561. (setq todo (if (match-end 1) (match-string 2))
  8562. tags (if (match-end 4) (match-string 4)))
  8563. (goto-char (setq lspos (1+ (match-beginning 0))))
  8564. (setq level (org-reduced-level (funcall outline-level))
  8565. category (org-get-category))
  8566. (setq i llast llast level)
  8567. ;; remove tag lists from same and sublevels
  8568. (while (>= i level)
  8569. (when (setq entry (assoc i tags-alist))
  8570. (setq tags-alist (delete entry tags-alist)))
  8571. (setq i (1- i)))
  8572. ;; add the next tags
  8573. (when tags
  8574. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8575. tags-alist
  8576. (cons (cons level tags) tags-alist)))
  8577. ;; compile tags for current headline
  8578. (setq tags-list
  8579. (if org-use-tag-inheritance
  8580. (apply 'append (mapcar 'cdr tags-alist))
  8581. tags))
  8582. (when (and tags org-use-tag-inheritance
  8583. (not (eq t org-use-tag-inheritance)))
  8584. ;; selective inheritance, remove uninherited ones
  8585. (setcdr (car tags-alist)
  8586. (org-remove-uniherited-tags (cdar tags-alist))))
  8587. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8588. (eval matcher)
  8589. (or
  8590. (not (member org-archive-tag tags-list))
  8591. ;; we have an archive tag, should we use this anyway?
  8592. (or (not org-agenda-skip-archived-trees)
  8593. (and (eq action 'agenda) org-agenda-archives-mode))))
  8594. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8595. ;; select this headline
  8596. (cond
  8597. ((eq action 'sparse-tree)
  8598. (and org-highlight-sparse-tree-matches
  8599. (org-get-heading) (match-end 0)
  8600. (org-highlight-new-match
  8601. (match-beginning 0) (match-beginning 1)))
  8602. (org-show-context 'tags-tree))
  8603. ((eq action 'agenda)
  8604. (setq txt (org-format-agenda-item
  8605. ""
  8606. (concat
  8607. (if org-tags-match-list-sublevels
  8608. (make-string (1- level) ?.) "")
  8609. (org-get-heading))
  8610. category tags-list)
  8611. priority (org-get-priority txt))
  8612. (goto-char lspos)
  8613. (setq marker (org-agenda-new-marker))
  8614. (org-add-props txt props
  8615. 'org-marker marker 'org-hd-marker marker 'org-category category
  8616. 'priority priority 'type "tagsmatch")
  8617. (push txt rtn))
  8618. ((functionp action)
  8619. (save-excursion
  8620. (setq rtn1 (funcall action))
  8621. (push rtn1 rtn))
  8622. (goto-char (point-at-eol)))
  8623. (t (error "Invalid action")))
  8624. ;; if we are to skip sublevels, jump to end of subtree
  8625. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8626. (when (and (eq action 'sparse-tree)
  8627. (not org-sparse-tree-open-archived-trees))
  8628. (org-hide-archived-subtrees (point-min) (point-max)))
  8629. (nreverse rtn)))
  8630. (defun org-remove-uniherited-tags (tags)
  8631. "Remove all tags that are not inherited from the list TAGS."
  8632. (cond
  8633. ((eq org-use-tag-inheritance t) tags)
  8634. ((not org-use-tag-inheritance) nil)
  8635. ((stringp org-use-tag-inheritance)
  8636. (delq nil (mapcar
  8637. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8638. tags)))
  8639. ((listp org-use-tag-inheritance)
  8640. (org-delete-all org-use-tag-inheritance tags))))
  8641. (defvar todo-only) ;; dynamically scoped
  8642. (defun org-tags-sparse-tree (&optional todo-only match)
  8643. "Create a sparse tree according to tags string MATCH.
  8644. MATCH can contain positive and negative selection of tags, like
  8645. \"+WORK+URGENT-WITHBOSS\".
  8646. If optional argument TODO_ONLY is non-nil, only select lines that are
  8647. also TODO lines."
  8648. (interactive "P")
  8649. (org-prepare-agenda-buffers (list (current-buffer)))
  8650. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8651. (defvar org-cached-props nil)
  8652. (defun org-cached-entry-get (pom property)
  8653. (if (or (eq t org-use-property-inheritance)
  8654. (and (stringp org-use-property-inheritance)
  8655. (string-match org-use-property-inheritance property))
  8656. (and (listp org-use-property-inheritance)
  8657. (member property org-use-property-inheritance)))
  8658. ;; Caching is not possible, check it directly
  8659. (org-entry-get pom property 'inherit)
  8660. ;; Get all properties, so that we can do complicated checks easily
  8661. (cdr (assoc property (or org-cached-props
  8662. (setq org-cached-props
  8663. (org-entry-properties pom)))))))
  8664. (defun org-global-tags-completion-table (&optional files)
  8665. "Return the list of all tags in all agenda buffer/files."
  8666. (save-excursion
  8667. (org-uniquify
  8668. (delq nil
  8669. (apply 'append
  8670. (mapcar
  8671. (lambda (file)
  8672. (set-buffer (find-file-noselect file))
  8673. (append (org-get-buffer-tags)
  8674. (mapcar (lambda (x) (if (stringp (car-safe x))
  8675. (list (car-safe x)) nil))
  8676. org-tag-alist)))
  8677. (if (and files (car files))
  8678. files
  8679. (org-agenda-files))))))))
  8680. (defun org-make-tags-matcher (match)
  8681. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8682. ;; todo-only is scoped dynamically into this function, and the function
  8683. ;; may change it it the matcher asksk for it.
  8684. (unless match
  8685. ;; Get a new match request, with completion
  8686. (let ((org-last-tags-completion-table
  8687. (org-global-tags-completion-table)))
  8688. (setq match (completing-read
  8689. "Match: " 'org-tags-completion-function nil nil nil
  8690. 'org-tags-history))))
  8691. ;; Parse the string and create a lisp form
  8692. (let ((match0 match)
  8693. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8694. minus tag mm
  8695. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8696. orterms term orlist re-p str-p level-p level-op time-p
  8697. prop-p pn pv po cat-p gv)
  8698. (if (string-match "/+" match)
  8699. ;; match contains also a todo-matching request
  8700. (progn
  8701. (setq tagsmatch (substring match 0 (match-beginning 0))
  8702. todomatch (substring match (match-end 0)))
  8703. (if (string-match "^!" todomatch)
  8704. (setq todo-only t todomatch (substring todomatch 1)))
  8705. (if (string-match "^\\s-*$" todomatch)
  8706. (setq todomatch nil)))
  8707. ;; only matching tags
  8708. (setq tagsmatch match todomatch nil))
  8709. ;; Make the tags matcher
  8710. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8711. (setq tagsmatcher t)
  8712. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8713. (while (setq term (pop orterms))
  8714. (while (and (equal (substring term -1) "\\") orterms)
  8715. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8716. (while (string-match re term)
  8717. (setq minus (and (match-end 1)
  8718. (equal (match-string 1 term) "-"))
  8719. tag (match-string 2 term)
  8720. re-p (equal (string-to-char tag) ?{)
  8721. level-p (match-end 4)
  8722. prop-p (match-end 5)
  8723. mm (cond
  8724. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8725. (level-p
  8726. (setq level-op (org-op-to-function (match-string 3 term)))
  8727. `(,level-op level ,(string-to-number
  8728. (match-string 4 term))))
  8729. (prop-p
  8730. (setq pn (match-string 5 term)
  8731. po (match-string 6 term)
  8732. pv (match-string 7 term)
  8733. cat-p (equal pn "CATEGORY")
  8734. re-p (equal (string-to-char pv) ?{)
  8735. str-p (equal (string-to-char pv) ?\")
  8736. time-p (save-match-data (string-match "^\"<.*>\"$" pv))
  8737. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8738. (if time-p (setq pv (org-matcher-time pv)))
  8739. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8740. (if (equal pn "CATEGORY")
  8741. (setq gv '(get-text-property (point) 'org-category))
  8742. (setq gv `(org-cached-entry-get nil ,pn)))
  8743. (if re-p
  8744. (if (eq po 'org<>)
  8745. `(not (string-match ,pv (or ,gv "")))
  8746. `(string-match ,pv (or ,gv "")))
  8747. (if str-p
  8748. `(,po (or ,gv "") ,pv)
  8749. `(,po (string-to-number (or ,gv ""))
  8750. ,(string-to-number pv) ))))
  8751. (t `(member ,(downcase tag) tags-list)))
  8752. mm (if minus (list 'not mm) mm)
  8753. term (substring term (match-end 0)))
  8754. (push mm tagsmatcher))
  8755. (push (if (> (length tagsmatcher) 1)
  8756. (cons 'and tagsmatcher)
  8757. (car tagsmatcher))
  8758. orlist)
  8759. (setq tagsmatcher nil))
  8760. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8761. (setq tagsmatcher
  8762. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8763. ;; Make the todo matcher
  8764. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8765. (setq todomatcher t)
  8766. (setq orterms (org-split-string todomatch "|") orlist nil)
  8767. (while (setq term (pop orterms))
  8768. (while (string-match re term)
  8769. (setq minus (and (match-end 1)
  8770. (equal (match-string 1 term) "-"))
  8771. kwd (match-string 2 term)
  8772. re-p (equal (string-to-char kwd) ?{)
  8773. term (substring term (match-end 0))
  8774. mm (if re-p
  8775. `(string-match ,(substring kwd 1 -1) todo)
  8776. (list 'equal 'todo kwd))
  8777. mm (if minus (list 'not mm) mm))
  8778. (push mm todomatcher))
  8779. (push (if (> (length todomatcher) 1)
  8780. (cons 'and todomatcher)
  8781. (car todomatcher))
  8782. orlist)
  8783. (setq todomatcher nil))
  8784. (setq todomatcher (if (> (length orlist) 1)
  8785. (cons 'or orlist) (car orlist))))
  8786. ;; Return the string and lisp forms of the matcher
  8787. (setq matcher (if todomatcher
  8788. (list 'and tagsmatcher todomatcher)
  8789. tagsmatcher))
  8790. (cons match0 matcher)))
  8791. (defun org-op-to-function (op &optional stringp)
  8792. "Turn an operator into the appropriate function."
  8793. (setq op
  8794. (cond
  8795. ((equal op "<" ) '(< string< org-time<))
  8796. ((equal op ">" ) '(> org-string> org-time>))
  8797. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8798. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8799. ((member op '("=" "==")) '(= string= org-time=))
  8800. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8801. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8802. (defun org<> (a b) (not (= a b)))
  8803. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8804. (defun org-string>= (a b) (not (string< a b)))
  8805. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8806. (defun org-string<> (a b) (not (string= a b)))
  8807. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8808. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8809. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8810. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8811. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8812. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8813. (defun org-2ft (s)
  8814. "Convert S to a floating point time.
  8815. If S is already a number, just return it. If it is a string, parse
  8816. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8817. (cond
  8818. ((numberp s) s)
  8819. ((stringp s)
  8820. (condition-case nil
  8821. (float-time (apply 'encode-time (org-parse-time-string s)))
  8822. (error 0.)))
  8823. (t 0.)))
  8824. (defun org-matcher-time (s)
  8825. (cond
  8826. ((equal s "<now>") (float-time))
  8827. ((equal s "<today>")
  8828. (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
  8829. (t (org-2ft s))))
  8830. (defun org-match-any-p (re list)
  8831. "Does re match any element of list?"
  8832. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8833. (delq nil list))
  8834. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8835. (defvar org-tags-overlay (org-make-overlay 1 1))
  8836. (org-detach-overlay org-tags-overlay)
  8837. (defun org-get-tags-at (&optional pos)
  8838. "Get a list of all headline tags applicable at POS.
  8839. POS defaults to point. If tags are inherited, the list contains
  8840. the targets in the same sequence as the headlines appear, i.e.
  8841. the tags of the current headline come last."
  8842. (interactive)
  8843. (let (tags ltags lastpos parent)
  8844. (save-excursion
  8845. (save-restriction
  8846. (widen)
  8847. (goto-char (or pos (point)))
  8848. (save-match-data
  8849. (condition-case nil
  8850. (progn
  8851. (org-back-to-heading t)
  8852. (while (not (equal lastpos (point)))
  8853. (setq lastpos (point))
  8854. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8855. (setq ltags (org-split-string
  8856. (org-match-string-no-properties 1) ":"))
  8857. (setq tags (append (org-remove-uniherited-tags ltags)
  8858. tags)))
  8859. (or org-use-tag-inheritance (error ""))
  8860. (org-up-heading-all 1)
  8861. (setq parent t)))
  8862. (error nil))))
  8863. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8864. (defun org-toggle-tag (tag &optional onoff)
  8865. "Toggle the tag TAG for the current line.
  8866. If ONOFF is `on' or `off', don't toggle but set to this state."
  8867. (unless (org-on-heading-p t) (error "Not on headling"))
  8868. (let (res current)
  8869. (save-excursion
  8870. (beginning-of-line)
  8871. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8872. (point-at-eol) t)
  8873. (progn
  8874. (setq current (match-string 1))
  8875. (replace-match ""))
  8876. (setq current ""))
  8877. (setq current (nreverse (org-split-string current ":")))
  8878. (cond
  8879. ((eq onoff 'on)
  8880. (setq res t)
  8881. (or (member tag current) (push tag current)))
  8882. ((eq onoff 'off)
  8883. (or (not (member tag current)) (setq current (delete tag current))))
  8884. (t (if (member tag current)
  8885. (setq current (delete tag current))
  8886. (setq res t)
  8887. (push tag current))))
  8888. (end-of-line 1)
  8889. (if current
  8890. (progn
  8891. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8892. (org-set-tags nil t))
  8893. (delete-horizontal-space))
  8894. (run-hooks 'org-after-tags-change-hook))
  8895. res))
  8896. (defun org-align-tags-here (to-col)
  8897. ;; Assumes that this is a headline
  8898. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8899. (beginning-of-line 1)
  8900. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8901. (< pos (match-beginning 2)))
  8902. (progn
  8903. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8904. (goto-char (match-beginning 1))
  8905. (insert " ")
  8906. (delete-region (point) (1+ (match-beginning 2)))
  8907. (setq ncol (max (1+ (current-column))
  8908. (1+ col)
  8909. (if (> to-col 0)
  8910. to-col
  8911. (- (abs to-col) tags-l))))
  8912. (setq p (point))
  8913. (insert (make-string (- ncol (current-column)) ?\ ))
  8914. (setq ncol (current-column))
  8915. (when indent-tabs-mode (tabify p (point-at-eol)))
  8916. (org-move-to-column (min ncol col) t))
  8917. (goto-char pos))))
  8918. (defun org-set-tags (&optional arg just-align)
  8919. "Set the tags for the current headline.
  8920. With prefix ARG, realign all tags in headings in the current buffer."
  8921. (interactive "P")
  8922. (let* ((re (concat "^" outline-regexp))
  8923. (current (org-get-tags-string))
  8924. (col (current-column))
  8925. (org-setting-tags t)
  8926. table current-tags inherited-tags ; computed below when needed
  8927. tags p0 c0 c1 rpl)
  8928. (if arg
  8929. (save-excursion
  8930. (goto-char (point-min))
  8931. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8932. (while (re-search-forward re nil t)
  8933. (org-set-tags nil t)
  8934. (end-of-line 1)))
  8935. (message "All tags realigned to column %d" org-tags-column))
  8936. (if just-align
  8937. (setq tags current)
  8938. ;; Get a new set of tags from the user
  8939. (save-excursion
  8940. (setq table (or org-tag-alist (org-get-buffer-tags))
  8941. org-last-tags-completion-table table
  8942. current-tags (org-split-string current ":")
  8943. inherited-tags (nreverse
  8944. (nthcdr (length current-tags)
  8945. (nreverse (org-get-tags-at))))
  8946. tags
  8947. (if (or (eq t org-use-fast-tag-selection)
  8948. (and org-use-fast-tag-selection
  8949. (delq nil (mapcar 'cdr table))))
  8950. (org-fast-tag-selection
  8951. current-tags inherited-tags table
  8952. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8953. (let ((org-add-colon-after-tag-completion t))
  8954. (org-trim
  8955. (org-without-partial-completion
  8956. (completing-read "Tags: " 'org-tags-completion-function
  8957. nil nil current 'org-tags-history)))))))
  8958. (while (string-match "[-+&]+" tags)
  8959. ;; No boolean logic, just a list
  8960. (setq tags (replace-match ":" t t tags))))
  8961. (if (string-match "\\`[\t ]*\\'" tags)
  8962. (setq tags "")
  8963. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8964. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8965. ;; Insert new tags at the correct column
  8966. (beginning-of-line 1)
  8967. (cond
  8968. ((and (equal current "") (equal tags "")))
  8969. ((re-search-forward
  8970. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8971. (point-at-eol) t)
  8972. (if (equal tags "")
  8973. (setq rpl "")
  8974. (goto-char (match-beginning 0))
  8975. (setq c0 (current-column) p0 (point)
  8976. c1 (max (1+ c0) (if (> org-tags-column 0)
  8977. org-tags-column
  8978. (- (- org-tags-column) (length tags))))
  8979. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8980. (replace-match rpl t t)
  8981. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8982. tags)
  8983. (t (error "Tags alignment failed")))
  8984. (org-move-to-column col)
  8985. (unless just-align
  8986. (run-hooks 'org-after-tags-change-hook)))))
  8987. (defun org-change-tag-in-region (beg end tag off)
  8988. "Add or remove TAG for each entry in the region.
  8989. This works in the agenda, and also in an org-mode buffer."
  8990. (interactive
  8991. (list (region-beginning) (region-end)
  8992. (let ((org-last-tags-completion-table
  8993. (if (org-mode-p)
  8994. (org-get-buffer-tags)
  8995. (org-global-tags-completion-table))))
  8996. (completing-read
  8997. "Tag: " 'org-tags-completion-function nil nil nil
  8998. 'org-tags-history))
  8999. (progn
  9000. (message "[s]et or [r]emove? ")
  9001. (equal (read-char-exclusive) ?r))))
  9002. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9003. (let ((agendap (equal major-mode 'org-agenda-mode))
  9004. l1 l2 m buf pos newhead (cnt 0))
  9005. (goto-char end)
  9006. (setq l2 (1- (org-current-line)))
  9007. (goto-char beg)
  9008. (setq l1 (org-current-line))
  9009. (loop for l from l1 to l2 do
  9010. (goto-line l)
  9011. (setq m (get-text-property (point) 'org-hd-marker))
  9012. (when (or (and (org-mode-p) (org-on-heading-p))
  9013. (and agendap m))
  9014. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9015. pos (if agendap m (point)))
  9016. (with-current-buffer buf
  9017. (save-excursion
  9018. (save-restriction
  9019. (goto-char pos)
  9020. (setq cnt (1+ cnt))
  9021. (org-toggle-tag tag (if off 'off 'on))
  9022. (setq newhead (org-get-heading)))))
  9023. (and agendap (org-agenda-change-all-lines newhead m))))
  9024. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9025. (defun org-tags-completion-function (string predicate &optional flag)
  9026. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9027. (confirm (lambda (x) (stringp (car x)))))
  9028. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9029. (setq s1 (match-string 1 string)
  9030. s2 (match-string 2 string))
  9031. (setq s1 "" s2 string))
  9032. (cond
  9033. ((eq flag nil)
  9034. ;; try completion
  9035. (setq rtn (try-completion s2 ctable confirm))
  9036. (if (stringp rtn)
  9037. (setq rtn
  9038. (concat s1 s2 (substring rtn (length s2))
  9039. (if (and org-add-colon-after-tag-completion
  9040. (assoc rtn ctable))
  9041. ":" ""))))
  9042. rtn)
  9043. ((eq flag t)
  9044. ;; all-completions
  9045. (all-completions s2 ctable confirm)
  9046. )
  9047. ((eq flag 'lambda)
  9048. ;; exact match?
  9049. (assoc s2 ctable)))
  9050. ))
  9051. (defun org-fast-tag-insert (kwd tags face &optional end)
  9052. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  9053. (insert (format "%-12s" (concat kwd ":"))
  9054. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9055. (or end "")))
  9056. (defun org-fast-tag-show-exit (flag)
  9057. (save-excursion
  9058. (goto-line 3)
  9059. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9060. (replace-match ""))
  9061. (when flag
  9062. (end-of-line 1)
  9063. (org-move-to-column (- (window-width) 19) t)
  9064. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9065. (defun org-set-current-tags-overlay (current prefix)
  9066. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9067. (if (featurep 'xemacs)
  9068. (org-overlay-display org-tags-overlay (concat prefix s)
  9069. 'secondary-selection)
  9070. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9071. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9072. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9073. "Fast tag selection with single keys.
  9074. CURRENT is the current list of tags in the headline, INHERITED is the
  9075. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9076. possibly with grouping information. TODO-TABLE is a similar table with
  9077. TODO keywords, should these have keys assigned to them.
  9078. If the keys are nil, a-z are automatically assigned.
  9079. Returns the new tags string, or nil to not change the current settings."
  9080. (let* ((fulltable (append table todo-table))
  9081. (maxlen (apply 'max (mapcar
  9082. (lambda (x)
  9083. (if (stringp (car x)) (string-width (car x)) 0))
  9084. fulltable)))
  9085. (buf (current-buffer))
  9086. (expert (eq org-fast-tag-selection-single-key 'expert))
  9087. (buffer-tags nil)
  9088. (fwidth (+ maxlen 3 1 3))
  9089. (ncol (/ (- (window-width) 4) fwidth))
  9090. (i-face 'org-done)
  9091. (c-face 'org-todo)
  9092. tg cnt e c char c1 c2 ntable tbl rtn
  9093. ov-start ov-end ov-prefix
  9094. (exit-after-next org-fast-tag-selection-single-key)
  9095. (done-keywords org-done-keywords)
  9096. groups ingroup)
  9097. (save-excursion
  9098. (beginning-of-line 1)
  9099. (if (looking-at
  9100. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9101. (setq ov-start (match-beginning 1)
  9102. ov-end (match-end 1)
  9103. ov-prefix "")
  9104. (setq ov-start (1- (point-at-eol))
  9105. ov-end (1+ ov-start))
  9106. (skip-chars-forward "^\n\r")
  9107. (setq ov-prefix
  9108. (concat
  9109. (buffer-substring (1- (point)) (point))
  9110. (if (> (current-column) org-tags-column)
  9111. " "
  9112. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9113. (org-move-overlay org-tags-overlay ov-start ov-end)
  9114. (save-window-excursion
  9115. (if expert
  9116. (set-buffer (get-buffer-create " *Org tags*"))
  9117. (delete-other-windows)
  9118. (split-window-vertically)
  9119. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9120. (erase-buffer)
  9121. (org-set-local 'org-done-keywords done-keywords)
  9122. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9123. (org-fast-tag-insert "Current" current c-face "\n\n")
  9124. (org-fast-tag-show-exit exit-after-next)
  9125. (org-set-current-tags-overlay current ov-prefix)
  9126. (setq tbl fulltable char ?a cnt 0)
  9127. (while (setq e (pop tbl))
  9128. (cond
  9129. ((equal e '(:startgroup))
  9130. (push '() groups) (setq ingroup t)
  9131. (when (not (= cnt 0))
  9132. (setq cnt 0)
  9133. (insert "\n"))
  9134. (insert "{ "))
  9135. ((equal e '(:endgroup))
  9136. (setq ingroup nil cnt 0)
  9137. (insert "}\n"))
  9138. (t
  9139. (setq tg (car e) c2 nil)
  9140. (if (cdr e)
  9141. (setq c (cdr e))
  9142. ;; automatically assign a character.
  9143. (setq c1 (string-to-char
  9144. (downcase (substring
  9145. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9146. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9147. (while (or (rassoc char ntable) (rassoc char table))
  9148. (setq char (1+ char)))
  9149. (setq c2 c1))
  9150. (setq c (or c2 char)))
  9151. (if ingroup (push tg (car groups)))
  9152. (setq tg (org-add-props tg nil 'face
  9153. (cond
  9154. ((not (assoc tg table))
  9155. (org-get-todo-face tg))
  9156. ((member tg current) c-face)
  9157. ((member tg inherited) i-face)
  9158. (t nil))))
  9159. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9160. (insert "[" c "] " tg (make-string
  9161. (- fwidth 4 (length tg)) ?\ ))
  9162. (push (cons tg c) ntable)
  9163. (when (= (setq cnt (1+ cnt)) ncol)
  9164. (insert "\n")
  9165. (if ingroup (insert " "))
  9166. (setq cnt 0)))))
  9167. (setq ntable (nreverse ntable))
  9168. (insert "\n")
  9169. (goto-char (point-min))
  9170. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  9171. (fit-window-to-buffer))
  9172. (setq rtn
  9173. (catch 'exit
  9174. (while t
  9175. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9176. (if groups " [!] no groups" " [!]groups")
  9177. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9178. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9179. (cond
  9180. ((= c ?\r) (throw 'exit t))
  9181. ((= c ?!)
  9182. (setq groups (not groups))
  9183. (goto-char (point-min))
  9184. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9185. ((= c ?\C-c)
  9186. (if (not expert)
  9187. (org-fast-tag-show-exit
  9188. (setq exit-after-next (not exit-after-next)))
  9189. (setq expert nil)
  9190. (delete-other-windows)
  9191. (split-window-vertically)
  9192. (org-switch-to-buffer-other-window " *Org tags*")
  9193. (and (fboundp 'fit-window-to-buffer)
  9194. (fit-window-to-buffer))))
  9195. ((or (= c ?\C-g)
  9196. (and (= c ?q) (not (rassoc c ntable))))
  9197. (org-detach-overlay org-tags-overlay)
  9198. (setq quit-flag t))
  9199. ((= c ?\ )
  9200. (setq current nil)
  9201. (if exit-after-next (setq exit-after-next 'now)))
  9202. ((= c ?\t)
  9203. (condition-case nil
  9204. (setq tg (completing-read
  9205. "Tag: "
  9206. (or buffer-tags
  9207. (with-current-buffer buf
  9208. (org-get-buffer-tags)))))
  9209. (quit (setq tg "")))
  9210. (when (string-match "\\S-" tg)
  9211. (add-to-list 'buffer-tags (list tg))
  9212. (if (member tg current)
  9213. (setq current (delete tg current))
  9214. (push tg current)))
  9215. (if exit-after-next (setq exit-after-next 'now)))
  9216. ((setq e (rassoc c todo-table) tg (car e))
  9217. (with-current-buffer buf
  9218. (save-excursion (org-todo tg)))
  9219. (if exit-after-next (setq exit-after-next 'now)))
  9220. ((setq e (rassoc c ntable) tg (car e))
  9221. (if (member tg current)
  9222. (setq current (delete tg current))
  9223. (loop for g in groups do
  9224. (if (member tg g)
  9225. (mapc (lambda (x)
  9226. (setq current (delete x current)))
  9227. g)))
  9228. (push tg current))
  9229. (if exit-after-next (setq exit-after-next 'now))))
  9230. ;; Create a sorted list
  9231. (setq current
  9232. (sort current
  9233. (lambda (a b)
  9234. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9235. (if (eq exit-after-next 'now) (throw 'exit t))
  9236. (goto-char (point-min))
  9237. (beginning-of-line 2)
  9238. (delete-region (point) (point-at-eol))
  9239. (org-fast-tag-insert "Current" current c-face)
  9240. (org-set-current-tags-overlay current ov-prefix)
  9241. (while (re-search-forward
  9242. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9243. (setq tg (match-string 1))
  9244. (add-text-properties
  9245. (match-beginning 1) (match-end 1)
  9246. (list 'face
  9247. (cond
  9248. ((member tg current) c-face)
  9249. ((member tg inherited) i-face)
  9250. (t (get-text-property (match-beginning 1) 'face))))))
  9251. (goto-char (point-min)))))
  9252. (org-detach-overlay org-tags-overlay)
  9253. (if rtn
  9254. (mapconcat 'identity current ":")
  9255. nil))))
  9256. (defun org-get-tags-string ()
  9257. "Get the TAGS string in the current headline."
  9258. (unless (org-on-heading-p t)
  9259. (error "Not on a heading"))
  9260. (save-excursion
  9261. (beginning-of-line 1)
  9262. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9263. (org-match-string-no-properties 1)
  9264. "")))
  9265. (defun org-get-tags ()
  9266. "Get the list of tags specified in the current headline."
  9267. (org-split-string (org-get-tags-string) ":"))
  9268. (defun org-get-buffer-tags ()
  9269. "Get a table of all tags used in the buffer, for completion."
  9270. (let (tags)
  9271. (save-excursion
  9272. (goto-char (point-min))
  9273. (while (re-search-forward
  9274. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9275. (when (equal (char-after (point-at-bol 0)) ?*)
  9276. (mapc (lambda (x) (add-to-list 'tags x))
  9277. (org-split-string (org-match-string-no-properties 1) ":")))))
  9278. (mapcar 'list tags)))
  9279. ;;;; The mapping API
  9280. ;;;###autoload
  9281. (defun org-map-entries (func &optional match scope &rest skip)
  9282. "Call FUNC at each headline selected by MATCH in SCOPE.
  9283. FUNC is a function or a lisp form. The function will be called without
  9284. arguments, with the cursor positioned at the beginning of the headline.
  9285. The return values of all calls to the function will be collected and
  9286. returned as a list.
  9287. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9288. Only headlines that are matched by this query will be considered during
  9289. the iteration. When MATCH is nil or t, all headlines will be
  9290. visited by the iteration.
  9291. SCOPE determines the scope of this command. It can be any of:
  9292. nil The current buffer, respecting the restriction if any
  9293. tree The subtree started with the entry at point
  9294. file The current buffer, without restriction
  9295. file-with-archives
  9296. The current buffer, and any archives associated with it
  9297. agenda All agenda files
  9298. agenda-with-archives
  9299. All agenda files with any archive files associated with them
  9300. \(file1 file2 ...)
  9301. If this is a list, all files in the list will be scanned
  9302. The remaining args are treated as settings for the skipping facilities of
  9303. the scanner. The following items can be given here:
  9304. archive skip trees with the archive tag.
  9305. comment skip trees with the COMMENT keyword
  9306. function or Emacs Lisp form:
  9307. will be used as value for `org-agenda-skip-function', so whenever
  9308. the the function returns t, FUNC will not be called for that
  9309. entry and search will continue from the point where the
  9310. function leaves it."
  9311. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9312. (org-agenda-skip-archived-trees (memq 'archive skip))
  9313. (org-agenda-skip-comment-trees (memq 'comment skip))
  9314. (org-agenda-skip-function
  9315. (car (org-delete-all '(comment archive) skip)))
  9316. (org-tags-match-list-sublevels t)
  9317. matcher pos file)
  9318. (cond
  9319. ((eq match t) (setq matcher t))
  9320. ((eq match nil) (setq matcher t))
  9321. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  9322. (when (eq scope 'tree)
  9323. (org-back-to-heading t)
  9324. (org-narrow-to-subtree)
  9325. (setq scope nil))
  9326. (if (not scope)
  9327. (progn
  9328. (org-prepare-agenda-buffers
  9329. (list (buffer-file-name (current-buffer))))
  9330. (org-scan-tags func matcher))
  9331. ;; Get the right scope
  9332. (setq pos (point))
  9333. (cond
  9334. ((and scope (listp scope) (symbolp (car scope)))
  9335. (setq scope (eval scope)))
  9336. ((eq scope 'agenda)
  9337. (setq scope (org-agenda-files t)))
  9338. ((eq scope 'agenda-with-archives)
  9339. (setq scope (org-agenda-files t))
  9340. (setq scope (org-add-archive-files scope)))
  9341. ((eq scope 'file)
  9342. (setq scope (list (buffer-file-name))))
  9343. ((eq scope 'file-with-archives)
  9344. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9345. (org-prepare-agenda-buffers scope)
  9346. (while (setq file (pop scope))
  9347. (with-current-buffer (org-find-base-buffer-visiting file)
  9348. (save-excursion
  9349. (save-restriction
  9350. (widen)
  9351. (goto-char (point-min))
  9352. (org-scan-tags func matcher))))))))
  9353. ;;;; Properties
  9354. ;;; Setting and retrieving properties
  9355. (defconst org-special-properties
  9356. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9357. "TIMESTAMP" "TIMESTAMP_IA")
  9358. "The special properties valid in Org-mode.
  9359. These are properties that are not defined in the property drawer,
  9360. but in some other way.")
  9361. (defconst org-default-properties
  9362. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9363. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9364. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9365. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9366. "Some properties that are used by Org-mode for various purposes.
  9367. Being in this list makes sure that they are offered for completion.")
  9368. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9369. "Regular expression matching the first line of a property drawer.")
  9370. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9371. "Regular expression matching the first line of a property drawer.")
  9372. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9373. "Regular expression matching the first line of a property drawer.")
  9374. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9375. "Regular expression matching the first line of a property drawer.")
  9376. (defconst org-property-drawer-re
  9377. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9378. org-property-end-re "\\)\n?")
  9379. "Matches an entire property drawer.")
  9380. (defconst org-clock-drawer-re
  9381. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9382. org-property-end-re "\\)\n?")
  9383. "Matches an entire clock drawer.")
  9384. (defun org-property-action ()
  9385. "Do an action on properties."
  9386. (interactive)
  9387. (let (c)
  9388. (org-at-property-p)
  9389. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9390. (setq c (read-char-exclusive))
  9391. (cond
  9392. ((equal c ?s)
  9393. (call-interactively 'org-set-property))
  9394. ((equal c ?d)
  9395. (call-interactively 'org-delete-property))
  9396. ((equal c ?D)
  9397. (call-interactively 'org-delete-property-globally))
  9398. ((equal c ?c)
  9399. (call-interactively 'org-compute-property-at-point))
  9400. (t (error "No such property action %c" c)))))
  9401. (defun org-at-property-p ()
  9402. "Is the cursor in a property line?"
  9403. ;; FIXME: Does not check if we are actually in the drawer.
  9404. ;; FIXME: also returns true on any drawers.....
  9405. ;; This is used by C-c C-c for property action.
  9406. (save-excursion
  9407. (beginning-of-line 1)
  9408. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9409. (defun org-get-property-block (&optional beg end force)
  9410. "Return the (beg . end) range of the body of the property drawer.
  9411. BEG and END can be beginning and end of subtree, if not given
  9412. they will be found.
  9413. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9414. (catch 'exit
  9415. (save-excursion
  9416. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9417. (end (or end (progn (outline-next-heading) (point)))))
  9418. (goto-char beg)
  9419. (if (re-search-forward org-property-start-re end t)
  9420. (setq beg (1+ (match-end 0)))
  9421. (if force
  9422. (save-excursion
  9423. (org-insert-property-drawer)
  9424. (setq end (progn (outline-next-heading) (point))))
  9425. (throw 'exit nil))
  9426. (goto-char beg)
  9427. (if (re-search-forward org-property-start-re end t)
  9428. (setq beg (1+ (match-end 0)))))
  9429. (if (re-search-forward org-property-end-re end t)
  9430. (setq end (match-beginning 0))
  9431. (or force (throw 'exit nil))
  9432. (goto-char beg)
  9433. (setq end beg)
  9434. (org-indent-line-function)
  9435. (insert ":END:\n"))
  9436. (cons beg end)))))
  9437. (defun org-entry-properties (&optional pom which)
  9438. "Get all properties of the entry at point-or-marker POM.
  9439. This includes the TODO keyword, the tags, time strings for deadline,
  9440. scheduled, and clocking, and any additional properties defined in the
  9441. entry. The return value is an alist, keys may occur multiple times
  9442. if the property key was used several times.
  9443. POM may also be nil, in which case the current entry is used.
  9444. If WHICH is nil or `all', get all properties. If WHICH is
  9445. `special' or `standard', only get that subclass."
  9446. (setq which (or which 'all))
  9447. (org-with-point-at pom
  9448. (let ((clockstr (substring org-clock-string 0 -1))
  9449. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9450. beg end range props sum-props key value string clocksum)
  9451. (save-excursion
  9452. (when (condition-case nil (org-back-to-heading t) (error nil))
  9453. (setq beg (point))
  9454. (setq sum-props (get-text-property (point) 'org-summaries))
  9455. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9456. (outline-next-heading)
  9457. (setq end (point))
  9458. (when (memq which '(all special))
  9459. ;; Get the special properties, like TODO and tags
  9460. (goto-char beg)
  9461. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9462. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9463. (when (looking-at org-priority-regexp)
  9464. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9465. (when (and (setq value (org-get-tags-string))
  9466. (string-match "\\S-" value))
  9467. (push (cons "TAGS" value) props))
  9468. (when (setq value (org-get-tags-at))
  9469. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9470. props))
  9471. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9472. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9473. string (if (equal key clockstr)
  9474. (org-no-properties
  9475. (org-trim
  9476. (buffer-substring
  9477. (match-beginning 3) (goto-char (point-at-eol)))))
  9478. (substring (org-match-string-no-properties 3) 1 -1)))
  9479. (unless key
  9480. (if (= (char-after (match-beginning 3)) ?\[)
  9481. (setq key "TIMESTAMP_IA")
  9482. (setq key "TIMESTAMP")))
  9483. (when (or (equal key clockstr) (not (assoc key props)))
  9484. (push (cons key string) props)))
  9485. )
  9486. (when (memq which '(all standard))
  9487. ;; Get the standard properties, like :PORP: ...
  9488. (setq range (org-get-property-block beg end))
  9489. (when range
  9490. (goto-char (car range))
  9491. (while (re-search-forward
  9492. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9493. (cdr range) t)
  9494. (setq key (org-match-string-no-properties 1)
  9495. value (org-trim (or (org-match-string-no-properties 2) "")))
  9496. (unless (member key excluded)
  9497. (push (cons key (or value "")) props)))))
  9498. (if clocksum
  9499. (push (cons "CLOCKSUM"
  9500. (org-columns-number-to-string (/ (float clocksum) 60.)
  9501. 'add_times))
  9502. props))
  9503. (append sum-props (nreverse props)))))))
  9504. (defun org-entry-get (pom property &optional inherit)
  9505. "Get value of PROPERTY for entry at point-or-marker POM.
  9506. If INHERIT is non-nil and the entry does not have the property,
  9507. then also check higher levels of the hierarchy.
  9508. If INHERIT is the symbol `selective', use inheritance only if the setting
  9509. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9510. If the property is present but empty, the return value is the empty string.
  9511. If the property is not present at all, nil is returned."
  9512. (org-with-point-at pom
  9513. (if (and inherit (if (eq inherit 'selective)
  9514. (org-property-inherit-p property)
  9515. t))
  9516. (org-entry-get-with-inheritance property)
  9517. (if (member property org-special-properties)
  9518. ;; We need a special property. Use brute force, get all properties.
  9519. (cdr (assoc property (org-entry-properties nil 'special)))
  9520. (let ((range (org-get-property-block)))
  9521. (if (and range
  9522. (goto-char (car range))
  9523. (re-search-forward
  9524. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9525. (cdr range) t))
  9526. ;; Found the property, return it.
  9527. (if (match-end 1)
  9528. (org-match-string-no-properties 1)
  9529. "")))))))
  9530. (defun org-property-or-variable-value (var &optional inherit)
  9531. "Check if there is a property fixing the value of VAR.
  9532. If yes, return this value. If not, return the current value of the variable."
  9533. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9534. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9535. (read prop)
  9536. (symbol-value var))))
  9537. (defun org-entry-delete (pom property)
  9538. "Delete the property PROPERTY from entry at point-or-marker POM."
  9539. (org-with-point-at pom
  9540. (if (member property org-special-properties)
  9541. nil ; cannot delete these properties.
  9542. (let ((range (org-get-property-block)))
  9543. (if (and range
  9544. (goto-char (car range))
  9545. (re-search-forward
  9546. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9547. (cdr range) t))
  9548. (progn
  9549. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9550. t)
  9551. nil)))))
  9552. ;; Multi-values properties are properties that contain multiple values
  9553. ;; These values are assumed to be single words, separated by whitespace.
  9554. (defun org-entry-add-to-multivalued-property (pom property value)
  9555. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9556. (let* ((old (org-entry-get pom property))
  9557. (values (and old (org-split-string old "[ \t]"))))
  9558. (unless (member value values)
  9559. (setq values (cons value values))
  9560. (org-entry-put pom property
  9561. (mapconcat 'identity values " ")))))
  9562. (defun org-entry-remove-from-multivalued-property (pom property value)
  9563. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9564. (let* ((old (org-entry-get pom property))
  9565. (values (and old (org-split-string old "[ \t]"))))
  9566. (when (member value values)
  9567. (setq values (delete value values))
  9568. (org-entry-put pom property
  9569. (mapconcat 'identity values " ")))))
  9570. (defun org-entry-member-in-multivalued-property (pom property value)
  9571. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9572. (let* ((old (org-entry-get pom property))
  9573. (values (and old (org-split-string old "[ \t]"))))
  9574. (member value values)))
  9575. (defvar org-entry-property-inherited-from (make-marker))
  9576. (defun org-entry-get-with-inheritance (property)
  9577. "Get entry property, and search higher levels if not present."
  9578. (let (tmp)
  9579. (save-excursion
  9580. (save-restriction
  9581. (widen)
  9582. (catch 'ex
  9583. (while t
  9584. (when (setq tmp (org-entry-get nil property))
  9585. (org-back-to-heading t)
  9586. (move-marker org-entry-property-inherited-from (point))
  9587. (throw 'ex tmp))
  9588. (or (org-up-heading-safe) (throw 'ex nil)))))
  9589. (or tmp
  9590. (cdr (assoc property org-file-properties))
  9591. (cdr (assoc property org-global-properties))
  9592. (cdr (assoc property org-global-properties-fixed))))))
  9593. (defun org-entry-put (pom property value)
  9594. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9595. (org-with-point-at pom
  9596. (org-back-to-heading t)
  9597. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9598. range)
  9599. (cond
  9600. ((equal property "TODO")
  9601. (when (and (stringp value) (string-match "\\S-" value)
  9602. (not (member value org-todo-keywords-1)))
  9603. (error "\"%s\" is not a valid TODO state" value))
  9604. (if (or (not value)
  9605. (not (string-match "\\S-" value)))
  9606. (setq value 'none))
  9607. (org-todo value)
  9608. (org-set-tags nil 'align))
  9609. ((equal property "PRIORITY")
  9610. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9611. (string-to-char value) ?\ ))
  9612. (org-set-tags nil 'align))
  9613. ((equal property "SCHEDULED")
  9614. (if (re-search-forward org-scheduled-time-regexp end t)
  9615. (cond
  9616. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9617. ((eq value 'later) (org-timestamp-change 1 'day))
  9618. (t (call-interactively 'org-schedule)))
  9619. (call-interactively 'org-schedule)))
  9620. ((equal property "DEADLINE")
  9621. (if (re-search-forward org-deadline-time-regexp end t)
  9622. (cond
  9623. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9624. ((eq value 'later) (org-timestamp-change 1 'day))
  9625. (t (call-interactively 'org-deadline)))
  9626. (call-interactively 'org-deadline)))
  9627. ((member property org-special-properties)
  9628. (error "The %s property can not yet be set with `org-entry-put'"
  9629. property))
  9630. (t ; a non-special property
  9631. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9632. (setq range (org-get-property-block beg end 'force))
  9633. (goto-char (car range))
  9634. (if (re-search-forward
  9635. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9636. (progn
  9637. (delete-region (match-beginning 1) (match-end 1))
  9638. (goto-char (match-beginning 1)))
  9639. (goto-char (cdr range))
  9640. (insert "\n")
  9641. (backward-char 1)
  9642. (org-indent-line-function)
  9643. (insert ":" property ":"))
  9644. (and value (insert " " value))
  9645. (org-indent-line-function)))))))
  9646. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9647. "Get all property keys in the current buffer.
  9648. With INCLUDE-SPECIALS, also list the special properties that relect things
  9649. like tags and TODO state.
  9650. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9651. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9652. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9653. formats in the current buffer."
  9654. (let (rtn range cfmt cols s p)
  9655. (save-excursion
  9656. (save-restriction
  9657. (widen)
  9658. (goto-char (point-min))
  9659. (while (re-search-forward org-property-start-re nil t)
  9660. (setq range (org-get-property-block))
  9661. (goto-char (car range))
  9662. (while (re-search-forward
  9663. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9664. (cdr range) t)
  9665. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9666. (outline-next-heading))))
  9667. (when include-specials
  9668. (setq rtn (append org-special-properties rtn)))
  9669. (when include-defaults
  9670. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9671. (when include-columns
  9672. (save-excursion
  9673. (save-restriction
  9674. (widen)
  9675. (goto-char (point-min))
  9676. (while (re-search-forward
  9677. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9678. nil t)
  9679. (setq cfmt (match-string 2) s 0)
  9680. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9681. cfmt s)
  9682. (setq s (match-end 0)
  9683. p (match-string 1 cfmt))
  9684. (unless (or (equal p "ITEM")
  9685. (member p org-special-properties))
  9686. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9687. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9688. (defun org-property-values (key)
  9689. "Return a list of all values of property KEY."
  9690. (save-excursion
  9691. (save-restriction
  9692. (widen)
  9693. (goto-char (point-min))
  9694. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9695. values)
  9696. (while (re-search-forward re nil t)
  9697. (add-to-list 'values (org-trim (match-string 1))))
  9698. (delete "" values)))))
  9699. (defun org-insert-property-drawer ()
  9700. "Insert a property drawer into the current entry."
  9701. (interactive)
  9702. (org-back-to-heading t)
  9703. (looking-at outline-regexp)
  9704. (let ((indent (- (match-end 0)(match-beginning 0)))
  9705. (beg (point))
  9706. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9707. end hiddenp)
  9708. (outline-next-heading)
  9709. (setq end (point))
  9710. (goto-char beg)
  9711. (while (re-search-forward re end t))
  9712. (setq hiddenp (org-invisible-p))
  9713. (end-of-line 1)
  9714. (and (equal (char-after) ?\n) (forward-char 1))
  9715. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9716. (beginning-of-line 2))
  9717. (org-skip-over-state-notes)
  9718. (skip-chars-backward " \t\n\r")
  9719. (if (eq (char-before) ?*) (forward-char 1))
  9720. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9721. (beginning-of-line 0)
  9722. (org-indent-to-column indent)
  9723. (beginning-of-line 2)
  9724. (org-indent-to-column indent)
  9725. (beginning-of-line 0)
  9726. (if hiddenp
  9727. (save-excursion
  9728. (org-back-to-heading t)
  9729. (hide-entry))
  9730. (org-flag-drawer t))))
  9731. (defun org-set-property (property value)
  9732. "In the current entry, set PROPERTY to VALUE.
  9733. When called interactively, this will prompt for a property name, offering
  9734. completion on existing and default properties. And then it will prompt
  9735. for a value, offering competion either on allowed values (via an inherited
  9736. xxx_ALL property) or on existing values in other instances of this property
  9737. in the current file."
  9738. (interactive
  9739. (let* ((completion-ignore-case t)
  9740. (keys (org-buffer-property-keys nil t t))
  9741. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9742. (prop (if (member prop0 keys)
  9743. prop0
  9744. (or (cdr (assoc (downcase prop0)
  9745. (mapcar (lambda (x) (cons (downcase x) x))
  9746. keys)))
  9747. prop0)))
  9748. (cur (org-entry-get nil prop))
  9749. (allowed (org-property-get-allowed-values nil prop 'table))
  9750. (existing (mapcar 'list (org-property-values prop)))
  9751. (val (if allowed
  9752. (org-completing-read "Value: " allowed nil 'req-match)
  9753. (org-completing-read
  9754. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9755. (concat "[" cur "]") "")
  9756. ": ")
  9757. existing nil nil "" nil cur))))
  9758. (list prop (if (equal val "") cur val))))
  9759. (unless (equal (org-entry-get nil property) value)
  9760. (org-entry-put nil property value)))
  9761. (defun org-delete-property (property)
  9762. "In the current entry, delete PROPERTY."
  9763. (interactive
  9764. (let* ((completion-ignore-case t)
  9765. (prop (completing-read
  9766. "Property: " (org-entry-properties nil 'standard))))
  9767. (list prop)))
  9768. (message "Property %s %s" property
  9769. (if (org-entry-delete nil property)
  9770. "deleted"
  9771. "was not present in the entry")))
  9772. (defun org-delete-property-globally (property)
  9773. "Remove PROPERTY globally, from all entries."
  9774. (interactive
  9775. (let* ((completion-ignore-case t)
  9776. (prop (completing-read
  9777. "Globally remove property: "
  9778. (mapcar 'list (org-buffer-property-keys)))))
  9779. (list prop)))
  9780. (save-excursion
  9781. (save-restriction
  9782. (widen)
  9783. (goto-char (point-min))
  9784. (let ((cnt 0))
  9785. (while (re-search-forward
  9786. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9787. nil t)
  9788. (setq cnt (1+ cnt))
  9789. (replace-match ""))
  9790. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9791. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9792. (defun org-compute-property-at-point ()
  9793. "Compute the property at point.
  9794. This looks for an enclosing column format, extracts the operator and
  9795. then applies it to the proerty in the column format's scope."
  9796. (interactive)
  9797. (unless (org-at-property-p)
  9798. (error "Not at a property"))
  9799. (let ((prop (org-match-string-no-properties 2)))
  9800. (org-columns-get-format-and-top-level)
  9801. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9802. (error "No operator defined for property %s" prop))
  9803. (org-columns-compute prop)))
  9804. (defun org-property-get-allowed-values (pom property &optional table)
  9805. "Get allowed values for the property PROPERTY.
  9806. When TABLE is non-nil, return an alist that can directly be used for
  9807. completion."
  9808. (let (vals)
  9809. (cond
  9810. ((equal property "TODO")
  9811. (setq vals (org-with-point-at pom
  9812. (append org-todo-keywords-1 '("")))))
  9813. ((equal property "PRIORITY")
  9814. (let ((n org-lowest-priority))
  9815. (while (>= n org-highest-priority)
  9816. (push (char-to-string n) vals)
  9817. (setq n (1- n)))))
  9818. ((member property org-special-properties))
  9819. (t
  9820. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9821. (when (and vals (string-match "\\S-" vals))
  9822. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9823. (setq vals (mapcar (lambda (x)
  9824. (cond ((stringp x) x)
  9825. ((numberp x) (number-to-string x))
  9826. ((symbolp x) (symbol-name x))
  9827. (t "???")))
  9828. vals)))))
  9829. (if table (mapcar 'list vals) vals)))
  9830. (defun org-property-previous-allowed-value (&optional previous)
  9831. "Switch to the next allowed value for this property."
  9832. (interactive)
  9833. (org-property-next-allowed-value t))
  9834. (defun org-property-next-allowed-value (&optional previous)
  9835. "Switch to the next allowed value for this property."
  9836. (interactive)
  9837. (unless (org-at-property-p)
  9838. (error "Not at a property"))
  9839. (let* ((key (match-string 2))
  9840. (value (match-string 3))
  9841. (allowed (or (org-property-get-allowed-values (point) key)
  9842. (and (member value '("[ ]" "[-]" "[X]"))
  9843. '("[ ]" "[X]"))))
  9844. nval)
  9845. (unless allowed
  9846. (error "Allowed values for this property have not been defined"))
  9847. (if previous (setq allowed (reverse allowed)))
  9848. (if (member value allowed)
  9849. (setq nval (car (cdr (member value allowed)))))
  9850. (setq nval (or nval (car allowed)))
  9851. (if (equal nval value)
  9852. (error "Only one allowed value for this property"))
  9853. (org-at-property-p)
  9854. (replace-match (concat " :" key ": " nval) t t)
  9855. (org-indent-line-function)
  9856. (beginning-of-line 1)
  9857. (skip-chars-forward " \t")))
  9858. (defun org-find-entry-with-id (ident)
  9859. "Locate the entry that contains the ID property with exact value IDENT.
  9860. IDENT can be a string, a symbol or a number, this function will search for
  9861. the string representation of it.
  9862. Return the position where this entry starts, or nil if there is no such entry."
  9863. (let ((id (cond
  9864. ((stringp ident) ident)
  9865. ((symbol-name ident) (symbol-name ident))
  9866. ((numberp ident) (number-to-string ident))
  9867. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9868. (case-fold-search nil))
  9869. (save-excursion
  9870. (save-restriction
  9871. (widen)
  9872. (goto-char (point-min))
  9873. (when (re-search-forward
  9874. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9875. nil t)
  9876. (org-back-to-heading)
  9877. (point))))))
  9878. ;;;; Timestamps
  9879. (defvar org-last-changed-timestamp nil)
  9880. (defvar org-last-inserted-timestamp nil
  9881. "The last time stamp inserted with `org-insert-time-stamp'.")
  9882. (defvar org-time-was-given) ; dynamically scoped parameter
  9883. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9884. (defvar org-ts-what) ; dynamically scoped parameter
  9885. (defun org-time-stamp (arg)
  9886. "Prompt for a date/time and insert a time stamp.
  9887. If the user specifies a time like HH:MM, or if this command is called
  9888. with a prefix argument, the time stamp will contain date and time.
  9889. Otherwise, only the date will be included. All parts of a date not
  9890. specified by the user will be filled in from the current date/time.
  9891. So if you press just return without typing anything, the time stamp
  9892. will represent the current date/time. If there is already a timestamp
  9893. at the cursor, it will be modified."
  9894. (interactive "P")
  9895. (let* ((ts nil)
  9896. (default-time
  9897. ;; Default time is either today, or, when entering a range,
  9898. ;; the range start.
  9899. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9900. (save-excursion
  9901. (re-search-backward
  9902. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9903. (- (point) 20) t)))
  9904. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9905. (current-time)))
  9906. (default-input (and ts (org-get-compact-tod ts)))
  9907. org-time-was-given org-end-time-was-given time)
  9908. (cond
  9909. ((and (org-at-timestamp-p)
  9910. (eq last-command 'org-time-stamp)
  9911. (eq this-command 'org-time-stamp))
  9912. (insert "--")
  9913. (setq time (let ((this-command this-command))
  9914. (org-read-date arg 'totime nil nil default-time default-input)))
  9915. (org-insert-time-stamp time (or org-time-was-given arg)))
  9916. ((org-at-timestamp-p)
  9917. (setq time (let ((this-command this-command))
  9918. (org-read-date arg 'totime nil nil default-time default-input)))
  9919. (when (org-at-timestamp-p) ; just to get the match data
  9920. (replace-match "")
  9921. (setq org-last-changed-timestamp
  9922. (org-insert-time-stamp
  9923. time (or org-time-was-given arg)
  9924. nil nil nil (list org-end-time-was-given))))
  9925. (message "Timestamp updated"))
  9926. (t
  9927. (setq time (let ((this-command this-command))
  9928. (org-read-date arg 'totime nil nil default-time default-input)))
  9929. (org-insert-time-stamp time (or org-time-was-given arg)
  9930. nil nil nil (list org-end-time-was-given))))))
  9931. ;; FIXME: can we use this for something else, like computing time differences?
  9932. (defun org-get-compact-tod (s)
  9933. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9934. (let* ((t1 (match-string 1 s))
  9935. (h1 (string-to-number (match-string 2 s)))
  9936. (m1 (string-to-number (match-string 3 s)))
  9937. (t2 (and (match-end 4) (match-string 5 s)))
  9938. (h2 (and t2 (string-to-number (match-string 6 s))))
  9939. (m2 (and t2 (string-to-number (match-string 7 s))))
  9940. dh dm)
  9941. (if (not t2)
  9942. t1
  9943. (setq dh (- h2 h1) dm (- m2 m1))
  9944. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9945. (concat t1 "+" (number-to-string dh)
  9946. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9947. (defun org-time-stamp-inactive (&optional arg)
  9948. "Insert an inactive time stamp.
  9949. An inactive time stamp is enclosed in square brackets instead of angle
  9950. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9951. does not link to the calendar and cannot be changed with the S-cursor keys.
  9952. So these are more for recording a certain time/date."
  9953. (interactive "P")
  9954. (let (org-time-was-given org-end-time-was-given time)
  9955. (setq time (org-read-date arg 'totime))
  9956. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9957. nil nil (list org-end-time-was-given))))
  9958. (defvar org-date-ovl (org-make-overlay 1 1))
  9959. (org-overlay-put org-date-ovl 'face 'org-warning)
  9960. (org-detach-overlay org-date-ovl)
  9961. (defvar org-ans1) ; dynamically scoped parameter
  9962. (defvar org-ans2) ; dynamically scoped parameter
  9963. (defvar org-plain-time-of-day-regexp) ; defined below
  9964. (defvar org-overriding-default-time nil) ; dynamically scoped
  9965. (defvar org-read-date-overlay nil)
  9966. (defvar org-dcst nil) ; dynamically scoped
  9967. (defun org-read-date (&optional with-time to-time from-string prompt
  9968. default-time default-input)
  9969. "Read a date, possibly a time, and make things smooth for the user.
  9970. The prompt will suggest to enter an ISO date, but you can also enter anything
  9971. which will at least partially be understood by `parse-time-string'.
  9972. Unrecognized parts of the date will default to the current day, month, year,
  9973. hour and minute. If this command is called to replace a timestamp at point,
  9974. of to enter the second timestamp of a range, the default time is taken from the
  9975. existing stamp. For example,
  9976. 3-2-5 --> 2003-02-05
  9977. feb 15 --> currentyear-02-15
  9978. sep 12 9 --> 2009-09-12
  9979. 12:45 --> today 12:45
  9980. 22 sept 0:34 --> currentyear-09-22 0:34
  9981. 12 --> currentyear-currentmonth-12
  9982. Fri --> nearest Friday (today or later)
  9983. etc.
  9984. Furthermore you can specify a relative date by giving, as the *first* thing
  9985. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9986. change in days weeks, months, years.
  9987. With a single plus or minus, the date is relative to today. With a double
  9988. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9989. +4d --> four days from today
  9990. +4 --> same as above
  9991. +2w --> two weeks from today
  9992. ++5 --> five days from default date
  9993. The function understands only English month and weekday abbreviations,
  9994. but this can be configured with the variables `parse-time-months' and
  9995. `parse-time-weekdays'.
  9996. While prompting, a calendar is popped up - you can also select the
  9997. date with the mouse (button 1). The calendar shows a period of three
  9998. months. To scroll it to other months, use the keys `>' and `<'.
  9999. If you don't like the calendar, turn it off with
  10000. \(setq org-read-date-popup-calendar nil)
  10001. With optional argument TO-TIME, the date will immediately be converted
  10002. to an internal time.
  10003. With an optional argument WITH-TIME, the prompt will suggest to also
  10004. insert a time. Note that when WITH-TIME is not set, you can still
  10005. enter a time, and this function will inform the calling routine about
  10006. this change. The calling routine may then choose to change the format
  10007. used to insert the time stamp into the buffer to include the time.
  10008. With optional argument FROM-STRING, read from this string instead from
  10009. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10010. the time/date that is used for everything that is not specified by the
  10011. user."
  10012. (require 'parse-time)
  10013. (let* ((org-time-stamp-rounding-minutes
  10014. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10015. (org-dcst org-display-custom-times)
  10016. (ct (org-current-time))
  10017. (def (or org-overriding-default-time default-time ct))
  10018. (defdecode (decode-time def))
  10019. (dummy (progn
  10020. (when (< (nth 2 defdecode) org-extend-today-until)
  10021. (setcar (nthcdr 2 defdecode) -1)
  10022. (setcar (nthcdr 1 defdecode) 59)
  10023. (setq def (apply 'encode-time defdecode)
  10024. defdecode (decode-time def)))))
  10025. (calendar-move-hook nil)
  10026. (calendar-view-diary-initially-flag nil)
  10027. (view-diary-entries-initially nil)
  10028. (calendar-view-holidays-initially-flag nil)
  10029. (view-calendar-holidays-initially nil)
  10030. (timestr (format-time-string
  10031. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10032. (prompt (concat (if prompt (concat prompt " ") "")
  10033. (format "Date+time [%s]: " timestr)))
  10034. ans (org-ans0 "") org-ans1 org-ans2 final)
  10035. (cond
  10036. (from-string (setq ans from-string))
  10037. (org-read-date-popup-calendar
  10038. (save-excursion
  10039. (save-window-excursion
  10040. (calendar)
  10041. (calendar-forward-day (- (time-to-days def)
  10042. (calendar-absolute-from-gregorian
  10043. (calendar-current-date))))
  10044. (org-eval-in-calendar nil t)
  10045. (let* ((old-map (current-local-map))
  10046. (map (copy-keymap calendar-mode-map))
  10047. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10048. (org-defkey map (kbd "RET") 'org-calendar-select)
  10049. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10050. 'org-calendar-select-mouse)
  10051. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10052. 'org-calendar-select-mouse)
  10053. (org-defkey minibuffer-local-map [(meta shift left)]
  10054. (lambda () (interactive)
  10055. (org-eval-in-calendar '(calendar-backward-month 1))))
  10056. (org-defkey minibuffer-local-map [(meta shift right)]
  10057. (lambda () (interactive)
  10058. (org-eval-in-calendar '(calendar-forward-month 1))))
  10059. (org-defkey minibuffer-local-map [(meta shift up)]
  10060. (lambda () (interactive)
  10061. (org-eval-in-calendar '(calendar-backward-year 1))))
  10062. (org-defkey minibuffer-local-map [(meta shift down)]
  10063. (lambda () (interactive)
  10064. (org-eval-in-calendar '(calendar-forward-year 1))))
  10065. (org-defkey minibuffer-local-map [(shift up)]
  10066. (lambda () (interactive)
  10067. (org-eval-in-calendar '(calendar-backward-week 1))))
  10068. (org-defkey minibuffer-local-map [(shift down)]
  10069. (lambda () (interactive)
  10070. (org-eval-in-calendar '(calendar-forward-week 1))))
  10071. (org-defkey minibuffer-local-map [(shift left)]
  10072. (lambda () (interactive)
  10073. (org-eval-in-calendar '(calendar-backward-day 1))))
  10074. (org-defkey minibuffer-local-map [(shift right)]
  10075. (lambda () (interactive)
  10076. (org-eval-in-calendar '(calendar-forward-day 1))))
  10077. (org-defkey minibuffer-local-map ">"
  10078. (lambda () (interactive)
  10079. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10080. (org-defkey minibuffer-local-map "<"
  10081. (lambda () (interactive)
  10082. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10083. (unwind-protect
  10084. (progn
  10085. (use-local-map map)
  10086. (add-hook 'post-command-hook 'org-read-date-display)
  10087. (setq org-ans0 (read-string prompt default-input nil nil))
  10088. ;; org-ans0: from prompt
  10089. ;; org-ans1: from mouse click
  10090. ;; org-ans2: from calendar motion
  10091. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10092. (remove-hook 'post-command-hook 'org-read-date-display)
  10093. (use-local-map old-map)
  10094. (when org-read-date-overlay
  10095. (org-delete-overlay org-read-date-overlay)
  10096. (setq org-read-date-overlay nil)))))))
  10097. (t ; Naked prompt only
  10098. (unwind-protect
  10099. (setq ans (read-string prompt default-input nil timestr))
  10100. (when org-read-date-overlay
  10101. (org-delete-overlay org-read-date-overlay)
  10102. (setq org-read-date-overlay nil)))))
  10103. (setq final (org-read-date-analyze ans def defdecode))
  10104. (if to-time
  10105. (apply 'encode-time final)
  10106. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10107. (format "%04d-%02d-%02d %02d:%02d"
  10108. (nth 5 final) (nth 4 final) (nth 3 final)
  10109. (nth 2 final) (nth 1 final))
  10110. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10111. (defvar def)
  10112. (defvar defdecode)
  10113. (defvar with-time)
  10114. (defun org-read-date-display ()
  10115. "Display the currrent date prompt interpretation in the minibuffer."
  10116. (when org-read-date-display-live
  10117. (when org-read-date-overlay
  10118. (org-delete-overlay org-read-date-overlay))
  10119. (let ((p (point)))
  10120. (end-of-line 1)
  10121. (while (not (equal (buffer-substring
  10122. (max (point-min) (- (point) 4)) (point))
  10123. " "))
  10124. (insert " "))
  10125. (goto-char p))
  10126. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10127. " " (or org-ans1 org-ans2)))
  10128. (org-end-time-was-given nil)
  10129. (f (org-read-date-analyze ans def defdecode))
  10130. (fmts (if org-dcst
  10131. org-time-stamp-custom-formats
  10132. org-time-stamp-formats))
  10133. (fmt (if (or with-time
  10134. (and (boundp 'org-time-was-given) org-time-was-given))
  10135. (cdr fmts)
  10136. (car fmts)))
  10137. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10138. (when (and org-end-time-was-given
  10139. (string-match org-plain-time-of-day-regexp txt))
  10140. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10141. org-end-time-was-given
  10142. (substring txt (match-end 0)))))
  10143. (setq org-read-date-overlay
  10144. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10145. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10146. (defun org-read-date-analyze (ans def defdecode)
  10147. "Analyze the combined answer of the date prompt."
  10148. ;; FIXME: cleanup and comment
  10149. (let (delta deltan deltaw deltadef year month day
  10150. hour minute second wday pm h2 m2 tl wday1
  10151. iso-year iso-weekday iso-week iso-year iso-date)
  10152. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10153. (setq ans "+0"))
  10154. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10155. (setq ans (replace-match "" t t ans)
  10156. deltan (car delta)
  10157. deltaw (nth 1 delta)
  10158. deltadef (nth 2 delta)))
  10159. ;; Check if there is an iso week date in there
  10160. ;; If yes, sore the info and ostpone interpreting it until the rest
  10161. ;; of the parsing is done
  10162. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10163. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10164. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10165. iso-week (string-to-number (match-string 2 ans)))
  10166. (setq ans (replace-match "" t t ans)))
  10167. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10168. (when (string-match
  10169. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10170. (setq year (if (match-end 2)
  10171. (string-to-number (match-string 2 ans))
  10172. (string-to-number (format-time-string "%Y")))
  10173. month (string-to-number (match-string 3 ans))
  10174. day (string-to-number (match-string 4 ans)))
  10175. (if (< year 100) (setq year (+ 2000 year)))
  10176. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10177. t nil ans)))
  10178. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10179. ;; If there is a time with am/pm, and *no* time without it, we convert
  10180. ;; so that matching will be successful.
  10181. (loop for i from 1 to 2 do ; twice, for end time as well
  10182. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10183. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10184. (setq hour (string-to-number (match-string 1 ans))
  10185. minute (if (match-end 3)
  10186. (string-to-number (match-string 3 ans))
  10187. 0)
  10188. pm (equal ?p
  10189. (string-to-char (downcase (match-string 4 ans)))))
  10190. (if (and (= hour 12) (not pm))
  10191. (setq hour 0)
  10192. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10193. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10194. t t ans))))
  10195. ;; Check if a time range is given as a duration
  10196. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10197. (setq hour (string-to-number (match-string 1 ans))
  10198. h2 (+ hour (string-to-number (match-string 3 ans)))
  10199. minute (string-to-number (match-string 2 ans))
  10200. m2 (+ minute (if (match-end 5) (string-to-number
  10201. (match-string 5 ans))0)))
  10202. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10203. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10204. t t ans)))
  10205. ;; Check if there is a time range
  10206. (when (boundp 'org-end-time-was-given)
  10207. (setq org-time-was-given nil)
  10208. (when (and (string-match org-plain-time-of-day-regexp ans)
  10209. (match-end 8))
  10210. (setq org-end-time-was-given (match-string 8 ans))
  10211. (setq ans (concat (substring ans 0 (match-beginning 7))
  10212. (substring ans (match-end 7))))))
  10213. (setq tl (parse-time-string ans)
  10214. day (or (nth 3 tl) (nth 3 defdecode))
  10215. month (or (nth 4 tl)
  10216. (if (and org-read-date-prefer-future
  10217. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10218. (1+ (nth 4 defdecode))
  10219. (nth 4 defdecode)))
  10220. year (or (nth 5 tl)
  10221. (if (and org-read-date-prefer-future
  10222. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10223. (1+ (nth 5 defdecode))
  10224. (nth 5 defdecode)))
  10225. hour (or (nth 2 tl) (nth 2 defdecode))
  10226. minute (or (nth 1 tl) (nth 1 defdecode))
  10227. second (or (nth 0 tl) 0)
  10228. wday (nth 6 tl))
  10229. ;; Special date definitions below
  10230. (cond
  10231. (iso-week
  10232. ;; There was an iso week
  10233. (setq year (or iso-year year)
  10234. day (or iso-weekday wday 1)
  10235. wday nil ; to make sure that the trigger below does not match
  10236. iso-date (calendar-gregorian-from-absolute
  10237. (calendar-absolute-from-iso
  10238. (list iso-week day year))))
  10239. ; FIXME: Should we also push ISO weeks into the future?
  10240. ; (when (and org-read-date-prefer-future
  10241. ; (not iso-year)
  10242. ; (< (calendar-absolute-from-gregorian iso-date)
  10243. ; (time-to-days (current-time))))
  10244. ; (setq year (1+ year)
  10245. ; iso-date (calendar-gregorian-from-absolute
  10246. ; (calendar-absolute-from-iso
  10247. ; (list iso-week day year)))))
  10248. (setq month (car iso-date)
  10249. year (nth 2 iso-date)
  10250. day (nth 1 iso-date)))
  10251. (deltan
  10252. (unless deltadef
  10253. (let ((now (decode-time (current-time))))
  10254. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10255. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10256. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10257. ((equal deltaw "m") (setq month (+ month deltan)))
  10258. ((equal deltaw "y") (setq year (+ year deltan)))))
  10259. ((and wday (not (nth 3 tl)))
  10260. ;; Weekday was given, but no day, so pick that day in the week
  10261. ;; on or after the derived date.
  10262. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10263. (unless (equal wday wday1)
  10264. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10265. (if (and (boundp 'org-time-was-given)
  10266. (nth 2 tl))
  10267. (setq org-time-was-given t))
  10268. (if (< year 100) (setq year (+ 2000 year)))
  10269. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10270. (list second minute hour day month year)))
  10271. (defvar parse-time-weekdays)
  10272. (defun org-read-date-get-relative (s today default)
  10273. "Check string S for special relative date string.
  10274. TODAY and DEFAULT are internal times, for today and for a default.
  10275. Return shift list (N what def-flag)
  10276. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10277. N is the number of WHATs to shift.
  10278. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10279. the DEFAULT date rather than TODAY."
  10280. (when (and
  10281. (string-match
  10282. (concat
  10283. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10284. "\\([0-9]+\\)?"
  10285. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10286. "\\([ \t]\\|$\\)") s)
  10287. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10288. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10289. (string-to-char (substring (match-string 1 s) -1))
  10290. ?+))
  10291. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10292. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10293. (what (if (match-end 3) (match-string 3 s) "d"))
  10294. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10295. (date (if rel default today))
  10296. (wday (nth 6 (decode-time date)))
  10297. delta)
  10298. (if wday1
  10299. (progn
  10300. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10301. (if (= dir ?-) (setq delta (- delta 7)))
  10302. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10303. (list delta "d" rel))
  10304. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10305. (defun org-eval-in-calendar (form &optional keepdate)
  10306. "Eval FORM in the calendar window and return to current window.
  10307. Also, store the cursor date in variable org-ans2."
  10308. (let ((sw (selected-window)))
  10309. (select-window (get-buffer-window "*Calendar*"))
  10310. (eval form)
  10311. (when (and (not keepdate) (calendar-cursor-to-date))
  10312. (let* ((date (calendar-cursor-to-date))
  10313. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10314. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10315. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10316. (select-window sw)))
  10317. ; ;; Update the prompt to show new default date
  10318. ; (save-excursion
  10319. ; (goto-char (point-min))
  10320. ; (when (and org-ans2
  10321. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10322. ; (get-text-property (match-end 0) 'field))
  10323. ; (let ((inhibit-read-only t))
  10324. ; (replace-match (concat "[" org-ans2 "]") t t)
  10325. ; (add-text-properties (point-min) (1+ (match-end 0))
  10326. ; (text-properties-at (1+ (point-min)))))))))
  10327. (defun org-calendar-select ()
  10328. "Return to `org-read-date' with the date currently selected.
  10329. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10330. (interactive)
  10331. (when (calendar-cursor-to-date)
  10332. (let* ((date (calendar-cursor-to-date))
  10333. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10334. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10335. (if (active-minibuffer-window) (exit-minibuffer))))
  10336. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10337. "Insert a date stamp for the date given by the internal TIME.
  10338. WITH-HM means, use the stamp format that includes the time of the day.
  10339. INACTIVE means use square brackets instead of angular ones, so that the
  10340. stamp will not contribute to the agenda.
  10341. PRE and POST are optional strings to be inserted before and after the
  10342. stamp.
  10343. The command returns the inserted time stamp."
  10344. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10345. stamp)
  10346. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10347. (insert-before-markers (or pre ""))
  10348. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10349. (when (listp extra)
  10350. (setq extra (car extra))
  10351. (if (and (stringp extra)
  10352. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10353. (setq extra (format "-%02d:%02d"
  10354. (string-to-number (match-string 1 extra))
  10355. (string-to-number (match-string 2 extra))))
  10356. (setq extra nil)))
  10357. (when extra
  10358. (backward-char 1)
  10359. (insert-before-markers extra)
  10360. (forward-char 1))
  10361. (insert-before-markers (or post ""))
  10362. (setq org-last-inserted-timestamp stamp)))
  10363. (defun org-toggle-time-stamp-overlays ()
  10364. "Toggle the use of custom time stamp formats."
  10365. (interactive)
  10366. (setq org-display-custom-times (not org-display-custom-times))
  10367. (unless org-display-custom-times
  10368. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10369. (while (setq p (next-single-property-change p 'display))
  10370. (if (and (get-text-property p 'display)
  10371. (eq (get-text-property p 'face) 'org-date))
  10372. (remove-text-properties
  10373. p (setq p (next-single-property-change p 'display))
  10374. '(display t))))
  10375. (set-buffer-modified-p bmp)))
  10376. (if (featurep 'xemacs)
  10377. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10378. (org-restart-font-lock)
  10379. (setq org-table-may-need-update t)
  10380. (if org-display-custom-times
  10381. (message "Time stamps are overlayed with custom format")
  10382. (message "Time stamp overlays removed")))
  10383. (defun org-display-custom-time (beg end)
  10384. "Overlay modified time stamp format over timestamp between BEG and END."
  10385. (let* ((ts (buffer-substring beg end))
  10386. t1 w1 with-hm tf time str w2 (off 0))
  10387. (save-match-data
  10388. (setq t1 (org-parse-time-string ts t))
  10389. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10390. (setq off (- (match-end 0) (match-beginning 0)))))
  10391. (setq end (- end off))
  10392. (setq w1 (- end beg)
  10393. with-hm (and (nth 1 t1) (nth 2 t1))
  10394. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10395. time (org-fix-decoded-time t1)
  10396. str (org-add-props
  10397. (format-time-string
  10398. (substring tf 1 -1) (apply 'encode-time time))
  10399. nil 'mouse-face 'highlight)
  10400. w2 (length str))
  10401. (if (not (= w2 w1))
  10402. (add-text-properties (1+ beg) (+ 2 beg)
  10403. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10404. (if (featurep 'xemacs)
  10405. (progn
  10406. (put-text-property beg end 'invisible t)
  10407. (put-text-property beg end 'end-glyph (make-glyph str)))
  10408. (put-text-property beg end 'display str))))
  10409. (defun org-translate-time (string)
  10410. "Translate all timestamps in STRING to custom format.
  10411. But do this only if the variable `org-display-custom-times' is set."
  10412. (when org-display-custom-times
  10413. (save-match-data
  10414. (let* ((start 0)
  10415. (re org-ts-regexp-both)
  10416. t1 with-hm inactive tf time str beg end)
  10417. (while (setq start (string-match re string start))
  10418. (setq beg (match-beginning 0)
  10419. end (match-end 0)
  10420. t1 (save-match-data
  10421. (org-parse-time-string (substring string beg end) t))
  10422. with-hm (and (nth 1 t1) (nth 2 t1))
  10423. inactive (equal (substring string beg (1+ beg)) "[")
  10424. tf (funcall (if with-hm 'cdr 'car)
  10425. org-time-stamp-custom-formats)
  10426. time (org-fix-decoded-time t1)
  10427. str (format-time-string
  10428. (concat
  10429. (if inactive "[" "<") (substring tf 1 -1)
  10430. (if inactive "]" ">"))
  10431. (apply 'encode-time time))
  10432. string (replace-match str t t string)
  10433. start (+ start (length str)))))))
  10434. string)
  10435. (defun org-fix-decoded-time (time)
  10436. "Set 0 instead of nil for the first 6 elements of time.
  10437. Don't touch the rest."
  10438. (let ((n 0))
  10439. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10440. (defun org-days-to-time (timestamp-string)
  10441. "Difference between TIMESTAMP-STRING and now in days."
  10442. (- (time-to-days (org-time-string-to-time timestamp-string))
  10443. (time-to-days (current-time))))
  10444. (defun org-deadline-close (timestamp-string &optional ndays)
  10445. "Is the time in TIMESTAMP-STRING close to the current date?"
  10446. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10447. (and (< (org-days-to-time timestamp-string) ndays)
  10448. (not (org-entry-is-done-p))))
  10449. (defun org-get-wdays (ts)
  10450. "Get the deadline lead time appropriate for timestring TS."
  10451. (cond
  10452. ((<= org-deadline-warning-days 0)
  10453. ;; 0 or negative, enforce this value no matter what
  10454. (- org-deadline-warning-days))
  10455. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10456. ;; lead time is specified.
  10457. (floor (* (string-to-number (match-string 1 ts))
  10458. (cdr (assoc (match-string 2 ts)
  10459. '(("d" . 1) ("w" . 7)
  10460. ("m" . 30.4) ("y" . 365.25)))))))
  10461. ;; go for the default.
  10462. (t org-deadline-warning-days)))
  10463. (defun org-calendar-select-mouse (ev)
  10464. "Return to `org-read-date' with the date currently selected.
  10465. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10466. (interactive "e")
  10467. (mouse-set-point ev)
  10468. (when (calendar-cursor-to-date)
  10469. (let* ((date (calendar-cursor-to-date))
  10470. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10471. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10472. (if (active-minibuffer-window) (exit-minibuffer))))
  10473. (defun org-check-deadlines (ndays)
  10474. "Check if there are any deadlines due or past due.
  10475. A deadline is considered due if it happens within `org-deadline-warning-days'
  10476. days from today's date. If the deadline appears in an entry marked DONE,
  10477. it is not shown. The prefix arg NDAYS can be used to test that many
  10478. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10479. (interactive "P")
  10480. (let* ((org-warn-days
  10481. (cond
  10482. ((equal ndays '(4)) 100000)
  10483. (ndays (prefix-numeric-value ndays))
  10484. (t (abs org-deadline-warning-days))))
  10485. (case-fold-search nil)
  10486. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10487. (callback
  10488. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10489. (message "%d deadlines past-due or due within %d days"
  10490. (org-occur regexp nil callback)
  10491. org-warn-days)))
  10492. (defun org-check-before-date (date)
  10493. "Check if there are deadlines or scheduled entries before DATE."
  10494. (interactive (list (org-read-date)))
  10495. (let ((case-fold-search nil)
  10496. (regexp (concat "\\<\\(" org-deadline-string
  10497. "\\|" org-scheduled-string
  10498. "\\) *<\\([^>]+\\)>"))
  10499. (callback
  10500. (lambda () (time-less-p
  10501. (org-time-string-to-time (match-string 2))
  10502. (org-time-string-to-time date)))))
  10503. (message "%d entries before %s"
  10504. (org-occur regexp nil callback) date)))
  10505. (defun org-evaluate-time-range (&optional to-buffer)
  10506. "Evaluate a time range by computing the difference between start and end.
  10507. Normally the result is just printed in the echo area, but with prefix arg
  10508. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10509. If the time range is actually in a table, the result is inserted into the
  10510. next column.
  10511. For time difference computation, a year is assumed to be exactly 365
  10512. days in order to avoid rounding problems."
  10513. (interactive "P")
  10514. (or
  10515. (org-clock-update-time-maybe)
  10516. (save-excursion
  10517. (unless (org-at-date-range-p t)
  10518. (goto-char (point-at-bol))
  10519. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10520. (if (not (org-at-date-range-p t))
  10521. (error "Not at a time-stamp range, and none found in current line")))
  10522. (let* ((ts1 (match-string 1))
  10523. (ts2 (match-string 2))
  10524. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10525. (match-end (match-end 0))
  10526. (time1 (org-time-string-to-time ts1))
  10527. (time2 (org-time-string-to-time ts2))
  10528. (t1 (time-to-seconds time1))
  10529. (t2 (time-to-seconds time2))
  10530. (diff (abs (- t2 t1)))
  10531. (negative (< (- t2 t1) 0))
  10532. ;; (ys (floor (* 365 24 60 60)))
  10533. (ds (* 24 60 60))
  10534. (hs (* 60 60))
  10535. (fy "%dy %dd %02d:%02d")
  10536. (fy1 "%dy %dd")
  10537. (fd "%dd %02d:%02d")
  10538. (fd1 "%dd")
  10539. (fh "%02d:%02d")
  10540. y d h m align)
  10541. (if havetime
  10542. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10543. y 0
  10544. d (floor (/ diff ds)) diff (mod diff ds)
  10545. h (floor (/ diff hs)) diff (mod diff hs)
  10546. m (floor (/ diff 60)))
  10547. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10548. y 0
  10549. d (floor (+ (/ diff ds) 0.5))
  10550. h 0 m 0))
  10551. (if (not to-buffer)
  10552. (message "%s" (org-make-tdiff-string y d h m))
  10553. (if (org-at-table-p)
  10554. (progn
  10555. (goto-char match-end)
  10556. (setq align t)
  10557. (and (looking-at " *|") (goto-char (match-end 0))))
  10558. (goto-char match-end))
  10559. (if (looking-at
  10560. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10561. (replace-match ""))
  10562. (if negative (insert " -"))
  10563. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10564. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10565. (insert " " (format fh h m))))
  10566. (if align (org-table-align))
  10567. (message "Time difference inserted")))))
  10568. (defun org-make-tdiff-string (y d h m)
  10569. (let ((fmt "")
  10570. (l nil))
  10571. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10572. l (push y l)))
  10573. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10574. l (push d l)))
  10575. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10576. l (push h l)))
  10577. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10578. l (push m l)))
  10579. (apply 'format fmt (nreverse l))))
  10580. (defun org-time-string-to-time (s)
  10581. (apply 'encode-time (org-parse-time-string s)))
  10582. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10583. "Convert a time stamp to an absolute day number.
  10584. If there is a specifyer for a cyclic time stamp, get the closest date to
  10585. DAYNR.
  10586. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10587. (cond
  10588. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10589. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10590. daynr
  10591. (+ daynr 1000)))
  10592. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10593. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10594. (time-to-days (current-time))) (match-string 0 s)
  10595. prefer show-all))
  10596. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10597. (defun org-days-to-iso-week (days)
  10598. "Return the iso week number."
  10599. (require 'cal-iso)
  10600. (car (calendar-iso-from-absolute days)))
  10601. (defun org-small-year-to-year (year)
  10602. "Convert 2-digit years into 4-digit years.
  10603. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10604. The year 2000 cannot be abbreviated. Any year lager than 99
  10605. is retrned unchanged."
  10606. (if (< year 38)
  10607. (setq year (+ 2000 year))
  10608. (if (< year 100)
  10609. (setq year (+ 1900 year))))
  10610. year)
  10611. (defun org-time-from-absolute (d)
  10612. "Return the time corresponding to date D.
  10613. D may be an absolute day number, or a calendar-type list (month day year)."
  10614. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10615. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10616. (defun org-calendar-holiday ()
  10617. "List of holidays, for Diary display in Org-mode."
  10618. (require 'holidays)
  10619. (let ((hl (funcall
  10620. (if (fboundp 'calendar-check-holidays)
  10621. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10622. (if hl (mapconcat 'identity hl "; "))))
  10623. (defun org-diary-sexp-entry (sexp entry date)
  10624. "Process a SEXP diary ENTRY for DATE."
  10625. (require 'diary-lib)
  10626. (let ((result (if calendar-debug-sexp
  10627. (let ((stack-trace-on-error t))
  10628. (eval (car (read-from-string sexp))))
  10629. (condition-case nil
  10630. (eval (car (read-from-string sexp)))
  10631. (error
  10632. (beep)
  10633. (message "Bad sexp at line %d in %s: %s"
  10634. (org-current-line)
  10635. (buffer-file-name) sexp)
  10636. (sleep-for 2))))))
  10637. (cond ((stringp result) result)
  10638. ((and (consp result)
  10639. (stringp (cdr result))) (cdr result))
  10640. (result entry)
  10641. (t nil))))
  10642. (defun org-diary-to-ical-string (frombuf)
  10643. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10644. This uses the icalendar.el library."
  10645. (let* ((tmpdir (if (featurep 'xemacs)
  10646. (temp-directory)
  10647. temporary-file-directory))
  10648. (tmpfile (make-temp-name
  10649. (expand-file-name "orgics" tmpdir)))
  10650. buf rtn b e)
  10651. (save-excursion
  10652. (set-buffer frombuf)
  10653. (icalendar-export-region (point-min) (point-max) tmpfile)
  10654. (setq buf (find-buffer-visiting tmpfile))
  10655. (set-buffer buf)
  10656. (goto-char (point-min))
  10657. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10658. (setq b (match-beginning 0)))
  10659. (goto-char (point-max))
  10660. (if (re-search-backward "^END:VEVENT" nil t)
  10661. (setq e (match-end 0)))
  10662. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10663. (kill-buffer buf)
  10664. (delete-file tmpfile)
  10665. rtn))
  10666. (defun org-closest-date (start current change prefer show-all)
  10667. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10668. When PREFER is `past' return a date that is either CURRENT or past.
  10669. When PREFER is `future', return a date that is either CURRENT or future.
  10670. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10671. ;; Make the proper lists from the dates
  10672. (catch 'exit
  10673. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10674. dn dw sday cday n1 n2
  10675. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10676. (setq start (org-date-to-gregorian start)
  10677. current (org-date-to-gregorian
  10678. (if show-all
  10679. current
  10680. (time-to-days (current-time))))
  10681. sday (calendar-absolute-from-gregorian start)
  10682. cday (calendar-absolute-from-gregorian current))
  10683. (if (<= cday sday) (throw 'exit sday))
  10684. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10685. (setq dn (string-to-number (match-string 1 change))
  10686. dw (cdr (assoc (match-string 2 change) a1)))
  10687. (error "Invalid change specifyer: %s" change))
  10688. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10689. (cond
  10690. ((eq dw 'day)
  10691. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10692. n2 (+ n1 dn)))
  10693. ((eq dw 'year)
  10694. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10695. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10696. (setq date1 (list m d y1)
  10697. n1 (calendar-absolute-from-gregorian date1)
  10698. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10699. n2 (calendar-absolute-from-gregorian date2)))
  10700. ((eq dw 'month)
  10701. ;; approx number of month between the two dates
  10702. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10703. ;; How often does dn fit in there?
  10704. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10705. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10706. m (+ m nm)
  10707. ny (floor (/ m 12))
  10708. y (+ y ny)
  10709. m (- m (* ny 12)))
  10710. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10711. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10712. (setq m2 (+ m dn) y2 y)
  10713. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10714. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10715. (while (<= n2 cday)
  10716. (setq n1 n2 m m2 y y2)
  10717. (setq m2 (+ m dn) y2 y)
  10718. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10719. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10720. (if show-all
  10721. (cond
  10722. ((eq prefer 'past) n1)
  10723. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10724. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10725. (cond
  10726. ((eq prefer 'past) n1)
  10727. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10728. (t (if (= cday n1) n1 n2)))))))
  10729. (defun org-date-to-gregorian (date)
  10730. "Turn any specification of DATE into a gregorian date for the calendar."
  10731. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10732. ((and (listp date) (= (length date) 3)) date)
  10733. ((stringp date)
  10734. (setq date (org-parse-time-string date))
  10735. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10736. ((listp date)
  10737. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10738. (defun org-parse-time-string (s &optional nodefault)
  10739. "Parse the standard Org-mode time string.
  10740. This should be a lot faster than the normal `parse-time-string'.
  10741. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10742. hour and minute fields will be nil if not given."
  10743. (if (string-match org-ts-regexp0 s)
  10744. (list 0
  10745. (if (or (match-beginning 8) (not nodefault))
  10746. (string-to-number (or (match-string 8 s) "0")))
  10747. (if (or (match-beginning 7) (not nodefault))
  10748. (string-to-number (or (match-string 7 s) "0")))
  10749. (string-to-number (match-string 4 s))
  10750. (string-to-number (match-string 3 s))
  10751. (string-to-number (match-string 2 s))
  10752. nil nil nil)
  10753. (make-list 9 0)))
  10754. (defun org-timestamp-up (&optional arg)
  10755. "Increase the date item at the cursor by one.
  10756. If the cursor is on the year, change the year. If it is on the month or
  10757. the day, change that.
  10758. With prefix ARG, change by that many units."
  10759. (interactive "p")
  10760. (org-timestamp-change (prefix-numeric-value arg)))
  10761. (defun org-timestamp-down (&optional arg)
  10762. "Decrease the date item at the cursor by one.
  10763. If the cursor is on the year, change the year. If it is on the month or
  10764. the day, change that.
  10765. With prefix ARG, change by that many units."
  10766. (interactive "p")
  10767. (org-timestamp-change (- (prefix-numeric-value arg))))
  10768. (defun org-timestamp-up-day (&optional arg)
  10769. "Increase the date in the time stamp by one day.
  10770. With prefix ARG, change that many days."
  10771. (interactive "p")
  10772. (if (and (not (org-at-timestamp-p t))
  10773. (org-on-heading-p))
  10774. (org-todo 'up)
  10775. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10776. (defun org-timestamp-down-day (&optional arg)
  10777. "Decrease the date in the time stamp by one day.
  10778. With prefix ARG, change that many days."
  10779. (interactive "p")
  10780. (if (and (not (org-at-timestamp-p t))
  10781. (org-on-heading-p))
  10782. (org-todo 'down)
  10783. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10784. (defun org-at-timestamp-p (&optional inactive-ok)
  10785. "Determine if the cursor is in or at a timestamp."
  10786. (interactive)
  10787. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10788. (pos (point))
  10789. (ans (or (looking-at tsr)
  10790. (save-excursion
  10791. (skip-chars-backward "^[<\n\r\t")
  10792. (if (> (point) (point-min)) (backward-char 1))
  10793. (and (looking-at tsr)
  10794. (> (- (match-end 0) pos) -1))))))
  10795. (and ans
  10796. (boundp 'org-ts-what)
  10797. (setq org-ts-what
  10798. (cond
  10799. ((= pos (match-beginning 0)) 'bracket)
  10800. ((= pos (1- (match-end 0))) 'bracket)
  10801. ((org-pos-in-match-range pos 2) 'year)
  10802. ((org-pos-in-match-range pos 3) 'month)
  10803. ((org-pos-in-match-range pos 7) 'hour)
  10804. ((org-pos-in-match-range pos 8) 'minute)
  10805. ((or (org-pos-in-match-range pos 4)
  10806. (org-pos-in-match-range pos 5)) 'day)
  10807. ((and (> pos (or (match-end 8) (match-end 5)))
  10808. (< pos (match-end 0)))
  10809. (- pos (or (match-end 8) (match-end 5))))
  10810. (t 'day))))
  10811. ans))
  10812. (defun org-toggle-timestamp-type ()
  10813. "Toggle the type (<active> or [inactive]) of a time stamp."
  10814. (interactive)
  10815. (when (org-at-timestamp-p t)
  10816. (save-excursion
  10817. (goto-char (match-beginning 0))
  10818. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10819. (goto-char (1- (match-end 0)))
  10820. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10821. (message "Timestamp is now %sactive"
  10822. (if (equal (char-before) ?>) "in" ""))))
  10823. (defun org-timestamp-change (n &optional what)
  10824. "Change the date in the time stamp at point.
  10825. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10826. `year', `minute', `second'. If WHAT is not given, the cursor position
  10827. in the timestamp determines what will be changed."
  10828. (let ((pos (point))
  10829. with-hm inactive
  10830. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10831. org-ts-what
  10832. extra rem
  10833. ts time time0)
  10834. (if (not (org-at-timestamp-p t))
  10835. (error "Not at a timestamp"))
  10836. (if (and (not what) (eq org-ts-what 'bracket))
  10837. (org-toggle-timestamp-type)
  10838. (if (and (not what) (not (eq org-ts-what 'day))
  10839. org-display-custom-times
  10840. (get-text-property (point) 'display)
  10841. (not (get-text-property (1- (point)) 'display)))
  10842. (setq org-ts-what 'day))
  10843. (setq org-ts-what (or what org-ts-what)
  10844. inactive (= (char-after (match-beginning 0)) ?\[)
  10845. ts (match-string 0))
  10846. (replace-match "")
  10847. (if (string-match
  10848. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10849. ts)
  10850. (setq extra (match-string 1 ts)))
  10851. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10852. (setq with-hm t))
  10853. (setq time0 (org-parse-time-string ts))
  10854. (when (and (eq org-ts-what 'minute)
  10855. (eq current-prefix-arg nil))
  10856. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10857. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10858. (setcar (cdr time0) (+ (nth 1 time0)
  10859. (if (> n 0) (- rem) (- dm rem))))))
  10860. (setq time
  10861. (encode-time (or (car time0) 0)
  10862. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10863. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10864. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10865. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10866. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10867. (nthcdr 6 time0)))
  10868. (when (integerp org-ts-what)
  10869. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10870. (if (eq what 'calendar)
  10871. (let ((cal-date (org-get-date-from-calendar)))
  10872. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10873. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10874. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10875. (setcar time0 (or (car time0) 0))
  10876. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10877. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10878. (setq time (apply 'encode-time time0))))
  10879. (setq org-last-changed-timestamp
  10880. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10881. (org-clock-update-time-maybe)
  10882. (goto-char pos)
  10883. ;; Try to recenter the calendar window, if any
  10884. (if (and org-calendar-follow-timestamp-change
  10885. (get-buffer-window "*Calendar*" t)
  10886. (memq org-ts-what '(day month year)))
  10887. (org-recenter-calendar (time-to-days time))))))
  10888. (defun org-modify-ts-extra (s pos n dm)
  10889. "Change the different parts of the lead-time and repeat fields in timestamp."
  10890. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10891. ng h m new rem)
  10892. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10893. (cond
  10894. ((or (org-pos-in-match-range pos 2)
  10895. (org-pos-in-match-range pos 3))
  10896. (setq m (string-to-number (match-string 3 s))
  10897. h (string-to-number (match-string 2 s)))
  10898. (if (org-pos-in-match-range pos 2)
  10899. (setq h (+ h n))
  10900. (setq n (* dm (org-no-warnings (signum n))))
  10901. (when (not (= 0 (setq rem (% m dm))))
  10902. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10903. (setq m (+ m n)))
  10904. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10905. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10906. (setq h (min 24 (max 0 h)))
  10907. (setq ng 1 new (format "-%02d:%02d" h m)))
  10908. ((org-pos-in-match-range pos 6)
  10909. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10910. ((org-pos-in-match-range pos 5)
  10911. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10912. ((org-pos-in-match-range pos 9)
  10913. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10914. ((org-pos-in-match-range pos 8)
  10915. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10916. (when ng
  10917. (setq s (concat
  10918. (substring s 0 (match-beginning ng))
  10919. new
  10920. (substring s (match-end ng))))))
  10921. s))
  10922. (defun org-recenter-calendar (date)
  10923. "If the calendar is visible, recenter it to DATE."
  10924. (let* ((win (selected-window))
  10925. (cwin (get-buffer-window "*Calendar*" t))
  10926. (calendar-move-hook nil))
  10927. (when cwin
  10928. (select-window cwin)
  10929. (calendar-goto-date (if (listp date) date
  10930. (calendar-gregorian-from-absolute date)))
  10931. (select-window win))))
  10932. (defun org-goto-calendar (&optional arg)
  10933. "Go to the Emacs calendar at the current date.
  10934. If there is a time stamp in the current line, go to that date.
  10935. A prefix ARG can be used to force the current date."
  10936. (interactive "P")
  10937. (let ((tsr org-ts-regexp) diff
  10938. (calendar-move-hook nil)
  10939. (calendar-view-holidays-initially-flag nil)
  10940. (view-calendar-holidays-initially nil)
  10941. (calendar-view-diary-initially-flag nil)
  10942. (view-diary-entries-initially nil))
  10943. (if (or (org-at-timestamp-p)
  10944. (save-excursion
  10945. (beginning-of-line 1)
  10946. (looking-at (concat ".*" tsr))))
  10947. (let ((d1 (time-to-days (current-time)))
  10948. (d2 (time-to-days
  10949. (org-time-string-to-time (match-string 1)))))
  10950. (setq diff (- d2 d1))))
  10951. (calendar)
  10952. (calendar-goto-today)
  10953. (if (and diff (not arg)) (calendar-forward-day diff))))
  10954. (defun org-get-date-from-calendar ()
  10955. "Return a list (month day year) of date at point in calendar."
  10956. (with-current-buffer "*Calendar*"
  10957. (save-match-data
  10958. (calendar-cursor-to-date))))
  10959. (defun org-date-from-calendar ()
  10960. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10961. If there is already a time stamp at the cursor position, update it."
  10962. (interactive)
  10963. (if (org-at-timestamp-p t)
  10964. (org-timestamp-change 0 'calendar)
  10965. (let ((cal-date (org-get-date-from-calendar)))
  10966. (org-insert-time-stamp
  10967. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10968. (defun org-minutes-to-hh:mm-string (m)
  10969. "Compute H:MM from a number of minutes."
  10970. (let ((h (/ m 60)))
  10971. (setq m (- m (* 60 h)))
  10972. (format org-time-clocksum-format h m)))
  10973. (defun org-hh:mm-string-to-minutes (s)
  10974. "Convert a string H:MM to a number of minutes."
  10975. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10976. (+ (* (string-to-number (match-string 1 s)) 60)
  10977. (string-to-number (match-string 2 s)))
  10978. 0))
  10979. ;;;; Agenda files
  10980. ;;;###autoload
  10981. (defun org-iswitchb (&optional arg)
  10982. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10983. With a prefix argument, restrict available to files.
  10984. With two prefix arguments, restrict available buffers to agenda files.
  10985. Due to some yet unresolved reason, the global function
  10986. `iswitchb-mode' needs to be active for this function to work."
  10987. (interactive "P")
  10988. (require 'iswitchb)
  10989. (let ((enabled iswitchb-mode) blist)
  10990. (or enabled (iswitchb-mode 1))
  10991. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10992. ((equal arg '(16)) (org-buffer-list 'agenda))
  10993. (t (org-buffer-list))))
  10994. (unwind-protect
  10995. (let ((iswitchb-make-buflist-hook
  10996. (lambda ()
  10997. (setq iswitchb-temp-buflist
  10998. (mapcar 'buffer-name blist)))))
  10999. (switch-to-buffer
  11000. (iswitchb-read-buffer
  11001. "Switch-to: " nil t))
  11002. (or enabled (iswitchb-mode -1))))))
  11003. (defun org-buffer-list (&optional predicate exclude-tmp)
  11004. "Return a list of Org buffers.
  11005. PREDICATE can be `export', `files' or `agenda'.
  11006. export restrict the list to Export buffers.
  11007. files restrict the list to buffers visiting Org files.
  11008. agenda restrict the list to buffers visiting agenda files.
  11009. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11010. (let* ((bfn nil)
  11011. (agenda-files (and (eq predicate 'agenda)
  11012. (mapcar 'file-truename (org-agenda-files t))))
  11013. (filter
  11014. (cond
  11015. ((eq predicate 'files)
  11016. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11017. ((eq predicate 'export)
  11018. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11019. ((eq predicate 'agenda)
  11020. (lambda (b)
  11021. (with-current-buffer b
  11022. (and (eq major-mode 'org-mode)
  11023. (setq bfn (buffer-file-name b))
  11024. (member (file-truename bfn) agenda-files)))))
  11025. (t (lambda (b) (with-current-buffer b
  11026. (or (eq major-mode 'org-mode)
  11027. (string-match "\*Org .*Export"
  11028. (buffer-name b)))))))))
  11029. (delq nil
  11030. (mapcar
  11031. (lambda(b)
  11032. (if (and (funcall filter b)
  11033. (or (not exclude-tmp)
  11034. (not (string-match "tmp" (buffer-name b)))))
  11035. b
  11036. nil))
  11037. (buffer-list)))))
  11038. (defun org-agenda-files (&optional unrestricted archives)
  11039. "Get the list of agenda files.
  11040. Optional UNRESTRICTED means return the full list even if a restriction
  11041. is currently in place.
  11042. When ARCHIVES is t, include all archive files hat are really being
  11043. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11044. `org-agenda-archives-mode' is t."
  11045. (let ((files
  11046. (cond
  11047. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11048. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11049. ((listp org-agenda-files) org-agenda-files)
  11050. (t (error "Invalid value of `org-agenda-files'")))))
  11051. (setq files (apply 'append
  11052. (mapcar (lambda (f)
  11053. (if (file-directory-p f)
  11054. (directory-files
  11055. f t org-agenda-file-regexp)
  11056. (list f)))
  11057. files)))
  11058. (when org-agenda-skip-unavailable-files
  11059. (setq files (delq nil
  11060. (mapcar (function
  11061. (lambda (file)
  11062. (and (file-readable-p file) file)))
  11063. files))))
  11064. (when (or (eq archives t)
  11065. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11066. (setq files (org-add-archive-files files)))
  11067. files))
  11068. (defun org-edit-agenda-file-list ()
  11069. "Edit the list of agenda files.
  11070. Depending on setup, this either uses customize to edit the variable
  11071. `org-agenda-files', or it visits the file that is holding the list. In the
  11072. latter case, the buffer is set up in a way that saving it automatically kills
  11073. the buffer and restores the previous window configuration."
  11074. (interactive)
  11075. (if (stringp org-agenda-files)
  11076. (let ((cw (current-window-configuration)))
  11077. (find-file org-agenda-files)
  11078. (org-set-local 'org-window-configuration cw)
  11079. (org-add-hook 'after-save-hook
  11080. (lambda ()
  11081. (set-window-configuration
  11082. (prog1 org-window-configuration
  11083. (kill-buffer (current-buffer))))
  11084. (org-install-agenda-files-menu)
  11085. (message "New agenda file list installed"))
  11086. nil 'local)
  11087. (message "%s" (substitute-command-keys
  11088. "Edit list and finish with \\[save-buffer]")))
  11089. (customize-variable 'org-agenda-files)))
  11090. (defun org-store-new-agenda-file-list (list)
  11091. "Set new value for the agenda file list and save it correcly."
  11092. (if (stringp org-agenda-files)
  11093. (let ((f org-agenda-files) b)
  11094. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11095. (with-temp-file f
  11096. (insert (mapconcat 'identity list "\n") "\n")))
  11097. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11098. (setq org-agenda-files list)
  11099. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11100. (defun org-read-agenda-file-list ()
  11101. "Read the list of agenda files from a file."
  11102. (when (file-directory-p org-agenda-files)
  11103. (error "`org-agenda-files' cannot be a single directory"))
  11104. (when (stringp org-agenda-files)
  11105. (with-temp-buffer
  11106. (insert-file-contents org-agenda-files)
  11107. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11108. ;;;###autoload
  11109. (defun org-cycle-agenda-files ()
  11110. "Cycle through the files in `org-agenda-files'.
  11111. If the current buffer visits an agenda file, find the next one in the list.
  11112. If the current buffer does not, find the first agenda file."
  11113. (interactive)
  11114. (let* ((fs (org-agenda-files t))
  11115. (files (append fs (list (car fs))))
  11116. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11117. file)
  11118. (unless files (error "No agenda files"))
  11119. (catch 'exit
  11120. (while (setq file (pop files))
  11121. (if (equal (file-truename file) tcf)
  11122. (when (car files)
  11123. (find-file (car files))
  11124. (throw 'exit t))))
  11125. (find-file (car fs)))
  11126. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11127. (defun org-agenda-file-to-front (&optional to-end)
  11128. "Move/add the current file to the top of the agenda file list.
  11129. If the file is not present in the list, it is added to the front. If it is
  11130. present, it is moved there. With optional argument TO-END, add/move to the
  11131. end of the list."
  11132. (interactive "P")
  11133. (let ((org-agenda-skip-unavailable-files nil)
  11134. (file-alist (mapcar (lambda (x)
  11135. (cons (file-truename x) x))
  11136. (org-agenda-files t)))
  11137. (ctf (file-truename buffer-file-name))
  11138. x had)
  11139. (setq x (assoc ctf file-alist) had x)
  11140. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11141. (if to-end
  11142. (setq file-alist (append (delq x file-alist) (list x)))
  11143. (setq file-alist (cons x (delq x file-alist))))
  11144. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11145. (org-install-agenda-files-menu)
  11146. (message "File %s to %s of agenda file list"
  11147. (if had "moved" "added") (if to-end "end" "front"))))
  11148. (defun org-remove-file (&optional file)
  11149. "Remove current file from the list of files in variable `org-agenda-files'.
  11150. These are the files which are being checked for agenda entries.
  11151. Optional argument FILE means, use this file instead of the current."
  11152. (interactive)
  11153. (let* ((org-agenda-skip-unavailable-files nil)
  11154. (file (or file buffer-file-name))
  11155. (true-file (file-truename file))
  11156. (afile (abbreviate-file-name file))
  11157. (files (delq nil (mapcar
  11158. (lambda (x)
  11159. (if (equal true-file
  11160. (file-truename x))
  11161. nil x))
  11162. (org-agenda-files t)))))
  11163. (if (not (= (length files) (length (org-agenda-files t))))
  11164. (progn
  11165. (org-store-new-agenda-file-list files)
  11166. (org-install-agenda-files-menu)
  11167. (message "Removed file: %s" afile))
  11168. (message "File was not in list: %s (not removed)" afile))))
  11169. (defun org-file-menu-entry (file)
  11170. (vector file (list 'find-file file) t))
  11171. (defun org-check-agenda-file (file)
  11172. "Make sure FILE exists. If not, ask user what to do."
  11173. (when (not (file-exists-p file))
  11174. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11175. (abbreviate-file-name file))
  11176. (let ((r (downcase (read-char-exclusive))))
  11177. (cond
  11178. ((equal r ?r)
  11179. (org-remove-file file)
  11180. (throw 'nextfile t))
  11181. (t (error "Abort"))))))
  11182. (defun org-get-agenda-file-buffer (file)
  11183. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11184. it to the list of buffers which might be released later."
  11185. (let ((buf (org-find-base-buffer-visiting file)))
  11186. (if buf
  11187. buf ; just return it
  11188. ;; Make a new buffer and remember it
  11189. (setq buf (find-file-noselect file))
  11190. (if buf (push buf org-agenda-new-buffers))
  11191. buf)))
  11192. (defun org-release-buffers (blist)
  11193. "Release all buffers in list, asking the user for confirmation when needed.
  11194. When a buffer is unmodified, it is just killed. When modified, it is saved
  11195. \(if the user agrees) and then killed."
  11196. (let (buf file)
  11197. (while (setq buf (pop blist))
  11198. (setq file (buffer-file-name buf))
  11199. (when (and (buffer-modified-p buf)
  11200. file
  11201. (y-or-n-p (format "Save file %s? " file)))
  11202. (with-current-buffer buf (save-buffer)))
  11203. (kill-buffer buf))))
  11204. (defun org-prepare-agenda-buffers (files)
  11205. "Create buffers for all agenda files, protect archived trees and comments."
  11206. (interactive)
  11207. (let ((pa '(:org-archived t))
  11208. (pc '(:org-comment t))
  11209. (pall '(:org-archived t :org-comment t))
  11210. (inhibit-read-only t)
  11211. (rea (concat ":" org-archive-tag ":"))
  11212. bmp file re)
  11213. (save-excursion
  11214. (save-restriction
  11215. (while (setq file (pop files))
  11216. (if (bufferp file)
  11217. (set-buffer file)
  11218. (org-check-agenda-file file)
  11219. (set-buffer (org-get-agenda-file-buffer file)))
  11220. (widen)
  11221. (setq bmp (buffer-modified-p))
  11222. (org-refresh-category-properties)
  11223. (setq org-todo-keywords-for-agenda
  11224. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11225. (setq org-done-keywords-for-agenda
  11226. (append org-done-keywords-for-agenda org-done-keywords))
  11227. (setq org-todo-keyword-alist-for-agenda
  11228. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11229. (setq org-tag-alist-for-agenda
  11230. (append org-tag-alist-for-agenda org-tag-alist))
  11231. (save-excursion
  11232. (remove-text-properties (point-min) (point-max) pall)
  11233. (when org-agenda-skip-archived-trees
  11234. (goto-char (point-min))
  11235. (while (re-search-forward rea nil t)
  11236. (if (org-on-heading-p t)
  11237. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11238. (goto-char (point-min))
  11239. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11240. (while (re-search-forward re nil t)
  11241. (add-text-properties
  11242. (match-beginning 0) (org-end-of-subtree t) pc)))
  11243. (set-buffer-modified-p bmp))))
  11244. (setq org-todo-keyword-alist-for-agenda
  11245. (org-uniquify org-todo-keyword-alist-for-agenda)
  11246. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11247. ;;;; Embedded LaTeX
  11248. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11249. "Keymap for the minor `org-cdlatex-mode'.")
  11250. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11251. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11252. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11253. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11254. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11255. (defvar org-cdlatex-texmathp-advice-is-done nil
  11256. "Flag remembering if we have applied the advice to texmathp already.")
  11257. (define-minor-mode org-cdlatex-mode
  11258. "Toggle the minor `org-cdlatex-mode'.
  11259. This mode supports entering LaTeX environment and math in LaTeX fragments
  11260. in Org-mode.
  11261. \\{org-cdlatex-mode-map}"
  11262. nil " OCDL" nil
  11263. (when org-cdlatex-mode (require 'cdlatex))
  11264. (unless org-cdlatex-texmathp-advice-is-done
  11265. (setq org-cdlatex-texmathp-advice-is-done t)
  11266. (defadvice texmathp (around org-math-always-on activate)
  11267. "Always return t in org-mode buffers.
  11268. This is because we want to insert math symbols without dollars even outside
  11269. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11270. en embedded LaTeX fragement, let texmathp do its job.
  11271. \\[org-cdlatex-mode-map]"
  11272. (interactive)
  11273. (let (p)
  11274. (cond
  11275. ((not (org-mode-p)) ad-do-it)
  11276. ((eq this-command 'cdlatex-math-symbol)
  11277. (setq ad-return-value t
  11278. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11279. (t
  11280. (let ((p (org-inside-LaTeX-fragment-p)))
  11281. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11282. (setq ad-return-value t
  11283. texmathp-why '("Org-mode embedded math" . 0))
  11284. (if p ad-do-it)))))))))
  11285. (defun turn-on-org-cdlatex ()
  11286. "Unconditionally turn on `org-cdlatex-mode'."
  11287. (org-cdlatex-mode 1))
  11288. (defun org-inside-LaTeX-fragment-p ()
  11289. "Test if point is inside a LaTeX fragment.
  11290. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11291. sequence appearing also before point.
  11292. Even though the matchers for math are configurable, this function assumes
  11293. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11294. delimiters are skipped when they have been removed by customization.
  11295. The return value is nil, or a cons cell with the delimiter and
  11296. and the position of this delimiter.
  11297. This function does a reasonably good job, but can locally be fooled by
  11298. for example currency specifications. For example it will assume being in
  11299. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11300. fragments that are properly closed, but during editing, we have to live
  11301. with the uncertainty caused by missing closing delimiters. This function
  11302. looks only before point, not after."
  11303. (catch 'exit
  11304. (let ((pos (point))
  11305. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11306. (lim (progn
  11307. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11308. (point)))
  11309. dd-on str (start 0) m re)
  11310. (goto-char pos)
  11311. (when dodollar
  11312. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11313. re (nth 1 (assoc "$" org-latex-regexps)))
  11314. (while (string-match re str start)
  11315. (cond
  11316. ((= (match-end 0) (length str))
  11317. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11318. ((= (match-end 0) (- (length str) 5))
  11319. (throw 'exit nil))
  11320. (t (setq start (match-end 0))))))
  11321. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11322. (goto-char pos)
  11323. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11324. (and (match-beginning 2) (throw 'exit nil))
  11325. ;; count $$
  11326. (while (re-search-backward "\\$\\$" lim t)
  11327. (setq dd-on (not dd-on)))
  11328. (goto-char pos)
  11329. (if dd-on (cons "$$" m))))))
  11330. (defun org-try-cdlatex-tab ()
  11331. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11332. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11333. - inside a LaTeX fragment, or
  11334. - after the first word in a line, where an abbreviation expansion could
  11335. insert a LaTeX environment."
  11336. (when org-cdlatex-mode
  11337. (cond
  11338. ((save-excursion
  11339. (skip-chars-backward "a-zA-Z0-9*")
  11340. (skip-chars-backward " \t")
  11341. (bolp))
  11342. (cdlatex-tab) t)
  11343. ((org-inside-LaTeX-fragment-p)
  11344. (cdlatex-tab) t)
  11345. (t nil))))
  11346. (defun org-cdlatex-underscore-caret (&optional arg)
  11347. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11348. Revert to the normal definition outside of these fragments."
  11349. (interactive "P")
  11350. (if (org-inside-LaTeX-fragment-p)
  11351. (call-interactively 'cdlatex-sub-superscript)
  11352. (let (org-cdlatex-mode)
  11353. (call-interactively (key-binding (vector last-input-event))))))
  11354. (defun org-cdlatex-math-modify (&optional arg)
  11355. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11356. Revert to the normal definition outside of these fragments."
  11357. (interactive "P")
  11358. (if (org-inside-LaTeX-fragment-p)
  11359. (call-interactively 'cdlatex-math-modify)
  11360. (let (org-cdlatex-mode)
  11361. (call-interactively (key-binding (vector last-input-event))))))
  11362. (defvar org-latex-fragment-image-overlays nil
  11363. "List of overlays carrying the images of latex fragments.")
  11364. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11365. (defun org-remove-latex-fragment-image-overlays ()
  11366. "Remove all overlays with LaTeX fragment images in current buffer."
  11367. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11368. (setq org-latex-fragment-image-overlays nil))
  11369. (defun org-preview-latex-fragment (&optional subtree)
  11370. "Preview the LaTeX fragment at point, or all locally or globally.
  11371. If the cursor is in a LaTeX fragment, create the image and overlay
  11372. it over the source code. If there is no fragment at point, display
  11373. all fragments in the current text, from one headline to the next. With
  11374. prefix SUBTREE, display all fragments in the current subtree. With a
  11375. double prefix `C-u C-u', or when the cursor is before the first headline,
  11376. display all fragments in the buffer.
  11377. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11378. (interactive "P")
  11379. (org-remove-latex-fragment-image-overlays)
  11380. (save-excursion
  11381. (save-restriction
  11382. (let (beg end at msg)
  11383. (cond
  11384. ((or (equal subtree '(16))
  11385. (not (save-excursion
  11386. (re-search-backward (concat "^" outline-regexp) nil t))))
  11387. (setq beg (point-min) end (point-max)
  11388. msg "Creating images for buffer...%s"))
  11389. ((equal subtree '(4))
  11390. (org-back-to-heading)
  11391. (setq beg (point) end (org-end-of-subtree t)
  11392. msg "Creating images for subtree...%s"))
  11393. (t
  11394. (if (setq at (org-inside-LaTeX-fragment-p))
  11395. (goto-char (max (point-min) (- (cdr at) 2)))
  11396. (org-back-to-heading))
  11397. (setq beg (point) end (progn (outline-next-heading) (point))
  11398. msg (if at "Creating image...%s"
  11399. "Creating images for entry...%s"))))
  11400. (message msg "")
  11401. (narrow-to-region beg end)
  11402. (goto-char beg)
  11403. (org-format-latex
  11404. (concat "ltxpng/" (file-name-sans-extension
  11405. (file-name-nondirectory
  11406. buffer-file-name)))
  11407. default-directory 'overlays msg at 'forbuffer)
  11408. (message msg "done. Use `C-c C-c' to remove images.")))))
  11409. (defvar org-latex-regexps
  11410. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11411. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11412. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11413. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11414. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11415. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11416. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11417. "Regular expressions for matching embedded LaTeX.")
  11418. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11419. "Replace LaTeX fragments with links to an image, and produce images."
  11420. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11421. (let* ((prefixnodir (file-name-nondirectory prefix))
  11422. (absprefix (expand-file-name prefix dir))
  11423. (todir (file-name-directory absprefix))
  11424. (opt org-format-latex-options)
  11425. (matchers (plist-get opt :matchers))
  11426. (re-list org-latex-regexps)
  11427. (cnt 0) txt link beg end re e checkdir
  11428. m n block linkfile movefile ov)
  11429. ;; Check if there are old images files with this prefix, and remove them
  11430. (when (file-directory-p todir)
  11431. (mapc 'delete-file
  11432. (directory-files
  11433. todir 'full
  11434. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11435. ;; Check the different regular expressions
  11436. (while (setq e (pop re-list))
  11437. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11438. block (if (nth 3 e) "\n\n" ""))
  11439. (when (member m matchers)
  11440. (goto-char (point-min))
  11441. (while (re-search-forward re nil t)
  11442. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11443. (setq txt (match-string n)
  11444. beg (match-beginning n) end (match-end n)
  11445. cnt (1+ cnt)
  11446. linkfile (format "%s_%04d.png" prefix cnt)
  11447. movefile (format "%s_%04d.png" absprefix cnt)
  11448. link (concat block "[[file:" linkfile "]]" block))
  11449. (if msg (message msg cnt))
  11450. (goto-char beg)
  11451. (unless checkdir ; make sure the directory exists
  11452. (setq checkdir t)
  11453. (or (file-directory-p todir) (make-directory todir)))
  11454. (org-create-formula-image
  11455. txt movefile opt forbuffer)
  11456. (if overlays
  11457. (progn
  11458. (setq ov (org-make-overlay beg end))
  11459. (if (featurep 'xemacs)
  11460. (progn
  11461. (org-overlay-put ov 'invisible t)
  11462. (org-overlay-put
  11463. ov 'end-glyph
  11464. (make-glyph (vector 'png :file movefile))))
  11465. (org-overlay-put
  11466. ov 'display
  11467. (list 'image :type 'png :file movefile :ascent 'center)))
  11468. (push ov org-latex-fragment-image-overlays)
  11469. (goto-char end))
  11470. (delete-region beg end)
  11471. (insert link))))))))
  11472. ;; This function borrows from Ganesh Swami's latex2png.el
  11473. (defun org-create-formula-image (string tofile options buffer)
  11474. (let* ((tmpdir (if (featurep 'xemacs)
  11475. (temp-directory)
  11476. temporary-file-directory))
  11477. (texfilebase (make-temp-name
  11478. (expand-file-name "orgtex" tmpdir)))
  11479. (texfile (concat texfilebase ".tex"))
  11480. (dvifile (concat texfilebase ".dvi"))
  11481. (pngfile (concat texfilebase ".png"))
  11482. (fnh (if (featurep 'xemacs)
  11483. (font-height (get-face-font 'default))
  11484. (face-attribute 'default :height nil)))
  11485. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11486. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11487. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11488. "Black"))
  11489. (bg (or (plist-get options (if buffer :background :html-background))
  11490. "Transparent")))
  11491. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11492. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11493. (with-temp-file texfile
  11494. (insert org-format-latex-header
  11495. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11496. (let ((dir default-directory))
  11497. (condition-case nil
  11498. (progn
  11499. (cd tmpdir)
  11500. (call-process "latex" nil nil nil texfile))
  11501. (error nil))
  11502. (cd dir))
  11503. (if (not (file-exists-p dvifile))
  11504. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11505. (condition-case nil
  11506. (call-process "dvipng" nil nil nil
  11507. "-E" "-fg" fg "-bg" bg
  11508. "-D" dpi
  11509. ;;"-x" scale "-y" scale
  11510. "-T" "tight"
  11511. "-o" pngfile
  11512. dvifile)
  11513. (error nil))
  11514. (if (not (file-exists-p pngfile))
  11515. (progn (message "Failed to create png file from %s" texfile) nil)
  11516. ;; Use the requested file name and clean up
  11517. (copy-file pngfile tofile 'replace)
  11518. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11519. (delete-file (concat texfilebase e)))
  11520. pngfile))))
  11521. (defun org-dvipng-color (attr)
  11522. "Return an rgb color specification for dvipng."
  11523. (apply 'format "rgb %s %s %s"
  11524. (mapcar 'org-normalize-color
  11525. (color-values (face-attribute 'default attr nil)))))
  11526. (defun org-normalize-color (value)
  11527. "Return string to be used as color value for an RGB component."
  11528. (format "%g" (/ value 65535.0)))
  11529. ;;;; Key bindings
  11530. ;; Make `C-c C-x' a prefix key
  11531. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11532. ;; TAB key with modifiers
  11533. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11534. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11535. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11536. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11537. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11538. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11539. ;; The following line is necessary under Suse GNU/Linux
  11540. (unless (featurep 'xemacs)
  11541. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11542. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11543. (define-key org-mode-map [backtab] 'org-shifttab)
  11544. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11545. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11546. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11547. ;; Cursor keys with modifiers
  11548. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11549. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11550. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11551. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11552. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11553. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11554. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11555. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11556. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11557. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11558. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11559. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11560. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11561. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11562. ;;; Extra keys for tty access.
  11563. ;; We only set them when really needed because otherwise the
  11564. ;; menus don't show the simple keys
  11565. (when (or org-use-extra-keys
  11566. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11567. (not window-system))
  11568. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11569. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11570. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11571. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11572. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11573. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11574. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11575. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11576. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11577. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11578. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11579. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11580. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11581. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11582. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11583. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11584. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11585. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11586. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11587. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11588. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11589. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11590. ;; All the other keys
  11591. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11592. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11593. (if (boundp 'narrow-map)
  11594. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11595. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11596. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11597. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11598. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11599. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11600. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11601. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11602. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11603. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11604. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11605. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11606. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11607. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11608. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11609. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11610. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11611. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11612. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11613. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  11614. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11615. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11616. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11617. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11618. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11619. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11620. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11621. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11622. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11623. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11624. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11625. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11626. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11627. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11628. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11629. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11630. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11631. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11632. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11633. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11634. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11635. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11636. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11637. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11638. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11639. (org-defkey org-mode-map "\C-m" 'org-return)
  11640. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11641. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11642. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11643. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11644. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11645. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11646. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11647. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11648. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11649. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11650. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11651. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11652. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11653. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11654. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11655. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11656. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11657. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11658. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11659. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11660. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11661. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11662. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11663. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11664. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11665. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11666. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11667. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11668. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11669. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11670. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11671. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11672. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11673. (when (featurep 'xemacs)
  11674. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11675. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11676. (defun org-self-insert-command (N)
  11677. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11678. If the cursor is in a table looking at whitespace, the whitespace is
  11679. overwritten, and the table is not marked as requiring realignment."
  11680. (interactive "p")
  11681. (if (and (org-table-p)
  11682. (progn
  11683. ;; check if we blank the field, and if that triggers align
  11684. (and (featurep 'org-table) org-table-auto-blank-field
  11685. (member last-command
  11686. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11687. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11688. ;; got extra space, this field does not determine column width
  11689. (let (org-table-may-need-update) (org-table-blank-field))
  11690. ;; no extra space, this field may determine column width
  11691. (org-table-blank-field)))
  11692. t)
  11693. (eq N 1)
  11694. (looking-at "[^|\n]* |"))
  11695. (let (org-table-may-need-update)
  11696. (goto-char (1- (match-end 0)))
  11697. (delete-backward-char 1)
  11698. (goto-char (match-beginning 0))
  11699. (self-insert-command N))
  11700. (setq org-table-may-need-update t)
  11701. (self-insert-command N)
  11702. (org-fix-tags-on-the-fly)))
  11703. (defun org-fix-tags-on-the-fly ()
  11704. (when (and (equal (char-after (point-at-bol)) ?*)
  11705. (org-on-heading-p))
  11706. (org-align-tags-here org-tags-column)))
  11707. (defun org-delete-backward-char (N)
  11708. "Like `delete-backward-char', insert whitespace at field end in tables.
  11709. When deleting backwards, in tables this function will insert whitespace in
  11710. front of the next \"|\" separator, to keep the table aligned. The table will
  11711. still be marked for re-alignment if the field did fill the entire column,
  11712. because, in this case the deletion might narrow the column."
  11713. (interactive "p")
  11714. (if (and (org-table-p)
  11715. (eq N 1)
  11716. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11717. (looking-at ".*?|"))
  11718. (let ((pos (point))
  11719. (noalign (looking-at "[^|\n\r]* |"))
  11720. (c org-table-may-need-update))
  11721. (backward-delete-char N)
  11722. (skip-chars-forward "^|")
  11723. (insert " ")
  11724. (goto-char (1- pos))
  11725. ;; noalign: if there were two spaces at the end, this field
  11726. ;; does not determine the width of the column.
  11727. (if noalign (setq org-table-may-need-update c)))
  11728. (backward-delete-char N)
  11729. (org-fix-tags-on-the-fly)))
  11730. (defun org-delete-char (N)
  11731. "Like `delete-char', but insert whitespace at field end in tables.
  11732. When deleting characters, in tables this function will insert whitespace in
  11733. front of the next \"|\" separator, to keep the table aligned. The table will
  11734. still be marked for re-alignment if the field did fill the entire column,
  11735. because, in this case the deletion might narrow the column."
  11736. (interactive "p")
  11737. (if (and (org-table-p)
  11738. (not (bolp))
  11739. (not (= (char-after) ?|))
  11740. (eq N 1))
  11741. (if (looking-at ".*?|")
  11742. (let ((pos (point))
  11743. (noalign (looking-at "[^|\n\r]* |"))
  11744. (c org-table-may-need-update))
  11745. (replace-match (concat
  11746. (substring (match-string 0) 1 -1)
  11747. " |"))
  11748. (goto-char pos)
  11749. ;; noalign: if there were two spaces at the end, this field
  11750. ;; does not determine the width of the column.
  11751. (if noalign (setq org-table-may-need-update c)))
  11752. (delete-char N))
  11753. (delete-char N)
  11754. (org-fix-tags-on-the-fly)))
  11755. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11756. (put 'org-self-insert-command 'delete-selection t)
  11757. (put 'orgtbl-self-insert-command 'delete-selection t)
  11758. (put 'org-delete-char 'delete-selection 'supersede)
  11759. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11760. ;; Make `flyspell-mode' delay after some commands
  11761. (put 'org-self-insert-command 'flyspell-delayed t)
  11762. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11763. (put 'org-delete-char 'flyspell-delayed t)
  11764. (put 'org-delete-backward-char 'flyspell-delayed t)
  11765. ;; Make pabbrev-mode expand after org-mode commands
  11766. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11767. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11768. ;; How to do this: Measure non-white length of current string
  11769. ;; If equal to column width, we should realign.
  11770. (defun org-remap (map &rest commands)
  11771. "In MAP, remap the functions given in COMMANDS.
  11772. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11773. (let (new old)
  11774. (while commands
  11775. (setq old (pop commands) new (pop commands))
  11776. (if (fboundp 'command-remapping)
  11777. (org-defkey map (vector 'remap old) new)
  11778. (substitute-key-definition old new map global-map)))))
  11779. (when (eq org-enable-table-editor 'optimized)
  11780. ;; If the user wants maximum table support, we need to hijack
  11781. ;; some standard editing functions
  11782. (org-remap org-mode-map
  11783. 'self-insert-command 'org-self-insert-command
  11784. 'delete-char 'org-delete-char
  11785. 'delete-backward-char 'org-delete-backward-char)
  11786. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11787. (defun org-shiftcursor-error ()
  11788. "Throw an error because Shift-Cursor command was applied in wrong context."
  11789. (error "This command is active in special context like tables, headlines or timestamps"))
  11790. (defun org-shifttab (&optional arg)
  11791. "Global visibility cycling or move to previous table field.
  11792. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11793. on context.
  11794. See the individual commands for more information."
  11795. (interactive "P")
  11796. (cond
  11797. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11798. ((integerp arg)
  11799. (message "Content view to level: %d" arg)
  11800. (org-content (prefix-numeric-value arg))
  11801. (setq org-cycle-global-status 'overview))
  11802. (t (call-interactively 'org-global-cycle))))
  11803. (defun org-shiftmetaleft ()
  11804. "Promote subtree or delete table column.
  11805. Calls `org-promote-subtree', `org-outdent-item',
  11806. or `org-table-delete-column', depending on context.
  11807. See the individual commands for more information."
  11808. (interactive)
  11809. (cond
  11810. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11811. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11812. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11813. (t (org-shiftcursor-error))))
  11814. (defun org-shiftmetaright ()
  11815. "Demote subtree or insert table column.
  11816. Calls `org-demote-subtree', `org-indent-item',
  11817. or `org-table-insert-column', depending on context.
  11818. See the individual commands for more information."
  11819. (interactive)
  11820. (cond
  11821. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11822. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11823. ((org-at-item-p) (call-interactively 'org-indent-item))
  11824. (t (org-shiftcursor-error))))
  11825. (defun org-shiftmetaup (&optional arg)
  11826. "Move subtree up or kill table row.
  11827. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11828. `org-move-item-up' depending on context. See the individual commands
  11829. for more information."
  11830. (interactive "P")
  11831. (cond
  11832. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11833. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11834. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11835. (t (org-shiftcursor-error))))
  11836. (defun org-shiftmetadown (&optional arg)
  11837. "Move subtree down or insert table row.
  11838. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11839. `org-move-item-down', depending on context. See the individual
  11840. commands for more information."
  11841. (interactive "P")
  11842. (cond
  11843. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11844. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11845. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11846. (t (org-shiftcursor-error))))
  11847. (defun org-metaleft (&optional arg)
  11848. "Promote heading or move table column to left.
  11849. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11850. With no specific context, calls the Emacs default `backward-word'.
  11851. See the individual commands for more information."
  11852. (interactive "P")
  11853. (cond
  11854. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11855. ((or (org-on-heading-p) (org-region-active-p))
  11856. (call-interactively 'org-do-promote))
  11857. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11858. (t (call-interactively 'backward-word))))
  11859. (defun org-metaright (&optional arg)
  11860. "Demote subtree or move table column to right.
  11861. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11862. With no specific context, calls the Emacs default `forward-word'.
  11863. See the individual commands for more information."
  11864. (interactive "P")
  11865. (cond
  11866. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11867. ((or (org-on-heading-p) (org-region-active-p))
  11868. (call-interactively 'org-do-demote))
  11869. ((org-at-item-p) (call-interactively 'org-indent-item))
  11870. (t (call-interactively 'forward-word))))
  11871. (defun org-metaup (&optional arg)
  11872. "Move subtree up or move table row up.
  11873. Calls `org-move-subtree-up' or `org-table-move-row' or
  11874. `org-move-item-up', depending on context. See the individual commands
  11875. for more information."
  11876. (interactive "P")
  11877. (cond
  11878. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11879. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11880. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11881. (t (transpose-lines 1) (beginning-of-line -1))))
  11882. (defun org-metadown (&optional arg)
  11883. "Move subtree down or move table row down.
  11884. Calls `org-move-subtree-down' or `org-table-move-row' or
  11885. `org-move-item-down', depending on context. See the individual
  11886. commands for more information."
  11887. (interactive "P")
  11888. (cond
  11889. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11890. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11891. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11892. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11893. (defun org-shiftup (&optional arg)
  11894. "Increase item in timestamp or increase priority of current headline.
  11895. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11896. depending on context. See the individual commands for more information."
  11897. (interactive "P")
  11898. (cond
  11899. ((org-at-timestamp-p t)
  11900. (call-interactively (if org-edit-timestamp-down-means-later
  11901. 'org-timestamp-down 'org-timestamp-up)))
  11902. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11903. ((org-at-item-p) (call-interactively 'org-previous-item))
  11904. ((org-clocktable-try-shift 'up arg))
  11905. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11906. (defun org-shiftdown (&optional arg)
  11907. "Decrease item in timestamp or decrease priority of current headline.
  11908. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11909. depending on context. See the individual commands for more information."
  11910. (interactive "P")
  11911. (cond
  11912. ((org-at-timestamp-p t)
  11913. (call-interactively (if org-edit-timestamp-down-means-later
  11914. 'org-timestamp-up 'org-timestamp-down)))
  11915. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11916. ((org-clocktable-try-shift 'down arg))
  11917. (t (call-interactively 'org-next-item))))
  11918. (defun org-shiftright (&optional arg)
  11919. "Next TODO keyword or timestamp one day later, depending on context."
  11920. (interactive "P")
  11921. (cond
  11922. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11923. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11924. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11925. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11926. ((org-clocktable-try-shift 'right arg))
  11927. (t (org-shiftcursor-error))))
  11928. (defun org-shiftleft (&optional arg)
  11929. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11930. (interactive "P")
  11931. (cond
  11932. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11933. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11934. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11935. ((org-at-property-p)
  11936. (call-interactively 'org-property-previous-allowed-value))
  11937. ((org-clocktable-try-shift 'left arg))
  11938. (t (org-shiftcursor-error))))
  11939. (defun org-shiftcontrolright ()
  11940. "Switch to next TODO set."
  11941. (interactive)
  11942. (cond
  11943. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11944. (t (org-shiftcursor-error))))
  11945. (defun org-shiftcontrolleft ()
  11946. "Switch to previous TODO set."
  11947. (interactive)
  11948. (cond
  11949. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11950. (t (org-shiftcursor-error))))
  11951. (defun org-ctrl-c-ret ()
  11952. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11953. (interactive)
  11954. (cond
  11955. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11956. (t (call-interactively 'org-insert-heading))))
  11957. (defun org-copy-special ()
  11958. "Copy region in table or copy current subtree.
  11959. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11960. See the individual commands for more information."
  11961. (interactive)
  11962. (call-interactively
  11963. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11964. (defun org-cut-special ()
  11965. "Cut region in table or cut current subtree.
  11966. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11967. See the individual commands for more information."
  11968. (interactive)
  11969. (call-interactively
  11970. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11971. (defun org-paste-special (arg)
  11972. "Paste rectangular region into table, or past subtree relative to level.
  11973. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11974. See the individual commands for more information."
  11975. (interactive "P")
  11976. (if (org-at-table-p)
  11977. (org-table-paste-rectangle)
  11978. (org-paste-subtree arg)))
  11979. (defun org-edit-special ()
  11980. "Call a special editor for the stuff at point.
  11981. When at a table, call the formula editor with `org-table-edit-formulas'.
  11982. When at the first line of an src example, call `org-edit-src-code'.
  11983. When in an #+include line, visit the include file. Otherwise call
  11984. `ffap' to visit the file at point."
  11985. (interactive)
  11986. (cond
  11987. ((org-at-table-p)
  11988. (call-interactively 'org-table-edit-formulas))
  11989. ((save-excursion
  11990. (beginning-of-line 1)
  11991. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11992. (find-file (org-trim (match-string 1))))
  11993. ((org-edit-src-code))
  11994. ((org-edit-fixed-width-region))
  11995. (t (call-interactively 'ffap))))
  11996. (defun org-ctrl-c-ctrl-c (&optional arg)
  11997. "Set tags in headline, or update according to changed information at point.
  11998. This command does many different things, depending on context:
  11999. - If the cursor is in a headline, prompt for tags and insert them
  12000. into the current line, aligned to `org-tags-column'. When called
  12001. with prefix arg, realign all tags in the current buffer.
  12002. - If the cursor is in one of the special #+KEYWORD lines, this
  12003. triggers scanning the buffer for these lines and updating the
  12004. information.
  12005. - If the cursor is inside a table, realign the table. This command
  12006. works even if the automatic table editor has been turned off.
  12007. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12008. the entire table.
  12009. - If the cursor is a the beginning of a dynamic block, update it.
  12010. - If the cursor is inside a table created by the table.el package,
  12011. activate that table.
  12012. - If the current buffer is a remember buffer, close note and file it.
  12013. with a prefix argument, file it without further interaction to the default
  12014. location.
  12015. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12016. links in this buffer.
  12017. - If the cursor is on a numbered item in a plain list, renumber the
  12018. ordered list.
  12019. - If the cursor is on a checkbox, toggle it."
  12020. (interactive "P")
  12021. (let ((org-enable-table-editor t))
  12022. (cond
  12023. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12024. org-occur-highlights
  12025. org-latex-fragment-image-overlays)
  12026. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  12027. (org-remove-occur-highlights)
  12028. (org-remove-latex-fragment-image-overlays)
  12029. (message "Temporary highlights/overlays removed from current buffer"))
  12030. ((and (local-variable-p 'org-finish-function (current-buffer))
  12031. (fboundp org-finish-function))
  12032. (funcall org-finish-function))
  12033. ((org-at-property-p)
  12034. (call-interactively 'org-property-action))
  12035. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12036. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12037. ((org-at-table.el-p)
  12038. (require 'table)
  12039. (beginning-of-line 1)
  12040. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12041. (call-interactively 'table-recognize-table))
  12042. ((org-at-table-p)
  12043. (org-table-maybe-eval-formula)
  12044. (if arg
  12045. (call-interactively 'org-table-recalculate)
  12046. (org-table-maybe-recalculate-line))
  12047. (call-interactively 'org-table-align))
  12048. ((org-at-item-checkbox-p)
  12049. (call-interactively 'org-toggle-checkbox))
  12050. ((org-at-item-p)
  12051. (call-interactively 'org-maybe-renumber-ordered-list))
  12052. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12053. ;; Dynamic block
  12054. (beginning-of-line 1)
  12055. (save-excursion (org-update-dblock)))
  12056. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12057. (cond
  12058. ((equal (match-string 1) "TBLFM")
  12059. ;; Recalculate the table before this line
  12060. (save-excursion
  12061. (beginning-of-line 1)
  12062. (skip-chars-backward " \r\n\t")
  12063. (if (org-at-table-p)
  12064. (org-call-with-arg 'org-table-recalculate t))))
  12065. (t
  12066. ; (org-set-regexps-and-options)
  12067. ; (org-restart-font-lock)
  12068. (let ((org-inhibit-startup t)) (org-mode-restart))
  12069. (message "Local setup has been refreshed"))))
  12070. (t (error "C-c C-c can do nothing useful at this location.")))))
  12071. (defun org-mode-restart ()
  12072. "Restart Org-mode, to scan again for special lines.
  12073. Also updates the keyword regular expressions."
  12074. (interactive)
  12075. (org-mode)
  12076. (message "Org-mode restarted"))
  12077. (defun org-kill-note-or-show-branches ()
  12078. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12079. (interactive)
  12080. (if (not org-finish-function)
  12081. (call-interactively 'show-branches)
  12082. (let ((org-note-abort t))
  12083. (funcall org-finish-function))))
  12084. (defun org-return (&optional indent)
  12085. "Goto next table row or insert a newline.
  12086. Calls `org-table-next-row' or `newline', depending on context.
  12087. See the individual commands for more information."
  12088. (interactive)
  12089. (cond
  12090. ((bobp) (if indent (newline-and-indent) (newline)))
  12091. ((and (org-at-heading-p)
  12092. (looking-at
  12093. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12094. (org-show-entry)
  12095. (end-of-line 1)
  12096. (newline))
  12097. ((org-at-table-p)
  12098. (org-table-justify-field-maybe)
  12099. (call-interactively 'org-table-next-row))
  12100. (t (if indent (newline-and-indent) (newline)))))
  12101. (defun org-return-indent ()
  12102. "Goto next table row or insert a newline and indent.
  12103. Calls `org-table-next-row' or `newline-and-indent', depending on
  12104. context. See the individual commands for more information."
  12105. (interactive)
  12106. (org-return t))
  12107. (defun org-ctrl-c-star ()
  12108. "Compute table, or change heading status of lines.
  12109. Calls `org-table-recalculate' or `org-toggle-region-headings',
  12110. depending on context. This will also turn a plain list item or a normal
  12111. line into a subheading."
  12112. (interactive)
  12113. (cond
  12114. ((org-at-table-p)
  12115. (call-interactively 'org-table-recalculate))
  12116. ((org-region-active-p)
  12117. ;; Convert all lines in region to list items
  12118. (call-interactively 'org-toggle-region-headings))
  12119. ((org-on-heading-p)
  12120. (org-toggle-region-headings (point-at-bol)
  12121. (min (1+ (point-at-eol)) (point-max))))
  12122. ((org-at-item-p)
  12123. ;; Convert to heading
  12124. (let ((level (save-match-data
  12125. (save-excursion
  12126. (condition-case nil
  12127. (progn
  12128. (org-back-to-heading t)
  12129. (funcall outline-level))
  12130. (error 0))))))
  12131. (replace-match
  12132. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12133. (t (org-toggle-region-headings (point-at-bol)
  12134. (min (1+ (point-at-eol)) (point-max))))))
  12135. (defun org-ctrl-c-minus ()
  12136. "Insert separator line in table or modify bullet status of line.
  12137. Also turns a plain line or a region of lines into list items.
  12138. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12139. `org-cycle-list-bullet', depending on context."
  12140. (interactive)
  12141. (cond
  12142. ((org-at-table-p)
  12143. (call-interactively 'org-table-insert-hline))
  12144. ((org-on-heading-p)
  12145. ;; Convert to item
  12146. (save-excursion
  12147. (beginning-of-line 1)
  12148. (if (looking-at "\\*+ ")
  12149. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12150. ((org-region-active-p)
  12151. ;; Convert all lines in region to list items
  12152. (call-interactively 'org-toggle-region-items))
  12153. ((org-in-item-p)
  12154. (call-interactively 'org-cycle-list-bullet))
  12155. (t (org-toggle-region-items (point-at-bol)
  12156. (min (1+ (point-at-eol)) (point-max))))))
  12157. (defun org-toggle-region-items (beg end)
  12158. "Convert all lines in region to list items.
  12159. If the first line is already an item, convert all list items in the region
  12160. to normal lines."
  12161. (interactive "r")
  12162. (let (l2 l)
  12163. (save-excursion
  12164. (goto-char end)
  12165. (setq l2 (org-current-line))
  12166. (goto-char beg)
  12167. (beginning-of-line 1)
  12168. (setq l (1- (org-current-line)))
  12169. (if (org-at-item-p)
  12170. ;; We already have items, de-itemize
  12171. (while (< (setq l (1+ l)) l2)
  12172. (when (org-at-item-p)
  12173. (goto-char (match-beginning 2))
  12174. (delete-region (match-beginning 2) (match-end 2))
  12175. (and (looking-at "[ \t]+") (replace-match "")))
  12176. (beginning-of-line 2))
  12177. (while (< (setq l (1+ l)) l2)
  12178. (unless (org-at-item-p)
  12179. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12180. (replace-match "\\1- \\2")))
  12181. (beginning-of-line 2))))))
  12182. (defun org-toggle-region-headings (beg end)
  12183. "Convert all lines in region to list items.
  12184. If the first line is already an item, convert all list items in the region
  12185. to normal lines."
  12186. (interactive "r")
  12187. (let (l2 l)
  12188. (save-excursion
  12189. (goto-char end)
  12190. (setq l2 (org-current-line))
  12191. (goto-char beg)
  12192. (beginning-of-line 1)
  12193. (setq l (1- (org-current-line)))
  12194. (if (org-on-heading-p)
  12195. ;; We already have headlines, de-star them
  12196. (while (< (setq l (1+ l)) l2)
  12197. (when (org-on-heading-p t)
  12198. (and (looking-at outline-regexp) (replace-match "")))
  12199. (beginning-of-line 2))
  12200. (let* ((stars (save-excursion
  12201. (re-search-backward org-complex-heading-regexp nil t)
  12202. (or (match-string 1) "*")))
  12203. (add-stars (if org-odd-levels-only "**" "*"))
  12204. (rpl (concat stars add-stars " \\2")))
  12205. (while (< (setq l (1+ l)) l2)
  12206. (unless (org-on-heading-p)
  12207. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12208. (replace-match rpl)))
  12209. (beginning-of-line 2)))))))
  12210. (defun org-meta-return (&optional arg)
  12211. "Insert a new heading or wrap a region in a table.
  12212. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12213. See the individual commands for more information."
  12214. (interactive "P")
  12215. (cond
  12216. ((org-at-table-p)
  12217. (call-interactively 'org-table-wrap-region))
  12218. (t (call-interactively 'org-insert-heading))))
  12219. ;;; Menu entries
  12220. ;; Define the Org-mode menus
  12221. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12222. '("Tbl"
  12223. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12224. ["Next Field" org-cycle (org-at-table-p)]
  12225. ["Previous Field" org-shifttab (org-at-table-p)]
  12226. ["Next Row" org-return (org-at-table-p)]
  12227. "--"
  12228. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12229. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12230. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12231. "--"
  12232. ("Column"
  12233. ["Move Column Left" org-metaleft (org-at-table-p)]
  12234. ["Move Column Right" org-metaright (org-at-table-p)]
  12235. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12236. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12237. ("Row"
  12238. ["Move Row Up" org-metaup (org-at-table-p)]
  12239. ["Move Row Down" org-metadown (org-at-table-p)]
  12240. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12241. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12242. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12243. "--"
  12244. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12245. ("Rectangle"
  12246. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12247. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12248. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12249. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12250. "--"
  12251. ("Calculate"
  12252. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12253. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12254. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12255. "--"
  12256. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12257. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12258. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12259. "--"
  12260. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12261. "--"
  12262. ["Sum Column/Rectangle" org-table-sum
  12263. (or (org-at-table-p) (org-region-active-p))]
  12264. ["Which Column?" org-table-current-column (org-at-table-p)])
  12265. ["Debug Formulas"
  12266. org-table-toggle-formula-debugger
  12267. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12268. ["Show Col/Row Numbers"
  12269. org-table-toggle-coordinate-overlays
  12270. :style toggle
  12271. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12272. "--"
  12273. ["Create" org-table-create (and (not (org-at-table-p))
  12274. org-enable-table-editor)]
  12275. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12276. ["Import from File" org-table-import (not (org-at-table-p))]
  12277. ["Export to File" org-table-export (org-at-table-p)]
  12278. "--"
  12279. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12280. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12281. '("Org"
  12282. ("Show/Hide"
  12283. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12284. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12285. ["Sparse Tree..." org-sparse-tree t]
  12286. ["Reveal Context" org-reveal t]
  12287. ["Show All" show-all t]
  12288. "--"
  12289. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12290. "--"
  12291. ["New Heading" org-insert-heading t]
  12292. ("Navigate Headings"
  12293. ["Up" outline-up-heading t]
  12294. ["Next" outline-next-visible-heading t]
  12295. ["Previous" outline-previous-visible-heading t]
  12296. ["Next Same Level" outline-forward-same-level t]
  12297. ["Previous Same Level" outline-backward-same-level t]
  12298. "--"
  12299. ["Jump" org-goto t])
  12300. ("Edit Structure"
  12301. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12302. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12303. "--"
  12304. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12305. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12306. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12307. "--"
  12308. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12309. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12310. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12311. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12312. "--"
  12313. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12314. "--"
  12315. ["Convert to odd levels" org-convert-to-odd-levels t]
  12316. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12317. ("Editing"
  12318. ["Emphasis..." org-emphasize t]
  12319. ["Edit Source Example" org-edit-special t])
  12320. ("Archive"
  12321. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12322. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12323. ; :active t :keys "C-u C-c C-x C-a"]
  12324. ["Sparse trees open ARCHIVE trees"
  12325. (setq org-sparse-tree-open-archived-trees
  12326. (not org-sparse-tree-open-archived-trees))
  12327. :style toggle :selected org-sparse-tree-open-archived-trees]
  12328. ["Cycling opens ARCHIVE trees"
  12329. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12330. :style toggle :selected org-cycle-open-archived-trees]
  12331. "--"
  12332. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12333. ; ["Check and Move Children" (org-archive-subtree '(4))
  12334. ; :active t :keys "C-u C-c C-x C-s"]
  12335. )
  12336. "--"
  12337. ("TODO Lists"
  12338. ["TODO/DONE/-" org-todo t]
  12339. ("Select keyword"
  12340. ["Next keyword" org-shiftright (org-on-heading-p)]
  12341. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12342. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12343. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12344. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12345. ["Show TODO Tree" org-show-todo-tree t]
  12346. ["Global TODO list" org-todo-list t]
  12347. "--"
  12348. ["Set Priority" org-priority t]
  12349. ["Priority Up" org-shiftup t]
  12350. ["Priority Down" org-shiftdown t])
  12351. ("TAGS and Properties"
  12352. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  12353. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12354. "--"
  12355. ["Set property" 'org-set-property t]
  12356. ["Column view of properties" org-columns t]
  12357. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12358. ("Dates and Scheduling"
  12359. ["Timestamp" org-time-stamp t]
  12360. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12361. ("Change Date"
  12362. ["1 Day Later" org-shiftright t]
  12363. ["1 Day Earlier" org-shiftleft t]
  12364. ["1 ... Later" org-shiftup t]
  12365. ["1 ... Earlier" org-shiftdown t])
  12366. ["Compute Time Range" org-evaluate-time-range t]
  12367. ["Schedule Item" org-schedule t]
  12368. ["Deadline" org-deadline t]
  12369. "--"
  12370. ["Custom time format" org-toggle-time-stamp-overlays
  12371. :style radio :selected org-display-custom-times]
  12372. "--"
  12373. ["Goto Calendar" org-goto-calendar t]
  12374. ["Date from Calendar" org-date-from-calendar t])
  12375. ("Logging work"
  12376. ["Clock in" org-clock-in t]
  12377. ["Clock out" org-clock-out t]
  12378. ["Clock cancel" org-clock-cancel t]
  12379. ["Goto running clock" org-clock-goto t]
  12380. ["Display times" org-clock-display t]
  12381. ["Create clock table" org-clock-report t]
  12382. "--"
  12383. ["Record DONE time"
  12384. (progn (setq org-log-done (not org-log-done))
  12385. (message "Switching to %s will %s record a timestamp"
  12386. (car org-done-keywords)
  12387. (if org-log-done "automatically" "not")))
  12388. :style toggle :selected org-log-done])
  12389. "--"
  12390. ["Agenda Command..." org-agenda t]
  12391. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12392. ("File List for Agenda")
  12393. ("Special views current file"
  12394. ["TODO Tree" org-show-todo-tree t]
  12395. ["Check Deadlines" org-check-deadlines t]
  12396. ["Timeline" org-timeline t]
  12397. ["Tags Tree" org-tags-sparse-tree t])
  12398. "--"
  12399. ("Hyperlinks"
  12400. ["Store Link (Global)" org-store-link t]
  12401. ["Insert Link" org-insert-link t]
  12402. ["Follow Link" org-open-at-point t]
  12403. "--"
  12404. ["Next link" org-next-link t]
  12405. ["Previous link" org-previous-link t]
  12406. "--"
  12407. ["Descriptive Links"
  12408. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12409. :style radio
  12410. :selected (member '(org-link) buffer-invisibility-spec)]
  12411. ["Literal Links"
  12412. (progn
  12413. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12414. :style radio
  12415. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12416. "--"
  12417. ["Export/Publish..." org-export t]
  12418. ("LaTeX"
  12419. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12420. :selected org-cdlatex-mode]
  12421. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12422. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12423. ["Modify math symbol" org-cdlatex-math-modify
  12424. (org-inside-LaTeX-fragment-p)]
  12425. ["Export LaTeX fragments as images"
  12426. (if (featurep 'org-exp)
  12427. (setq org-export-with-LaTeX-fragments
  12428. (not org-export-with-LaTeX-fragments))
  12429. (require 'org-exp))
  12430. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12431. org-export-with-LaTeX-fragments)])
  12432. "--"
  12433. ("Documentation"
  12434. ["Show Version" org-version t]
  12435. ["Info Documentation" org-info t])
  12436. ("Customize"
  12437. ["Browse Org Group" org-customize t]
  12438. "--"
  12439. ["Expand This Menu" org-create-customize-menu
  12440. (fboundp 'customize-menu-create)])
  12441. "--"
  12442. ["Refresh setup" org-mode-restart t]
  12443. ))
  12444. (defun org-info (&optional node)
  12445. "Read documentation for Org-mode in the info system.
  12446. With optional NODE, go directly to that node."
  12447. (interactive)
  12448. (info (format "(org)%s" (or node ""))))
  12449. (defun org-install-agenda-files-menu ()
  12450. (let ((bl (buffer-list)))
  12451. (save-excursion
  12452. (while bl
  12453. (set-buffer (pop bl))
  12454. (if (org-mode-p) (setq bl nil)))
  12455. (when (org-mode-p)
  12456. (easy-menu-change
  12457. '("Org") "File List for Agenda"
  12458. (append
  12459. (list
  12460. ["Edit File List" (org-edit-agenda-file-list) t]
  12461. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12462. ["Remove Current File from List" org-remove-file t]
  12463. ["Cycle through agenda files" org-cycle-agenda-files t]
  12464. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12465. "--")
  12466. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12467. ;;;; Documentation
  12468. ;;;###autoload
  12469. (defun org-require-autoloaded-modules ()
  12470. (interactive)
  12471. (mapc 'require
  12472. '(org-agenda org-archive org-clock org-colview
  12473. org-exp org-id org-export-latex org-publish
  12474. org-remember org-table)))
  12475. ;;;###autoload
  12476. (defun org-customize ()
  12477. "Call the customize function with org as argument."
  12478. (interactive)
  12479. (org-load-modules-maybe)
  12480. (org-require-autoloaded-modules)
  12481. (customize-browse 'org))
  12482. (defun org-create-customize-menu ()
  12483. "Create a full customization menu for Org-mode, insert it into the menu."
  12484. (interactive)
  12485. (org-load-modules-maybe)
  12486. (org-require-autoloaded-modules)
  12487. (if (fboundp 'customize-menu-create)
  12488. (progn
  12489. (easy-menu-change
  12490. '("Org") "Customize"
  12491. `(["Browse Org group" org-customize t]
  12492. "--"
  12493. ,(customize-menu-create 'org)
  12494. ["Set" Custom-set t]
  12495. ["Save" Custom-save t]
  12496. ["Reset to Current" Custom-reset-current t]
  12497. ["Reset to Saved" Custom-reset-saved t]
  12498. ["Reset to Standard Settings" Custom-reset-standard t]))
  12499. (message "\"Org\"-menu now contains full customization menu"))
  12500. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12501. ;;;; Miscellaneous stuff
  12502. ;;; Generally useful functions
  12503. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12504. "Display the given MESSAGE as a warning."
  12505. (if (fboundp 'display-warning)
  12506. (display-warning 'org message
  12507. (if (featurep 'xemacs)
  12508. 'warning
  12509. :warning))
  12510. (let ((buf (get-buffer-create "*Org warnings*")))
  12511. (with-current-buffer buf
  12512. (goto-char (point-max))
  12513. (insert "Warning (Org): " message)
  12514. (unless (bolp)
  12515. (newline)))
  12516. (display-buffer buf)
  12517. (sit-for 0))))
  12518. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12519. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12520. (if (and marker (marker-buffer marker)
  12521. (buffer-live-p (marker-buffer marker)))
  12522. (progn
  12523. (switch-to-buffer (marker-buffer marker))
  12524. (if (or (> marker (point-max)) (< marker (point-min)))
  12525. (widen))
  12526. (goto-char marker))
  12527. (if bookmark
  12528. (bookmark-jump bookmark)
  12529. (error "Cannot find location"))))
  12530. (defun org-quote-csv-field (s)
  12531. "Quote field for inclusion in CSV material."
  12532. (if (string-match "[\",]" s)
  12533. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12534. s))
  12535. (defun org-plist-delete (plist property)
  12536. "Delete PROPERTY from PLIST.
  12537. This is in contrast to merely setting it to 0."
  12538. (let (p)
  12539. (while plist
  12540. (if (not (eq property (car plist)))
  12541. (setq p (plist-put p (car plist) (nth 1 plist))))
  12542. (setq plist (cddr plist)))
  12543. p))
  12544. (defun org-force-self-insert (N)
  12545. "Needed to enforce self-insert under remapping."
  12546. (interactive "p")
  12547. (self-insert-command N))
  12548. (defun org-string-width (s)
  12549. "Compute width of string, ignoring invisible characters.
  12550. This ignores character with invisibility property `org-link', and also
  12551. characters with property `org-cwidth', because these will become invisible
  12552. upon the next fontification round."
  12553. (let (b l)
  12554. (when (or (eq t buffer-invisibility-spec)
  12555. (assq 'org-link buffer-invisibility-spec))
  12556. (while (setq b (text-property-any 0 (length s)
  12557. 'invisible 'org-link s))
  12558. (setq s (concat (substring s 0 b)
  12559. (substring s (or (next-single-property-change
  12560. b 'invisible s) (length s)))))))
  12561. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12562. (setq s (concat (substring s 0 b)
  12563. (substring s (or (next-single-property-change
  12564. b 'org-cwidth s) (length s))))))
  12565. (setq l (string-width s) b -1)
  12566. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12567. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12568. l))
  12569. (defun org-base-buffer (buffer)
  12570. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12571. (if (not buffer)
  12572. buffer
  12573. (or (buffer-base-buffer buffer)
  12574. buffer)))
  12575. (defun org-trim (s)
  12576. "Remove whitespace at beginning and end of string."
  12577. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12578. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12579. s)
  12580. (defun org-wrap (string &optional width lines)
  12581. "Wrap string to either a number of lines, or a width in characters.
  12582. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12583. that costs. If there is a word longer than WIDTH, the text is actually
  12584. wrapped to the length of that word.
  12585. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12586. many lines, whatever width that takes.
  12587. The return value is a list of lines, without newlines at the end."
  12588. (let* ((words (org-split-string string "[ \t\n]+"))
  12589. (maxword (apply 'max (mapcar 'org-string-width words)))
  12590. w ll)
  12591. (cond (width
  12592. (org-do-wrap words (max maxword width)))
  12593. (lines
  12594. (setq w maxword)
  12595. (setq ll (org-do-wrap words maxword))
  12596. (if (<= (length ll) lines)
  12597. ll
  12598. (setq ll words)
  12599. (while (> (length ll) lines)
  12600. (setq w (1+ w))
  12601. (setq ll (org-do-wrap words w)))
  12602. ll))
  12603. (t (error "Cannot wrap this")))))
  12604. (defun org-do-wrap (words width)
  12605. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12606. (let (lines line)
  12607. (while words
  12608. (setq line (pop words))
  12609. (while (and words (< (+ (length line) (length (car words))) width))
  12610. (setq line (concat line " " (pop words))))
  12611. (setq lines (push line lines)))
  12612. (nreverse lines)))
  12613. (defun org-split-string (string &optional separators)
  12614. "Splits STRING into substrings at SEPARATORS.
  12615. No empty strings are returned if there are matches at the beginning
  12616. and end of string."
  12617. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12618. (start 0)
  12619. notfirst
  12620. (list nil))
  12621. (while (and (string-match rexp string
  12622. (if (and notfirst
  12623. (= start (match-beginning 0))
  12624. (< start (length string)))
  12625. (1+ start) start))
  12626. (< (match-beginning 0) (length string)))
  12627. (setq notfirst t)
  12628. (or (eq (match-beginning 0) 0)
  12629. (and (eq (match-beginning 0) (match-end 0))
  12630. (eq (match-beginning 0) start))
  12631. (setq list
  12632. (cons (substring string start (match-beginning 0))
  12633. list)))
  12634. (setq start (match-end 0)))
  12635. (or (eq start (length string))
  12636. (setq list
  12637. (cons (substring string start)
  12638. list)))
  12639. (nreverse list)))
  12640. (defun org-context ()
  12641. "Return a list of contexts of the current cursor position.
  12642. If several contexts apply, all are returned.
  12643. Each context entry is a list with a symbol naming the context, and
  12644. two positions indicating start and end of the context. Possible
  12645. contexts are:
  12646. :headline anywhere in a headline
  12647. :headline-stars on the leading stars in a headline
  12648. :todo-keyword on a TODO keyword (including DONE) in a headline
  12649. :tags on the TAGS in a headline
  12650. :priority on the priority cookie in a headline
  12651. :item on the first line of a plain list item
  12652. :item-bullet on the bullet/number of a plain list item
  12653. :checkbox on the checkbox in a plain list item
  12654. :table in an org-mode table
  12655. :table-special on a special filed in a table
  12656. :table-table in a table.el table
  12657. :link on a hyperlink
  12658. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12659. :target on a <<target>>
  12660. :radio-target on a <<<radio-target>>>
  12661. :latex-fragment on a LaTeX fragment
  12662. :latex-preview on a LaTeX fragment with overlayed preview image
  12663. This function expects the position to be visible because it uses font-lock
  12664. faces as a help to recognize the following contexts: :table-special, :link,
  12665. and :keyword."
  12666. (let* ((f (get-text-property (point) 'face))
  12667. (faces (if (listp f) f (list f)))
  12668. (p (point)) clist o)
  12669. ;; First the large context
  12670. (cond
  12671. ((org-on-heading-p t)
  12672. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12673. (when (progn
  12674. (beginning-of-line 1)
  12675. (looking-at org-todo-line-tags-regexp))
  12676. (push (org-point-in-group p 1 :headline-stars) clist)
  12677. (push (org-point-in-group p 2 :todo-keyword) clist)
  12678. (push (org-point-in-group p 4 :tags) clist))
  12679. (goto-char p)
  12680. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12681. (if (looking-at "\\[#[A-Z0-9]\\]")
  12682. (push (org-point-in-group p 0 :priority) clist)))
  12683. ((org-at-item-p)
  12684. (push (org-point-in-group p 2 :item-bullet) clist)
  12685. (push (list :item (point-at-bol)
  12686. (save-excursion (org-end-of-item) (point)))
  12687. clist)
  12688. (and (org-at-item-checkbox-p)
  12689. (push (org-point-in-group p 0 :checkbox) clist)))
  12690. ((org-at-table-p)
  12691. (push (list :table (org-table-begin) (org-table-end)) clist)
  12692. (if (memq 'org-formula faces)
  12693. (push (list :table-special
  12694. (previous-single-property-change p 'face)
  12695. (next-single-property-change p 'face)) clist)))
  12696. ((org-at-table-p 'any)
  12697. (push (list :table-table) clist)))
  12698. (goto-char p)
  12699. ;; Now the small context
  12700. (cond
  12701. ((org-at-timestamp-p)
  12702. (push (org-point-in-group p 0 :timestamp) clist))
  12703. ((memq 'org-link faces)
  12704. (push (list :link
  12705. (previous-single-property-change p 'face)
  12706. (next-single-property-change p 'face)) clist))
  12707. ((memq 'org-special-keyword faces)
  12708. (push (list :keyword
  12709. (previous-single-property-change p 'face)
  12710. (next-single-property-change p 'face)) clist))
  12711. ((org-on-target-p)
  12712. (push (org-point-in-group p 0 :target) clist)
  12713. (goto-char (1- (match-beginning 0)))
  12714. (if (looking-at org-radio-target-regexp)
  12715. (push (org-point-in-group p 0 :radio-target) clist))
  12716. (goto-char p))
  12717. ((setq o (car (delq nil
  12718. (mapcar
  12719. (lambda (x)
  12720. (if (memq x org-latex-fragment-image-overlays) x))
  12721. (org-overlays-at (point))))))
  12722. (push (list :latex-fragment
  12723. (org-overlay-start o) (org-overlay-end o)) clist)
  12724. (push (list :latex-preview
  12725. (org-overlay-start o) (org-overlay-end o)) clist))
  12726. ((org-inside-LaTeX-fragment-p)
  12727. ;; FIXME: positions wrong.
  12728. (push (list :latex-fragment (point) (point)) clist)))
  12729. (setq clist (nreverse (delq nil clist)))
  12730. clist))
  12731. ;; FIXME: Compare with at-regexp-p Do we need both?
  12732. (defun org-in-regexp (re &optional nlines visually)
  12733. "Check if point is inside a match of regexp.
  12734. Normally only the current line is checked, but you can include NLINES extra
  12735. lines both before and after point into the search.
  12736. If VISUALLY is set, require that the cursor is not after the match but
  12737. really on, so that the block visually is on the match."
  12738. (catch 'exit
  12739. (let ((pos (point))
  12740. (eol (point-at-eol (+ 1 (or nlines 0))))
  12741. (inc (if visually 1 0)))
  12742. (save-excursion
  12743. (beginning-of-line (- 1 (or nlines 0)))
  12744. (while (re-search-forward re eol t)
  12745. (if (and (<= (match-beginning 0) pos)
  12746. (>= (+ inc (match-end 0)) pos))
  12747. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12748. (defun org-at-regexp-p (regexp)
  12749. "Is point inside a match of REGEXP in the current line?"
  12750. (catch 'exit
  12751. (save-excursion
  12752. (let ((pos (point)) (end (point-at-eol)))
  12753. (beginning-of-line 1)
  12754. (while (re-search-forward regexp end t)
  12755. (if (and (<= (match-beginning 0) pos)
  12756. (>= (match-end 0) pos))
  12757. (throw 'exit t)))
  12758. nil))))
  12759. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12760. "Call `multi-occur' with buffers for all agenda files."
  12761. (interactive "sOrg-files matching: \np")
  12762. (let* ((files (org-agenda-files))
  12763. (tnames (mapcar 'file-truename files))
  12764. (extra org-agenda-text-search-extra-files)
  12765. f)
  12766. (when (eq (car extra) 'agenda-archives)
  12767. (setq extra (cdr extra))
  12768. (setq files (org-add-archive-files files)))
  12769. (while (setq f (pop extra))
  12770. (unless (member (file-truename f) tnames)
  12771. (add-to-list 'files f 'append)
  12772. (add-to-list 'tnames (file-truename f) 'append)))
  12773. (multi-occur
  12774. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12775. regexp)))
  12776. (if (boundp 'occur-mode-find-occurrence-hook)
  12777. ;; Emacs 23
  12778. (add-hook 'occur-mode-find-occurrence-hook
  12779. (lambda ()
  12780. (when (org-mode-p)
  12781. (org-reveal))))
  12782. ;; Emacs 22
  12783. (defadvice occur-mode-goto-occurrence
  12784. (after org-occur-reveal activate)
  12785. (and (org-mode-p) (org-reveal)))
  12786. (defadvice occur-mode-goto-occurrence-other-window
  12787. (after org-occur-reveal activate)
  12788. (and (org-mode-p) (org-reveal)))
  12789. (defadvice occur-mode-display-occurrence
  12790. (after org-occur-reveal activate)
  12791. (when (org-mode-p)
  12792. (let ((pos (occur-mode-find-occurrence)))
  12793. (with-current-buffer (marker-buffer pos)
  12794. (save-excursion
  12795. (goto-char pos)
  12796. (org-reveal)))))))
  12797. (defun org-uniquify (list)
  12798. "Remove duplicate elements from LIST."
  12799. (let (res)
  12800. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12801. res))
  12802. (defun org-delete-all (elts list)
  12803. "Remove all elements in ELTS from LIST."
  12804. (while elts
  12805. (setq list (delete (pop elts) list)))
  12806. list)
  12807. (defun org-back-over-empty-lines ()
  12808. "Move backwards over witespace, to the beginning of the first empty line.
  12809. Returns the number of empty lines passed."
  12810. (let ((pos (point)))
  12811. (skip-chars-backward " \t\n\r")
  12812. (beginning-of-line 2)
  12813. (goto-char (min (point) pos))
  12814. (count-lines (point) pos)))
  12815. (defun org-skip-whitespace ()
  12816. (skip-chars-forward " \t\n\r"))
  12817. (defun org-point-in-group (point group &optional context)
  12818. "Check if POINT is in match-group GROUP.
  12819. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12820. match. If the match group does ot exist or point is not inside it,
  12821. return nil."
  12822. (and (match-beginning group)
  12823. (>= point (match-beginning group))
  12824. (<= point (match-end group))
  12825. (if context
  12826. (list context (match-beginning group) (match-end group))
  12827. t)))
  12828. (defun org-switch-to-buffer-other-window (&rest args)
  12829. "Switch to buffer in a second window on the current frame.
  12830. In particular, do not allow pop-up frames."
  12831. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12832. special-display-function)
  12833. (apply 'switch-to-buffer-other-window args)))
  12834. (defun org-combine-plists (&rest plists)
  12835. "Create a single property list from all plists in PLISTS.
  12836. The process starts by copying the first list, and then setting properties
  12837. from the other lists. Settings in the last list are the most significant
  12838. ones and overrule settings in the other lists."
  12839. (let ((rtn (copy-sequence (pop plists)))
  12840. p v ls)
  12841. (while plists
  12842. (setq ls (pop plists))
  12843. (while ls
  12844. (setq p (pop ls) v (pop ls))
  12845. (setq rtn (plist-put rtn p v))))
  12846. rtn))
  12847. (defun org-move-line-down (arg)
  12848. "Move the current line down. With prefix argument, move it past ARG lines."
  12849. (interactive "p")
  12850. (let ((col (current-column))
  12851. beg end pos)
  12852. (beginning-of-line 1) (setq beg (point))
  12853. (beginning-of-line 2) (setq end (point))
  12854. (beginning-of-line (+ 1 arg))
  12855. (setq pos (move-marker (make-marker) (point)))
  12856. (insert (delete-and-extract-region beg end))
  12857. (goto-char pos)
  12858. (org-move-to-column col)))
  12859. (defun org-move-line-up (arg)
  12860. "Move the current line up. With prefix argument, move it past ARG lines."
  12861. (interactive "p")
  12862. (let ((col (current-column))
  12863. beg end pos)
  12864. (beginning-of-line 1) (setq beg (point))
  12865. (beginning-of-line 2) (setq end (point))
  12866. (beginning-of-line (- arg))
  12867. (setq pos (move-marker (make-marker) (point)))
  12868. (insert (delete-and-extract-region beg end))
  12869. (goto-char pos)
  12870. (org-move-to-column col)))
  12871. (defun org-replace-escapes (string table)
  12872. "Replace %-escapes in STRING with values in TABLE.
  12873. TABLE is an association list with keys like \"%a\" and string values.
  12874. The sequences in STRING may contain normal field width and padding information,
  12875. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12876. so values can contain further %-escapes if they are define later in TABLE."
  12877. (let ((case-fold-search nil)
  12878. e re rpl)
  12879. (while (setq e (pop table))
  12880. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12881. (while (string-match re string)
  12882. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12883. (cdr e)))
  12884. (setq string (replace-match rpl t t string))))
  12885. string))
  12886. (defun org-sublist (list start end)
  12887. "Return a section of LIST, from START to END.
  12888. Counting starts at 1."
  12889. (let (rtn (c start))
  12890. (setq list (nthcdr (1- start) list))
  12891. (while (and list (<= c end))
  12892. (push (pop list) rtn)
  12893. (setq c (1+ c)))
  12894. (nreverse rtn)))
  12895. (defun org-find-base-buffer-visiting (file)
  12896. "Like `find-buffer-visiting' but alway return the base buffer and
  12897. not an indirect buffer."
  12898. (let ((buf (find-buffer-visiting file)))
  12899. (if buf
  12900. (or (buffer-base-buffer buf) buf)
  12901. nil)))
  12902. (defun org-image-file-name-regexp ()
  12903. "Return regexp matching the file names of images."
  12904. (if (fboundp 'image-file-name-regexp)
  12905. (image-file-name-regexp)
  12906. (let ((image-file-name-extensions
  12907. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12908. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12909. (concat "\\."
  12910. (regexp-opt (nconc (mapcar 'upcase
  12911. image-file-name-extensions)
  12912. image-file-name-extensions)
  12913. t)
  12914. "\\'"))))
  12915. (defun org-file-image-p (file)
  12916. "Return non-nil if FILE is an image."
  12917. (save-match-data
  12918. (string-match (org-image-file-name-regexp) file)))
  12919. (defun org-get-cursor-date ()
  12920. "Return the date at cursor in as a time.
  12921. This works in the calendar and in the agenda, anywhere else it just
  12922. returns the current time."
  12923. (let (date day defd)
  12924. (cond
  12925. ((eq major-mode 'calendar-mode)
  12926. (setq date (calendar-cursor-to-date)
  12927. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12928. ((eq major-mode 'org-agenda-mode)
  12929. (setq day (get-text-property (point) 'day))
  12930. (if day
  12931. (setq date (calendar-gregorian-from-absolute day)
  12932. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12933. (nth 2 date))))))
  12934. (or defd (current-time))))
  12935. (defvar org-agenda-action-marker (make-marker)
  12936. "Marker pointing to the entry for the next agenda action.")
  12937. (defun org-mark-entry-for-agenda-action ()
  12938. "Mark the current entry as target of an agenda action.
  12939. Agenda actions are actions executed from the agenda with the key `k',
  12940. which make use of the date at the cursor."
  12941. (interactive)
  12942. (move-marker org-agenda-action-marker
  12943. (save-excursion (org-back-to-heading t) (point))
  12944. (current-buffer))
  12945. (message
  12946. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12947. ;;; Paragraph filling stuff.
  12948. ;; We want this to be just right, so use the full arsenal.
  12949. (defun org-indent-line-function ()
  12950. "Indent line like previous, but further if previous was headline or item."
  12951. (interactive)
  12952. (let* ((pos (point))
  12953. (itemp (org-at-item-p))
  12954. column bpos bcol tpos tcol bullet btype bullet-type)
  12955. ;; Find the previous relevant line
  12956. (beginning-of-line 1)
  12957. (cond
  12958. ((looking-at "#") (setq column 0))
  12959. ((looking-at "\\*+ ") (setq column 0))
  12960. (t
  12961. (beginning-of-line 0)
  12962. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12963. (beginning-of-line 0))
  12964. (cond
  12965. ((looking-at "\\*+[ \t]+")
  12966. (if (not org-adapt-indentation)
  12967. (setq column 0)
  12968. (goto-char (match-end 0))
  12969. (setq column (current-column))))
  12970. ((org-in-item-p)
  12971. (org-beginning-of-item)
  12972. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12973. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12974. (setq bpos (match-beginning 1) tpos (match-end 0)
  12975. bcol (progn (goto-char bpos) (current-column))
  12976. tcol (progn (goto-char tpos) (current-column))
  12977. bullet (match-string 1)
  12978. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12979. (if (> tcol (+ bcol org-description-max-indent))
  12980. (setq tcol (+ bcol 5)))
  12981. (if (not itemp)
  12982. (setq column tcol)
  12983. (goto-char pos)
  12984. (beginning-of-line 1)
  12985. (if (looking-at "\\S-")
  12986. (progn
  12987. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12988. (setq bullet (match-string 1)
  12989. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12990. (setq column (if (equal btype bullet-type) bcol tcol)))
  12991. (setq column (org-get-indentation)))))
  12992. (t (setq column (org-get-indentation))))))
  12993. (goto-char pos)
  12994. (if (<= (current-column) (current-indentation))
  12995. (org-indent-line-to column)
  12996. (save-excursion (org-indent-line-to column)))
  12997. (setq column (current-column))
  12998. (beginning-of-line 1)
  12999. (if (looking-at
  13000. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13001. (replace-match (concat "\\1" (format org-property-format
  13002. (match-string 2) (match-string 3)))
  13003. t nil))
  13004. (org-move-to-column column)))
  13005. (defun org-set-autofill-regexps ()
  13006. (interactive)
  13007. ;; In the paragraph separator we include headlines, because filling
  13008. ;; text in a line directly attached to a headline would otherwise
  13009. ;; fill the headline as well.
  13010. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13011. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13012. ;; The paragraph starter includes hand-formatted lists.
  13013. (org-set-local 'paragraph-start
  13014. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13015. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13016. ;; But only if the user has not turned off tables or fixed-width regions
  13017. (org-set-local
  13018. 'auto-fill-inhibit-regexp
  13019. (concat "\\*+ \\|#\\+"
  13020. "\\|[ \t]*" org-keyword-time-regexp
  13021. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13022. (concat
  13023. "\\|[ \t]*["
  13024. (if org-enable-table-editor "|" "")
  13025. (if org-enable-fixed-width-editor ":" "")
  13026. "]"))))
  13027. ;; We use our own fill-paragraph function, to make sure that tables
  13028. ;; and fixed-width regions are not wrapped. That function will pass
  13029. ;; through to `fill-paragraph' when appropriate.
  13030. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13031. ; Adaptive filling: To get full control, first make sure that
  13032. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13033. (org-set-local 'adaptive-fill-regexp "\000")
  13034. (org-set-local 'adaptive-fill-function
  13035. 'org-adaptive-fill-function)
  13036. (org-set-local
  13037. 'align-mode-rules-list
  13038. '((org-in-buffer-settings
  13039. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13040. (modes . '(org-mode))))))
  13041. (defun org-fill-paragraph (&optional justify)
  13042. "Re-align a table, pass through to fill-paragraph if no table."
  13043. (let ((table-p (org-at-table-p))
  13044. (table.el-p (org-at-table.el-p)))
  13045. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13046. (save-excursion (goto-char (point-at-bol))
  13047. (looking-at outline-regexp)))
  13048. t) ; skip headlines
  13049. (table.el-p t) ; skip table.el tables
  13050. (table-p (org-table-align) t) ; align org-mode tables
  13051. (t nil)))) ; call paragraph-fill
  13052. ;; For reference, this is the default value of adaptive-fill-regexp
  13053. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13054. (defun org-adaptive-fill-function ()
  13055. "Return a fill prefix for org-mode files.
  13056. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13057. work correctly."
  13058. (cond ((looking-at "#[ \t]+")
  13059. (match-string 0))
  13060. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13061. (save-excursion
  13062. (if (> (match-end 1) (+ (match-beginning 1)
  13063. org-description-max-indent))
  13064. (goto-char (+ (match-beginning 1) 5))
  13065. (goto-char (match-end 0)))
  13066. (make-string (current-column) ?\ )))
  13067. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  13068. (save-excursion
  13069. (goto-char (match-end 0))
  13070. (make-string (current-column) ?\ )))
  13071. (t nil)))
  13072. ;;; Other stuff.
  13073. (defun org-toggle-fixed-width-section (arg)
  13074. "Toggle the fixed-width export.
  13075. If there is no active region, the QUOTE keyword at the current headline is
  13076. inserted or removed. When present, it causes the text between this headline
  13077. and the next to be exported as fixed-width text, and unmodified.
  13078. If there is an active region, this command adds or removes a colon as the
  13079. first character of this line. If the first character of a line is a colon,
  13080. this line is also exported in fixed-width font."
  13081. (interactive "P")
  13082. (let* ((cc 0)
  13083. (regionp (org-region-active-p))
  13084. (beg (if regionp (region-beginning) (point)))
  13085. (end (if regionp (region-end)))
  13086. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13087. (case-fold-search nil)
  13088. (re "[ \t]*\\(:\\)")
  13089. off)
  13090. (if regionp
  13091. (save-excursion
  13092. (goto-char beg)
  13093. (setq cc (current-column))
  13094. (beginning-of-line 1)
  13095. (setq off (looking-at re))
  13096. (while (> nlines 0)
  13097. (setq nlines (1- nlines))
  13098. (beginning-of-line 1)
  13099. (cond
  13100. (arg
  13101. (org-move-to-column cc t)
  13102. (insert ":\n")
  13103. (forward-line -1))
  13104. ((and off (looking-at re))
  13105. (replace-match "" t t nil 1))
  13106. ((not off) (org-move-to-column cc t) (insert ":")))
  13107. (forward-line 1)))
  13108. (save-excursion
  13109. (org-back-to-heading)
  13110. (if (looking-at (concat outline-regexp
  13111. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13112. (replace-match "" t t nil 1)
  13113. (if (looking-at outline-regexp)
  13114. (progn
  13115. (goto-char (match-end 0))
  13116. (insert org-quote-string " "))))))))
  13117. ;;;; Functions extending outline functionality
  13118. (defun org-beginning-of-line (&optional arg)
  13119. "Go to the beginning of the current line. If that is invisible, continue
  13120. to a visible line beginning. This makes the function of C-a more intuitive.
  13121. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13122. first attempt, and only move to after the tags when the cursor is already
  13123. beyond the end of the headline."
  13124. (interactive "P")
  13125. (let ((pos (point)) refpos)
  13126. (beginning-of-line 1)
  13127. (if (bobp)
  13128. nil
  13129. (backward-char 1)
  13130. (if (org-invisible-p)
  13131. (while (and (not (bobp)) (org-invisible-p))
  13132. (backward-char 1)
  13133. (beginning-of-line 1))
  13134. (forward-char 1)))
  13135. (when org-special-ctrl-a/e
  13136. (cond
  13137. ((and (looking-at org-complex-heading-regexp)
  13138. (= (char-after (match-end 1)) ?\ ))
  13139. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13140. (point-at-eol)))
  13141. (goto-char
  13142. (if (eq org-special-ctrl-a/e t)
  13143. (cond ((> pos refpos) refpos)
  13144. ((= pos (point)) refpos)
  13145. (t (point)))
  13146. (cond ((> pos (point)) (point))
  13147. ((not (eq last-command this-command)) (point))
  13148. (t refpos)))))
  13149. ((org-at-item-p)
  13150. (goto-char
  13151. (if (eq org-special-ctrl-a/e t)
  13152. (cond ((> pos (match-end 4)) (match-end 4))
  13153. ((= pos (point)) (match-end 4))
  13154. (t (point)))
  13155. (cond ((> pos (point)) (point))
  13156. ((not (eq last-command this-command)) (point))
  13157. (t (match-end 4))))))))
  13158. (org-no-warnings
  13159. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13160. (defun org-end-of-line (&optional arg)
  13161. "Go to the end of the line.
  13162. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13163. first attempt, and only move to after the tags when the cursor is already
  13164. beyond the end of the headline."
  13165. (interactive "P")
  13166. (if (or (not org-special-ctrl-a/e)
  13167. (not (org-on-heading-p)))
  13168. (end-of-line arg)
  13169. (let ((pos (point)))
  13170. (beginning-of-line 1)
  13171. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13172. (if (eq org-special-ctrl-a/e t)
  13173. (if (or (< pos (match-beginning 1))
  13174. (= pos (match-end 0)))
  13175. (goto-char (match-beginning 1))
  13176. (goto-char (match-end 0)))
  13177. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13178. (goto-char (match-end 0))
  13179. (goto-char (match-beginning 1))))
  13180. (end-of-line arg))))
  13181. (org-no-warnings
  13182. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13183. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13184. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13185. (defun org-kill-line (&optional arg)
  13186. "Kill line, to tags or end of line."
  13187. (interactive "P")
  13188. (cond
  13189. ((or (not org-special-ctrl-k)
  13190. (bolp)
  13191. (not (org-on-heading-p)))
  13192. (call-interactively 'kill-line))
  13193. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13194. (kill-region (point) (match-beginning 1))
  13195. (org-set-tags nil t))
  13196. (t (kill-region (point) (point-at-eol)))))
  13197. (define-key org-mode-map "\C-k" 'org-kill-line)
  13198. (defun org-invisible-p ()
  13199. "Check if point is at a character currently not visible."
  13200. ;; Early versions of noutline don't have `outline-invisible-p'.
  13201. (if (fboundp 'outline-invisible-p)
  13202. (outline-invisible-p)
  13203. (get-char-property (point) 'invisible)))
  13204. (defun org-invisible-p2 ()
  13205. "Check if point is at a character currently not visible."
  13206. (save-excursion
  13207. (if (and (eolp) (not (bobp))) (backward-char 1))
  13208. ;; Early versions of noutline don't have `outline-invisible-p'.
  13209. (if (fboundp 'outline-invisible-p)
  13210. (outline-invisible-p)
  13211. (get-char-property (point) 'invisible))))
  13212. (defalias 'org-back-to-heading 'outline-back-to-heading)
  13213. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13214. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13215. (defun org-at-heading-or-item-p ()
  13216. (or (org-on-heading-p) (org-at-item-p)))
  13217. (defun org-on-target-p ()
  13218. (or (org-in-regexp org-radio-target-regexp)
  13219. (org-in-regexp org-target-regexp)))
  13220. (defun org-up-heading-all (arg)
  13221. "Move to the heading line of which the present line is a subheading.
  13222. This function considers both visible and invisible heading lines.
  13223. With argument, move up ARG levels."
  13224. (if (fboundp 'outline-up-heading-all)
  13225. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13226. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13227. (defun org-up-heading-safe ()
  13228. "Move to the heading line of which the present line is a subheading.
  13229. This version will not throw an error. It will return the level of the
  13230. headline found, or nil if no higher level is found."
  13231. (let ((pos (point)) start-level level
  13232. (re (concat "^" outline-regexp)))
  13233. (catch 'exit
  13234. (outline-back-to-heading t)
  13235. (setq start-level (funcall outline-level))
  13236. (if (equal start-level 1) (throw 'exit nil))
  13237. (while (re-search-backward re nil t)
  13238. (setq level (funcall outline-level))
  13239. (if (< level start-level) (throw 'exit level)))
  13240. nil)))
  13241. (defun org-first-sibling-p ()
  13242. "Is this heading the first child of its parents?"
  13243. (interactive)
  13244. (let ((re (concat "^" outline-regexp))
  13245. level l)
  13246. (unless (org-at-heading-p t)
  13247. (error "Not at a heading"))
  13248. (setq level (funcall outline-level))
  13249. (save-excursion
  13250. (if (not (re-search-backward re nil t))
  13251. t
  13252. (setq l (funcall outline-level))
  13253. (< l level)))))
  13254. (defun org-goto-sibling (&optional previous)
  13255. "Goto the next sibling, even if it is invisible.
  13256. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13257. when a sibling was found. When none is found, return nil and don't
  13258. move point."
  13259. (let ((fun (if previous 're-search-backward 're-search-forward))
  13260. (pos (point))
  13261. (re (concat "^" outline-regexp))
  13262. level l)
  13263. (when (condition-case nil (org-back-to-heading t) (error nil))
  13264. (setq level (funcall outline-level))
  13265. (catch 'exit
  13266. (or previous (forward-char 1))
  13267. (while (funcall fun re nil t)
  13268. (setq l (funcall outline-level))
  13269. (when (< l level) (goto-char pos) (throw 'exit nil))
  13270. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13271. (goto-char pos)
  13272. nil))))
  13273. (defun org-show-siblings ()
  13274. "Show all siblings of the current headline."
  13275. (save-excursion
  13276. (while (org-goto-sibling) (org-flag-heading nil)))
  13277. (save-excursion
  13278. (while (org-goto-sibling 'previous)
  13279. (org-flag-heading nil))))
  13280. (defun org-show-hidden-entry ()
  13281. "Show an entry where even the heading is hidden."
  13282. (save-excursion
  13283. (org-show-entry)))
  13284. (defun org-flag-heading (flag &optional entry)
  13285. "Flag the current heading. FLAG non-nil means make invisible.
  13286. When ENTRY is non-nil, show the entire entry."
  13287. (save-excursion
  13288. (org-back-to-heading t)
  13289. ;; Check if we should show the entire entry
  13290. (if entry
  13291. (progn
  13292. (org-show-entry)
  13293. (save-excursion
  13294. (and (outline-next-heading)
  13295. (org-flag-heading nil))))
  13296. (outline-flag-region (max (point-min) (1- (point)))
  13297. (save-excursion (outline-end-of-heading) (point))
  13298. flag))))
  13299. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13300. ;; This is an exact copy of the original function, but it uses
  13301. ;; `org-back-to-heading', to make it work also in invisible
  13302. ;; trees. And is uses an invisible-OK argument.
  13303. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13304. (org-back-to-heading invisible-OK)
  13305. (let ((first t)
  13306. (level (funcall outline-level)))
  13307. (while (and (not (eobp))
  13308. (or first (> (funcall outline-level) level)))
  13309. (setq first nil)
  13310. (outline-next-heading))
  13311. (unless to-heading
  13312. (if (memq (preceding-char) '(?\n ?\^M))
  13313. (progn
  13314. ;; Go to end of line before heading
  13315. (forward-char -1)
  13316. (if (memq (preceding-char) '(?\n ?\^M))
  13317. ;; leave blank line before heading
  13318. (forward-char -1))))))
  13319. (point))
  13320. (defun org-show-subtree ()
  13321. "Show everything after this heading at deeper levels."
  13322. (outline-flag-region
  13323. (point)
  13324. (save-excursion
  13325. (outline-end-of-subtree) (outline-next-heading) (point))
  13326. nil))
  13327. (defun org-show-entry ()
  13328. "Show the body directly following this heading.
  13329. Show the heading too, if it is currently invisible."
  13330. (interactive)
  13331. (save-excursion
  13332. (condition-case nil
  13333. (progn
  13334. (org-back-to-heading t)
  13335. (outline-flag-region
  13336. (max (point-min) (1- (point)))
  13337. (save-excursion
  13338. (re-search-forward
  13339. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13340. (or (match-beginning 1) (point-max)))
  13341. nil))
  13342. (error nil))))
  13343. (defun org-make-options-regexp (kwds)
  13344. "Make a regular expression for keyword lines."
  13345. (concat
  13346. "^"
  13347. "#?[ \t]*\\+\\("
  13348. (mapconcat 'regexp-quote kwds "\\|")
  13349. "\\):[ \t]*"
  13350. "\\(.+\\)"))
  13351. ;; Make isearch reveal the necessary context
  13352. (defun org-isearch-end ()
  13353. "Reveal context after isearch exits."
  13354. (when isearch-success ; only if search was successful
  13355. (if (featurep 'xemacs)
  13356. ;; Under XEmacs, the hook is run in the correct place,
  13357. ;; we directly show the context.
  13358. (org-show-context 'isearch)
  13359. ;; In Emacs the hook runs *before* restoring the overlays.
  13360. ;; So we have to use a one-time post-command-hook to do this.
  13361. ;; (Emacs 22 has a special variable, see function `org-mode')
  13362. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13363. isearch-mode-end-hook-quit)
  13364. ;; Only when the isearch was not quitted.
  13365. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13366. 'append 'local)))))
  13367. (defun org-isearch-post-command ()
  13368. "Remove self from hook, and show context."
  13369. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13370. (org-show-context 'isearch))
  13371. ;;;; Integration with and fixes for other packages
  13372. ;;; Imenu support
  13373. (defvar org-imenu-markers nil
  13374. "All markers currently used by Imenu.")
  13375. (make-variable-buffer-local 'org-imenu-markers)
  13376. (defun org-imenu-new-marker (&optional pos)
  13377. "Return a new marker for use by Imenu, and remember the marker."
  13378. (let ((m (make-marker)))
  13379. (move-marker m (or pos (point)))
  13380. (push m org-imenu-markers)
  13381. m))
  13382. (defun org-imenu-get-tree ()
  13383. "Produce the index for Imenu."
  13384. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13385. (setq org-imenu-markers nil)
  13386. (let* ((n org-imenu-depth)
  13387. (re (concat "^" outline-regexp))
  13388. (subs (make-vector (1+ n) nil))
  13389. (last-level 0)
  13390. m tree level head)
  13391. (save-excursion
  13392. (save-restriction
  13393. (widen)
  13394. (goto-char (point-max))
  13395. (while (re-search-backward re nil t)
  13396. (setq level (org-reduced-level (funcall outline-level)))
  13397. (when (<= level n)
  13398. (looking-at org-complex-heading-regexp)
  13399. (setq head (org-match-string-no-properties 4)
  13400. m (org-imenu-new-marker))
  13401. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13402. (if (>= level last-level)
  13403. (push (cons head m) (aref subs level))
  13404. (push (cons head (aref subs (1+ level))) (aref subs level))
  13405. (loop for i from (1+ level) to n do (aset subs i nil)))
  13406. (setq last-level level)))))
  13407. (aref subs 1)))
  13408. (eval-after-load "imenu"
  13409. '(progn
  13410. (add-hook 'imenu-after-jump-hook
  13411. (lambda ()
  13412. (if (eq major-mode 'org-mode)
  13413. (org-show-context 'org-goto))))))
  13414. ;; Speedbar support
  13415. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13416. "Overlay marking the agenda restriction line in speedbar.")
  13417. (org-overlay-put org-speedbar-restriction-lock-overlay
  13418. 'face 'org-agenda-restriction-lock)
  13419. (org-overlay-put org-speedbar-restriction-lock-overlay
  13420. 'help-echo "Agendas are currently limited to this item.")
  13421. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13422. (defun org-speedbar-set-agenda-restriction ()
  13423. "Restrict future agenda commands to the location at point in speedbar.
  13424. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13425. (interactive)
  13426. (require 'org-agenda)
  13427. (let (p m tp np dir txt w)
  13428. (cond
  13429. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13430. 'org-imenu t))
  13431. (setq m (get-text-property p 'org-imenu-marker))
  13432. (save-excursion
  13433. (save-restriction
  13434. (set-buffer (marker-buffer m))
  13435. (goto-char m)
  13436. (org-agenda-set-restriction-lock 'subtree))))
  13437. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13438. 'speedbar-function 'speedbar-find-file))
  13439. (setq tp (previous-single-property-change
  13440. (1+ p) 'speedbar-function)
  13441. np (next-single-property-change
  13442. tp 'speedbar-function)
  13443. dir (speedbar-line-directory)
  13444. txt (buffer-substring-no-properties (or tp (point-min))
  13445. (or np (point-max))))
  13446. (save-excursion
  13447. (save-restriction
  13448. (set-buffer (find-file-noselect
  13449. (let ((default-directory dir))
  13450. (expand-file-name txt))))
  13451. (unless (org-mode-p)
  13452. (error "Cannot restrict to non-Org-mode file"))
  13453. (org-agenda-set-restriction-lock 'file))))
  13454. (t (error "Don't know how to restrict Org-mode's agenda")))
  13455. (org-move-overlay org-speedbar-restriction-lock-overlay
  13456. (point-at-bol) (point-at-eol))
  13457. (setq current-prefix-arg nil)
  13458. (org-agenda-maybe-redo)))
  13459. (eval-after-load "speedbar"
  13460. '(progn
  13461. (speedbar-add-supported-extension ".org")
  13462. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13463. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13464. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13465. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13466. (add-hook 'speedbar-visiting-tag-hook
  13467. (lambda () (org-show-context 'org-goto)))))
  13468. ;;; Fixes and Hacks for problems with other packages
  13469. ;; Make flyspell not check words in links, to not mess up our keymap
  13470. (defun org-mode-flyspell-verify ()
  13471. "Don't let flyspell put overlays at active buttons."
  13472. (not (get-text-property (point) 'keymap)))
  13473. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13474. (eval-after-load "bookmark"
  13475. '(if (boundp 'bookmark-after-jump-hook)
  13476. ;; We can use the hook
  13477. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13478. ;; Hook not available, use advice
  13479. (defadvice bookmark-jump (after org-make-visible activate)
  13480. "Make the position visible."
  13481. (org-bookmark-jump-unhide))))
  13482. (defun org-bookmark-jump-unhide ()
  13483. "Unhide the current position, to show the bookmark location."
  13484. (and (org-mode-p)
  13485. (or (org-invisible-p)
  13486. (save-excursion (goto-char (max (point-min) (1- (point))))
  13487. (org-invisible-p)))
  13488. (org-show-context 'bookmark-jump)))
  13489. ;; Make session.el ignore our circular variable
  13490. (eval-after-load "session"
  13491. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13492. ;;;; Experimental code
  13493. (defun org-closed-in-range ()
  13494. "Sparse tree of items closed in a certain time range.
  13495. Still experimental, may disappear in the future."
  13496. (interactive)
  13497. ;; Get the time interval from the user.
  13498. (let* ((time1 (time-to-seconds
  13499. (org-read-date nil 'to-time nil "Starting date: ")))
  13500. (time2 (time-to-seconds
  13501. (org-read-date nil 'to-time nil "End date:")))
  13502. ;; callback function
  13503. (callback (lambda ()
  13504. (let ((time
  13505. (time-to-seconds
  13506. (apply 'encode-time
  13507. (org-parse-time-string
  13508. (match-string 1))))))
  13509. ;; check if time in interval
  13510. (and (>= time time1) (<= time time2))))))
  13511. ;; make tree, check each match with the callback
  13512. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13513. ;;;; Finish up
  13514. (provide 'org)
  13515. (run-hooks 'org-load-hook)
  13516. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13517. ;;; org.el ends here